| 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,
|
| 98 |
(int *) 0)) != -1)
|
| 99 |
switch (opt) {
|
| 100 |
case 'a':
|
| 101 |
levels = rc_runlevel_list ();
|
| 102 |
break;
|
| 103 |
case 'l':
|
| 104 |
levels = rc_runlevel_list ();
|
| 105 |
STRLIST_FOREACH (levels, level, i)
|
| 106 |
printf ("%s\n", level);
|
| 107 |
rc_strlist_free (levels);
|
| 108 |
exit (EXIT_SUCCESS);
|
| 109 |
case 's':
|
| 110 |
services = rc_services_in_runlevel (NULL);
|
| 111 |
STRLIST_FOREACH (services, service, i)
|
| 112 |
print_service (service);
|
| 113 |
rc_strlist_free (services);
|
| 114 |
exit (EXIT_SUCCESS);
|
| 115 |
case 'u':
|
| 116 |
services = rc_services_in_runlevel (NULL);
|
| 117 |
levels = rc_runlevel_list ();
|
| 118 |
STRLIST_FOREACH (services, service, i) {
|
| 119 |
bool found = false;
|
| 120 |
STRLIST_FOREACH (levels, level, j)
|
| 121 |
if (rc_service_in_runlevel (service, level)) {
|
| 122 |
found = true;
|
| 123 |
break;
|
| 124 |
}
|
| 125 |
if (! found)
|
| 126 |
print_service (service);
|
| 127 |
}
|
| 128 |
rc_strlist_free (levels);
|
| 129 |
rc_strlist_free (services);
|
| 130 |
exit (EXIT_SUCCESS);
|
| 131 |
|
| 132 |
case_RC_COMMON_GETOPT
|
| 133 |
}
|
| 134 |
|
| 135 |
while (optind < argc)
|
| 136 |
rc_strlist_add (&levels, argv[optind++]);
|
| 137 |
|
| 138 |
if (! levels) {
|
| 139 |
level = rc_runlevel_get ();
|
| 140 |
rc_strlist_add (&levels, level);
|
| 141 |
free (level);
|
| 142 |
}
|
| 143 |
|
| 144 |
/* Output the services in the order in which they would start */
|
| 145 |
if (geteuid () == 0)
|
| 146 |
deptree = _rc_deptree_load ();
|
| 147 |
else
|
| 148 |
deptree = rc_deptree_load ();
|
| 149 |
|
| 150 |
STRLIST_FOREACH (levels, level, i) {
|
| 151 |
print_level (level);
|
| 152 |
services = rc_services_in_runlevel (level);
|
| 153 |
if (deptree) {
|
| 154 |
ordered = rc_deptree_depends (deptree, types_nua,
|
| 155 |
(const char **) services,
|
| 156 |
level, depopts);
|
| 157 |
rc_strlist_free (services);
|
| 158 |
services = ordered;
|
| 159 |
ordered = NULL;
|
| 160 |
}
|
| 161 |
STRLIST_FOREACH (services, service, j)
|
| 162 |
if (rc_service_in_runlevel (service, level))
|
| 163 |
print_service (service);
|
| 164 |
rc_strlist_free (services);
|
| 165 |
}
|
| 166 |
|
| 167 |
rc_strlist_free (levels);
|
| 168 |
rc_deptree_free (deptree);
|
| 169 |
|
| 170 |
return (EXIT_SUCCESS);
|
| 171 |
}
|