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
Informational functions
*/
*/
/*
* Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -110,10 +110,10 @@ hidden_proto(ewendv)
#define UP "\033[A"
#define _GET_CAP(_d, _c) strlcpy(_d, tgoto(_c, 0, 0), sizeof(_d));
#define _ASSIGN_CAP(_v) { \
#define _ASSIGN_CAP(_v) do { \
_v = p; \
p += strlcpy(p, tmp, sizeof(ebuffer) - (p - ebuffer)) + 1; \
}
} while (0)
/* A pointer to a string to prefix to einfo/ewarn/eerror messages */
static const char *_eprefix = NULL;
@ -497,7 +497,7 @@ colour_terminal(FILE * EINFO_RESTRICT f)
_GET_CAP(tmp, _me);
if (tmp[0])
_ASSIGN_CAP(ecolors_str[i])
_ASSIGN_CAP(ecolors_str[i]);
else
ecolors_str[i] = &nullstr;
}
@ -627,10 +627,10 @@ ecolor(ECOLOR color)
#define LASTCMD(_cmd) { \
unsetenv("EINFO_LASTCMD"); \
setenv("EINFO_LASTCMD", _cmd, 1); \
}
}
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;
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)
_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;
FILE *fp = stdout;

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
/*
librc
core RC functions
*/
*/
/*
* 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"
#ifdef __FreeBSD__
#include <sys/sysctl.h>
# include <sys/sysctl.h>
#endif
#define RC_RUNLEVEL RC_SVCDIR "/softlevel"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
/*
librc-misc.c
rc misc functions
*/
*/
/*
* Copyright 2007-2008 Roy Marples <roy@marples.name>
@ -34,8 +34,8 @@
#include <sys/utsname.h>
#ifdef __linux__
#include <sys/sysinfo.h>
#include <regex.h>
# include <sys/sysinfo.h>
# include <regex.h>
#endif
#include <ctype.h>
@ -244,7 +244,8 @@ env_config(void)
if (sys)
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 */
if (uname(&uts) == 0)
setenv("RC_UNAME", uts.sysname, 1);

View File

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

View File

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

View File

@ -1,7 +1,7 @@
/*
rc-update
Manage init scripts and runlevels
*/
*/
/*
* 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 (errno == ENOEXEC)
eerror("%s: service `%s' is not executeable", applet, service);
eerror("%s: service `%s' is not executeable",
applet, service);
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)) {
ewarn ("%s: %s already installed in runlevel `%s'; skipping",
applet, service, runlevel);
@ -199,7 +201,8 @@ rc_update(int argc, char **argv)
if (action)
optind++;
else
eerrorx("%s: invalid command `%s'", applet, argv[optind]);
eerrorx("%s: invalid command `%s'",
applet, argv[optind]);
}
if (!action)
action = DOSHOW;
@ -254,7 +257,7 @@ rc_update(int argc, char **argv)
if (!TAILQ_FIRST(runlevels)) {
free(runlevels);
eerrorx ("%s: no runlevels found", applet);
eerrorx("%s: no runlevels found", applet);
}
TAILQ_FOREACH(runlevel, runlevels, entries) {

View File

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

View File

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