/[baselayout]/trunk/src/rc-status.c
Gentoo

Contents of /trunk/src/rc-status.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3038 - (show annotations) (download) (as text)
Mon Oct 15 11:17:57 2007 UTC (6 years, 11 months ago) by uberlord
File MIME type: text/x-csrc
File size: 4165 byte(s)
rc-status now orders services in start order, #195630
1 /*
2 rc-status
3 Display the status of the services in runlevels
4 Copyright 2007 Gentoo Foundation
5 Released under the GPLv2
6 */
7
8 #include <getopt.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include "builtins.h"
15 #include "einfo.h"
16 #include "rc.h"
17 #include "rc-misc.h"
18 #include "strlist.h"
19
20 #define APPLET "rc-status"
21
22 static char const *types[] = { "ineed", "iuse", "iafter", NULL };
23
24 static void print_level (char *level)
25 {
26 printf ("Runlevel: %s%s%s\n",
27 ecolor (ECOLOR_HILITE),
28 level,
29 ecolor (ECOLOR_NORMAL));
30 }
31
32 static void print_service (char *service)
33 {
34 char status[10];
35 int cols = printf (" %s", service);
36 rc_service_state_t state = rc_service_state (service);
37 einfo_color_t color = ECOLOR_BAD;
38
39 if (state & RC_SERVICE_STOPPING)
40 snprintf (status, sizeof (status), "stopping ");
41 else if (state & RC_SERVICE_STARTING) {
42 snprintf (status, sizeof (status), "starting ");
43 color = ECOLOR_WARN;
44 } else if (state & RC_SERVICE_INACTIVE) {
45 snprintf (status, sizeof (status), "inactive ");
46 color = ECOLOR_WARN;
47 } else if (state & RC_SERVICE_STARTED) {
48 if (geteuid () == 0 && rc_service_daemons_crashed (service))
49 snprintf (status, sizeof (status), " crashed ");
50 else {
51 snprintf (status, sizeof (status), " started ");
52 color = ECOLOR_GOOD;
53 }
54 } else if (state & RC_SERVICE_SCHEDULED) {
55 snprintf (status, sizeof (status), "scheduled");
56 color = ECOLOR_WARN;
57 } else
58 snprintf (status, sizeof (status), " stopped ");
59
60 if (isatty (fileno (stdout)) && ! rc_env_bool ("RC_NOCOLOR"))
61 printf ("\n");
62 ebracket (cols, color, status);
63 }
64
65 #include "_usage.h"
66 #define extraopts "[runlevel1] [runlevel2] ..."
67 #define getoptstring "alsu" getoptstring_COMMON
68 static const struct option longopts[] = {
69 {"all", 0, NULL, 'a'},
70 {"list", 0, NULL, 'l'},
71 {"servicelist", 0, NULL, 's'},
72 {"unused", 0, NULL, 'u'},
73 longopts_COMMON
74 };
75 static const char * const longopts_help[] = {
76 "Show services from all run levels",
77 "Show list of run levels",
78 "Show service list",
79 "Show services not assigned to any runlevel",
80 longopts_help_COMMON
81 };
82 #include "_usage.c"
83
84 int rc_status (int argc, char **argv)
85 {
86 rc_depinfo_t *deptree = NULL;
87 char **levels = NULL;
88 char **services = NULL;
89 char **ordered = NULL;
90 char *level;
91 char *service;
92 int opt;
93 int i;
94 int j;
95
96 while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1)
97 switch (opt) {
98 case 'a':
99 levels = rc_runlevel_list ();
100 break;
101 case 'l':
102 levels = rc_runlevel_list ();
103 STRLIST_FOREACH (levels, level, i)
104 printf ("%s\n", level);
105 rc_strlist_free (levels);
106 exit (EXIT_SUCCESS);
107 case 's':
108 services = rc_services_in_runlevel (NULL);
109 STRLIST_FOREACH (services, service, i)
110 print_service (service);
111 rc_strlist_free (services);
112 exit (EXIT_SUCCESS);
113 case 'u':
114 services = rc_services_in_runlevel (NULL);
115 levels = rc_runlevel_list ();
116 STRLIST_FOREACH (services, service, i) {
117 bool found = false;
118 STRLIST_FOREACH (levels, level, j)
119 if (rc_service_in_runlevel (service, level)) {
120 found = true;
121 break;
122 }
123 if (! found)
124 print_service (service);
125 }
126 rc_strlist_free (levels);
127 rc_strlist_free (services);
128 exit (EXIT_SUCCESS);
129
130 case_RC_COMMON_GETOPT
131 }
132
133 while (optind < argc)
134 rc_strlist_add (&levels, argv[optind++]);
135
136 if (! levels) {
137 level = rc_runlevel_get ();
138 rc_strlist_add (&levels, level);
139 free (level);
140 }
141
142 /* Output the services in the order in which they would start */
143 deptree = rc_deptree_load ();
144 STRLIST_FOREACH (levels, level, i) {
145 print_level (level);
146 services = rc_services_in_runlevel (level);
147 if (deptree) {
148 ordered = rc_deptree_depends (deptree, (char **) types, services,
149 level, RC_DEP_STRICT | RC_DEP_START);
150 rc_strlist_free (services);
151 services = ordered;
152 ordered = NULL;
153 }
154 STRLIST_FOREACH (services, service, j)
155 if (rc_service_in_runlevel (service, level))
156 print_service (service);
157 rc_strlist_free (services);
158 }
159
160 rc_strlist_free (levels);
161 rc_deptree_free (deptree);
162
163 return (EXIT_SUCCESS);
164 }

  ViewVC Help
Powered by ViewVC 1.1.20