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

View File

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

View File

@ -40,8 +40,7 @@
/* A cheat sheet of colour capable terminals
This is taken from DIR_COLORS from GNU coreutils
We embed it here as we shouldn't depend on coreutils */
static const char *colour_terms[] =
{
static const char *colour_terms[] = {
"Eterm",
"ansi",
"color-xterm",
@ -113,10 +112,8 @@ bool colour_terminal (void)
if (! term)
return (true);
while (colour_terms[i])
{
if (strcmp (colour_terms[i], term) == 0)
{
while (colour_terms[i]) {
if (strcmp (colour_terms[i], term) == 0) {
in_colour = 1;
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)
return (0);
if (! (fp = fopen (file, "a")))
{
if (! (fp = fopen (file, "a"))) {
fprintf (stderr, "fopen `%s': %s\n", file, strerror (errno));
return (0);
}
fprintf (fp, "%s %d ", cmd, retval);
if (fmt)
{
if (fmt) {
va_list apc;
va_copy (apc, ap);
l = vsnprintf (buffer, sizeof (buffer), fmt, apc);
fprintf (fp, "%d %s\n", l, buffer);
va_end (apc);
}
else
} else
fprintf (fp, "0\n");
fclose (fp);
@ -224,18 +218,15 @@ void eflush (void)
char newfile[PATH_MAX];
int i = 1;
if (! file|| (stat (file, &buf) != 0))
{
if (! file|| (stat (file, &buf) != 0)) {
errno = 0;
return;
}
/* Find a unique name for our file */
while (true)
{
while (true) {
snprintf (newfile, sizeof (newfile), "%s.%d", file, i);
if (stat (newfile, &buf) != 0)
{
if (stat (newfile, &buf) != 0) {
if (rename (file, newfile))
fprintf (stderr, "rename `%s' `%s': %s\n", file, newfile,
strerror (errno));
@ -245,8 +236,7 @@ void eflush (void)
}
/* 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));
return;
}
@ -255,8 +245,7 @@ void eflush (void)
return;
/* Spin until we can lock the ebuffer */
while (true)
{
while (true) {
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 20000;
@ -269,8 +258,7 @@ void eflush (void)
strerror (errno));
}
if (! (fp = fopen (newfile, "r")))
{
if (! (fp = fopen (newfile, "r"))) {
fprintf (stderr, "fopen `%s': %s\n", newfile, strerror (errno));
return;
}
@ -278,8 +266,7 @@ void eflush (void)
unsetenv ("RC_EBUFFER");
memset (buffer, 0, RC_LINEBUFFER);
while (fgets (buffer, RC_LINEBUFFER, fp))
{
while (fgets (buffer, RC_LINEBUFFER, fp)) {
i = strlen (buffer) - 1;
if (i < 1)
continue;
@ -290,38 +277,30 @@ void eflush (void)
p = buffer;
cmd = 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);
continue;
}
token = strsep (&p, " ");
if (sscanf (token, "%d", &length) != 1)
{
if (sscanf (token, "%d", &length) != 1) {
fprintf (stderr, "eflush `%s': not a number", token);
continue;
}
i = 0;
while (funcmap[i].name)
{
if (strcmp (funcmap[i].name, cmd) == 0)
{
if (funcmap[i].efunc)
{
while (funcmap[i].name) {
if (strcmp (funcmap[i].name, cmd) == 0) {
if (funcmap[i].efunc) {
if (p)
funcmap[i].efunc ("%s", p);
else
funcmap[i].efunc (NULL, NULL);
}
else if (funcmap[i].eefunc)
{
} else if (funcmap[i].eefunc) {
if (p)
funcmap[i].eefunc (retval, "%s", p);
else
funcmap[i].eefunc (retval, NULL, NULL);
}
else if (funcmap[i].eind)
} else if (funcmap[i].eind)
funcmap[i].eind ();
else
fprintf (stderr, "eflush `%s': no function defined\n", cmd);
@ -344,8 +323,7 @@ void eflush (void)
_exit (EXIT_SUCCESS);
}
#define EBUFFER(_cmd, _retval, _fmt, _ap) \
{ \
#define EBUFFER(_cmd, _retval, _fmt, _ap) { \
int _i = ebuffer (_cmd, _retval, _fmt, _ap); \
if (_i) \
return (_i); \
@ -356,8 +334,7 @@ static void elog (int level, const char *fmt, va_list ap)
char *e = getenv ("RC_ELOG");
va_list apc;
if (fmt && e)
{
if (fmt && e) {
closelog ();
openlog (e, LOG_PID, LOG_DAEMON);
va_copy (apc, ap);
@ -366,14 +343,14 @@ static void elog (int level, const char *fmt, va_list ap)
closelog ();
}
}
static int _eindent (FILE *stream)
{
char *env = getenv ("RC_EINDENT");
int amount = 0;
char indent[INDENT_MAX];
if (env)
{
if (env) {
errno = 0;
amount = strtol (env, NULL, 0);
if (errno != 0 || amount < 0)
@ -484,8 +461,7 @@ int einfo (const char *fmt, ...)
return (0);
va_start (ap, fmt);
if (! (retval = ebuffer ("einfo", 0, fmt, ap)))
{
if (! (retval = ebuffer ("einfo", 0, fmt, ap))) {
retval = _einfovn (fmt, ap);
retval += printf ("\n");
}
@ -504,8 +480,7 @@ int ewarn (const char *fmt, ...)
va_start (ap, fmt);
elog (LOG_WARNING, fmt, ap);
if (! (retval = ebuffer ("ewarn", 0, fmt, ap)))
{
if (! (retval = ebuffer ("ewarn", 0, fmt, ap))) {
retval = _ewarnvn (fmt, ap);
retval += printf ("\n");
}
@ -519,8 +494,7 @@ void ewarnx (const char *fmt, ...)
int retval;
va_list ap;
if (fmt && ! is_env ("RC_QUIET", "yes"))
{
if (fmt && ! is_env ("RC_QUIET", "yes")) {
va_start (ap, fmt);
elog (LOG_WARNING, fmt, ap);
retval = _ewarnvn (fmt, ap);
@ -551,8 +525,7 @@ void eerrorx (const char *fmt, ...)
{
va_list ap;
if (fmt)
{
if (fmt) {
va_start (ap, fmt);
elog (LOG_ERR, fmt, ap);
_eerrorvn (fmt, ap);
@ -571,8 +544,7 @@ int ebegin (const char *fmt, ...)
return (0);
va_start (ap, fmt);
if ((retval = ebuffer ("ebegin", 0, fmt, ap)))
{
if ((retval = ebuffer ("ebegin", 0, fmt, ap))) {
va_end (ap);
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);
if (cols > 0 && colour_terminal ())
{
if (cols > 0 && colour_terminal ()) {
fprintf (fp, "\033[A\033[%dC %s[ ", cols, EINFO_BRACKET);
switch (color)
{
switch (color) {
case einfo_good:
fprintf (fp, EINFO_GOOD);
break;
@ -625,9 +595,7 @@ static void _eend (int col, einfo_color_t color, const char *msg)
break;
}
fprintf (fp, "%s%s ]%s\n", msg, EINFO_BRACKET, EINFO_NORMAL);
}
else
{
} else {
for (i = -1; i < cols - col; i++)
fprintf (fp, " ");
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;
int eb;
if (fmt)
{
if (fmt) {
va_copy (apc, ap);
eb = ebuffer (cmd, retval, fmt, 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);
}
if (fmt && retval != 0)
{
if (fmt && retval != 0) {
va_copy (apc, ap);
if (strcmp (cmd, "ewend") == 0)
{
if (strcmp (cmd, "ewend") == 0) {
col = _ewarnvn (fmt, apc);
fp = stdout;
}
else
{
} else {
col = _eerrorvn (fmt, apc);
fp = stderr;
}
@ -714,8 +677,7 @@ void eindent (void)
if (ebuffer ("eindent", 0, NULL, NULL))
return;
if (env)
{
if (env) {
errno = 0;
amount = strtol (env, NULL, 0);
if (errno != 0)
@ -751,8 +713,7 @@ void eoutdent (void)
if (amount <= 0)
unsetenv ("RC_EINDENT");
else
{
else {
snprintf (num, 10, "%08d", amount);
setenv ("RC_EINDENT", num, 1);
}
@ -805,8 +766,7 @@ int einfov (const char *fmt, ...)
return (0);
va_start (ap, fmt);
if (! (retval = ebuffer ("einfov", 0, fmt, ap)))
{
if (! (retval = ebuffer ("einfov", 0, fmt, ap))) {
retval = _einfovn (fmt, ap);
retval += printf ("\n");
}
@ -826,8 +786,7 @@ int ewarnv (const char *fmt, ...)
return (0);
va_start (ap, fmt);
if (! (retval = ebuffer ("ewarnv", 0, fmt, ap)))
{
if (! (retval = ebuffer ("ewarnv", 0, fmt, ap))) {
retval = _ewarnvn (fmt, ap);
retval += printf ("\n");
}
@ -848,8 +807,7 @@ int ebeginv (const char *fmt, ...)
return (0);
va_start (ap, fmt);
if (! (retval = ebuffer ("ebeginv", 0, fmt, ap)))
{
if (! (retval = ebuffer ("ebeginv", 0, fmt, ap))) {
retval = _einfovn (fmt, ap);
retval += printf (" ...");
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 != '(')
;
if (c != '(')
{
if (c != '(') {
fclose(fp);
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);
memset (buffer, 0, sizeof (buffer));
if (readlink (cmdline, buffer, sizeof (buffer)) != -1)
{
if (readlink (cmdline, buffer, sizeof (buffer)) != -1) {
if (strcmp (exec, buffer) == 0)
return (true);
/* We should cater for deleted binaries too */
if (strlen (buffer) > 10)
{
if (strlen (buffer) > 10) {
p = buffer + (strlen (buffer) - 10);
if (strcmp (p, " (deleted)") == 0)
{
if (strcmp (p, " (deleted)") == 0) {
*p = 0;
if (strcmp (buffer, exec) == 0)
return (true);
@ -132,14 +128,12 @@ pid_t *rc_find_pids (const char *exec, const char *cmd,
nasty
*/
if ((pp = getenv ("RC_RUNSCRIPT_PID")))
{
if ((pp = getenv ("RC_RUNSCRIPT_PID"))) {
if (sscanf (pp, "%d", &runscript_pid) != 1)
runscript_pid = 0;
}
while ((entry = readdir (procdir)) != NULL)
{
while ((entry = readdir (procdir)) != NULL) {
if (sscanf (entry->d_name, "%d", &p) != 1)
continue;
foundany = true;
@ -150,8 +144,7 @@ pid_t *rc_find_pids (const char *exec, const char *cmd,
if (pid != 0 && pid != p)
continue;
if (uid)
{
if (uid) {
snprintf (buffer, sizeof (buffer), "/proc/%d", pid);
if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
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),
&processes);
#endif
for (i = 0; i < processes; i++)
{
for (i = 0; i < processes; i++) {
pid_t p = _GET_KINFO_PID (kp[i]);
if (pid != 0 && pid != p)
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]))
continue;
if (cmd)
{
if (cmd) {
if (! _GET_KINFO_COMM (kp[i]) ||
strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0)
continue;
}
if (exec && ! cmd)
{
if (exec && ! cmd) {
if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv)
continue;
@ -273,14 +263,12 @@ static bool _match_daemon (const char *path, const char *file,
int lc = 0;
int m = 0;
if (! rc_exists (ffile))
{
if (! rc_exists (ffile)) {
free (ffile);
return (false);
}
if ((fp = fopen (ffile, "r")) == NULL)
{
if ((fp = fopen (ffile, "r")) == NULL) {
eerror ("fopen `%s': %s", ffile, strerror (errno));
free (ffile);
return (false);
@ -292,8 +280,7 @@ static bool _match_daemon (const char *path, const char *file,
m += 100;
memset (buffer, 0, sizeof (buffer));
while ((fgets (buffer, RC_LINEBUFFER, fp)))
{
while ((fgets (buffer, RC_LINEBUFFER, fp))) {
int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n')
buffer[lb] = 0;
@ -336,54 +323,42 @@ void rc_set_service_daemon (const char *service, const char *exec,
if (! exec && ! name && ! pidfile)
return;
if (exec)
{
if (exec) {
i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec);
}
else
} else
mexec = strdup ("exec=");
if (name)
{
if (name) {
i = strlen (name) + 6;
mname = rc_xmalloc (sizeof (char *) * i);
snprintf (mname, i, "name=%s", name);
}
else
} else
mname = strdup ("name=");
if (pidfile)
{
if (pidfile) {
i = strlen (pidfile) + 9;
mpidfile = rc_xmalloc (sizeof (char *) * i);
snprintf (mpidfile, i, "pidfile=%s", pidfile);
}
else
} else
mpidfile = strdup ("pidfile=");
/* Regardless, erase any existing daemon info */
if (rc_is_dir (dirpath))
{
if (rc_is_dir (dirpath)) {
char *oldfile = NULL;
files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i)
{
STRLIST_FOREACH (files, file, i) {
ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
nfiles++;
if (! oldfile)
{
if (_match_daemon (dirpath, file, mexec, mname, mpidfile))
{
if (! oldfile) {
if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) {
unlink (ffile);
oldfile = ffile;
nfiles--;
}
}
else
{
} else {
rename (ffile, oldfile);
free (oldfile);
oldfile = ffile;
@ -395,8 +370,7 @@ void rc_set_service_daemon (const char *service, const char *exec,
}
/* Now store our daemon info */
if (started)
{
if (started) {
char buffer[10];
FILE *fp;
@ -408,8 +382,7 @@ void rc_set_service_daemon (const char *service, const char *exec,
file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
if ((fp = fopen (file, "w")) == NULL)
eerror ("fopen `%s': %s", file, strerror (errno));
else
{
else {
fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
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),
(char *) NULL);
if (! rc_is_dir (dirpath))
{
if (! rc_is_dir (dirpath)) {
free (dirpath);
return (false);
}
@ -446,19 +418,15 @@ bool rc_service_started_daemon (const char *service, const char *exec,
mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec);
if (indx > 0)
{
if (indx > 0) {
int len = sizeof (char *) * 10;
file = rc_xmalloc (len);
snprintf (file, len, "%03d", indx);
retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
free (file);
}
else
{
} else {
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);
if (retval)
break;
@ -493,27 +461,23 @@ bool rc_service_daemons_crashed (const char *service)
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL);
if (! rc_is_dir (dirpath))
{
if (! rc_is_dir (dirpath)) {
free (dirpath);
return (false);
}
memset (buffer, 0, sizeof (buffer));
files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i)
{
STRLIST_FOREACH (files, file, i) {
path = rc_strcatpaths (dirpath, file, (char *) NULL);
fp = fopen (path, "r");
free (path);
if (! fp)
{
if (! fp) {
eerror ("fopen `%s': %s", file, strerror (errno));
continue;
}
while ((fgets (buffer, RC_LINEBUFFER, fp)))
{
while ((fgets (buffer, RC_LINEBUFFER, fp))) {
int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n')
buffer[lb] = 0;
@ -525,20 +489,15 @@ bool rc_service_daemons_crashed (const char *service)
if (strlen (p) == 0)
continue;
if (strcmp (token, "exec") == 0)
{
if (strcmp (token, "exec") == 0) {
if (exec)
free (exec);
exec = strdup (p);
}
else if (strcmp (token, "name") == 0)
{
} else if (strcmp (token, "name") == 0) {
if (name)
free (name);
name = strdup (p);
}
else if (strcmp (token, "pidfile") == 0)
{
} else if (strcmp (token, "pidfile") == 0) {
if (pidfile)
free (pidfile);
pidfile = strdup (p);
@ -547,23 +506,19 @@ bool rc_service_daemons_crashed (const char *service)
fclose (fp);
pid = 0;
if (pidfile)
{
if (! rc_exists (pidfile))
{
if (pidfile) {
if (! rc_exists (pidfile)) {
retval = true;
break;
}
if ((fp = fopen (pidfile, "r")) == NULL)
{
if ((fp = fopen (pidfile, "r")) == NULL) {
eerror ("fopen `%s': %s", pidfile, strerror (errno));
retval = true;
break;
}
if (fscanf (fp, "%d", &pid) != 1)
{
if (fscanf (fp, "%d", &pid) != 1) {
eerror ("no pid found in `%s'", pidfile);
fclose (fp);
retval = true;
@ -575,32 +530,27 @@ bool rc_service_daemons_crashed (const char *service)
pidfile = NULL;
}
if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL)
{
if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) {
retval = true;
break;
}
free (pids);
if (exec)
{
if (exec) {
free (exec);
exec = NULL;
}
if (name)
{
if (name) {
free (name);
name = NULL;
}
}
if (exec)
{
if (exec) {
free (exec);
exec = NULL;
}
if (name)
{
if (name) {
free (name);
name = NULL;
}

View File

@ -119,8 +119,7 @@ char *rc_strcatpaths (const char *path1, const char *paths, ...)
memset (path, 0, length);
memcpy (path, path1, strlen (path1));
pathp = path + strlen (path1) - 1;
if (*pathp != '/')
{
if (*pathp != '/') {
pathp++;
*pathp++ = '/';
}
@ -130,8 +129,7 @@ char *rc_strcatpaths (const char *path1, const char *paths, ...)
pathp += strlen (paths);
va_start (ap, paths);
while ((p = va_arg (ap, char *)) != NULL)
{
while ((p = va_arg (ap, char *)) != NULL) {
if (*pathp != '/')
*pathp++ = '/';
i = strlen (p);
@ -223,19 +221,15 @@ char **rc_ls_dir (char **list, const char *dir, int options)
if (! dir)
return (list);
if ((dp = opendir (dir)) == NULL)
{
if ((dp = opendir (dir)) == NULL) {
eerror ("failed to opendir `%s': %s", dir, strerror (errno));
return (list);
}
errno = 0;
while (((d = readdir (dp)) != NULL) && errno == 0)
{
if (d->d_name[0] != '.')
{
if (options & RC_LS_INITD)
{
while (((d = readdir (dp)) != NULL) && errno == 0) {
if (d->d_name[0] != '.') {
if (options & RC_LS_INITD) {
int l = strlen (d->d_name);
char *init = rc_strcatpaths (RC_INITDIR, d->d_name,
(char *) NULL);
@ -255,8 +249,7 @@ char **rc_ls_dir (char **list, const char *dir, int options)
}
closedir (dp);
if (errno != 0)
{
if (errno != 0) {
eerror ("failed to readdir `%s': %s", dir, strerror (errno));
rc_strlist_free (list);
return (NULL);
@ -273,31 +266,24 @@ bool rc_rm_dir (const char *pathname, bool top)
if (! pathname)
return (false);
if ((dp = opendir (pathname)) == NULL)
{
if ((dp = opendir (pathname)) == NULL) {
eerror ("failed to opendir `%s': %s", pathname, strerror (errno));
return (false);
}
errno = 0;
while (((d = readdir (dp)) != NULL) && errno == 0)
{
if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
{
while (((d = readdir (dp)) != NULL) && errno == 0) {
if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) {
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))
{
free (tmp);
closedir (dp);
return (false);
}
}
else
{
if (unlink (tmp))
{
} else {
if (unlink (tmp)) {
eerror ("failed to unlink `%s': %s", tmp, strerror (errno));
free (tmp);
closedir (dp);
@ -311,8 +297,7 @@ bool rc_rm_dir (const char *pathname, bool top)
eerror ("failed to readdir `%s': %s", pathname, strerror (errno));
closedir (dp);
if (top && rmdir (pathname) != 0)
{
if (top && rmdir (pathname) != 0) {
eerror ("failed to rmdir `%s': %s", pathname, strerror (errno));
return false;
}
@ -334,14 +319,12 @@ char **rc_get_config (char **list, const char *file)
char *entry;
char *newline;
if (! (fp = fopen (file, "r")))
{
if (! (fp = fopen (file, "r"))) {
ewarn ("load_config_file `%s': %s", file, strerror (errno));
return (list);
}
while (fgets (buffer, RC_LINEBUFFER, fp))
{
while (fgets (buffer, RC_LINEBUFFER, fp)) {
p = buffer;
/* Strip leading spaces/tabs */
@ -358,12 +341,10 @@ char **rc_get_config (char **list, const char *file)
entry = rc_xstrdup (token);
do
{
do {
/* Bash variables are usually quoted */
token = strsep (&p, "\"\'");
}
while ((token) && (strlen (token) == 0));
} while ((token) && (strlen (token) == 0));
/* Drop a newline if that's all we have */
i = strlen (token) - 1;
@ -377,13 +358,11 @@ char **rc_get_config (char **list, const char *file)
replaced = false;
/* In shells the last item takes precedence, so we need to remove
any prior values we may already have */
STRLIST_FOREACH (list, line, i)
{
STRLIST_FOREACH (list, line, i) {
char *tmp = rc_xstrdup (line);
linep = tmp;
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 */
free (list[i - 1]);
list[i - 1] = newline;
@ -394,8 +373,7 @@ char **rc_get_config (char **list, const char *file)
free (tmp);
}
if (! replaced)
{
if (! replaced) {
list = rc_strlist_addsort (list, newline);
free (newline);
}
@ -412,8 +390,7 @@ char *rc_get_config_entry (char **list, const char *entry)
int i;
char *p;
STRLIST_FOREACH (list, line, i)
{
STRLIST_FOREACH (list, line, i) {
p = strchr (line, '=');
if (p && strncmp (entry, line, p - line) == 0)
return (p += 1);
@ -429,14 +406,12 @@ char **rc_get_list (char **list, const char *file)
char *p;
char *token;
if (! (fp = fopen (file, "r")))
{
if (! (fp = fopen (file, "r"))) {
ewarn ("rc_get_list `%s': %s", file, strerror (errno));
return (list);
}
while (fgets (buffer, RC_LINEBUFFER, fp))
{
while (fgets (buffer, RC_LINEBUFFER, fp)) {
p = buffer;
/* Strip leading spaces/tabs */
@ -445,8 +420,7 @@ char **rc_get_list (char **list, const char *file)
/* Get entry - we do not want comments */
token = strsep (&p, "#");
if (token && (strlen (token) > 1))
{
if (token && (strlen (token) > 1)) {
/* Stip the newline if present */
if (token[strlen (token) - 1] == '\n')
token[strlen (token) - 1] = 0;
@ -487,16 +461,14 @@ char **rc_filter_env (void)
if (rc_is_file (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, ' ');
if (space)
*space = 0;
env_var = getenv (env_name);
if (! env_var && profile)
{
if (! env_var && profile) {
env_len = strlen (env_name) + strlen ("export ") + 1;
p = rc_xmalloc (sizeof (char *) * env_len);
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 */
sep = env_var;
while ((token = strsep (&sep, ":")))
{
while ((token = strsep (&sep, ":"))) {
char *np = strdup (PATH_PREFIX);
char *npp = np;
char *tok = NULL;
@ -532,9 +503,7 @@ char **rc_filter_env (void)
free (np);
}
*p++ = 0;
}
else
{
} else {
env_len = strlen (env_name) + strlen (env_var) + 2;
e = rc_xmalloc (sizeof (char *) * env_len);
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.
However, we do need a path, so use a default. */
if (! got_path)
{
if (! got_path) {
env_len = strlen ("PATH=") + strlen (PATH_PREFIX) + 2;
p = rc_xmalloc (sizeof (char *) * env_len);
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))
return (false);
if (! (fp = fopen (file, "r")))
{
if (! (fp = fopen (file, "r"))) {
ewarn ("file_regex `%s': %s", file, strerror (errno));
return (false);
}
if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0)
{
if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
fclose (fp);
regerror (result, &re, buffer, sizeof (buffer));
eerror ("file_regex: %s", buffer);
return (false);
}
while (fgets (buffer, RC_LINEBUFFER, fp))
{
while (fgets (buffer, RC_LINEBUFFER, fp)) {
if (regexec (&re, buffer, 0, NULL, 0) == 0)
{
retval = true;
@ -616,21 +581,17 @@ char **rc_config_env (char **env)
FILE *fp;
char buffer[PATH_MAX];
STRLIST_FOREACH (config, line, i)
{
STRLIST_FOREACH (config, line, i) {
p = strchr (line, '=');
if (! p)
continue;
*p = 0;
e = getenv (line);
if (! e)
{
if (! e) {
*p = '=';
env = rc_strlist_add (env, line);
}
else
{
} else {
int len = strlen (line) + strlen (e) + 2;
char *new = rc_xmalloc (sizeof (char *) * len);
snprintf (new, len, "%s=%s", line, e);
@ -663,16 +624,13 @@ char **rc_config_env (char **env)
env = rc_strlist_add (env, line);
free (line);
if (rc_exists (RC_SVCDIR "ksoftlevel"))
{
if (rc_exists (RC_SVCDIR "ksoftlevel")) {
if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "r")))
eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel",
strerror (errno));
else
{
else {
memset (buffer, 0, sizeof (buffer));
if (fgets (buffer, sizeof (buffer), fp))
{
if (fgets (buffer, sizeof (buffer), fp)) {
i = strlen (buffer) - 1;
if (buffer[i] == '\n')
buffer[i] = 0;
@ -684,8 +642,7 @@ char **rc_config_env (char **env)
}
fclose (fp);
}
}
else
} else
env = rc_strlist_add (env, "RC_DEFAULTLEVEL=" RC_LEVEL_DEFAULT);
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
We store this special system in RC_SYS so our scripts run fast */
#ifdef __linux__
if (rc_is_dir ("/proc/xen"))
{
if (rc_is_dir ("/proc/xen")) {
fp = fopen ("/proc/xen/capabilities", "r");
if (fp)
{
if (fp) {
fclose (fp);
if (file_regex ("/proc/xen/capabilities", "control_d"))
snprintf (sys, sizeof (sys), "XENU");
}
if (! sys)
snprintf (sys, sizeof (sys), "XEN0");
}
else if (file_regex ("/proc/cpuinfo", "UML"))
} else if (file_regex ("/proc/cpuinfo", "UML"))
snprintf (sys, sizeof (sys), "UML");
else if (file_regex ("/proc/self/status",
"(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 */
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;
break;
}
if (! has_net_fs_list)
{
if (! has_net_fs_list) {
i = strlen ("RC_NET_FS_LIST=") + strlen (RC_NET_FS_LIST_DEFAULT) + 1;
line = rc_xmalloc (sizeof (char *) * i);
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);
}
if (sys[0])
{
if (sys[0]) {
i = strlen ("RC_SYS=") + strlen (sys) + 2;
line = rc_xmalloc (sizeof (char *) * i);
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
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;
line = rc_xmalloc (sizeof (char *) * i);
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)
return (list);
while (list && list[i])
{
while (list && list[i]) {
if (uniq && strcmp (list[i], item) == 0)
return (list);
i++;
@ -64,8 +63,7 @@ static char **_rc_strlist_addsort (char **list, const char *item,
tmp1 = newlist[i];
newlist[i] = rc_xstrdup (item);
do
{
do {
i++;
tmp2 = newlist[i];
newlist[i] = tmp1;
@ -98,11 +96,9 @@ char **rc_strlist_delete (char **list, const char *item)
return (list);
while (list[i])
if (! strcmp (list[i], item))
{
if (! strcmp (list[i], item)) {
free (list[i]);
do
{
do {
list[i] = list[i + 1];
i++;
} while (list[i]);
@ -124,8 +120,7 @@ void rc_strlist_reverse (char **list)
j++;
j--;
while (i < j && list[i] && list[j])
{
while (i < j && list[i] && list[j]) {
item = list[i];
list[i] = list[j];
list[j] = item;
@ -141,8 +136,7 @@ void rc_strlist_free (char **list)
if (! list)
return;
while (list[i])
{
while (list[i]) {
free (list[i]);
list[i++] = NULL;
}

View File

@ -68,8 +68,7 @@ char **rc_get_runlevels (void)
int i;
char *dir;
STRLIST_FOREACH (dirs, dir, i)
{
STRLIST_FOREACH (dirs, dir, i) {
char *path = rc_strcatpaths (RC_RUNLEVELDIR, dir, (char *) NULL);
if (rc_is_dir (path))
runlevels = rc_strlist_addsort (runlevels, dir);
@ -85,14 +84,12 @@ char *rc_get_runlevel (void)
FILE *fp;
static char buffer [PATH_MAX];
if (! (fp = fopen (SOFTLEVEL, "r")))
{
if (! (fp = fopen (SOFTLEVEL, "r"))) {
snprintf (buffer, sizeof (buffer), "sysinit");
return (buffer);
}
if (fgets (buffer, PATH_MAX, fp))
{
if (fgets (buffer, PATH_MAX, fp)) {
int i = strlen (buffer) - 1;
if (buffer[i] == '\n')
buffer[i] = 0;
@ -142,20 +139,17 @@ char *rc_resolve_service (const char *service)
return (strdup (service));
file = rc_strcatpaths (RC_SVCDIR, "started", service, (char *) NULL);
if (! rc_is_link (file))
{
if (! rc_is_link (file)) {
free (file);
file = rc_strcatpaths (RC_SVCDIR, "inactive", service, (char *) NULL);
if (! rc_is_link (file))
{
if (! rc_is_link (file)) {
free (file);
file = NULL;
}
}
memset (buffer, 0, sizeof (buffer));
if (file)
{
if (file) {
r = readlink (file, buffer, sizeof (buffer));
free (file);
if (r > 0)
@ -223,10 +217,8 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
base = basename (service);
if (state != rc_service_stopped)
{
if (! rc_is_file(init))
{
if (state != rc_service_stopped) {
if (! rc_is_file(init)) {
free (init);
return (false);
}
@ -236,8 +228,7 @@ bool rc_mark_service (const char *service, const rc_service_state_t state)
if (rc_exists (file))
unlink (file);
i = symlink (init, file);
if (i != 0)
{
if (i != 0) {
free (file);
free (init);
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;
}
if (state == rc_service_coldplugged)
{
if (state == rc_service_coldplugged) {
free (init);
return (true);
}
/* Remove any old states now */
i = 0;
while (rc_service_state_names[i])
{
while (rc_service_state_names[i]) {
if ((i != skip_state &&
i != rc_service_stopped &&
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,
(char *) NULL);
if (rc_exists (file))
{
if (rc_exists (file)) {
if ((state == rc_service_starting ||
state == rc_service_stopping) &&
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 */
if (state == rc_service_stopped)
{
if (state == rc_service_stopped) {
char *dir = rc_strcatpaths (RC_SVCDIR, "options", base, (char *) NULL);
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 */
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 **dirs = rc_ls_dir (NULL, sdir, 0);
char *dir;
int serrno;
STRLIST_FOREACH (dirs, dir, i)
{
STRLIST_FOREACH (dirs, dir, i) {
char *bdir = rc_strcatpaths (sdir, dir, (char *) NULL);
file = rc_strcatpaths (bdir, base, (char *) NULL);
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 */
if (state == rc_service_crashed)
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);
retval = (services);
if (services)
@ -402,15 +386,12 @@ bool rc_get_service_option (const char *service, const char *option,
(char *) NULL);
bool retval = false;
if (rc_exists (file))
{
if (rc_exists (file)) {
if ((fp = fopen (file, "r")) == NULL)
eerror ("fopen `%s': %s", file, strerror (errno));
else
{
else {
memset (buffer, 0, sizeof (buffer));
while (fgets (buffer, RC_LINEBUFFER, fp))
{
while (fgets (buffer, RC_LINEBUFFER, fp)) {
memcpy (value, buffer, 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);
bool retval = false;
if (! rc_is_dir (path))
{
if (mkdir (path, 0755) != 0)
{
if (! rc_is_dir (path)) {
if (mkdir (path, 0755) != 0) {
eerror ("mkdir `%s': %s", path, strerror (errno));
free (path);
free (file);
@ -444,8 +423,7 @@ bool rc_set_service_option (const char *service, const char *option,
if ((fp = fopen (file, "w")) == NULL)
eerror ("fopen `%s': %s", file, strerror (errno));
else
{
else {
if (value)
fprintf (fp, "%s", value);
fclose (fp);
@ -466,8 +444,7 @@ static pid_t _exec_service (const char *service, const char *arg)
int status;
file = rc_resolve_service (service);
if (! rc_is_file (file))
{
if (! rc_is_file (file)) {
rc_mark_service (service, rc_service_stopped);
free (file);
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),
(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));
free (fifo);
free (file);
return (-1);
}
if ((pid = fork ()) == 0)
{
if ((pid = fork ()) == 0) {
char *myarg = strdup (arg);
int e = 0;
execl (file, file, myarg, (char *) NULL);
@ -500,8 +475,7 @@ static pid_t _exec_service (const char *service, const char *arg)
free (fifo);
free (file);
if (pid == -1)
{
if (pid == -1) {
eerror ("unable to fork: %s", strerror (errno));
return (pid);
}
@ -511,11 +485,9 @@ static pid_t _exec_service (const char *service, const char *arg)
savedpid = pid;
errno = 0;
do
{
do {
pid = waitpid (savedpid, &status, 0);
if (pid < 0)
{
if (pid < 0) {
if (errno != ECHILD)
eerror ("waitpid %d: %s", savedpid, strerror (errno));
return (-1);
@ -556,8 +528,7 @@ void rc_schedule_start_service (const char *service,
dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service),
(char *) NULL);
if (! rc_is_dir (dir))
if (mkdir (dir, 0755) != 0)
{
if (mkdir (dir, 0755) != 0) {
eerror ("mkdir `%s': %s", dir, strerror (errno));
free (dir);
return;
@ -592,36 +563,32 @@ bool rc_wait_service (const char *service)
struct timeval now;
bool retval = false;
if (gettimeofday (&stopat, NULL) != 0)
{
if (gettimeofday (&stopat, NULL) != 0) {
eerror ("gettimeofday: %s", strerror (errno));
return (false);
}
stopat.tv_sec += WAIT_MAX;
while (true)
{
if (! rc_exists (fifo))
{
while (true) {
if (! rc_exists (fifo)) {
retval = true;
break;
}
tv.tv_sec = 0;
tv.tv_usec = WAIT_INTERVAL;
if (select (0, 0, 0, 0, &tv) < 0)
{
if (select (0, 0, 0, 0, &tv) < 0) {
if (errno != EINTR)
eerror ("select: %s",strerror (errno));
break;
}
/* Don't hang around forever */
if (gettimeofday (&now, NULL) != 0)
{
if (gettimeofday (&now, NULL) != 0) {
eerror ("gettimeofday: %s", strerror (errno));
break;
}
if (timercmp (&now, &stopat, >))
break;
}
@ -659,14 +626,12 @@ char **rc_services_in_state (rc_service_state_t state)
(char *) NULL);
char **list = NULL;
if (state == rc_service_scheduled)
{
if (state == rc_service_scheduled) {
char **dirs = rc_ls_dir (NULL, dir, 0);
char *d;
int i;
STRLIST_FOREACH (dirs, d, i)
{
STRLIST_FOREACH (dirs, d, i) {
char *p = rc_strcatpaths (dir, d, (char *) NULL);
char **entries = rc_ls_dir (NULL, p, RC_LS_INITD);
char *e;
@ -681,9 +646,7 @@ char **rc_services_in_state (rc_service_state_t state)
if (dirs)
free (dirs);
}
else
{
} else {
if (rc_is_dir (dir))
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 *file;
if (! rc_runlevel_exists (runlevel))
{
if (! rc_runlevel_exists (runlevel)) {
errno = ENOENT;
return (false);
}
if (rc_service_in_runlevel (service, runlevel))
{
if (rc_service_in_runlevel (service, runlevel)) {
errno = EEXIST;
return (false);
}
@ -743,8 +704,7 @@ char **rc_services_scheduled_by (const char *service)
char *dir;
int i;
STRLIST_FOREACH (dirs, dir, i)
{
STRLIST_FOREACH (dirs, dir, i) {
char *file = rc_strcatpaths (RC_SVCDIR "scheduled", dir, service,
(char *) NULL);
if (rc_exists (file))
@ -782,28 +742,21 @@ bool rc_allow_plug (char *service)
list = strdup (match);
p = list;
while ((token = strsep (&p, " ")))
{
while ((token = strsep (&p, " "))) {
bool truefalse = true;
if (token[0] == '!')
{
if (token[0] == '!') {
truefalse = false;
token++;
}
star = strchr (token, '*');
if (star)
{
if (strncmp (service, token, star - token) == 0)
{
if (star) {
if (strncmp (service, token, star - token) == 0) {
allow = truefalse;
break;
}
}
else
{
if (strcmp (service, token) == 0)
{
} else {
if (strcmp (service, token) == 0) {
allow = truefalse;
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)
eerrorx ("getmntinfo: %s", strerror (errno));
for (i = 0; i < nmnts; i++)
{
for (i = 0; i < nmnts; i++) {
if (node_regex &&
regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
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)
continue;
if (mounts)
{
if (mounts) {
bool found = false;
int j;
char *mnt;
STRLIST_FOREACH (mounts, mnt, j)
if (strcmp (mnt, mnts[i].f_mntonname) == 0)
{
if (strcmp (mnt, mnts[i].f_mntonname) == 0) {
found = true;
break;
}
@ -86,8 +83,7 @@ static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
if ((fp = fopen ("/proc/mounts", "r")) == NULL)
eerrorx ("getmntinfo: %s", strerror (errno));
while (fgets (buffer, sizeof (buffer), fp))
{
while (fgets (buffer, sizeof (buffer), fp)) {
p = buffer;
from = strsep (&p, " ");
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)
continue;
if (mounts)
{
if (mounts) {
bool found = false;
int j;
char *mnt;
STRLIST_FOREACH (mounts, mnt, j)
if (strcmp (mnt, to) == 0)
{
if (strcmp (mnt, to) == 0) {
found = true;
break;
}
@ -147,10 +141,8 @@ int main (int argc, char **argv)
bool reverse = false;
char **mounts = NULL;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc))
{
for (i = 1; i < argc; i++) {
if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc)) {
i++;
if (fstype_regex)
free (fstype_regex);
@ -164,8 +156,7 @@ int main (int argc, char **argv)
continue;
}
if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc))
{
if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) {
i++;
if (node_regex)
free (node_regex);
@ -179,8 +170,7 @@ int main (int argc, char **argv)
continue;
}
if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc))
{
if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) {
i++;
if (skip_regex)
free (skip_regex);
@ -194,19 +184,16 @@ int main (int argc, char **argv)
continue;
}
if (strcmp (argv[i], "--fstype") == 0)
{
if (strcmp (argv[i], "--fstype") == 0) {
list_fstype = true;
continue;
}
if (strcmp (argv[i], "--node") == 0)
{
if (strcmp (argv[i], "--node") == 0) {
list_nodes = true;
continue;
}
if (strcmp (argv[i], "--reverse") == 0)
{
if (strcmp (argv[i], "--reverse") == 0) {
reverse = true;
continue;
}
@ -229,8 +216,7 @@ int main (int argc, char **argv)
rc_strlist_reverse (nodes);
result = EXIT_FAILURE;
STRLIST_FOREACH (nodes, node, i)
{
STRLIST_FOREACH (nodes, node, i) {
if (skip_regex && regexec (skip_regex, node, 0, NULL, 0) == 0)
continue;
printf ("%s\n", node);

View File

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

View File

@ -41,16 +41,14 @@ void rc_plugin_load (void)
return;
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);
void *h = dlopen (p, RTLD_LAZY);
char *func;
void *f;
int len;
if (! h)
{
if (! h) {
eerror ("dlopen `%s': %s", p, dlerror ());
free (p);
continue;
@ -63,19 +61,14 @@ void rc_plugin_load (void)
snprintf (func, len, "_%s_hook", file);
f = dlsym (h, func);
if (! f)
{
if (! f) {
eerror ("`%s' does not expose the symbol `%s'", p, func);
dlclose (h);
}
else
{
if (plugin)
{
} else {
if (plugin) {
plugin->next = rc_xmalloc (sizeof (plugin_t));
plugin = plugin->next;
}
else
} else
plugin = plugins = rc_xmalloc (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;
while (plugin)
{
while (plugin) {
if (plugin->hook)
plugin->hook (hook, value);
@ -109,8 +101,7 @@ void rc_plugin_unload (void)
plugin_t *plugin = plugins;
plugin_t *next;
while (plugin)
{
while (plugin) {
next = plugin->next;
dlclose (plugin->handle);
free (plugin->name);

View File

@ -32,29 +32,21 @@ static void print_service (char *service)
if (rc_service_state (service, rc_service_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 ");
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 ");
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 ");
else if (rc_service_state (service, rc_service_started))
{
else if (rc_service_state (service, rc_service_started)) {
snprintf (status, sizeof (status), " started ");
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");
color = einfo_warn;
}
else
} else
snprintf (status, sizeof (status), " stopped ");
ebracket (cols, color, status);
}
@ -70,8 +62,7 @@ int main (int argc, char **argv)
int i;
int j;
const struct option longopts[] =
{
const struct option longopts[] = {
{"all", no_argument, NULL, 'a'},
{"list", no_argument, NULL, 'l'},
{"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)
switch (c)
{
switch (c) {
case 'a':
levels = rc_get_runlevels ();
break;
@ -100,12 +90,10 @@ int main (int argc, char **argv)
case 'u':
services = rc_services_in_runlevel (NULL);
levels = rc_get_runlevels ();
STRLIST_FOREACH (services, service, i)
{
STRLIST_FOREACH (services, service, i) {
bool found = false;
STRLIST_FOREACH (levels, level, j)
if (rc_service_in_runlevel (service, level))
{
if (rc_service_in_runlevel (service, level)) {
found = true;
break;
}
@ -127,8 +115,7 @@ int main (int argc, char **argv)
if (! levels)
levels = rc_strlist_add (NULL, rc_get_runlevel ());
STRLIST_FOREACH (levels, level, i)
{
STRLIST_FOREACH (levels, level, i) {
print_level (level);
services = rc_services_in_runlevel (level);
STRLIST_FOREACH (services, service, j)

View File

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

214
src/rc.c
View File

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

View File

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

View File

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