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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (show annotations) (download) (as text)
Thu Jun 28 15:44:14 2007 UTC (8 years, 1 month ago) by uberlord
File MIME type: text/x-csrc
File size: 3342 byte(s)
Add --nocolor to more programs
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 #define APPLET "rc-status"
9
10 #include <getopt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "einfo.h"
17 #include "rc.h"
18 #include "rc-misc.h"
19 #include "strlist.h"
20
21 static void print_level (char *level)
22 {
23 printf ("Runlevel: %s%s%s\n",
24 ecolor (ecolor_hilite),
25 level,
26 ecolor (ecolor_normal));
27 }
28
29 static void print_service (char *service)
30 {
31 char status[10];
32 int cols = printf (" %s\n", service);
33 einfo_color_t color = ecolor_bad;
34
35 if (rc_service_state (service, rc_service_stopping))
36 snprintf (status, sizeof (status), "stopping ");
37 else if (rc_service_state (service, rc_service_starting)) {
38 snprintf (status, sizeof (status), "starting ");
39 color = ecolor_warn;
40 } else if (rc_service_state (service, rc_service_inactive)) {
41 snprintf (status, sizeof (status), "inactive ");
42 color = ecolor_warn;
43 } else if (geteuid () == 0 && rc_service_state (service, rc_service_crashed))
44 snprintf (status, sizeof (status), " crashed ");
45 else if (rc_service_state (service, rc_service_started)) {
46 snprintf (status, sizeof (status), " started ");
47 color = ecolor_good;
48 } else if (rc_service_state (service, rc_service_scheduled)) {
49 snprintf (status, sizeof (status), "scheduled");
50 color = ecolor_warn;
51 } else
52 snprintf (status, sizeof (status), " stopped ");
53 ebracket (cols, color, status);
54 }
55
56 #include "_usage.h"
57 #define getoptstring "alsu" getoptstring_COMMON
58 const struct option longopts[] = {
59 {"all", 0, NULL, 'a'},
60 {"list", 0, NULL, 'l'},
61 {"servicelist", 0, NULL, 's'},
62 {"unused", 0, NULL, 'u'},
63 longopts_COMMON
64 {NULL, 0, NULL, 0}
65 };
66 #include "_usage.c"
67
68 int main (int argc, char **argv)
69 {
70 char **levels = NULL;
71 char **services = NULL;
72 char *level;
73 char *service;
74 int opt;
75 int i;
76 int j;
77
78 while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1)
79 switch (opt) {
80 case 'a':
81 levels = rc_get_runlevels ();
82 break;
83 case 'l':
84 levels = rc_get_runlevels ();
85 STRLIST_FOREACH (levels, level, i)
86 printf ("%s\n", level);
87 rc_strlist_free (levels);
88 exit (EXIT_SUCCESS);
89 case 's':
90 services = rc_services_in_runlevel (NULL);
91 STRLIST_FOREACH (services, service, i)
92 print_service (service);
93 rc_strlist_free (services);
94 exit (EXIT_SUCCESS);
95 case 'u':
96 services = rc_services_in_runlevel (NULL);
97 levels = rc_get_runlevels ();
98 STRLIST_FOREACH (services, service, i) {
99 bool found = false;
100 STRLIST_FOREACH (levels, level, j)
101 if (rc_service_in_runlevel (service, level)) {
102 found = true;
103 break;
104 }
105 if (! found)
106 print_service (service);
107 }
108 rc_strlist_free (levels);
109 rc_strlist_free (services);
110 exit (EXIT_SUCCESS);
111
112 case_RC_COMMON_GETOPT
113 }
114
115 while (optind < argc)
116 levels = rc_strlist_add (levels, argv[optind++]);
117
118 if (! levels)
119 levels = rc_strlist_add (NULL, rc_get_runlevel ());
120
121 STRLIST_FOREACH (levels, level, i) {
122 print_level (level);
123 services = rc_services_in_runlevel (level);
124 STRLIST_FOREACH (services, service, j)
125 print_service (service);
126 rc_strlist_free (services);
127 }
128
129 rc_strlist_free (levels);
130
131 return (EXIT_SUCCESS);
132 }

  ViewVC Help
Powered by ViewVC 1.1.20