| 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", service); |
| 34 |
rc_service_state_t state = rc_service_state (service); |
| 35 |
einfo_color_t color = ECOLOR_BAD; |
| 36 |
|
| 37 |
if (state & RC_SERVICE_STOPPING) |
| 38 |
snprintf (status, sizeof (status), "stopping "); |
| 39 |
else if (state & RC_SERVICE_STARTING) { |
| 40 |
snprintf (status, sizeof (status), "starting "); |
| 41 |
color = ECOLOR_WARN; |
| 42 |
} else if (state & RC_SERVICE_INACTIVE) { |
| 43 |
snprintf (status, sizeof (status), "inactive "); |
| 44 |
color = ECOLOR_WARN; |
| 45 |
} else if (state & RC_SERVICE_STARTED) { |
| 46 |
if (geteuid () == 0 && rc_service_daemons_crashed (service)) |
| 47 |
snprintf (status, sizeof (status), " crashed "); |
| 48 |
else { |
| 49 |
snprintf (status, sizeof (status), " started "); |
| 50 |
color = ECOLOR_GOOD; |
| 51 |
} |
| 52 |
} else if (state & RC_SERVICE_SCHEDULED) { |
| 53 |
snprintf (status, sizeof (status), "scheduled"); |
| 54 |
color = ECOLOR_WARN; |
| 55 |
} else |
| 56 |
snprintf (status, sizeof (status), " stopped "); |
| 57 |
|
| 58 |
if (isatty (fileno (stdout)) && ! rc_env_bool ("RC_NOCOLOR")) |
| 59 |
printf ("\n"); |
| 60 |
ebracket (cols, color, status); |
| 61 |
} |
| 62 |
|
| 63 |
#include "_usage.h" |
| 64 |
#define extraopts "[runlevel1] [runlevel2] ..." |
| 65 |
#define getoptstring "alsu" getoptstring_COMMON |
| 66 |
static const struct option longopts[] = { |
| 67 |
{"all", 0, NULL, 'a'}, |
| 68 |
{"list", 0, NULL, 'l'}, |
| 69 |
{"servicelist", 0, NULL, 's'}, |
| 70 |
{"unused", 0, NULL, 'u'}, |
| 71 |
longopts_COMMON |
| 72 |
{NULL, 0, NULL, 0} |
| 73 |
}; |
| 74 |
static const char * const longopts_help[] = { |
| 75 |
"Show services from all run levels", |
| 76 |
"Show list of run levels", |
| 77 |
"Show service list", |
| 78 |
"Show services not assigned to any run level", |
| 79 |
longopts_help_COMMON |
| 80 |
}; |
| 81 |
#include "_usage.c" |
| 82 |
|
| 83 |
int rc_status (int argc, char **argv) |
| 84 |
{ |
| 85 |
char **levels = NULL; |
| 86 |
char **services = NULL; |
| 87 |
char *level; |
| 88 |
char *service; |
| 89 |
int opt; |
| 90 |
int i; |
| 91 |
int j; |
| 92 |
|
| 93 |
while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1) |
| 94 |
switch (opt) { |
| 95 |
case 'a': |
| 96 |
levels = rc_get_runlevels (); |
| 97 |
break; |
| 98 |
case 'l': |
| 99 |
levels = rc_get_runlevels (); |
| 100 |
STRLIST_FOREACH (levels, level, i) |
| 101 |
printf ("%s\n", level); |
| 102 |
rc_strlist_free (levels); |
| 103 |
exit (EXIT_SUCCESS); |
| 104 |
case 's': |
| 105 |
services = rc_services_in_runlevel (NULL); |
| 106 |
STRLIST_FOREACH (services, service, i) |
| 107 |
print_service (service); |
| 108 |
rc_strlist_free (services); |
| 109 |
exit (EXIT_SUCCESS); |
| 110 |
case 'u': |
| 111 |
services = rc_services_in_runlevel (NULL); |
| 112 |
levels = rc_get_runlevels (); |
| 113 |
STRLIST_FOREACH (services, service, i) { |
| 114 |
bool found = false; |
| 115 |
STRLIST_FOREACH (levels, level, j) |
| 116 |
if (rc_service_in_runlevel (service, level)) { |
| 117 |
found = true; |
| 118 |
break; |
| 119 |
} |
| 120 |
if (! found) |
| 121 |
print_service (service); |
| 122 |
} |
| 123 |
rc_strlist_free (levels); |
| 124 |
rc_strlist_free (services); |
| 125 |
exit (EXIT_SUCCESS); |
| 126 |
|
| 127 |
case_RC_COMMON_GETOPT |
| 128 |
} |
| 129 |
|
| 130 |
while (optind < argc) |
| 131 |
rc_strlist_add (&levels, argv[optind++]); |
| 132 |
|
| 133 |
if (! levels) { |
| 134 |
level = rc_runlevel_get (); |
| 135 |
rc_strlist_add (&levels, level); |
| 136 |
free (level); |
| 137 |
} |
| 138 |
|
| 139 |
STRLIST_FOREACH (levels, level, i) { |
| 140 |
print_level (level); |
| 141 |
services = rc_services_in_runlevel (level); |
| 142 |
STRLIST_FOREACH (services, service, j) |
| 143 |
print_service (service); |
| 144 |
rc_strlist_free (services); |
| 145 |
} |
| 146 |
|
| 147 |
rc_strlist_free (levels); |
| 148 |
|
| 149 |
return (EXIT_SUCCESS); |
| 150 |
} |