Improve style for KNF

This commit is contained in:
Roy Marples 2009-04-23 21:31:22 +00:00
parent 6953474655
commit ade85d4bd8
20 changed files with 760 additions and 667 deletions

View File

@ -1,7 +1,7 @@
/* /*
einfo.c einfo.c
Informational functions Informational functions
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -110,10 +110,10 @@ hidden_proto(ewendv)
#define UP "\033[A" #define UP "\033[A"
#define _GET_CAP(_d, _c) strlcpy(_d, tgoto(_c, 0, 0), sizeof(_d)); #define _GET_CAP(_d, _c) strlcpy(_d, tgoto(_c, 0, 0), sizeof(_d));
#define _ASSIGN_CAP(_v) { \ #define _ASSIGN_CAP(_v) do { \
_v = p; \ _v = p; \
p += strlcpy(p, tmp, sizeof(ebuffer) - (p - ebuffer)) + 1; \ p += strlcpy(p, tmp, sizeof(ebuffer) - (p - ebuffer)) + 1; \
} } while (0)
/* A pointer to a string to prefix to einfo/ewarn/eerror messages */ /* A pointer to a string to prefix to einfo/ewarn/eerror messages */
static const char *_eprefix = NULL; static const char *_eprefix = NULL;
@ -497,7 +497,7 @@ colour_terminal(FILE * EINFO_RESTRICT f)
_GET_CAP(tmp, _me); _GET_CAP(tmp, _me);
if (tmp[0]) if (tmp[0])
_ASSIGN_CAP(ecolors_str[i]) _ASSIGN_CAP(ecolors_str[i]);
else else
ecolors_str[i] = &nullstr; ecolors_str[i] = &nullstr;
} }
@ -627,10 +627,10 @@ ecolor(ECOLOR color)
#define LASTCMD(_cmd) { \ #define LASTCMD(_cmd) { \
unsetenv("EINFO_LASTCMD"); \ unsetenv("EINFO_LASTCMD"); \
setenv("EINFO_LASTCMD", _cmd, 1); \ setenv("EINFO_LASTCMD", _cmd, 1); \
} }
static int EINFO_PRINTF(3, 0) static int EINFO_PRINTF(3, 0)
_einfo(FILE *f, ECOLOR color, const char *EINFO_RESTRICT fmt, va_list va) _einfo(FILE *f, ECOLOR color, const char *EINFO_RESTRICT fmt, va_list va)
{ {
int retval = 0; int retval = 0;
char *last = getenv("EINFO_LASTCMD"); char *last = getenv("EINFO_LASTCMD");
@ -847,7 +847,8 @@ _eend(FILE * EINFO_RESTRICT fp, int col, ECOLOR color, const char *msg)
} }
static int EINFO_PRINTF(3, 0) static int EINFO_PRINTF(3, 0)
_do_eend(const char *cmd, int retval, const char *EINFO_RESTRICT fmt, va_list ap) _do_eend(const char *cmd, int retval,
const char *EINFO_RESTRICT fmt, va_list ap)
{ {
int col = 0; int col = 0;
FILE *fp = stdout; FILE *fp = stdout;

View File

@ -1,7 +1,7 @@
/* /*
librc-daemon librc-daemon
Finds PID for given daemon criteria Finds PID for given daemon criteria
*/ */
/* /*
* Copyright 2007-2009 Roy Marples <roy@marples.name> * Copyright 2007-2009 Roy Marples <roy@marples.name>
@ -32,7 +32,8 @@
#include "librc.h" #include "librc.h"
#if defined(__linux__) #if defined(__linux__)
static bool pid_is_exec(pid_t pid, const char *exec) static bool
pid_is_exec(pid_t pid, const char *exec)
{ {
char buffer[32]; char buffer[32];
FILE *fp; FILE *fp;
@ -55,7 +56,8 @@ static bool pid_is_exec(pid_t pid, const char *exec)
return retval; return retval;
} }
static bool pid_is_argv(pid_t pid, const char *const *argv) static bool
pid_is_argv(pid_t pid, const char *const *argv)
{ {
char cmdline[32]; char cmdline[32];
int fd; int fd;
@ -84,8 +86,8 @@ static bool pid_is_argv(pid_t pid, const char *const *argv)
return true; return true;
} }
RC_PIDLIST *rc_find_pids(const char *exec, const char *const *argv, RC_PIDLIST *
uid_t uid, pid_t pid) rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
{ {
DIR *procdir; DIR *procdir;
struct dirent *entry; struct dirent *entry;
@ -170,8 +172,8 @@ librc_hidden_def(rc_find_pids)
# define _KVM_FLAGS O_RDONLY # define _KVM_FLAGS O_RDONLY
# endif # endif
RC_PIDLIST *rc_find_pids(const char *exec, const char *const *argv, RC_PIDLIST *
uid_t uid, pid_t pid) rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
{ {
static kvm_t *kd = NULL; static kvm_t *kd = NULL;
char errbuf[_POSIX2_LINE_MAX]; char errbuf[_POSIX2_LINE_MAX];
@ -249,8 +251,8 @@ librc_hidden_def(rc_find_pids)
# error "Platform not supported!" # error "Platform not supported!"
#endif #endif
static bool _match_daemon(const char *path, const char *file, static bool
RC_STRINGLIST *match) _match_daemon(const char *path, const char *file, RC_STRINGLIST *match)
{ {
char *line = NULL; char *line = NULL;
size_t len = 0; size_t len = 0;
@ -280,8 +282,8 @@ static bool _match_daemon(const char *path, const char *file,
return true; return true;
} }
static RC_STRINGLIST *_match_list(const char *exec, const char* const* argv, static RC_STRINGLIST *
const char *pidfile) _match_list(const char *exec, const char* const* argv, const char *pidfile)
{ {
RC_STRINGLIST *match = rc_stringlist_new(); RC_STRINGLIST *match = rc_stringlist_new();
int i = 0; int i = 0;
@ -315,7 +317,8 @@ static RC_STRINGLIST *_match_list(const char *exec, const char* const* argv,
return match; return match;
} }
bool rc_service_daemon_set(const char *service, const char *exec, bool
rc_service_daemon_set(const char *service, const char *exec,
const char *const *argv, const char *const *argv,
const char *pidfile, bool started) const char *pidfile, bool started)
{ {
@ -392,9 +395,9 @@ bool rc_service_daemon_set(const char *service, const char *exec,
} }
librc_hidden_def(rc_service_daemon_set) librc_hidden_def(rc_service_daemon_set)
bool rc_service_started_daemon(const char *service, bool
const char *exec, const char *const *argv, rc_service_started_daemon(const char *service,
int indx) const char *exec, const char *const *argv, int indx)
{ {
char dirpath[PATH_MAX]; char dirpath[PATH_MAX];
char file[16]; char file[16];
@ -431,7 +434,8 @@ bool rc_service_started_daemon(const char *service,
} }
librc_hidden_def(rc_service_started_daemon) librc_hidden_def(rc_service_started_daemon)
bool rc_service_daemons_crashed(const char *service) bool
rc_service_daemons_crashed(const char *service)
{ {
char dirpath[PATH_MAX]; char dirpath[PATH_MAX];
DIR *dp; DIR *dp;

View File

@ -1,7 +1,7 @@
/* /*
rc-misc.c rc-misc.c
rc misc functions rc misc functions
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -31,7 +31,8 @@
#include "librc.h" #include "librc.h"
bool rc_yesno(const char *value) bool
rc_yesno(const char *value)
{ {
if (!value) { if (!value) {
errno = ENOENT; errno = ENOENT;
@ -54,7 +55,8 @@ bool rc_yesno(const char *value)
} }
librc_hidden_def(rc_yesno) librc_hidden_def(rc_yesno)
ssize_t rc_getline(char **line, size_t *len, FILE *fp) ssize_t
rc_getline(char **line, size_t *len, FILE *fp)
{ {
char *p; char *p;
size_t last = 0; size_t last = 0;
@ -78,7 +80,8 @@ ssize_t rc_getline(char **line, size_t *len, FILE *fp)
} }
librc_hidden_def(rc_getline) librc_hidden_def(rc_getline)
RC_STRINGLIST *rc_config_list(const char *file) RC_STRINGLIST *
rc_config_list(const char *file)
{ {
FILE *fp; FILE *fp;
char *buffer = NULL; char *buffer = NULL;
@ -116,7 +119,8 @@ RC_STRINGLIST *rc_config_list(const char *file)
} }
librc_hidden_def(rc_config_list) librc_hidden_def(rc_config_list)
RC_STRINGLIST *rc_config_load(const char *file) RC_STRINGLIST *
rc_config_load(const char *file)
{ {
RC_STRINGLIST *list; RC_STRINGLIST *list;
RC_STRINGLIST *config; RC_STRINGLIST *config;
@ -194,7 +198,8 @@ RC_STRINGLIST *rc_config_load(const char *file)
} }
librc_hidden_def(rc_config_load) librc_hidden_def(rc_config_load)
char *rc_config_value(RC_STRINGLIST *list, const char *entry) char *
rc_config_value(RC_STRINGLIST *list, const char *entry)
{ {
RC_STRING *line; RC_STRING *line;
char *p; char *p;

View File

@ -1,7 +1,7 @@
/* /*
librc-strlist.h librc-strlist.h
String list functions to make using queue(3) a little easier. String list functions to make using queue(3) a little easier.
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -31,7 +31,8 @@
#include "librc.h" #include "librc.h"
RC_STRINGLIST *rc_stringlist_new(void) RC_STRINGLIST *
rc_stringlist_new(void)
{ {
RC_STRINGLIST *l = xmalloc(sizeof(*l)); RC_STRINGLIST *l = xmalloc(sizeof(*l));
TAILQ_INIT(l); TAILQ_INIT(l);
@ -39,7 +40,8 @@ RC_STRINGLIST *rc_stringlist_new(void)
} }
librc_hidden_def(rc_stringlist_new) librc_hidden_def(rc_stringlist_new)
RC_STRING *rc_stringlist_add (RC_STRINGLIST *list, const char *value) RC_STRING *
rc_stringlist_add (RC_STRINGLIST *list, const char *value)
{ {
RC_STRING *s = xmalloc(sizeof(*s)); RC_STRING *s = xmalloc(sizeof(*s));
@ -49,7 +51,8 @@ RC_STRING *rc_stringlist_add (RC_STRINGLIST *list, const char *value)
} }
librc_hidden_def(rc_stringlist_add) librc_hidden_def(rc_stringlist_add)
RC_STRING *rc_stringlist_addu (RC_STRINGLIST *list, const char *value) RC_STRING *
rc_stringlist_addu (RC_STRINGLIST *list, const char *value)
{ {
RC_STRING *s; RC_STRING *s;
@ -63,7 +66,8 @@ RC_STRING *rc_stringlist_addu (RC_STRINGLIST *list, const char *value)
} }
librc_hidden_def(rc_stringlist_addu) librc_hidden_def(rc_stringlist_addu)
bool rc_stringlist_delete(RC_STRINGLIST *list, const char *value) bool
rc_stringlist_delete(RC_STRINGLIST *list, const char *value)
{ {
RC_STRING *s; RC_STRING *s;
@ -80,7 +84,8 @@ bool rc_stringlist_delete(RC_STRINGLIST *list, const char *value)
} }
librc_hidden_def(rc_stringlist_delete) librc_hidden_def(rc_stringlist_delete)
RC_STRING *rc_stringlist_find(RC_STRINGLIST *list, const char *value) RC_STRING *
rc_stringlist_find(RC_STRINGLIST *list, const char *value)
{ {
RC_STRING *s; RC_STRING *s;
@ -93,7 +98,8 @@ RC_STRING *rc_stringlist_find(RC_STRINGLIST *list, const char *value)
} }
librc_hidden_def(rc_stringlist_find) librc_hidden_def(rc_stringlist_find)
RC_STRINGLIST *rc_stringlist_split(const char *value, const char *sep) RC_STRINGLIST *
rc_stringlist_split(const char *value, const char *sep)
{ {
RC_STRINGLIST *list = rc_stringlist_new(); RC_STRINGLIST *list = rc_stringlist_new();
char *d = xstrdup(value); char *d = xstrdup(value);
@ -107,7 +113,8 @@ RC_STRINGLIST *rc_stringlist_split(const char *value, const char *sep)
} }
librc_hidden_def(rc_stringlist_split) librc_hidden_def(rc_stringlist_split)
void rc_stringlist_sort(RC_STRINGLIST **list) void
rc_stringlist_sort(RC_STRINGLIST **list)
{ {
RC_STRINGLIST *l = *list; RC_STRINGLIST *l = *list;
RC_STRINGLIST *new = rc_stringlist_new(); RC_STRINGLIST *new = rc_stringlist_new();
@ -119,7 +126,7 @@ void rc_stringlist_sort(RC_STRINGLIST **list)
TAILQ_FOREACH_SAFE(s, l, entries, sn) { TAILQ_FOREACH_SAFE(s, l, entries, sn) {
TAILQ_REMOVE(l, s, entries); TAILQ_REMOVE(l, s, entries);
last = NULL; last = NULL;
TAILQ_FOREACH (n, new, entries) { TAILQ_FOREACH(n, new, entries) {
if (strcmp (s->value, n->value) < 0) if (strcmp (s->value, n->value) < 0)
break; break;
last = n; last = n;
@ -136,7 +143,8 @@ void rc_stringlist_sort(RC_STRINGLIST **list)
} }
librc_hidden_def(rc_stringlist_sort) librc_hidden_def(rc_stringlist_sort)
void rc_stringlist_free(RC_STRINGLIST *list) void
rc_stringlist_free(RC_STRINGLIST *list)
{ {
RC_STRING *s1; RC_STRING *s1;
RC_STRING *s2; RC_STRING *s2;

View File

@ -1,7 +1,7 @@
/* /*
librc librc
core RC functions core RC functions
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -33,7 +33,7 @@ const char librc_copyright[] = "Copyright (c) 2007-2008 Roy Marples";
#include "librc.h" #include "librc.h"
#ifdef __FreeBSD__ #ifdef __FreeBSD__
#include <sys/sysctl.h> # include <sys/sysctl.h>
#endif #endif
#define RC_RUNLEVEL RC_SVCDIR "/softlevel" #define RC_RUNLEVEL RC_SVCDIR "/softlevel"

View File

@ -33,7 +33,8 @@
# define _noreturn # define _noreturn
#endif #endif
_noreturn static void usage(int exit_status) _noreturn static void
usage(int exit_status)
{ {
const char * const has_arg[] = { "", "<arg>", "[arg]" }; const char * const has_arg[] = { "", "<arg>", "[arg]" };
int i; int i;

View File

@ -2,7 +2,7 @@
checkpath.c checkpath.c
Checks for the existance of a file or directory and creates it Checks for the existance of a file or directory and creates it
if necessary. It can also correct its ownership. if necessary. It can also correct its ownership.
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -49,7 +49,8 @@
extern const char *applet; extern const char *applet;
static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, int file) static int
do_check(char *path, uid_t uid, gid_t gid, mode_t mode, int file)
{ {
struct stat st; struct stat st;
int fd; int fd;
@ -57,8 +58,9 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, int file)
if (stat(path, &st)) { if (stat(path, &st)) {
if (file) { if (file) {
einfo("%s: creating file", path); einfo("%s: creating file", path);
if (! mode) if (!mode)
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; mode = S_IRUSR | S_IWUSR | S_IRGRP |
S_IWGRP | S_IROTH;
if ((fd = open(path, O_CREAT, mode)) == -1) { if ((fd = open(path, O_CREAT, mode)) == -1) {
eerror("%s: open: %s", applet, strerror(errno)); eerror("%s: open: %s", applet, strerror(errno));
return -1; return -1;
@ -66,17 +68,18 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, int file)
close (fd); close (fd);
} else { } else {
einfo("%s: creating directory", path); einfo("%s: creating directory", path);
if (! mode) if (!mode)
mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
if (mkdir(path, mode)) { if (mkdir(path, mode)) {
eerror("%s: mkdir: %s", applet, strerror (errno)); eerror("%s: mkdir: %s", applet,
strerror (errno));
return -1; return -1;
} }
mode = 0; mode = 0;
} }
} else { } else {
if ((file && S_ISDIR(st.st_mode)) || if ((file && S_ISDIR(st.st_mode)) ||
(! file && ! S_ISDIR(st.st_mode))) (!file && !S_ISDIR(st.st_mode)))
{ {
if (file) if (file)
eerror("%s: is a directory", path); eerror("%s: is a directory", path);
@ -106,8 +109,8 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, int file)
return 0; return 0;
} }
static int parse_owner(struct passwd **user, struct group **group, static int
const char *owner) parse_owner(struct passwd **user, struct group **group, const char *owner)
{ {
char *u = xstrdup (owner); char *u = xstrdup (owner);
char *g = strchr (u, ':'); char *g = strchr (u, ':');
@ -122,7 +125,7 @@ static int parse_owner(struct passwd **user, struct group **group,
*user = getpwuid((uid_t) id); *user = getpwuid((uid_t) id);
else else
*user = getpwnam(u); *user = getpwnam(u);
if (! *user) if (*user == NULL)
retval = -1; retval = -1;
} }
@ -131,7 +134,7 @@ static int parse_owner(struct passwd **user, struct group **group,
*group = getgrgid((gid_t) id); *group = getgrgid((gid_t) id);
else else
*group = getgrnam(g); *group = getgrnam(g);
if (! *group) if (*group == NULL)
retval = -1; retval = -1;
} }
@ -158,7 +161,8 @@ static const char * const longopts_help[] = {
}; };
#include "_usage.c" #include "_usage.c"
int checkpath(int argc, char **argv) int
checkpath(int argc, char **argv)
{ {
int opt; int opt;
uid_t uid = geteuid(); uid_t uid = geteuid();
@ -181,14 +185,16 @@ int checkpath(int argc, char **argv)
break; break;
case 'm': case 'm':
if (parse_mode(&mode, optarg) != 0) if (parse_mode(&mode, optarg) != 0)
eerrorx("%s: invalid mode `%s'", applet, optarg); eerrorx("%s: invalid mode `%s'",
applet, optarg);
break; break;
case 'o': case 'o':
if (parse_owner(&pw, &gr, optarg) != 0) if (parse_owner(&pw, &gr, optarg) != 0)
eerrorx("%s: owner `%s' not found", applet, optarg); eerrorx("%s: owner `%s' not found",
applet, optarg);
break; break;
case_RC_COMMON_GETOPT case_RC_COMMON_GETOPT;
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
fstabinfo.c fstabinfo.c
Gets information about /etc/fstab. Gets information about /etc/fstab.
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -42,31 +42,31 @@
Okay, we could use getfsent but the man page says use getmntent instead Okay, we could use getfsent but the man page says use getmntent instead
AND we don't have getfsent on uclibc or dietlibc for some odd reason. */ AND we don't have getfsent on uclibc or dietlibc for some odd reason. */
#ifdef __linux__ #ifdef __linux__
#define HAVE_GETMNTENT # define HAVE_GETMNTENT
#include <mntent.h> # include <mntent.h>
#define ENT mntent # define ENT mntent
#define START_ENT fp = setmntent ("/etc/fstab", "r"); # define START_ENT fp = setmntent ("/etc/fstab", "r");
#define GET_ENT getmntent (fp) # define GET_ENT getmntent (fp)
#define GET_ENT_FILE(_name) getmntfile (_name) # define GET_ENT_FILE(_name) getmntfile (_name)
#define END_ENT endmntent (fp) # define END_ENT endmntent (fp)
#define ENT_BLOCKDEVICE(_ent) ent->mnt_fsname # define ENT_BLOCKDEVICE(_ent) ent->mnt_fsname
#define ENT_FILE(_ent) ent->mnt_dir # define ENT_FILE(_ent) ent->mnt_dir
#define ENT_TYPE(_ent) ent->mnt_type # define ENT_TYPE(_ent) ent->mnt_type
#define ENT_OPTS(_ent) ent->mnt_opts # define ENT_OPTS(_ent) ent->mnt_opts
#define ENT_PASS(_ent) ent->mnt_passno # define ENT_PASS(_ent) ent->mnt_passno
#else #else
#define HAVE_GETFSENT # define HAVE_GETFSENT
#include <fstab.h> # include <fstab.h>
#define ENT fstab # define ENT fstab
#define START_ENT # define START_ENT
#define GET_ENT getfsent () # define GET_ENT getfsent ()
#define GET_ENT_FILE(_name) getfsfile (_name) # define GET_ENT_FILE(_name) getfsfile (_name)
#define END_ENT endfsent () # define END_ENT endfsent ()
#define ENT_BLOCKDEVICE(_ent) ent->fs_spec # define ENT_BLOCKDEVICE(_ent) ent->fs_spec
#define ENT_TYPE(_ent) ent->fs_vfstype # define ENT_TYPE(_ent) ent->fs_vfstype
#define ENT_FILE(_ent) ent->fs_file # define ENT_FILE(_ent) ent->fs_file
#define ENT_OPTS(_ent) ent->fs_mntops # define ENT_OPTS(_ent) ent->fs_mntops
#define ENT_PASS(_ent) ent->fs_passno # define ENT_PASS(_ent) ent->fs_passno
#endif #endif
#include "builtins.h" #include "builtins.h"
@ -93,7 +93,8 @@ getmntfile(const char *file)
extern const char *applet; extern const char *applet;
static int do_mount(struct ENT *ent) static int
do_mount(struct ENT *ent)
{ {
char *argv[8]; char *argv[8];
pid_t pid; pid_t pid;
@ -230,7 +231,8 @@ fstabinfo(int argc, char **argv)
START_ENT; START_ENT;
while ((ent = GET_ENT)) while ((ent = GET_ENT))
if (strcmp(token, ENT_TYPE(ent)) == 0) if (strcmp(token, ENT_TYPE(ent)) == 0)
rc_stringlist_add(files, ENT_FILE(ent)); rc_stringlist_add(files,
ENT_FILE(ent));
END_ENT; END_ENT;
} }
break; break;
@ -246,7 +248,8 @@ fstabinfo(int argc, char **argv)
if (strcmp(argv[i], file->value) == 0) if (strcmp(argv[i], file->value) == 0)
break; break;
if (i >= argc) if (i >= argc)
rc_stringlist_delete(files, file->value); rc_stringlist_delete(files,
file->value);
} }
} else { } else {
while (optind < argc) while (optind < argc)

View File

@ -1,7 +1,7 @@
/* /*
mountinfo.c mountinfo.c
Obtains information about mounted filesystems. Obtains information about mounted filesystems.
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -41,7 +41,7 @@
# define statfs statvfs # define statfs statvfs
# define F_FLAGS f_flag # define F_FLAGS f_flag
#elif defined (__linux__) #elif defined (__linux__)
#include <mntent.h> # include <mntent.h>
#endif #endif
#include <errno.h> #include <errno.h>
@ -84,7 +84,8 @@ struct args {
net_opts netdev; net_opts netdev;
}; };
static int process_mount(RC_STRINGLIST *list, struct args *args, static int
process_mount(RC_STRINGLIST *list, struct args *args,
char *from, char *to, char *fstype, char *options, char *from, char *to, char *fstype, char *options,
int netdev) int netdev)
{ {
@ -211,7 +212,8 @@ static struct opt {
{ 0, NULL } { 0, NULL }
}; };
static RC_STRINGLIST *find_mounts(struct args *args) static RC_STRINGLIST *
find_mounts(struct args *args)
{ {
struct statfs *mnts; struct statfs *mnts;
int nmnts; int nmnts;
@ -238,9 +240,11 @@ static RC_STRINGLIST *find_mounts(struct args *args)
if (! options) if (! options)
options = xstrdup(o->o_name); options = xstrdup(o->o_name);
else { else {
l = strlen(options) + strlen(o->o_name) + 2; l = strlen(options) +
strlen(o->o_name) + 2;
tmp = xmalloc(sizeof (char) * l); tmp = xmalloc(sizeof (char) * l);
snprintf(tmp, l, "%s,%s", options, o->o_name); snprintf(tmp, l, "%s,%s", options,
o->o_name);
free(options); free(options);
options = tmp; options = tmp;
} }
@ -263,7 +267,8 @@ static RC_STRINGLIST *find_mounts(struct args *args)
} }
#elif defined (__linux__) #elif defined (__linux__)
static struct mntent *getmntfile(const char *file) static struct mntent *
getmntfile(const char *file)
{ {
struct mntent *ent = NULL; struct mntent *ent = NULL;
FILE *fp; FILE *fp;
@ -277,7 +282,8 @@ static struct mntent *getmntfile(const char *file)
return ent; return ent;
} }
static RC_STRINGLIST *find_mounts(struct args *args) static RC_STRINGLIST *
find_mounts(struct args *args)
{ {
FILE *fp; FILE *fp;
char *buffer; char *buffer;
@ -321,7 +327,8 @@ static RC_STRINGLIST *find_mounts(struct args *args)
# error "Operating system not supported!" # error "Operating system not supported!"
#endif #endif
static regex_t *get_regex(const char *string) static regex_t *
get_regex(const char *string)
{ {
regex_t *reg = xmalloc(sizeof (*reg)); regex_t *reg = xmalloc(sizeof (*reg));
int result; int result;
@ -373,7 +380,8 @@ static const char * const longopts_help[] = {
}; };
#include "_usage.c" #include "_usage.c"
int mountinfo(int argc, char **argv) int
mountinfo(int argc, char **argv)
{ {
struct args args; struct args args;
regex_t *point_regex = NULL; regex_t *point_regex = NULL;
@ -448,7 +456,8 @@ int mountinfo(int argc, char **argv)
while (optind < argc) { while (optind < argc) {
if (argv[optind][0] != '/') if (argv[optind][0] != '/')
eerrorx("%s: `%s' is not a mount point", argv[0], argv[optind]); eerrorx("%s: `%s' is not a mount point",
argv[0], argv[optind]);
rc_stringlist_add(args.mounts, argv[optind++]); rc_stringlist_add(args.mounts, argv[optind++]);
} }
nodes = find_mounts(&args); nodes = find_mounts(&args);

View File

@ -4,7 +4,7 @@
Handle multicall applets for use in our init scripts. Handle multicall applets for use in our init scripts.
Basically this makes us a lot faster for the most part, and removes Basically this makes us a lot faster for the most part, and removes
any shell incompatabilities we might otherwise encounter. any shell incompatabilities we might otherwise encounter.
*/ */
/* /*
* Copyright 2007-2009 Roy Marples <roy@marples.name> * Copyright 2007-2009 Roy Marples <roy@marples.name>
@ -60,7 +60,8 @@
/* Applet is first parsed in rc.c - no point in doing it again */ /* Applet is first parsed in rc.c - no point in doing it again */
extern const char *applet; extern const char *applet;
static int syslog_decode(char *name, CODE *codetab) static int
syslog_decode(char *name, CODE *codetab)
{ {
CODE *c; CODE *c;
@ -74,7 +75,8 @@ static int syslog_decode(char *name, CODE *codetab)
return -1; return -1;
} }
static int do_e(int argc, char **argv) static int
do_e(int argc, char **argv)
{ {
int retval = EXIT_SUCCESS; int retval = EXIT_SUCCESS;
int i; int i;
@ -122,7 +124,8 @@ static int do_e(int argc, char **argv)
} else if (strcmp(applet, "esyslog") == 0 || } else if (strcmp(applet, "esyslog") == 0 ||
strcmp(applet, "elog") == 0) { strcmp(applet, "elog") == 0) {
p = strchr(argv[0], '.'); p = strchr(argv[0], '.');
if (!p || (level = syslog_decode(p + 1, prioritynames)) == -1) if (!p ||
(level = syslog_decode(p + 1, prioritynames)) == -1)
eerrorx("%s: invalid log level `%s'", applet, argv[0]); eerrorx("%s: invalid log level `%s'", applet, argv[0]);
if (argc < 3) if (argc < 3)
@ -251,7 +254,8 @@ static int do_e(int argc, char **argv)
return retval; return retval;
} }
static int do_service(int argc, char **argv) static int
do_service(int argc, char **argv)
{ {
bool ok = false; bool ok = false;
char *service; char *service;
@ -263,7 +267,7 @@ static int do_service(int argc, char **argv)
else else
service = getenv("RC_SVCNAME"); service = getenv("RC_SVCNAME");
if (! service || *service == '\0') if (service == NULL || *service == '\0')
eerrorx("%s: no service specified", applet); eerrorx("%s: no service specified", applet);
if (strcmp(applet, "service_started") == 0) if (strcmp(applet, "service_started") == 0)
@ -305,7 +309,8 @@ static int do_service(int argc, char **argv)
return ok ? EXIT_SUCCESS : EXIT_FAILURE; return ok ? EXIT_SUCCESS : EXIT_FAILURE;
} }
static int do_mark_service(int argc, char **argv) static int
do_mark_service(int argc, char **argv)
{ {
bool ok = false; bool ok = false;
char *svcname = getenv("RC_SVCNAME"); char *svcname = getenv("RC_SVCNAME");
@ -320,7 +325,7 @@ static int do_mark_service(int argc, char **argv)
else else
service = getenv("RC_SVCNAME"); service = getenv("RC_SVCNAME");
if (! service || *service == '\0') if (service == NULL || *service == '\0')
eerrorx("%s: no service specified", applet); eerrorx("%s: no service specified", applet);
if (strcmp(applet, "mark_service_started") == 0) if (strcmp(applet, "mark_service_started") == 0)
@ -340,8 +345,8 @@ static int do_mark_service(int argc, char **argv)
else else
eerrorx("%s: unknown applet", applet); eerrorx("%s: unknown applet", applet);
/* If we're marking ourselves then we need to inform our parent runscript /* If we're marking ourselves then we need to inform our parent
process so they do not mark us based on our exit code */ runscript process so they do not mark us based on our exit code */
if (ok && svcname && strcmp(svcname, service) == 0) { if (ok && svcname && strcmp(svcname, service) == 0) {
runscript_pid = getenv("RC_RUNSCRIPT_PID"); runscript_pid = getenv("RC_RUNSCRIPT_PID");
if (runscript_pid && sscanf(runscript_pid, "%d", &pid) == 1) if (runscript_pid && sscanf(runscript_pid, "%d", &pid) == 1)
@ -364,13 +369,14 @@ static int do_mark_service(int argc, char **argv)
return ok ? EXIT_SUCCESS : EXIT_FAILURE; return ok ? EXIT_SUCCESS : EXIT_FAILURE;
} }
static int do_value(int argc, char **argv) static int
do_value(int argc, char **argv)
{ {
bool ok = false; bool ok = false;
char *service = getenv("RC_SVCNAME"); char *service = getenv("RC_SVCNAME");
char *option; char *option;
if (! service) if (service == NULL)
eerrorx("%s: no service specified", applet); eerrorx("%s: no service specified", applet);
if (argc < 2 || ! argv[1] || *argv[1] == '\0') if (argc < 2 || ! argv[1] || *argv[1] == '\0')
@ -394,7 +400,8 @@ static int do_value(int argc, char **argv)
return ok ? EXIT_SUCCESS : EXIT_FAILURE; return ok ? EXIT_SUCCESS : EXIT_FAILURE;
} }
static int do_shell_var(int argc, char **argv) static int
do_shell_var(int argc, char **argv)
{ {
int i; int i;
char *p; char *p;
@ -402,10 +409,8 @@ static int do_shell_var(int argc, char **argv)
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
p = argv[i]; p = argv[i];
if (i != 1) if (i != 1)
putchar(' '); putchar(' ');
while (*p) { while (*p) {
c = (unsigned char)*p++; c = (unsigned char)*p++;
if (! isalnum(c)) if (! isalnum(c))
@ -414,11 +419,11 @@ static int do_shell_var(int argc, char **argv)
} }
} }
putchar('\n'); putchar('\n');
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void run_applets(int argc, char **argv) void
run_applets(int argc, char **argv)
{ {
int i = 2; int i = 2;
char *p; char *p;

View File

@ -1,7 +1,7 @@
/* /*
rc-depend rc-depend
rc service dependency and ordering rc service dependency and ordering
*/ */
/* /*
* Copyright 2007-2009 Roy Marples <roy@marples.name> * Copyright 2007-2009 Roy Marples <roy@marples.name>

View File

@ -2,7 +2,7 @@
rc-logger.c rc-logger.c
Spawns a logging daemon to capture stdout and stderr so we can log Spawns a logging daemon to capture stdout and stderr so we can log
them to a buffer and/or files. them to a buffer and/or files.
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -78,11 +78,12 @@ pid_t rc_logger_pid = -1;
int rc_logger_tty = -1; int rc_logger_tty = -1;
bool rc_in_logger = false; bool rc_in_logger = false;
static void write_log(int logfd, const char *buffer, size_t bytes) static void
write_log(int logfd, const char *buffer, size_t bytes)
{ {
const char *p = buffer; const char *p = buffer;
while ((size_t) (p - buffer) < bytes) { while ((size_t)(p - buffer) < bytes) {
switch (*p) { switch (*p) {
case '\r': case '\r':
goto cont; goto cont;
@ -110,7 +111,9 @@ cont:
p++; p++;
} }
} }
static void write_time(FILE *f, const char *s)
static void
write_time(FILE *f, const char *s)
{ {
time_t now = time(NULL); time_t now = time(NULL);
struct tm *tm = localtime(&now); struct tm *tm = localtime(&now);
@ -119,7 +122,8 @@ static void write_time(FILE *f, const char *s)
fflush(f); fflush(f);
} }
void rc_logger_close(void) void
rc_logger_close(void)
{ {
int sig = SIGTERM; int sig = SIGTERM;
@ -138,7 +142,8 @@ void rc_logger_close(void)
dup2(fd_stderr, STDERR_FILENO); dup2(fd_stderr, STDERR_FILENO);
} }
void rc_logger_open(const char *level) void
rc_logger_open(const char *level)
{ {
int slave_tty; int slave_tty;
struct termios tt; struct termios tt;
@ -202,12 +207,14 @@ void rc_logger_open(const char *level)
if (rc_logger_tty >= 0) if (rc_logger_tty >= 0)
fd[1].fd = rc_logger_tty; fd[1].fd = rc_logger_tty;
for (;;) { for (;;) {
if ((s = poll(fd, rc_logger_tty >= 0 ? 2 : 1, -1)) == -1) { if ((s = poll(fd,
rc_logger_tty >= 0 ? 2 : 1, -1)) == -1)
{
eerror("poll: %s", strerror(errno)); eerror("poll: %s", strerror(errno));
break; break;
} } else if (s == 0)
continue;
if (s > 0) {
if (fd[1].revents & (POLLIN | POLLHUP)) { if (fd[1].revents & (POLLIN | POLLHUP)) {
memset(buffer, 0, BUFSIZ); memset(buffer, 0, BUFSIZ);
bytes = read(rc_logger_tty, buffer, BUFSIZ); bytes = read(rc_logger_tty, buffer, BUFSIZ);
@ -223,7 +230,8 @@ void rc_logger_open(const char *level)
logbuf_size); logbuf_size);
} }
memcpy(logbuf + logbuf_len, buffer, bytes); memcpy(logbuf + logbuf_len,
buffer, bytes);
logbuf_len += bytes; logbuf_len += bytes;
} }
} }
@ -232,7 +240,6 @@ void rc_logger_open(const char *level)
if (fd[0].revents & (POLLIN | POLLHUP)) if (fd[0].revents & (POLLIN | POLLHUP))
break; break;
} }
}
free(buffer); free(buffer);
if (logbuf) { if (logbuf) {
if ((log = fopen(LOGFILE, "a"))) { if ((log = fopen(LOGFILE, "a"))) {
@ -246,10 +253,9 @@ void rc_logger_open(const char *level)
fclose(log); fclose(log);
} }
/* Try and cat our new logfile to a more permament location and then /* Try and cat our new logfile to a more permament location
* punt it */ and then punt it */
system(MOVELOG); system(MOVELOG);
exit(0); exit(0);
/* NOTREACHED */ /* NOTREACHED */

View File

@ -1,7 +1,7 @@
/* /*
librc-misc.c librc-misc.c
rc misc functions rc misc functions
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -34,8 +34,8 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#ifdef __linux__ #ifdef __linux__
#include <sys/sysinfo.h> # include <sys/sysinfo.h>
#include <regex.h> # include <regex.h>
#endif #endif
#include <ctype.h> #include <ctype.h>
@ -244,7 +244,8 @@ env_config(void)
if (sys) if (sys)
setenv("RC_SYS", sys, 1); setenv("RC_SYS", sys, 1);
/* Some scripts may need to take a different code path if Linux/FreeBSD, etc /* Some scripts may need to take a different code path if
Linux/FreeBSD, etc
To save on calling uname, we store it in an environment variable */ To save on calling uname, we store it in an environment variable */
if (uname(&uts) == 0) if (uname(&uts) == 0)
setenv("RC_UNAME", uts.sysname, 1); setenv("RC_UNAME", uts.sysname, 1);

View File

@ -1,7 +1,7 @@
/* /*
librc-plugin.c librc-plugin.c
Simple plugin handler Simple plugin handler
*/ */
/* /*
* Copyright 2007-2008 Roy Marples <roy@marples.name> * Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -62,7 +62,8 @@ typedef struct plugin
TAILQ_HEAD(, plugin) plugins; TAILQ_HEAD(, plugin) plugins;
#ifndef __FreeBSD__ #ifndef __FreeBSD__
dlfunc_t dlfunc(void * __restrict handle, const char * __restrict symbol) dlfunc_t
dlfunc(void * __restrict handle, const char * __restrict symbol)
{ {
union { union {
void *d; void *d;
@ -74,7 +75,8 @@ dlfunc_t dlfunc(void * __restrict handle, const char * __restrict symbol)
} }
#endif #endif
void rc_plugin_load(void) void
rc_plugin_load(void)
{ {
DIR *dp; DIR *dp;
struct dirent *d; struct dirent *d;
@ -89,7 +91,7 @@ void rc_plugin_load(void)
TAILQ_INIT(&plugins); TAILQ_INIT(&plugins);
if (! (dp = opendir(RC_PLUGINDIR))) if (!(dp = opendir(RC_PLUGINDIR)))
return; return;
while ((d = readdir(dp))) { while ((d = readdir(dp))) {
@ -98,14 +100,16 @@ void rc_plugin_load(void)
snprintf(file, sizeof(file), RC_PLUGINDIR "/%s", d->d_name); snprintf(file, sizeof(file), RC_PLUGINDIR "/%s", d->d_name);
h = dlopen(file, RTLD_LAZY); h = dlopen(file, RTLD_LAZY);
if (! h) { if (h == NULL) {
eerror("dlopen: %s", dlerror()); eerror("dlopen: %s", dlerror());
continue; continue;
} }
fptr = (int (*)(RC_HOOK, const char*))dlfunc(h, RC_PLUGIN_HOOK); fptr = (int (*)(RC_HOOK, const char*))
if (! fptr) { dlfunc(h, RC_PLUGIN_HOOK);
eerror("%s: cannot find symbol `%s'", d->d_name, RC_PLUGIN_HOOK); if (fptr == NULL) {
eerror("%s: cannot find symbol `%s'",
d->d_name, RC_PLUGIN_HOOK);
dlclose(h); dlclose(h);
} else { } else {
plugin = xmalloc(sizeof(*plugin)); plugin = xmalloc(sizeof(*plugin));
@ -118,7 +122,8 @@ void rc_plugin_load(void)
closedir(dp); closedir(dp);
} }
int rc_waitpid(pid_t pid) int
rc_waitpid(pid_t pid)
{ {
int status; int status;
@ -131,7 +136,8 @@ int rc_waitpid(pid_t pid)
return status; return status;
} }
void rc_plugin_run(RC_HOOK hook, const char *value) void
rc_plugin_run(RC_HOOK hook, const char *value)
{ {
PLUGIN *plugin; PLUGIN *plugin;
struct sigaction sa; struct sigaction sa;
@ -234,7 +240,8 @@ void rc_plugin_run(RC_HOOK hook, const char *value)
} }
} }
void rc_plugin_unload(void) void
rc_plugin_unload(void)
{ {
PLUGIN *plugin = TAILQ_FIRST(&plugins); PLUGIN *plugin = TAILQ_FIRST(&plugins);
PLUGIN *next; PLUGIN *next;

View File

@ -1,7 +1,7 @@
/* /*
rc-service.c rc-service.c
Finds all OpenRC services Finds all OpenRC services
*/ */
/* /*
* Copyright 2008 Roy Marples <roy@marples.name> * Copyright 2008 Roy Marples <roy@marples.name>

View File

@ -1,7 +1,7 @@
/* /*
rc-update rc-update
Manage init scripts and runlevels Manage init scripts and runlevels
*/ */
/* /*
* Copyright 2007-2009 Roy Marples <roy@marples.name> * Copyright 2007-2009 Roy Marples <roy@marples.name>
@ -57,9 +57,11 @@ add(const char *runlevel, const char *service)
if (!rc_service_exists(service)) { if (!rc_service_exists(service)) {
if (errno == ENOEXEC) if (errno == ENOEXEC)
eerror("%s: service `%s' is not executeable", applet, service); eerror("%s: service `%s' is not executeable",
applet, service);
else else
eerror("%s: service `%s' does not exist", applet, service); eerror("%s: service `%s' does not exist",
applet, service);
} else if (rc_service_in_runlevel(service, runlevel)) { } else if (rc_service_in_runlevel(service, runlevel)) {
ewarn ("%s: %s already installed in runlevel `%s'; skipping", ewarn ("%s: %s already installed in runlevel `%s'; skipping",
applet, service, runlevel); applet, service, runlevel);
@ -199,7 +201,8 @@ rc_update(int argc, char **argv)
if (action) if (action)
optind++; optind++;
else else
eerrorx("%s: invalid command `%s'", applet, argv[optind]); eerrorx("%s: invalid command `%s'",
applet, argv[optind]);
} }
if (!action) if (!action)
action = DOSHOW; action = DOSHOW;
@ -254,7 +257,7 @@ rc_update(int argc, char **argv)
if (!TAILQ_FIRST(runlevels)) { if (!TAILQ_FIRST(runlevels)) {
free(runlevels); free(runlevels);
eerrorx ("%s: no runlevels found", applet); eerrorx("%s: no runlevels found", applet);
} }
TAILQ_FOREACH(runlevel, runlevels, entries) { TAILQ_FOREACH(runlevel, runlevels, entries) {

View File

@ -217,8 +217,8 @@ read_key(bool block)
if (!isatty(fd)) if (!isatty(fd))
return false; return false;
/* Now save our terminal settings. We need to restore them at exit as we /* Now save our terminal settings. We need to restore them at exit as
* will be changing it for non-blocking reads for Interactive */ we will be changing it for non-blocking reads for Interactive */
if (!termios_orig) { if (!termios_orig) {
termios_orig = xmalloc(sizeof(*termios_orig)); termios_orig = xmalloc(sizeof(*termios_orig));
tcgetattr(fd, termios_orig); tcgetattr(fd, termios_orig);
@ -611,13 +611,14 @@ do_stop_services(const char *newlevel, bool parallel)
if (!svc1) { if (!svc1) {
tmplist = rc_stringlist_new(); tmplist = rc_stringlist_new();
rc_stringlist_add(tmplist, service->value); rc_stringlist_add(tmplist, service->value);
deporder = rc_deptree_depends(deptree, types_n, tmplist, deporder = rc_deptree_depends(deptree, types_n,
newlevel ? newlevel : runlevel, tmplist, newlevel ? newlevel : runlevel,
RC_DEP_STRICT | RC_DEP_TRACE); RC_DEP_STRICT | RC_DEP_TRACE);
rc_stringlist_free(tmplist); rc_stringlist_free(tmplist);
svc2 = NULL; svc2 = NULL;
TAILQ_FOREACH(svc1, deporder, entries) { TAILQ_FOREACH(svc1, deporder, entries) {
svc2 = rc_stringlist_find(start_services, svc1->value); svc2 = rc_stringlist_find(start_services,
svc1->value);
if (svc2) if (svc2)
break; break;
} }
@ -661,7 +662,7 @@ do_start_services(bool parallel)
interactive = want_interactive(); interactive = want_interactive();
if (interactive) { if (interactive) {
interactive_retry: interactive_retry:
printf("\n"); printf("\n");
einfo("About to start the service %s", einfo("About to start the service %s",
service->value); service->value);
@ -669,7 +670,7 @@ interactive_retry:
einfo("1) Start the service\t\t2) Skip the service"); einfo("1) Start the service\t\t2) Skip the service");
einfo("3) Continue boot process\t\t4) Exit to shell"); einfo("3) Continue boot process\t\t4) Exit to shell");
eoutdent(); eoutdent();
interactive_option: interactive_option:
switch (read_key(true)) { switch (read_key(true)) {
case '1': break; case '1': break;
case '2': continue; case '2': continue;
@ -737,7 +738,8 @@ static const struct option longopts[] = {
longopts_COMMON longopts_COMMON
}; };
static const char * const longopts_help[] = { static const char * const longopts_help[] = {
"override the next runlevel to change into\nwhen leaving single user or boot runlevels", "override the next runlevel to change into\n"
"when leaving single user or boot runlevels",
"runs the service specified with the rest\nof the arguments", "runs the service specified with the rest\nof the arguments",
"output the RC system type, if any", "output the RC system type, if any",
longopts_help_COMMON longopts_help_COMMON
@ -895,7 +897,7 @@ main(int argc, char **argv)
setenv("RC_RUNLEVEL", newlevel, 1); setenv("RC_RUNLEVEL", newlevel, 1);
setenv("RC_GOINGDOWN", "YES", 1); setenv("RC_GOINGDOWN", "YES", 1);
} else { } else {
/* We should not use krunevel in sysinit or the boot runlevel */ /* We should not use krunevel in sysinit or boot runlevels */
if (!newlevel || if (!newlevel ||
(strcmp(newlevel, RC_LEVEL_SYSINIT) != 0 && (strcmp(newlevel, RC_LEVEL_SYSINIT) != 0 &&
strcmp(newlevel, getenv("RC_BOOTLEVEL")) != 0)) strcmp(newlevel, getenv("RC_BOOTLEVEL")) != 0))

View File

@ -5,7 +5,7 @@
This is essentially a ground up re-write of Debians This is essentially a ground up re-write of Debians
start-stop-daemon for cleaner code and to integrate into our RC start-stop-daemon for cleaner code and to integrate into our RC
system so we can monitor daemons a little. system so we can monitor daemons a little.
*/ */
/* /*
* Copyright 2007-2009 Roy Marples <roy@marples.name> * Copyright 2007-2009 Roy Marples <roy@marples.name>
@ -103,7 +103,8 @@ static char *changeuser, *ch_root, *ch_dir;
extern char **environ; extern char **environ;
static void free_schedulelist(void) static void
free_schedulelist(void)
{ {
SCHEDULEITEM *s1 = TAILQ_FIRST(&schedule); SCHEDULEITEM *s1 = TAILQ_FIRST(&schedule);
SCHEDULEITEM *s2; SCHEDULEITEM *s2;
@ -117,7 +118,8 @@ static void free_schedulelist(void)
} }
#ifdef DEBUG_MEMORY #ifdef DEBUG_MEMORY
static void cleanup(void) static void
cleanup(void)
{ {
free(changeuser); free(changeuser);
free(nav); free(nav);
@ -125,7 +127,8 @@ static void cleanup(void)
} }
#endif #endif
static int parse_signal(const char *sig) static int
parse_signal(const char *sig)
{ {
typedef struct signalpair typedef struct signalpair
{ {
@ -181,7 +184,8 @@ static int parse_signal(const char *sig)
/* NOTREACHED */ /* NOTREACHED */
} }
static SCHEDULEITEM *parse_schedule_item(const char *string) static SCHEDULEITEM *
parse_schedule_item(const char *string)
{ {
const char *after_hyph; const char *after_hyph;
int sig; int sig;
@ -195,21 +199,21 @@ static SCHEDULEITEM *parse_schedule_item(const char *string)
item->type = SC_TIMEOUT; item->type = SC_TIMEOUT;
errno = 0; errno = 0;
if (sscanf(string, "%d", &item->value) != 1) if (sscanf(string, "%d", &item->value) != 1)
eerrorx("%s: invalid timeout value in schedule `%s'", applet, eerrorx("%s: invalid timeout value in schedule `%s'",
string); applet, string);
} else if ((after_hyph = string + (string[0] == '-')) && } else if ((after_hyph = string + (string[0] == '-')) &&
((sig = parse_signal(after_hyph)) != -1)) ((sig = parse_signal(after_hyph)) != -1))
{ {
item->type = SC_SIGNAL; item->type = SC_SIGNAL;
item->value = (int)sig; item->value = (int)sig;
} } else
else
eerrorx("%s: invalid schedule item `%s'", applet, string); eerrorx("%s: invalid schedule item `%s'", applet, string);
return item; return item;
} }
static void parse_schedule(const char *string, int timeout) static void
parse_schedule(const char *string, int timeout)
{ {
char buffer[20]; char buffer[20];
const char *slash; const char *slash;
@ -238,7 +242,8 @@ static void parse_schedule(const char *string, int timeout)
TAILQ_INSERT_TAIL(&schedule, item, entries); TAILQ_INSERT_TAIL(&schedule, item, entries);
if (string) { if (string) {
if (sscanf(string, "%d", &item->value) != 1) if (sscanf(string, "%d", &item->value) != 1)
eerrorx("%s: invalid timeout value in schedule", applet); eerrorx("%s: invalid timeout in schedule",
applet);
} else } else
item->value = 5; item->value = 5;
@ -251,8 +256,9 @@ static void parse_schedule(const char *string, int timeout)
else else
len = strlen(string); len = strlen(string);
if (len >= (ptrdiff_t) sizeof(buffer)) if (len >= (ptrdiff_t)sizeof(buffer))
eerrorx("%s: invalid schedule item, far too long", applet); eerrorx("%s: invalid schedule item, far too long",
applet);
memcpy(buffer, string, len); memcpy(buffer, string, len);
buffer[len] = 0; buffer[len] = 0;
@ -281,7 +287,8 @@ static void parse_schedule(const char *string, int timeout)
return; return;
} }
static pid_t get_pid(const char *pidfile, bool quiet) static pid_t
get_pid(const char *pidfile, bool quiet)
{ {
FILE *fp; FILE *fp;
pid_t pid; pid_t pid;
@ -291,7 +298,8 @@ static pid_t get_pid(const char *pidfile, bool quiet)
if ((fp = fopen(pidfile, "r")) == NULL) { if ((fp = fopen(pidfile, "r")) == NULL) {
if (!quiet) if (!quiet)
eerror("%s: fopen `%s': %s", applet, pidfile, strerror(errno)); eerror("%s: fopen `%s': %s",
applet, pidfile, strerror(errno));
return -1; return -1;
} }
@ -308,7 +316,8 @@ static pid_t get_pid(const char *pidfile, bool quiet)
} }
/* return number of processed killed, -1 on error */ /* return number of processed killed, -1 on error */
static int do_stop(const char *exec, const char *const *argv, static int
do_stop(const char *exec, const char *const *argv,
pid_t pid, uid_t uid,int sig, pid_t pid, uid_t uid,int sig,
bool quiet, bool verbose, bool test) bool quiet, bool verbose, bool test)
{ {
@ -357,7 +366,8 @@ static int do_stop(const char *exec, const char *const *argv,
return nkilled; return nkilled;
} }
static int run_stop_schedule(const char *exec, const char *const *argv, static int
run_stop_schedule(const char *exec, const char *const *argv,
const char *pidfile, uid_t uid, const char *pidfile, uid_t uid,
bool quiet, bool verbose, bool test) bool quiet, bool verbose, bool test)
{ {
@ -434,7 +444,8 @@ static int run_stop_schedule(const char *exec, const char *const *argv,
if (nanosleep(&ts, NULL) == -1) { if (nanosleep(&ts, NULL) == -1) {
if (errno == EINTR) if (errno == EINTR)
eerror("%s: caught an interrupt", applet); eerror("%s: caught an"
" interrupt", applet);
else { else {
eerror("%s: nanosleep: %s", eerror("%s: nanosleep: %s",
applet, strerror(errno)); applet, strerror(errno));
@ -446,7 +457,8 @@ static int run_stop_schedule(const char *exec, const char *const *argv,
break; break;
default: default:
eerror("%s: invalid schedule item `%d'", applet, item->type); eerror("%s: invalid schedule item `%d'",
applet, item->type);
return 0; return 0;
} }
@ -459,15 +471,18 @@ static int run_stop_schedule(const char *exec, const char *const *argv,
if (! quiet) { if (! quiet) {
if (nrunning == 1) if (nrunning == 1)
eerror("%s: %d process refused to stop", applet, nrunning); eerror("%s: %d process refused to stop",
applet, nrunning);
else else
eerror("%s: %d process(es) refused to stop", applet, nrunning); eerror("%s: %d process(es) refused to stop",
applet, nrunning);
} }
return -nrunning; return -nrunning;
} }
static void handle_signal(int sig) static void
handle_signal(int sig)
{ {
int status; int status;
int serrno = errno; int serrno = errno;
@ -492,7 +507,8 @@ static void handle_signal(int sig)
for (;;) { for (;;) {
if (waitpid(-1, &status, WNOHANG) < 0) { if (waitpid(-1, &status, WNOHANG) < 0) {
if (errno != ECHILD) if (errno != ECHILD)
eerror("%s: waitpid: %s", applet, strerror(errno)); eerror("%s: waitpid: %s",
applet, strerror(errno));
break; break;
} }
} }
@ -606,7 +622,8 @@ static const char * const longopts_help[] = {
}; };
#include "_usage.c" #include "_usage.c"
int start_stop_daemon(int argc, char **argv) int
start_stop_daemon(int argc, char **argv)
{ {
int devnull_fd = -1; int devnull_fd = -1;
#ifdef TIOCNOTTY #ifdef TIOCNOTTY
@ -1067,15 +1084,18 @@ int start_stop_daemon(int argc, char **argv)
if (nicelevel) { if (nicelevel) {
if (setpriority(PRIO_PROCESS, mypid, nicelevel) == -1) if (setpriority(PRIO_PROCESS, mypid, nicelevel) == -1)
eerrorx("%s: setpritory %d: %s", applet, nicelevel, eerrorx("%s: setpritory %d: %s",
applet, nicelevel,
strerror(errno)); strerror(errno));
} }
if (ch_root && chroot(ch_root) < 0) if (ch_root && chroot(ch_root) < 0)
eerrorx("%s: chroot `%s': %s", applet, ch_root, strerror(errno)); eerrorx("%s: chroot `%s': %s",
applet, ch_root, strerror(errno));
if (ch_dir && chdir(ch_dir) < 0) if (ch_dir && chdir(ch_dir) < 0)
eerrorx("%s: chdir `%s': %s", applet, ch_dir, strerror(errno)); eerrorx("%s: chdir `%s': %s",
applet, ch_dir, strerror(errno));
if (makepidfile && pidfile) { if (makepidfile && pidfile) {
fp = fopen(pidfile, "w"); fp = fopen(pidfile, "w");
@ -1088,9 +1108,11 @@ int start_stop_daemon(int argc, char **argv)
#ifdef HAVE_PAM #ifdef HAVE_PAM
if (changeuser != NULL) if (changeuser != NULL)
pamr = pam_start("start-stop-daemon", changeuser, &conv, &pamh); pamr = pam_start("start-stop-daemon",
changeuser, &conv, &pamh);
else else
pamr = pam_start("start-stop-daemon", "nobody", &conv, &pamh); pamr = pam_start("start-stop-daemon",
"nobody", &conv, &pamh);
if (pamr == PAM_SUCCESS) if (pamr == PAM_SUCCESS)
pamr = pam_authenticate(pamh, PAM_SILENT); pamr = pam_authenticate(pamh, PAM_SILENT);
@ -1099,15 +1121,19 @@ int start_stop_daemon(int argc, char **argv)
if (pamr == PAM_SUCCESS) if (pamr == PAM_SUCCESS)
pamr = pam_open_session(pamh, PAM_SILENT); pamr = pam_open_session(pamh, PAM_SILENT);
if (pamr != PAM_SUCCESS) if (pamr != PAM_SUCCESS)
eerrorx("%s: pam error: %s", applet, pam_strerror(pamh, pamr)); eerrorx("%s: pam error: %s",
applet, pam_strerror(pamh, pamr));
#endif #endif
if (gid && setgid(gid)) if (gid && setgid(gid))
eerrorx("%s: unable to set groupid to %d", applet, gid); eerrorx("%s: unable to set groupid to %d",
applet, gid);
if (changeuser && initgroups(changeuser, gid)) if (changeuser && initgroups(changeuser, gid))
eerrorx("%s: initgroups (%s, %d)", applet, changeuser, gid); eerrorx("%s: initgroups (%s, %d)",
applet, changeuser, gid);
if (uid && setuid(uid)) if (uid && setuid(uid))
eerrorx ("%s: unable to set userid to %d", applet, uid); eerrorx ("%s: unable to set userid to %d",
applet, uid);
/* Close any fd's to the passwd database */ /* Close any fd's to the passwd database */
endpwent(); endpwent();
@ -1179,15 +1205,19 @@ int start_stop_daemon(int argc, char **argv)
stdout_fd = devnull_fd; stdout_fd = devnull_fd;
stderr_fd = devnull_fd; stderr_fd = devnull_fd;
if (redirect_stdout) { if (redirect_stdout) {
if ((stdout_fd = open(redirect_stdout, O_WRONLY | O_CREAT | O_APPEND, if ((stdout_fd = open(redirect_stdout,
O_WRONLY | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR)) == -1) S_IRUSR | S_IWUSR)) == -1)
eerrorx("%s: unable to open the logfile for stdout `%s': %s", eerrorx("%s: unable to open the logfile"
" for stdout `%s': %s",
applet, redirect_stdout, strerror(errno)); applet, redirect_stdout, strerror(errno));
} }
if (redirect_stderr) { if (redirect_stderr) {
if ((stderr_fd = open(redirect_stderr, O_WRONLY | O_CREAT | O_APPEND, if ((stderr_fd = open(redirect_stderr,
O_WRONLY | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR)) == -1) S_IRUSR | S_IWUSR)) == -1)
eerrorx("%s: unable to open the logfile for stderr `%s': %s", eerrorx("%s: unable to open the logfile"
" for stderr `%s': %s",
applet, redirect_stderr, strerror(errno)); applet, redirect_stderr, strerror(errno));
} }
@ -1220,13 +1250,15 @@ int start_stop_daemon(int argc, char **argv)
do { do {
pid = waitpid(spid, &i, 0); pid = waitpid(spid, &i, 0);
if (pid < 1) { if (pid < 1) {
eerror("waitpid %d: %s", spid, strerror(errno)); eerror("waitpid %d: %s",
spid, strerror(errno));
return -1; return -1;
} }
} while (!WIFEXITED(i) && !WIFSIGNALED(i)); } while (!WIFEXITED(i) && !WIFSIGNALED(i));
if (!WIFEXITED(i) || WEXITSTATUS(i) != 0) { if (!WIFEXITED(i) || WEXITSTATUS(i) != 0) {
if (!quiet) if (!quiet)
eerrorx("%s: failed to start `%s'", applet, exec); eerrorx("%s: failed to start `%s'",
applet, exec);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
pid = spid; pid = spid;
@ -1281,8 +1313,8 @@ int start_stop_daemon(int argc, char **argv)
} }
if (svcname) if (svcname)
rc_service_daemon_set(svcname, exec, (const char *const *)margv, rc_service_daemon_set(svcname, exec,
pidfile, true); (const char *const *)margv, pidfile, true);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
/* NOTREACHED */ /* NOTREACHED */