diff options
author | Roy Marples <roy@marples.name> | 2009-04-23 21:31:22 +0000 |
---|---|---|
committer | Roy Marples <roy@marples.name> | 2009-04-23 21:31:22 +0000 |
commit | ade85d4bd81d7257f5a7e2e4ed40b9cbbbe23651 (patch) | |
tree | 278e8ec69f53fac2bc51b5122e6980e625ffeeca /src/librc | |
parent | 69534746553adb52d3ede38a4cf8cd3630c97091 (diff) |
Improve style for KNF
Diffstat (limited to 'src/librc')
-rw-r--r-- | src/librc/librc-daemon.c | 88 | ||||
-rw-r--r-- | src/librc/librc-misc.c | 23 | ||||
-rw-r--r-- | src/librc/librc-stringlist.c | 56 | ||||
-rw-r--r-- | src/librc/librc.c | 74 |
4 files changed, 129 insertions, 112 deletions
diff --git a/src/librc/librc-daemon.c b/src/librc/librc-daemon.c index 3d09c99e..3c87faa2 100644 --- a/src/librc/librc-daemon.c +++ b/src/librc/librc-daemon.c @@ -1,7 +1,7 @@ /* - librc-daemon - Finds PID for given daemon criteria - */ + 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; @@ -101,15 +103,15 @@ RC_PIDLIST *rc_find_pids(const char *exec, const char *const *argv, return NULL; /* - 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) @@ -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]; @@ -187,7 +189,7 @@ RC_PIDLIST *rc_find_pids(const char *exec, const char *const *argv, int match; if ((kd = kvm_openfiles(_KVM_PATH, _KVM_PATH, - NULL, _KVM_FLAGS, errbuf)) == NULL) + NULL, _KVM_FLAGS, errbuf)) == NULL) { fprintf(stderr, "kvm_open: %s\n", errbuf); return NULL; @@ -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; @@ -266,10 +268,10 @@ static bool _match_daemon(const char *path, const char *file, while ((rc_getline(&line, &len, fp))) { TAILQ_FOREACH(m, match, entries) - if (strcmp(line, m->value) == 0) { - TAILQ_REMOVE(match, m, entries); - break; - } + if (strcmp(line, m->value) == 0) { + TAILQ_REMOVE(match, m, entries); + break; + } if (!TAILQ_FIRST(match)) break; } @@ -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,9 +317,10 @@ 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, - const char *const *argv, - const char *pidfile, bool started) +bool +rc_service_daemon_set(const char *service, const char *exec, + const char *const *argv, + const char *pidfile, bool started) { char dirpath[PATH_MAX]; char file[PATH_MAX]; @@ -336,7 +339,7 @@ bool rc_service_daemon_set(const char *service, const char *exec, } snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s", - basename_c(service)); + basename_c(service)); /* Regardless, erase any existing daemon info */ if ((dp = opendir(dirpath))) { @@ -346,7 +349,7 @@ bool rc_service_daemon_set(const char *service, const char *exec, continue; snprintf(file, sizeof(file), "%s/%s", - dirpath, d->d_name); + dirpath, d->d_name); nfiles++; if (!*oldfile) { @@ -368,7 +371,7 @@ bool rc_service_daemon_set(const char *service, const char *exec, if (started) { if (mkdir(dirpath, 0755) == 0 || errno == EEXIST) { snprintf(file, sizeof(file), "%s/%03d", - dirpath, nfiles + 1); + dirpath, nfiles + 1); if ((fp = fopen(file, "w"))) { fprintf(fp, "exec="); if (exec) @@ -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]; @@ -407,7 +410,7 @@ bool rc_service_started_daemon(const char *service, return false; snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s", - basename_c(service)); + basename_c(service)); match = _match_list(exec, argv, NULL); if (indx > 0) { @@ -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; @@ -456,7 +460,7 @@ bool rc_service_daemons_crashed(const char *service) size_t i; path += snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s", - basename_c(service)); + basename_c(service)); if (!(dp = opendir(dirpath))) return false; @@ -466,7 +470,7 @@ bool rc_service_daemons_crashed(const char *service) continue; snprintf(path, sizeof(dirpath) - (path - dirpath), "/%s", - d->d_name); + d->d_name); fp = fopen(dirpath, "r"); if (!fp) break; @@ -529,19 +533,19 @@ bool rc_service_daemons_crashed(const char *service) /* We need to flatten our linked list into an array */ i = 0; TAILQ_FOREACH(s, list, entries) - i++; + i++; argv = xmalloc(sizeof(char *) * (i + 1)); i = 0; TAILQ_FOREACH(s, list, entries) - argv[i++] = s->value; + argv[i++] = s->value; argv[i] = '\0'; } } if (!retval) { if ((pids = rc_find_pids(exec, - (const char *const *)argv, - 0, pid))) + (const char *const *)argv, + 0, pid))) { p1 = LIST_FIRST(pids); while (p1) { diff --git a/src/librc/librc-misc.c b/src/librc/librc-misc.c index 04c097b1..fe6d21ef 100644 --- a/src/librc/librc-misc.c +++ b/src/librc/librc-misc.c @@ -1,7 +1,7 @@ /* - rc-misc.c - rc misc functions - */ + 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; @@ -172,7 +176,7 @@ RC_STRINGLIST *rc_config_load(const char *file) TAILQ_FOREACH(cline, config, entries) { p = strchr(cline->value, '='); if (p && strncmp(entry, cline->value, - (size_t)(p - cline->value)) == 0) + (size_t)(p - cline->value)) == 0) { /* We have a match now - to save time we directly replace it */ free(cline->value); @@ -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; diff --git a/src/librc/librc-stringlist.c b/src/librc/librc-stringlist.c index 90e2af8d..7a8f30a2 100644 --- a/src/librc/librc-stringlist.c +++ b/src/librc/librc-stringlist.c @@ -1,7 +1,7 @@ /* - librc-strlist.h - String list functions to make using queue(3) a little easier. - */ + 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,51 +51,55 @@ 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; TAILQ_FOREACH(s, list, entries) - if (strcmp(s->value, value) == 0) { - errno = EEXIST; - return NULL; - } + if (strcmp(s->value, value) == 0) { + errno = EEXIST; + return NULL; + } return rc_stringlist_add(list, 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; TAILQ_FOREACH(s, list, entries) - if (strcmp(s->value, value) == 0) { - TAILQ_REMOVE(list, s, entries); - free (s->value); - free (s); - return true; - } + if (strcmp(s->value, value) == 0) { + TAILQ_REMOVE(list, s, entries); + free (s->value); + free (s); + return true; + } errno = EEXIST; return false; } 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; if (list) { TAILQ_FOREACH(s, list, entries) - if (strcmp(s->value, value) == 0) - return s; + if (strcmp(s->value, value) == 0) + return s; } return NULL; } 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; diff --git a/src/librc/librc.c b/src/librc/librc.c index b7ab9203..8b8f5561 100644 --- a/src/librc/librc.c +++ b/src/librc/librc.c @@ -1,7 +1,7 @@ /* - librc - core RC functions - */ + librc + core RC functions +*/ /* * Copyright 2007-2008 Roy Marples <roy@marples.name> @@ -33,13 +33,13 @@ 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" #ifndef S_IXUGO -# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH) +# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH) #endif /* File stream used for plugins to write environ vars to */ @@ -88,7 +88,7 @@ ls_dir(const char *dir, int options) * This is important as a service maybe in a * runlevel, but could have been removed. */ snprintf(file, sizeof(file), "%s/%s", - dir, d->d_name); + dir, d->d_name); r = stat(file, &buf); if (r != 0) continue; @@ -130,7 +130,7 @@ rm_dir(const char *pathname, bool top) strcmp(d->d_name, "..") != 0) { snprintf(file, sizeof(file), - "%s/%s", pathname, d->d_name); + "%s/%s", pathname, d->d_name); if (stat(file, &s) != 0) { retval = false; break; @@ -229,12 +229,12 @@ rc_sys(void) } else if (file_regex("/proc/cpuinfo", "UML")) return RC_SYS_UML; else if (file_regex("/proc/self/status", - "(s_context|VxID):[[:space:]]*[1-9]")) + "(s_context|VxID):[[:space:]]*[1-9]")) return RC_SYS_VSERVER; else if (exists("/proc/vz/veinfo") && !exists("/proc/vz/version")) return RC_SYS_OPENVZ; else if (file_regex("/proc/self/status", - "envID:[[:space:]]*[1-9]")) + "envID:[[:space:]]*[1-9]")) return RC_SYS_OPENVZ; /* old test */ #endif @@ -349,7 +349,7 @@ rc_service_resolve(const char *service) snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", "started", service); if (lstat(file, &buf) || ! S_ISLNK(buf.st_mode)) { snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "inactive", service); + "inactive", service); if (lstat(file, &buf) || ! S_ISLNK(buf.st_mode)) *file = '\0'; } @@ -497,7 +497,7 @@ rc_service_in_runlevel(const char *service, const char *runlevel) char file[PATH_MAX]; snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", - runlevel, basename_c(service)); + runlevel, basename_c(service)); return exists(file); } librc_hidden_def(rc_service_in_runlevel) @@ -528,7 +528,7 @@ rc_service_mark(const char *service, const RC_SERVICE state) } snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - rc_parse_service_state(state), base); + rc_parse_service_state(state), base); if (exists(file)) unlink(file); i = symlink(init, file); @@ -549,22 +549,22 @@ rc_service_mark(const char *service, const RC_SERVICE state) s = rc_service_state_names[i].state; if ((s != skip_state && - s != RC_SERVICE_STOPPED && - s != RC_SERVICE_HOTPLUGGED && - s != RC_SERVICE_SCHEDULED) && + s != RC_SERVICE_STOPPED && + s != RC_SERVICE_HOTPLUGGED && + s != RC_SERVICE_SCHEDULED) && (! skip_wasinactive || s != RC_SERVICE_WASINACTIVE)) { snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - rc_service_state_names[i].name, base); + rc_service_state_names[i].name, base); if (exists(file)) { if ((state == RC_SERVICE_STARTING || - state == RC_SERVICE_STOPPING) && + state == RC_SERVICE_STOPPING) && s == RC_SERVICE_INACTIVE) { snprintf(was, sizeof(was), - RC_SVCDIR "/%s/%s", - rc_parse_service_state(RC_SERVICE_WASINACTIVE), - base); + RC_SVCDIR "/%s/%s", + rc_parse_service_state(RC_SERVICE_WASINACTIVE), + base); if (symlink(init, was) == -1) return false; skip_wasinactive = true; @@ -583,18 +583,18 @@ rc_service_mark(const char *service, const RC_SERVICE state) state == RC_SERVICE_INACTIVE) { snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "exclusive", base); + "exclusive", base); unlink(file); } /* Remove any options and daemons the service may have stored */ if (state == RC_SERVICE_STOPPED) { snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "options", base); + "options", base); rm_dir(file, true); snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "daemons", base); + "daemons", base); rm_dir(file, true); rc_service_schedule_clear(service); @@ -606,7 +606,7 @@ rc_service_mark(const char *service, const RC_SERVICE state) dirs = ls_dir(file, 0); TAILQ_FOREACH(dir, dirs, entries) { snprintf(was, sizeof(was), "%s/%s/%s", - file, dir->value, base); + file, dir->value, base); unlink(was); /* Try and remove the dir; we don't care about errors */ @@ -634,7 +634,7 @@ rc_service_state(const char *service) for (i = 0; rc_service_state_names[i].name; i++) { snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - rc_service_state_names[i].name, base); + rc_service_state_names[i].name, base); if (exists(file)) { if (rc_service_state_names[i].state <= 0x10) state = rc_service_state_names[i].state; @@ -647,8 +647,8 @@ rc_service_state(const char *service) dirs = ls_dir(RC_SVCDIR "/scheduled", 0); TAILQ_FOREACH (dir, dirs, entries) { snprintf(file, sizeof(file), - RC_SVCDIR "/scheduled/%s/%s", - dir->value, service); + RC_SVCDIR "/scheduled/%s/%s", + dir->value, service); if (exists(file)) { state |= RC_SERVICE_SCHEDULED; break; @@ -670,7 +670,7 @@ rc_service_value_get(const char *service, const char *option) char file[PATH_MAX]; snprintf(file, sizeof(file), RC_SVCDIR "/options/%s/%s", - service, option); + service, option); if ((fp = fopen(file, "r"))) { rc_getline(&line, &len, fp); fclose(fp); @@ -682,7 +682,7 @@ librc_hidden_def(rc_service_value_get) bool rc_service_value_set(const char *service, const char *option, - const char *value) + const char *value) { FILE *fp; char file[PATH_MAX]; @@ -716,13 +716,13 @@ rc_service_schedule_start(const char *service, const char *service_to_start) return false; p += snprintf(file, sizeof(file), RC_SVCDIR "/scheduled/%s", - basename_c(service)); + basename_c(service)); if (mkdir(file, 0755) != 0 && errno != EEXIST) return false; init = rc_service_resolve(service_to_start); snprintf(p, sizeof(file) - (p - file), - "/%s", basename_c(service_to_start)); + "/%s", basename_c(service_to_start)); retval = (exists(file) || symlink(init, file) == 0); free(init); return retval; @@ -735,7 +735,7 @@ rc_service_schedule_clear(const char *service) char dir[PATH_MAX]; snprintf(dir, sizeof(dir), RC_SVCDIR "/scheduled/%s", - basename_c(service)); + basename_c(service)); if (!rm_dir(dir, true) && errno == ENOENT) return true; return false; @@ -791,7 +791,7 @@ rc_services_in_state(RC_SERVICE state) char *p = dir; p += snprintf(dir, sizeof(dir), RC_SVCDIR "/%s", - rc_parse_service_state(state)); + rc_parse_service_state(state)); if (state != RC_SERVICE_SCHEDULED) return ls_dir(dir, LS_INITD); @@ -837,7 +837,7 @@ rc_service_add(const char *runlevel, const char *service) i = init = rc_service_resolve(service); snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", - runlevel, basename_c(service)); + runlevel, basename_c(service)); /* We need to ensure that only things in /etc/init.d are added * to the boot runlevel */ @@ -868,7 +868,7 @@ rc_service_delete(const char *runlevel, const char *service) char file[PATH_MAX]; snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", - runlevel, basename_c(service)); + runlevel, basename_c(service)); if (unlink(file) == 0) return true; return false; @@ -885,7 +885,7 @@ rc_services_scheduled_by(const char *service) TAILQ_FOREACH (dir, dirs, entries) { snprintf(file, sizeof(file), RC_SVCDIR "/scheduled/%s/%s", - dir->value, service); + dir->value, service); if (exists(file)) rc_stringlist_add(list, file); } @@ -900,7 +900,7 @@ rc_services_scheduled(const char *service) char dir[PATH_MAX]; snprintf(dir, sizeof(dir), RC_SVCDIR "/scheduled/%s", - basename_c(service)); + basename_c(service)); return ls_dir(dir, LS_INITD); } librc_hidden_def(rc_services_scheduled) |