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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20