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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3040 - (show annotations) (download) (as text)
Mon Oct 15 14:40:53 2007 UTC (6 years, 9 months ago) by uberlord
File MIME type: text/x-csrc
File size: 4230 byte(s)
const correctness
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 const char *types_nua[] = { "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 int depopts = RC_DEP_STRICT | RC_DEP_START | RC_DEP_TRACE;
96
97 while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1)
98 switch (opt) {
99 case 'a':
100 levels = rc_runlevel_list ();
101 break;
102 case 'l':
103 levels = rc_runlevel_list ();
104 STRLIST_FOREACH (levels, level, i)
105 printf ("%s\n", level);
106 rc_strlist_free (levels);
107 exit (EXIT_SUCCESS);
108 case 's':
109 services = rc_services_in_runlevel (NULL);
110 STRLIST_FOREACH (services, service, i)
111 print_service (service);
112 rc_strlist_free (services);
113 exit (EXIT_SUCCESS);
114 case 'u':
115 services = rc_services_in_runlevel (NULL);
116 levels = rc_runlevel_list ();
117 STRLIST_FOREACH (services, service, i) {
118 bool found = false;
119 STRLIST_FOREACH (levels, level, j)
120 if (rc_service_in_runlevel (service, level)) {
121 found = true;
122 break;
123 }
124 if (! found)
125 print_service (service);
126 }
127 rc_strlist_free (levels);
128 rc_strlist_free (services);
129 exit (EXIT_SUCCESS);
130
131 case_RC_COMMON_GETOPT
132 }
133
134 while (optind < argc)
135 rc_strlist_add (&levels, argv[optind++]);
136
137 if (! levels) {
138 level = rc_runlevel_get ();
139 rc_strlist_add (&levels, level);
140 free (level);
141 }
142
143 /* Output the services in the order in which they would start */
144 deptree = rc_deptree_load ();
145 STRLIST_FOREACH (levels, level, i) {
146 print_level (level);
147 services = rc_services_in_runlevel (level);
148 if (deptree) {
149 ordered = rc_deptree_depends (deptree, types_nua,
150 (const char **) services,
151 level, depopts);
152 rc_strlist_free (services);
153 services = ordered;
154 ordered = NULL;
155 }
156 STRLIST_FOREACH (services, service, j)
157 if (rc_service_in_runlevel (service, level))
158 print_service (service);
159 rc_strlist_free (services);
160 }
161
162 rc_strlist_free (levels);
163 rc_deptree_free (deptree);
164
165 return (EXIT_SUCCESS);
166 }

  ViewVC Help
Powered by ViewVC 1.1.20