Cuddle up to LKML C style

This commit is contained in:
Roy Marples 2007-04-11 12:44:47 +00:00
parent 55884f7959
commit e6674b2cae
18 changed files with 5197 additions and 5698 deletions

View File

@ -5,6 +5,7 @@
Copyright 2007 Gentoo Foundation Copyright 2007 Gentoo Foundation
Released under the GPLv2 Released under the GPLv2
*/ */
#include <errno.h> #include <errno.h>
@ -32,8 +33,7 @@
#define LDNOTICE "# ld.so.conf autogenerated by env-update; make all\n" \ #define LDNOTICE "# ld.so.conf autogenerated by env-update; make all\n" \
"# changes to contents of /etc/env.d directory\n" "# changes to contents of /etc/env.d directory\n"
static const char *specials[] = static const char *specials[] = {
{
"ADA_INCLUDE_PATH", "ADA_INCLUDE_PATH",
"ADA_OBJECTS_PATH", "ADA_OBJECTS_PATH",
"CLASSPATH", "CLASSPATH",
@ -50,8 +50,7 @@ static const char *specials[] =
NULL NULL
}; };
static const char *special_spaces[] = static const char *special_spaces[] = {
{
"CONFIG_PROTECT", "CONFIG_PROTECT",
"CONFIG_PROTECT_MASK", "CONFIG_PROTECT_MASK",
NULL, NULL,
@ -77,8 +76,7 @@ int main (int argc, char **argv)
if (! files) if (! files)
eerrorx ("%s: no files in " ENVDIR " to process", applet); eerrorx ("%s: no files in " ENVDIR " to process", applet);
STRLIST_FOREACH (files, file, i) STRLIST_FOREACH (files, file, i) {
{
char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL); char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL);
char **entries = NULL; char **entries = NULL;
char *entry; char *entry;
@ -88,8 +86,7 @@ int main (int argc, char **argv)
entries = rc_get_config (NULL, path); entries = rc_get_config (NULL, path);
free (path); free (path);
STRLIST_FOREACH (entries, entry, j) STRLIST_FOREACH (entries, entry, j) {
{
char *tmpent = rc_xstrdup (entry); char *tmpent = rc_xstrdup (entry);
char *value = tmpent; char *value = tmpent;
char *var = strsep (&value, "="); char *var = strsep (&value, "=");
@ -99,18 +96,15 @@ int main (int argc, char **argv)
bool replaced = false; bool replaced = false;
for (k = 0; special_spaces[k]; k++) for (k = 0; special_spaces[k]; k++)
if (strcmp (special_spaces[k], var) == 0) if (strcmp (special_spaces[k], var) == 0) {
{
isspecial = true; isspecial = true;
isspecial_spaced = true; isspecial_spaced = true;
break; break;
} }
if (! isspecial) if (! isspecial) {
{
for (k = 0; specials[k]; k++) for (k = 0; specials[k]; k++)
if (strcmp (specials[k], var) == 0) if (strcmp (specials[k], var) == 0) {
{
isspecial = true; isspecial = true;
break; break;
} }
@ -124,23 +118,18 @@ int main (int argc, char **argv)
continue; continue;
} }
STRLIST_FOREACH (envs, env, k) STRLIST_FOREACH (envs, env, k) {
{
char *tmpenv = rc_xstrdup (env); char *tmpenv = rc_xstrdup (env);
char *tmpvalue = tmpenv; char *tmpvalue = tmpenv;
char *tmpentry = strsep (&tmpvalue, "="); char *tmpentry = strsep (&tmpvalue, "=");
if (strcmp (tmpentry, var) == 0) if (strcmp (tmpentry, var) == 0) {
{ if (isspecial) {
if (isspecial)
{
int len = strlen (envs[k - 1]) + strlen (entry) + 1; int len = strlen (envs[k - 1]) + strlen (entry) + 1;
envs[k - 1] = rc_xrealloc (envs[k - 1], len); envs[k - 1] = rc_xrealloc (envs[k - 1], len);
snprintf (envs[k - 1] + strlen (envs[k - 1]), len, snprintf (envs[k - 1] + strlen (envs[k - 1]), len,
"%s%s", isspecial_spaced ? " " : ":", value); "%s%s", isspecial_spaced ? " " : ":", value);
} } else {
else
{
free (envs[k - 1]); free (envs[k - 1]);
envs[k - 1] = strdup (entry); envs[k - 1] = strdup (entry);
} }
@ -162,8 +151,8 @@ int main (int argc, char **argv)
if ((fp = fopen (PROFILE_ENV, "w")) == NULL) if ((fp = fopen (PROFILE_ENV, "w")) == NULL)
eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno)); eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
fprintf (fp, NOTICE, "/etc/profile", PROFILE_ENV); fprintf (fp, NOTICE, "/etc/profile", PROFILE_ENV);
STRLIST_FOREACH (envs, env, i)
{ STRLIST_FOREACH (envs, env, i) {
char *tmpent = rc_xstrdup (env); char *tmpent = rc_xstrdup (env);
char *value = tmpent; char *value = tmpent;
char *var = strsep (&value, "="); char *var = strsep (&value, "=");
@ -176,8 +165,8 @@ int main (int argc, char **argv)
if ((fp = fopen (CSH_ENV, "w")) == NULL) if ((fp = fopen (CSH_ENV, "w")) == NULL)
eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno)); eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
fprintf (fp, NOTICE, "/etc/csh.cshrc", PROFILE_ENV); fprintf (fp, NOTICE, "/etc/csh.cshrc", PROFILE_ENV);
STRLIST_FOREACH (envs, env, i)
{ STRLIST_FOREACH (envs, env, i) {
char *tmpent = rc_xstrdup (env); char *tmpent = rc_xstrdup (env);
char *value = tmpent; char *value = tmpent;
char *var = strsep (&value, "="); char *var = strsep (&value, "=");
@ -196,8 +185,7 @@ int main (int argc, char **argv)
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
while ((file = strsep (&ldent, ":"))) while ((file = strsep (&ldent, ":"))) {
{
if (strlen (file) == 0) if (strlen (file) == 0)
continue; continue;
@ -206,8 +194,7 @@ int main (int argc, char **argv)
} }
/* Update ld.so.conf only if different */ /* Update ld.so.conf only if different */
if (rc_exists (LDSOCONF)) if (rc_exists (LDSOCONF)) {
{
char **lines = rc_get_list (NULL, LDSOCONF); char **lines = rc_get_list (NULL, LDSOCONF);
char *line; char *line;
ld = false; ld = false;
@ -221,8 +208,7 @@ int main (int argc, char **argv)
ld = true; ld = true;
} }
if (ld) if (ld) {
{
int retval = 0; int retval = 0;
if ((fp = fopen (LDSOCONF, "w")) == NULL) if ((fp = fopen (LDSOCONF, "w")) == NULL)

View File

@ -67,14 +67,12 @@ int main (int argc, char **argv)
char *token; char *token;
int n = 0; int n = 0;
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++) {
{
#ifdef HAVE_GETMNTENT #ifdef HAVE_GETMNTENT
fp = setmntent ("/etc/fstab", "r"); fp = setmntent ("/etc/fstab", "r");
#endif #endif
if (strcmp (argv[i], "--fstype") == 0 && i + 1 < argc) if (strcmp (argv[i], "--fstype") == 0 && i + 1 < argc) {
{
i++; i++;
p = argv[i]; p = argv[i];
while ((token = strsep (&p, ","))) while ((token = strsep (&p, ",")))
@ -84,8 +82,7 @@ int main (int argc, char **argv)
result = EXIT_SUCCESS; result = EXIT_SUCCESS;
} }
if (strcmp (argv[i], "--mount-cmd") == 0 && i + 1 < argc) if (strcmp (argv[i], "--mount-cmd") == 0 && i + 1 < argc) {
{
i++; i++;
if ((ent = GET_ENT_FILE (argv[i])) == NULL) if ((ent = GET_ENT_FILE (argv[i])) == NULL)
continue; continue;
@ -94,8 +91,7 @@ int main (int argc, char **argv)
result = EXIT_SUCCESS; result = EXIT_SUCCESS;
} }
if (strcmp (argv[i], "--opts") == 0 && i + 1 < argc) if (strcmp (argv[i], "--opts") == 0 && i + 1 < argc) {
{
i++; i++;
if ((ent = GET_ENT_FILE (argv[i])) == NULL) if ((ent = GET_ENT_FILE (argv[i])) == NULL)
continue; continue;
@ -103,19 +99,16 @@ int main (int argc, char **argv)
result = EXIT_SUCCESS; result = EXIT_SUCCESS;
} }
if (strcmp (argv[i], "--passno") == 0 && i + 1 < argc) if (strcmp (argv[i], "--passno") == 0 && i + 1 < argc) {
{
i++; i++;
switch (argv[i][0]) switch (argv[i][0]) {
{
case '=': case '=':
case '<': case '<':
case '>': case '>':
if (sscanf (argv[i] + 1, "%d", &n) != 1) if (sscanf (argv[i] + 1, "%d", &n) != 1)
eerrorx ("%s: invalid passno %s", argv[0], argv[i] + 1); eerrorx ("%s: invalid passno %s", argv[0], argv[i] + 1);
while ((ent = GET_ENT)) while ((ent = GET_ENT)) {
{
if (((argv[i][0] == '=' && n == ENT_PASS (ent)) || if (((argv[i][0] == '=' && n == ENT_PASS (ent)) ||
(argv[i][0] == '<' && n > ENT_PASS (ent)) || (argv[i][0] == '<' && n > ENT_PASS (ent)) ||
(argv[i][0] == '>' && n < ENT_PASS (ent))) && (argv[i][0] == '>' && n < ENT_PASS (ent))) &&
@ -133,8 +126,7 @@ int main (int argc, char **argv)
END_ENT; END_ENT;
if (result != EXIT_SUCCESS) if (result != EXIT_SUCCESS) {
{
eerror ("%s: unknown option `%s'", basename (argv[0]), argv[i]); eerror ("%s: unknown option `%s'", basename (argv[0]), argv[i]);
break; break;
} }

View File

@ -40,8 +40,7 @@
/* A cheat sheet of colour capable terminals /* A cheat sheet of colour capable terminals
This is taken from DIR_COLORS from GNU coreutils This is taken from DIR_COLORS from GNU coreutils
We embed it here as we shouldn't depend on coreutils */ We embed it here as we shouldn't depend on coreutils */
static const char *colour_terms[] = static const char *colour_terms[] = {
{
"Eterm", "Eterm",
"ansi", "ansi",
"color-xterm", "color-xterm",
@ -113,10 +112,8 @@ bool colour_terminal (void)
if (! term) if (! term)
return (true); return (true);
while (colour_terms[i]) while (colour_terms[i]) {
{ if (strcmp (colour_terms[i], term) == 0) {
if (strcmp (colour_terms[i], term) == 0)
{
in_colour = 1; in_colour = 1;
return (true); return (true);
} }
@ -154,23 +151,20 @@ static int ebuffer (const char *cmd, int retval, const char *fmt, va_list ap)
if (! file || ! cmd || strlen (cmd) < 4) if (! file || ! cmd || strlen (cmd) < 4)
return (0); return (0);
if (! (fp = fopen (file, "a"))) if (! (fp = fopen (file, "a"))) {
{
fprintf (stderr, "fopen `%s': %s\n", file, strerror (errno)); fprintf (stderr, "fopen `%s': %s\n", file, strerror (errno));
return (0); return (0);
} }
fprintf (fp, "%s %d ", cmd, retval); fprintf (fp, "%s %d ", cmd, retval);
if (fmt) if (fmt) {
{
va_list apc; va_list apc;
va_copy (apc, ap); va_copy (apc, ap);
l = vsnprintf (buffer, sizeof (buffer), fmt, apc); l = vsnprintf (buffer, sizeof (buffer), fmt, apc);
fprintf (fp, "%d %s\n", l, buffer); fprintf (fp, "%d %s\n", l, buffer);
va_end (apc); va_end (apc);
} } else
else
fprintf (fp, "0\n"); fprintf (fp, "0\n");
fclose (fp); fclose (fp);
@ -224,18 +218,15 @@ void eflush (void)
char newfile[PATH_MAX]; char newfile[PATH_MAX];
int i = 1; int i = 1;
if (! file|| (stat (file, &buf) != 0)) if (! file|| (stat (file, &buf) != 0)) {
{
errno = 0; errno = 0;
return; return;
} }
/* Find a unique name for our file */ /* Find a unique name for our file */
while (true) while (true) {
{
snprintf (newfile, sizeof (newfile), "%s.%d", file, i); snprintf (newfile, sizeof (newfile), "%s.%d", file, i);
if (stat (newfile, &buf) != 0) if (stat (newfile, &buf) != 0) {
{
if (rename (file, newfile)) if (rename (file, newfile))
fprintf (stderr, "rename `%s' `%s': %s\n", file, newfile, fprintf (stderr, "rename `%s' `%s': %s\n", file, newfile,
strerror (errno)); strerror (errno));
@ -245,8 +236,7 @@ void eflush (void)
} }
/* We fork a child process here so we don't hold anything up */ /* We fork a child process here so we don't hold anything up */
if ((pid = fork ()) == -1) if ((pid = fork ()) == -1) {
{
fprintf (stderr, "fork: %s", strerror (errno)); fprintf (stderr, "fork: %s", strerror (errno));
return; return;
} }
@ -255,8 +245,7 @@ void eflush (void)
return; return;
/* Spin until we can lock the ebuffer */ /* Spin until we can lock the ebuffer */
while (true) while (true) {
{
struct timeval tv; struct timeval tv;
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 20000; tv.tv_usec = 20000;
@ -269,8 +258,7 @@ void eflush (void)
strerror (errno)); strerror (errno));
} }
if (! (fp = fopen (newfile, "r"))) if (! (fp = fopen (newfile, "r"))) {
{
fprintf (stderr, "fopen `%s': %s\n", newfile, strerror (errno)); fprintf (stderr, "fopen `%s': %s\n", newfile, strerror (errno));
return; return;
} }
@ -278,8 +266,7 @@ void eflush (void)
unsetenv ("RC_EBUFFER"); unsetenv ("RC_EBUFFER");
memset (buffer, 0, RC_LINEBUFFER); memset (buffer, 0, RC_LINEBUFFER);
while (fgets (buffer, RC_LINEBUFFER, fp)) while (fgets (buffer, RC_LINEBUFFER, fp)) {
{
i = strlen (buffer) - 1; i = strlen (buffer) - 1;
if (i < 1) if (i < 1)
continue; continue;
@ -290,38 +277,30 @@ void eflush (void)
p = buffer; p = buffer;
cmd = strsep (&p, " "); cmd = strsep (&p, " ");
token = strsep (&p, " "); token = strsep (&p, " ");
if (sscanf (token, "%d", &retval) != 1) if (sscanf (token, "%d", &retval) != 1) {
{
fprintf (stderr, "eflush `%s': not a number", token); fprintf (stderr, "eflush `%s': not a number", token);
continue; continue;
} }
token = strsep (&p, " "); token = strsep (&p, " ");
if (sscanf (token, "%d", &length) != 1) if (sscanf (token, "%d", &length) != 1) {
{
fprintf (stderr, "eflush `%s': not a number", token); fprintf (stderr, "eflush `%s': not a number", token);
continue; continue;
} }
i = 0; i = 0;
while (funcmap[i].name) while (funcmap[i].name) {
{ if (strcmp (funcmap[i].name, cmd) == 0) {
if (strcmp (funcmap[i].name, cmd) == 0) if (funcmap[i].efunc) {
{
if (funcmap[i].efunc)
{
if (p) if (p)
funcmap[i].efunc ("%s", p); funcmap[i].efunc ("%s", p);
else else
funcmap[i].efunc (NULL, NULL); funcmap[i].efunc (NULL, NULL);
} } else if (funcmap[i].eefunc) {
else if (funcmap[i].eefunc)
{
if (p) if (p)
funcmap[i].eefunc (retval, "%s", p); funcmap[i].eefunc (retval, "%s", p);
else else
funcmap[i].eefunc (retval, NULL, NULL); funcmap[i].eefunc (retval, NULL, NULL);
} } else if (funcmap[i].eind)
else if (funcmap[i].eind)
funcmap[i].eind (); funcmap[i].eind ();
else else
fprintf (stderr, "eflush `%s': no function defined\n", cmd); fprintf (stderr, "eflush `%s': no function defined\n", cmd);
@ -344,8 +323,7 @@ void eflush (void)
_exit (EXIT_SUCCESS); _exit (EXIT_SUCCESS);
} }
#define EBUFFER(_cmd, _retval, _fmt, _ap) \ #define EBUFFER(_cmd, _retval, _fmt, _ap) { \
{ \
int _i = ebuffer (_cmd, _retval, _fmt, _ap); \ int _i = ebuffer (_cmd, _retval, _fmt, _ap); \
if (_i) \ if (_i) \
return (_i); \ return (_i); \
@ -356,8 +334,7 @@ static void elog (int level, const char *fmt, va_list ap)
char *e = getenv ("RC_ELOG"); char *e = getenv ("RC_ELOG");
va_list apc; va_list apc;
if (fmt && e) if (fmt && e) {
{
closelog (); closelog ();
openlog (e, LOG_PID, LOG_DAEMON); openlog (e, LOG_PID, LOG_DAEMON);
va_copy (apc, ap); va_copy (apc, ap);
@ -366,14 +343,14 @@ static void elog (int level, const char *fmt, va_list ap)
closelog (); closelog ();
} }
} }
static int _eindent (FILE *stream) static int _eindent (FILE *stream)
{ {
char *env = getenv ("RC_EINDENT"); char *env = getenv ("RC_EINDENT");
int amount = 0; int amount = 0;
char indent[INDENT_MAX]; char indent[INDENT_MAX];
if (env) if (env) {
{
errno = 0; errno = 0;
amount = strtol (env, NULL, 0); amount = strtol (env, NULL, 0);
if (errno != 0 || amount < 0) if (errno != 0 || amount < 0)
@ -484,8 +461,7 @@ int einfo (const char *fmt, ...)
return (0); return (0);
va_start (ap, fmt); va_start (ap, fmt);
if (! (retval = ebuffer ("einfo", 0, fmt, ap))) if (! (retval = ebuffer ("einfo", 0, fmt, ap))) {
{
retval = _einfovn (fmt, ap); retval = _einfovn (fmt, ap);
retval += printf ("\n"); retval += printf ("\n");
} }
@ -504,8 +480,7 @@ int ewarn (const char *fmt, ...)
va_start (ap, fmt); va_start (ap, fmt);
elog (LOG_WARNING, fmt, ap); elog (LOG_WARNING, fmt, ap);
if (! (retval = ebuffer ("ewarn", 0, fmt, ap))) if (! (retval = ebuffer ("ewarn", 0, fmt, ap))) {
{
retval = _ewarnvn (fmt, ap); retval = _ewarnvn (fmt, ap);
retval += printf ("\n"); retval += printf ("\n");
} }
@ -519,8 +494,7 @@ void ewarnx (const char *fmt, ...)
int retval; int retval;
va_list ap; va_list ap;
if (fmt && ! is_env ("RC_QUIET", "yes")) if (fmt && ! is_env ("RC_QUIET", "yes")) {
{
va_start (ap, fmt); va_start (ap, fmt);
elog (LOG_WARNING, fmt, ap); elog (LOG_WARNING, fmt, ap);
retval = _ewarnvn (fmt, ap); retval = _ewarnvn (fmt, ap);
@ -551,8 +525,7 @@ void eerrorx (const char *fmt, ...)
{ {
va_list ap; va_list ap;
if (fmt) if (fmt) {
{
va_start (ap, fmt); va_start (ap, fmt);
elog (LOG_ERR, fmt, ap); elog (LOG_ERR, fmt, ap);
_eerrorvn (fmt, ap); _eerrorvn (fmt, ap);
@ -571,8 +544,7 @@ int ebegin (const char *fmt, ...)
return (0); return (0);
va_start (ap, fmt); va_start (ap, fmt);
if ((retval = ebuffer ("ebegin", 0, fmt, ap))) if ((retval = ebuffer ("ebegin", 0, fmt, ap))) {
{
va_end (ap); va_end (ap);
return (retval); return (retval);
} }
@ -600,11 +572,9 @@ static void _eend (int col, einfo_color_t color, const char *msg)
cols = get_term_columns () - (strlen (msg) + 6); cols = get_term_columns () - (strlen (msg) + 6);
if (cols > 0 && colour_terminal ()) if (cols > 0 && colour_terminal ()) {
{
fprintf (fp, "\033[A\033[%dC %s[ ", cols, EINFO_BRACKET); fprintf (fp, "\033[A\033[%dC %s[ ", cols, EINFO_BRACKET);
switch (color) switch (color) {
{
case einfo_good: case einfo_good:
fprintf (fp, EINFO_GOOD); fprintf (fp, EINFO_GOOD);
break; break;
@ -625,9 +595,7 @@ static void _eend (int col, einfo_color_t color, const char *msg)
break; break;
} }
fprintf (fp, "%s%s ]%s\n", msg, EINFO_BRACKET, EINFO_NORMAL); fprintf (fp, "%s%s ]%s\n", msg, EINFO_BRACKET, EINFO_NORMAL);
} } else {
else
{
for (i = -1; i < cols - col; i++) for (i = -1; i < cols - col; i++)
fprintf (fp, " "); fprintf (fp, " ");
fprintf (fp, "[ %s ]\n", msg); fprintf (fp, "[ %s ]\n", msg);
@ -641,8 +609,7 @@ static int _do_eend (const char *cmd, int retval, const char *fmt, va_list ap)
va_list apc; va_list apc;
int eb; int eb;
if (fmt) if (fmt) {
{
va_copy (apc, ap); va_copy (apc, ap);
eb = ebuffer (cmd, retval, fmt, apc); eb = ebuffer (cmd, retval, fmt, apc);
va_end (apc); va_end (apc);
@ -650,16 +617,12 @@ static int _do_eend (const char *cmd, int retval, const char *fmt, va_list ap)
return (retval); return (retval);
} }
if (fmt && retval != 0) if (fmt && retval != 0) {
{
va_copy (apc, ap); va_copy (apc, ap);
if (strcmp (cmd, "ewend") == 0) if (strcmp (cmd, "ewend") == 0) {
{
col = _ewarnvn (fmt, apc); col = _ewarnvn (fmt, apc);
fp = stdout; fp = stdout;
} } else {
else
{
col = _eerrorvn (fmt, apc); col = _eerrorvn (fmt, apc);
fp = stderr; fp = stderr;
} }
@ -714,8 +677,7 @@ void eindent (void)
if (ebuffer ("eindent", 0, NULL, NULL)) if (ebuffer ("eindent", 0, NULL, NULL))
return; return;
if (env) if (env) {
{
errno = 0; errno = 0;
amount = strtol (env, NULL, 0); amount = strtol (env, NULL, 0);
if (errno != 0) if (errno != 0)
@ -751,8 +713,7 @@ void eoutdent (void)
if (amount <= 0) if (amount <= 0)
unsetenv ("RC_EINDENT"); unsetenv ("RC_EINDENT");
else else {
{
snprintf (num, 10, "%08d", amount); snprintf (num, 10, "%08d", amount);
setenv ("RC_EINDENT", num, 1); setenv ("RC_EINDENT", num, 1);
} }
@ -805,8 +766,7 @@ int einfov (const char *fmt, ...)
return (0); return (0);
va_start (ap, fmt); va_start (ap, fmt);
if (! (retval = ebuffer ("einfov", 0, fmt, ap))) if (! (retval = ebuffer ("einfov", 0, fmt, ap))) {
{
retval = _einfovn (fmt, ap); retval = _einfovn (fmt, ap);
retval += printf ("\n"); retval += printf ("\n");
} }
@ -826,8 +786,7 @@ int ewarnv (const char *fmt, ...)
return (0); return (0);
va_start (ap, fmt); va_start (ap, fmt);
if (! (retval = ebuffer ("ewarnv", 0, fmt, ap))) if (! (retval = ebuffer ("ewarnv", 0, fmt, ap))) {
{
retval = _ewarnvn (fmt, ap); retval = _ewarnvn (fmt, ap);
retval += printf ("\n"); retval += printf ("\n");
} }
@ -848,8 +807,7 @@ int ebeginv (const char *fmt, ...)
return (0); return (0);
va_start (ap, fmt); va_start (ap, fmt);
if (! (retval = ebuffer ("ebeginv", 0, fmt, ap))) if (! (retval = ebuffer ("ebeginv", 0, fmt, ap))) {
{
retval = _einfovn (fmt, ap); retval = _einfovn (fmt, ap);
retval += printf (" ..."); retval += printf (" ...");
if (colour_terminal ()) if (colour_terminal ())

View File

@ -48,8 +48,7 @@ static bool pid_is_cmd (pid_t pid, const char *cmd)
while ((c = getc (fp)) != EOF && c != '(') while ((c = getc (fp)) != EOF && c != '(')
; ;
if (c != '(') if (c != '(') {
{
fclose(fp); fclose(fp);
return (false); return (false);
} }
@ -72,17 +71,14 @@ static bool pid_is_exec (pid_t pid, const char *exec)
snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid); snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid);
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
if (readlink (cmdline, buffer, sizeof (buffer)) != -1) if (readlink (cmdline, buffer, sizeof (buffer)) != -1) {
{
if (strcmp (exec, buffer) == 0) if (strcmp (exec, buffer) == 0)
return (true); return (true);
/* We should cater for deleted binaries too */ /* We should cater for deleted binaries too */
if (strlen (buffer) > 10) if (strlen (buffer) > 10) {
{
p = buffer + (strlen (buffer) - 10); p = buffer + (strlen (buffer) - 10);
if (strcmp (p, " (deleted)") == 0) if (strcmp (p, " (deleted)") == 0) {
{
*p = 0; *p = 0;
if (strcmp (buffer, exec) == 0) if (strcmp (buffer, exec) == 0)
return (true); return (true);
@ -132,14 +128,12 @@ pid_t *rc_find_pids (const char *exec, const char *cmd,
nasty nasty
*/ */
if ((pp = getenv ("RC_RUNSCRIPT_PID"))) if ((pp = getenv ("RC_RUNSCRIPT_PID"))) {
{
if (sscanf (pp, "%d", &runscript_pid) != 1) if (sscanf (pp, "%d", &runscript_pid) != 1)
runscript_pid = 0; runscript_pid = 0;
} }
while ((entry = readdir (procdir)) != NULL) while ((entry = readdir (procdir)) != NULL) {
{
if (sscanf (entry->d_name, "%d", &p) != 1) if (sscanf (entry->d_name, "%d", &p) != 1)
continue; continue;
foundany = true; foundany = true;
@ -150,8 +144,7 @@ pid_t *rc_find_pids (const char *exec, const char *cmd,
if (pid != 0 && pid != p) if (pid != 0 && pid != p)
continue; continue;
if (uid) if (uid) {
{
snprintf (buffer, sizeof (buffer), "/proc/%d", pid); snprintf (buffer, sizeof (buffer), "/proc/%d", pid);
if (stat (buffer, &sb) != 0 || sb.st_uid != uid) if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
continue; continue;
@ -221,8 +214,7 @@ pid_t *rc_find_pids (const char *exec, const char *cmd,
kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
&processes); &processes);
#endif #endif
for (i = 0; i < processes; i++) for (i = 0; i < processes; i++) {
{
pid_t p = _GET_KINFO_PID (kp[i]); pid_t p = _GET_KINFO_PID (kp[i]);
if (pid != 0 && pid != p) if (pid != 0 && pid != p)
continue; continue;
@ -230,15 +222,13 @@ pid_t *rc_find_pids (const char *exec, const char *cmd,
if (uid != 0 && uid != _GET_KINFO_UID (kp[i])) if (uid != 0 && uid != _GET_KINFO_UID (kp[i]))
continue; continue;
if (cmd) if (cmd) {
{
if (! _GET_KINFO_COMM (kp[i]) || if (! _GET_KINFO_COMM (kp[i]) ||
strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0) strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0)
continue; continue;
} }
if (exec && ! cmd) if (exec && ! cmd) {
{
if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv) if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv)
continue; continue;
@ -273,14 +263,12 @@ static bool _match_daemon (const char *path, const char *file,
int lc = 0; int lc = 0;
int m = 0; int m = 0;
if (! rc_exists (ffile)) if (! rc_exists (ffile)) {
{
free (ffile); free (ffile);
return (false); return (false);
} }
if ((fp = fopen (ffile, "r")) == NULL) if ((fp = fopen (ffile, "r")) == NULL) {
{
eerror ("fopen `%s': %s", ffile, strerror (errno)); eerror ("fopen `%s': %s", ffile, strerror (errno));
free (ffile); free (ffile);
return (false); return (false);
@ -292,8 +280,7 @@ static bool _match_daemon (const char *path, const char *file,
m += 100; m += 100;
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
while ((fgets (buffer, RC_LINEBUFFER, fp))) while ((fgets (buffer, RC_LINEBUFFER, fp))) {
{
int lb = strlen (buffer) - 1; int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n') if (buffer[lb] == '\n')
buffer[lb] = 0; buffer[lb] = 0;
@ -336,54 +323,42 @@ void rc_set_service_daemon (const char *service, const char *exec,
if (! exec && ! name && ! pidfile) if (! exec && ! name && ! pidfile)
return; return;
if (exec) if (exec) {
{
i = strlen (exec) + 6; i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i); mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec); snprintf (mexec, i, "exec=%s", exec);
} } else
else
mexec = strdup ("exec="); mexec = strdup ("exec=");
if (name) if (name) {
{
i = strlen (name) + 6; i = strlen (name) + 6;
mname = rc_xmalloc (sizeof (char *) * i); mname = rc_xmalloc (sizeof (char *) * i);
snprintf (mname, i, "name=%s", name); snprintf (mname, i, "name=%s", name);
} } else
else
mname = strdup ("name="); mname = strdup ("name=");
if (pidfile) if (pidfile) {
{
i = strlen (pidfile) + 9; i = strlen (pidfile) + 9;
mpidfile = rc_xmalloc (sizeof (char *) * i); mpidfile = rc_xmalloc (sizeof (char *) * i);
snprintf (mpidfile, i, "pidfile=%s", pidfile); snprintf (mpidfile, i, "pidfile=%s", pidfile);
} } else
else
mpidfile = strdup ("pidfile="); mpidfile = strdup ("pidfile=");
/* Regardless, erase any existing daemon info */ /* Regardless, erase any existing daemon info */
if (rc_is_dir (dirpath)) if (rc_is_dir (dirpath)) {
{
char *oldfile = NULL; char *oldfile = NULL;
files = rc_ls_dir (NULL, dirpath, 0); files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i) STRLIST_FOREACH (files, file, i) {
{
ffile = rc_strcatpaths (dirpath, file, (char *) NULL); ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
nfiles++; nfiles++;
if (! oldfile) if (! oldfile) {
{ if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) {
if (_match_daemon (dirpath, file, mexec, mname, mpidfile))
{
unlink (ffile); unlink (ffile);
oldfile = ffile; oldfile = ffile;
nfiles--; nfiles--;
} }
} } else {
else
{
rename (ffile, oldfile); rename (ffile, oldfile);
free (oldfile); free (oldfile);
oldfile = ffile; oldfile = ffile;
@ -395,8 +370,7 @@ void rc_set_service_daemon (const char *service, const char *exec,
} }
/* Now store our daemon info */ /* Now store our daemon info */
if (started) if (started) {
{
char buffer[10]; char buffer[10];
FILE *fp; FILE *fp;
@ -408,8 +382,7 @@ void rc_set_service_daemon (const char *service, const char *exec,
file = rc_strcatpaths (dirpath, buffer, (char *) NULL); file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
if ((fp = fopen (file, "w")) == NULL) if ((fp = fopen (file, "w")) == NULL)
eerror ("fopen `%s': %s", file, strerror (errno)); eerror ("fopen `%s': %s", file, strerror (errno));
else else {
{
fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile); fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
fclose (fp); fclose (fp);
} }
@ -436,8 +409,7 @@ bool rc_service_started_daemon (const char *service, const char *exec,
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL); (char *) NULL);
if (! rc_is_dir (dirpath)) if (! rc_is_dir (dirpath)) {
{
free (dirpath); free (dirpath);
return (false); return (false);
} }
@ -446,19 +418,15 @@ bool rc_service_started_daemon (const char *service, const char *exec,
mexec = rc_xmalloc (sizeof (char *) * i); mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec); snprintf (mexec, i, "exec=%s", exec);
if (indx > 0) if (indx > 0) {
{
int len = sizeof (char *) * 10; int len = sizeof (char *) * 10;
file = rc_xmalloc (len); file = rc_xmalloc (len);
snprintf (file, len, "%03d", indx); snprintf (file, len, "%03d", indx);
retval = _match_daemon (dirpath, file, mexec, NULL, NULL); retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
free (file); free (file);
} } else {
else
{
char **files = rc_ls_dir (NULL, dirpath, 0); char **files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i) STRLIST_FOREACH (files, file, i) {
{
retval = _match_daemon (dirpath, file, mexec, NULL, NULL); retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
if (retval) if (retval)
break; break;
@ -493,27 +461,23 @@ bool rc_service_daemons_crashed (const char *service)
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL); (char *) NULL);
if (! rc_is_dir (dirpath)) if (! rc_is_dir (dirpath)) {
{
free (dirpath); free (dirpath);
return (false); return (false);
} }
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
files = rc_ls_dir (NULL, dirpath, 0); files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i) STRLIST_FOREACH (files, file, i) {
{
path = rc_strcatpaths (dirpath, file, (char *) NULL); path = rc_strcatpaths (dirpath, file, (char *) NULL);
fp = fopen (path, "r"); fp = fopen (path, "r");
free (path); free (path);
if (! fp) if (! fp) {
{
eerror ("fopen `%s': %s", file, strerror (errno)); eerror ("fopen `%s': %s", file, strerror (errno));
continue; continue;
} }
while ((fgets (buffer, RC_LINEBUFFER, fp))) while ((fgets (buffer, RC_LINEBUFFER, fp))) {
{
int lb = strlen (buffer) - 1; int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n') if (buffer[lb] == '\n')
buffer[lb] = 0; buffer[lb] = 0;
@ -525,20 +489,15 @@ bool rc_service_daemons_crashed (const char *service)
if (strlen (p) == 0) if (strlen (p) == 0)
continue; continue;
if (strcmp (token, "exec") == 0) if (strcmp (token, "exec") == 0) {
{
if (exec) if (exec)
free (exec); free (exec);
exec = strdup (p); exec = strdup (p);
} } else if (strcmp (token, "name") == 0) {
else if (strcmp (token, "name") == 0)
{
if (name) if (name)
free (name); free (name);
name = strdup (p); name = strdup (p);
} } else if (strcmp (token, "pidfile") == 0) {
else if (strcmp (token, "pidfile") == 0)
{
if (pidfile) if (pidfile)
free (pidfile); free (pidfile);
pidfile = strdup (p); pidfile = strdup (p);
@ -547,23 +506,19 @@ bool rc_service_daemons_crashed (const char *service)
fclose (fp); fclose (fp);
pid = 0; pid = 0;
if (pidfile) if (pidfile) {
{ if (! rc_exists (pidfile)) {
if (! rc_exists (pidfile))
{
retval = true; retval = true;
break; break;
} }
if ((fp = fopen (pidfile, "r")) == NULL) if ((fp = fopen (pidfile, "r")) == NULL) {
{
eerror ("fopen `%s': %s", pidfile, strerror (errno)); eerror ("fopen `%s': %s", pidfile, strerror (errno));
retval = true; retval = true;
break; break;
} }
if (fscanf (fp, "%d", &pid) != 1) if (fscanf (fp, "%d", &pid) != 1) {
{
eerror ("no pid found in `%s'", pidfile); eerror ("no pid found in `%s'", pidfile);
fclose (fp); fclose (fp);
retval = true; retval = true;
@ -575,32 +530,27 @@ bool rc_service_daemons_crashed (const char *service)
pidfile = NULL; pidfile = NULL;
} }
if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) {
{
retval = true; retval = true;
break; break;
} }
free (pids); free (pids);
if (exec) if (exec) {
{
free (exec); free (exec);
exec = NULL; exec = NULL;
} }
if (name) if (name) {
{
free (name); free (name);
name = NULL; name = NULL;
} }
} }
if (exec) if (exec) {
{
free (exec); free (exec);
exec = NULL; exec = NULL;
} }
if (name) if (name) {
{
free (name); free (name);
name = NULL; name = NULL;
} }

View File

@ -119,8 +119,7 @@ char *rc_strcatpaths (const char *path1, const char *paths, ...)
memset (path, 0, length); memset (path, 0, length);
memcpy (path, path1, strlen (path1)); memcpy (path, path1, strlen (path1));
pathp = path + strlen (path1) - 1; pathp = path + strlen (path1) - 1;
if (*pathp != '/') if (*pathp != '/') {
{
pathp++; pathp++;
*pathp++ = '/'; *pathp++ = '/';
} }
@ -130,8 +129,7 @@ char *rc_strcatpaths (const char *path1, const char *paths, ...)
pathp += strlen (paths); pathp += strlen (paths);
va_start (ap, paths); va_start (ap, paths);
while ((p = va_arg (ap, char *)) != NULL) while ((p = va_arg (ap, char *)) != NULL) {
{
if (*pathp != '/') if (*pathp != '/')
*pathp++ = '/'; *pathp++ = '/';
i = strlen (p); i = strlen (p);
@ -223,19 +221,15 @@ char **rc_ls_dir (char **list, const char *dir, int options)
if (! dir) if (! dir)
return (list); return (list);
if ((dp = opendir (dir)) == NULL) if ((dp = opendir (dir)) == NULL) {
{
eerror ("failed to opendir `%s': %s", dir, strerror (errno)); eerror ("failed to opendir `%s': %s", dir, strerror (errno));
return (list); return (list);
} }
errno = 0; errno = 0;
while (((d = readdir (dp)) != NULL) && errno == 0) while (((d = readdir (dp)) != NULL) && errno == 0) {
{ if (d->d_name[0] != '.') {
if (d->d_name[0] != '.') if (options & RC_LS_INITD) {
{
if (options & RC_LS_INITD)
{
int l = strlen (d->d_name); int l = strlen (d->d_name);
char *init = rc_strcatpaths (RC_INITDIR, d->d_name, char *init = rc_strcatpaths (RC_INITDIR, d->d_name,
(char *) NULL); (char *) NULL);
@ -255,8 +249,7 @@ char **rc_ls_dir (char **list, const char *dir, int options)
} }
closedir (dp); closedir (dp);
if (errno != 0) if (errno != 0) {
{
eerror ("failed to readdir `%s': %s", dir, strerror (errno)); eerror ("failed to readdir `%s': %s", dir, strerror (errno));
rc_strlist_free (list); rc_strlist_free (list);
return (NULL); return (NULL);
@ -273,31 +266,24 @@ bool rc_rm_dir (const char *pathname, bool top)
if (! pathname) if (! pathname)
return (false); return (false);
if ((dp = opendir (pathname)) == NULL) if ((dp = opendir (pathname)) == NULL) {
{
eerror ("failed to opendir `%s': %s", pathname, strerror (errno)); eerror ("failed to opendir `%s': %s", pathname, strerror (errno));
return (false); return (false);
} }
errno = 0; errno = 0;
while (((d = readdir (dp)) != NULL) && errno == 0) while (((d = readdir (dp)) != NULL) && errno == 0) {
{ if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) {
if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
{
char *tmp = rc_strcatpaths (pathname, d->d_name, (char *) NULL); char *tmp = rc_strcatpaths (pathname, d->d_name, (char *) NULL);
if (d->d_type == DT_DIR) if (d->d_type == DT_DIR) {
{
if (! rc_rm_dir (tmp, true)) if (! rc_rm_dir (tmp, true))
{ {
free (tmp); free (tmp);
closedir (dp); closedir (dp);
return (false); return (false);
} }
} } else {
else if (unlink (tmp)) {
{
if (unlink (tmp))
{
eerror ("failed to unlink `%s': %s", tmp, strerror (errno)); eerror ("failed to unlink `%s': %s", tmp, strerror (errno));
free (tmp); free (tmp);
closedir (dp); closedir (dp);
@ -311,8 +297,7 @@ bool rc_rm_dir (const char *pathname, bool top)
eerror ("failed to readdir `%s': %s", pathname, strerror (errno)); eerror ("failed to readdir `%s': %s", pathname, strerror (errno));
closedir (dp); closedir (dp);
if (top && rmdir (pathname) != 0) if (top && rmdir (pathname) != 0) {
{
eerror ("failed to rmdir `%s': %s", pathname, strerror (errno)); eerror ("failed to rmdir `%s': %s", pathname, strerror (errno));
return false; return false;
} }
@ -334,14 +319,12 @@ char **rc_get_config (char **list, const char *file)
char *entry; char *entry;
char *newline; char *newline;
if (! (fp = fopen (file, "r"))) if (! (fp = fopen (file, "r"))) {
{
ewarn ("load_config_file `%s': %s", file, strerror (errno)); ewarn ("load_config_file `%s': %s", file, strerror (errno));
return (list); return (list);
} }
while (fgets (buffer, RC_LINEBUFFER, fp)) while (fgets (buffer, RC_LINEBUFFER, fp)) {
{
p = buffer; p = buffer;
/* Strip leading spaces/tabs */ /* Strip leading spaces/tabs */
@ -358,12 +341,10 @@ char **rc_get_config (char **list, const char *file)
entry = rc_xstrdup (token); entry = rc_xstrdup (token);
do do {
{
/* Bash variables are usually quoted */ /* Bash variables are usually quoted */
token = strsep (&p, "\"\'"); token = strsep (&p, "\"\'");
} } while ((token) && (strlen (token) == 0));
while ((token) && (strlen (token) == 0));
/* Drop a newline if that's all we have */ /* Drop a newline if that's all we have */
i = strlen (token) - 1; i = strlen (token) - 1;
@ -377,13 +358,11 @@ char **rc_get_config (char **list, const char *file)
replaced = false; replaced = false;
/* In shells the last item takes precedence, so we need to remove /* In shells the last item takes precedence, so we need to remove
any prior values we may already have */ any prior values we may already have */
STRLIST_FOREACH (list, line, i) STRLIST_FOREACH (list, line, i) {
{
char *tmp = rc_xstrdup (line); char *tmp = rc_xstrdup (line);
linep = tmp; linep = tmp;
linetok = strsep (&linep, "="); linetok = strsep (&linep, "=");
if (strcmp (linetok, entry) == 0) if (strcmp (linetok, entry) == 0) {
{
/* We have a match now - to save time we directly replace it */ /* We have a match now - to save time we directly replace it */
free (list[i - 1]); free (list[i - 1]);
list[i - 1] = newline; list[i - 1] = newline;
@ -394,8 +373,7 @@ char **rc_get_config (char **list, const char *file)
free (tmp); free (tmp);
} }
if (! replaced) if (! replaced) {
{
list = rc_strlist_addsort (list, newline); list = rc_strlist_addsort (list, newline);
free (newline); free (newline);
} }
@ -412,8 +390,7 @@ char *rc_get_config_entry (char **list, const char *entry)
int i; int i;
char *p; char *p;
STRLIST_FOREACH (list, line, i) STRLIST_FOREACH (list, line, i) {
{
p = strchr (line, '='); p = strchr (line, '=');
if (p && strncmp (entry, line, p - line) == 0) if (p && strncmp (entry, line, p - line) == 0)
return (p += 1); return (p += 1);
@ -429,14 +406,12 @@ char **rc_get_list (char **list, const char *file)
char *p; char *p;
char *token; char *token;
if (! (fp = fopen (file, "r"))) if (! (fp = fopen (file, "r"))) {
{
ewarn ("rc_get_list `%s': %s", file, strerror (errno)); ewarn ("rc_get_list `%s': %s", file, strerror (errno));
return (list); return (list);
} }
while (fgets (buffer, RC_LINEBUFFER, fp)) while (fgets (buffer, RC_LINEBUFFER, fp)) {
{
p = buffer; p = buffer;
/* Strip leading spaces/tabs */ /* Strip leading spaces/tabs */
@ -445,8 +420,7 @@ char **rc_get_list (char **list, const char *file)
/* Get entry - we do not want comments */ /* Get entry - we do not want comments */
token = strsep (&p, "#"); token = strsep (&p, "#");
if (token && (strlen (token) > 1)) if (token && (strlen (token) > 1)) {
{
/* Stip the newline if present */ /* Stip the newline if present */
if (token[strlen (token) - 1] == '\n') if (token[strlen (token) - 1] == '\n')
token[strlen (token) - 1] = 0; token[strlen (token) - 1] = 0;
@ -487,16 +461,14 @@ char **rc_filter_env (void)
if (rc_is_file (PROFILE_ENV)) if (rc_is_file (PROFILE_ENV))
profile = rc_get_config (profile, PROFILE_ENV); profile = rc_get_config (profile, PROFILE_ENV);
STRLIST_FOREACH (whitelist, env_name, count) STRLIST_FOREACH (whitelist, env_name, count) {
{
char *space = strchr (env_name, ' '); char *space = strchr (env_name, ' ');
if (space) if (space)
*space = 0; *space = 0;
env_var = getenv (env_name); env_var = getenv (env_name);
if (! env_var && profile) if (! env_var && profile) {
{
env_len = strlen (env_name) + strlen ("export ") + 1; env_len = strlen (env_name) + strlen ("export ") + 1;
p = rc_xmalloc (sizeof (char *) * env_len); p = rc_xmalloc (sizeof (char *) * env_len);
snprintf (p, env_len, "export %s", env_name); snprintf (p, env_len, "export %s", env_name);
@ -519,8 +491,7 @@ char **rc_filter_env (void)
/* Now go through the env var and only add bits not in our PREFIX */ /* Now go through the env var and only add bits not in our PREFIX */
sep = env_var; sep = env_var;
while ((token = strsep (&sep, ":"))) while ((token = strsep (&sep, ":"))) {
{
char *np = strdup (PATH_PREFIX); char *np = strdup (PATH_PREFIX);
char *npp = np; char *npp = np;
char *tok = NULL; char *tok = NULL;
@ -532,9 +503,7 @@ char **rc_filter_env (void)
free (np); free (np);
} }
*p++ = 0; *p++ = 0;
} } else {
else
{
env_len = strlen (env_name) + strlen (env_var) + 2; env_len = strlen (env_name) + strlen (env_var) + 2;
e = rc_xmalloc (sizeof (char *) * env_len); e = rc_xmalloc (sizeof (char *) * env_len);
snprintf (e, env_len, "%s=%s", env_name, env_var); snprintf (e, env_len, "%s=%s", env_name, env_var);
@ -546,8 +515,7 @@ char **rc_filter_env (void)
/* We filtered the env but didn't get a PATH? Very odd. /* We filtered the env but didn't get a PATH? Very odd.
However, we do need a path, so use a default. */ However, we do need a path, so use a default. */
if (! got_path) if (! got_path) {
{
env_len = strlen ("PATH=") + strlen (PATH_PREFIX) + 2; env_len = strlen ("PATH=") + strlen (PATH_PREFIX) + 2;
p = rc_xmalloc (sizeof (char *) * env_len); p = rc_xmalloc (sizeof (char *) * env_len);
snprintf (p, env_len, "PATH=%s", PATH_PREFIX); snprintf (p, env_len, "PATH=%s", PATH_PREFIX);
@ -574,22 +542,19 @@ static bool file_regex (const char *file, const char *regex)
if (! rc_exists (file)) if (! rc_exists (file))
return (false); return (false);
if (! (fp = fopen (file, "r"))) if (! (fp = fopen (file, "r"))) {
{
ewarn ("file_regex `%s': %s", file, strerror (errno)); ewarn ("file_regex `%s': %s", file, strerror (errno));
return (false); return (false);
} }
if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
{
fclose (fp); fclose (fp);
regerror (result, &re, buffer, sizeof (buffer)); regerror (result, &re, buffer, sizeof (buffer));
eerror ("file_regex: %s", buffer); eerror ("file_regex: %s", buffer);
return (false); return (false);
} }
while (fgets (buffer, RC_LINEBUFFER, fp)) while (fgets (buffer, RC_LINEBUFFER, fp)) {
{
if (regexec (&re, buffer, 0, NULL, 0) == 0) if (regexec (&re, buffer, 0, NULL, 0) == 0)
{ {
retval = true; retval = true;
@ -616,21 +581,17 @@ char **rc_config_env (char **env)
FILE *fp; FILE *fp;
char buffer[PATH_MAX]; char buffer[PATH_MAX];
STRLIST_FOREACH (config, line, i) STRLIST_FOREACH (config, line, i) {
{
p = strchr (line, '='); p = strchr (line, '=');
if (! p) if (! p)
continue; continue;
*p = 0; *p = 0;
e = getenv (line); e = getenv (line);
if (! e) if (! e) {
{
*p = '='; *p = '=';
env = rc_strlist_add (env, line); env = rc_strlist_add (env, line);
} } else {
else
{
int len = strlen (line) + strlen (e) + 2; int len = strlen (line) + strlen (e) + 2;
char *new = rc_xmalloc (sizeof (char *) * len); char *new = rc_xmalloc (sizeof (char *) * len);
snprintf (new, len, "%s=%s", line, e); snprintf (new, len, "%s=%s", line, e);
@ -663,16 +624,13 @@ char **rc_config_env (char **env)
env = rc_strlist_add (env, line); env = rc_strlist_add (env, line);
free (line); free (line);
if (rc_exists (RC_SVCDIR "ksoftlevel")) if (rc_exists (RC_SVCDIR "ksoftlevel")) {
{
if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "r"))) if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "r")))
eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel", eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel",
strerror (errno)); strerror (errno));
else else {
{
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
if (fgets (buffer, sizeof (buffer), fp)) if (fgets (buffer, sizeof (buffer), fp)) {
{
i = strlen (buffer) - 1; i = strlen (buffer) - 1;
if (buffer[i] == '\n') if (buffer[i] == '\n')
buffer[i] = 0; buffer[i] = 0;
@ -684,8 +642,7 @@ char **rc_config_env (char **env)
} }
fclose (fp); fclose (fp);
} }
} } else
else
env = rc_strlist_add (env, "RC_DEFAULTLEVEL=" RC_LEVEL_DEFAULT); env = rc_strlist_add (env, "RC_DEFAULTLEVEL=" RC_LEVEL_DEFAULT);
memset (sys, 0, sizeof (sys)); memset (sys, 0, sizeof (sys));
@ -693,19 +650,16 @@ char **rc_config_env (char **env)
/* Linux can run some funky stuff like Xen, VServer, UML, etc /* Linux can run some funky stuff like Xen, VServer, UML, etc
We store this special system in RC_SYS so our scripts run fast */ We store this special system in RC_SYS so our scripts run fast */
#ifdef __linux__ #ifdef __linux__
if (rc_is_dir ("/proc/xen")) if (rc_is_dir ("/proc/xen")) {
{
fp = fopen ("/proc/xen/capabilities", "r"); fp = fopen ("/proc/xen/capabilities", "r");
if (fp) if (fp) {
{
fclose (fp); fclose (fp);
if (file_regex ("/proc/xen/capabilities", "control_d")) if (file_regex ("/proc/xen/capabilities", "control_d"))
snprintf (sys, sizeof (sys), "XENU"); snprintf (sys, sizeof (sys), "XENU");
} }
if (! sys) if (! sys)
snprintf (sys, sizeof (sys), "XEN0"); snprintf (sys, sizeof (sys), "XEN0");
} } else if (file_regex ("/proc/cpuinfo", "UML"))
else if (file_regex ("/proc/cpuinfo", "UML"))
snprintf (sys, sizeof (sys), "UML"); snprintf (sys, sizeof (sys), "UML");
else if (file_regex ("/proc/self/status", else if (file_regex ("/proc/self/status",
"(s_context|VxID|envID):[[:space:]]*[1-9]")) "(s_context|VxID|envID):[[:space:]]*[1-9]"))
@ -714,13 +668,12 @@ char **rc_config_env (char **env)
/* Only add a NET_FS list if not defined */ /* Only add a NET_FS list if not defined */
STRLIST_FOREACH (env, line, i) STRLIST_FOREACH (env, line, i)
if (strncmp (line, "RC_NET_FS_LIST=", strlen ("RC_NET_FS_LIST=")) == 0) if (strncmp (line, "RC_NET_FS_LIST=", strlen ("RC_NET_FS_LIST=")) == 0) {
{
has_net_fs_list = true; has_net_fs_list = true;
break; break;
} }
if (! has_net_fs_list)
{ if (! has_net_fs_list) {
i = strlen ("RC_NET_FS_LIST=") + strlen (RC_NET_FS_LIST_DEFAULT) + 1; i = strlen ("RC_NET_FS_LIST=") + strlen (RC_NET_FS_LIST_DEFAULT) + 1;
line = rc_xmalloc (sizeof (char *) * i); line = rc_xmalloc (sizeof (char *) * i);
snprintf (line, i, "RC_NET_FS_LIST=%s", RC_NET_FS_LIST_DEFAULT); snprintf (line, i, "RC_NET_FS_LIST=%s", RC_NET_FS_LIST_DEFAULT);
@ -728,8 +681,7 @@ char **rc_config_env (char **env)
free (line); free (line);
} }
if (sys[0]) if (sys[0]) {
{
i = strlen ("RC_SYS=") + strlen (sys) + 2; i = strlen ("RC_SYS=") + strlen (sys) + 2;
line = rc_xmalloc (sizeof (char *) * i); line = rc_xmalloc (sizeof (char *) * i);
snprintf (line, i, "RC_SYS=%s", sys); snprintf (line, i, "RC_SYS=%s", sys);
@ -739,8 +691,7 @@ char **rc_config_env (char **env)
/* Some scripts may need to take a different code path if Linux/FreeBSD, etc /* Some scripts may need to take a different code path if Linux/FreeBSD, etc
To save on calling uname, we store it in an environment variable */ To save on calling uname, we store it in an environment variable */
if (uname (&uts) == 0) if (uname (&uts) == 0) {
{
i = strlen ("RC_UNAME=") + strlen (uts.sysname) + 2; i = strlen ("RC_UNAME=") + strlen (uts.sysname) + 2;
line = rc_xmalloc (sizeof (char *) * i); line = rc_xmalloc (sizeof (char *) * i);
snprintf (line, i, "RC_UNAME=%s", uts.sysname); snprintf (line, i, "RC_UNAME=%s", uts.sysname);

View File

@ -45,8 +45,7 @@ static char **_rc_strlist_addsort (char **list, const char *item,
if (! item) if (! item)
return (list); return (list);
while (list && list[i]) while (list && list[i]) {
{
if (uniq && strcmp (list[i], item) == 0) if (uniq && strcmp (list[i], item) == 0)
return (list); return (list);
i++; i++;
@ -64,8 +63,7 @@ static char **_rc_strlist_addsort (char **list, const char *item,
tmp1 = newlist[i]; tmp1 = newlist[i];
newlist[i] = rc_xstrdup (item); newlist[i] = rc_xstrdup (item);
do do {
{
i++; i++;
tmp2 = newlist[i]; tmp2 = newlist[i];
newlist[i] = tmp1; newlist[i] = tmp1;
@ -98,11 +96,9 @@ char **rc_strlist_delete (char **list, const char *item)
return (list); return (list);
while (list[i]) while (list[i])
if (! strcmp (list[i], item)) if (! strcmp (list[i], item)) {
{
free (list[i]); free (list[i]);
do do {
{
list[i] = list[i + 1]; list[i] = list[i + 1];
i++; i++;
} while (list[i]); } while (list[i]);
@ -124,8 +120,7 @@ void rc_strlist_reverse (char **list)
j++; j++;
j--; j--;
while (i < j && list[i] && list[j]) while (i < j && list[i] && list[j]) {
{
item = list[i]; item = list[i];
list[i] = list[j]; list[i] = list[j];
list[j] = item; list[j] = item;
@ -141,8 +136,7 @@ void rc_strlist_free (char **list)
if (! list) if (! list)
return; return;
while (list[i]) while (list[i]) {
{
free (list[i]); free (list[i]);
list[i++] = NULL; list[i++] = NULL;
} }

View File

@ -68,8 +68,7 @@ char **rc_get_runlevels (void)
int i; int i;
char *dir; char *dir;
STRLIST_FOREACH (dirs, dir, i) STRLIST_FOREACH (dirs, dir, i) {
{
char *path = rc_strcatpaths (RC_RUNLEVELDIR, dir, (char *) NULL); char *path = rc_strcatpaths (RC_RUNLEVELDIR, dir, (char *) NULL);
if (rc_is_dir (path)) if (rc_is_dir (path))
runlevels = rc_strlist_addsort (runlevels, dir); runlevels = rc_strlist_addsort (runlevels, dir);
@ -85,14 +84,12 @@ char *rc_get_runlevel (void)
FILE *fp; FILE *fp;
static char buffer [PATH_MAX]; static char buffer [PATH_MAX];
if (! (fp = fopen (SOFTLEVEL, "r"))) if (! (fp = fopen (SOFTLEVEL, "r"))) {
{
snprintf (buffer, sizeof (buffer), "sysinit"); snprintf (buffer, sizeof (buffer), "sysinit");
return (buffer); return (buffer);
} }
if (fgets (buffer, PATH_MAX, fp)) if (fgets (buffer, PATH_MAX, fp)) {
{
int i = strlen (buffer) - 1; int i = strlen (buffer) - 1;
if (buffer[i] == '\n') if (buffer[i] == '\n')
buffer[i] = 0; buffer[i] = 0;
@ -142,20 +139,17 @@ char *rc_resolve_service (const char *service)
return (strdup (service)); return (strdup (service));
file = rc_strcatpaths (RC_SVCDIR, "started", service, (char *) NULL); file = rc_strcatpaths (RC_SVCDIR, "started", service, (char *) NULL);
if (! rc_is_link (file)) if (! rc_is_link (file)) {
{
free (file); free (file);
file = rc_strcatpaths (RC_SVCDIR, "inactive", service, (char *) NULL); file = rc_strcatpaths (RC_SVCDIR, "inactive", service, (char *) NULL);
if (! rc_is_link (file)) if (! rc_is_link (file)) {
{
free (file); free (file);
file = NULL; file = NULL;
} }
} }
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
if (file) if (file) {
{
r = readlink (file, buffer, sizeof (buffer)); r = readlink (file, buffer, sizeof (buffer));
free (file); free (file);
if (r > 0) if (r > 0)
@ -223,10 +217,8 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
base = basename (service); base = basename (service);
if (state != rc_service_stopped) if (state != rc_service_stopped) {
{ if (! rc_is_file(init)) {
if (! rc_is_file(init))
{
free (init); free (init);
return (false); return (false);
} }
@ -236,8 +228,7 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
if (rc_exists (file)) if (rc_exists (file))
unlink (file); unlink (file);
i = symlink (init, file); i = symlink (init, file);
if (i != 0) if (i != 0) {
{
free (file); free (file);
free (init); free (init);
einfo ("%d %s %s", state, rc_service_state_names[state], base); einfo ("%d %s %s", state, rc_service_state_names[state], base);
@ -249,16 +240,14 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
skip_state = state; skip_state = state;
} }
if (state == rc_service_coldplugged) if (state == rc_service_coldplugged) {
{
free (init); free (init);
return (true); return (true);
} }
/* Remove any old states now */ /* Remove any old states now */
i = 0; i = 0;
while (rc_service_state_names[i]) while (rc_service_state_names[i]) {
{
if ((i != skip_state && if ((i != skip_state &&
i != rc_service_stopped && i != rc_service_stopped &&
i != rc_service_coldplugged && i != rc_service_coldplugged &&
@ -268,8 +257,7 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
{ {
file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[i], base, file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[i], base,
(char *) NULL); (char *) NULL);
if (rc_exists (file)) if (rc_exists (file)) {
{
if ((state == rc_service_starting || if ((state == rc_service_starting ||
state == rc_service_stopping) && state == rc_service_stopping) &&
i == rc_service_inactive) i == rc_service_inactive)
@ -309,8 +297,7 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
} }
/* Remove any options and daemons the service may have stored */ /* Remove any options and daemons the service may have stored */
if (state == rc_service_stopped) if (state == rc_service_stopped) {
{
char *dir = rc_strcatpaths (RC_SVCDIR, "options", base, (char *) NULL); char *dir = rc_strcatpaths (RC_SVCDIR, "options", base, (char *) NULL);
if (rc_is_dir (dir)) if (rc_is_dir (dir))
@ -326,15 +313,13 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
} }
/* These are final states, so remove us from scheduled */ /* These are final states, so remove us from scheduled */
if (state == rc_service_started || state == rc_service_stopped) if (state == rc_service_started || state == rc_service_stopped) {
{
char *sdir = rc_strcatpaths (RC_SVCDIR, "scheduled", (char *) NULL); char *sdir = rc_strcatpaths (RC_SVCDIR, "scheduled", (char *) NULL);
char **dirs = rc_ls_dir (NULL, sdir, 0); char **dirs = rc_ls_dir (NULL, sdir, 0);
char *dir; char *dir;
int serrno; int serrno;
STRLIST_FOREACH (dirs, dir, i) STRLIST_FOREACH (dirs, dir, i) {
{
char *bdir = rc_strcatpaths (sdir, dir, (char *) NULL); char *bdir = rc_strcatpaths (sdir, dir, (char *) NULL);
file = rc_strcatpaths (bdir, base, (char *) NULL); file = rc_strcatpaths (bdir, base, (char *) NULL);
if (rc_exists (file)) if (rc_exists (file))
@ -375,8 +360,7 @@ bool rc_service_state (const char *service, const rc_service_state_t state)
/* The crashed state and scheduled states are virtual */ /* The crashed state and scheduled states are virtual */
if (state == rc_service_crashed) if (state == rc_service_crashed)
return (rc_service_daemons_crashed (service)); return (rc_service_daemons_crashed (service));
else if (state == rc_service_scheduled) else if (state == rc_service_scheduled) {
{
char **services = rc_services_scheduled_by (service); char **services = rc_services_scheduled_by (service);
retval = (services); retval = (services);
if (services) if (services)
@ -402,15 +386,12 @@ bool rc_get_service_option (const char *service, const char *option,
(char *) NULL); (char *) NULL);
bool retval = false; bool retval = false;
if (rc_exists (file)) if (rc_exists (file)) {
{
if ((fp = fopen (file, "r")) == NULL) if ((fp = fopen (file, "r")) == NULL)
eerror ("fopen `%s': %s", file, strerror (errno)); eerror ("fopen `%s': %s", file, strerror (errno));
else else {
{
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
while (fgets (buffer, RC_LINEBUFFER, fp)) while (fgets (buffer, RC_LINEBUFFER, fp)) {
{
memcpy (value, buffer, strlen (buffer)); memcpy (value, buffer, strlen (buffer));
value += strlen (buffer); value += strlen (buffer);
} }
@ -431,10 +412,8 @@ bool rc_set_service_option (const char *service, const char *option,
char *file = rc_strcatpaths (path, option, (char *) NULL); char *file = rc_strcatpaths (path, option, (char *) NULL);
bool retval = false; bool retval = false;
if (! rc_is_dir (path)) if (! rc_is_dir (path)) {
{ if (mkdir (path, 0755) != 0) {
if (mkdir (path, 0755) != 0)
{
eerror ("mkdir `%s': %s", path, strerror (errno)); eerror ("mkdir `%s': %s", path, strerror (errno));
free (path); free (path);
free (file); free (file);
@ -444,8 +423,7 @@ bool rc_set_service_option (const char *service, const char *option,
if ((fp = fopen (file, "w")) == NULL) if ((fp = fopen (file, "w")) == NULL)
eerror ("fopen `%s': %s", file, strerror (errno)); eerror ("fopen `%s': %s", file, strerror (errno));
else else {
{
if (value) if (value)
fprintf (fp, "%s", value); fprintf (fp, "%s", value);
fclose (fp); fclose (fp);
@ -466,8 +444,7 @@ static pid_t _exec_service (const char *service, const char *arg)
int status; int status;
file = rc_resolve_service (service); file = rc_resolve_service (service);
if (! rc_is_file (file)) if (! rc_is_file (file)) {
{
rc_mark_service (service, rc_service_stopped); rc_mark_service (service, rc_service_stopped);
free (file); free (file);
return (0); return (0);
@ -477,16 +454,14 @@ static pid_t _exec_service (const char *service, const char *arg)
fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (service), fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (service),
(char *) NULL); (char *) NULL);
if (mkfifo (fifo, 0600) != 0 && errno != EEXIST) if (mkfifo (fifo, 0600) != 0 && errno != EEXIST) {
{
eerror ("unable to create fifo `%s': %s", fifo, strerror (errno)); eerror ("unable to create fifo `%s': %s", fifo, strerror (errno));
free (fifo); free (fifo);
free (file); free (file);
return (-1); return (-1);
} }
if ((pid = fork ()) == 0) if ((pid = fork ()) == 0) {
{
char *myarg = strdup (arg); char *myarg = strdup (arg);
int e = 0; int e = 0;
execl (file, file, myarg, (char *) NULL); execl (file, file, myarg, (char *) NULL);
@ -500,8 +475,7 @@ static pid_t _exec_service (const char *service, const char *arg)
free (fifo); free (fifo);
free (file); free (file);
if (pid == -1) if (pid == -1) {
{
eerror ("unable to fork: %s", strerror (errno)); eerror ("unable to fork: %s", strerror (errno));
return (pid); return (pid);
} }
@ -511,11 +485,9 @@ static pid_t _exec_service (const char *service, const char *arg)
savedpid = pid; savedpid = pid;
errno = 0; errno = 0;
do do {
{
pid = waitpid (savedpid, &status, 0); pid = waitpid (savedpid, &status, 0);
if (pid < 0) if (pid < 0) {
{
if (errno != ECHILD) if (errno != ECHILD)
eerror ("waitpid %d: %s", savedpid, strerror (errno)); eerror ("waitpid %d: %s", savedpid, strerror (errno));
return (-1); return (-1);
@ -556,8 +528,7 @@ void rc_schedule_start_service (const char *service,
dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service), dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service),
(char *) NULL); (char *) NULL);
if (! rc_is_dir (dir)) if (! rc_is_dir (dir))
if (mkdir (dir, 0755) != 0) if (mkdir (dir, 0755) != 0) {
{
eerror ("mkdir `%s': %s", dir, strerror (errno)); eerror ("mkdir `%s': %s", dir, strerror (errno));
free (dir); free (dir);
return; return;
@ -592,36 +563,32 @@ bool rc_wait_service (const char *service)
struct timeval now; struct timeval now;
bool retval = false; bool retval = false;
if (gettimeofday (&stopat, NULL) != 0) if (gettimeofday (&stopat, NULL) != 0) {
{
eerror ("gettimeofday: %s", strerror (errno)); eerror ("gettimeofday: %s", strerror (errno));
return (false); return (false);
} }
stopat.tv_sec += WAIT_MAX; stopat.tv_sec += WAIT_MAX;
while (true) while (true) {
{ if (! rc_exists (fifo)) {
if (! rc_exists (fifo))
{
retval = true; retval = true;
break; break;
} }
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = WAIT_INTERVAL; tv.tv_usec = WAIT_INTERVAL;
if (select (0, 0, 0, 0, &tv) < 0) if (select (0, 0, 0, 0, &tv) < 0) {
{
if (errno != EINTR) if (errno != EINTR)
eerror ("select: %s",strerror (errno)); eerror ("select: %s",strerror (errno));
break; break;
} }
/* Don't hang around forever */ /* Don't hang around forever */
if (gettimeofday (&now, NULL) != 0) if (gettimeofday (&now, NULL) != 0) {
{
eerror ("gettimeofday: %s", strerror (errno)); eerror ("gettimeofday: %s", strerror (errno));
break; break;
} }
if (timercmp (&now, &stopat, >)) if (timercmp (&now, &stopat, >))
break; break;
} }
@ -659,14 +626,12 @@ char **rc_services_in_state (rc_service_state_t state)
(char *) NULL); (char *) NULL);
char **list = NULL; char **list = NULL;
if (state == rc_service_scheduled) if (state == rc_service_scheduled) {
{
char **dirs = rc_ls_dir (NULL, dir, 0); char **dirs = rc_ls_dir (NULL, dir, 0);
char *d; char *d;
int i; int i;
STRLIST_FOREACH (dirs, d, i) STRLIST_FOREACH (dirs, d, i) {
{
char *p = rc_strcatpaths (dir, d, (char *) NULL); char *p = rc_strcatpaths (dir, d, (char *) NULL);
char **entries = rc_ls_dir (NULL, p, RC_LS_INITD); char **entries = rc_ls_dir (NULL, p, RC_LS_INITD);
char *e; char *e;
@ -681,9 +646,7 @@ char **rc_services_in_state (rc_service_state_t state)
if (dirs) if (dirs)
free (dirs); free (dirs);
} } else {
else
{
if (rc_is_dir (dir)) if (rc_is_dir (dir))
list = rc_ls_dir (list, dir, RC_LS_INITD); list = rc_ls_dir (list, dir, RC_LS_INITD);
} }
@ -698,14 +661,12 @@ bool rc_service_add (const char *runlevel, const char *service)
char *init; char *init;
char *file; char *file;
if (! rc_runlevel_exists (runlevel)) if (! rc_runlevel_exists (runlevel)) {
{
errno = ENOENT; errno = ENOENT;
return (false); return (false);
} }
if (rc_service_in_runlevel (service, runlevel)) if (rc_service_in_runlevel (service, runlevel)) {
{
errno = EEXIST; errno = EEXIST;
return (false); return (false);
} }
@ -743,8 +704,7 @@ char **rc_services_scheduled_by (const char *service)
char *dir; char *dir;
int i; int i;
STRLIST_FOREACH (dirs, dir, i) STRLIST_FOREACH (dirs, dir, i) {
{
char *file = rc_strcatpaths (RC_SVCDIR "scheduled", dir, service, char *file = rc_strcatpaths (RC_SVCDIR "scheduled", dir, service,
(char *) NULL); (char *) NULL);
if (rc_exists (file)) if (rc_exists (file))
@ -782,28 +742,21 @@ bool rc_allow_plug (char *service)
list = strdup (match); list = strdup (match);
p = list; p = list;
while ((token = strsep (&p, " "))) while ((token = strsep (&p, " "))) {
{
bool truefalse = true; bool truefalse = true;
if (token[0] == '!') if (token[0] == '!') {
{
truefalse = false; truefalse = false;
token++; token++;
} }
star = strchr (token, '*'); star = strchr (token, '*');
if (star) if (star) {
{ if (strncmp (service, token, star - token) == 0) {
if (strncmp (service, token, star - token) == 0)
{
allow = truefalse; allow = truefalse;
break; break;
} }
} } else {
else if (strcmp (service, token) == 0) {
{
if (strcmp (service, token) == 0)
{
allow = truefalse; allow = truefalse;
break; break;
} }

View File

@ -38,8 +38,7 @@ static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0) if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
eerrorx ("getmntinfo: %s", strerror (errno)); eerrorx ("getmntinfo: %s", strerror (errno));
for (i = 0; i < nmnts; i++) for (i = 0; i < nmnts; i++) {
{
if (node_regex && if (node_regex &&
regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0) regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
continue; continue;
@ -47,14 +46,12 @@ static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0) regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
continue; continue;
if (mounts) if (mounts) {
{
bool found = false; bool found = false;
int j; int j;
char *mnt; char *mnt;
STRLIST_FOREACH (mounts, mnt, j) STRLIST_FOREACH (mounts, mnt, j)
if (strcmp (mnt, mnts[i].f_mntonname) == 0) if (strcmp (mnt, mnts[i].f_mntonname) == 0) {
{
found = true; found = true;
break; break;
} }
@ -86,8 +83,7 @@ static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
if ((fp = fopen ("/proc/mounts", "r")) == NULL) if ((fp = fopen ("/proc/mounts", "r")) == NULL)
eerrorx ("getmntinfo: %s", strerror (errno)); eerrorx ("getmntinfo: %s", strerror (errno));
while (fgets (buffer, sizeof (buffer), fp)) while (fgets (buffer, sizeof (buffer), fp)) {
{
p = buffer; p = buffer;
from = strsep (&p, " "); from = strsep (&p, " ");
if (node_regex && if (node_regex &&
@ -103,14 +99,12 @@ static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
regexec (fstype_regex, fstype, 0, NULL, 0) != 0) regexec (fstype_regex, fstype, 0, NULL, 0) != 0)
continue; continue;
if (mounts) if (mounts) {
{
bool found = false; bool found = false;
int j; int j;
char *mnt; char *mnt;
STRLIST_FOREACH (mounts, mnt, j) STRLIST_FOREACH (mounts, mnt, j)
if (strcmp (mnt, to) == 0) if (strcmp (mnt, to) == 0) {
{
found = true; found = true;
break; break;
} }
@ -147,10 +141,8 @@ int main (int argc, char **argv)
bool reverse = false; bool reverse = false;
char **mounts = NULL; char **mounts = NULL;
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++) {
{ if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc)) {
if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc))
{
i++; i++;
if (fstype_regex) if (fstype_regex)
free (fstype_regex); free (fstype_regex);
@ -164,8 +156,7 @@ int main (int argc, char **argv)
continue; continue;
} }
if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) {
{
i++; i++;
if (node_regex) if (node_regex)
free (node_regex); free (node_regex);
@ -179,8 +170,7 @@ int main (int argc, char **argv)
continue; continue;
} }
if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) {
{
i++; i++;
if (skip_regex) if (skip_regex)
free (skip_regex); free (skip_regex);
@ -194,19 +184,16 @@ int main (int argc, char **argv)
continue; continue;
} }
if (strcmp (argv[i], "--fstype") == 0) if (strcmp (argv[i], "--fstype") == 0) {
{
list_fstype = true; list_fstype = true;
continue; continue;
} }
if (strcmp (argv[i], "--node") == 0) if (strcmp (argv[i], "--node") == 0) {
{
list_nodes = true; list_nodes = true;
continue; continue;
} }
if (strcmp (argv[i], "--reverse") == 0) if (strcmp (argv[i], "--reverse") == 0) {
{
reverse = true; reverse = true;
continue; continue;
} }
@ -229,8 +216,7 @@ int main (int argc, char **argv)
rc_strlist_reverse (nodes); rc_strlist_reverse (nodes);
result = EXIT_FAILURE; result = EXIT_FAILURE;
STRLIST_FOREACH (nodes, node, i) STRLIST_FOREACH (nodes, node, i) {
{
if (skip_regex && regexec (skip_regex, node, 0, NULL, 0) == 0) if (skip_regex && regexec (skip_regex, node, 0, NULL, 0) == 0)
continue; continue;
printf ("%s\n", node); printf ("%s\n", node);

View File

@ -35,37 +35,29 @@ int main (int argc, char **argv)
if (! runlevel) if (! runlevel)
runlevel = rc_get_runlevel (); runlevel = rc_get_runlevel ();
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++) {
{ if (strcmp (argv[i], "--update") == 0) {
if (strcmp (argv[i], "--update") == 0) if (! update) {
{
if (! update)
{
rc_update_deptree (true); rc_update_deptree (true);
update = true; update = true;
} }
continue; continue;
} }
if (strcmp (argv[i], "--strict") == 0) if (strcmp (argv[i], "--strict") == 0) {
{
options |= RC_DEP_STRICT; options |= RC_DEP_STRICT;
continue; continue;
} }
if (strcmp (argv[i], "--notrace") == 0) if (strcmp (argv[i], "--notrace") == 0) {
{
options &= RC_DEP_TRACE; options &= RC_DEP_TRACE;
continue; continue;
} }
if (argv[i][0] == '-') if (argv[i][0] == '-') {
{
argv[i]++; argv[i]++;
types = rc_strlist_add (types, argv[i]); types = rc_strlist_add (types, argv[i]);
} } else {
else
{
if ((deptree = rc_load_deptree ()) == NULL) if ((deptree = rc_load_deptree ()) == NULL)
eerrorx ("failed to load deptree"); eerrorx ("failed to load deptree");
@ -77,8 +69,7 @@ int main (int argc, char **argv)
} }
} }
if (! services) if (! services) {
{
rc_strlist_free (types); rc_strlist_free (types);
rc_free_deptree (deptree); rc_free_deptree (deptree);
if (update) if (update)
@ -87,18 +78,15 @@ int main (int argc, char **argv)
} }
/* If we don't have any types, then supply some defaults */ /* If we don't have any types, then supply some defaults */
if (! types) if (! types) {
{
types = rc_strlist_add (NULL, "ineed"); types = rc_strlist_add (NULL, "ineed");
rc_strlist_add (types, "iuse"); rc_strlist_add (types, "iuse");
} }
depends = rc_get_depends (deptree, types, services, runlevel, options); depends = rc_get_depends (deptree, types, services, runlevel, options);
if (depends) if (depends) {
{ STRLIST_FOREACH (depends, service, i) {
STRLIST_FOREACH (depends, service, i)
{
if (first) if (first)
first = false; first = false;
else else

View File

@ -41,16 +41,14 @@ void rc_plugin_load (void)
return; return;
files = rc_ls_dir (NULL, RC_PLUGINDIR, 0); files = rc_ls_dir (NULL, RC_PLUGINDIR, 0);
STRLIST_FOREACH (files, file, i) STRLIST_FOREACH (files, file, i) {
{
char *p = rc_strcatpaths (RC_PLUGINDIR, file, NULL); char *p = rc_strcatpaths (RC_PLUGINDIR, file, NULL);
void *h = dlopen (p, RTLD_LAZY); void *h = dlopen (p, RTLD_LAZY);
char *func; char *func;
void *f; void *f;
int len; int len;
if (! h) if (! h) {
{
eerror ("dlopen `%s': %s", p, dlerror ()); eerror ("dlopen `%s': %s", p, dlerror ());
free (p); free (p);
continue; continue;
@ -63,19 +61,14 @@ void rc_plugin_load (void)
snprintf (func, len, "_%s_hook", file); snprintf (func, len, "_%s_hook", file);
f = dlsym (h, func); f = dlsym (h, func);
if (! f) if (! f) {
{
eerror ("`%s' does not expose the symbol `%s'", p, func); eerror ("`%s' does not expose the symbol `%s'", p, func);
dlclose (h); dlclose (h);
} } else {
else if (plugin) {
{
if (plugin)
{
plugin->next = rc_xmalloc (sizeof (plugin_t)); plugin->next = rc_xmalloc (sizeof (plugin_t));
plugin = plugin->next; plugin = plugin->next;
} } else
else
plugin = plugins = rc_xmalloc (sizeof (plugin_t)); plugin = plugins = rc_xmalloc (sizeof (plugin_t));
memset (plugin, 0, sizeof (plugin_t)); memset (plugin, 0, sizeof (plugin_t));
@ -95,8 +88,7 @@ void rc_plugin_run (rc_hook_t hook, const char *value)
{ {
plugin_t *plugin = plugins; plugin_t *plugin = plugins;
while (plugin) while (plugin) {
{
if (plugin->hook) if (plugin->hook)
plugin->hook (hook, value); plugin->hook (hook, value);
@ -109,8 +101,7 @@ void rc_plugin_unload (void)
plugin_t *plugin = plugins; plugin_t *plugin = plugins;
plugin_t *next; plugin_t *next;
while (plugin) while (plugin) {
{
next = plugin->next; next = plugin->next;
dlclose (plugin->handle); dlclose (plugin->handle);
free (plugin->name); free (plugin->name);

View File

@ -32,29 +32,21 @@ static void print_service (char *service)
if (rc_service_state (service, rc_service_stopping)) if (rc_service_state (service, rc_service_stopping))
snprintf (status, sizeof (status), "stopping "); snprintf (status, sizeof (status), "stopping ");
else if (rc_service_state (service, rc_service_starting)) else if (rc_service_state (service, rc_service_starting)) {
{
snprintf (status, sizeof (status), "starting "); snprintf (status, sizeof (status), "starting ");
color = einfo_warn; color = einfo_warn;
} } else if (rc_service_state (service, rc_service_inactive)) {
else if (rc_service_state (service, rc_service_inactive))
{
snprintf (status, sizeof (status), "inactive "); snprintf (status, sizeof (status), "inactive ");
color = einfo_warn; color = einfo_warn;
} } else if (geteuid () == 0 && rc_service_state (service, rc_service_crashed))
else if (geteuid () == 0 && rc_service_state (service, rc_service_crashed))
snprintf (status, sizeof (status), " crashed "); snprintf (status, sizeof (status), " crashed ");
else if (rc_service_state (service, rc_service_started)) else if (rc_service_state (service, rc_service_started)) {
{
snprintf (status, sizeof (status), " started "); snprintf (status, sizeof (status), " started ");
color = einfo_good; color = einfo_good;
} } else if (rc_service_state (service, rc_service_scheduled)) {
else if (rc_service_state (service, rc_service_scheduled))
{
snprintf (status, sizeof (status), "scheduled"); snprintf (status, sizeof (status), "scheduled");
color = einfo_warn; color = einfo_warn;
} } else
else
snprintf (status, sizeof (status), " stopped "); snprintf (status, sizeof (status), " stopped ");
ebracket (cols, color, status); ebracket (cols, color, status);
} }
@ -70,8 +62,7 @@ int main (int argc, char **argv)
int i; int i;
int j; int j;
const struct option longopts[] = const struct option longopts[] = {
{
{"all", no_argument, NULL, 'a'}, {"all", no_argument, NULL, 'a'},
{"list", no_argument, NULL, 'l'}, {"list", no_argument, NULL, 'l'},
{"servicelist", no_argument, NULL, 's'}, {"servicelist", no_argument, NULL, 's'},
@ -80,8 +71,7 @@ int main (int argc, char **argv)
}; };
while ((c = getopt_long(argc, argv, "alsu", longopts, &option_index)) != -1) while ((c = getopt_long(argc, argv, "alsu", longopts, &option_index)) != -1)
switch (c) switch (c) {
{
case 'a': case 'a':
levels = rc_get_runlevels (); levels = rc_get_runlevels ();
break; break;
@ -100,12 +90,10 @@ int main (int argc, char **argv)
case 'u': case 'u':
services = rc_services_in_runlevel (NULL); services = rc_services_in_runlevel (NULL);
levels = rc_get_runlevels (); levels = rc_get_runlevels ();
STRLIST_FOREACH (services, service, i) STRLIST_FOREACH (services, service, i) {
{
bool found = false; bool found = false;
STRLIST_FOREACH (levels, level, j) STRLIST_FOREACH (levels, level, j)
if (rc_service_in_runlevel (service, level)) if (rc_service_in_runlevel (service, level)) {
{
found = true; found = true;
break; break;
} }
@ -127,8 +115,7 @@ int main (int argc, char **argv)
if (! levels) if (! levels)
levels = rc_strlist_add (NULL, rc_get_runlevel ()); levels = rc_strlist_add (NULL, rc_get_runlevel ());
STRLIST_FOREACH (levels, level, i) STRLIST_FOREACH (levels, level, i) {
{
print_level (level); print_level (level);
services = rc_services_in_runlevel (level); services = rc_services_in_runlevel (level);
STRLIST_FOREACH (services, service, j) STRLIST_FOREACH (services, service, j)

View File

@ -24,13 +24,11 @@ static bool add (const char *runlevel, const char *service)
{ {
bool retval = true; bool retval = true;
if (! rc_runlevel_exists (runlevel)) if (! rc_runlevel_exists (runlevel)) {
{
ewarn ("runlevel `%s' does not exist", runlevel); ewarn ("runlevel `%s' does not exist", runlevel);
return (false); return (false);
} }
if (rc_service_in_runlevel (service, runlevel)) if (rc_service_in_runlevel (service, runlevel)) {
{
ewarn ("%s already installed in runlevel `%s'; skipping", ewarn ("%s already installed in runlevel `%s'; skipping",
service, runlevel); service, runlevel);
return (false); return (false);
@ -38,8 +36,7 @@ static bool add (const char *runlevel, const char *service)
if (rc_service_add (runlevel, service)) if (rc_service_add (runlevel, service))
einfo ("%s added to runlevel %s", service, runlevel); einfo ("%s added to runlevel %s", service, runlevel);
else else {
{
eerror ("%s: failed to add service `%s' to runlevel `%s': %s", eerror ("%s: failed to add service `%s' to runlevel `%s': %s",
applet, service, runlevel, strerror (errno)); applet, service, runlevel, strerror (errno));
retval = false; retval = false;
@ -64,8 +61,7 @@ int main (int argc, char **argv)
bool verbose = false; bool verbose = false;
char **services = rc_services_in_runlevel (NULL); char **services = rc_services_in_runlevel (NULL);
for (i = 2; i < argc; i++) for (i = 2; i < argc; i++) {
{
if (strcmp (argv[i], "--verbose") == 0 || if (strcmp (argv[i], "--verbose") == 0 ||
strcmp (argv[i], "-v") == 0) strcmp (argv[i], "-v") == 0)
verbose = true; verbose = true;
@ -76,20 +72,15 @@ int main (int argc, char **argv)
if (! runlevels) if (! runlevels)
runlevels = rc_get_runlevels (); runlevels = rc_get_runlevels ();
STRLIST_FOREACH (services, service, i) STRLIST_FOREACH (services, service, i) {
{
char **in = NULL; char **in = NULL;
bool inone = false; bool inone = false;
STRLIST_FOREACH (runlevels, runlevel, j) STRLIST_FOREACH (runlevels, runlevel, j) {
{ if (rc_service_in_runlevel (service, runlevel)) {
if (rc_service_in_runlevel (service, runlevel))
{
in = rc_strlist_add (in, runlevel); in = rc_strlist_add (in, runlevel);
inone = true; inone = true;
} } else {
else
{
char buffer[PATH_MAX]; char buffer[PATH_MAX];
memset (buffer, ' ', strlen (runlevel)); memset (buffer, ' ', strlen (runlevel));
buffer[strlen (runlevel)] = 0; buffer[strlen (runlevel)] = 0;
@ -143,10 +134,8 @@ int main (int argc, char **argv)
if (! runlevels) if (! runlevels)
runlevels = rc_strlist_add (runlevels, rc_get_runlevel ()); runlevels = rc_strlist_add (runlevels, rc_get_runlevel ());
STRLIST_FOREACH (runlevels, runlevel, i) STRLIST_FOREACH (runlevels, runlevel, i) {
{ if (rc_service_in_runlevel (service, runlevel)) {
if (rc_service_in_runlevel (service, runlevel))
{
if (rc_service_delete (runlevel, service)) if (rc_service_delete (runlevel, service))
einfo ("%s removed from runlevel %s", service, runlevel); einfo ("%s removed from runlevel %s", service, runlevel);
else else

214
src/rc.c
View File

@ -46,9 +46,7 @@
#define DEVBOOT "/dev/.rcboot" #define DEVBOOT "/dev/.rcboot"
/* Cleanup anything in main */ /* Cleanup anything in main */
#define CHAR_FREE(_item) \ #define CHAR_FREE(_item) if (_item) { \
if (_item) \
{ \
free (_item); \ free (_item); \
_item = NULL; \ _item = NULL; \
} }
@ -73,8 +71,7 @@ static void cleanup (void)
{ {
rc_plugin_unload (); rc_plugin_unload ();
if (termios_orig) if (termios_orig) {
{
tcsetattr (STDIN_FILENO, TCSANOW, termios_orig); tcsetattr (STDIN_FILENO, TCSANOW, termios_orig);
free (termios_orig); free (termios_orig);
} }
@ -122,14 +119,12 @@ static int do_e (int argc, char **argv)
strcmp (applet, "veend") == 0 || strcmp (applet, "veend") == 0 ||
strcmp (applet, "vweend") == 0) strcmp (applet, "vweend") == 0)
{ {
if (argc > 0) if (argc > 0) {
{
errno = 0; errno = 0;
retval = strtol (argv[0], NULL, 0); retval = strtol (argv[0], NULL, 0);
if (errno != 0) if (errno != 0)
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
else else {
{
argc--; argc--;
argv++; argv++;
} }
@ -138,16 +133,14 @@ static int do_e (int argc, char **argv)
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} }
if (argc > 0) if (argc > 0) {
{
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
l += strlen (argv[i]) + 1; l += strlen (argv[i]) + 1;
message = rc_xmalloc (l); message = rc_xmalloc (l);
p = message; p = message;
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++) {
{
if (i > 0) if (i > 0)
*p++ = ' '; *p++ = ' ';
memcpy (p, argv[i], strlen (argv[i])); memcpy (p, argv[i], strlen (argv[i]));
@ -167,17 +160,13 @@ static int do_e (int argc, char **argv)
ewarn (fmt, message); ewarn (fmt, message);
else if (strcmp (applet, "ewarnn") == 0) else if (strcmp (applet, "ewarnn") == 0)
ewarnn (fmt, message); ewarnn (fmt, message);
else if (strcmp (applet, "eerror") == 0) else if (strcmp (applet, "eerror") == 0) {
{
eerror (fmt, message); eerror (fmt, message);
retval = 1; retval = 1;
} } else if (strcmp (applet, "eerrorn") == 0) {
else if (strcmp (applet, "eerrorn") == 0)
{
eerrorn (fmt, message); eerrorn (fmt, message);
retval = 1; retval = 1;
} } else if (strcmp (applet, "ebegin") == 0)
else if (strcmp (applet, "ebegin") == 0)
ebegin (fmt, message); ebegin (fmt, message);
else if (strcmp (applet, "eend") == 0) else if (strcmp (applet, "eend") == 0)
eend (retval, fmt, message); eend (retval, fmt, message);
@ -207,8 +196,7 @@ static int do_e (int argc, char **argv)
eoutdentv (); eoutdentv ();
else if (strcmp (applet, "eflush") == 0) else if (strcmp (applet, "eflush") == 0)
eflush (); eflush ();
else else {
{
eerror ("%s: unknown applet", applet); eerror ("%s: unknown applet", applet);
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} }
@ -241,15 +229,13 @@ static int do_service (int argc, char **argv)
ok = rc_service_state (argv[0], rc_service_coldplugged); ok = rc_service_state (argv[0], rc_service_coldplugged);
else if (strcmp (applet, "service_wasinactive") == 0) else if (strcmp (applet, "service_wasinactive") == 0)
ok = rc_service_state (argv[0], rc_service_wasinactive); ok = rc_service_state (argv[0], rc_service_wasinactive);
else if (strcmp (applet, "service_started_daemon") == 0) else if (strcmp (applet, "service_started_daemon") == 0) {
{
int idx = 0; int idx = 0;
if (argc > 2) if (argc > 2)
sscanf (argv[2], "%d", &idx); sscanf (argv[2], "%d", &idx);
exit (rc_service_started_daemon (argv[0], argv[1], idx) exit (rc_service_started_daemon (argv[0], argv[1], idx)
? 0 : 1); ? 0 : 1);
} } else
else
eerrorx ("%s: unknown applet", applet); eerrorx ("%s: unknown applet", applet);
return (ok ? EXIT_SUCCESS : EXIT_FAILURE); return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
@ -280,8 +266,7 @@ static int do_mark_service (int argc, char **argv)
/* If we're marking ourselves then we need to inform our parent runscript /* If we're marking ourselves then we need to inform our parent runscript
process so they do not mark us based on our exit code */ process so they do not mark us based on our exit code */
if (ok && svcname && strcmp (svcname, argv[0]) == 0) if (ok && svcname && strcmp (svcname, argv[0]) == 0) {
{
char *runscript_pid = getenv ("RC_RUNSCRIPT_PID"); char *runscript_pid = getenv ("RC_RUNSCRIPT_PID");
char *mtime; char *mtime;
pid_t pid = 0; pid_t pid = 0;
@ -320,15 +305,13 @@ static int do_options (int argc, char **argv)
if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0) if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
eerrorx ("%s: no option specified", applet); eerrorx ("%s: no option specified", applet);
if (strcmp (applet, "get_options") == 0) if (strcmp (applet, "get_options") == 0) {
{
char buffer[1024]; char buffer[1024];
memset (buffer, 0, 1024); memset (buffer, 0, 1024);
ok = rc_get_service_option (service, argv[0], buffer); ok = rc_get_service_option (service, argv[0], buffer);
if (ok) if (ok)
printf ("%s", buffer); printf ("%s", buffer);
} } else if (strcmp (applet, "save_options") == 0)
else if (strcmp (applet, "save_options") == 0)
ok = rc_set_service_option (service, argv[0], argv[1]); ok = rc_set_service_option (service, argv[0], argv[1]);
else else
eerrorx ("%s: unknown applet", applet); eerrorx ("%s: unknown applet", applet);
@ -346,8 +329,7 @@ static char read_key (bool block)
/* Now save our terminal settings. We need to restore them at exit as we /* Now save our terminal settings. We need to restore them at exit as we
will be changing it for non-blocking reads for Interactive */ will be changing it for non-blocking reads for Interactive */
if (! termios_orig) if (! termios_orig) {
{
termios_orig = rc_xmalloc (sizeof (struct termios)); termios_orig = rc_xmalloc (sizeof (struct termios));
tcgetattr (STDIN_FILENO, termios_orig); tcgetattr (STDIN_FILENO, termios_orig);
} }
@ -356,8 +338,7 @@ static char read_key (bool block)
termios.c_lflag &= ~(ICANON | ECHO); termios.c_lflag &= ~(ICANON | ECHO);
if (block) if (block)
termios.c_cc[VMIN] = 1; termios.c_cc[VMIN] = 1;
else else {
{
termios.c_cc[VMIN] = 0; termios.c_cc[VMIN] = 0;
termios.c_cc[VTIME] = 0; termios.c_cc[VTIME] = 0;
} }
@ -386,15 +367,13 @@ static void mark_interactive (void)
static void sulogin (bool cont) static void sulogin (bool cont)
{ {
#ifdef __linux__ #ifdef __linux__
if (cont) if (cont) {
{
int status = 0; int status = 0;
pid_t pid = fork(); pid_t pid = fork();
if (pid == -1) if (pid == -1)
eerrorx ("%s: fork: %s", applet, strerror (errno)); eerrorx ("%s: fork: %s", applet, strerror (errno));
if (pid == 0) if (pid == 0) {
{
newenv = rc_filter_env (); newenv = rc_filter_env ();
mycmd = rc_xstrdup ("/sbin/sulogin"); mycmd = rc_xstrdup ("/sbin/sulogin");
myarg = rc_xstrdup (getenv ("CONSOLE")); myarg = rc_xstrdup (getenv ("CONSOLE"));
@ -402,10 +381,7 @@ static void sulogin (bool cont)
eerrorx ("%s: unable to exec `/sbin/sulogin': %s", applet, strerror (errno)); eerrorx ("%s: unable to exec `/sbin/sulogin': %s", applet, strerror (errno));
} }
waitpid (pid, &status, 0); waitpid (pid, &status, 0);
} } else {
else
{
newenv = rc_filter_env (); newenv = rc_filter_env ();
mycmd = rc_xstrdup ("/sbin/sulogin"); mycmd = rc_xstrdup ("/sbin/sulogin");
myarg = rc_xstrdup (getenv ("CONSOLE")); myarg = rc_xstrdup (getenv ("CONSOLE"));
@ -414,8 +390,7 @@ static void sulogin (bool cont)
} }
#else #else
/* Appease gcc */ /* Appease gcc */
cont = cont; exit (cont ? EXIT_FAILURE : EXIT_SUCCESS);
exit (EXIT_SUCCESS);
#endif #endif
} }
@ -434,8 +409,7 @@ static void set_ksoftlevel (const char *runlevel)
return; return;
} }
if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "w"))) if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "w"))) {
{
eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel", strerror (errno)); eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel", strerror (errno));
return; return;
} }
@ -461,8 +435,7 @@ static void handle_signal (int sig)
int serrno = errno; int serrno = errno;
char signame[10] = { '\0' }; char signame[10] = { '\0' };
switch (sig) switch (sig) {
{
case SIGINT: case SIGINT:
if (! signame[0]) if (! signame[0])
snprintf (signame, sizeof (signame), "SIGINT"); snprintf (signame, sizeof (signame), "SIGINT");
@ -548,8 +521,7 @@ int main (int argc, char **argv)
env = rc_filter_env (); env = rc_filter_env ();
env = rc_config_env (env); env = rc_config_env (env);
if (env) if (env) {
{
char *p; char *p;
#ifdef __linux__ #ifdef __linux__
@ -562,8 +534,7 @@ int main (int argc, char **argv)
We could manipulate environ directly ourselves, but it seems that We could manipulate environ directly ourselves, but it seems that
some kernels bitch about this according to the environ man pages some kernels bitch about this according to the environ man pages
so we walk though environ and call unsetenv for each value. */ so we walk though environ and call unsetenv for each value. */
while (environ[0]) while (environ[0]) {
{
tmp = rc_xstrdup (environ[0]); tmp = rc_xstrdup (environ[0]);
p = tmp; p = tmp;
var = strsep (&p, "="); var = strsep (&p, "=");
@ -592,13 +563,10 @@ int main (int argc, char **argv)
RUNLEVEL = getenv ("RUNLEVEL"); RUNLEVEL = getenv ("RUNLEVEL");
PREVLEVEL = getenv ("PREVLEVEL"); PREVLEVEL = getenv ("PREVLEVEL");
if (RUNLEVEL && newlevel) if (RUNLEVEL && newlevel) {
{ if (strcmp (RUNLEVEL, "S") == 0 || strcmp (RUNLEVEL, "1") == 0) {
if (strcmp (RUNLEVEL, "S") == 0 || strcmp (RUNLEVEL, "1") == 0)
{
/* OK, we're either in runlevel 1 or single user mode */ /* OK, we're either in runlevel 1 or single user mode */
if (strcmp (newlevel, RC_LEVEL_SYSINIT) == 0) if (strcmp (newlevel, RC_LEVEL_SYSINIT) == 0) {
{
struct utsname uts; struct utsname uts;
pid_t pid; pid_t pid;
pid_t wpid; pid_t wpid;
@ -630,16 +598,14 @@ int main (int argc, char **argv)
if ((pid = fork ()) == -1) if ((pid = fork ()) == -1)
eerrorx ("%s: fork: %s", applet, strerror (errno)); eerrorx ("%s: fork: %s", applet, strerror (errno));
if (pid == 0) if (pid == 0) {
{
mycmd = rc_xstrdup (INITSH); mycmd = rc_xstrdup (INITSH);
execl (mycmd, mycmd, (char *) NULL); execl (mycmd, mycmd, (char *) NULL);
eerrorx ("%s: unable to exec `" INITSH "': %s", eerrorx ("%s: unable to exec `" INITSH "': %s",
applet, strerror (errno)); applet, strerror (errno));
} }
do do {
{
wpid = waitpid (pid, &status, 0); wpid = waitpid (pid, &status, 0);
if (wpid < 1) if (wpid < 1)
eerror ("waitpid: %s", strerror (errno)); eerror ("waitpid: %s", strerror (errno));
@ -652,8 +618,7 @@ int main (int argc, char **argv)
#ifdef __linux__ #ifdef __linux__
set_ksoftlevel (NULL); set_ksoftlevel (NULL);
if ((fp = fopen ("/proc/cmdline", "r"))) if ((fp = fopen ("/proc/cmdline", "r"))) {
{
char buffer[RC_LINEBUFFER]; char buffer[RC_LINEBUFFER];
char *soft; char *soft;
@ -662,8 +627,7 @@ int main (int argc, char **argv)
(soft = strstr (buffer, "softlevel="))) (soft = strstr (buffer, "softlevel=")))
{ {
i = soft - buffer; i = soft - buffer;
if (i == 0 || buffer[i - 1] == ' ') if (i == 0 || buffer[i - 1] == ' ') {
{
char *level; char *level;
/* Trim the trailing carriage return if present */ /* Trim the trailing carriage return if present */
@ -698,8 +662,7 @@ int main (int argc, char **argv)
char *token; char *token;
char lvl[2] = {0, 0}; char lvl[2] = {0, 0};
STRLIST_FOREACH (inittab, line, i) STRLIST_FOREACH (inittab, line, i) {
{
p = line; p = line;
token = strsep (&p, ":"); token = strsep (&p, ":");
if (! token || token[0] != 'l') if (! token || token[0] != 'l')
@ -724,13 +687,10 @@ int main (int argc, char **argv)
rc_strlist_free (inittab); rc_strlist_free (inittab);
/* We have a level, so telinit into it */ /* We have a level, so telinit into it */
if (lvl[0] == 0) if (lvl[0] == 0) {
{
eerrorx ("%s: couldn't find a runlevel called `%s'", eerrorx ("%s: couldn't find a runlevel called `%s'",
applet, newlevel); applet, newlevel);
} } else {
else
{
mycmd = rc_xstrdup ("/sbin/telinit"); mycmd = rc_xstrdup ("/sbin/telinit");
myarg = rc_xstrdup (lvl); myarg = rc_xstrdup (lvl);
execl (mycmd, mycmd, myarg, (char *) NULL); execl (mycmd, mycmd, myarg, (char *) NULL);
@ -747,16 +707,13 @@ int main (int argc, char **argv)
rc shutdown rc shutdown
rc reboot rc reboot
*/ */
if (newlevel) if (newlevel) {
{ if (myarg) {
if (myarg)
{
free (myarg); free (myarg);
myarg = NULL; myarg = NULL;
} }
if (strcmp (newlevel, RC_LEVEL_SINGLE) == 0) if (strcmp (newlevel, RC_LEVEL_SINGLE) == 0) {
{
if (! RUNLEVEL || if (! RUNLEVEL ||
(strcmp (RUNLEVEL, "S") != 0 && (strcmp (RUNLEVEL, "S") != 0 &&
strcmp (RUNLEVEL, "1") != 0)) strcmp (RUNLEVEL, "1") != 0))
@ -776,9 +733,7 @@ int main (int argc, char **argv)
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
#endif #endif
} }
} } else if (strcmp (newlevel, RC_LEVEL_REBOOT) == 0) {
else if (strcmp (newlevel, RC_LEVEL_REBOOT) == 0)
{
if (! RUNLEVEL || if (! RUNLEVEL ||
strcmp (RUNLEVEL, "6") != 0) strcmp (RUNLEVEL, "6") != 0)
{ {
@ -789,9 +744,7 @@ int main (int argc, char **argv)
eerrorx ("%s: unable to exec `%s': %s", eerrorx ("%s: unable to exec `%s': %s",
mycmd, applet, strerror (errno)); mycmd, applet, strerror (errno));
} }
} } else if (strcmp (newlevel, RC_LEVEL_SHUTDOWN) == 0) {
else if (strcmp (newlevel, RC_LEVEL_SHUTDOWN) == 0)
{
if (! RUNLEVEL || if (! RUNLEVEL ||
strcmp (RUNLEVEL, "0") != 0) strcmp (RUNLEVEL, "0") != 0)
{ {
@ -830,10 +783,8 @@ int main (int argc, char **argv)
if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "r"))) if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "r")))
eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel", eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel",
strerror (errno)); strerror (errno));
else else {
{ if (fgets (ksoftbuffer, sizeof (ksoftbuffer), fp)) {
if (fgets (ksoftbuffer, sizeof (ksoftbuffer), fp))
{
i = strlen (ksoftbuffer) - 1; i = strlen (ksoftbuffer) - 1;
if (ksoftbuffer[i] == '\n') if (ksoftbuffer[i] == '\n')
ksoftbuffer[i] = 0; ksoftbuffer[i] = 0;
@ -841,8 +792,7 @@ int main (int argc, char **argv)
} }
fclose (fp); fclose (fp);
} }
} } else
else
set_ksoftlevel (NULL); set_ksoftlevel (NULL);
} }
@ -855,15 +805,12 @@ int main (int argc, char **argv)
rc_set_runlevel (newlevel); rc_set_runlevel (newlevel);
setenv ("RC_SOFTLEVEL", newlevel, 1); setenv ("RC_SOFTLEVEL", newlevel, 1);
rc_plugin_run (rc_hook_runlevel_stop_in, newlevel); rc_plugin_run (rc_hook_runlevel_stop_in, newlevel);
} } else {
else
{
rc_plugin_run (rc_hook_runlevel_stop_in, runlevel); rc_plugin_run (rc_hook_runlevel_stop_in, runlevel);
} }
/* Check if runlevel is valid if we're changing */ /* Check if runlevel is valid if we're changing */
if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) {
{
tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel, (char *) NULL); tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel, (char *) NULL);
if (! rc_is_dir (tmp)) if (! rc_is_dir (tmp))
eerrorx ("%s: is not a valid runlevel", newlevel); eerrorx ("%s: is not a valid runlevel", newlevel);
@ -885,8 +832,7 @@ int main (int argc, char **argv)
its coldplugging thing. runscript knows when we're not ready so it its coldplugging thing. runscript knows when we're not ready so it
stores a list of coldplugged services in DEVBOOT for us to pick up stores a list of coldplugged services in DEVBOOT for us to pick up
here when we are ready for them */ here when we are ready for them */
if (rc_is_dir (DEVBOOT)) if (rc_is_dir (DEVBOOT)) {
{
start_services = rc_ls_dir (NULL, DEVBOOT, RC_LS_INITD); start_services = rc_ls_dir (NULL, DEVBOOT, RC_LS_INITD);
rc_rm_dir (DEVBOOT, true); rc_rm_dir (DEVBOOT, true);
@ -911,8 +857,7 @@ int main (int argc, char **argv)
{ {
/* The net interfaces are easy - they're all in net /dev/net :) */ /* The net interfaces are easy - they're all in net /dev/net :) */
start_services = rc_ls_dir (NULL, "/dev/net", 0); start_services = rc_ls_dir (NULL, "/dev/net", 0);
STRLIST_FOREACH (start_services, service, i) STRLIST_FOREACH (start_services, service, i) {
{
j = (strlen ("net.") + strlen (service) + 1); j = (strlen ("net.") + strlen (service) + 1);
tmp = rc_xmalloc (sizeof (char *) * j); tmp = rc_xmalloc (sizeof (char *) * j);
snprintf (tmp, j, "net.%s", service); snprintf (tmp, j, "net.%s", service);
@ -925,14 +870,12 @@ int main (int argc, char **argv)
/* The mice are a little more tricky. /* The mice are a little more tricky.
If we coldplug anything else, we'll probably do it here. */ If we coldplug anything else, we'll probably do it here. */
start_services = rc_ls_dir (NULL, "/dev", 0); start_services = rc_ls_dir (NULL, "/dev", 0);
STRLIST_FOREACH (start_services, service, i) STRLIST_FOREACH (start_services, service, i) {
{
if (strncmp (service, "psm", 3) == 0 || if (strncmp (service, "psm", 3) == 0 ||
strncmp (service, "ums", 3) == 0) strncmp (service, "ums", 3) == 0)
{ {
char *p = service + 3; char *p = service + 3;
if (p && isdigit (*p)) if (p && isdigit (*p)) {
{
j = (strlen ("moused.") + strlen (service) + 1); j = (strlen ("moused.") + strlen (service) + 1);
tmp = rc_xmalloc (sizeof (char *) * j); tmp = rc_xmalloc (sizeof (char *) * j);
snprintf (tmp, j, "moused.%s", service); snprintf (tmp, j, "moused.%s", service);
@ -971,13 +914,10 @@ int main (int argc, char **argv)
/* Load our start services now. /* Load our start services now.
We have different rules dependent on runlevel. */ We have different rules dependent on runlevel. */
if (newlevel && strcmp (newlevel, RC_LEVEL_BOOT) == 0) if (newlevel && strcmp (newlevel, RC_LEVEL_BOOT) == 0) {
{ if (coldplugged_services) {
if (coldplugged_services)
{
einfon ("Device initiated services:"); einfon ("Device initiated services:");
STRLIST_FOREACH (coldplugged_services, service, i) STRLIST_FOREACH (coldplugged_services, service, i) {
{
printf (" %s", service); printf (" %s", service);
start_services = rc_strlist_add (start_services, service); start_services = rc_strlist_add (start_services, service);
} }
@ -987,9 +927,7 @@ int main (int argc, char **argv)
(char *) NULL); (char *) NULL);
start_services = rc_ls_dir (start_services, tmp, RC_LS_INITD); start_services = rc_ls_dir (start_services, tmp, RC_LS_INITD);
CHAR_FREE (tmp); CHAR_FREE (tmp);
} } else {
else
{
/* Store our list of coldplugged services */ /* Store our list of coldplugged services */
coldplugged_services = rc_ls_dir (coldplugged_services, RC_SVCDIR_COLDPLUGGED, coldplugged_services = rc_ls_dir (coldplugged_services, RC_SVCDIR_COLDPLUGGED,
RC_LS_INITD); RC_LS_INITD);
@ -1017,8 +955,7 @@ int main (int argc, char **argv)
types = rc_strlist_add (NULL, "needsme"); types = rc_strlist_add (NULL, "needsme");
types = rc_strlist_add (types, "usesme"); types = rc_strlist_add (types, "usesme");
/* Now stop the services that shouldn't be running */ /* Now stop the services that shouldn't be running */
STRLIST_FOREACH (stop_services, service, i) STRLIST_FOREACH (stop_services, service, i) {
{
bool found = false; bool found = false;
char *conf = NULL; char *conf = NULL;
char **stopdeps = NULL; char **stopdeps = NULL;
@ -1030,23 +967,20 @@ int main (int argc, char **argv)
continue; continue;
/* We always stop the service when in these runlevels */ /* We always stop the service when in these runlevels */
if (going_down) if (going_down) {
{
rc_stop_service (service); rc_stop_service (service);
continue; continue;
} }
/* If we're in the start list then don't bother stopping us */ /* If we're in the start list then don't bother stopping us */
STRLIST_FOREACH (start_services, svc1, j) STRLIST_FOREACH (start_services, svc1, j)
if (strcmp (svc1, service) == 0) if (strcmp (svc1, service) == 0) {
{
found = true; found = true;
break; break;
} }
/* Unless we would use a different config file */ /* Unless we would use a different config file */
if (found) if (found) {
{
int len; int len;
if (! newlevel) if (! newlevel)
continue; continue;
@ -1058,8 +992,7 @@ int main (int argc, char **argv)
found = rc_exists (conf); found = rc_exists (conf);
CHAR_FREE (conf); CHAR_FREE (conf);
CHAR_FREE (tmp); CHAR_FREE (tmp);
if (! found) if (! found) {
{
len = strlen (service) + strlen (newlevel) + 2; len = strlen (service) + strlen (newlevel) + 2;
tmp = rc_xmalloc (sizeof (char *) * len); tmp = rc_xmalloc (sizeof (char *) * len);
snprintf (tmp, len, "%s.%s", service, newlevel); snprintf (tmp, len, "%s.%s", service, newlevel);
@ -1070,10 +1003,8 @@ int main (int argc, char **argv)
if (!found) if (!found)
continue; continue;
} }
} } else {
else
/* Allow coldplugged services not to be in the runlevels list */ /* Allow coldplugged services not to be in the runlevels list */
{
if (rc_service_state (service, rc_service_coldplugged)) if (rc_service_state (service, rc_service_coldplugged))
continue; continue;
} }
@ -1086,11 +1017,9 @@ int main (int argc, char **argv)
rc_strlist_free (stopdeps); rc_strlist_free (stopdeps);
stopdeps = NULL; stopdeps = NULL;
found = false; found = false;
STRLIST_FOREACH (deporder, svc1, j) STRLIST_FOREACH (deporder, svc1, j) {
{
STRLIST_FOREACH (start_services, svc2, k) STRLIST_FOREACH (start_services, svc2, k)
if (strcmp (svc1, svc2) == 0) if (strcmp (svc1, svc2) == 0) {
{
found = true; found = true;
break; break;
} }
@ -1117,8 +1046,7 @@ int main (int argc, char **argv)
rmdir (RC_SVCDIR "/softscripts.new"); rmdir (RC_SVCDIR "/softscripts.new");
/* Store the new runlevel */ /* Store the new runlevel */
if (newlevel) if (newlevel) {
{
rc_set_runlevel (newlevel); rc_set_runlevel (newlevel);
runlevel = newlevel; runlevel = newlevel;
setenv ("RC_SOFTLEVEL", runlevel, 1); setenv ("RC_SOFTLEVEL", runlevel, 1);
@ -1136,8 +1064,7 @@ int main (int argc, char **argv)
} }
/* Single user is done now */ /* Single user is done now */
if (strcmp (runlevel, RC_LEVEL_SINGLE) == 0) if (strcmp (runlevel, RC_LEVEL_SINGLE) == 0) {
{
if (rc_exists (INTERACTIVE)) if (rc_exists (INTERACTIVE))
unlink (INTERACTIVE); unlink (INTERACTIVE);
sulogin (false); sulogin (false);
@ -1162,15 +1089,12 @@ int main (int argc, char **argv)
start_services = deporder; start_services = deporder;
deporder = NULL; deporder = NULL;
STRLIST_FOREACH (start_services, service, i) STRLIST_FOREACH (start_services, service, i) {
{ if (rc_service_state (service, rc_service_stopped)) {
if (rc_service_state (service, rc_service_stopped))
{
if (! interactive) if (! interactive)
interactive = want_interactive (); interactive = want_interactive ();
if (interactive) if (interactive) {
{
interactive_retry: interactive_retry:
printf ("\n"); printf ("\n");
einfo ("About to start the service %s", service); einfo ("About to start the service %s", service);
@ -1179,8 +1103,7 @@ interactive_retry:
einfo ("3) Continue boot process\t\t4) Exit to shell"); einfo ("3) Continue boot process\t\t4) Exit to shell");
eoutdent (); eoutdent ();
interactive_option: interactive_option:
switch (read_key (true)) switch (read_key (true)) {
{
case '1': break; case '1': break;
case '2': continue; case '2': continue;
case '3': interactive = false; break; case '3': interactive = false; break;
@ -1201,8 +1124,7 @@ interactive_option:
/* Store our interactive status for boot */ /* Store our interactive status for boot */
if (interactive && strcmp (runlevel, RC_LEVEL_BOOT) == 0) if (interactive && strcmp (runlevel, RC_LEVEL_BOOT) == 0)
mark_interactive (); mark_interactive ();
else else {
{
if (rc_exists (INTERACTIVE)) if (rc_exists (INTERACTIVE))
unlink (INTERACTIVE); unlink (INTERACTIVE);
} }

View File

@ -68,8 +68,7 @@ void setup_selinux (int argc, char **argv)
void *lib_handle = NULL; void *lib_handle = NULL;
lib_handle = dlopen (SELINUX_LIB, RTLD_NOW | RTLD_GLOBAL); lib_handle = dlopen (SELINUX_LIB, RTLD_NOW | RTLD_GLOBAL);
if (lib_handle) if (lib_handle) {
{
/* FIXME: the below code generates the warning /* FIXME: the below code generates the warning
ISO C forbids assignment between function pointer and 'void *' ISO C forbids assignment between function pointer and 'void *'
which sucks ass which sucks ass
@ -96,18 +95,15 @@ static void handle_signal (int sig)
int serrno = errno; int serrno = errno;
char signame[10] = { '\0' }; char signame[10] = { '\0' };
switch (sig) switch (sig) {
{
case SIGHUP: case SIGHUP:
sighup = true; sighup = true;
break; break;
case SIGCHLD: case SIGCHLD:
do do {
{
pid = waitpid (-1, &status, WNOHANG); pid = waitpid (-1, &status, WNOHANG);
if (pid < 0) if (pid < 0) {
{
if (errno != ECHILD) if (errno != ECHILD)
eerror ("waitpid: %s", strerror (errno)); eerror ("waitpid: %s", strerror (errno));
return; return;
@ -170,14 +166,11 @@ static bool in_control ()
if (! (mtime = get_mtime (mtime_test, false))) if (! (mtime = get_mtime (mtime_test, false)))
return (false); return (false);
while (tests[i]) while (tests[i]) {
{
path = rc_strcatpaths (RC_SVCDIR, tests[i], applet, (char *) NULL); path = rc_strcatpaths (RC_SVCDIR, tests[i], applet, (char *) NULL);
if (rc_exists (path)) if (rc_exists (path)) {
{
time_t m = get_mtime (path, false); time_t m = get_mtime (path, false);
if (mtime < m && m != 0) if (mtime < m && m != 0) {
{
free (path); free (path);
return (false); return (false);
} }
@ -232,10 +225,8 @@ static void cleanup (void)
if (ibsave) if (ibsave)
free (ibsave); free (ibsave);
if (in_control ()) if (in_control ()) {
{ if (rc_service_state (applet, rc_service_stopping)) {
if (rc_service_state (applet, rc_service_stopping))
{
/* If the we're shutting down, do it cleanly */ /* If the we're shutting down, do it cleanly */
if ((softlevel && if ((softlevel &&
rc_runlevel_stopping () && rc_runlevel_stopping () &&
@ -286,22 +277,18 @@ static bool svc_exec (const char *service, const char *arg1, const char *arg2)
if (pid == -1) if (pid == -1)
eerrorx ("%s: fork: %s", service, strerror (errno)); eerrorx ("%s: fork: %s", service, strerror (errno));
if (pid == 0) if (pid == 0) {
{
mycmd = rc_xstrdup (service); mycmd = rc_xstrdup (service);
myarg1 = rc_xstrdup (arg1); myarg1 = rc_xstrdup (arg1);
if (arg2) if (arg2)
myarg2 = rc_xstrdup (arg2); myarg2 = rc_xstrdup (arg2);
if (rc_exists (RC_SVCDIR "runscript.sh")) if (rc_exists (RC_SVCDIR "runscript.sh")) {
{
execl (RC_SVCDIR "runscript.sh", mycmd, mycmd, myarg1, myarg2, execl (RC_SVCDIR "runscript.sh", mycmd, mycmd, myarg1, myarg2,
(char *) NULL); (char *) NULL);
eerrorx ("%s: exec `" RC_SVCDIR "runscript.sh': %s", eerrorx ("%s: exec `" RC_SVCDIR "runscript.sh': %s",
service, strerror (errno)); service, strerror (errno));
} } else {
else
{
execl (RC_LIBDIR "sh/runscript.sh", mycmd, mycmd, myarg1, myarg2, execl (RC_LIBDIR "sh/runscript.sh", mycmd, mycmd, myarg1, myarg2,
(char *) NULL); (char *) NULL);
eerrorx ("%s: exec `" RC_LIBDIR "sh/runscript.sh': %s", eerrorx ("%s: exec `" RC_LIBDIR "sh/runscript.sh': %s",
@ -309,10 +296,8 @@ static bool svc_exec (const char *service, const char *arg1, const char *arg2)
} }
} }
do do {
{ if (waitpid (pid, &status, 0) < 0) {
if (waitpid (pid, &status, 0) < 0)
{
if (errno != ECHILD) if (errno != ECHILD)
eerror ("waitpid: %s", strerror (errno)); eerror ("waitpid: %s", strerror (errno));
break; break;
@ -335,36 +320,26 @@ static rc_service_state_t svc_status (const char *service)
rc_service_state_t retval = rc_service_stopped; rc_service_state_t retval = rc_service_stopped;
if (rc_service_state (service, rc_service_stopping)) if (rc_service_state (service, rc_service_stopping)) {
{
snprintf (status, sizeof (status), "stopping"); snprintf (status, sizeof (status), "stopping");
e = &ewarn; e = &ewarn;
retval = rc_service_stopping; retval = rc_service_stopping;
} } else if (rc_service_state (service, rc_service_starting)) {
else if (rc_service_state (service, rc_service_starting))
{
snprintf (status, sizeof (status), "starting"); snprintf (status, sizeof (status), "starting");
e = &ewarn; e = &ewarn;
retval = rc_service_starting; retval = rc_service_starting;
} } else if (rc_service_state (service, rc_service_inactive)) {
else if (rc_service_state (service, rc_service_inactive))
{
snprintf (status, sizeof (status), "inactive"); snprintf (status, sizeof (status), "inactive");
e = &ewarn; e = &ewarn;
retval = rc_service_inactive; retval = rc_service_inactive;
} } else if (rc_service_state (service, rc_service_crashed)) {
else if (rc_service_state (service, rc_service_crashed))
{
snprintf (status, sizeof (status), "crashed"); snprintf (status, sizeof (status), "crashed");
e = &eerror; e = &eerror;
retval = rc_service_crashed; retval = rc_service_crashed;
} } else if (rc_service_state (service, rc_service_started)) {
else if (rc_service_state (service, rc_service_started))
{
snprintf (status, sizeof (status), "started"); snprintf (status, sizeof (status), "started");
retval = rc_service_started; retval = rc_service_started;
} } else
else
snprintf (status, sizeof (status), "stopped"); snprintf (status, sizeof (status), "stopped");
e ("status: %s", status); e ("status: %s", status);
@ -391,8 +366,7 @@ static void make_exclusive (const char *service)
snprintf (mtime_test, i, "%s.%d", path, getpid ()); snprintf (mtime_test, i, "%s.%d", path, getpid ());
free (path); free (path);
if (rc_exists (mtime_test) && unlink (mtime_test) != 0) if (rc_exists (mtime_test) && unlink (mtime_test) != 0) {
{
eerror ("%s: unlink `%s': %s", eerror ("%s: unlink `%s': %s",
applet, mtime_test, strerror (errno)); applet, mtime_test, strerror (errno));
free (mtime_test); free (mtime_test);
@ -400,8 +374,7 @@ static void make_exclusive (const char *service)
return; return;
} }
if (symlink (service, mtime_test) != 0) if (symlink (service, mtime_test) != 0) {
{
eerror ("%s: symlink `%s' to `%s': %s", eerror ("%s: symlink `%s' to `%s': %s",
applet, service, mtime_test, strerror (errno)); applet, service, mtime_test, strerror (errno));
free (mtime_test); free (mtime_test);
@ -451,8 +424,7 @@ static void svc_start (const char *service, bool deps)
if (rc_is_env ("RC_STRICT_DEPEND", "yes")) if (rc_is_env ("RC_STRICT_DEPEND", "yes"))
depoptions |= RC_DEP_STRICT; depoptions |= RC_DEP_STRICT;
if (rc_is_env ("IN_HOTPLUG", "1") || in_background) if (rc_is_env ("IN_HOTPLUG", "1") || in_background) {
{
if (! rc_service_state (service, rc_service_inactive)) if (! rc_service_state (service, rc_service_inactive))
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
background = true; background = true;
@ -472,8 +444,7 @@ static void svc_start (const char *service, bool deps)
make_exclusive (service); make_exclusive (service);
if (deps) if (deps) {
{
if (! deptree && ((deptree = rc_load_deptree ()) == NULL)) if (! deptree && ((deptree = rc_load_deptree ()) == NULL))
eerrorx ("failed to load deptree"); eerrorx ("failed to load deptree");
@ -483,11 +454,9 @@ static void svc_start (const char *service, bool deps)
svclist = rc_strlist_add (NULL, applet); svclist = rc_strlist_add (NULL, applet);
rc_strlist_free (services); rc_strlist_free (services);
services = rc_get_depends (deptree, types, svclist, softlevel, 0); services = rc_get_depends (deptree, types, svclist, softlevel, 0);
if (services) if (services) {
{
eerrorn ("ERROR: `%s' needs ", applet); eerrorn ("ERROR: `%s' needs ", applet);
STRLIST_FOREACH (services, svc, i) STRLIST_FOREACH (services, svc, i) {
{
if (i > 0) if (i > 0)
fprintf (stderr, ", "); fprintf (stderr, ", ");
fprintf (stderr, "%s", svc); fprintf (stderr, "%s", svc);
@ -503,8 +472,7 @@ static void svc_start (const char *service, bool deps)
need_services = rc_get_depends (deptree, types, svclist, need_services = rc_get_depends (deptree, types, svclist,
softlevel, depoptions); softlevel, depoptions);
types = rc_strlist_add (types, "iuse"); types = rc_strlist_add (types, "iuse");
if (! rc_runlevel_starting ()) if (! rc_runlevel_starting ()) {
{
services = rc_get_depends (deptree, types, svclist, services = rc_get_depends (deptree, types, svclist,
softlevel, depoptions); softlevel, depoptions);
STRLIST_FOREACH (services, svc, i) STRLIST_FOREACH (services, svc, i)
@ -523,8 +491,7 @@ static void svc_start (const char *service, bool deps)
rc_strlist_free (tmplist); rc_strlist_free (tmplist);
tmplist = NULL; tmplist = NULL;
STRLIST_FOREACH (services, svc, i) STRLIST_FOREACH (services, svc, i) {
{
if (rc_service_state (svc, rc_service_started)) if (rc_service_state (svc, rc_service_started))
continue; continue;
if (! rc_wait_service (svc)) if (! rc_wait_service (svc))
@ -533,8 +500,7 @@ static void svc_start (const char *service, bool deps)
continue; continue;
STRLIST_FOREACH (need_services, svc2, j) STRLIST_FOREACH (need_services, svc2, j)
if (strcmp (svc, svc2) == 0) if (strcmp (svc, svc2) == 0) {
{
if (rc_service_state (svc, rc_service_inactive) || if (rc_service_state (svc, rc_service_inactive) ||
rc_service_state (svc, rc_service_wasinactive)) rc_service_state (svc, rc_service_wasinactive))
tmplist = rc_strlist_add (tmplist, svc); tmplist = rc_strlist_add (tmplist, svc);
@ -544,8 +510,7 @@ static void svc_start (const char *service, bool deps)
} }
} }
if (tmplist) if (tmplist) {
{
int n = 0; int n = 0;
int len = 0; int len = 0;
char *p; char *p;
@ -557,8 +522,7 @@ static void svc_start (const char *service, bool deps)
rc_strlist_free (types); rc_strlist_free (types);
types = rc_strlist_add (NULL, "iprovide"); types = rc_strlist_add (NULL, "iprovide");
STRLIST_FOREACH (tmplist, svc, i) STRLIST_FOREACH (tmplist, svc, i) {
{
rc_schedule_start_service (svc, service); rc_schedule_start_service (svc, service);
rc_strlist_free (svclist); rc_strlist_free (svclist);
@ -576,10 +540,8 @@ static void svc_start (const char *service, bool deps)
len += 5; len += 5;
tmp = rc_xmalloc (sizeof (char *) * len); tmp = rc_xmalloc (sizeof (char *) * len);
p = tmp; p = tmp;
STRLIST_FOREACH (tmplist, svc, i) STRLIST_FOREACH (tmplist, svc, i) {
{ if (i > 1) {
if (i > 1)
{
if (i == n - 1) if (i == n - 1)
p += snprintf (p, len, " or "); p += snprintf (p, len, " or ");
else else
@ -606,14 +568,11 @@ static void svc_start (const char *service, bool deps)
if (ibsave) if (ibsave)
unsetenv ("IN_BACKGROUND"); unsetenv ("IN_BACKGROUND");
if (in_control ()) if (in_control ()) {
{ if (! started) {
if (! started)
{
if (rc_service_state (service, rc_service_wasinactive)) if (rc_service_state (service, rc_service_wasinactive))
rc_mark_service (service, rc_service_inactive); rc_mark_service (service, rc_service_inactive);
else else {
{
rc_mark_service (service, rc_service_stopped); rc_mark_service (service, rc_service_stopped);
if (rc_runlevel_starting ()) if (rc_runlevel_starting ())
rc_mark_service (service, rc_service_failed); rc_mark_service (service, rc_service_failed);
@ -624,9 +583,7 @@ static void svc_start (const char *service, bool deps)
rc_mark_service (service, rc_service_started); rc_mark_service (service, rc_service_started);
unlink_mtime_test (); unlink_mtime_test ();
rc_plugin_run (rc_hook_service_start_done, applet); rc_plugin_run (rc_hook_service_start_done, applet);
} } else {
else
{
rc_plugin_run (rc_hook_service_start_done, applet); rc_plugin_run (rc_hook_service_start_done, applet);
if (rc_service_state (service, rc_service_inactive)) if (rc_service_state (service, rc_service_inactive))
ewarnx ("WARNING: %s has started, but is inactive", applet); ewarnx ("WARNING: %s has started, but is inactive", applet);
@ -651,8 +608,7 @@ static void svc_start (const char *service, bool deps)
rc_strlist_free (tmplist); rc_strlist_free (tmplist);
tmplist = rc_get_depends (deptree, types, svclist, softlevel, depoptions); tmplist = rc_get_depends (deptree, types, svclist, softlevel, depoptions);
STRLIST_FOREACH (tmplist, svc2, j) STRLIST_FOREACH (tmplist, svc2, j) {
{
rc_strlist_free (services); rc_strlist_free (services);
services = rc_services_scheduled (svc2); services = rc_services_scheduled (svc2);
STRLIST_FOREACH (services, svc, i) STRLIST_FOREACH (services, svc, i)
@ -692,8 +648,7 @@ static void svc_stop (const char *service, bool deps)
rc_service_in_runlevel (service, RC_LEVEL_BOOT)) rc_service_in_runlevel (service, RC_LEVEL_BOOT))
ewarn ("WARNING: you are stopping a boot service"); ewarn ("WARNING: you are stopping a boot service");
if (deps || ! rc_service_state (service, rc_service_wasinactive)) if (deps || ! rc_service_state (service, rc_service_wasinactive)) {
{
int depoptions = RC_DEP_TRACE; int depoptions = RC_DEP_TRACE;
char *svc; char *svc;
int i; int i;
@ -714,8 +669,7 @@ static void svc_stop (const char *service, bool deps)
services = rc_get_depends (deptree, types, svclist, services = rc_get_depends (deptree, types, svclist,
softlevel, depoptions); softlevel, depoptions);
rc_strlist_reverse (services); rc_strlist_reverse (services);
STRLIST_FOREACH (services, svc, i) STRLIST_FOREACH (services, svc, i) {
{
if (rc_service_state (svc, rc_service_started) || if (rc_service_state (svc, rc_service_started) ||
rc_service_state (svc, rc_service_inactive)) rc_service_state (svc, rc_service_inactive))
{ {
@ -731,15 +685,13 @@ static void svc_stop (const char *service, bool deps)
rc_strlist_free (services); rc_strlist_free (services);
services = NULL; services = NULL;
STRLIST_FOREACH (tmplist, svc, i) STRLIST_FOREACH (tmplist, svc, i) {
{
if (rc_service_state (svc, rc_service_stopped)) if (rc_service_state (svc, rc_service_stopped))
continue; continue;
/* We used to loop 3 times here - maybe re-do this if needed */ /* We used to loop 3 times here - maybe re-do this if needed */
rc_wait_service (svc); rc_wait_service (svc);
if (! rc_service_state (svc, rc_service_stopped)) if (! rc_service_state (svc, rc_service_stopped)) {
{
if (rc_runlevel_stopping ()) if (rc_runlevel_stopping ())
rc_mark_service (svc, rc_service_failed); rc_mark_service (svc, rc_service_failed);
eerrorx ("ERROR: cannot stop %s as %s is still up", eerrorx ("ERROR: cannot stop %s as %s is still up",
@ -755,8 +707,7 @@ static void svc_stop (const char *service, bool deps)
types = rc_strlist_add (types, "ibefore"); types = rc_strlist_add (types, "ibefore");
services = rc_get_depends (deptree, types, svclist, services = rc_get_depends (deptree, types, svclist,
softlevel, depoptions); softlevel, depoptions);
STRLIST_FOREACH (services, svc, i) STRLIST_FOREACH (services, svc, i) {
{
if (rc_service_state (svc, rc_service_stopped)) if (rc_service_state (svc, rc_service_stopped))
continue; continue;
rc_wait_service (svc); rc_wait_service (svc);
@ -775,14 +726,12 @@ static void svc_stop (const char *service, bool deps)
if (ibsave) if (ibsave)
unsetenv ("IN_BACKGROUND"); unsetenv ("IN_BACKGROUND");
if (! in_control ()) if (! in_control ()) {
{
rc_plugin_run (rc_hook_service_stop_done, applet); rc_plugin_run (rc_hook_service_stop_done, applet);
ewarnx ("WARNING: %s not under our control, aborting", applet); ewarnx ("WARNING: %s not under our control, aborting", applet);
} }
if (! stopped) if (! stopped) {
{
if (rc_service_state (service, rc_service_wasinactive)) if (rc_service_state (service, rc_service_wasinactive))
rc_mark_service (service, rc_service_inactive); rc_mark_service (service, rc_service_inactive);
else else
@ -816,8 +765,7 @@ static void svc_restart (const char *service, bool deps)
which means just that. The downside is that there is a small window when which means just that. The downside is that there is a small window when
our status is invalid. our status is invalid.
One workaround would be to introduce a new status, or status locking. */ One workaround would be to introduce a new status, or status locking. */
if (! deps) if (! deps) {
{
if (rc_service_state (service, rc_service_started) || if (rc_service_state (service, rc_service_started) ||
rc_service_state (service, rc_service_inactive)) rc_service_state (service, rc_service_inactive))
svc_exec (service, "stop", "start"); svc_exec (service, "stop", "start");
@ -826,8 +774,7 @@ static void svc_restart (const char *service, bool deps)
return; return;
} }
if (! rc_service_state (service, rc_service_stopped)) if (! rc_service_state (service, rc_service_stopped)) {
{
get_started_services (); get_started_services ();
svc_stop (service, deps); svc_stop (service, deps);
@ -845,17 +792,13 @@ static void svc_restart (const char *service, bool deps)
rc_service_state (service, rc_service_starting) || rc_service_state (service, rc_service_starting) ||
rc_service_state (service, rc_service_started)) rc_service_state (service, rc_service_started))
{ {
STRLIST_FOREACH (restart_services, svc, i) STRLIST_FOREACH (restart_services, svc, i) {
{ if (rc_service_state (svc, rc_service_stopped)) {
if (rc_service_state (svc, rc_service_stopped)) if (inactive) {
{
if (inactive)
{
rc_schedule_start_service (service, svc); rc_schedule_start_service (service, svc);
ewarn ("WARNING: %s is scheduled to started when %s has started", ewarn ("WARNING: %s is scheduled to started when %s has started",
svc, basename (service)); svc, basename (service));
} } else
else
rc_start_service (svc); rc_start_service (svc);
} }
} }
@ -876,8 +819,7 @@ int main (int argc, char **argv)
atexit (cleanup); atexit (cleanup);
/* Show help if insufficient args */ /* Show help if insufficient args */
if (argc < 3) if (argc < 3) {
{
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL); execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
applet, strerror (errno)); applet, strerror (errno));
@ -886,8 +828,7 @@ int main (int argc, char **argv)
#ifdef __linux__ #ifdef __linux__
/* coldplug events can trigger init scripts, but we don't want to run them /* coldplug events can trigger init scripts, but we don't want to run them
until after rc sysinit has completed so we punt them to the boot runlevel */ until after rc sysinit has completed so we punt them to the boot runlevel */
if (rc_exists ("/dev/.rcsysinit")) if (rc_exists ("/dev/.rcsysinit")) {
{
eerror ("%s: cannot run until sysvinit completes", applet); eerror ("%s: cannot run until sysvinit completes", applet);
if (mkdir ("/dev/.rcboot", 0755) != 0 && errno != EEXIST) if (mkdir ("/dev/.rcboot", 0755) != 0 && errno != EEXIST)
eerrorx ("%s: mkdir `/dev/.rcboot': %s", applet, strerror (errno)); eerrorx ("%s: mkdir `/dev/.rcboot': %s", applet, strerror (errno));
@ -897,15 +838,13 @@ int main (int argc, char **argv)
} }
#endif #endif
if ((softlevel = getenv ("RC_SOFTLEVEL")) == NULL) if ((softlevel = getenv ("RC_SOFTLEVEL")) == NULL) {
{
/* Ensure our environment is pure /* Ensure our environment is pure
Also, add our configuration to it */ Also, add our configuration to it */
env = rc_filter_env (); env = rc_filter_env ();
env = rc_config_env (env); env = rc_config_env (env);
if (env) if (env) {
{
char *p; char *p;
#ifdef __linux__ #ifdef __linux__
@ -918,8 +857,7 @@ int main (int argc, char **argv)
We could manipulate environ directly ourselves, but it seems that We could manipulate environ directly ourselves, but it seems that
some kernels bitch about this according to the environ man pages some kernels bitch about this according to the environ man pages
so we walk though environ and call unsetenv for each value. */ so we walk though environ and call unsetenv for each value. */
while (environ[0]) while (environ[0]) {
{
tmp = rc_xstrdup (environ[0]); tmp = rc_xstrdup (environ[0]);
p = tmp; p = tmp;
var = strsep (&p, "="); var = strsep (&p, "=");
@ -950,8 +888,7 @@ int main (int argc, char **argv)
snprintf (pid, sizeof (pid), "%d", (int) getpid ()); snprintf (pid, sizeof (pid), "%d", (int) getpid ());
setenv ("RC_RUNSCRIPT_PID", pid, 1); setenv ("RC_RUNSCRIPT_PID", pid, 1);
if (rc_is_env ("RC_PARALLEL_STARTUP", "yes")) if (rc_is_env ("RC_PARALLEL_STARTUP", "yes")) {
{
char ebname[PATH_MAX]; char ebname[PATH_MAX];
char *eb; char *eb;
@ -963,8 +900,7 @@ int main (int argc, char **argv)
/* Save the IN_BACKGROUND env flag so it's ONLY passed to the service /* Save the IN_BACKGROUND env flag so it's ONLY passed to the service
that is being called and not any dependents */ that is being called and not any dependents */
if (getenv ("IN_BACKGROUND")) if (getenv ("IN_BACKGROUND")) {
{
in_background = rc_is_env ("IN_BACKGROUND", "true"); in_background = rc_is_env ("IN_BACKGROUND", "true");
ibsave = strdup (getenv ("IN_BACKGROUND")); ibsave = strdup (getenv ("IN_BACKGROUND"));
unsetenv ("IN_BACKGROUND"); unsetenv ("IN_BACKGROUND");
@ -976,20 +912,17 @@ int main (int argc, char **argv)
#endif #endif
/* Right then, parse any options there may be */ /* Right then, parse any options there may be */
for (i = 2; i < argc; i++) for (i = 2; i < argc; i++) {
{
if (strlen (argv[i]) < 2 || argv[i][0] != '-' || argv[i][1] != '-') if (strlen (argv[i]) < 2 || argv[i][0] != '-' || argv[i][1] != '-')
continue; continue;
if (strcmp (argv[i], "--debug") == 0) if (strcmp (argv[i], "--debug") == 0)
setenv ("RC_DEBUG", "yes", 1); setenv ("RC_DEBUG", "yes", 1);
else if (strcmp (argv[i], "--help") == 0) else if (strcmp (argv[i], "--help") == 0) {
{
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL); execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
applet, strerror (errno)); applet, strerror (errno));
} } else if (strcmp (argv[i],"--ifstarted") == 0)
else if (strcmp (argv[i],"--ifstarted") == 0)
ifstarted = true; ifstarted = true;
else if (strcmp (argv[i], "--nocolour") == 0 || else if (strcmp (argv[i], "--nocolour") == 0 ||
strcmp (argv[i], "--nocolor") == 0) strcmp (argv[i], "--nocolor") == 0)
@ -1006,15 +939,13 @@ int main (int argc, char **argv)
eerror ("%s: unknown option `%s'", applet, argv[i]); eerror ("%s: unknown option `%s'", applet, argv[i]);
} }
if (ifstarted && ! rc_service_state (applet, rc_service_started)) if (ifstarted && ! rc_service_state (applet, rc_service_started)) {
{
if (! rc_is_env("RC_QUIET", "yes")) if (! rc_is_env("RC_QUIET", "yes"))
eerror ("ERROR: %s is not started", applet); eerror ("ERROR: %s is not started", applet);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if (rc_is_env ("IN_HOTPLUG", "1")) if (rc_is_env ("IN_HOTPLUG", "1")) {
{
if (! rc_is_env ("RC_HOTPLUG", "yes") || ! rc_allow_plug (applet)) if (! rc_is_env ("RC_HOTPLUG", "yes") || ! rc_allow_plug (applet))
eerrorx ("%s: not allowed to be hotplugged", applet); eerrorx ("%s: not allowed to be hotplugged", applet);
} }
@ -1031,8 +962,7 @@ int main (int argc, char **argv)
/* Now run each option */ /* Now run each option */
retval = EXIT_SUCCESS; retval = EXIT_SUCCESS;
for (i = 2; i < argc; i++) for (i = 2; i < argc; i++) {
{
/* Abort on a sighup here */ /* Abort on a sighup here */
if (sighup) if (sighup)
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
@ -1059,13 +989,10 @@ int main (int argc, char **argv)
svc_restart (service, deps); svc_restart (service, deps);
else if (strcmp (argv[i], "start") == 0) else if (strcmp (argv[i], "start") == 0)
svc_start (service, deps); svc_start (service, deps);
else if (strcmp (argv[i], "status") == 0) else if (strcmp (argv[i], "status") == 0) {
{
rc_service_state_t r = svc_status (service); rc_service_state_t r = svc_status (service);
retval = (int) r; retval = (int) r;
} } else if (strcmp (argv[i], "stop") == 0) {
else if (strcmp (argv[i], "stop") == 0)
{
if (in_background) if (in_background)
get_started_services (); get_started_services ();
@ -1085,20 +1012,15 @@ int main (int argc, char **argv)
if (rc_service_state (svc, rc_service_stopped)) if (rc_service_state (svc, rc_service_stopped))
rc_schedule_start_service (service, svc); rc_schedule_start_service (service, svc);
} }
} } else if (strcmp (argv[i], "zap") == 0) {
else if (strcmp (argv[i], "zap") == 0)
{
einfo ("Manually resetting %s to stopped state", applet); einfo ("Manually resetting %s to stopped state", applet);
rc_mark_service (applet, rc_service_stopped); rc_mark_service (applet, rc_service_stopped);
uncoldplug (applet); uncoldplug (applet);
} } else if (strcmp (argv[i], "help") == 0) {
else if (strcmp (argv[i], "help") == 0)
{
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, "help", (char *) NULL); execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, "help", (char *) NULL);
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
applet, strerror (errno)); applet, strerror (errno));
} }else
else
svc_exec (service, argv[i], NULL); svc_exec (service, argv[i], NULL);
/* Flush our buffered output if any */ /* Flush our buffered output if any */
@ -1109,8 +1031,7 @@ int main (int argc, char **argv)
restart_services = NULL; restart_services = NULL;
} }
if (! doneone) if (! doneone) {
{
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL); execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
applet, strerror (errno)); applet, strerror (errno));

View File

@ -70,8 +70,7 @@ static void free_schedulelist (schedulelist_t **list)
schedulelist_t *here; schedulelist_t *here;
schedulelist_t *next; schedulelist_t *next;
for (here = *list; here; here = next) for (here = *list; here; here = next) {
{
next = here->next; next = here->next;
free (here); free (here);
} }
@ -127,8 +126,7 @@ static int parse_signal (const char *sig)
if (! sig || strlen (sig) == 0) if (! sig || strlen (sig) == 0)
return (-1); return (-1);
if (sscanf (sig, "%u", &i) == 1) if (sscanf (sig, "%u", &i) == 1) {
{
if (i > 0 && i < sizeof (signallist) / sizeof (signallist[0])) if (i > 0 && i < sizeof (signallist) / sizeof (signallist[0]))
return (i); return (i);
eerrorx ("%s: `%s' is not a valid signal", progname, sig); eerrorx ("%s: `%s' is not a valid signal", progname, sig);
@ -154,15 +152,13 @@ static void parse_schedule_item (schedulelist_t *item, const char *string)
if (strcmp (string,"forever") == 0) if (strcmp (string,"forever") == 0)
item->type = schedule_forever; item->type = schedule_forever;
else if (isdigit (string[0])) else if (isdigit (string[0])) {
{
item->type = schedule_timeout; item->type = schedule_timeout;
errno = 0; errno = 0;
if (sscanf (string, "%d", &item->value) != 1) if (sscanf (string, "%d", &item->value) != 1)
eerrorx ("%s: invalid timeout value in schedule `%s'", progname, eerrorx ("%s: invalid timeout value in schedule `%s'", progname,
string); string);
} } else if ((after_hyph = string + (string[0] == '-')) &&
else if ((after_hyph = string + (string[0] == '-')) &&
((sig = parse_signal (after_hyph)) != -1)) ((sig = parse_signal (after_hyph)) != -1))
{ {
item->type = schedule_signal; item->type = schedule_signal;
@ -192,16 +188,14 @@ static void parse_schedule (const char *string, int default_signal)
schedule = rc_xmalloc (sizeof (schedulelist_t)); schedule = rc_xmalloc (sizeof (schedulelist_t));
schedule->gotolist = NULL; schedule->gotolist = NULL;
if (count == 0) if (count == 0) {
{
schedule->type = schedule_signal; schedule->type = schedule_signal;
schedule->value = default_signal; schedule->value = default_signal;
schedule->next = rc_xmalloc (sizeof (schedulelist_t)); schedule->next = rc_xmalloc (sizeof (schedulelist_t));
next = schedule->next; next = schedule->next;
next->type = schedule_timeout; next->type = schedule_timeout;
next->gotolist = NULL; next->gotolist = NULL;
if (string) if (string) {
{
if (sscanf (string, "%d", &next->value) != 1) if (sscanf (string, "%d", &next->value) != 1)
eerrorx ("%s: invalid timeout value in schedule", progname); eerrorx ("%s: invalid timeout value in schedule", progname);
} }
@ -213,8 +207,7 @@ static void parse_schedule (const char *string, int default_signal)
} }
next = schedule; next = schedule;
while (string != NULL) while (string != NULL) {
{
if ((slash = strchr (string, '/'))) if ((slash = strchr (string, '/')))
len = slash - string; len = slash - string;
else else
@ -228,8 +221,7 @@ static void parse_schedule (const char *string, int default_signal)
string = slash ? slash + 1 : NULL; string = slash ? slash + 1 : NULL;
parse_schedule_item (next, buffer); parse_schedule_item (next, buffer);
if (next->type == schedule_forever) if (next->type == schedule_forever) {
{
if (repeatat) if (repeatat)
eerrorx ("%s: invalid schedule, `forever' appears more than once", eerrorx ("%s: invalid schedule, `forever' appears more than once",
progname); progname);
@ -238,16 +230,14 @@ static void parse_schedule (const char *string, int default_signal)
continue; continue;
} }
if (string) if (string) {
{
next->next = rc_xmalloc (sizeof (schedulelist_t)); next->next = rc_xmalloc (sizeof (schedulelist_t));
next = next->next; next = next->next;
next->gotolist = NULL; next->gotolist = NULL;
} }
} }
if (repeatat) if (repeatat) {
{
next->next = rc_xmalloc (sizeof (schedulelist_t)); next->next = rc_xmalloc (sizeof (schedulelist_t));
next = next->next; next = next->next;
next->type = schedule_goto; next->type = schedule_goto;
@ -267,15 +257,13 @@ static pid_t get_pid (const char *pidfile, bool quiet)
if (! pidfile) if (! pidfile)
return (-1); return (-1);
if ((fp = fopen (pidfile, "r")) == NULL) if ((fp = fopen (pidfile, "r")) == NULL) {
{
if (! quiet) if (! quiet)
eerror ("%s: fopen `%s': %s", progname, pidfile, strerror (errno)); eerror ("%s: fopen `%s': %s", progname, pidfile, strerror (errno));
return (-1); return (-1);
} }
if (fscanf (fp, "%d", &pid) != 1) if (fscanf (fp, "%d", &pid) != 1) {
{
if (! quiet) if (! quiet)
eerror ("%s: no pid found in `%s'", progname, pidfile); eerror ("%s: no pid found in `%s'", progname, pidfile);
fclose (fp); fclose (fp);
@ -304,10 +292,8 @@ static int do_stop (const char *exec, const char *cmd,
if ((pids = rc_find_pids (exec, cmd, uid, pid)) == NULL) if ((pids = rc_find_pids (exec, cmd, uid, pid)) == NULL)
return (0); return (0);
for (i = 0; pids[i]; i++) for (i = 0; pids[i]; i++) {
{ if (test) {
if (test)
{
if (! quiet) if (! quiet)
einfo ("Would send signal %d to PID %d", sig, pids[i]); einfo ("Would send signal %d to PID %d", sig, pids[i]);
nkilled++; nkilled++;
@ -318,17 +304,14 @@ static int do_stop (const char *exec, const char *cmd,
ebegin ("Sending signal %d to PID %d", sig, pids[i]); ebegin ("Sending signal %d to PID %d", sig, pids[i]);
errno = 0; errno = 0;
killed = (kill (pids[i], sig) == 0 || errno == ESRCH ? true : false); killed = (kill (pids[i], sig) == 0 || errno == ESRCH ? true : false);
if (! killed) if (! killed) {
{
if (! quiet) if (! quiet)
eerror ("%s: failed to send signal %d to PID %d: %s", eerror ("%s: failed to send signal %d to PID %d: %s",
progname, sig, pids[i], strerror (errno)); progname, sig, pids[i], strerror (errno));
if (verbose) if (verbose)
eend (1, NULL); eend (1, NULL);
nkilled = -1; nkilled = -1;
} } else {
else
{
if (verbose) if (verbose)
eend (0, NULL); eend (0, NULL);
if (nkilled != -1) if (nkilled != -1)
@ -352,8 +335,7 @@ static int run_stop_schedule (const char *exec, const char *cmd,
struct timeval now; struct timeval now;
struct timeval stopat; struct timeval stopat;
if (verbose) if (verbose) {
{
if (pidfile) if (pidfile)
einfo ("Will stop PID in pidfile `%s'", pidfile); einfo ("Will stop PID in pidfile `%s'", pidfile);
if (uid) if (uid)
@ -364,10 +346,8 @@ static int run_stop_schedule (const char *exec, const char *cmd,
einfo ("Will stop processes called `%s'", cmd); einfo ("Will stop processes called `%s'", cmd);
} }
while (item) while (item) {
{ switch (item->type) {
switch (item->type)
{
case schedule_goto: case schedule_goto:
item = item->gotolist; item = item->gotolist;
continue; continue;
@ -376,10 +356,8 @@ static int run_stop_schedule (const char *exec, const char *cmd,
nrunning = 0; nrunning = 0;
nkilled = do_stop (exec, cmd, pidfile, uid, item->value, nkilled = do_stop (exec, cmd, pidfile, uid, item->value,
quiet, verbose, test); quiet, verbose, test);
if (nkilled == 0) if (nkilled == 0) {
{ if (tkilled == 0) {
if (tkilled == 0)
{
if (! quiet) if (! quiet)
eerror ("%s: no matching processes found", progname); eerror ("%s: no matching processes found", progname);
} }
@ -391,29 +369,25 @@ static int run_stop_schedule (const char *exec, const char *cmd,
tkilled += nkilled; tkilled += nkilled;
break; break;
case schedule_timeout: case schedule_timeout:
if (item->value < 1) if (item->value < 1) {
{
item = NULL; item = NULL;
break; break;
} }
if (gettimeofday (&stopat, NULL) != 0) if (gettimeofday (&stopat, NULL) != 0) {
{
eerror ("%s: gettimeofday: %s", progname, strerror (errno)); eerror ("%s: gettimeofday: %s", progname, strerror (errno));
return (0); return (0);
} }
stopat.tv_sec += item->value; stopat.tv_sec += item->value;
while (1) while (1) {
{
if ((nrunning = do_stop (exec, cmd, pidfile, if ((nrunning = do_stop (exec, cmd, pidfile,
uid, 0, true, false, true)) == 0) uid, 0, true, false, true)) == 0)
return (true); return (true);
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = POLL_INTERVAL; tv.tv_usec = POLL_INTERVAL;
if (select (0, 0, 0, 0, &tv) < 0) if (select (0, 0, 0, 0, &tv) < 0) {
{
if (errno == EINTR) if (errno == EINTR)
eerror ("%s: caught an interupt", progname); eerror ("%s: caught an interupt", progname);
else else
@ -421,8 +395,7 @@ static int run_stop_schedule (const char *exec, const char *cmd,
return (0); return (0);
} }
if (gettimeofday (&now, NULL) != 0) if (gettimeofday (&now, NULL) != 0) {
{
eerror ("%s: gettimeofday: %s", progname, strerror (errno)); eerror ("%s: gettimeofday: %s", progname, strerror (errno));
return (0); return (0);
} }
@ -443,8 +416,7 @@ static int run_stop_schedule (const char *exec, const char *cmd,
if (test || (tkilled > 0 && nrunning == 0)) if (test || (tkilled > 0 && nrunning == 0))
return (nkilled); return (nkilled);
if (! quiet) if (! quiet) {
{
if (nrunning == 1) if (nrunning == 1)
eerror ("%s: %d process refused to stop", progname, nrunning); eerror ("%s: %d process refused to stop", progname, nrunning);
else else
@ -461,8 +433,7 @@ static void handle_signal (int sig)
int serrno = errno; int serrno = errno;
char signame[10] = { '\0' }; char signame[10] = { '\0' };
switch (sig) switch (sig) {
{
case SIGINT: case SIGINT:
if (! signame[0]) if (! signame[0])
snprintf (signame, sizeof (signame), "SIGINT"); snprintf (signame, sizeof (signame), "SIGINT");
@ -475,10 +446,8 @@ static void handle_signal (int sig)
eerrorx ("%s: caught %s, aborting", progname, signame); eerrorx ("%s: caught %s, aborting", progname, signame);
case SIGCHLD: case SIGCHLD:
while (1) while (1) {
{ if ((pid = waitpid (-1, &status, WNOHANG)) < 0) {
if ((pid = waitpid (-1, &status, WNOHANG)) < 0)
{
if (errno != ECHILD) if (errno != ECHILD)
eerror ("%s: waitpid: %s", progname, strerror (errno)); eerror ("%s: waitpid: %s", progname, strerror (errno));
break; break;
@ -570,8 +539,7 @@ int main (int argc, char **argv)
while ((c = getopt_long (argc, argv, while ((c = getopt_long (argc, argv,
"KN:R:Sbc:d:g:mn:op:qs:tu:r:vx:1:2:", "KN:R:Sbc:d:g:mn:op:qs:tu:r:vx:1:2:",
longopts, (int *) 0)) != -1) longopts, (int *) 0)) != -1)
switch (c) switch (c) {
{
case 'K': /* --stop */ case 'K': /* --stop */
stop = true; stop = true;
break; break;
@ -600,26 +568,21 @@ int main (int argc, char **argv)
char *p = optarg; char *p = optarg;
char *cu = strsep (&p, ":"); char *cu = strsep (&p, ":");
changeuser = strdup (cu); changeuser = strdup (cu);
if (sscanf (cu, "%d", &tid) != 1) if (sscanf (cu, "%d", &tid) != 1) {
{
struct passwd *pw = getpwnam (cu); struct passwd *pw = getpwnam (cu);
if (! pw) if (! pw)
eerrorx ("%s: user `%s' not found", progname, cu); eerrorx ("%s: user `%s' not found", progname, cu);
ch_uid = pw->pw_uid; ch_uid = pw->pw_uid;
} } else
else
ch_uid = tid; ch_uid = tid;
if (p) if (p) {
{
char *cg = strsep (&p, ":"); char *cg = strsep (&p, ":");
if (sscanf (cg, "%d", &tid) != 1) if (sscanf (cg, "%d", &tid) != 1) {
{
struct group *gr = getgrnam (cg); struct group *gr = getgrnam (cg);
if (! gr) if (! gr)
eerrorx ("%s: group `%s' not found", progname, cg); eerrorx ("%s: group `%s' not found", progname, cg);
ch_gid = gr->gr_gid; ch_gid = gr->gr_gid;
} } else
else
ch_gid = tid; ch_gid = tid;
} }
} }
@ -630,14 +593,12 @@ int main (int argc, char **argv)
break; break;
case 'g': /* --group <group>|<gid> */ case 'g': /* --group <group>|<gid> */
if (sscanf (optarg, "%d", &tid) != 1) if (sscanf (optarg, "%d", &tid) != 1) {
{
struct group *gr = getgrnam (optarg); struct group *gr = getgrnam (optarg);
if (! gr) if (! gr)
eerrorx ("%s: group `%s' not found", progname, optarg); eerrorx ("%s: group `%s' not found", progname, optarg);
ch_gid = gr->gr_gid; ch_gid = gr->gr_gid;
} } else
else
ch_gid = tid; ch_gid = tid;
break; break;
@ -670,14 +631,12 @@ int main (int argc, char **argv)
break; break;
case 'u': /* --user <username>|<uid> */ case 'u': /* --user <username>|<uid> */
if (sscanf (optarg, "%d", &tid) != 1) if (sscanf (optarg, "%d", &tid) != 1) {
{
struct passwd *pw = getpwnam (optarg); struct passwd *pw = getpwnam (optarg);
if (! pw) if (! pw)
eerrorx ("%s: user `%s' not found", progname, optarg); eerrorx ("%s: user `%s' not found", progname, optarg);
uid = pw->pw_uid; uid = pw->pw_uid;
} } else
else
uid = tid; uid = tid;
break; break;
@ -732,15 +691,13 @@ int main (int argc, char **argv)
argv += optind; argv += optind;
/* Validate that the binary rc_exists if we are starting */ /* Validate that the binary rc_exists if we are starting */
if (exec && start) if (exec && start) {
{
char *tmp; char *tmp;
if (ch_root) if (ch_root)
tmp = rc_strcatpaths (ch_root, exec, (char *) NULL); tmp = rc_strcatpaths (ch_root, exec, (char *) NULL);
else else
tmp = exec; tmp = exec;
if (! rc_is_file (tmp)) if (! rc_is_file (tmp)) {
{
eerror ("%s: %s does not exist", progname, tmp); eerror ("%s: %s does not exist", progname, tmp);
if (ch_root) if (ch_root)
free (tmp); free (tmp);
@ -750,12 +707,10 @@ int main (int argc, char **argv)
free (tmp); free (tmp);
} }
if (stop) if (stop) {
{
int result; int result;
if (! schedule) if (! schedule) {
{
if (test || oknodo) if (test || oknodo)
parse_schedule ("0", sig); parse_schedule ("0", sig);
else else
@ -780,8 +735,7 @@ int main (int argc, char **argv)
if (do_stop (exec, cmd, pidfile, uid, 0, true, false, true) > 0) if (do_stop (exec, cmd, pidfile, uid, 0, true, false, true) > 0)
eerrorx ("%s: %s is already running", progname, exec); eerrorx ("%s: %s is already running", progname, exec);
if (test) if (test) {
{
if (quiet) if (quiet)
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
@ -808,8 +762,7 @@ int main (int argc, char **argv)
Then we filter the environment accordingly */ Then we filter the environment accordingly */
unsetenv ("RC_SOFTLEVEL"); unsetenv ("RC_SOFTLEVEL");
if (verbose) if (verbose) {
{
ebegin ("Detaching to start `%s'", exec); ebegin ("Detaching to start `%s'", exec);
eindent (); eindent ();
} }
@ -822,8 +775,7 @@ int main (int argc, char **argv)
eerrorx ("%s: fork: %s", progname, strerror (errno)); eerrorx ("%s: fork: %s", progname, strerror (errno));
/* Child process - lets go! */ /* Child process - lets go! */
if (pid == 0) if (pid == 0) {
{
pid_t mypid = getpid (); pid_t mypid = getpid ();
#ifdef TIOCNOTTY #ifdef TIOCNOTTY
@ -832,8 +784,7 @@ int main (int argc, char **argv)
devnull_fd = open("/dev/null", O_RDWR); devnull_fd = open("/dev/null", O_RDWR);
if (nicelevel) if (nicelevel) {
{
if (setpriority (PRIO_PROCESS, mypid, nicelevel) == -1) if (setpriority (PRIO_PROCESS, mypid, nicelevel) == -1)
eerrorx ("%s: setpritory %d: %s", progname, nicelevel, eerrorx ("%s: setpritory %d: %s", progname, nicelevel,
strerror(errno)); strerror(errno));
@ -845,8 +796,7 @@ int main (int argc, char **argv)
if (ch_dir && chdir (ch_dir) < 0) if (ch_dir && chdir (ch_dir) < 0)
eerrorx ("%s: chdir `%s': %s", progname, ch_dir, strerror (errno)); eerrorx ("%s: chdir `%s': %s", progname, ch_dir, strerror (errno));
if (makepidfile && pidfile) if (makepidfile && pidfile) {
{
FILE *fp = fopen (pidfile, "w"); FILE *fp = fopen (pidfile, "w");
if (! fp) if (! fp)
eerrorx ("%s: fopen `%s': %s", progname, pidfile, strerror eerrorx ("%s: fopen `%s': %s", progname, pidfile, strerror
@ -878,11 +828,9 @@ int main (int argc, char **argv)
eerrorx ("%s: initgroups (%s, %d)", progname, changeuser, ch_gid); eerrorx ("%s: initgroups (%s, %d)", progname, changeuser, ch_gid);
if (ch_uid && setuid (ch_uid)) if (ch_uid && setuid (ch_uid))
eerrorx ("%s: unable to set userid to %d", progname, ch_uid); eerrorx ("%s: unable to set userid to %d", progname, ch_uid);
else else {
{
struct passwd *passwd = getpwuid (ch_uid); struct passwd *passwd = getpwuid (ch_uid);
if (passwd) if (passwd) {
{
unsetenv ("HOME"); unsetenv ("HOME");
if (passwd->pw_dir) if (passwd->pw_dir)
setenv ("HOME", passwd->pw_dir, 1); setenv ("HOME", passwd->pw_dir, 1);
@ -902,8 +850,7 @@ int main (int argc, char **argv)
/* Clean the environment of any RC_ variables */ /* Clean the environment of any RC_ variables */
STRLIST_FOREACH (environ, env, i) STRLIST_FOREACH (environ, env, i)
if (env && strncmp (env, "RC_", 3) != 0) if (env && strncmp (env, "RC_", 3) != 0) {
{
/* For the path character, remove the rcscript bin dir from it */ /* For the path character, remove the rcscript bin dir from it */
if (strncmp (env, "PATH=" RC_LIBDIR "bin:", if (strncmp (env, "PATH=" RC_LIBDIR "bin:",
strlen ("PATH=" RC_LIBDIR "bin:")) == 0) strlen ("PATH=" RC_LIBDIR "bin:")) == 0)
@ -917,8 +864,7 @@ int main (int argc, char **argv)
snprintf (newpath, len, "PATH=%s", path); snprintf (newpath, len, "PATH=%s", path);
newenv = rc_strlist_add (newenv, newpath); newenv = rc_strlist_add (newenv, newpath);
free (newpath); free (newpath);
} } else
else
newenv = rc_strlist_add (newenv, env); newenv = rc_strlist_add (newenv, env);
} }
@ -926,24 +872,22 @@ int main (int argc, char **argv)
stdout_fd = devnull_fd; stdout_fd = devnull_fd;
stderr_fd = devnull_fd; stderr_fd = devnull_fd;
if (redirect_stdout) if (redirect_stdout) {
{
if ((stdout_fd = open (redirect_stdout, O_WRONLY | O_CREAT | O_APPEND, if ((stdout_fd = open (redirect_stdout, O_WRONLY | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR)) == -1) S_IRUSR | S_IWUSR)) == -1)
eerrorx ("%s: unable to open the logfile for stdout `%s': %s", eerrorx ("%s: unable to open the logfile for stdout `%s': %s",
progname, redirect_stdout, strerror (errno)); progname, redirect_stdout, strerror (errno));
} }
if (redirect_stderr) if (redirect_stderr) {
{
if ((stderr_fd = open (redirect_stderr, O_WRONLY | O_CREAT | O_APPEND, if ((stderr_fd = open (redirect_stderr, O_WRONLY | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR)) == -1) S_IRUSR | S_IWUSR)) == -1)
eerrorx ("%s: unable to open the logfile for stderr `%s': %s", eerrorx ("%s: unable to open the logfile for stderr `%s': %s",
progname, redirect_stderr, strerror (errno)); progname, redirect_stderr, strerror (errno));
} }
if (background) {
/* Hmmm, some daemons may need stdin? */
dup2 (devnull_fd, STDIN_FILENO); dup2 (devnull_fd, STDIN_FILENO);
if (background)
{
dup2 (stdout_fd, STDOUT_FILENO); dup2 (stdout_fd, STDOUT_FILENO);
dup2 (stderr_fd, STDERR_FILENO); dup2 (stderr_fd, STDERR_FILENO);
} }
@ -962,25 +906,21 @@ int main (int argc, char **argv)
} }
/* Parent process */ /* Parent process */
if (! background) if (! background) {
{
/* As we're not backgrounding the process, wait for our pid to return */ /* As we're not backgrounding the process, wait for our pid to return */
int status = 0; int status = 0;
int savepid = pid; int savepid = pid;
errno = 0; errno = 0;
do do {
{
pid = waitpid (savepid, &status, 0); pid = waitpid (savepid, &status, 0);
if (pid < 1) if (pid < 1) {
{
eerror ("waitpid %d: %s", savepid, strerror (errno)); eerror ("waitpid %d: %s", savepid, strerror (errno));
return (-1); return (-1);
} }
} while (! WIFEXITED (status) && ! WIFSIGNALED (status)); } while (! WIFEXITED (status) && ! WIFSIGNALED (status));
if (! WIFEXITED (status) || WEXITSTATUS (status) != 0) if (! WIFEXITED (status) || WEXITSTATUS (status) != 0) {
{
if (! quiet) if (! quiet)
eerrorx ("%s: failed to started `%s'", progname, exec); eerrorx ("%s: failed to started `%s'", progname, exec);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
@ -991,8 +931,7 @@ int main (int argc, char **argv)
/* Wait a little bit and check that process is still running /* Wait a little bit and check that process is still running
We do this as some badly written daemons fork and then barf */ We do this as some badly written daemons fork and then barf */
if (START_WAIT > 0) if (START_WAIT > 0) {
{
struct timeval stopat; struct timeval stopat;
struct timeval now; struct timeval now;
@ -1000,14 +939,12 @@ int main (int argc, char **argv)
eerrorx ("%s: gettimeofday: %s", progname, strerror (errno)); eerrorx ("%s: gettimeofday: %s", progname, strerror (errno));
stopat.tv_usec += START_WAIT; stopat.tv_usec += START_WAIT;
while (1) while (1) {
{
bool alive = false; bool alive = false;
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = POLL_INTERVAL; tv.tv_usec = POLL_INTERVAL;
if (select (0, 0, 0, 0, &tv) < 0) if (select (0, 0, 0, 0, &tv) < 0) {
{
/* Let our signal handler handle the interupt */ /* Let our signal handler handle the interupt */
if (errno != EINTR) if (errno != EINTR)
eerrorx ("%s: select: %s", progname, strerror (errno)); eerrorx ("%s: select: %s", progname, strerror (errno));
@ -1022,20 +959,14 @@ int main (int argc, char **argv)
Failing that, we'll have to query processes. Failing that, we'll have to query processes.
We sleep first as some programs like ntp like to fork, and write We sleep first as some programs like ntp like to fork, and write
their pidfile a LONG time later. */ their pidfile a LONG time later. */
if (background) if (background) {
{
if (kill (pid, 0) == 0) if (kill (pid, 0) == 0)
alive = true; alive = true;
} } else {
else if (pidfile && rc_exists (pidfile)) {
{
if (pidfile && rc_exists (pidfile))
{
if (do_stop (NULL, NULL, pidfile, uid, 0, true, false, true) > 0) if (do_stop (NULL, NULL, pidfile, uid, 0, true, false, true) > 0)
alive = true; alive = true;
} } else {
else
{
if (do_stop (exec, cmd, NULL, uid, 0, true, false, true) > 0) if (do_stop (exec, cmd, NULL, uid, 0, true, false, true) > 0)
alive = true; alive = true;
} }