| 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 |
#define getoptstring "alsuh"
|
| 57 |
const struct option longopts[] = {
|
| 58 |
{"all", 0, NULL, 'a'},
|
| 59 |
{"list", 0, NULL, 'l'},
|
| 60 |
{"servicelist", 0, NULL, 's'},
|
| 61 |
{"unused", 0, NULL, 'u'},
|
| 62 |
{"help", 0, NULL, 'h'},
|
| 63 |
{NULL, 0, NULL, 0}
|
| 64 |
};
|
| 65 |
#include "_usage.c"
|
| 66 |
|
| 67 |
int main (int argc, char **argv)
|
| 68 |
{
|
| 69 |
char **levels = NULL;
|
| 70 |
char **services = NULL;
|
| 71 |
char *level;
|
| 72 |
char *service;
|
| 73 |
int opt;
|
| 74 |
int i;
|
| 75 |
int j;
|
| 76 |
|
| 77 |
while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1)
|
| 78 |
switch (opt) {
|
| 79 |
case 'a':
|
| 80 |
levels = rc_get_runlevels ();
|
| 81 |
break;
|
| 82 |
case 'l':
|
| 83 |
levels = rc_get_runlevels ();
|
| 84 |
STRLIST_FOREACH (levels, level, i)
|
| 85 |
printf ("%s\n", level);
|
| 86 |
rc_strlist_free (levels);
|
| 87 |
exit (EXIT_SUCCESS);
|
| 88 |
case 's':
|
| 89 |
services = rc_services_in_runlevel (NULL);
|
| 90 |
STRLIST_FOREACH (services, service, i)
|
| 91 |
print_service (service);
|
| 92 |
rc_strlist_free (services);
|
| 93 |
exit (EXIT_SUCCESS);
|
| 94 |
case 'u':
|
| 95 |
services = rc_services_in_runlevel (NULL);
|
| 96 |
levels = rc_get_runlevels ();
|
| 97 |
STRLIST_FOREACH (services, service, i) {
|
| 98 |
bool found = false;
|
| 99 |
STRLIST_FOREACH (levels, level, j)
|
| 100 |
if (rc_service_in_runlevel (service, level)) {
|
| 101 |
found = true;
|
| 102 |
break;
|
| 103 |
}
|
| 104 |
if (! found)
|
| 105 |
print_service (service);
|
| 106 |
}
|
| 107 |
rc_strlist_free (levels);
|
| 108 |
rc_strlist_free (services);
|
| 109 |
exit (EXIT_SUCCESS);
|
| 110 |
|
| 111 |
case_RC_COMMON_GETOPT
|
| 112 |
}
|
| 113 |
|
| 114 |
while (optind < argc)
|
| 115 |
levels = rc_strlist_add (levels, argv[optind++]);
|
| 116 |
|
| 117 |
if (! levels)
|
| 118 |
levels = rc_strlist_add (NULL, rc_get_runlevel ());
|
| 119 |
|
| 120 |
STRLIST_FOREACH (levels, level, i) {
|
| 121 |
print_level (level);
|
| 122 |
services = rc_services_in_runlevel (level);
|
| 123 |
STRLIST_FOREACH (services, service, j)
|
| 124 |
print_service (service);
|
| 125 |
rc_strlist_free (services);
|
| 126 |
}
|
| 127 |
|
| 128 |
rc_strlist_free (levels);
|
| 129 |
|
| 130 |
return (EXIT_SUCCESS);
|
| 131 |
}
|