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

@ -18,12 +18,12 @@
typedef enum
{
einfo_good,
einfo_warn,
einfo_bad,
einfo_hilite,
einfo_bracket,
einfo_normal
einfo_good,
einfo_warn,
einfo_bad,
einfo_hilite,
einfo_bracket,
einfo_normal
} einfo_color_t;
/* Colour codes used by the below functions. */
@ -50,7 +50,7 @@ typedef enum
NOTE We use the v suffix here so we can add veinfo for va_list
in the future, but veinfo is used by shell scripts as they don't
have the va_list concept
*/
*/
bool colour_terminal (void);
int einfon (const char *fmt, ...) EINFO_PRINTF (1, 2);
int ewarnn (const char *fmt, ...) EINFO_PRINTF (1, 2);

View File

@ -5,7 +5,8 @@
Copyright 2007 Gentoo Foundation
Released under the GPLv2
*/
*/
#include <errno.h>
#include <limits.h>
@ -26,221 +27,206 @@
#define LDSOCONF "/etc/ld.so.conf"
#define NOTICE "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n" \
"# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n" \
"# GO INTO %s NOT %s\n\n"
"# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n" \
"# GO INTO %s NOT %s\n\n"
#define LDNOTICE "# ld.so.conf autogenerated by env-update; make all\n" \
"# changes to contents of /etc/env.d directory\n"
"# changes to contents of /etc/env.d directory\n"
static const char *specials[] =
{
"ADA_INCLUDE_PATH",
"ADA_OBJECTS_PATH",
"CLASSPATH",
"INFOPATH",
"KDEDIRS",
"LDPATH",
"MANPATH",
"PATH",
"PKG_CONFIG_PATH",
"PRELINK_PATH",
"PRELINK_PATH_MASK",
"PYTHONPATH",
"ROOTPATH",
NULL
static const char *specials[] = {
"ADA_INCLUDE_PATH",
"ADA_OBJECTS_PATH",
"CLASSPATH",
"INFOPATH",
"KDEDIRS",
"LDPATH",
"MANPATH",
"PATH",
"PKG_CONFIG_PATH",
"PRELINK_PATH",
"PRELINK_PATH_MASK",
"PYTHONPATH",
"ROOTPATH",
NULL
};
static const char *special_spaces[] =
{
"CONFIG_PROTECT",
"CONFIG_PROTECT_MASK",
NULL,
static const char *special_spaces[] = {
"CONFIG_PROTECT",
"CONFIG_PROTECT_MASK",
NULL,
};
static char *applet = NULL;
int main (int argc, char **argv)
{
char **files = rc_ls_dir (NULL, ENVDIR, 0);
char *file;
char **envs = NULL;
char *env;
int i = 0;
FILE *fp;
bool ld = true;
char *ldent;
char **ldents = NULL;
int nents = 0;
char **files = rc_ls_dir (NULL, ENVDIR, 0);
char *file;
char **envs = NULL;
char *env;
int i = 0;
FILE *fp;
bool ld = true;
char *ldent;
char **ldents = NULL;
int nents = 0;
applet = argv[0];
applet = argv[0];
if (! files)
eerrorx ("%s: no files in " ENVDIR " to process", applet);
if (! files)
eerrorx ("%s: no files in " ENVDIR " to process", applet);
STRLIST_FOREACH (files, file, i)
{
char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL);
char **entries = NULL;
char *entry;
int j;
STRLIST_FOREACH (files, file, i) {
char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL);
char **entries = NULL;
char *entry;
int j;
if (! rc_is_dir (path))
entries = rc_get_config (NULL, path);
free (path);
if (! rc_is_dir (path))
entries = rc_get_config (NULL, path);
free (path);
STRLIST_FOREACH (entries, entry, j)
{
char *tmpent = rc_xstrdup (entry);
char *value = tmpent;
char *var = strsep (&value, "=");
int k;
bool isspecial = false;
bool isspecial_spaced = false;
bool replaced = false;
STRLIST_FOREACH (entries, entry, j) {
char *tmpent = rc_xstrdup (entry);
char *value = tmpent;
char *var = strsep (&value, "=");
int k;
bool isspecial = false;
bool isspecial_spaced = false;
bool replaced = false;
for (k = 0; special_spaces[k]; k++)
if (strcmp (special_spaces[k], var) == 0)
{
isspecial = true;
isspecial_spaced = true;
break;
}
for (k = 0; special_spaces[k]; k++)
if (strcmp (special_spaces[k], var) == 0) {
isspecial = true;
isspecial_spaced = true;
break;
}
if (! isspecial)
{
for (k = 0; specials[k]; k++)
if (strcmp (specials[k], var) == 0)
{
isspecial = true;
break;
}
}
if (! isspecial) {
for (k = 0; specials[k]; k++)
if (strcmp (specials[k], var) == 0) {
isspecial = true;
break;
}
}
/* Skip blank vars */
if (isspecial &&
(! value || strlen (value)) == 0)
{
free (tmpent);
continue;
}
/* Skip blank vars */
if (isspecial &&
(! value || strlen (value)) == 0)
{
free (tmpent);
continue;
}
STRLIST_FOREACH (envs, env, k)
{
char *tmpenv = rc_xstrdup (env);
char *tmpvalue = tmpenv;
char *tmpentry = strsep (&tmpvalue, "=");
STRLIST_FOREACH (envs, env, k) {
char *tmpenv = rc_xstrdup (env);
char *tmpvalue = tmpenv;
char *tmpentry = strsep (&tmpvalue, "=");
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
{
free (envs[k - 1]);
envs[k - 1] = strdup (entry);
}
replaced = true;
}
free (tmpenv);
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 {
free (envs[k - 1]);
envs[k - 1] = strdup (entry);
}
replaced = true;
}
free (tmpenv);
if (replaced)
break;
}
if (replaced)
break;
}
if (! replaced)
envs = rc_strlist_addsort (envs, entry);
if (! replaced)
envs = rc_strlist_addsort (envs, entry);
free (tmpent);
}
}
free (tmpent);
}
}
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)
{
char *tmpent = rc_xstrdup (env);
char *value = tmpent;
char *var = strsep (&value, "=");
if (strcmp (var, "LDPATH") != 0)
fprintf (fp, "export %s='%s'\n", var, value);
free (tmpent);
}
fclose (fp);
if ((fp = fopen (PROFILE_ENV, "w")) == NULL)
eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
fprintf (fp, NOTICE, "/etc/profile", PROFILE_ENV);
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)
{
char *tmpent = rc_xstrdup (env);
char *value = tmpent;
char *var = strsep (&value, "=");
if (strcmp (var, "LDPATH") != 0)
fprintf (fp, "setenv %s '%s'\n", var, value);
free (tmpent);
}
fclose (fp);
STRLIST_FOREACH (envs, env, i) {
char *tmpent = rc_xstrdup (env);
char *value = tmpent;
char *var = strsep (&value, "=");
if (strcmp (var, "LDPATH") != 0)
fprintf (fp, "export %s='%s'\n", var, value);
free (tmpent);
}
fclose (fp);
ldent = rc_get_config_entry (envs, "LDPATH");
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);
if (! ldent ||
(argc > 1 && argv[1] && strcmp (argv[1], "--no-ldconfig") == 0))
{
free (envs);
return (EXIT_SUCCESS);
}
STRLIST_FOREACH (envs, env, i) {
char *tmpent = rc_xstrdup (env);
char *value = tmpent;
char *var = strsep (&value, "=");
if (strcmp (var, "LDPATH") != 0)
fprintf (fp, "setenv %s '%s'\n", var, value);
free (tmpent);
}
fclose (fp);
while ((file = strsep (&ldent, ":")))
{
if (strlen (file) == 0)
continue;
ldent = rc_get_config_entry (envs, "LDPATH");
ldents = rc_strlist_add (ldents, file);
nents++;
}
if (! ldent ||
(argc > 1 && argv[1] && strcmp (argv[1], "--no-ldconfig") == 0))
{
free (envs);
return (EXIT_SUCCESS);
}
/* Update ld.so.conf only if different */
if (rc_exists (LDSOCONF))
{
char **lines = rc_get_list (NULL, LDSOCONF);
char *line;
ld = false;
STRLIST_FOREACH (lines, line, i)
if (i > nents || strcmp (line, ldents[i - 1]) != 0)
{
ld = true;
break;
}
if (i - 1 != nents)
ld = true;
}
while ((file = strsep (&ldent, ":"))) {
if (strlen (file) == 0)
continue;
if (ld)
{
int retval = 0;
ldents = rc_strlist_add (ldents, file);
nents++;
}
if ((fp = fopen (LDSOCONF, "w")) == NULL)
eerrorx ("%s: fopen `%s': %s", applet, LDSOCONF, strerror (errno));
fprintf (fp, LDNOTICE);
STRLIST_FOREACH (ldents, ldent, i)
fprintf (fp, "%s\n", ldent);
fclose (fp);
/* Update ld.so.conf only if different */
if (rc_exists (LDSOCONF)) {
char **lines = rc_get_list (NULL, LDSOCONF);
char *line;
ld = false;
STRLIST_FOREACH (lines, line, i)
if (i > nents || strcmp (line, ldents[i - 1]) != 0)
{
ld = true;
break;
}
if (i - 1 != nents)
ld = true;
}
if (ld) {
int retval = 0;
if ((fp = fopen (LDSOCONF, "w")) == NULL)
eerrorx ("%s: fopen `%s': %s", applet, LDSOCONF, strerror (errno));
fprintf (fp, LDNOTICE);
STRLIST_FOREACH (ldents, ldent, i)
fprintf (fp, "%s\n", ldent);
fclose (fp);
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
ebegin ("Regenerating /var/run/ld-elf.so.hints");
retval = system ("/sbin/ldconfig -elf -i '" LDSOCONF "'");
ebegin ("Regenerating /var/run/ld-elf.so.hints");
retval = system ("/sbin/ldconfig -elf -i '" LDSOCONF "'");
#else
ebegin ("Regenerating /etc/ld.so.cache");
retval = system ("/sbin/ldconfig");
ebegin ("Regenerating /etc/ld.so.cache");
retval = system ("/sbin/ldconfig");
#endif
eend (retval, NULL);
}
eend (retval, NULL);
}
return(EXIT_SUCCESS);
return(EXIT_SUCCESS);
}

View File

@ -43,104 +43,96 @@
#ifdef HAVE_GETMNTENT
static struct mntent *getmntfile (FILE *fp, const char *file)
{
struct mntent *ent;
struct mntent *ent;
while ((ent = getmntent (fp)))
if (strcmp (file, ent->mnt_dir) == 0)
return (ent);
while ((ent = getmntent (fp)))
if (strcmp (file, ent->mnt_dir) == 0)
return (ent);
return (NULL);
return (NULL);
}
#endif
int main (int argc, char **argv)
{
int i;
int i;
#ifdef HAVE_GETMNTENT
FILE *fp;
struct mntent *ent;
FILE *fp;
struct mntent *ent;
#else
struct fstab *ent;
struct fstab *ent;
#endif
int result = EXIT_FAILURE;
char *p;
char *token;
int n = 0;
int result = EXIT_FAILURE;
char *p;
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");
fp = setmntent ("/etc/fstab", "r");
#endif
if (strcmp (argv[i], "--fstype") == 0 && i + 1 < argc)
{
i++;
p = argv[i];
while ((token = strsep (&p, ",")))
while ((ent = GET_ENT))
if (strcmp (token, ENT_TYPE (ent)) == 0)
printf ("%s\n", ENT_FILE (ent));
result = EXIT_SUCCESS;
}
if (strcmp (argv[i], "--fstype") == 0 && i + 1 < argc) {
i++;
p = argv[i];
while ((token = strsep (&p, ",")))
while ((ent = GET_ENT))
if (strcmp (token, ENT_TYPE (ent)) == 0)
printf ("%s\n", ENT_FILE (ent));
result = EXIT_SUCCESS;
}
if (strcmp (argv[i], "--mount-cmd") == 0 && i + 1 < argc)
{
i++;
if ((ent = GET_ENT_FILE (argv[i])) == NULL)
continue;
printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
ENT_DEVICE (ent), ENT_FILE (ent));
result = EXIT_SUCCESS;
}
if (strcmp (argv[i], "--mount-cmd") == 0 && i + 1 < argc) {
i++;
if ((ent = GET_ENT_FILE (argv[i])) == NULL)
continue;
printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
ENT_DEVICE (ent), ENT_FILE (ent));
result = EXIT_SUCCESS;
}
if (strcmp (argv[i], "--opts") == 0 && i + 1 < argc)
{
i++;
if ((ent = GET_ENT_FILE (argv[i])) == NULL)
continue;
printf ("%s\n", ENT_OPTS (ent));
result = EXIT_SUCCESS;
}
if (strcmp (argv[i], "--opts") == 0 && i + 1 < argc) {
i++;
if ((ent = GET_ENT_FILE (argv[i])) == NULL)
continue;
printf ("%s\n", ENT_OPTS (ent));
result = EXIT_SUCCESS;
}
if (strcmp (argv[i], "--passno") == 0 && i + 1 < argc)
{
i++;
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);
if (strcmp (argv[i], "--passno") == 0 && i + 1 < argc) {
i++;
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))
{
if (((argv[i][0] == '=' && n == ENT_PASS (ent)) ||
(argv[i][0] == '<' && n > ENT_PASS (ent)) ||
(argv[i][0] == '>' && n < ENT_PASS (ent))) &&
strcmp (ENT_FILE (ent), "none") != 0)
printf ("%s\n", ENT_FILE (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))) &&
strcmp (ENT_FILE (ent), "none") != 0)
printf ("%s\n", ENT_FILE (ent));
}
default:
if ((ent = GET_ENT_FILE (argv[i])) == NULL)
continue;
printf ("%d\n", ENT_PASS (ent));
result = EXIT_SUCCESS;
}
}
default:
if ((ent = GET_ENT_FILE (argv[i])) == NULL)
continue;
printf ("%d\n", ENT_PASS (ent));
result = EXIT_SUCCESS;
}
}
END_ENT;
END_ENT;
if (result != EXIT_SUCCESS)
{
eerror ("%s: unknown option `%s'", basename (argv[0]), argv[i]);
break;
}
if (result != EXIT_SUCCESS) {
eerror ("%s: unknown option `%s'", basename (argv[0]), argv[i]);
break;
}
}
}
exit (result);
exit (result);
}

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
#include <sys/stat.h>
#if defined(__DragonFly__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined (__OpenBSD__)
defined(__NetBSD__) || defined (__OpenBSD__)
#include <sys/param.h>
#include <sys/user.h>
#include <sys/sysctl.h>
@ -37,150 +37,143 @@
#if defined(__linux__)
static bool pid_is_cmd (pid_t pid, const char *cmd)
{
char buffer[32];
FILE *fp;
int c;
char buffer[32];
FILE *fp;
int c;
snprintf(buffer, sizeof (buffer), "/proc/%d/stat", pid);
if ((fp = fopen (buffer, "r")) == NULL)
return (false);
snprintf(buffer, sizeof (buffer), "/proc/%d/stat", pid);
if ((fp = fopen (buffer, "r")) == NULL)
return (false);
while ((c = getc (fp)) != EOF && c != '(')
;
while ((c = getc (fp)) != EOF && c != '(')
;
if (c != '(')
{
fclose(fp);
return (false);
}
if (c != '(') {
fclose(fp);
return (false);
}
while ((c = getc (fp)) != EOF && c == *cmd)
cmd++;
while ((c = getc (fp)) != EOF && c == *cmd)
cmd++;
fclose (fp);
fclose (fp);
return ((c == ')' && *cmd == '\0') ? true : false);
return ((c == ')' && *cmd == '\0') ? true : false);
}
static bool pid_is_exec (pid_t pid, const char *exec)
{
char cmdline[32];
char buffer[PATH_MAX];
char *p;
int fd = -1;
int r;
char cmdline[32];
char buffer[PATH_MAX];
char *p;
int fd = -1;
int r;
snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid);
memset (buffer, 0, sizeof (buffer));
if (readlink (cmdline, buffer, sizeof (buffer)) != -1)
{
if (strcmp (exec, buffer) == 0)
return (true);
snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid);
memset (buffer, 0, sizeof (buffer));
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)
{
p = buffer + (strlen (buffer) - 10);
if (strcmp (p, " (deleted)") == 0)
{
*p = 0;
if (strcmp (buffer, exec) == 0)
return (true);
}
}
}
/* We should cater for deleted binaries too */
if (strlen (buffer) > 10) {
p = buffer + (strlen (buffer) - 10);
if (strcmp (p, " (deleted)") == 0) {
*p = 0;
if (strcmp (buffer, exec) == 0)
return (true);
}
}
}
snprintf (cmdline, sizeof (cmdline), "/proc/%u/cmdline", pid);
if ((fd = open (cmdline, O_RDONLY)) < 0)
return (false);
snprintf (cmdline, sizeof (cmdline), "/proc/%u/cmdline", pid);
if ((fd = open (cmdline, O_RDONLY)) < 0)
return (false);
r = read(fd, buffer, sizeof (buffer));
close (fd);
r = read(fd, buffer, sizeof (buffer));
close (fd);
if (r == -1)
return 0;
if (r == -1)
return 0;
buffer[r] = 0;
return (strcmp (exec, buffer) == 0 ? true : false);
buffer[r] = 0;
return (strcmp (exec, buffer) == 0 ? true : false);
}
pid_t *rc_find_pids (const char *exec, const char *cmd,
uid_t uid, pid_t pid)
uid_t uid, pid_t pid)
{
DIR *procdir;
struct dirent *entry;
int npids = 0;
int foundany = false;
pid_t p;
pid_t *pids = NULL;
char buffer[PATH_MAX];
struct stat sb;
pid_t runscript_pid = 0;
char *pp;
DIR *procdir;
struct dirent *entry;
int npids = 0;
int foundany = false;
pid_t p;
pid_t *pids = NULL;
char buffer[PATH_MAX];
struct stat sb;
pid_t runscript_pid = 0;
char *pp;
if ((procdir = opendir ("/proc")) == NULL)
eerrorx ("opendir `/proc': %s", strerror (errno));
if ((procdir = opendir ("/proc")) == NULL)
eerrorx ("opendir `/proc': %s", strerror (errno));
/*
We never match RC_RUNSCRIPT_PID if present so we avoid the below
scenario
/*
We never match RC_RUNSCRIPT_PID if present so we avoid the below
scenario
/etc/init.d/ntpd stop does
start-stop-daemon --stop --name ntpd
catching /etc/init.d/ntpd stop
/etc/init.d/ntpd stop does
start-stop-daemon --stop --name ntpd
catching /etc/init.d/ntpd stop
nasty
*/
nasty
*/
if ((pp = getenv ("RC_RUNSCRIPT_PID")))
{
if (sscanf (pp, "%d", &runscript_pid) != 1)
runscript_pid = 0;
}
if ((pp = getenv ("RC_RUNSCRIPT_PID"))) {
if (sscanf (pp, "%d", &runscript_pid) != 1)
runscript_pid = 0;
}
while ((entry = readdir (procdir)) != NULL)
{
if (sscanf (entry->d_name, "%d", &p) != 1)
continue;
foundany = true;
while ((entry = readdir (procdir)) != NULL) {
if (sscanf (entry->d_name, "%d", &p) != 1)
continue;
foundany = true;
if (runscript_pid != 0 && runscript_pid == p)
continue;
if (runscript_pid != 0 && runscript_pid == p)
continue;
if (pid != 0 && pid != p)
continue;
if (pid != 0 && pid != p)
continue;
if (uid)
{
snprintf (buffer, sizeof (buffer), "/proc/%d", pid);
if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
continue;
}
if (uid) {
snprintf (buffer, sizeof (buffer), "/proc/%d", pid);
if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
continue;
}
if (cmd && ! pid_is_cmd (p, cmd))
continue;
if (cmd && ! pid_is_cmd (p, cmd))
continue;
if (exec && ! cmd && ! pid_is_exec (p, exec))
continue;
if (exec && ! cmd && ! pid_is_exec (p, exec))
continue;
pids = realloc (pids, sizeof (pid_t) * (npids + 2));
if (! pids)
eerrorx ("memory exhausted");
pids = realloc (pids, sizeof (pid_t) * (npids + 2));
if (! pids)
eerrorx ("memory exhausted");
pids[npids] = p;
pids[npids + 1] = 0;
npids++;
}
closedir (procdir);
pids[npids] = p;
pids[npids + 1] = 0;
npids++;
}
closedir (procdir);
if (! foundany)
eerrorx ("nothing in /proc");
if (! foundany)
eerrorx ("nothing in /proc");
return (pids);
return (pids);
}
#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined(__OpenBSD__)
defined(__NetBSD__) || defined(__OpenBSD__)
# if defined(__DragonFly__) || defined(__FreeBSD__)
# ifndef KERN_PROC_PROC
@ -200,63 +193,60 @@ pid_t *rc_find_pids (const char *exec, const char *cmd,
# endif
pid_t *rc_find_pids (const char *exec, const char *cmd,
uid_t uid, pid_t pid)
uid_t uid, pid_t pid)
{
static kvm_t *kd = NULL;
char errbuf[_POSIX2_LINE_MAX];
struct _KINFO_PROC *kp;
int i;
int processes = 0;
int argc = 0;
char **argv;
pid_t *pids = NULL;
int npids = 0;
static kvm_t *kd = NULL;
char errbuf[_POSIX2_LINE_MAX];
struct _KINFO_PROC *kp;
int i;
int processes = 0;
int argc = 0;
char **argv;
pid_t *pids = NULL;
int npids = 0;
if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL)
eerrorx ("kvm_open: %s", errbuf);
if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL)
eerrorx ("kvm_open: %s", errbuf);
#if defined(__DragonFly__) || defined( __FreeBSD__)
kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes);
kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes);
#else
kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
&processes);
kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
&processes);
#endif
for (i = 0; i < processes; i++)
{
pid_t p = _GET_KINFO_PID (kp[i]);
if (pid != 0 && pid != p)
continue;
for (i = 0; i < processes; i++) {
pid_t p = _GET_KINFO_PID (kp[i]);
if (pid != 0 && pid != p)
continue;
if (uid != 0 && uid != _GET_KINFO_UID (kp[i]))
continue;
if (uid != 0 && uid != _GET_KINFO_UID (kp[i]))
continue;
if (cmd)
{
if (! _GET_KINFO_COMM (kp[i]) ||
strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0)
continue;
}
if (cmd) {
if (! _GET_KINFO_COMM (kp[i]) ||
strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0)
continue;
}
if (exec && ! cmd)
{
if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv)
continue;
if (exec && ! cmd) {
if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv)
continue;
if (strcmp (*argv, exec) != 0)
continue;
}
if (strcmp (*argv, exec) != 0)
continue;
}
pids = realloc (pids, sizeof (pid_t) * (npids + 2));
if (! pids)
eerrorx ("memory exhausted");
pids = realloc (pids, sizeof (pid_t) * (npids + 2));
if (! pids)
eerrorx ("memory exhausted");
pids[npids] = p;
pids[npids + 1] = 0;
npids++;
}
kvm_close(kd);
pids[npids] = p;
pids[npids + 1] = 0;
npids++;
}
kvm_close(kd);
return (pids);
return (pids);
}
#else
@ -264,349 +254,309 @@ pid_t *rc_find_pids (const char *exec, const char *cmd,
#endif
static bool _match_daemon (const char *path, const char *file,
const char *mexec, const char *mname,
const char *mpidfile)
const char *mexec, const char *mname,
const char *mpidfile)
{
char buffer[RC_LINEBUFFER];
char *ffile = rc_strcatpaths (path, file, (char *) NULL);
FILE *fp;
int lc = 0;
int m = 0;
char buffer[RC_LINEBUFFER];
char *ffile = rc_strcatpaths (path, file, (char *) NULL);
FILE *fp;
int lc = 0;
int m = 0;
if (! rc_exists (ffile))
{
free (ffile);
return (false);
}
if (! rc_exists (ffile)) {
free (ffile);
return (false);
}
if ((fp = fopen (ffile, "r")) == NULL)
{
eerror ("fopen `%s': %s", ffile, strerror (errno));
free (ffile);
return (false);
}
if ((fp = fopen (ffile, "r")) == NULL) {
eerror ("fopen `%s': %s", ffile, strerror (errno));
free (ffile);
return (false);
}
if (! mname)
m += 10;
if (! mpidfile)
m += 100;
if (! mname)
m += 10;
if (! mpidfile)
m += 100;
memset (buffer, 0, sizeof (buffer));
while ((fgets (buffer, RC_LINEBUFFER, fp)))
{
int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n')
buffer[lb] = 0;
memset (buffer, 0, sizeof (buffer));
while ((fgets (buffer, RC_LINEBUFFER, fp))) {
int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n')
buffer[lb] = 0;
if (strcmp (buffer, mexec) == 0)
m += 1;
else if (mname && strcmp (buffer, mname) == 0)
m += 10;
else if (mpidfile && strcmp (buffer, mpidfile) == 0)
m += 100;
if (strcmp (buffer, mexec) == 0)
m += 1;
else if (mname && strcmp (buffer, mname) == 0)
m += 10;
else if (mpidfile && strcmp (buffer, mpidfile) == 0)
m += 100;
if (m == 111)
break;
if (m == 111)
break;
lc++;
if (lc > 5)
break;
}
fclose (fp);
free (ffile);
lc++;
if (lc > 5)
break;
}
fclose (fp);
free (ffile);
return (m == 111 ? true : false);
return (m == 111 ? true : false);
}
void rc_set_service_daemon (const char *service, const char *exec,
const char *name, const char *pidfile,
bool started)
const char *name, const char *pidfile,
bool started)
{
char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL);
char **files = NULL;
char *file;
char *ffile = NULL;
int i;
char *mexec;
char *mname;
char *mpidfile;
int nfiles = 0;
char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL);
char **files = NULL;
char *file;
char *ffile = NULL;
int i;
char *mexec;
char *mname;
char *mpidfile;
int nfiles = 0;
if (! exec && ! name && ! pidfile)
return;
if (! exec && ! name && ! pidfile)
return;
if (exec)
{
i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec);
}
else
mexec = strdup ("exec=");
if (exec) {
i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec);
} else
mexec = strdup ("exec=");
if (name)
{
i = strlen (name) + 6;
mname = rc_xmalloc (sizeof (char *) * i);
snprintf (mname, i, "name=%s", name);
}
else
mname = strdup ("name=");
if (name) {
i = strlen (name) + 6;
mname = rc_xmalloc (sizeof (char *) * i);
snprintf (mname, i, "name=%s", name);
} else
mname = strdup ("name=");
if (pidfile)
{
i = strlen (pidfile) + 9;
mpidfile = rc_xmalloc (sizeof (char *) * i);
snprintf (mpidfile, i, "pidfile=%s", pidfile);
}
else
mpidfile = strdup ("pidfile=");
if (pidfile) {
i = strlen (pidfile) + 9;
mpidfile = rc_xmalloc (sizeof (char *) * i);
snprintf (mpidfile, i, "pidfile=%s", pidfile);
} else
mpidfile = strdup ("pidfile=");
/* Regardless, erase any existing daemon info */
if (rc_is_dir (dirpath))
{
char *oldfile = NULL;
files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i)
{
ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
nfiles++;
/* Regardless, erase any existing daemon info */
if (rc_is_dir (dirpath)) {
char *oldfile = NULL;
files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i) {
ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
nfiles++;
if (! oldfile)
{
if (_match_daemon (dirpath, file, mexec, mname, mpidfile))
{
unlink (ffile);
oldfile = ffile;
nfiles--;
}
}
else
{
rename (ffile, oldfile);
free (oldfile);
oldfile = ffile;
}
}
if (ffile)
free (ffile);
free (files);
}
if (! oldfile) {
if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) {
unlink (ffile);
oldfile = ffile;
nfiles--;
}
} else {
rename (ffile, oldfile);
free (oldfile);
oldfile = ffile;
}
}
if (ffile)
free (ffile);
free (files);
}
/* Now store our daemon info */
if (started)
{
char buffer[10];
FILE *fp;
/* Now store our daemon info */
if (started) {
char buffer[10];
FILE *fp;
if (! rc_is_dir (dirpath))
if (mkdir (dirpath, 0755) != 0)
eerror ("mkdir `%s': %s", dirpath, strerror (errno));
if (! rc_is_dir (dirpath))
if (mkdir (dirpath, 0755) != 0)
eerror ("mkdir `%s': %s", dirpath, strerror (errno));
snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
if ((fp = fopen (file, "w")) == NULL)
eerror ("fopen `%s': %s", file, strerror (errno));
else
{
fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
fclose (fp);
}
free (file);
}
snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
if ((fp = fopen (file, "w")) == NULL)
eerror ("fopen `%s': %s", file, strerror (errno));
else {
fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
fclose (fp);
}
free (file);
}
free (mexec);
free (mname);
free (mpidfile);
free (dirpath);
free (mexec);
free (mname);
free (mpidfile);
free (dirpath);
}
bool rc_service_started_daemon (const char *service, const char *exec,
int indx)
int indx)
{
char *dirpath;
char *file;
int i;
char *mexec;
bool retval = false;
char *dirpath;
char *file;
int i;
char *mexec;
bool retval = false;
if (! service || ! exec)
return (false);
if (! service || ! exec)
return (false);
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL);
if (! rc_is_dir (dirpath))
{
free (dirpath);
return (false);
}
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL);
if (! rc_is_dir (dirpath)) {
free (dirpath);
return (false);
}
i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec);
i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec);
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
{
char **files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i)
{
retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
if (retval)
break;
}
free (files);
}
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 {
char **files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i) {
retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
if (retval)
break;
}
free (files);
}
free (mexec);
return (retval);
free (mexec);
return (retval);
}
bool rc_service_daemons_crashed (const char *service)
{
char *dirpath;
char **files;
char *file;
char *path;
int i;
FILE *fp;
char buffer[RC_LINEBUFFER];
char *exec = NULL;
char *name = NULL;
char *pidfile = NULL;
pid_t pid = 0;
pid_t *pids = NULL;
char *p;
char *token;
bool retval = false;
char *dirpath;
char **files;
char *file;
char *path;
int i;
FILE *fp;
char buffer[RC_LINEBUFFER];
char *exec = NULL;
char *name = NULL;
char *pidfile = NULL;
pid_t pid = 0;
pid_t *pids = NULL;
char *p;
char *token;
bool retval = false;
if (! service)
return (false);
if (! service)
return (false);
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL);
if (! rc_is_dir (dirpath))
{
free (dirpath);
return (false);
}
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
(char *) NULL);
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)
{
path = rc_strcatpaths (dirpath, file, (char *) NULL);
fp = fopen (path, "r");
free (path);
if (! fp)
{
eerror ("fopen `%s': %s", file, strerror (errno));
continue;
}
memset (buffer, 0, sizeof (buffer));
files = rc_ls_dir (NULL, dirpath, 0);
STRLIST_FOREACH (files, file, i) {
path = rc_strcatpaths (dirpath, file, (char *) NULL);
fp = fopen (path, "r");
free (path);
if (! fp) {
eerror ("fopen `%s': %s", file, strerror (errno));
continue;
}
while ((fgets (buffer, RC_LINEBUFFER, fp)))
{
int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n')
buffer[lb] = 0;
while ((fgets (buffer, RC_LINEBUFFER, fp))) {
int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n')
buffer[lb] = 0;
p = buffer;
if ((token = strsep (&p, "=")) == NULL || ! p)
continue;
p = buffer;
if ((token = strsep (&p, "=")) == NULL || ! p)
continue;
if (strlen (p) == 0)
continue;
if (strlen (p) == 0)
continue;
if (strcmp (token, "exec") == 0)
{
if (exec)
free (exec);
exec = strdup (p);
}
else if (strcmp (token, "name") == 0)
{
if (name)
free (name);
name = strdup (p);
}
else if (strcmp (token, "pidfile") == 0)
{
if (pidfile)
free (pidfile);
pidfile = strdup (p);
}
}
fclose (fp);
if (strcmp (token, "exec") == 0) {
if (exec)
free (exec);
exec = strdup (p);
} else if (strcmp (token, "name") == 0) {
if (name)
free (name);
name = strdup (p);
} else if (strcmp (token, "pidfile") == 0) {
if (pidfile)
free (pidfile);
pidfile = strdup (p);
}
}
fclose (fp);
pid = 0;
if (pidfile)
{
if (! rc_exists (pidfile))
{
retval = true;
break;
}
pid = 0;
if (pidfile) {
if (! rc_exists (pidfile)) {
retval = true;
break;
}
if ((fp = fopen (pidfile, "r")) == NULL)
{
eerror ("fopen `%s': %s", pidfile, strerror (errno));
retval = true;
break;
}
if ((fp = fopen (pidfile, "r")) == NULL) {
eerror ("fopen `%s': %s", pidfile, strerror (errno));
retval = true;
break;
}
if (fscanf (fp, "%d", &pid) != 1)
{
eerror ("no pid found in `%s'", pidfile);
fclose (fp);
retval = true;
break;
}
if (fscanf (fp, "%d", &pid) != 1) {
eerror ("no pid found in `%s'", pidfile);
fclose (fp);
retval = true;
break;
}
fclose (fp);
free (pidfile);
pidfile = NULL;
}
fclose (fp);
free (pidfile);
pidfile = NULL;
}
if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL)
{
retval = true;
break;
}
free (pids);
if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) {
retval = true;
break;
}
free (pids);
if (exec)
{
free (exec);
exec = NULL;
}
if (name)
{
free (name);
name = NULL;
}
}
if (exec) {
free (exec);
exec = NULL;
}
if (name) {
free (name);
name = NULL;
}
}
if (exec)
{
free (exec);
exec = NULL;
}
if (name)
{
free (name);
name = NULL;
}
if (exec) {
free (exec);
exec = NULL;
}
if (name) {
free (name);
name = NULL;
}
free (dirpath);
rc_strlist_free (files);
free (dirpath);
rc_strlist_free (files);
return (retval);
return (retval);
}

File diff suppressed because it is too large Load Diff

View File

@ -16,136 +16,130 @@
char **rc_strlist_add (char **list, const char *item)
{
char **newlist;
int i = 0;
char **newlist;
int i = 0;
if (! item)
return (list);
if (! item)
return (list);
while (list && list[i])
i++;
while (list && list[i])
i++;
newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
newlist[i] = rc_xstrdup (item);
newlist[i + 1] = NULL;
newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
newlist[i] = rc_xstrdup (item);
newlist[i + 1] = NULL;
return (newlist);
return (newlist);
}
static char **_rc_strlist_addsort (char **list, const char *item,
int (*sortfunc) (const char *s1,
const char *s2),
bool uniq)
int (*sortfunc) (const char *s1,
const char *s2),
bool uniq)
{
char **newlist;
int i = 0;
char *tmp1;
char *tmp2;
char **newlist;
int i = 0;
char *tmp1;
char *tmp2;
if (! item)
return (list);
if (! item)
return (list);
while (list && list[i])
{
if (uniq && strcmp (list[i], item) == 0)
return (list);
i++;
}
while (list && list[i]) {
if (uniq && strcmp (list[i], item) == 0)
return (list);
i++;
}
newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
if (! i)
newlist[i] = NULL;
newlist[i + 1] = NULL;
if (! i)
newlist[i] = NULL;
newlist[i + 1] = NULL;
i = 0;
while (newlist[i] && sortfunc (newlist[i], item) < 0)
i++;
i = 0;
while (newlist[i] && sortfunc (newlist[i], item) < 0)
i++;
tmp1 = newlist[i];
newlist[i] = rc_xstrdup (item);
do
{
i++;
tmp2 = newlist[i];
newlist[i] = tmp1;
tmp1 = tmp2;
} while (tmp1);
tmp1 = newlist[i];
newlist[i] = rc_xstrdup (item);
do {
i++;
tmp2 = newlist[i];
newlist[i] = tmp1;
tmp1 = tmp2;
} while (tmp1);
return (newlist);
return (newlist);
}
char **rc_strlist_addsort (char **list, const char *item)
{
return (_rc_strlist_addsort (list, item, strcoll, false));
return (_rc_strlist_addsort (list, item, strcoll, false));
}
char **rc_strlist_addsortc (char **list, const char *item)
{
return (_rc_strlist_addsort (list, item, strcmp, false));
return (_rc_strlist_addsort (list, item, strcmp, false));
}
char **rc_strlist_addsortu (char **list, const char *item)
{
return (_rc_strlist_addsort (list, item, strcmp, true));
return (_rc_strlist_addsort (list, item, strcmp, true));
}
char **rc_strlist_delete (char **list, const char *item)
{
int i = 0;
int i = 0;
if (!list || ! item)
return (list);
if (!list || ! item)
return (list);
while (list[i])
if (! strcmp (list[i], item))
{
free (list[i]);
do
{
list[i] = list[i + 1];
i++;
} while (list[i]);
}
while (list[i])
if (! strcmp (list[i], item)) {
free (list[i]);
do {
list[i] = list[i + 1];
i++;
} while (list[i]);
}
return (list);
return (list);
}
void rc_strlist_reverse (char **list)
{
char *item;
int i = 0;
int j = 0;
char *item;
int i = 0;
int j = 0;
if (! list)
return;
if (! list)
return;
while (list[j])
j++;
j--;
while (list[j])
j++;
j--;
while (i < j && list[i] && list[j])
{
item = list[i];
list[i] = list[j];
list[j] = item;
i++;
j--;
}
while (i < j && list[i] && list[j]) {
item = list[i];
list[i] = list[j];
list[j] = item;
i++;
j--;
}
}
void rc_strlist_free (char **list)
{
int i = 0;
int i = 0;
if (! list)
return;
if (! list)
return;
while (list[i])
{
free (list[i]);
list[i++] = NULL;
}
while (list[i]) {
free (list[i]);
list[i++] = NULL;
}
free (list);
free (list);
}

File diff suppressed because it is too large Load Diff

View File

@ -28,104 +28,98 @@
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
char **mounts, bool list_nodes, bool list_fstype)
char **mounts, bool list_nodes, bool list_fstype)
{
struct statfs *mnts;
int nmnts;
int i;
char **list = NULL;
struct statfs *mnts;
int nmnts;
int i;
char **list = NULL;
if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
eerrorx ("getmntinfo: %s", strerror (errno));
if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
eerrorx ("getmntinfo: %s", strerror (errno));
for (i = 0; i < nmnts; i++)
{
if (node_regex &&
regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
continue;
if (fstype_regex &&
regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
continue;
for (i = 0; i < nmnts; i++) {
if (node_regex &&
regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
continue;
if (fstype_regex &&
regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
continue;
if (mounts)
{
bool found = false;
int j;
char *mnt;
STRLIST_FOREACH (mounts, mnt, j)
if (strcmp (mnt, mnts[i].f_mntonname) == 0)
{
found = true;
break;
}
if (! found)
continue;
}
if (mounts) {
bool found = false;
int j;
char *mnt;
STRLIST_FOREACH (mounts, mnt, j)
if (strcmp (mnt, mnts[i].f_mntonname) == 0) {
found = true;
break;
}
if (! found)
continue;
}
list = rc_strlist_addsortc (list, list_nodes ?
mnts[i].f_mntfromname :
list_fstype ? mnts[i].f_fstypename :
mnts[i].f_mntonname);
}
list = rc_strlist_addsortc (list, list_nodes ?
mnts[i].f_mntfromname :
list_fstype ? mnts[i].f_fstypename :
mnts[i].f_mntonname);
}
return (list);
return (list);
}
#elif defined (__linux__)
static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
char **mounts, bool list_nodes, bool list_fstype)
char **mounts, bool list_nodes, bool list_fstype)
{
FILE *fp;
char buffer[PATH_MAX * 3];
char *p;
char *from;
char *to;
char *fstype;
char **list = NULL;
FILE *fp;
char buffer[PATH_MAX * 3];
char *p;
char *from;
char *to;
char *fstype;
char **list = NULL;
if ((fp = fopen ("/proc/mounts", "r")) == NULL)
eerrorx ("getmntinfo: %s", strerror (errno));
if ((fp = fopen ("/proc/mounts", "r")) == NULL)
eerrorx ("getmntinfo: %s", strerror (errno));
while (fgets (buffer, sizeof (buffer), fp))
{
p = buffer;
from = strsep (&p, " ");
if (node_regex &&
regexec (node_regex, from, 0, NULL, 0) != 0)
continue;
while (fgets (buffer, sizeof (buffer), fp)) {
p = buffer;
from = strsep (&p, " ");
if (node_regex &&
regexec (node_regex, from, 0, NULL, 0) != 0)
continue;
to = strsep (&p, " ");
fstype = strsep (&p, " ");
/* Skip the really silly rootfs */
if (strcmp (fstype, "rootfs") == 0)
continue;
if (fstype_regex &&
regexec (fstype_regex, fstype, 0, NULL, 0) != 0)
continue;
to = strsep (&p, " ");
fstype = strsep (&p, " ");
/* Skip the really silly rootfs */
if (strcmp (fstype, "rootfs") == 0)
continue;
if (fstype_regex &&
regexec (fstype_regex, fstype, 0, NULL, 0) != 0)
continue;
if (mounts)
{
bool found = false;
int j;
char *mnt;
STRLIST_FOREACH (mounts, mnt, j)
if (strcmp (mnt, to) == 0)
{
found = true;
break;
}
if (! found)
continue;
}
if (mounts) {
bool found = false;
int j;
char *mnt;
STRLIST_FOREACH (mounts, mnt, j)
if (strcmp (mnt, to) == 0) {
found = true;
break;
}
if (! found)
continue;
}
list = rc_strlist_addsortc (list,
list_nodes ?
list_fstype ? fstype :
from : to);
}
fclose (fp);
list = rc_strlist_addsortc (list,
list_nodes ?
list_fstype ? fstype :
from : to);
}
fclose (fp);
return (list);
return (list);
}
#else
@ -134,113 +128,105 @@ static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
int main (int argc, char **argv)
{
int i;
regex_t *fstype_regex = NULL;
regex_t *node_regex = NULL;
regex_t *skip_regex = NULL;
char **nodes = NULL;
char *node;
int result;
char buffer[256];
bool list_nodes = false;
bool list_fstype = false;
bool reverse = false;
char **mounts = NULL;
int i;
regex_t *fstype_regex = NULL;
regex_t *node_regex = NULL;
regex_t *skip_regex = NULL;
char **nodes = NULL;
char *node;
int result;
char buffer[256];
bool list_nodes = false;
bool list_fstype = false;
bool reverse = false;
char **mounts = NULL;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc))
{
i++;
if (fstype_regex)
free (fstype_regex);
fstype_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (fstype_regex, argv[i],
REG_EXTENDED | REG_NOSUB)) != 0)
{
regerror (result, fstype_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
}
continue;
}
for (i = 1; i < argc; i++) {
if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc)) {
i++;
if (fstype_regex)
free (fstype_regex);
fstype_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (fstype_regex, argv[i],
REG_EXTENDED | REG_NOSUB)) != 0)
{
regerror (result, fstype_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
}
continue;
}
if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc))
{
i++;
if (node_regex)
free (node_regex);
node_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (node_regex, argv[i],
REG_EXTENDED | REG_NOSUB)) != 0)
{
regerror (result, node_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
}
continue;
}
if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) {
i++;
if (node_regex)
free (node_regex);
node_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (node_regex, argv[i],
REG_EXTENDED | REG_NOSUB)) != 0)
{
regerror (result, node_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
}
continue;
}
if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc))
{
i++;
if (skip_regex)
free (skip_regex);
skip_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (skip_regex, argv[i],
REG_EXTENDED | REG_NOSUB)) != 0)
{
regerror (result, skip_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
}
continue;
}
if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) {
i++;
if (skip_regex)
free (skip_regex);
skip_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (skip_regex, argv[i],
REG_EXTENDED | REG_NOSUB)) != 0)
{
regerror (result, skip_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
}
continue;
}
if (strcmp (argv[i], "--fstype") == 0)
{
list_fstype = true;
continue;
}
if (strcmp (argv[i], "--fstype") == 0) {
list_fstype = true;
continue;
}
if (strcmp (argv[i], "--node") == 0)
{
list_nodes = true;
continue;
}
if (strcmp (argv[i], "--reverse") == 0)
{
reverse = true;
continue;
}
if (strcmp (argv[i], "--node") == 0) {
list_nodes = true;
continue;
}
if (strcmp (argv[i], "--reverse") == 0) {
reverse = true;
continue;
}
if (argv[i][0] != '/')
eerrorx ("%s: `%s' is not a mount point", argv[0], argv[i]);
if (argv[i][0] != '/')
eerrorx ("%s: `%s' is not a mount point", argv[0], argv[i]);
mounts = rc_strlist_add (mounts, argv[i]);
}
mounts = rc_strlist_add (mounts, argv[i]);
}
nodes = find_mounts (node_regex, fstype_regex, mounts,
list_nodes, list_fstype);
nodes = find_mounts (node_regex, fstype_regex, mounts,
list_nodes, list_fstype);
if (node_regex)
regfree (node_regex);
if (fstype_regex)
regfree (fstype_regex);
if (node_regex)
regfree (node_regex);
if (fstype_regex)
regfree (fstype_regex);
if (reverse)
rc_strlist_reverse (nodes);
if (reverse)
rc_strlist_reverse (nodes);
result = EXIT_FAILURE;
STRLIST_FOREACH (nodes, node, i)
{
if (skip_regex && regexec (skip_regex, node, 0, NULL, 0) == 0)
continue;
printf ("%s\n", node);
result = EXIT_SUCCESS;
}
rc_strlist_free (nodes);
result = EXIT_FAILURE;
STRLIST_FOREACH (nodes, node, i) {
if (skip_regex && regexec (skip_regex, node, 0, NULL, 0) == 0)
continue;
printf ("%s\n", node);
result = EXIT_SUCCESS;
}
rc_strlist_free (nodes);
if (skip_regex)
free (skip_regex);
if (skip_regex)
free (skip_regex);
exit (result);
exit (result);
}

View File

@ -20,101 +20,89 @@
int main (int argc, char **argv)
{
char **types = NULL;
char **services = NULL;
char **depends = NULL;
rc_depinfo_t *deptree = NULL;
rc_depinfo_t *di;
char *service;
int options = RC_DEP_TRACE;
bool first = true;
int i;
bool update = false;
char *runlevel = getenv ("RC_SOFTLEVEL");
char **types = NULL;
char **services = NULL;
char **depends = NULL;
rc_depinfo_t *deptree = NULL;
rc_depinfo_t *di;
char *service;
int options = RC_DEP_TRACE;
bool first = true;
int i;
bool update = false;
char *runlevel = getenv ("RC_SOFTLEVEL");
if (! runlevel)
runlevel = rc_get_runlevel ();
if (! runlevel)
runlevel = rc_get_runlevel ();
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "--update") == 0)
{
if (! update)
{
rc_update_deptree (true);
update = true;
}
continue;
}
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)
{
options |= RC_DEP_STRICT;
continue;
}
if (strcmp (argv[i], "--strict") == 0) {
options |= RC_DEP_STRICT;
continue;
}
if (strcmp (argv[i], "--notrace") == 0)
{
options &= RC_DEP_TRACE;
continue;
}
if (strcmp (argv[i], "--notrace") == 0) {
options &= RC_DEP_TRACE;
continue;
}
if (argv[i][0] == '-')
{
argv[i]++;
types = rc_strlist_add (types, argv[i]);
}
else
{
if ((deptree = rc_load_deptree ()) == NULL)
eerrorx ("failed to load deptree");
if (argv[i][0] == '-') {
argv[i]++;
types = rc_strlist_add (types, argv[i]);
} else {
if ((deptree = rc_load_deptree ()) == NULL)
eerrorx ("failed to load deptree");
di = rc_get_depinfo (deptree, argv[i]);
if (! di)
eerror ("no dependency info for service `%s'", argv[i]);
else
services = rc_strlist_add (services, argv[i]);
}
}
di = rc_get_depinfo (deptree, argv[i]);
if (! di)
eerror ("no dependency info for service `%s'", argv[i]);
else
services = rc_strlist_add (services, argv[i]);
}
}
if (! services)
{
rc_strlist_free (types);
rc_free_deptree (deptree);
if (update)
return (EXIT_SUCCESS);
eerrorx ("no services specified");
}
if (! services) {
rc_strlist_free (types);
rc_free_deptree (deptree);
if (update)
return (EXIT_SUCCESS);
eerrorx ("no services specified");
}
/* If we don't have any types, then supply some defaults */
if (! types)
{
types = rc_strlist_add (NULL, "ineed");
rc_strlist_add (types, "iuse");
}
/* If we don't have any types, then supply some defaults */
if (! types) {
types = rc_strlist_add (NULL, "ineed");
rc_strlist_add (types, "iuse");
}
depends = rc_get_depends (deptree, types, services, runlevel, options);
depends = rc_get_depends (deptree, types, services, runlevel, options);
if (depends)
{
STRLIST_FOREACH (depends, service, i)
{
if (first)
first = false;
else
printf (" ");
if (depends) {
STRLIST_FOREACH (depends, service, i) {
if (first)
first = false;
else
printf (" ");
if (service)
printf ("%s", service);
if (service)
printf ("%s", service);
}
printf ("\n");
}
}
printf ("\n");
}
rc_strlist_free (types);
rc_strlist_free (services);
rc_strlist_free (depends);
rc_free_deptree (deptree);
rc_strlist_free (types);
rc_strlist_free (services);
rc_strlist_free (depends);
rc_free_deptree (deptree);
return (EXIT_SUCCESS);
return (EXIT_SUCCESS);
}

View File

@ -19,103 +19,94 @@
typedef struct plugin
{
char *name;
void *handle;
int (*hook) (rc_hook_t hook, const char *name);
struct plugin *next;
char *name;
void *handle;
int (*hook) (rc_hook_t hook, const char *name);
struct plugin *next;
} plugin_t;
static plugin_t *plugins = NULL;
void rc_plugin_load (void)
{
char **files;
char *file;
int i;
plugin_t *plugin = plugins;
char **files;
char *file;
int i;
plugin_t *plugin = plugins;
/* Ensure some sanity here */
rc_plugin_unload ();
/* Ensure some sanity here */
rc_plugin_unload ();
if (! rc_exists (RC_PLUGINDIR))
return;
if (! rc_exists (RC_PLUGINDIR))
return;
files = rc_ls_dir (NULL, RC_PLUGINDIR, 0);
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;
files = rc_ls_dir (NULL, RC_PLUGINDIR, 0);
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)
{
eerror ("dlopen `%s': %s", p, dlerror ());
free (p);
continue;
}
if (! h) {
eerror ("dlopen `%s': %s", p, dlerror ());
free (p);
continue;
}
func = file;
file = strsep (&func, ".");
len = strlen (file) + 7;
func = rc_xmalloc (sizeof (char *) * len);
snprintf (func, len, "_%s_hook", file);
func = file;
file = strsep (&func, ".");
len = strlen (file) + 7;
func = rc_xmalloc (sizeof (char *) * len);
snprintf (func, len, "_%s_hook", file);
f = dlsym (h, func);
if (! f)
{
eerror ("`%s' does not expose the symbol `%s'", p, func);
dlclose (h);
}
else
{
if (plugin)
{
plugin->next = rc_xmalloc (sizeof (plugin_t));
plugin = plugin->next;
}
else
plugin = plugins = rc_xmalloc (sizeof (plugin_t));
f = dlsym (h, func);
if (! f) {
eerror ("`%s' does not expose the symbol `%s'", p, func);
dlclose (h);
} else {
if (plugin) {
plugin->next = rc_xmalloc (sizeof (plugin_t));
plugin = plugin->next;
} else
plugin = plugins = rc_xmalloc (sizeof (plugin_t));
memset (plugin, 0, sizeof (plugin_t));
plugin->name = strdup (file);
plugin->handle = h;
plugin->hook = f;
}
memset (plugin, 0, sizeof (plugin_t));
plugin->name = strdup (file);
plugin->handle = h;
plugin->hook = f;
}
free (func);
free (p);
}
free (func);
free (p);
}
rc_strlist_free (files);
rc_strlist_free (files);
}
void rc_plugin_run (rc_hook_t hook, const char *value)
{
plugin_t *plugin = plugins;
plugin_t *plugin = plugins;
while (plugin)
{
if (plugin->hook)
plugin->hook (hook, value);
while (plugin) {
if (plugin->hook)
plugin->hook (hook, value);
plugin = plugin->next;
}
plugin = plugin->next;
}
}
void rc_plugin_unload (void)
{
plugin_t *plugin = plugins;
plugin_t *next;
plugin_t *plugin = plugins;
plugin_t *next;
while (plugin)
{
next = plugin->next;
dlclose (plugin->handle);
free (plugin->name);
free (plugin);
plugin = next;
}
plugins = NULL;
while (plugin) {
next = plugin->next;
dlclose (plugin->handle);
free (plugin->name);
free (plugin);
plugin = next;
}
plugins = NULL;
}

View File

@ -18,125 +18,112 @@
static void print_level (char *level)
{
printf ("Runlevel: ");
PEINFO_HILITE;
printf ("%s\n", level);
PEINFO_NORMAL;
printf ("Runlevel: ");
PEINFO_HILITE;
printf ("%s\n", level);
PEINFO_NORMAL;
}
static void print_service (char *service)
{
char status[10];
int cols = printf (" %s\n", service);
einfo_color_t color = einfo_bad;
char status[10];
int cols = printf (" %s\n", service);
einfo_color_t color = einfo_bad;
if (rc_service_state (service, rc_service_stopping))
snprintf (status, sizeof (status), "stopping ");
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))
{
snprintf (status, sizeof (status), "inactive ");
color = einfo_warn;
}
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))
{
snprintf (status, sizeof (status), " started ");
color = einfo_good;
}
else if (rc_service_state (service, rc_service_scheduled))
{
snprintf (status, sizeof (status), "scheduled");
color = einfo_warn;
}
else
snprintf (status, sizeof (status), " stopped ");
ebracket (cols, color, status);
if (rc_service_state (service, rc_service_stopping))
snprintf (status, sizeof (status), "stopping ");
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)) {
snprintf (status, sizeof (status), "inactive ");
color = einfo_warn;
} 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)) {
snprintf (status, sizeof (status), " started ");
color = einfo_good;
} else if (rc_service_state (service, rc_service_scheduled)) {
snprintf (status, sizeof (status), "scheduled");
color = einfo_warn;
} else
snprintf (status, sizeof (status), " stopped ");
ebracket (cols, color, status);
}
int main (int argc, char **argv)
{
char **levels = NULL;
char **services = NULL;
char *level;
char *service;
char c;
int option_index = 0;
int i;
int j;
char **levels = NULL;
char **services = NULL;
char *level;
char *service;
char c;
int option_index = 0;
int i;
int j;
const struct option longopts[] =
{
{"all", no_argument, NULL, 'a'},
{"list", no_argument, NULL, 'l'},
{"servicelist", no_argument, NULL, 's'},
{"unused", no_argument, NULL, 'u'},
{NULL, 0, NULL, 0}
};
const struct option longopts[] = {
{"all", no_argument, NULL, 'a'},
{"list", no_argument, NULL, 'l'},
{"servicelist", no_argument, NULL, 's'},
{"unused", no_argument, NULL, 'u'},
{NULL, 0, NULL, 0}
};
while ((c = getopt_long(argc, argv, "alsu", longopts, &option_index)) != -1)
switch (c)
{
case 'a':
levels = rc_get_runlevels ();
break;
case 'l':
levels = rc_get_runlevels ();
STRLIST_FOREACH (levels, level, i)
printf ("%s\n", level);
rc_strlist_free (levels);
exit (EXIT_SUCCESS);
case 's':
services = rc_services_in_runlevel (NULL);
STRLIST_FOREACH (services, service, i)
print_service (service);
rc_strlist_free (services);
exit (EXIT_SUCCESS);
case 'u':
services = rc_services_in_runlevel (NULL);
levels = rc_get_runlevels ();
STRLIST_FOREACH (services, service, i)
{
bool found = false;
STRLIST_FOREACH (levels, level, j)
if (rc_service_in_runlevel (service, level))
{
found = true;
break;
}
if (! found)
print_service (service);
}
rc_strlist_free (levels);
rc_strlist_free (services);
exit (EXIT_SUCCESS);
case '?':
exit (EXIT_FAILURE);
default:
exit (EXIT_FAILURE);
}
while ((c = getopt_long(argc, argv, "alsu", longopts, &option_index)) != -1)
switch (c) {
case 'a':
levels = rc_get_runlevels ();
break;
case 'l':
levels = rc_get_runlevels ();
STRLIST_FOREACH (levels, level, i)
printf ("%s\n", level);
rc_strlist_free (levels);
exit (EXIT_SUCCESS);
case 's':
services = rc_services_in_runlevel (NULL);
STRLIST_FOREACH (services, service, i)
print_service (service);
rc_strlist_free (services);
exit (EXIT_SUCCESS);
case 'u':
services = rc_services_in_runlevel (NULL);
levels = rc_get_runlevels ();
STRLIST_FOREACH (services, service, i) {
bool found = false;
STRLIST_FOREACH (levels, level, j)
if (rc_service_in_runlevel (service, level)) {
found = true;
break;
}
if (! found)
print_service (service);
}
rc_strlist_free (levels);
rc_strlist_free (services);
exit (EXIT_SUCCESS);
case '?':
exit (EXIT_FAILURE);
default:
exit (EXIT_FAILURE);
}
while (optind < argc)
levels = rc_strlist_add (levels, argv[optind++]);
while (optind < argc)
levels = rc_strlist_add (levels, argv[optind++]);
if (! levels)
levels = rc_strlist_add (NULL, rc_get_runlevel ());
if (! levels)
levels = rc_strlist_add (NULL, rc_get_runlevel ());
STRLIST_FOREACH (levels, level, i)
{
print_level (level);
services = rc_services_in_runlevel (level);
STRLIST_FOREACH (services, service, j)
print_service (service);
rc_strlist_free (services);
}
STRLIST_FOREACH (levels, level, i) {
print_level (level);
services = rc_services_in_runlevel (level);
STRLIST_FOREACH (services, service, j)
print_service (service);
rc_strlist_free (services);
}
rc_strlist_free (levels);
rc_strlist_free (levels);
return (EXIT_SUCCESS);
return (EXIT_SUCCESS);
}

View File

@ -22,141 +22,130 @@ static char *applet = NULL;
static bool add (const char *runlevel, const char *service)
{
bool retval = true;
bool retval = true;
if (! rc_runlevel_exists (runlevel))
{
ewarn ("runlevel `%s' does not exist", runlevel);
return (false);
}
if (rc_service_in_runlevel (service, runlevel))
{
ewarn ("%s already installed in runlevel `%s'; skipping",
service, runlevel);
return (false);
}
if (! rc_runlevel_exists (runlevel)) {
ewarn ("runlevel `%s' does not exist", runlevel);
return (false);
}
if (rc_service_in_runlevel (service, runlevel)) {
ewarn ("%s already installed in runlevel `%s'; skipping",
service, runlevel);
return (false);
}
if (rc_service_add (runlevel, service))
einfo ("%s added to runlevel %s", service, runlevel);
else
{
eerror ("%s: failed to add service `%s' to runlevel `%s': %s",
applet, service, runlevel, strerror (errno));
retval = false;
}
if (rc_service_add (runlevel, service))
einfo ("%s added to runlevel %s", service, runlevel);
else {
eerror ("%s: failed to add service `%s' to runlevel `%s': %s",
applet, service, runlevel, strerror (errno));
retval = false;
}
return (retval);
return (retval);
}
int main (int argc, char **argv)
{
int i;
int j;
char *service;
char **runlevels = NULL;
char *runlevel;
int i;
int j;
char *service;
char **runlevels = NULL;
char *runlevel;
applet = argv[0];
if (argc < 2 ||
strcmp (argv[1], "show") == 0 ||
strcmp (argv[1], "-s") == 0)
{
bool verbose = false;
char **services = rc_services_in_runlevel (NULL);
applet = argv[0];
if (argc < 2 ||
strcmp (argv[1], "show") == 0 ||
strcmp (argv[1], "-s") == 0)
{
bool verbose = false;
char **services = rc_services_in_runlevel (NULL);
for (i = 2; i < argc; i++)
{
if (strcmp (argv[i], "--verbose") == 0 ||
strcmp (argv[i], "-v") == 0)
verbose = true;
else
runlevels = rc_strlist_add (runlevels, argv[i]);
}
for (i = 2; i < argc; i++) {
if (strcmp (argv[i], "--verbose") == 0 ||
strcmp (argv[i], "-v") == 0)
verbose = true;
else
runlevels = rc_strlist_add (runlevels, argv[i]);
}
if (! runlevels)
runlevels = rc_get_runlevels ();
if (! runlevels)
runlevels = rc_get_runlevels ();
STRLIST_FOREACH (services, service, i)
{
char **in = NULL;
bool inone = false;
STRLIST_FOREACH (services, service, i) {
char **in = NULL;
bool inone = false;
STRLIST_FOREACH (runlevels, runlevel, j)
{
if (rc_service_in_runlevel (service, runlevel))
{
in = rc_strlist_add (in, runlevel);
inone = true;
}
else
{
char buffer[PATH_MAX];
memset (buffer, ' ', strlen (runlevel));
buffer[strlen (runlevel)] = 0;
in = rc_strlist_add (in, buffer);
}
}
STRLIST_FOREACH (runlevels, runlevel, j) {
if (rc_service_in_runlevel (service, runlevel)) {
in = rc_strlist_add (in, runlevel);
inone = true;
} else {
char buffer[PATH_MAX];
memset (buffer, ' ', strlen (runlevel));
buffer[strlen (runlevel)] = 0;
in = rc_strlist_add (in, buffer);
}
}
if (! inone && ! verbose)
continue;
if (! inone && ! verbose)
continue;
printf (" %20s |", service);
STRLIST_FOREACH (in, runlevel, j)
printf (" %s", runlevel);
printf ("\n");
}
printf (" %20s |", service);
STRLIST_FOREACH (in, runlevel, j)
printf (" %s", runlevel);
printf ("\n");
}
return (EXIT_SUCCESS);
}
return (EXIT_SUCCESS);
}
if (geteuid () != 0)
eerrorx ("%s: must be root to add or delete services from runlevels",
applet);
if (geteuid () != 0)
eerrorx ("%s: must be root to add or delete services from runlevels",
applet);
if (! (service = argv[2]))
eerrorx ("%s: no service specified", applet);
if (! (service = argv[2]))
eerrorx ("%s: no service specified", applet);
if (strcmp (argv[1], "add") == 0 ||
strcmp (argv[1], "-a") == 0)
{
if (! service)
eerrorx ("%s: no service specified", applet);
if (! rc_service_exists (service))
eerrorx ("%s: service `%s' does not exist", applet, service);
if (strcmp (argv[1], "add") == 0 ||
strcmp (argv[1], "-a") == 0)
{
if (! service)
eerrorx ("%s: no service specified", applet);
if (! rc_service_exists (service))
eerrorx ("%s: service `%s' does not exist", applet, service);
if (argc < 4)
add (rc_get_runlevel (), service);
if (argc < 4)
add (rc_get_runlevel (), service);
for (i = 3; i < argc; i++)
add (argv[i], service);
for (i = 3; i < argc; i++)
add (argv[i], service);
return (EXIT_SUCCESS);
}
return (EXIT_SUCCESS);
}
if (strcmp (argv[1], "delete") == 0 ||
strcmp (argv[1], "del") == 0 ||
strcmp (argv[1], "-d") == 0)
{
for (i = 3; i < argc; i++)
runlevels = rc_strlist_add (runlevels, argv[i]);
if (strcmp (argv[1], "delete") == 0 ||
strcmp (argv[1], "del") == 0 ||
strcmp (argv[1], "-d") == 0)
{
for (i = 3; i < argc; i++)
runlevels = rc_strlist_add (runlevels, argv[i]);
if (! runlevels)
runlevels = rc_strlist_add (runlevels, rc_get_runlevel ());
if (! runlevels)
runlevels = rc_strlist_add (runlevels, rc_get_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
eerror ("%s: failed to remove service `%s' from runlevel `%s': %s",
applet, service, runlevel, strerror (errno));
}
}
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
eerror ("%s: failed to remove service `%s' from runlevel `%s': %s",
applet, service, runlevel, strerror (errno));
}
}
return (EXIT_SUCCESS);
}
return (EXIT_SUCCESS);
}
eerrorx ("%s: unknown command `%s'", applet, argv[1]);
eerrorx ("%s: unknown command `%s'", applet, argv[1]);
}

1816
src/rc.c

File diff suppressed because it is too large Load Diff

View File

@ -25,16 +25,16 @@
typedef enum
{
rc_service_started,
rc_service_stopped,
rc_service_starting,
rc_service_stopping,
rc_service_inactive,
rc_service_wasinactive,
rc_service_coldplugged,
rc_service_failed,
rc_service_scheduled,
rc_service_crashed
rc_service_started,
rc_service_stopped,
rc_service_starting,
rc_service_stopping,
rc_service_inactive,
rc_service_wasinactive,
rc_service_coldplugged,
rc_service_failed,
rc_service_scheduled,
rc_service_crashed
} rc_service_state_t;
char *rc_resolve_service (const char *service);
@ -45,19 +45,19 @@ bool rc_mark_service (const char *service, rc_service_state_t state);
pid_t rc_stop_service (const char *service);
pid_t rc_start_service (const char *service);
void rc_schedule_start_service (const char *service,
const char *service_to_start);
const char *service_to_start);
char **rc_services_scheduled_by (const char *service);
void rc_schedule_clear (const char *service);
bool rc_wait_service (const char *service);
bool rc_get_service_option (const char *service, const char *option,
char *value);
char *value);
bool rc_set_service_option (const char *service, const char *option,
const char *value);
const char *value);
void rc_set_service_daemon (const char *service, const char *exec,
const char *name, const char *pidfile,
bool started);
const char *name, const char *pidfile,
bool started);
bool rc_service_started_daemon (const char *service, const char *exec,
int indx);
int indx);
bool rc_allow_plug (char *service);
@ -75,7 +75,7 @@ char **rc_services_scheduled (const char *service);
/* Find pids based on criteria - free the pointer returned after use */
pid_t *rc_find_pids (const char *exec, const char *cmd,
uid_t uid, pid_t pid);
uid_t uid, pid_t pid);
/* Checks that all daemons started with start-stop-daemon by the service
are still running. If so, return false otherwise true.
You should check that the service has been started before calling this. */
@ -84,16 +84,16 @@ bool rc_service_daemons_crashed (const char *service);
/* Dependency tree structs and functions. */
typedef struct rc_deptype
{
char *type;
char **services;
struct rc_deptype *next;
char *type;
char **services;
struct rc_deptype *next;
} rc_deptype_t;
typedef struct rc_depinfo
{
char *service;
rc_deptype_t *depends;
struct rc_depinfo *next;
char *service;
rc_deptype_t *depends;
struct rc_depinfo *next;
} rc_depinfo_t;
@ -110,14 +110,14 @@ rc_depinfo_t *rc_load_deptree (void);
rc_depinfo_t *rc_get_depinfo (rc_depinfo_t *deptree, const char *service);
rc_deptype_t *rc_get_deptype (rc_depinfo_t *depinfo, const char *type);
char **rc_get_depends (rc_depinfo_t *deptree, char **types,
char **services, const char *runlevel, int options);
char **services, const char *runlevel, int options);
/* List all the services that should be started, in order, the the
given runlevel, including sysinit and boot services where
approriate.
If reboot, shutdown or single are given then we list all the services
we that we need to shutdown in order. */
char **rc_order_services (rc_depinfo_t *deptree, const char *runlevel,
int options);
int options);
void rc_free_deptree (rc_depinfo_t *deptree);
@ -134,19 +134,19 @@ void rc_free_deptree (rc_depinfo_t *deptree);
when we have done it as may start scheduled services at this point. */
typedef enum
{
rc_hook_runlevel_stop_in = 1,
rc_hook_runlevel_stop_out = 4,
rc_hook_runlevel_start_in = 5,
rc_hook_runlevel_start_out = 8,
/* We reserved a few numbers if we need rc_runlevel_stop_now and done */
rc_hook_service_stop_in = 101,
rc_hook_service_stop_now,
rc_hook_service_stop_done,
rc_hook_service_stop_out,
rc_hook_service_start_in,
rc_hook_service_start_now,
rc_hook_service_start_done,
rc_hook_service_start_out
rc_hook_runlevel_stop_in = 1,
rc_hook_runlevel_stop_out = 4,
rc_hook_runlevel_start_in = 5,
rc_hook_runlevel_start_out = 8,
/* We reserved a few numbers if we need rc_runlevel_stop_now and done */
rc_hook_service_stop_in = 101,
rc_hook_service_stop_now,
rc_hook_service_stop_done,
rc_hook_service_stop_out,
rc_hook_service_start_in,
rc_hook_service_start_now,
rc_hook_service_start_done,
rc_hook_service_start_out
} rc_hook_t;
/* RC utility functions.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,7 @@
it should usually by +1 from what you expect, and should only be
used in the scope of the macro) */
#define STRLIST_FOREACH(_list, _pos, _counter) \
if ((_list) && _list[0] && ! (_counter = 0)) \
while ((_pos = _list[_counter++]))
if ((_list) && _list[0] && ! (_counter = 0)) \
while ((_pos = _list[_counter++]))
#endif /* __STRLIST_H__ */