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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2882 - (show annotations) (download) (as text)
Tue Sep 18 11:36:55 2007 UTC (7 years ago) by uberlord
File MIME type: text/x-csrc
File size: 3362 byte(s)
    API change! rc_strlist_add and friends now take char *** instead of
    char ** and return a pointer to the item added instead of the new
    list head. This is so we can easily tell if the item was successfully
    added or not instead of iterating through the list looking for it.

    list = rc_strlist_add (list, item);
    becomes
    rc_strlist_add (&list, item);
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 void print_level (char *level)
23 {
24 printf ("Runlevel: %s%s%s\n",
25 ecolor (ecolor_hilite),
26 level,
27 ecolor (ecolor_normal));
28 }
29
30 static void print_service (char *service)
31 {
32 char status[10];
33 int cols = printf (" %s\n", service);
34 einfo_color_t color = ecolor_bad;
35
36 if (rc_service_state (service, rc_service_stopping))
37 snprintf (status, sizeof (status), "stopping ");
38 else if (rc_service_state (service, rc_service_starting)) {
39 snprintf (status, sizeof (status), "starting ");
40 color = ecolor_warn;
41 } else if (rc_service_state (service, rc_service_inactive)) {
42 snprintf (status, sizeof (status), "inactive ");
43 color = ecolor_warn;
44 } else if (geteuid () == 0 && rc_service_state (service, rc_service_crashed))
45 snprintf (status, sizeof (status), " crashed ");
46 else if (rc_service_state (service, rc_service_started)) {
47 snprintf (status, sizeof (status), " started ");
48 color = ecolor_good;
49 } else if (rc_service_state (service, rc_service_scheduled)) {
50 snprintf (status, sizeof (status), "scheduled");
51 color = ecolor_warn;
52 } else
53 snprintf (status, sizeof (status), " stopped ");
54 ebracket (cols, color, status);
55 }
56
57 #include "_usage.h"
58 #define getoptstring "alsu" getoptstring_COMMON
59 static const struct option longopts[] = {
60 {"all", 0, NULL, 'a'},
61 {"list", 0, NULL, 'l'},
62 {"servicelist", 0, NULL, 's'},
63 {"unused", 0, NULL, 'u'},
64 longopts_COMMON
65 {NULL, 0, NULL, 0}
66 };
67 #include "_usage.c"
68
69 int rc_status (int argc, char **argv)
70 {
71 char **levels = NULL;
72 char **services = NULL;
73 char *level;
74 char *service;
75 int opt;
76 int i;
77 int j;
78
79 while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1)
80 switch (opt) {
81 case 'a':
82 levels = rc_get_runlevels ();
83 break;
84 case 'l':
85 levels = rc_get_runlevels ();
86 STRLIST_FOREACH (levels, level, i)
87 printf ("%s\n", level);
88 rc_strlist_free (levels);
89 exit (EXIT_SUCCESS);
90 case 's':
91 services = rc_services_in_runlevel (NULL);
92 STRLIST_FOREACH (services, service, i)
93 print_service (service);
94 rc_strlist_free (services);
95 exit (EXIT_SUCCESS);
96 case 'u':
97 services = rc_services_in_runlevel (NULL);
98 levels = rc_get_runlevels ();
99 STRLIST_FOREACH (services, service, i) {
100 bool found = false;
101 STRLIST_FOREACH (levels, level, j)
102 if (rc_service_in_runlevel (service, level)) {
103 found = true;
104 break;
105 }
106 if (! found)
107 print_service (service);
108 }
109 rc_strlist_free (levels);
110 rc_strlist_free (services);
111 exit (EXIT_SUCCESS);
112
113 case_RC_COMMON_GETOPT
114 }
115
116 while (optind < argc)
117 rc_strlist_add (&levels, argv[optind++]);
118
119 if (! levels)
120 rc_strlist_add (&levels, rc_get_runlevel ());
121
122 STRLIST_FOREACH (levels, level, i) {
123 print_level (level);
124 services = rc_services_in_runlevel (level);
125 STRLIST_FOREACH (services, service, j)
126 print_service (service);
127 rc_strlist_free (services);
128 }
129
130 rc_strlist_free (levels);
131
132 return (EXIT_SUCCESS);
133 }

  ViewVC Help
Powered by ViewVC 1.1.20