diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..84366ac05 --- /dev/null +++ b/.clang-format @@ -0,0 +1,110 @@ +# .clang-format +--- +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: false +BreakStringLiterals: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +ContinuationIndentWidth: 8 +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +ForEachMacros: + - SLIST_FOREACH + - SLIST_FOREACH_FROM + - SLIST_FOREACH_FROM_SAFE + - SLIST_FOREACH_SAFE + - SLIST_FOREACH_PREVPTR + - SPLAY_FOREACH + - LIST_FOREACH + - LIST_FOREACH_FROM + - LIST_FOREACH_FROM_SAFE + - LIST_FOREACH_SAFE + - STAILQ_FOREACH + - STAILQ_FOREACH_FROM + - STAILQ_FOREACH_FROM_SAFE + - STAILQ_FOREACH_SAFE + - TAILQ_FOREACH + - TAILQ_FOREACH_FROM + - TAILQ_FOREACH_FROM_SAFE + - TAILQ_FOREACH_REVERSE + - TAILQ_FOREACH_REVERSE_FROM + - TAILQ_FOREACH_REVERSE_FROM_SAFE + - TAILQ_FOREACH_REVERSE_SAFE + - TAILQ_FOREACH_SAFE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentGotoLabels: false +IndentPPDirectives: None +IndentWidth: 8 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 + +# Taken from git's rules +PenaltyBreakAssignment: 10 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +PointerAlignment: Right +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatementsExceptForEachMacros +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 8 +UseTab: Always +... diff --git a/.github/workflows/clang-format-ubuntu.yml b/.github/workflows/clang-format-ubuntu.yml new file mode 100644 index 000000000..8d2022cb4 --- /dev/null +++ b/.github/workflows/clang-format-ubuntu.yml @@ -0,0 +1,14 @@ +name: clang_format_ubuntu + +on: [push, pull_request] + +jobs: + + clang-format-ubuntu: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: RafikFarhad/clang-format-github-action@master + with: + sources: "src/**/*.h,src/**/*.h.in,src/**/*.c" + style: file diff --git a/STYLE-GUIDE.md b/STYLE-GUIDE.md index 49827da63..5d44c50e3 100644 --- a/STYLE-GUIDE.md +++ b/STYLE-GUIDE.md @@ -7,32 +7,10 @@ or fixes you might notice. ## C CODE -The BSD Kernel Normal Form (KNF) style is used [1]. Basically, it is like -K&R/LKML, but wrapped lines that are indented use 4 spaces. Here are the -highlights. - -- no trailing whitespace -- indented code use tabs (not line wrapped) -- cuddle the braces (except for functions) -- space after native statements and before paren (for/if/while/...) -- no space between function and paren -- pointer asterisk cuddles the variable, not the type - -``` -void foo(int c) -{ - int ret = 0; - - if (c > 1000) - return; - - while (c--) { - bar(c); - ret++; - } - - return ret; -} +This project uses clang format to define the code style. To format code +automatically, run the following command: +```bash +$ clang-format --style=file -i path/of/source/file ``` ## COMMIT MESSAGES diff --git a/src/checkpath/checkpath.c b/src/checkpath/checkpath.c index a98703a92..322671c0e 100644 --- a/src/checkpath/checkpath.c +++ b/src/checkpath/checkpath.c @@ -17,8 +17,8 @@ */ #define _GNU_SOURCE -#include #include +#include #include #include @@ -31,11 +31,11 @@ #include #include +#include "_usage.h" #include "einfo.h" -#include "rc.h" #include "misc.h" +#include "rc.h" #include "selinux.h" -#include "_usage.h" typedef enum { inode_unknown = 0, @@ -45,21 +45,15 @@ typedef enum { } inode_t; const char *applet = NULL; -const char *extraopts ="path1 [path2] [...]"; +const char *extraopts = "path1 [path2] [...]"; const char getoptstring[] = "dDfFpm:o:sW" getoptstring_COMMON; const struct option longopts[] = { - { "directory", 0, NULL, 'd'}, - { "directory-truncate", 0, NULL, 'D'}, - { "file", 0, NULL, 'f'}, - { "file-truncate", 0, NULL, 'F'}, - { "pipe", 0, NULL, 'p'}, - { "mode", 1, NULL, 'm'}, - { "owner", 1, NULL, 'o'}, - { "symlinks", 0, NULL, 's'}, - { "writable", 0, NULL, 'W'}, - longopts_COMMON -}; -const char * const longopts_help[] = { + {"directory", 0, NULL, 'd'}, {"directory-truncate", 0, NULL, 'D'}, + {"file", 0, NULL, 'f'}, {"file-truncate", 0, NULL, 'F'}, + {"pipe", 0, NULL, 'p'}, {"mode", 1, NULL, 'm'}, + {"owner", 1, NULL, 'o'}, {"symlinks", 0, NULL, 's'}, + {"writable", 0, NULL, 'W'}, longopts_COMMON}; +const char *const longopts_help[] = { "Create a directory if not exists", "Create/empty directory", "Create a file if not exists", @@ -69,8 +63,7 @@ const char * const longopts_help[] = { "Owner to check (user:group)", "follow symbolic links (irrelevant on linux)", "Check whether the path is writable or not", - longopts_help_COMMON -}; + longopts_help_COMMON}; const char *usagestring = NULL; static int get_dirfd(char *path, bool symlinks) @@ -91,8 +84,8 @@ static int get_dirfd(char *path, bool symlinks) eerrorx("%s: empty or relative path", applet); dirfd = openat(dirfd, "/", O_RDONLY); if (dirfd == -1) - eerrorx("%s: unable to open the root directory: %s", - applet, strerror(errno)); + eerrorx("%s: unable to open the root directory: %s", applet, + strerror(errno)); ch = path; while (*ch) { if (*ch == '/') @@ -111,22 +104,24 @@ static int get_dirfd(char *path, bool symlinks) str = xstrdup(linkpath ? linkpath : item); new_dirfd = openat(dirfd, str, flags); if (new_dirfd == -1) - eerrorx("%s: %s: could not open %s: %s", applet, path, str, - strerror(errno)); + eerrorx("%s: %s: could not open %s: %s", applet, path, + str, strerror(errno)); if (fstat(new_dirfd, &st) == -1) - eerrorx("%s: %s: unable to stat %s: %s", applet, path, item, - strerror(errno)); - if (S_ISLNK(st.st_mode) ) { + eerrorx("%s: %s: unable to stat %s: %s", applet, path, + item, strerror(errno)); + if (S_ISLNK(st.st_mode)) { if (st.st_uid != 0) eerrorx("%s: %s: symbolic link %s not owned by root", - applet, path, str); - linksize = st.st_size+1; + applet, path, str); + linksize = st.st_size + 1; if (linkpath) free(linkpath); linkpath = xmalloc(linksize); memset(linkpath, 0, linksize); - if (readlinkat(new_dirfd, "", linkpath, linksize) != st.st_size) - eerrorx("%s: symbolic link destination changed", applet); + if (readlinkat(new_dirfd, "", linkpath, linksize) != + st.st_size) + eerrorx("%s: symbolic link destination changed", + applet); /* * now follow the symlink. */ @@ -160,7 +155,7 @@ static char *clean_path(char *path) *ch2 = *ch; ch++; ch2++; - if (!*(ch-1)) + if (!*(ch - 1)) break; while (*(ch - 1) == '/' && *ch == '/') ch++; @@ -169,7 +164,7 @@ static char *clean_path(char *path) while ((ch = strrchr(str, '/'))) { if (ch == str) break; - if (!*(ch+1)) + if (!*(ch + 1)) *ch = 0; else break; @@ -177,8 +172,8 @@ static char *clean_path(char *path) return str; } -static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, - inode_t type, bool trunc, bool chowner, bool symlinks, bool selinux_on) +static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, inode_t type, + bool trunc, bool chowner, bool symlinks, bool selinux_on) { struct stat st; char *name = NULL; @@ -191,8 +186,8 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, int u; memset(&st, 0, sizeof(st)); - flags = O_CREAT|O_NDELAY|O_WRONLY|O_NOCTTY; - readflags = O_NDELAY|O_NOCTTY|O_RDONLY; + flags = O_CREAT | O_NDELAY | O_WRONLY | O_NOCTTY; + readflags = O_NDELAY | O_NOCTTY | O_RDONLY; #ifdef O_CLOEXEC flags |= O_CLOEXEC; readflags |= O_CLOEXEC; @@ -210,7 +205,8 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, if (type == inode_file) { einfo("%s: creating file", path); if (!mode) /* 664 */ - mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; + mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | + S_IROTH; u = umask(0); fd = openat(dirfd, name, flags, mode); umask(u); @@ -233,14 +229,14 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, if (r == -1 && errno != EEXIST) { free(name); eerror("%s: mkdirat: %s", applet, - strerror (errno)); + strerror(errno)); return -1; } readfd = openat(dirfd, name, readflags); if (readfd == -1) { free(name); - eerror("%s: unable to open directory: %s", applet, - strerror(errno)); + eerror("%s: unable to open directory: %s", + applet, strerror(errno)); return -1; } } else if (type == inode_fifo) { @@ -253,14 +249,14 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, if (r == -1 && errno != EEXIST) { free(name); eerror("%s: mkfifo: %s", applet, - strerror (errno)); + strerror(errno)); return -1; } readfd = openat(dirfd, name, readflags); if (readfd == -1) { free(name); eerror("%s: unable to open fifo: %s", applet, - strerror(errno)); + strerror(errno)); return -1; } } @@ -287,18 +283,21 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, if (mode && (st.st_mode & 0777) != mode) { if ((type != inode_dir) && (st.st_nlink > 1)) { - eerror("%s: chmod: Too many hard links to %s", applet, path); + eerror("%s: chmod: Too many hard links to %s", + applet, path); close(readfd); return -1; } if (S_ISLNK(st.st_mode)) { - eerror("%s: chmod: %s %s", applet, path, " is a symbolic link"); + eerror("%s: chmod: %s %s", applet, path, + " is a symbolic link"); close(readfd); return -1; } einfo("%s: correcting mode", path); if (fchmod(readfd, mode)) { - eerror("%s: chmod: %s", applet, strerror(errno)); + eerror("%s: chmod: %s", applet, + strerror(errno)); close(readfd); return -1; } @@ -306,18 +305,21 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, if (chowner && (st.st_uid != uid || st.st_gid != gid)) { if ((type != inode_dir) && (st.st_nlink > 1)) { - eerror("%s: chown: %s %s", applet, "Too many hard links to", path); + eerror("%s: chown: %s %s", applet, + "Too many hard links to", path); close(readfd); return -1; } if (S_ISLNK(st.st_mode)) { - eerror("%s: chown: %s %s", applet, path, " is a symbolic link"); + eerror("%s: chown: %s %s", applet, path, + " is a symbolic link"); close(readfd); return -1; } einfo("%s: correcting owner", path); if (fchown(readfd, uid, gid)) { - eerror("%s: chown: %s", applet, strerror(errno)); + eerror("%s: chown: %s", applet, + strerror(errno)); close(readfd); return -1; } @@ -335,10 +337,10 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode, } static int parse_owner(struct passwd **user, struct group **group, - const char *owner) + const char *owner) { - char *u = xstrdup (owner); - char *g = strchr (u, ':'); + char *u = xstrdup(owner); + char *g = strchr(u, ':'); int id = 0; int retval = 0; @@ -347,7 +349,7 @@ static int parse_owner(struct passwd **user, struct group **group, if (user && *u) { if (sscanf(u, "%d", &id) == 1) - *user = getpwuid((uid_t) id); + *user = getpwuid((uid_t)id); else *user = getpwnam(u); if (*user == NULL) @@ -356,7 +358,7 @@ static int parse_owner(struct passwd **user, struct group **group, if (group && g && *g) { if (sscanf(g, "%d", &id) == 1) - *group = getgrgid((gid_t) id); + *group = getgrgid((gid_t)id); else *group = getgrnam(g); if (*group == NULL) @@ -385,9 +387,8 @@ int main(int argc, char **argv) char *path = NULL; applet = basename_c(argv[0]); - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { case 'D': trunc = true; @@ -406,14 +407,14 @@ int main(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': chowner = true; 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 's': #ifndef O_PATH @@ -424,7 +425,7 @@ int main(int argc, char **argv) writable = true; break; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } } @@ -432,7 +433,8 @@ int main(int argc, char **argv) usage(EXIT_FAILURE); if (writable && type != inode_unknown) - eerrorx("%s: -W cannot be specified along with -d, -f or -p", applet); + eerrorx("%s: -W cannot be specified along with -d, -f or -p", + applet); if (pw) { uid = pw->pw_uid; @@ -449,7 +451,7 @@ int main(int argc, char **argv) if (writable) exit(!is_writable(path)); if (do_check(path, uid, gid, mode, type, trunc, chowner, - symlinks, selinux_on)) + symlinks, selinux_on)) retval = EXIT_FAILURE; optind++; free(path); diff --git a/src/einfo/einfo.c b/src/einfo/einfo.c index 691688cea..f764685b9 100644 --- a/src/einfo/einfo.c +++ b/src/einfo/einfo.c @@ -12,16 +12,16 @@ #define SYSLOG_NAMES -#include #include +#include -#include #include +#include #include +#include #include #include #include -#include #include #include #include @@ -31,7 +31,7 @@ #include "helpers.h" /* usecs to wait while we poll the file existence */ -#define WAIT_INTERVAL 20000000 +#define WAIT_INTERVAL 20000000 const char *applet = NULL; @@ -59,8 +59,8 @@ int main(int argc, char **argv) int level = 0; struct timespec ts; struct timeval stop, now; - int (*e) (const char *, ...) EINFO_PRINTF(1, 2) = NULL; - int (*ee) (int, const char *, ...) EINFO_PRINTF(2, 3) = NULL; + int (*e)(const char *, ...) EINFO_PRINTF(1, 2) = NULL; + int (*ee)(int, const char *, ...) EINFO_PRINTF(2, 3) = NULL; applet = basename_c(argv[0]); argc--; @@ -68,12 +68,9 @@ int main(int argc, char **argv) if (strcmp(applet, "eval_ecolors") == 0) { printf("GOOD='%s'\nWARN='%s'\nBAD='%s'\nHILITE='%s'\nBRACKET='%s'\nNORMAL='%s'\n", - ecolor(ECOLOR_GOOD), - ecolor(ECOLOR_WARN), - ecolor(ECOLOR_BAD), - ecolor(ECOLOR_HILITE), - ecolor(ECOLOR_BRACKET), - ecolor(ECOLOR_NORMAL)); + ecolor(ECOLOR_GOOD), ecolor(ECOLOR_WARN), + ecolor(ECOLOR_BAD), ecolor(ECOLOR_HILITE), + ecolor(ECOLOR_BRACKET), ecolor(ECOLOR_NORMAL)); exit(EXIT_SUCCESS); } @@ -82,8 +79,7 @@ int main(int argc, char **argv) strcmp(applet, "ewend") == 0 || strcmp(applet, "veend") == 0 || strcmp(applet, "vweend") == 0 || - strcmp(applet, "ewaitfile") == 0) - { + strcmp(applet, "ewaitfile") == 0) { errno = 0; retval = (int)strtoimax(argv[0], &p, 0); if (!p || *p != '\0') @@ -95,11 +91,12 @@ int main(int argc, char **argv) argv++; } } else if (strcmp(applet, "esyslog") == 0 || - strcmp(applet, "elog") == 0) { + strcmp(applet, "elog") == 0) { p = strchr(argv[0], '.'); 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) eerrorx("%s: not enough arguments", applet); @@ -135,8 +132,8 @@ int main(int argc, char **argv) continue; if (timercmp(&now, &stop, <)) continue; - eendv(EXIT_FAILURE, - "timed out waiting for %s", argv[i]); + eendv(EXIT_FAILURE, "timed out waiting for %s", + argv[i]); return EXIT_FAILURE; } eendv(EXIT_SUCCESS, NULL); diff --git a/src/fstabinfo/fstabinfo.c b/src/fstabinfo/fstabinfo.c index 839e6e361..ac4bb3666 100644 --- a/src/fstabinfo/fstabinfo.c +++ b/src/fstabinfo/fstabinfo.c @@ -28,53 +28,48 @@ 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 -# 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 +#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 -# 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 +#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 "_usage.h" #include "einfo.h" +#include "misc.h" #include "queue.h" #include "rc.h" -#include "misc.h" -#include "_usage.h" const char *applet = NULL; const char *extraopts = NULL; const char getoptstring[] = "MRbmop:t:" getoptstring_COMMON; const struct option longopts[] = { - { "mount", 0, NULL, 'M' }, - { "remount", 0, NULL, 'R' }, - { "blockdevice", 0, NULL, 'b' }, - { "mountargs", 0, NULL, 'm' }, - { "options", 0, NULL, 'o' }, - { "passno", 1, NULL, 'p' }, - { "fstype", 1, NULL, 't' }, - longopts_COMMON -}; -const char * const longopts_help[] = { + {"mount", 0, NULL, 'M'}, {"remount", 0, NULL, 'R'}, + {"blockdevice", 0, NULL, 'b'}, {"mountargs", 0, NULL, 'm'}, + {"options", 0, NULL, 'o'}, {"passno", 1, NULL, 'p'}, + {"fstype", 1, NULL, 't'}, longopts_COMMON}; +const char *const longopts_help[] = { "Mounts the filesystem from the mountpoint", "Remounts the filesystem based on the information in fstab", "Extract the block device", @@ -82,13 +77,11 @@ const char * const longopts_help[] = { "Extract the options field", "Extract or query the pass number field", "List entries with matching file system type", - longopts_help_COMMON -}; + longopts_help_COMMON}; const char *usagestring = NULL; #ifdef HAVE_GETMNTENT -static struct mntent * -getmntfile(const char *file) +static struct mntent *getmntfile(const char *file) { struct mntent *ent; FILE *fp; @@ -105,8 +98,7 @@ getmntfile(const char *file) extern const char *applet; -static int -do_mount(struct ENT *ent, bool remount) +static int do_mount(struct ENT *ent, bool remount) { char *argv[10]; pid_t pid; @@ -154,13 +146,13 @@ do_mount(struct ENT *ent, bool remount) } } -#define OUTPUT_FILE (1 << 1) +#define OUTPUT_FILE (1 << 1) #define OUTPUT_MOUNTARGS (1 << 2) -#define OUTPUT_OPTIONS (1 << 3) -#define OUTPUT_PASSNO (1 << 4) -#define OUTPUT_BLOCKDEV (1 << 5) -#define OUTPUT_MOUNT (1 << 6) -#define OUTPUT_REMOUNT (1 << 7) +#define OUTPUT_OPTIONS (1 << 3) +#define OUTPUT_PASSNO (1 << 4) +#define OUTPUT_BLOCKDEV (1 << 5) +#define OUTPUT_MOUNT (1 << 6) +#define OUTPUT_REMOUNT (1 << 7) int main(int argc, char **argv) { @@ -185,9 +177,8 @@ int main(int argc, char **argv) unsetenv("EINFO_QUIET"); applet = basename_c(argv[0]); - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { case 'M': output = OUTPUT_MOUNT; @@ -212,7 +203,7 @@ int main(int argc, char **argv) case '>': if (sscanf(optarg + 1, "%d", &i) != 1) eerrorx("%s: invalid passno %s", - argv[0], optarg + 1); + argv[0], optarg + 1); filtered = true; opt = optarg[0]; @@ -224,8 +215,8 @@ int main(int argc, char **argv) if ((opt == '=' && i == p) || (opt == '<' && i > p && p != 0) || (opt == '>' && i < p && p != 0)) - rc_stringlist_add(files, - ENT_FILE(ent)); + rc_stringlist_add( + files, ENT_FILE(ent)); } END_ENT; break; @@ -243,13 +234,13 @@ int main(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; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } } @@ -261,7 +252,7 @@ int main(int argc, char **argv) break; if (i >= argc) rc_stringlist_delete(files, - file->value); + file->value); } } else { while (optind < argc) @@ -311,11 +302,9 @@ int main(int argc, char **argv) break; case OUTPUT_MOUNTARGS: - printf("-o %s -t %s %s %s\n", - ENT_OPTS(ent), - ENT_TYPE(ent), - ENT_BLOCKDEVICE(ent), - file->value); + printf("-o %s -t %s %s %s\n", ENT_OPTS(ent), + ENT_TYPE(ent), ENT_BLOCKDEVICE(ent), + file->value); break; case OUTPUT_OPTIONS: diff --git a/src/is_newer_than/is_newer_than.c b/src/is_newer_than/is_newer_than.c index bd916ec5c..c44b6ec1f 100644 --- a/src/is_newer_than/is_newer_than.c +++ b/src/is_newer_than/is_newer_than.c @@ -10,13 +10,13 @@ * except according to the terms contained in the LICENSE file. */ -#include #include +#include #include #include -#include "rc.h" #include "misc.h" +#include "rc.h" int main(int argc, char **argv) { diff --git a/src/is_older_than/is_older_than.c b/src/is_older_than/is_older_than.c index b3f7072b3..9a6250f6d 100644 --- a/src/is_older_than/is_older_than.c +++ b/src/is_older_than/is_older_than.c @@ -10,13 +10,13 @@ * except according to the terms contained in the LICENSE file. */ -#include #include +#include #include #include -#include "rc.h" #include "misc.h" +#include "rc.h" int main(int argc, char **argv) { diff --git a/src/kill_all/kill_all.c b/src/kill_all/kill_all.c index 704f30b8e..817c463e8 100644 --- a/src/kill_all/kill_all.c +++ b/src/kill_all/kill_all.c @@ -15,7 +15,6 @@ * except according to the terms contained in the LICENSE file. */ - #include #include #include @@ -24,29 +23,25 @@ #include #include #include -#include -#include #include #include +#include +#include +#include "_usage.h" #include "einfo.h" -#include "rc.h" #include "misc.h" -#include "_usage.h" +#include "rc.h" const char *applet = NULL; const char *extraopts = "[signal number]"; const char getoptstring[] = "do:" getoptstring_COMMON; -const struct option longopts[] = { - { "dry-run", 0, NULL, 'd' }, - { "omit", 1, NULL, 'o' }, - longopts_COMMON -}; -const char * const longopts_help[] = { - "print what would be done", - "omit this pid (can be repeated)", - longopts_help_COMMON -}; +const struct option longopts[] = {{"dry-run", 0, NULL, 'd'}, + {"omit", 1, NULL, 'o'}, + longopts_COMMON}; +const char *const longopts_help[] = {"print what would be done", + "omit this pid (can be repeated)", + longopts_help_COMMON}; const char *usagestring = NULL; static int mount_proc(void) @@ -59,24 +54,24 @@ static int mount_proc(void) return 0; pid = fork(); switch (pid) { - case -1: - syslog(LOG_ERR, "Unable to fork"); - return -1; - break; - case 0: - /* attempt to mount /proc */ - execlp("mount", "mount", "-t", "proc", "proc", "/proc", NULL); - syslog(LOG_ERR, "Unable to execute mount"); - exit(1); - break; - default: - /* wait for child process */ - while ((rc = wait(&status)) != pid) - if (rc < 0 && errno == ECHILD) - break; - if (rc != pid || WEXITSTATUS(status) != 0) - syslog(LOG_ERR, "mount returned non-zero exit status"); - break; + case -1: + syslog(LOG_ERR, "Unable to fork"); + return -1; + break; + case 0: + /* attempt to mount /proc */ + execlp("mount", "mount", "-t", "proc", "proc", "/proc", NULL); + syslog(LOG_ERR, "Unable to execute mount"); + exit(1); + break; + default: + /* wait for child process */ + while ((rc = wait(&status)) != pid) + if (rc < 0 && errno == ECHILD) + break; + if (rc != pid || WEXITSTATUS(status) != 0) + syslog(LOG_ERR, "mount returned non-zero exit status"); + break; } if (!exists("/proc/version")) { syslog(LOG_ERR, "Could not mount /proc"); @@ -94,7 +89,7 @@ static bool is_user_process(pid_t pid) size_t size; bool user_process = true; - while (pid >0 && user_process) { + while (pid > 0 && user_process) { if (pid == 2) { user_process = false; continue; @@ -125,7 +120,8 @@ static bool is_user_process(pid_t pid) } fclose(fp); if (temp_pid == -1) { - syslog(LOG_ERR, "Unable to read pid from /proc/%d/status", pid); + syslog(LOG_ERR, + "Unable to read pid from /proc/%d/status", pid); user_process = false; continue; } @@ -139,7 +135,7 @@ static int signal_processes(int sig, RC_STRINGLIST *omits, bool dryrun) sigset_t signals; sigset_t oldsigs; DIR *dir; - struct dirent *d; + struct dirent *d; char *buf = NULL; pid_t pid; int sendcount = 0; @@ -170,7 +166,7 @@ static int signal_processes(int sig, RC_STRINGLIST *omits, bool dryrun) /* Walk through the directory. */ while ((d = readdir(dir)) != NULL) { /* Is this a process? */ - pid = (pid_t) atoi(d->d_name); + pid = (pid_t)atoi(d->d_name); if (pid == 0) continue; @@ -217,39 +213,39 @@ int main(int argc, char **argv) applet = basename_c(argv[0]); rc_stringlist_addu(omits, "1"); - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { - case 'd': - dryrun = true; - break; - case 'o': - here = optarg; - while ((token = strsep(&here, ",;:"))) { - if ((pid_t) atoi(token) > 0) - rc_stringlist_addu(omits, token); - else { - eerror("Invalid omit pid value %s", token); - usage(EXIT_FAILURE); - } + case 'd': + dryrun = true; + break; + case 'o': + here = optarg; + while ((token = strsep(&here, ",;:"))) { + if ((pid_t)atoi(token) > 0) + rc_stringlist_addu(omits, token); + else { + eerror("Invalid omit pid value %s", + token); + usage(EXIT_FAILURE); } - break; + } + break; case_RC_COMMON_GETOPT } } if (argc > optind) { - arg = argv[optind]; - sig = atoi(arg); - if (sig <= 0 || sig > 31) { - rc_stringlist_free(omits); - eerror("Invalid signal %s", arg); - usage(EXIT_FAILURE); - } + arg = argv[optind]; + sig = atoi(arg); + if (sig <= 0 || sig > 31) { + rc_stringlist_free(omits); + eerror("Invalid signal %s", arg); + usage(EXIT_FAILURE); + } } - openlog(applet, LOG_CONS|LOG_PID, LOG_DAEMON); + openlog(applet, LOG_CONS | LOG_PID, LOG_DAEMON); if (mount_proc() != 0) { rc_stringlist_free(omits); eerrorx("Unable to mount /proc file system"); diff --git a/src/libeinfo/einfo.h b/src/libeinfo/einfo.h index d9f1b54eb..17f3502ca 100644 --- a/src/libeinfo/einfo.h +++ b/src/libeinfo/einfo.h @@ -14,25 +14,26 @@ #define __EINFO_H__ #if defined(__GNUC__) -# define EINFO_PRINTF(a, b) __attribute__((__format__(__printf__, a, b))) -# define EINFO_XPRINTF(a, b) __attribute__((__noreturn__,__format__(__printf__, a, b))) +#define EINFO_PRINTF(a, b) __attribute__((__format__(__printf__, a, b))) +#define EINFO_XPRINTF(a, b) \ + __attribute__((__noreturn__, __format__(__printf__, a, b))) #else -# define EINFO_PRINTF(a, b) -# define EINFO_XPRINTF(a, b) +#define EINFO_PRINTF(a, b) +#define EINFO_XPRINTF(a, b) #endif -#include #include +#include /* Although OpenRC requires C99, linking to us should not. */ #ifdef restrict -# define EINFO_RESTRICT restrict +#define EINFO_RESTRICT restrict #else -# ifdef __restrict -# define EINFO_RESTRICT __restrict -# else -# define EINFO_RESTRICT -# endif +#ifdef __restrict +#define EINFO_RESTRICT __restrict +#else +#define EINFO_RESTRICT +#endif #endif /* __BEGIN_DECLS */ @@ -41,21 +42,20 @@ extern "C" { #endif /*! @brief Color types to use */ -typedef enum -{ - ECOLOR_NORMAL = 1, - ECOLOR_GOOD = 2, - ECOLOR_WARN = 3, - ECOLOR_BAD = 4, - ECOLOR_HILITE = 5, - ECOLOR_BRACKET = 6 +typedef enum { + ECOLOR_NORMAL = 1, + ECOLOR_GOOD = 2, + ECOLOR_WARN = 3, + ECOLOR_BAD = 4, + ECOLOR_HILITE = 5, + ECOLOR_BRACKET = 6 } ECOLOR; /*! @brief Returns the ASCII code for the color */ const char *ecolor(ECOLOR); /*! @brief Writes to syslog. */ -void elog(int, const char * EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); +void elog(int, const char *EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); /*! * @brief Display informational messages. @@ -74,22 +74,22 @@ void elog(int, const char * EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); * The x suffix means function will exit() returning failure. */ /*@{*/ -int einfon(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int ewarnn(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int eerrorn(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int einfo(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int ewarn(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -void ewarnx(const char * __EINFO_RESTRICT, ...) EINFO_XPRINTF(1, 2); -int eerror(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -void eerrorx(const char * __EINFO_RESTRICT, ...) EINFO_XPRINTF(1, 2); - -int einfovn(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int ewarnvn(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int ebeginvn(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int eendvn(int, const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); -int ewendvn(int, const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); -int einfov(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int ewarnv(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int einfon(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int ewarnn(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int eerrorn(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int einfo(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int ewarn(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +void ewarnx(const char *__EINFO_RESTRICT, ...) EINFO_XPRINTF(1, 2); +int eerror(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +void eerrorx(const char *__EINFO_RESTRICT, ...) EINFO_XPRINTF(1, 2); + +int einfovn(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int ewarnvn(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int ebeginvn(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int eendvn(int, const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); +int ewendvn(int, const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); +int einfov(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int ewarnv(const char *__EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); /*@}*/ /*! @ingroup ebegin @@ -97,8 +97,8 @@ int ewarnv(const char * __EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); * * Similar to einfo, but we add ... to the end of the message */ /*@{*/ -int ebeginv(const char * EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); -int ebegin(const char * EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int ebeginv(const char *EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); +int ebegin(const char *EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); /*@}*/ /*! @ingroup eend @@ -110,12 +110,12 @@ int ebegin(const char * EINFO_RESTRICT, ...) EINFO_PRINTF(1, 2); * * ebracket allows you to specify the position, color and message */ /*@{*/ -int eend(int, const char * EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); -int ewend(int, const char * EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); -void ebracket(int, ECOLOR, const char * EINFO_RESTRICT); +int eend(int, const char *EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); +int ewend(int, const char *EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); +void ebracket(int, ECOLOR, const char *EINFO_RESTRICT); -int eendv(int, const char * EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); -int ewendv(int, const char * EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); +int eendv(int, const char *EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); +int ewendv(int, const char *EINFO_RESTRICT, ...) EINFO_PRINTF(2, 3); /*@}*/ /*! @ingroup eindent @@ -129,7 +129,7 @@ void eindentv(void); void eoutdentv(void); /*! @brief Prefix each einfo line with something */ -void eprefix(const char * EINFO_RESTRICT); +void eprefix(const char *EINFO_RESTRICT); /* __END_DECLS */ #ifdef __cplusplus diff --git a/src/libeinfo/libeinfo.c b/src/libeinfo/libeinfo.c index aa49e0d04..26e9d281d 100644 --- a/src/libeinfo/libeinfo.c +++ b/src/libeinfo/libeinfo.c @@ -15,9 +15,9 @@ * except according to the terms contained in the LICENSE file. */ -#include #include #include +#include #include #include @@ -31,7 +31,7 @@ #include #include #ifdef HAVE_TERMCAP -# include +#include #endif #include @@ -39,23 +39,23 @@ #include "helpers.h" /* Incase we cannot work out how many columns from ioctl, supply a default */ -#define DEFAULT_COLS 80 +#define DEFAULT_COLS 80 -#define OK "ok" -#define NOT_OK "!!" +#define OK "ok" +#define NOT_OK "!!" /* Number of spaces for an indent */ -#define INDENT_WIDTH 2 +#define INDENT_WIDTH 2 /* How wide can the indent go? */ -#define INDENT_MAX 40 +#define INDENT_MAX 40 /* Default colours */ -#define GOOD 2 -#define WARN 3 -#define BAD 1 -#define HILITE 6 -#define BRACKET 4 +#define GOOD 2 +#define WARN 3 +#define BAD 1 +#define HILITE 6 +#define BRACKET 4 /* We fallback to these escape codes if termcap isn't available * like say /usr isn't mounted */ @@ -67,9 +67,10 @@ #define UP "\033[A" #define _GET_CAP(_d, _c) strlcpy(_d, tgoto(_c, 0, 0), sizeof(_d)); -#define _ASSIGN_CAP(_v) do { \ - _v = p; \ - p += strlcpy(p, tmp, sizeof(ebuffer) - (p - ebuffer)) + 1; \ +#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 */ @@ -85,12 +86,12 @@ struct ecolor { static char nullstr = '\0'; static const struct ecolor ecolors[] = { - { ECOLOR_GOOD, GOOD, "good" }, - { ECOLOR_WARN, WARN, "warn" }, - { ECOLOR_BAD, BAD, "bad" }, - { ECOLOR_HILITE, HILITE, "hilite" }, - { ECOLOR_BRACKET, BRACKET, "bracket" }, - { ECOLOR_NORMAL, 0, NULL }, + {ECOLOR_GOOD, GOOD, "good"}, + {ECOLOR_WARN, WARN, "warn"}, + {ECOLOR_BAD, BAD, "bad"}, + {ECOLOR_HILITE, HILITE, "hilite"}, + {ECOLOR_BRACKET, BRACKET, "bracket"}, + {ECOLOR_NORMAL, 0, NULL}, }; static const char *ecolors_str[ARRAY_SIZE(ecolors)]; @@ -109,52 +110,49 @@ static char tcapbuf[512]; #else /* No curses support, so we hardcode a list of colour capable terms * Only terminals without "color" in the name need to be explicitly listed */ -static const char *const color_terms[] = { - "Eterm", - "ansi", - "con132x25", - "con132x30", - "con132x43", - "con132x60", - "con80x25", - "con80x28", - "con80x30", - "con80x43", - "con80x50", - "con80x60", - "cons25", - "console", - "cygwin", - "dtterm", - "gnome", - "konsole", - "kterm", - "linux", - "linux-c", - "mlterm", - "putty", - "rxvt", - "rxvt-cygwin", - "rxvt-cygwin-native", - "rxvt-unicode", - "screen", - "screen-bce", - "screen-w", - "screen.linux", - "vt100", - "vt220", - "wsvt25", - "xterm", - "xterm-debian", - NULL -}; +static const char *const color_terms[] = {"Eterm", + "ansi", + "con132x25", + "con132x30", + "con132x43", + "con132x60", + "con80x25", + "con80x28", + "con80x30", + "con80x43", + "con80x50", + "con80x60", + "cons25", + "console", + "cygwin", + "dtterm", + "gnome", + "konsole", + "kterm", + "linux", + "linux-c", + "mlterm", + "putty", + "rxvt", + "rxvt-cygwin", + "rxvt-cygwin-native", + "rxvt-unicode", + "screen", + "screen-bce", + "screen-w", + "screen.linux", + "vt100", + "vt220", + "wsvt25", + "xterm", + "xterm-debian", + NULL}; #endif /* strlcat and strlcpy are nice, shame glibc does not define them */ #ifdef __GLIBC__ -# if !defined (__UCLIBC__) && !defined (__dietlibc__) -static size_t -strlcat(char *dst, const char *src, size_t size) +#if !defined(__UCLIBC__) && !defined(__dietlibc__) +static size_t strlcat(char *dst, const char *src, size_t size) { char *d = dst; const char *s = src; @@ -180,36 +178,30 @@ strlcat(char *dst, const char *src, size_t size) return dst_n + (s - src); } -# endif +#endif #endif -static bool -yesno(const char *value) +static bool yesno(const char *value) { if (!value) { errno = ENOENT; return false; } - if (strcasecmp(value, "yes") == 0 || - strcasecmp(value, "y") == 0 || - strcasecmp(value, "true") == 0 || - strcasecmp(value, "on") == 0 || + if (strcasecmp(value, "yes") == 0 || strcasecmp(value, "y") == 0 || + strcasecmp(value, "true") == 0 || strcasecmp(value, "on") == 0 || strcasecmp(value, "1") == 0) return true; - if (strcasecmp(value, "no") != 0 && - strcasecmp(value, "n") != 0 && - strcasecmp(value, "false") != 0 && - strcasecmp(value, "off") != 0 && + if (strcasecmp(value, "no") != 0 && strcasecmp(value, "n") != 0 && + strcasecmp(value, "false") != 0 && strcasecmp(value, "off") != 0 && strcasecmp(value, "0") != 0) errno = EINVAL; return false; } -static bool -noyes(const char *value) +static bool noyes(const char *value) { int serrno = errno; bool retval; @@ -224,28 +216,24 @@ noyes(const char *value) return retval; } -static bool -is_quiet(void) +static bool is_quiet(void) { return yesno(getenv("EINFO_QUIET")); } -static bool -is_really_quiet(void) +static bool is_really_quiet(void) { return yesno(getenv("EERROR_QUIET")); } -static bool -is_verbose(void) +static bool is_verbose(void) { - return yesno(getenv ("EINFO_VERBOSE")); + return yesno(getenv("EINFO_VERBOSE")); } /* Fake tgoto call - very crapy, but works for our needs */ #ifndef HAVE_TERMCAP -static char * -tgoto(const char *cap, int col, int line) +static char *tgoto(const char *cap, int col, int line) { static char buf[20]; char *p, *e, c, dbuf[6]; @@ -308,8 +296,7 @@ tgoto(const char *cap, int col, int line) } #endif -static bool -colour_terminal(FILE * EINFO_RESTRICT f) +static bool colour_terminal(FILE *EINFO_RESTRICT f) { static int in_colour = -1; char *e, *ee, *end, *d, *p; @@ -362,20 +349,20 @@ colour_terminal(FILE * EINFO_RESTRICT f) * which is not available to us when we boot */ if (term_is_cons25 || strcmp(term, "wsvt25") == 0) { #else - if (strstr(term, "color")) - in_colour = 1; + if (strstr(term, "color")) + in_colour = 1; - while (color_terms[i] && in_colour != 1) { - if (strcmp(color_terms[i], term) == 0) { - in_colour = 1; - } - i++; + while (color_terms[i] && in_colour != 1) { + if (strcmp(color_terms[i], term) == 0) { + in_colour = 1; } + i++; + } - if (in_colour != 1) { - in_colour = 0; - return false; - } + if (in_colour != 1) { + in_colour = 0; + return false; + } #endif if (!_af) _af = AF; @@ -455,8 +442,7 @@ colour_terminal(FILE * EINFO_RESTRICT f) return true; } -static int -get_term_columns(FILE * EINFO_RESTRICT stream) +static int get_term_columns(FILE *EINFO_RESTRICT stream) { struct winsize ws; char *env = getenv("COLUMNS"); @@ -475,14 +461,13 @@ get_term_columns(FILE * EINFO_RESTRICT stream) return DEFAULT_COLS; } -void -eprefix(const char *EINFO_RESTRICT prefix) +void eprefix(const char *EINFO_RESTRICT prefix) { _eprefix = prefix; } static void EINFO_PRINTF(2, 0) -elogv(int level, const char *EINFO_RESTRICT fmt, va_list ap) + elogv(int level, const char *EINFO_RESTRICT fmt, va_list ap) { char *e = getenv("EINFO_LOG"); va_list apc; @@ -497,8 +482,7 @@ elogv(int level, const char *EINFO_RESTRICT fmt, va_list ap) } } -void -elog(int level, const char *EINFO_RESTRICT fmt, ...) +void elog(int level, const char *EINFO_RESTRICT fmt, ...) { va_list ap; @@ -507,8 +491,7 @@ elog(int level, const char *EINFO_RESTRICT fmt, ...) va_end(ap); } -static int -_eindent(FILE * EINFO_RESTRICT stream) +static int _eindent(FILE *EINFO_RESTRICT stream) { char *env = getenv("EINFO_INDENT"); int amount = 0; @@ -531,8 +514,7 @@ _eindent(FILE * EINFO_RESTRICT stream) return fprintf(stream, "%s", indent); } -static const char * -_ecolor(FILE * EINFO_RESTRICT f, ECOLOR color) +static const char *_ecolor(FILE *EINFO_RESTRICT f, ECOLOR color) { unsigned int i; @@ -545,8 +527,7 @@ _ecolor(FILE * EINFO_RESTRICT f, ECOLOR color) return ""; } -const char * -ecolor(ECOLOR color) +const char *ecolor(ECOLOR color) { FILE *f = stdout; @@ -563,30 +544,30 @@ ecolor(ECOLOR color) return _ecolor(f, color); } -#define LASTCMD(_cmd) { \ - unsetenv("EINFO_LASTCMD"); \ - setenv("EINFO_LASTCMD", _cmd, 1); \ +#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) +static int EINFO_PRINTF(3, 0) _einfo(FILE *f, ECOLOR color, + const char *EINFO_RESTRICT fmt, va_list va) { int retval = 0; char *last = getenv("EINFO_LASTCMD"); va_list ap; - if (last && - !colour_terminal(f) && - strcmp(last, "ewarn") != 0 && + if (last && !colour_terminal(f) && strcmp(last, "ewarn") != 0 && last[strlen(last) - 1] == 'n') fprintf(f, "\n"); if (_eprefix) - fprintf(f, "%s%s%s|", _ecolor(f, color), _eprefix, _ecolor(f, ECOLOR_NORMAL)); + fprintf(f, "%s%s%s|", _ecolor(f, color), _eprefix, + _ecolor(f, ECOLOR_NORMAL)); fprintf(f, " %s*%s ", _ecolor(f, color), _ecolor(f, ECOLOR_NORMAL)); retval += _eindent(f); va_copy(ap, va); retval += vfprintf(f, fmt, ap) + 3; - va_end(ap); \ + va_end(ap); if (colour_terminal(f)) fprintf(f, "%s", flush); return retval; @@ -596,8 +577,7 @@ static int EINFO_PRINTF(3, 0) #define _ewarnvn(fmt, ap) _einfo(stderr, ECOLOR_WARN, fmt, ap) #define _eerrorvn(fmt, ap) _einfo(stderr, ECOLOR_BAD, fmt, ap) -int -einfon(const char *EINFO_RESTRICT fmt, ...) +int einfon(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -611,8 +591,7 @@ einfon(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -ewarnn(const char *EINFO_RESTRICT fmt, ...) +int ewarnn(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -626,8 +605,7 @@ ewarnn(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -eerrorn(const char *EINFO_RESTRICT fmt, ...) +int eerrorn(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -641,8 +619,7 @@ eerrorn(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -einfo(const char *EINFO_RESTRICT fmt, ...) +int einfo(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -657,8 +634,7 @@ einfo(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -ewarn(const char *EINFO_RESTRICT fmt, ...) +int ewarn(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -674,8 +650,7 @@ ewarn(const char *EINFO_RESTRICT fmt, ...) return retval; } -void -ewarnx(const char *EINFO_RESTRICT fmt, ...) +void ewarnx(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -690,8 +665,7 @@ ewarnx(const char *EINFO_RESTRICT fmt, ...) exit(EXIT_FAILURE); } -int -eerror(const char *EINFO_RESTRICT fmt, ...) +int eerror(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -707,8 +681,7 @@ eerror(const char *EINFO_RESTRICT fmt, ...) return retval; } -void -eerrorx(const char *EINFO_RESTRICT fmt, ...) +void eerrorx(const char *EINFO_RESTRICT fmt, ...) { va_list ap; @@ -722,8 +695,7 @@ eerrorx(const char *EINFO_RESTRICT fmt, ...) exit(EXIT_FAILURE); } -int -ebegin(const char *EINFO_RESTRICT fmt, ...) +int ebegin(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -740,8 +712,8 @@ ebegin(const char *EINFO_RESTRICT fmt, ...) return retval; } -static void -_eend(FILE * EINFO_RESTRICT fp, int col, ECOLOR color, const char *msg) +static void _eend(FILE *EINFO_RESTRICT fp, int col, ECOLOR color, + const char *msg) { int i; int cols; @@ -764,9 +736,10 @@ _eend(FILE * EINFO_RESTRICT fp, int col, ECOLOR color, const char *msg) cols--; if (cols > 0 && colour_terminal(fp)) { - fprintf(fp, "%s%s %s[%s %s %s]%s\n", up, tgoto(goto_column, 0, cols), - ecolor(ECOLOR_BRACKET), ecolor(color), msg, - ecolor(ECOLOR_BRACKET), ecolor(ECOLOR_NORMAL)); + fprintf(fp, "%s%s %s[%s %s %s]%s\n", up, + tgoto(goto_column, 0, cols), ecolor(ECOLOR_BRACKET), + ecolor(color), msg, ecolor(ECOLOR_BRACKET), + ecolor(ECOLOR_NORMAL)); } else { if (col > 0) for (i = 0; i < cols - col; i++) @@ -776,8 +749,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; @@ -793,14 +766,12 @@ _do_eend(const char *cmd, int retval, col += fprintf(fp, "\n"); va_end(apc); } - _eend(fp, col, - retval == 0 ? ECOLOR_GOOD : ECOLOR_BAD, - retval == 0 ? OK : NOT_OK); + _eend(fp, col, retval == 0 ? ECOLOR_GOOD : ECOLOR_BAD, + retval == 0 ? OK : NOT_OK); return retval; } -int -eend(int retval, const char *EINFO_RESTRICT fmt, ...) +int eend(int retval, const char *EINFO_RESTRICT fmt, ...) { va_list ap; @@ -813,8 +784,7 @@ eend(int retval, const char *EINFO_RESTRICT fmt, ...) return retval; } -int -ewend(int retval, const char *EINFO_RESTRICT fmt, ...) +int ewend(int retval, const char *EINFO_RESTRICT fmt, ...) { va_list ap; @@ -827,14 +797,12 @@ ewend(int retval, const char *EINFO_RESTRICT fmt, ...) return retval; } -void -ebracket(int col, ECOLOR color, const char *msg) +void ebracket(int col, ECOLOR color, const char *msg) { _eend(stdout, col, color, msg); } -void -eindent(void) +void eindent(void) { char *env = getenv("EINFO_INDENT"); int amount = 0; @@ -879,8 +847,7 @@ void eoutdent(void) errno = serrno; } -int -einfovn(const char *EINFO_RESTRICT fmt, ...) +int einfovn(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -894,8 +861,7 @@ einfovn(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -ewarnvn(const char *EINFO_RESTRICT fmt, ...) +int ewarnvn(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -909,8 +875,7 @@ ewarnvn(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -einfov(const char *EINFO_RESTRICT fmt, ...) +int einfov(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -925,8 +890,7 @@ einfov(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -ewarnv(const char *EINFO_RESTRICT fmt, ...) +int ewarnv(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -941,8 +905,7 @@ ewarnv(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -ebeginv(const char *EINFO_RESTRICT fmt, ...) +int ebeginv(const char *EINFO_RESTRICT fmt, ...) { int retval; va_list ap; @@ -960,8 +923,7 @@ ebeginv(const char *EINFO_RESTRICT fmt, ...) return retval; } -int -eendv(int retval, const char *EINFO_RESTRICT fmt, ...) +int eendv(int retval, const char *EINFO_RESTRICT fmt, ...) { va_list ap; @@ -974,8 +936,7 @@ eendv(int retval, const char *EINFO_RESTRICT fmt, ...) return retval; } -int -ewendv(int retval, const char *EINFO_RESTRICT fmt, ...) +int ewendv(int retval, const char *EINFO_RESTRICT fmt, ...) { va_list ap; @@ -988,15 +949,13 @@ ewendv(int retval, const char *EINFO_RESTRICT fmt, ...) return retval; } -void -eindentv(void) +void eindentv(void) { if (is_verbose()) eindent(); } -void -eoutdentv(void) +void eoutdentv(void) { if (is_verbose()) eoutdent(); diff --git a/src/librc/librc-daemon.c b/src/librc/librc-daemon.c index ec02e8b75..9f3a26d92 100644 --- a/src/librc/librc-daemon.c +++ b/src/librc/librc-daemon.c @@ -17,13 +17,13 @@ #include -#include "queue.h" #include "librc.h" +#include "queue.h" -#if defined(__linux__) || (defined (__FreeBSD_kernel__) && defined(__GLIBC__)) \ - || defined(__GNU__) -static bool -pid_is_exec(pid_t pid, const char *exec) +#if defined(__linux__) || \ + (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) || \ + defined(__GNU__) +static bool pid_is_exec(pid_t pid, const char *exec) { char *buffer = NULL; FILE *fp; @@ -47,8 +47,7 @@ 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 = NULL; int fd; @@ -80,8 +79,8 @@ 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; @@ -143,7 +142,7 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) memset(my_ns, 0, sizeof(my_ns)); memset(proc_ns, 0, sizeof(proc_ns)); if (exists("/proc/self/ns/pid")) { - rc = readlink("/proc/self/ns/pid", my_ns, sizeof(my_ns)-1); + rc = readlink("/proc/self/ns/pid", my_ns, sizeof(my_ns) - 1); if (rc <= 0) my_ns[0] = '\0'; } @@ -157,12 +156,13 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) continue; xasprintf(&buffer, "/proc/%d/ns/pid", p); if (exists(buffer)) { - rc = readlink(buffer, proc_ns, sizeof(proc_ns)-1); + rc = readlink(buffer, proc_ns, sizeof(proc_ns) - 1); if (rc <= 0) proc_ns[0] = '\0'; } free(buffer); - if (pid == 0 && strlen(my_ns) && strlen (proc_ns) && strcmp(my_ns, proc_ns)) + if (pid == 0 && strlen(my_ns) && strlen(proc_ns) && + strcmp(my_ns, proc_ns)) continue; if (uid) { xasprintf(&buffer, "/proc/%d", p); @@ -174,8 +174,7 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) } if (exec && !pid_is_exec(p, exec)) continue; - if (argv && - !pid_is_argv(p, (const char *const *)argv)) + if (argv && !pid_is_argv(p, (const char *const *)argv)) continue; /* If this is an OpenVZ host, filter out container processes */ if (openvz_host) { @@ -188,7 +187,10 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) while (!feof(fp)) { rc_getline(&line, &len, fp); if (strncmp(line, "envID:", 6) == 0) { - container_pid = !(strncmp(line, "envID:\t0", 8) == 0); + container_pid = + !(strncmp(line, + "envID:\t0", + 8) == 0); break; } } @@ -213,36 +215,36 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) #elif BSD -# if defined(__NetBSD__) || defined(__OpenBSD__) -# define _KVM_GETPROC2 -# define _KINFO_PROC kinfo_proc2 -# define _KVM_GETARGV kvm_getargv2 -# define _GET_KINFO_UID(kp) (kp.p_ruid) -# define _GET_KINFO_COMM(kp) (kp.p_comm) -# define _GET_KINFO_PID(kp) (kp.p_pid) -# define _KVM_PATH NULL -# define _KVM_FLAGS KVM_NO_FILES -# else -# ifndef KERN_PROC_PROC -# define KERN_PROC_PROC KERN_PROC_ALL -# endif -# define _KINFO_PROC kinfo_proc -# define _KVM_GETARGV kvm_getargv -# if defined(__DragonFly__) -# define _GET_KINFO_UID(kp) (kp.kp_ruid) -# define _GET_KINFO_COMM(kp) (kp.kp_comm) -# define _GET_KINFO_PID(kp) (kp.kp_pid) -# else -# define _GET_KINFO_UID(kp) (kp.ki_ruid) -# define _GET_KINFO_COMM(kp) (kp.ki_comm) -# define _GET_KINFO_PID(kp) (kp.ki_pid) -# endif -# define _KVM_PATH _PATH_DEVNULL -# define _KVM_FLAGS O_RDONLY -# endif - -RC_PIDLIST * -rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) +#if defined(__NetBSD__) || defined(__OpenBSD__) +#define _KVM_GETPROC2 +#define _KINFO_PROC kinfo_proc2 +#define _KVM_GETARGV kvm_getargv2 +#define _GET_KINFO_UID(kp) (kp.p_ruid) +#define _GET_KINFO_COMM(kp) (kp.p_comm) +#define _GET_KINFO_PID(kp) (kp.p_pid) +#define _KVM_PATH NULL +#define _KVM_FLAGS KVM_NO_FILES +#else +#ifndef KERN_PROC_PROC +#define KERN_PROC_PROC KERN_PROC_ALL +#endif +#define _KINFO_PROC kinfo_proc +#define _KVM_GETARGV kvm_getargv +#if defined(__DragonFly__) +#define _GET_KINFO_UID(kp) (kp.kp_ruid) +#define _GET_KINFO_COMM(kp) (kp.kp_comm) +#define _GET_KINFO_PID(kp) (kp.kp_pid) +#else +#define _GET_KINFO_UID(kp) (kp.ki_ruid) +#define _GET_KINFO_COMM(kp) (kp.ki_comm) +#define _GET_KINFO_PID(kp) (kp.ki_pid) +#endif +#define _KVM_PATH _PATH_DEVNULL +#define _KVM_FLAGS O_RDONLY +#endif + +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]; @@ -257,9 +259,8 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) const char *const *arg; int match; - if ((kd = kvm_openfiles(_KVM_PATH, _KVM_PATH, - NULL, _KVM_FLAGS, errbuf)) == NULL) - { + if ((kd = kvm_openfiles(_KVM_PATH, _KVM_PATH, NULL, _KVM_FLAGS, + errbuf)) == NULL) { fprintf(stderr, "kvm_open: %s\n", errbuf); return NULL; } @@ -316,11 +317,11 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) } #else -# error "Platform not supported!" +#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; @@ -337,10 +338,10 @@ _match_daemon(const char *path, const char *file, RC_STRINGLIST *match) 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; } @@ -351,8 +352,8 @@ _match_daemon(const char *path, const char *file, RC_STRINGLIST *match) 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; @@ -379,15 +380,14 @@ _match_list(const char *exec, const char *const *argv, const char *pidfile) 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 = NULL; char *file = NULL; int nfiles = 0; - char oldfile[PATH_MAX] = { '\0' }; + char oldfile[PATH_MAX] = {'\0'}; bool retval = false; DIR *dp; struct dirent *d; @@ -456,9 +456,8 @@ rc_service_daemon_set(const char *service, const char *exec, return retval; } -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 = NULL; char *file = NULL; @@ -482,7 +481,8 @@ rc_service_started_daemon(const char *service, while ((d = readdir(dp))) { if (d->d_name[0] == '.') continue; - retval = _match_daemon(dirpath, d->d_name, match); + retval = _match_daemon(dirpath, d->d_name, + match); if (retval) break; } @@ -495,8 +495,7 @@ rc_service_started_daemon(const char *service, return retval; } -bool -rc_service_daemons_crashed(const char *service) +bool rc_service_daemons_crashed(const char *service) { char dirpath[PATH_MAX]; DIR *dp; @@ -523,7 +522,7 @@ rc_service_daemons_crashed(const char *service) char *spidfile; path += snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s", - basename_c(service)); + basename_c(service)); if (!(dp = opendir(dirpath))) return false; @@ -533,7 +532,7 @@ 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; @@ -568,7 +567,8 @@ rc_service_daemons_crashed(const char *service) ch_root = rc_service_value_get(basename_c(service), "chroot"); spidfile = pidfile; if (ch_root && pidfile) { - spidfile = xmalloc(strlen(ch_root) + strlen(pidfile) + 1); + spidfile = + xmalloc(strlen(ch_root) + strlen(pidfile) + 1); strcpy(spidfile, ch_root); strcat(spidfile, pidfile); free(pidfile); @@ -608,11 +608,11 @@ rc_service_daemons_crashed(const char *service) 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] = NULL; } } @@ -621,10 +621,9 @@ rc_service_daemons_crashed(const char *service) if (pid != 0) { if (kill(pid, 0) == -1 && errno == ESRCH) retval = true; - } else if ((pids = rc_find_pids(exec, - (const char *const *)argv, - 0, pid))) - { + } else if ((pids = rc_find_pids( + exec, (const char *const *)argv, 0, + pid))) { p1 = LIST_FIRST(pids); while (p1) { p2 = LIST_NEXT(p1, entries); diff --git a/src/librc/librc-depend.c b/src/librc/librc-depend.c index 4da81c030..d8c7cbddc 100644 --- a/src/librc/librc-depend.c +++ b/src/librc/librc-depend.c @@ -17,17 +17,16 @@ #include -#include "queue.h" #include "librc.h" +#include "queue.h" -#define GENDEP RC_LIBEXECDIR "/sh/gendepends.sh" +#define GENDEP RC_LIBEXECDIR "/sh/gendepends.sh" -#define RC_DEPCONFIG RC_SVCDIR "/depconfig" +#define RC_DEPCONFIG RC_SVCDIR "/depconfig" static const char *bootlevel = NULL; -static char * -get_shell_value(char *string) +static char *get_shell_value(char *string) { char *p = string; char *e; @@ -50,8 +49,7 @@ get_shell_value(char *string) return NULL; } -void -rc_deptree_free(RC_DEPTREE *deptree) +void rc_deptree_free(RC_DEPTREE *deptree) { RC_DEPINFO *di; RC_DEPINFO *di2; @@ -81,8 +79,7 @@ rc_deptree_free(RC_DEPTREE *deptree) free(deptree); } -static RC_DEPINFO * -get_depinfo(const RC_DEPTREE *deptree, const char *service) +static RC_DEPINFO *get_depinfo(const RC_DEPTREE *deptree, const char *service) { RC_DEPINFO *di; if (deptree) { @@ -93,8 +90,7 @@ get_depinfo(const RC_DEPTREE *deptree, const char *service) return NULL; } -static RC_DEPTYPE * -get_deptype(const RC_DEPINFO *depinfo, const char *type) +static RC_DEPTYPE *get_deptype(const RC_DEPINFO *depinfo, const char *type) { RC_DEPTYPE *dt; @@ -106,13 +102,12 @@ get_deptype(const RC_DEPINFO *depinfo, const char *type) return NULL; } -RC_DEPTREE * -rc_deptree_load(void) { +RC_DEPTREE *rc_deptree_load(void) +{ return rc_deptree_load_file(RC_DEPTREE_CACHE); } -RC_DEPTREE * -rc_deptree_load_file(const char *deptree_file) +RC_DEPTREE *rc_deptree_load_file(const char *deptree_file) { FILE *fp; RC_DEPTREE *deptree; @@ -130,8 +125,7 @@ rc_deptree_load_file(const char *deptree_file) deptree = xmalloc(sizeof(*deptree)); TAILQ_INIT(deptree); - while ((rc_getline(&line, &len, fp))) - { + while ((rc_getline(&line, &len, fp))) { p = line; e = strsep(&p, "_"); if (!e || strcmp(e, "depinfo") != 0) @@ -174,42 +168,38 @@ rc_deptree_load_file(const char *deptree_file) return deptree; } -static bool -valid_service(const char *runlevel, const char *service, const char *type) +static bool valid_service(const char *runlevel, const char *service, + const char *type) { RC_SERVICE state; - if (!runlevel || - strcmp(type, "ineed") == 0 || - strcmp(type, "needsme") == 0 || - strcmp(type, "iwant") == 0 || + if (!runlevel || strcmp(type, "ineed") == 0 || + strcmp(type, "needsme") == 0 || strcmp(type, "iwant") == 0 || strcmp(type, "wantsme") == 0) return true; if (rc_service_in_runlevel(service, runlevel)) return true; if (strcmp(runlevel, RC_LEVEL_SYSINIT) == 0) - return false; + return false; if (strcmp(runlevel, RC_LEVEL_SHUTDOWN) == 0 && strcmp(type, "iafter") == 0) - return false; + return false; if (strcmp(runlevel, bootlevel) != 0) { if (rc_service_in_runlevel(service, bootlevel)) return true; } state = rc_service_state(service); - if (state & RC_SERVICE_HOTPLUGGED || - state & RC_SERVICE_STARTED) + if (state & RC_SERVICE_HOTPLUGGED || state & RC_SERVICE_STARTED) return true; return false; } -static bool -get_provided1(const char *runlevel, RC_STRINGLIST *providers, - RC_DEPTYPE *deptype, const char *level, - bool hotplugged, RC_SERVICE state) +static bool get_provided1(const char *runlevel, RC_STRINGLIST *providers, + RC_DEPTYPE *deptype, const char *level, + bool hotplugged, RC_SERVICE state) { RC_STRING *service; RC_SERVICE st; @@ -231,18 +221,18 @@ get_provided1(const char *runlevel, RC_STRINGLIST *providers, if (!ok) continue; switch (state) { - case RC_SERVICE_STARTED: - ok = (st & RC_SERVICE_STARTED); - break; - case RC_SERVICE_INACTIVE: - case RC_SERVICE_STARTING: - case RC_SERVICE_STOPPING: - ok = (st & RC_SERVICE_STARTING || - st & RC_SERVICE_STOPPING || - st & RC_SERVICE_INACTIVE); - break; - default: - break; + case RC_SERVICE_STARTED: + ok = (st & RC_SERVICE_STARTED); + break; + case RC_SERVICE_INACTIVE: + case RC_SERVICE_STARTING: + case RC_SERVICE_STOPPING: + ok = (st & RC_SERVICE_STARTING || + st & RC_SERVICE_STOPPING || + st & RC_SERVICE_INACTIVE); + break; + default: + break; } if (!ok) continue; @@ -262,8 +252,8 @@ get_provided1(const char *runlevel, RC_STRINGLIST *providers, If there are any bugs in rc-depend, they will probably be here as provided dependancy can change depending on runlevel state. */ -static RC_STRINGLIST * -get_provided(const RC_DEPINFO *depinfo, const char *runlevel, int options) +static RC_STRINGLIST *get_provided(const RC_DEPINFO *depinfo, + const char *runlevel, int options) { RC_DEPTYPE *dt; RC_STRINGLIST *providers = rc_stringlist_new(); @@ -289,7 +279,8 @@ get_provided(const RC_DEPINFO *depinfo, const char *runlevel, int options) if (rc_service_in_runlevel(service->value, runlevel) || rc_service_in_runlevel(service->value, bootlevel) || (options & RC_DEP_START && - rc_service_state(service->value) & RC_SERVICE_HOTPLUGGED)) + rc_service_state(service->value) & + RC_SERVICE_HOTPLUGGED)) rc_stringlist_add(providers, service->value); if (TAILQ_FIRST(providers)) return providers; @@ -305,44 +296,60 @@ get_provided(const RC_DEPINFO *depinfo, const char *runlevel, int options) * Our sub preference in each of these is in order:- * runlevel, hotplugged, bootlevel, any */ -#define DO \ - if (TAILQ_FIRST(providers)) { \ +#define DO \ + if (TAILQ_FIRST(providers)) { \ if (TAILQ_NEXT(TAILQ_FIRST(providers), entries)) { \ - rc_stringlist_free(providers); \ - providers = rc_stringlist_new(); \ - } \ - return providers; \ + rc_stringlist_free(providers); \ + providers = rc_stringlist_new(); \ + } \ + return providers; \ } /* Anything running has to come first */ - if (get_provided1(runlevel, providers, dt, runlevel, false, RC_SERVICE_STARTED)) - { DO } - if (get_provided1(runlevel, providers, dt, NULL, true, RC_SERVICE_STARTED)) - { DO } + if (get_provided1(runlevel, providers, dt, runlevel, false, + RC_SERVICE_STARTED)) { + DO + } + if (get_provided1(runlevel, providers, dt, NULL, true, + RC_SERVICE_STARTED)) { + DO + } if (bootlevel && strcmp(runlevel, bootlevel) != 0 && - get_provided1(runlevel, providers, dt, bootlevel, false, RC_SERVICE_STARTED)) - { DO } - if (get_provided1(runlevel, providers, dt, NULL, false, RC_SERVICE_STARTED)) - { DO } + get_provided1(runlevel, providers, dt, bootlevel, false, + RC_SERVICE_STARTED)) { + DO + } + if (get_provided1(runlevel, providers, dt, NULL, false, + RC_SERVICE_STARTED)) { + DO + } /* Check starting services */ - if (get_provided1(runlevel, providers, dt, runlevel, false, RC_SERVICE_STARTING)) + if (get_provided1(runlevel, providers, dt, runlevel, false, + RC_SERVICE_STARTING)) return providers; - if (get_provided1(runlevel, providers, dt, NULL, true, RC_SERVICE_STARTING)) + if (get_provided1(runlevel, providers, dt, NULL, true, + RC_SERVICE_STARTING)) return providers; if (bootlevel && strcmp(runlevel, bootlevel) != 0 && - get_provided1(runlevel, providers, dt, bootlevel, false, RC_SERVICE_STARTING)) - return providers; - if (get_provided1(runlevel, providers, dt, NULL, false, RC_SERVICE_STARTING)) + get_provided1(runlevel, providers, dt, bootlevel, false, + RC_SERVICE_STARTING)) + return providers; + if (get_provided1(runlevel, providers, dt, NULL, false, + RC_SERVICE_STARTING)) return providers; /* Nothing started then. OK, lets get the stopped services */ - if (get_provided1(runlevel, providers, dt, runlevel, false, RC_SERVICE_STOPPED)) + if (get_provided1(runlevel, providers, dt, runlevel, false, + RC_SERVICE_STOPPED)) return providers; - if (get_provided1(runlevel, providers, dt, NULL, true, RC_SERVICE_STOPPED)) - { DO } + if (get_provided1(runlevel, providers, dt, NULL, true, + RC_SERVICE_STOPPED)) { + DO + } if (bootlevel && (strcmp(runlevel, bootlevel) != 0) && - get_provided1(runlevel, providers, dt, bootlevel, false, RC_SERVICE_STOPPED)) + get_provided1(runlevel, providers, dt, bootlevel, false, + RC_SERVICE_STOPPED)) return providers; /* Still nothing? OK, list our first provided service. */ @@ -353,13 +360,10 @@ get_provided(const RC_DEPINFO *depinfo, const char *runlevel, int options) return providers; } -static void -visit_service(const RC_DEPTREE *deptree, - const RC_STRINGLIST *types, - RC_STRINGLIST *sorted, - RC_STRINGLIST *visited, - const RC_DEPINFO *depinfo, - const char *runlevel, int options) +static void visit_service(const RC_DEPTREE *deptree, const RC_STRINGLIST *types, + RC_STRINGLIST *sorted, RC_STRINGLIST *visited, + const RC_DEPINFO *depinfo, const char *runlevel, + int options) { RC_STRING *type; RC_STRING *service; @@ -376,15 +380,13 @@ visit_service(const RC_DEPTREE *deptree, /* Add ourselves as a visited service */ rc_stringlist_add(visited, depinfo->service); - TAILQ_FOREACH(type, types, entries) - { + TAILQ_FOREACH(type, types, entries) { if (!(dt = get_deptype(depinfo, type->value))) continue; TAILQ_FOREACH(service, dt->services, entries) { if (!(options & RC_DEP_TRACE) || - strcmp(type->value, "iprovide") == 0) - { + strcmp(type->value, "iprovide") == 0) { rc_stringlist_add(sorted, service->value); continue; } @@ -396,31 +398,35 @@ visit_service(const RC_DEPTREE *deptree, if (TAILQ_FIRST(provided)) { TAILQ_FOREACH(p, provided, entries) { di = get_depinfo(deptree, p->value); - if (di && valid_service(runlevel, di->service, type->value)) - visit_service(deptree, types, sorted, visited, di, - runlevel, options | RC_DEP_TRACE); + if (di && + valid_service(runlevel, di->service, + type->value)) + visit_service( + deptree, types, sorted, + visited, di, runlevel, + options | RC_DEP_TRACE); } - } - else if (di && valid_service(runlevel, service->value, type->value)) - visit_service(deptree, types, sorted, visited, di, - runlevel, options | RC_DEP_TRACE); + } else if (di && valid_service(runlevel, service->value, + type->value)) + visit_service(deptree, types, sorted, visited, + di, runlevel, + options | RC_DEP_TRACE); rc_stringlist_free(provided); } } /* Now visit the stuff we provide for */ - if (options & RC_DEP_TRACE && - (dt = get_deptype(depinfo, "iprovide"))) - { + if (options & RC_DEP_TRACE && (dt = get_deptype(depinfo, "iprovide"))) { TAILQ_FOREACH(service, dt->services, entries) { if (!(di = get_depinfo(deptree, service->value))) continue; provided = get_provided(di, runlevel, options); TAILQ_FOREACH(p, provided, entries) if (strcmp(p->value, depinfo->service) == 0) { - visit_service(deptree, types, sorted, visited, di, - runlevel, options | RC_DEP_TRACE); + visit_service(deptree, types, sorted, + visited, di, runlevel, + options | RC_DEP_TRACE); break; } rc_stringlist_free(provided); @@ -436,9 +442,8 @@ visit_service(const RC_DEPTREE *deptree, } } -RC_STRINGLIST * -rc_deptree_depend(const RC_DEPTREE *deptree, - const char *service, const char *type) +RC_STRINGLIST *rc_deptree_depend(const RC_DEPTREE *deptree, const char *service, + const char *type) { RC_DEPINFO *di; RC_DEPTYPE *dt; @@ -447,8 +452,7 @@ rc_deptree_depend(const RC_DEPTREE *deptree, svcs = rc_stringlist_new(); if (!(di = get_depinfo(deptree, service)) || - !(dt = get_deptype(di, type))) - { + !(dt = get_deptype(di, type))) { errno = ENOENT; return svcs; } @@ -459,11 +463,10 @@ rc_deptree_depend(const RC_DEPTREE *deptree, return svcs; } -RC_STRINGLIST * -rc_deptree_depends(const RC_DEPTREE *deptree, - const RC_STRINGLIST *types, - const RC_STRINGLIST *services, - const char *runlevel, int options) +RC_STRINGLIST *rc_deptree_depends(const RC_DEPTREE *deptree, + const RC_STRINGLIST *types, + const RC_STRINGLIST *services, + const char *runlevel, int options) { RC_STRINGLIST *sorted = rc_stringlist_new(); RC_STRINGLIST *visited = rc_stringlist_new(); @@ -479,15 +482,15 @@ rc_deptree_depends(const RC_DEPTREE *deptree, continue; } if (types) - visit_service(deptree, types, sorted, visited, - di, runlevel, options); + visit_service(deptree, types, sorted, visited, di, + runlevel, options); } rc_stringlist_free(visited); return sorted; } -RC_STRINGLIST * -rc_deptree_order(const RC_DEPTREE *deptree, const char *runlevel, int options) +RC_STRINGLIST *rc_deptree_order(const RC_DEPTREE *deptree, const char *runlevel, + int options) { RC_STRINGLIST *list; RC_STRINGLIST *list2; @@ -500,8 +503,7 @@ rc_deptree_order(const RC_DEPTREE *deptree, const char *runlevel, int options) /* When shutting down, list all running services */ if (strcmp(runlevel, RC_LEVEL_SINGLE) == 0 || - strcmp(runlevel, RC_LEVEL_SHUTDOWN) == 0) - { + strcmp(runlevel, RC_LEVEL_SHUTDOWN) == 0) { list = rc_services_in_state(RC_SERVICE_STARTED); list2 = rc_services_in_state(RC_SERVICE_INACTIVE); TAILQ_CONCAT(list, list2, entries); @@ -541,14 +543,12 @@ rc_deptree_order(const RC_DEPTREE *deptree, const char *runlevel, int options) return services; } - /* Given a time, recurse the target path to find out if there are any older (or newer) files. If false, sets the time to the oldest (or newest) found. */ -static bool -deep_mtime_check(const char *target, bool newer, - time_t *rel, char *file) +static bool deep_mtime_check(const char *target, bool newer, time_t *rel, + char *file) { struct stat buf; bool retval = true; @@ -602,9 +602,8 @@ deep_mtime_check(const char *target, bool newer, * If false, return the filename and most different time (if * the return value arguments are non-null). */ -static bool -mtime_check(const char *source, const char *target, bool newer, - time_t *rel, char *file) +static bool mtime_check(const char *source, const char *target, bool newer, + time_t *rel, char *file) { struct stat buf; time_t mtime; @@ -615,64 +614,52 @@ mtime_check(const char *source, const char *target, bool newer, return false; mtime = buf.st_mtime; - retval = deep_mtime_check(target,newer,&mtime,file); + retval = deep_mtime_check(target, newer, &mtime, file); if (rel) { *rel = mtime; } return retval; } -bool -rc_newer_than(const char *source, const char *target, - time_t *newest, char *file) +bool rc_newer_than(const char *source, const char *target, time_t *newest, + char *file) { - return mtime_check(source, target, true, newest, file); } -bool -rc_older_than(const char *source, const char *target, - time_t *oldest, char *file) +bool rc_older_than(const char *source, const char *target, time_t *oldest, + char *file) { return mtime_check(source, target, false, oldest, file); } -typedef struct deppair -{ +typedef struct deppair { const char *depend; const char *addto; } DEPPAIR; static const DEPPAIR deppairs[] = { - { "ineed", "needsme" }, - { "iuse", "usesme" }, - { "iwant", "wantsme" }, - { "iafter", "ibefore" }, - { "ibefore", "iafter" }, - { "iprovide", "providedby" }, - { NULL, NULL } -}; - -static const char *const depdirs[] = -{ - RC_SVCDIR, - RC_SVCDIR "/starting", - RC_SVCDIR "/started", - RC_SVCDIR "/stopping", - RC_SVCDIR "/inactive", - RC_SVCDIR "/wasinactive", - RC_SVCDIR "/failed", - RC_SVCDIR "/hotplugged", - RC_SVCDIR "/daemons", - RC_SVCDIR "/options", - RC_SVCDIR "/exclusive", - RC_SVCDIR "/scheduled", - RC_SVCDIR "/tmp", - NULL -}; - -bool -rc_deptree_update_needed(time_t *newest, char *file) + {"ineed", "needsme"}, {"iuse", "usesme"}, + {"iwant", "wantsme"}, {"iafter", "ibefore"}, + {"ibefore", "iafter"}, {"iprovide", "providedby"}, + {NULL, NULL}}; + +static const char *const depdirs[] = {RC_SVCDIR, + RC_SVCDIR "/starting", + RC_SVCDIR "/started", + RC_SVCDIR "/stopping", + RC_SVCDIR "/inactive", + RC_SVCDIR "/wasinactive", + RC_SVCDIR "/failed", + RC_SVCDIR "/hotplugged", + RC_SVCDIR "/daemons", + RC_SVCDIR "/options", + RC_SVCDIR "/exclusive", + RC_SVCDIR "/scheduled", + RC_SVCDIR "/tmp", + NULL}; + +bool rc_deptree_update_needed(time_t *newest, char *file) { bool newer = false; RC_STRINGLIST *config; @@ -684,7 +671,8 @@ rc_deptree_update_needed(time_t *newest, char *file) /* Create base directories if needed */ for (i = 0; depdirs[i]; i++) if (mkdir(depdirs[i], 0755) != 0 && errno != EEXIST) - fprintf(stderr, "mkdir `%s': %s\n", depdirs[i], strerror(errno)); + fprintf(stderr, "mkdir `%s': %s\n", depdirs[i], + strerror(errno)); /* Quick test to see if anything we use has changed and we have * data in our deptree. */ @@ -700,21 +688,21 @@ rc_deptree_update_needed(time_t *newest, char *file) mtime = time(NULL); } - newer |= !deep_mtime_check(RC_INITDIR,true,&mtime,file); - newer |= !deep_mtime_check(RC_CONFDIR,true,&mtime,file); + newer |= !deep_mtime_check(RC_INITDIR, true, &mtime, file); + newer |= !deep_mtime_check(RC_CONFDIR, true, &mtime, file); #ifdef RC_PKG_INITDIR - newer |= !deep_mtime_check(RC_PKG_INITDIR,true,&mtime,file); + newer |= !deep_mtime_check(RC_PKG_INITDIR, true, &mtime, file); #endif #ifdef RC_PKG_CONFDIR - newer |= !deep_mtime_check(RC_PKG_CONFDIR,true,&mtime,file); + newer |= !deep_mtime_check(RC_PKG_CONFDIR, true, &mtime, file); #endif #ifdef RC_LOCAL_INITDIRs - newer |= !deep_mtime_check(RC_LOCAL_INITDIR,true,&mtime,file); + newer |= !deep_mtime_check(RC_LOCAL_INITDIR, true, &mtime, file); #endif #ifdef RC_LOCAL_CONFDIR - newer |= !deep_mtime_check(RC_LOCAL_CONFDIR,true,&mtime,file); + newer |= !deep_mtime_check(RC_LOCAL_CONFDIR, true, &mtime, file); #endif - newer |= !deep_mtime_check(RC_CONF,true,&mtime,file); + newer |= !deep_mtime_check(RC_CONF, true, &mtime, file); /* Some init scripts dependencies change depending on config files * outside of baselayout, like syslog-ng, so we check those too. */ @@ -726,7 +714,7 @@ rc_deptree_update_needed(time_t *newest, char *file) /* Return newest file time, if requested */ if ((newer) && (newest != NULL)) { - *newest = mtime; + *newest = mtime; } return newer; @@ -743,8 +731,7 @@ rc_deptree_update_needed(time_t *newest, char *file) with the same names Phase 7 saves the depinfo object to disk */ -bool -rc_deptree_update(void) +bool rc_deptree_update(void) { FILE *fp; RC_DEPTREE *deptree, *providers; @@ -775,8 +762,7 @@ rc_deptree_update(void) deptree = xmalloc(sizeof(*deptree)); TAILQ_INIT(deptree); config = rc_stringlist_new(); - while ((rc_getline(&line, &len, fp))) - { + while ((rc_getline(&line, &len, fp))) { depends = line; service = strsep(&depends, " "); if (!service || !*service) @@ -806,14 +792,14 @@ rc_deptree_update(void) deptype = xmalloc(sizeof(*deptype)); deptype->type = xstrdup(type); deptype->services = rc_stringlist_new(); - TAILQ_INSERT_TAIL(&depinfo->depends, deptype, entries); + TAILQ_INSERT_TAIL(&depinfo->depends, deptype, + entries); } } /* Now add each depend to our type. We do this individually so we handle multiple spaces gracefully */ - while ((depend = strsep(&depends, " "))) - { + while ((depend = strsep(&depends, " "))) { if (depend[0] == 0) continue; @@ -829,15 +815,14 @@ rc_deptree_update(void) /* .sh files are not init scripts */ l = strlen(depend); - if (l > 2 && - depend[l - 3] == '.' && - depend[l - 2] == 's' && - depend[l - 1] == 'h') + if (l > 2 && depend[l - 3] == '.' && + depend[l - 2] == 's' && depend[l - 1] == 'h') continue; /* Remove our dependency if instructed */ if (depend[0] == '!') { - rc_stringlist_delete(deptype->services, depend + 1); + rc_stringlist_delete(deptype->services, + depend + 1); continue; } @@ -848,7 +833,8 @@ rc_deptree_update(void) /* If we're before something, remove us from the after list */ if (strcmp(type, "ibefore") == 0) { if ((dt = get_deptype(depinfo, "iafter"))) - rc_stringlist_delete(dt->services, depend); + rc_stringlist_delete(dt->services, + depend); } /* If we're after something, remove us from the before list */ if (strcmp(type, "iafter") == 0 || @@ -856,7 +842,8 @@ rc_deptree_update(void) strcmp(type, "iwant") == 0 || strcmp(type, "iuse") == 0) { if ((dt = get_deptype(depinfo, "ibefore"))) - rc_stringlist_delete(dt->services, depend); + rc_stringlist_delete(dt->services, + depend); } } } @@ -884,18 +871,35 @@ rc_deptree_update(void) if ((deptype = get_deptype(depinfo, "keyword"))) TAILQ_FOREACH(s, deptype->services, entries) if (strcmp(s->value, nosys) == 0 || - strcmp(s->value, onosys) == 0) - { - provide = get_deptype(depinfo, "iprovide"); - TAILQ_REMOVE(deptree, depinfo, entries); - TAILQ_FOREACH(di, deptree, entries) { - TAILQ_FOREACH_SAFE(dt, &di->depends, entries, dt_np) { - rc_stringlist_delete(dt->services, depinfo->service); + strcmp(s->value, onosys) == 0) { + provide = get_deptype( + depinfo, "iprovide"); + TAILQ_REMOVE(deptree, depinfo, + entries); + TAILQ_FOREACH(di, deptree, + entries) { + TAILQ_FOREACH_SAFE( + dt, + &di->depends, + entries, + dt_np) { + rc_stringlist_delete( + dt->services, + depinfo->service); if (provide) - TAILQ_FOREACH(s2, provide->services, entries) - rc_stringlist_delete(dt->services, s2->value); - if (!TAILQ_FIRST(dt->services)) { - TAILQ_REMOVE(&di->depends, dt, entries); + TAILQ_FOREACH( + s2, + provide->services, + entries) + rc_stringlist_delete( + dt->services, + s2->value); + if (!TAILQ_FIRST( + dt->services)) { + TAILQ_REMOVE( + &di->depends, + dt, + entries); free(dt->type); free(dt->services); free(dt); @@ -920,7 +924,8 @@ rc_deptree_update(void) di = xmalloc(sizeof(*di)); TAILQ_INIT(&di->depends); di->service = xstrdup(s->value); - TAILQ_INSERT_TAIL(providers, di, entries); + TAILQ_INSERT_TAIL(providers, di, + entries); } } TAILQ_CONCAT(deptree, providers, entries); @@ -935,19 +940,28 @@ rc_deptree_update(void) TAILQ_FOREACH(s, deptype->services, entries) { di = get_depinfo(deptree, s->value); if (!di) { - if (strcmp(deptype->type, "ineed") == 0) { + if (strcmp(deptype->type, "ineed") == + 0) { fprintf(stderr, - "Service `%s' needs non" - " existent service `%s'\n", - depinfo->service, s->value); - dt = get_deptype(depinfo, "broken"); + "Service `%s' needs non" + " existent service `%s'\n", + depinfo->service, + s->value); + dt = get_deptype(depinfo, + "broken"); if (!dt) { - dt = xmalloc(sizeof(*dt)); - dt->type = xstrdup("broken"); - dt->services = rc_stringlist_new(); - TAILQ_INSERT_TAIL(&depinfo->depends, dt, entries); + dt = xmalloc( + sizeof(*dt)); + dt->type = xstrdup( + "broken"); + dt->services = + rc_stringlist_new(); + TAILQ_INSERT_TAIL( + &depinfo->depends, + dt, entries); } - rc_stringlist_addu(dt->services, s->value); + rc_stringlist_addu(dt->services, + s->value); } continue; } @@ -957,13 +971,14 @@ rc_deptree_update(void) dt = xmalloc(sizeof(*dt)); dt->type = xstrdup(deppairs[i].addto); dt->services = rc_stringlist_new(); - TAILQ_INSERT_TAIL(&di->depends, dt, entries); + TAILQ_INSERT_TAIL(&di->depends, dt, + entries); } - rc_stringlist_addu(dt->services, depinfo->service); + rc_stringlist_addu(dt->services, + depinfo->service); } } - /* Phase 5 - Remove broken before directives */ types = rc_stringlist_new(); rc_stringlist_add(types, "ineed"); @@ -976,8 +991,8 @@ rc_deptree_update(void) continue; sorted = rc_stringlist_new(); visited = rc_stringlist_new(); - visit_service(deptree, types, sorted, visited, depinfo, - NULL, 0); + visit_service(deptree, types, sorted, visited, depinfo, NULL, + 0); rc_stringlist_free(visited); TAILQ_FOREACH_SAFE(s2, deptype->services, entries, s2_np) { TAILQ_FOREACH(s3, sorted, entries) { @@ -987,7 +1002,9 @@ rc_deptree_update(void) if (strcmp(s2->value, s3->value) == 0) { dt = get_deptype(di, "iafter"); if (dt) - rc_stringlist_delete(dt->services, depinfo->service); + rc_stringlist_delete( + dt->services, + depinfo->service); break; } dt = get_deptype(di, "iprovide"); @@ -1002,13 +1019,16 @@ rc_deptree_update(void) if (di) { dt = get_deptype(di, "iafter"); if (dt) - rc_stringlist_delete(dt->services, depinfo->service); + rc_stringlist_delete( + dt->services, + depinfo->service); } break; } } if (s3) - rc_stringlist_delete(deptype->services, s2->value); + rc_stringlist_delete(deptype->services, + s2->value); } rc_stringlist_free(sorted); } @@ -1019,11 +1039,11 @@ rc_deptree_update(void) TAILQ_FOREACH(depinfo, deptree, entries) { serrno = errno; errno = 0; - rc_stringlist_addu(dupes,depinfo->service); + rc_stringlist_addu(dupes, depinfo->service); if (errno == EEXIST) { fprintf(stderr, - "Error: %s is the name of a real and virtual service.\n", - depinfo->service); + "Error: %s is the name of a real and virtual service.\n", + depinfo->service); } errno = serrno; } @@ -1038,13 +1058,12 @@ rc_deptree_update(void) if ((fp = fopen(RC_DEPTREE_CACHE, "w"))) { i = 0; TAILQ_FOREACH(depinfo, deptree, entries) { - fprintf(fp, "depinfo_%zu_service='%s'\n", - i, depinfo->service); + fprintf(fp, "depinfo_%zu_service='%s'\n", i, + depinfo->service); TAILQ_FOREACH(deptype, &depinfo->depends, entries) { k = 0; TAILQ_FOREACH(s, deptype->services, entries) { - fprintf(fp, - "depinfo_%zu_%s_%zu='%s'\n", + fprintf(fp, "depinfo_%zu_%s_%zu='%s'\n", i, deptype->type, k, s->value); k++; } @@ -1053,8 +1072,8 @@ rc_deptree_update(void) } fclose(fp); } else { - fprintf(stderr, "fopen `%s': %s\n", - RC_DEPTREE_CACHE, strerror(errno)); + fprintf(stderr, "fopen `%s': %s\n", RC_DEPTREE_CACHE, + strerror(errno)); retval = false; } @@ -1065,8 +1084,8 @@ rc_deptree_update(void) fprintf(fp, "%s\n", s->value); fclose(fp); } else { - fprintf(stderr, "fopen `%s': %s\n", - RC_DEPCONFIG, strerror(errno)); + fprintf(stderr, "fopen `%s': %s\n", RC_DEPCONFIG, + strerror(errno)); retval = false; } } else { diff --git a/src/librc/librc-misc.c b/src/librc/librc-misc.c index 9dfbaec0e..217c350d9 100644 --- a/src/librc/librc-misc.c +++ b/src/librc/librc-misc.c @@ -18,42 +18,35 @@ #include #include -#include "queue.h" -#include "librc.h" #include "helpers.h" +#include "librc.h" +#include "queue.h" -bool -rc_yesno(const char *value) +bool rc_yesno(const char *value) { if (!value) { errno = ENOENT; return false; } - if (strcasecmp(value, "yes") == 0 || - strcasecmp(value, "y") == 0 || - strcasecmp(value, "true") == 0 || - strcasecmp(value, "1") == 0) + if (strcasecmp(value, "yes") == 0 || strcasecmp(value, "y") == 0 || + strcasecmp(value, "true") == 0 || strcasecmp(value, "1") == 0) return true; - if (strcasecmp(value, "no") != 0 && - strcasecmp(value, "n") != 0 && - strcasecmp(value, "false") != 0 && - strcasecmp(value, "0") != 0) + if (strcasecmp(value, "no") != 0 && strcasecmp(value, "n") != 0 && + strcasecmp(value, "false") != 0 && strcasecmp(value, "0") != 0) errno = EINVAL; return false; } - /** * Read the entire @file into the buffer and set @len to the * size of the buffer when finished. For C strings, this will * be strlen(buffer) + 1. * Don't forget to free the buffer afterwards! */ -bool -rc_getfile(const char *file, char **buffer, size_t *len) +bool rc_getfile(const char *file, char **buffer, size_t *len) { bool ret = false; FILE *fp; @@ -82,7 +75,7 @@ rc_getfile(const char *file, char **buffer, size_t *len) } ret = true; - finished: +finished: if (!ret) { free(*buffer); *len = 0; @@ -92,8 +85,7 @@ rc_getfile(const char *file, char **buffer, size_t *len) return ret; } -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; @@ -116,8 +108,7 @@ rc_getline(char **line, size_t *len, FILE *fp) return last; } -char * -rc_proc_getent(const char *ent _unused) +char *rc_proc_getent(const char *ent _unused) { #ifdef __linux__ FILE *fp; @@ -139,7 +130,9 @@ rc_proc_getent(const char *ent _unused) len = strlen(ent); while ((p = strsep(&proc, " "))) { - if (strncmp(ent, p, len) == 0 && (p[len] == '\0' || p[len] == ' ' || p[len] == '=')) { + if (strncmp(ent, p, len) == 0 && + (p[len] == '\0' || p[len] == ' ' || + p[len] == '=')) { p += len; if (*p == '=') @@ -162,8 +155,7 @@ rc_proc_getent(const char *ent _unused) #endif } -RC_STRINGLIST * -rc_config_list(const char *file) +RC_STRINGLIST *rc_config_list(const char *file) { FILE *fp; char *buffer = NULL; @@ -243,7 +235,8 @@ static void rc_config_set_value(RC_STRINGLIST *config, char *value) any prior values we may already have */ TAILQ_FOREACH(cline, config, entries) { i = strlen(entry); - if (strncmp(entry, cline->value, i) == 0 && cline->value[i] == '=') { + if (strncmp(entry, cline->value, i) == 0 && + cline->value[i] == '=') { /* We have a match now - to save time we directly replace it */ free(cline->value); cline->value = newline; @@ -298,8 +291,9 @@ static RC_STRINGLIST *rc_config_kcl(RC_STRINGLIST *config) * duplicates */ TAILQ_FOREACH_SAFE(cline, config, entries, config_np) { - if (strncmp(override->value, cline->value, varlen) == 0 - && cline->value[varlen] == '=') { + if (strncmp(override->value, cline->value, varlen) == + 0 && + cline->value[varlen] == '=') { rc_stringlist_delete(config, cline->value); break; } @@ -317,7 +311,7 @@ static RC_STRINGLIST *rc_config_kcl(RC_STRINGLIST *config) return config; } -static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config) +static RC_STRINGLIST *rc_config_directory(RC_STRINGLIST *config) { DIR *dp; struct dirent *d; @@ -344,7 +338,8 @@ static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config) rc_conf_d_list = rc_config_list(path); TAILQ_FOREACH(line, rc_conf_d_list, entries) if (line->value) - rc_config_set_value(config, line->value); + rc_config_set_value( + config, line->value); rc_stringlist_free(rc_conf_d_list); } rc_stringlist_free(rc_conf_d_files); @@ -353,8 +348,7 @@ static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config) return config; } -RC_STRINGLIST * -rc_config_load(const char *file) +RC_STRINGLIST *rc_config_load(const char *file) { RC_STRINGLIST *list; RC_STRINGLIST *config; @@ -370,8 +364,7 @@ rc_config_load(const char *file) return config; } -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; @@ -381,7 +374,8 @@ rc_config_value(RC_STRINGLIST *list, const char *entry) TAILQ_FOREACH(line, list, entries) { p = strchr(line->value, '='); if (p != NULL) { - if (strncmp(entry, line->value, len) == 0 && line->value[len] == '=') + if (strncmp(entry, line->value, len) == 0 && + line->value[len] == '=') return ++p; } } @@ -392,14 +386,12 @@ rc_config_value(RC_STRINGLIST *list, const char *entry) * each rc_conf_value call */ static RC_STRINGLIST *rc_conf = NULL; -static void -_free_rc_conf(void) +static void _free_rc_conf(void) { rc_stringlist_free(rc_conf); } -char * -rc_conf_value(const char *setting) +char *rc_conf_value(const char *setting) { RC_STRINGLIST *old; RC_STRING *s; diff --git a/src/librc/librc-stringlist.c b/src/librc/librc-stringlist.c index ee352c5f0..a09890306 100644 --- a/src/librc/librc-stringlist.c +++ b/src/librc/librc-stringlist.c @@ -15,19 +15,17 @@ * except according to the terms contained in the LICENSE file. */ -#include "queue.h" #include "librc.h" +#include "queue.h" -RC_STRINGLIST * -rc_stringlist_new(void) +RC_STRINGLIST *rc_stringlist_new(void) { RC_STRINGLIST *l = xmalloc(sizeof(*l)); TAILQ_INIT(l); return l; } -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)); @@ -36,52 +34,48 @@ rc_stringlist_add(RC_STRINGLIST *list, const char *value) return s; } -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); } -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; } -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; } -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); @@ -94,8 +88,7 @@ rc_stringlist_split(const char *value, const char *sep) return list; } -void -rc_stringlist_sort(RC_STRINGLIST **list) +void rc_stringlist_sort(RC_STRINGLIST **list) { RC_STRINGLIST *l = *list; RC_STRINGLIST *new = rc_stringlist_new(); @@ -123,8 +116,7 @@ rc_stringlist_sort(RC_STRINGLIST **list) *list = new; } -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 7802cc413..8b0170b2a 100644 --- a/src/librc/librc.c +++ b/src/librc/librc.c @@ -15,17 +15,17 @@ * except according to the terms contained in the LICENSE file. */ -#include "queue.h" #include "librc.h" +#include "queue.h" #include #ifdef __FreeBSD__ -# include +#include #endif -#define RC_RUNLEVEL RC_SVCDIR "/softlevel" +#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 */ @@ -39,23 +39,21 @@ typedef struct rc_service_state_name { /* We MUST list the states below 0x10 first * The rest can be in any order */ static const rc_service_state_name_t rc_service_state_names[] = { - { RC_SERVICE_STARTED, "started" }, - { RC_SERVICE_STOPPED, "stopped" }, - { RC_SERVICE_STARTING, "starting" }, - { RC_SERVICE_STOPPING, "stopping" }, - { RC_SERVICE_INACTIVE, "inactive" }, - { RC_SERVICE_WASINACTIVE, "wasinactive" }, - { RC_SERVICE_HOTPLUGGED, "hotplugged" }, - { RC_SERVICE_FAILED, "failed" }, - { RC_SERVICE_SCHEDULED, "scheduled"}, - { RC_SERVICE_CRASHED, "crashed"}, - { 0, NULL} -}; - -#define LS_INITD 0x01 -#define LS_DIR 0x02 -static RC_STRINGLIST * -ls_dir(const char *dir, int options) + {RC_SERVICE_STARTED, "started"}, + {RC_SERVICE_STOPPED, "stopped"}, + {RC_SERVICE_STARTING, "starting"}, + {RC_SERVICE_STOPPING, "stopping"}, + {RC_SERVICE_INACTIVE, "inactive"}, + {RC_SERVICE_WASINACTIVE, "wasinactive"}, + {RC_SERVICE_HOTPLUGGED, "hotplugged"}, + {RC_SERVICE_FAILED, "failed"}, + {RC_SERVICE_SCHEDULED, "scheduled"}, + {RC_SERVICE_CRASHED, "crashed"}, + {0, NULL}}; + +#define LS_INITD 0x01 +#define LS_DIR 0x02 +static RC_STRINGLIST *ls_dir(const char *dir, int options) { DIR *dp; struct dirent *d; @@ -74,8 +72,8 @@ ls_dir(const char *dir, int options) /* Check that our file really exists. * 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); + snprintf(file, sizeof(file), "%s/%s", dir, + d->d_name); r = stat(file, &buf); if (r != 0) continue; @@ -88,8 +86,8 @@ ls_dir(const char *dir, int options) continue; } if (options & LS_DIR) { - snprintf(file, sizeof(file), "%s/%s", - dir, d->d_name); + snprintf(file, sizeof(file), "%s/%s", dir, + d->d_name); if (stat(file, &buf) != 0 || !S_ISDIR(buf.st_mode)) continue; @@ -101,8 +99,7 @@ ls_dir(const char *dir, int options) return list; } -static bool -rm_dir(const char *pathname, bool top) +static bool rm_dir(const char *pathname, bool top) { DIR *dp; struct dirent *d; @@ -116,17 +113,15 @@ rm_dir(const char *pathname, bool top) errno = 0; while (((d = readdir(dp)) != NULL) && errno == 0) { if (strcmp(d->d_name, ".") != 0 && - strcmp(d->d_name, "..") != 0) - { - snprintf(file, sizeof(file), - "%s/%s", pathname, d->d_name); + strcmp(d->d_name, "..") != 0) { + snprintf(file, sizeof(file), "%s/%s", pathname, + d->d_name); if (stat(file, &s) != 0) { retval = false; break; } if (S_ISDIR(s.st_mode)) { - if (!rm_dir(file, true)) - { + if (!rm_dir(file, true)) { retval = false; break; } @@ -151,8 +146,7 @@ rm_dir(const char *pathname, bool top) /* Other systems may need this at some point, but for now it's Linux only */ #ifdef __linux__ -static bool -file_regex(const char *file, const char *regex) +static bool file_regex(const char *file, const char *regex) { FILE *fp; char *line = NULL; @@ -197,25 +191,22 @@ file_regex(const char *file, const char *regex) } #endif - -static const char * -get_systype(void) +static const char *get_systype(void) { char *systype = rc_conf_value("rc_sys"); if (systype) { char *s = systype; /* Convert to uppercase */ while (s && *s) { - if (islower((unsigned char) *s)) - *s = toupper((unsigned char) *s); + if (islower((unsigned char)*s)) + *s = toupper((unsigned char)*s); s++; } } return systype; } -static const char * -detect_prefix(const char *systype) +static const char *detect_prefix(const char *systype) { #ifdef PREFIX return RC_SYS_PREFIX; @@ -231,13 +222,12 @@ detect_prefix(const char *systype) #endif } -static const char * -detect_container(const char *systype _unused) +static const char *detect_container(const char *systype _unused) { #ifdef __FreeBSD__ if (systype) { if (strcmp(systype, RC_SYS_NONE) == 0) - return NULL; + return NULL; if (strcmp(systype, RC_SYS_JAIL) == 0) return RC_SYS_JAIL; } @@ -263,21 +253,20 @@ detect_container(const char *systype _unused) if (strcmp(systype, RC_SYS_LXC) == 0) return RC_SYS_LXC; if (strcmp(systype, RC_SYS_RKT) == 0) - return RC_SYS_RKT; + return RC_SYS_RKT; if (strcmp(systype, RC_SYS_SYSTEMD_NSPAWN) == 0) - return RC_SYS_SYSTEMD_NSPAWN; + return RC_SYS_SYSTEMD_NSPAWN; if (strcmp(systype, RC_SYS_DOCKER) == 0) - return RC_SYS_DOCKER; + return RC_SYS_DOCKER; } 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]")) + else if (file_regex("/proc/self/status", "envID:[[:space:]]*[1-9]")) return RC_SYS_OPENVZ; /* old test */ else if (file_regex("/proc/1/environ", "container=lxc")) return RC_SYS_LXC; @@ -295,8 +284,7 @@ detect_container(const char *systype _unused) return NULL; } -static const char * -detect_vm(const char *systype _unused) +static const char *detect_vm(const char *systype _unused) { #ifdef __NetBSD__ if (systype) { @@ -332,8 +320,7 @@ detect_vm(const char *systype _unused) return NULL; } -const char * -rc_sys(void) +const char *rc_sys(void) { const char *systype; const char *sys; @@ -350,8 +337,7 @@ rc_sys(void) return sys; } -static const char * -rc_parse_service_state(RC_SERVICE state) +static const char *rc_parse_service_state(RC_SERVICE state) { int i; @@ -365,8 +351,8 @@ rc_parse_service_state(RC_SERVICE state) /* Returns a list of all the chained runlevels used by the * specified runlevel in dependency order, including the * specified runlevel. */ -static void -get_runlevel_chain(const char *runlevel, RC_STRINGLIST *level_list, RC_STRINGLIST *ancestor_list) +static void get_runlevel_chain(const char *runlevel, RC_STRINGLIST *level_list, + RC_STRINGLIST *ancestor_list) { char path[PATH_MAX]; RC_STRINGLIST *dirs; @@ -400,8 +386,9 @@ get_runlevel_chain(const char *runlevel, RC_STRINGLIST *level_list, RC_STRINGLIS /* Check for loop */ if (rc_stringlist_find(ancestor_list, nextlevel)) { - fprintf(stderr, "Loop detected in stacked runlevels attempting to enter runlevel %s!\n", - nextlevel); + fprintf(stderr, + "Loop detected in stacked runlevels attempting to enter runlevel %s!\n", + nextlevel); fprintf(stderr, "Ancestors:\n"); TAILQ_FOREACH(parent, ancestor_list, entries) fprintf(stderr, "\t%s\n", parent->value); @@ -418,14 +405,12 @@ get_runlevel_chain(const char *runlevel, RC_STRINGLIST *level_list, RC_STRINGLIS rc_stringlist_free(dirs); } -bool -rc_runlevel_starting(void) +bool rc_runlevel_starting(void) { return exists(RC_STARTING); } -bool -rc_runlevel_stopping(void) +bool rc_runlevel_stopping(void) { return exists(RC_STOPPING); } @@ -435,8 +420,7 @@ RC_STRINGLIST *rc_runlevel_list(void) return ls_dir(RC_RUNLEVELDIR, LS_DIR); } -char * -rc_runlevel_get(void) +char *rc_runlevel_get(void) { FILE *fp; char *runlevel = NULL; @@ -461,8 +445,7 @@ rc_runlevel_get(void) return runlevel; } -bool -rc_runlevel_set(const char *runlevel) +bool rc_runlevel_set(const char *runlevel) { FILE *fp = fopen(RC_RUNLEVEL, "w"); @@ -473,14 +456,13 @@ rc_runlevel_set(const char *runlevel) return true; } -bool -rc_runlevel_exists(const char *runlevel) +bool rc_runlevel_exists(const char *runlevel) { char path[PATH_MAX]; struct stat buf; - if (!runlevel || strcmp(runlevel, "") == 0 || strcmp(runlevel, ".") == 0 || - strcmp(runlevel, "..") == 0) + if (!runlevel || strcmp(runlevel, "") == 0 || + strcmp(runlevel, ".") == 0 || strcmp(runlevel, "..") == 0) return false; snprintf(path, sizeof(path), "%s/%s", RC_RUNLEVELDIR, runlevel); if (stat(path, &buf) == 0 && S_ISDIR(buf.st_mode)) @@ -488,8 +470,7 @@ rc_runlevel_exists(const char *runlevel) return false; } -bool -rc_runlevel_stack(const char *dst, const char *src) +bool rc_runlevel_stack(const char *dst, const char *src) { char d[PATH_MAX], s[PATH_MAX]; @@ -500,8 +481,7 @@ rc_runlevel_stack(const char *dst, const char *src) return (symlink(s, d) == 0 ? true : false); } -bool -rc_runlevel_unstack(const char *dst, const char *src) +bool rc_runlevel_unstack(const char *dst, const char *src) { char path[PATH_MAX]; @@ -509,8 +489,7 @@ rc_runlevel_unstack(const char *dst, const char *src) return (unlink(path) == 0 ? true : false); } -RC_STRINGLIST * -rc_runlevel_stacks(const char *runlevel) +RC_STRINGLIST *rc_runlevel_stacks(const char *runlevel) { RC_STRINGLIST *stack, *ancestor_list; stack = rc_stringlist_new(); @@ -522,8 +501,7 @@ rc_runlevel_stacks(const char *runlevel) } /* Resolve a service name to its full path */ -char * -rc_service_resolve(const char *service) +char *rc_service_resolve(const char *service) { char buffer[PATH_MAX]; char file[PATH_MAX]; @@ -539,15 +517,15 @@ rc_service_resolve(const char *service) /* First check started services */ 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); + snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", "inactive", + service); if (lstat(file, &buf) || !S_ISLNK(buf.st_mode)) *file = '\0'; } if (*file) { memset(buffer, 0, sizeof(buffer)); - r = readlink(file, buffer, sizeof(buffer)-1); + r = readlink(file, buffer, sizeof(buffer) - 1); if (r > 0) return xstrdup(buffer); } @@ -574,8 +552,7 @@ rc_service_resolve(const char *service) return NULL; } -bool -rc_service_exists(const char *service) +bool rc_service_exists(const char *service) { char *file; bool retval = false; @@ -590,8 +567,7 @@ rc_service_exists(const char *service) len = strlen(service); /* .sh files are not init scripts */ - if (len > 2 && service[len - 3] == '.' && - service[len - 2] == 's' && + if (len > 2 && service[len - 3] == '.' && service[len - 2] == 's' && service[len - 1] == 'h') { errno = EINVAL; return false; @@ -613,10 +589,9 @@ rc_service_exists(const char *service) } #define OPTSTR \ -". '%s'; echo $extra_commands $extra_started_commands $extra_stopped_commands" + ". '%s'; echo $extra_commands $extra_started_commands $extra_stopped_commands" -RC_STRINGLIST * -rc_service_extra_commands(const char *service) +RC_STRINGLIST *rc_service_extra_commands(const char *service) { char *svc; char *cmd = NULL; @@ -654,8 +629,7 @@ rc_service_extra_commands(const char *service) } #define DESCSTR ". '%s'; echo \"${description%s%s}\"" -char * -rc_service_description(const char *service, const char *option) +char *rc_service_description(const char *service, const char *option) { char *svc; char *cmd; @@ -682,18 +656,16 @@ rc_service_description(const char *service, const char *option) return desc; } -bool -rc_service_in_runlevel(const char *service, const char *runlevel) +bool 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)); + snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", runlevel, + basename_c(service)); return exists(file); } -bool -rc_service_mark(const char *service, const RC_SERVICE state) +bool rc_service_mark(const char *service, const RC_SERVICE state) { char file[PATH_MAX]; int i = 0; @@ -718,7 +690,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); @@ -738,23 +710,21 @@ rc_service_mark(const char *service, const RC_SERVICE state) for (i = 0; rc_service_state_names[i].name; i++) { s = rc_service_state_names[i].state; - if ((s != skip_state && - s != RC_SERVICE_STOPPED && - s != RC_SERVICE_HOTPLUGGED && - s != RC_SERVICE_SCHEDULED) && - (!skip_wasinactive || s != RC_SERVICE_WASINACTIVE)) - { + if ((s != skip_state && 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) && - s == RC_SERVICE_INACTIVE) - { - snprintf(was, sizeof(was), - RC_SVCDIR "/%s/%s", - rc_parse_service_state(RC_SERVICE_WASINACTIVE), - base); + state == RC_SERVICE_STOPPING) && + s == RC_SERVICE_INACTIVE) { + snprintf( + was, sizeof(was), + RC_SVCDIR "/%s/%s", + rc_parse_service_state( + RC_SERVICE_WASINACTIVE), + base); if (symlink(init, was) == -1) return false; skip_wasinactive = true; @@ -768,23 +738,21 @@ rc_service_mark(const char *service, const RC_SERVICE state) } /* Remove the exclusive state if we're inactive */ - if (state == RC_SERVICE_STARTED || - state == RC_SERVICE_STOPPED || - state == RC_SERVICE_INACTIVE) - { - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "exclusive", base); + if (state == RC_SERVICE_STARTED || state == RC_SERVICE_STOPPED || + state == RC_SERVICE_INACTIVE) { + snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", "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); + snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", "options", + base); rm_dir(file, true); - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "daemons", base); + snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", "daemons", + base); rm_dir(file, true); rc_service_schedule_clear(service); @@ -795,8 +763,8 @@ rc_service_mark(const char *service, const RC_SERVICE state) snprintf(file, sizeof(file), RC_SVCDIR "/%s", "scheduled"); dirs = ls_dir(file, 0); TAILQ_FOREACH(dir, dirs, entries) { - snprintf(was, sizeof(was), "%s/%s/%s", - file, dir->value, base); + snprintf(was, sizeof(was), "%s/%s/%s", file, dir->value, + base); unlink(was); /* Try and remove the dir; we don't care about errors */ @@ -823,7 +791,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; @@ -840,8 +808,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; @@ -853,23 +821,21 @@ rc_service_state(const char *service) return state; } -char * -rc_service_value_get(const char *service, const char *option) +char *rc_service_value_get(const char *service, const char *option) { char *buffer = NULL; size_t len = 0; char file[PATH_MAX]; - snprintf(file, sizeof(file), RC_SVCDIR "/options/%s/%s", - service, option); + snprintf(file, sizeof(file), RC_SVCDIR "/options/%s/%s", service, + option); rc_getfile(file, &buffer, &len); return buffer; } -bool -rc_service_value_set(const char *service, const char *option, - const char *value) +bool rc_service_value_set(const char *service, const char *option, + const char *value) { FILE *fp; char file[PATH_MAX]; @@ -888,12 +854,11 @@ rc_service_value_set(const char *service, const char *option, } else { unlink(file); } - return true; + return true; } - -bool -rc_service_schedule_start(const char *service, const char *service_to_start) +bool rc_service_schedule_start(const char *service, + const char *service_to_start) { char file[PATH_MAX]; char *p = file; @@ -905,32 +870,30 @@ 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)); + snprintf(p, sizeof(file) - (p - file), "/%s", + basename_c(service_to_start)); retval = (exists(file) || symlink(init, file) == 0); free(init); return retval; } -bool -rc_service_schedule_clear(const char *service) +bool 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; } -RC_STRINGLIST * -rc_services_in_runlevel(const char *runlevel) +RC_STRINGLIST *rc_services_in_runlevel(const char *runlevel) { char dir[PATH_MAX]; RC_STRINGLIST *list = NULL; @@ -966,8 +929,7 @@ rc_services_in_runlevel(const char *runlevel) return list; } -RC_STRINGLIST * -rc_services_in_runlevel_stacked(const char *runlevel) +RC_STRINGLIST *rc_services_in_runlevel_stacked(const char *runlevel) { RC_STRINGLIST *list, *stacks, *sl; RC_STRING *stack; @@ -983,8 +945,7 @@ rc_services_in_runlevel_stacked(const char *runlevel) return list; } -RC_STRINGLIST * -rc_services_in_state(RC_SERVICE state) +RC_STRINGLIST *rc_services_in_state(RC_SERVICE state) { RC_STRINGLIST *services; RC_STRINGLIST *list; @@ -994,7 +955,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); @@ -1016,13 +977,12 @@ rc_services_in_state(RC_SERVICE state) return list; } -bool -rc_service_add(const char *runlevel, const char *service) +bool rc_service_add(const char *runlevel, const char *service) { bool retval; char *init; char file[PATH_MAX]; - char path[MAXPATHLEN] = { '\0' }; + char path[MAXPATHLEN] = {'\0'}; char binit[PATH_MAX]; char *i; @@ -1037,8 +997,8 @@ 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)); + snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", runlevel, + basename_c(service)); /* We need to ensure that only things in /etc/init.d are added * to the boot runlevel */ @@ -1061,20 +1021,18 @@ rc_service_add(const char *runlevel, const char *service) return retval; } -bool -rc_service_delete(const char *runlevel, const char *service) +bool 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)); + snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", runlevel, + basename_c(service)); if (unlink(file) == 0) return true; return false; } -RC_STRINGLIST * -rc_services_scheduled_by(const char *service) +RC_STRINGLIST *rc_services_scheduled_by(const char *service) { RC_STRINGLIST *dirs = ls_dir(RC_SVCDIR "/scheduled", 0); RC_STRINGLIST *list = rc_stringlist_new(); @@ -1083,7 +1041,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); } @@ -1091,12 +1049,11 @@ rc_services_scheduled_by(const char *service) return list; } -RC_STRINGLIST * -rc_services_scheduled(const char *service) +RC_STRINGLIST *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); } diff --git a/src/librc/librc.h b/src/librc/librc.h index 09ab92761..99cb15a5b 100644 --- a/src/librc/librc.h +++ b/src/librc/librc.h @@ -20,10 +20,10 @@ #define _IN_LIBRC -#include #include #include #include +#include #include #include @@ -45,15 +45,15 @@ #include #if defined(BSD) && !defined(__GNU__) +#include #include -#include #include -#include +#include #else #include #endif -#include "rc.h" #include "misc.h" +#include "rc.h" #endif diff --git a/src/librc/rc.h.in b/src/librc/rc.h.in index 69d9d0e47..ef93c6df0 100644 --- a/src/librc/rc.h.in +++ b/src/librc/rc.h.in @@ -13,9 +13,9 @@ #ifndef __RC_H__ #define __RC_H__ -#include #include #include +#include /* __BEGIN_DECLS */ #ifdef __cplusplus @@ -23,47 +23,48 @@ extern "C" { #endif #define RC_PREFIX "@PREFIX@" -#define RC_SYSCONFDIR "@SYSCONFDIR@" -#define RC_LIBDIR "@PREFIX@/@LIB@/rc" -#define RC_LIBEXECDIR "@LIBEXECDIR@" +#define RC_SYSCONFDIR "@SYSCONFDIR@" +#define RC_LIBDIR "@PREFIX@/@LIB@/rc" +#define RC_LIBEXECDIR "@LIBEXECDIR@" #if defined(PREFIX) -#define RC_SVCDIR RC_LIBEXECDIR "/init.d" -#elif defined(__linux__) || (defined(__FreeBSD_kernel__) && \ - defined(__GLIBC__)) || defined(__GNU__) -#define RC_SVCDIR "/run/openrc" +#define RC_SVCDIR RC_LIBEXECDIR "/init.d" +#elif defined(__linux__) || \ + (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) || \ + defined(__GNU__) +#define RC_SVCDIR "/run/openrc" #else -#define RC_SVCDIR RC_LIBEXECDIR "/init.d" +#define RC_SVCDIR RC_LIBEXECDIR "/init.d" #endif -#define RC_RUNLEVELDIR RC_SYSCONFDIR "/runlevels" -#define RC_INITDIR RC_SYSCONFDIR "/init.d" -#define RC_CONFDIR RC_SYSCONFDIR "/conf.d" -#define RC_PLUGINDIR RC_LIBDIR "/plugins" +#define RC_RUNLEVELDIR RC_SYSCONFDIR "/runlevels" +#define RC_INITDIR RC_SYSCONFDIR "/init.d" +#define RC_CONFDIR RC_SYSCONFDIR "/conf.d" +#define RC_PLUGINDIR RC_LIBDIR "/plugins" -#define RC_INIT_FIFO RC_SVCDIR"/init.ctl" -#define RC_PROFILE_ENV RC_SYSCONFDIR "/profile.env" -#define RC_SYS_WHITELIST RC_LIBEXECDIR "/conf.d/env_whitelist" -#define RC_USR_WHITELIST RC_SYSCONFDIR "/conf.d/env_whitelist" -#define RC_CONF RC_SYSCONFDIR "/rc.conf" -#define RC_CONF_D RC_SYSCONFDIR "/rc.conf.d" -#define RC_CONF_OLD RC_SYSCONFDIR "/conf.d/rc" +#define RC_INIT_FIFO RC_SVCDIR "/init.ctl" +#define RC_PROFILE_ENV RC_SYSCONFDIR "/profile.env" +#define RC_SYS_WHITELIST RC_LIBEXECDIR "/conf.d/env_whitelist" +#define RC_USR_WHITELIST RC_SYSCONFDIR "/conf.d/env_whitelist" +#define RC_CONF RC_SYSCONFDIR "/rc.conf" +#define RC_CONF_D RC_SYSCONFDIR "/rc.conf.d" +#define RC_CONF_OLD RC_SYSCONFDIR "/conf.d/rc" -#define RC_PATH_PREFIX RC_LIBEXECDIR "/bin:/bin:/sbin:/usr/bin:/usr/sbin" +#define RC_PATH_PREFIX RC_LIBEXECDIR "/bin:/bin:/sbin:/usr/bin:/usr/sbin" /* PKG_PREFIX is where packages are installed if different from the base OS * On Gentoo this is normally unset, on FreeBSD /usr/local and on NetBSD * /usr/pkg. */ -#define RC_PKG_PREFIX "@PKG_PREFIX@" +#define RC_PKG_PREFIX "@PKG_PREFIX@" #ifdef RC_PKG_PREFIX -# define RC_PKG_INITDIR RC_PKG_PREFIX "/etc/init.d" -# define RC_PKG_CONFDIR RC_PKG_PREFIX "/etc/conf.d" +#define RC_PKG_INITDIR RC_PKG_PREFIX "/etc/init.d" +#define RC_PKG_CONFDIR RC_PKG_PREFIX "/etc/conf.d" #endif /* LOCAL_PREFIX is for user written stuff, which the base OS and package * manger don't touch. */ -#define RC_LOCAL_PREFIX "@LOCAL_PREFIX@" +#define RC_LOCAL_PREFIX "@LOCAL_PREFIX@" #ifdef RC_LOCAL_PREFIX -# define RC_LOCAL_INITDIR RC_LOCAL_PREFIX "/etc/init.d" -# define RC_LOCAL_CONFDIR RC_LOCAL_PREFIX "/etc/conf.d" +#define RC_LOCAL_INITDIR RC_LOCAL_PREFIX "/etc/init.d" +#define RC_LOCAL_CONFDIR RC_LOCAL_PREFIX "/etc/conf.d" #endif #ifndef _SYS_QUEUE_H_ @@ -75,39 +76,44 @@ extern "C" { /* * List definitions. */ -#define LIST_HEAD(name, type) \ -struct name { \ - struct type *lh_first; /* first element */ \ -} - -#define LIST_HEAD_INITIALIZER(head) \ - { NULL } - -#define LIST_ENTRY(type) \ -struct { \ - struct type *le_next; /* next element */ \ - struct type **le_prev; /* address of previous next element */ \ -} +#define LIST_HEAD(name, type) \ + struct name { \ + struct type *lh_first; /* first element */ \ + } + +#define LIST_HEAD_INITIALIZER(head) \ + { \ + NULL \ + } + +#define LIST_ENTRY(type) \ + struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ + } /* * Tail queue definitions. */ -#define _TAILQ_HEAD(name, type, qual) \ -struct name { \ - qual type *tqh_first; /* first element */ \ - qual type *qual *tqh_last; /* addr of last next element */ \ -} -#define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type,) - -#define TAILQ_HEAD_INITIALIZER(head) \ - { TAILQ_END(head), &(head).tqh_first } - -#define _TAILQ_ENTRY(type, qual) \ -struct { \ - qual type *tqe_next; /* next element */ \ - qual type *qual *tqe_prev; /* address of previous next element */\ -} -#define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type,) +#define _TAILQ_HEAD(name, type, qual) \ + struct name { \ + qual type *tqh_first; /* first element */ \ + qual type *qual *tqh_last; /* addr of last next element */ \ + } +#define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type, ) + +#define TAILQ_HEAD_INITIALIZER(head) \ + { \ + TAILQ_END(head), &(head).tqh_first \ + } + +#define _TAILQ_ENTRY(type, qual) \ + struct { \ + qual type *tqe_next; /* next element */ \ + qual type *qual \ + *tqe_prev; /* address of previous next element */ \ + } +#define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type, ) #endif /* _SYS_QUEUE_H_ */ @@ -119,9 +125,9 @@ typedef struct rc_string { typedef TAILQ_HEAD(rc_stringlist, rc_string) RC_STRINGLIST; /*! @name Reserved runlevel names */ -#define RC_LEVEL_SYSINIT "sysinit" -#define RC_LEVEL_SINGLE "single" -#define RC_LEVEL_SHUTDOWN "shutdown" +#define RC_LEVEL_SYSINIT "sysinit" +#define RC_LEVEL_SINGLE "single" +#define RC_LEVEL_SHUTDOWN "shutdown" /*! Return the current runlevel. * @return the current runlevel */ @@ -172,24 +178,23 @@ bool rc_runlevel_stopping(void); * This should allow the use if local init.d directories in the future. */ /*! @brief States a service can be in */ -typedef enum -{ +typedef enum { /* These are actual states * The service has to be in one only at all times */ - RC_SERVICE_STOPPED = 0x0001, - RC_SERVICE_STARTED = 0x0002, - RC_SERVICE_STOPPING = 0x0004, - RC_SERVICE_STARTING = 0x0008, - RC_SERVICE_INACTIVE = 0x0010, + RC_SERVICE_STOPPED = 0x0001, + RC_SERVICE_STARTED = 0x0002, + RC_SERVICE_STOPPING = 0x0004, + RC_SERVICE_STARTING = 0x0008, + RC_SERVICE_INACTIVE = 0x0010, /* Service may or may not have been hotplugged */ RC_SERVICE_HOTPLUGGED = 0x0100, /* Optional states service could also be in */ - RC_SERVICE_FAILED = 0x0200, - RC_SERVICE_SCHEDULED = 0x0400, + RC_SERVICE_FAILED = 0x0200, + RC_SERVICE_SCHEDULED = 0x0400, RC_SERVICE_WASINACTIVE = 0x0800, - RC_SERVICE_CRASHED = 0x1000, + RC_SERVICE_CRASHED = 0x1000, } RC_SERVICE; /*! Add the service to the runlevel @@ -210,8 +215,8 @@ bool rc_service_delete(const char *, const char *); * @param name of the process (optional) * @param pidfile of the process (optional) * @param started if true, add the arguments otherwise remove existing matching arguments */ -bool rc_service_daemon_set(const char *, const char *, const char *const *, const char *, - bool); +bool rc_service_daemon_set(const char *, const char *, const char *const *, + const char *, bool); /*! Returns a description of what the service and/or option does. * @param service to check @@ -273,8 +278,8 @@ RC_SERVICE rc_service_state(const char *); * @param argv to check * @param indx of the daemon (optional - 1st daemon, 2nd daemon, etc) * @return true if started by this service, otherwise false */ -bool rc_service_started_daemon(const char *, const char *, - const char *const *, int); +bool rc_service_started_daemon(const char *, const char *, const char *const *, + int); /*! Return a saved value for a service * @param service to check @@ -318,18 +323,18 @@ bool rc_service_daemons_crashed(const char *); /*! @name System types * OpenRC can support some special sub system types, normally virtualization. * Some services cannot work in these systems, or we do something else. */ -#define RC_SYS_DOCKER "DOCKER" -#define RC_SYS_JAIL "JAIL" -#define RC_SYS_NONE "" -#define RC_SYS_OPENVZ "OPENVZ" -#define RC_SYS_LXC "LXC" -#define RC_SYS_PREFIX "PREFIX" -#define RC_SYS_RKT "RKT" +#define RC_SYS_DOCKER "DOCKER" +#define RC_SYS_JAIL "JAIL" +#define RC_SYS_NONE "" +#define RC_SYS_OPENVZ "OPENVZ" +#define RC_SYS_LXC "LXC" +#define RC_SYS_PREFIX "PREFIX" +#define RC_SYS_RKT "RKT" #define RC_SYS_SYSTEMD_NSPAWN "SYSTEMD-NSPAWN" -#define RC_SYS_UML "UML" +#define RC_SYS_UML "UML" #define RC_SYS_VSERVER "VSERVER" -#define RC_SYS_XEN0 "XEN0" -#define RC_SYS_XENU "XENU" +#define RC_SYS_XEN0 "XEN0" +#define RC_SYS_XENU "XENU" /*! Returns the type of subsystem * @return string from RC_SYS_* types or NULL if none detected */ @@ -339,13 +344,13 @@ const char *rc_sys(void); * These options can change the services found by the rc_get_depinfo and * rc_get_depends functions. */ /*! Trace provided services */ -#define RC_DEP_TRACE (1<<0) +#define RC_DEP_TRACE (1 << 0) /*! Only use services added to runlevels */ -#define RC_DEP_STRICT (1<<1) +#define RC_DEP_STRICT (1 << 1) /*! Runlevel is starting */ -#define RC_DEP_START (1<<2) +#define RC_DEP_START (1 << 2) /*! Runlevel is stopping */ -#define RC_DEP_STOP (1<<3) +#define RC_DEP_STOP (1 << 3) /*! @name Dependencies * We analyse each init script and cache the resultant dependency tree. @@ -357,8 +362,7 @@ const char *rc_sys(void); /*! Singly linked list of dependency types that list the services the * type is for */ -typedef struct rc_deptype -{ +typedef struct rc_deptype { /*! ineed, iuse, iafter, etc */ char *type; /*! list of services */ @@ -368,8 +372,7 @@ typedef struct rc_deptype } RC_DEPTYPE; /*! Singly linked list of services and their dependencies */ -typedef struct rc_depinfo -{ +typedef struct rc_depinfo { /*! Name of service */ char *service; /*! Dependencies */ @@ -378,7 +381,7 @@ typedef struct rc_depinfo TAILQ_ENTRY(rc_depinfo) entries; } RC_DEPINFO; -typedef TAILQ_HEAD(,rc_depinfo) RC_DEPTREE; +typedef TAILQ_HEAD(, rc_depinfo) RC_DEPTREE; #else /* Handles to internal structures */ typedef void *RC_DEPTREE; @@ -431,24 +434,27 @@ bool rc_deptree_update_needed(time_t *, char *); * This pointer should be freed with rc_deptree_free when done. * @return pointer to the dependency tree */ #ifdef HAVE_MALLOC_EXTENDED_ATTRIBUTE -__attribute__ ((malloc (rc_deptree_free, 1))) +__attribute__((malloc(rc_deptree_free, 1))) #endif -RC_DEPTREE *rc_deptree_load(void); +RC_DEPTREE * +rc_deptree_load(void); /*! Load a cached dependency tree from the specified file and return a pointer * to it. This pointer should be freed with rc_deptree_free when done. * @return pointer to the dependency tree */ #ifdef HAVE_MALLOC_EXTENDED_ATTRIBUTE -__attribute__ ((malloc (rc_deptree_free, 1))) +__attribute__((malloc(rc_deptree_free, 1))) #endif -RC_DEPTREE *rc_deptree_load_file(const char *); +RC_DEPTREE * +rc_deptree_load_file(const char *); /*! List the depend for the type of service * @param deptree to search * @param type to use (keywords, etc) * @param service to check * @return NULL terminated list of services in order */ -RC_STRINGLIST *rc_deptree_depend(const RC_DEPTREE *, const char *, const char *); +RC_STRINGLIST *rc_deptree_depend(const RC_DEPTREE *, const char *, + const char *); /*! List all the services in order that the given services have * for the given types and options. @@ -482,23 +488,22 @@ RC_STRINGLIST *rc_deptree_order(const RC_DEPTREE *, const char *, int); * question. now shows we really will start the service now and done shows * when we have done it as may start scheduled services at this point. */ /*! Points at which a plugin can hook into RC */ -typedef enum -{ - RC_HOOK_RUNLEVEL_STOP_IN = 1, - RC_HOOK_RUNLEVEL_STOP_OUT = 4, - RC_HOOK_RUNLEVEL_START_IN = 5, +typedef enum { + RC_HOOK_RUNLEVEL_STOP_IN = 1, + RC_HOOK_RUNLEVEL_STOP_OUT = 4, + RC_HOOK_RUNLEVEL_START_IN = 5, RC_HOOK_RUNLEVEL_START_OUT = 8, /*! We send the abort if an init script requests we abort and drop * into single user mode if system not fully booted */ - RC_HOOK_ABORT = 99, - RC_HOOK_SERVICE_STOP_IN = 101, - RC_HOOK_SERVICE_STOP_NOW = 102, - RC_HOOK_SERVICE_STOP_DONE = 103, - RC_HOOK_SERVICE_STOP_OUT = 104, - RC_HOOK_SERVICE_START_IN = 105, - RC_HOOK_SERVICE_START_NOW = 106, + RC_HOOK_ABORT = 99, + RC_HOOK_SERVICE_STOP_IN = 101, + RC_HOOK_SERVICE_STOP_NOW = 102, + RC_HOOK_SERVICE_STOP_DONE = 103, + RC_HOOK_SERVICE_STOP_OUT = 104, + RC_HOOK_SERVICE_START_IN = 105, + RC_HOOK_SERVICE_START_NOW = 106, RC_HOOK_SERVICE_START_DONE = 107, - RC_HOOK_SERVICE_START_OUT = 108 + RC_HOOK_SERVICE_START_OUT = 108 } RC_HOOK; /*! Plugin entry point @@ -511,7 +516,6 @@ int rc_plugin_hook(RC_HOOK, const char *); * variables they wish. Variables should be separated by NULLs. */ extern FILE *rc_environ_fd; - /*! Return a NULL terminated list of non comment lines from a file. */ RC_STRINGLIST *rc_config_list(const char *); @@ -541,10 +545,10 @@ void rc_stringlist_free(RC_STRINGLIST *); /*! Create a new stringlinst * @return pointer to new list */ #ifdef HAVE_MALLOC_EXTENDED_ATTRIBUTE -__attribute__ ((malloc (rc_stringlist_free, 1))) +__attribute__((malloc(rc_stringlist_free, 1))) #endif -__attribute__ ((warn_unused_result)) -RC_STRINGLIST *rc_stringlist_new(void); +__attribute__((warn_unused_result)) RC_STRINGLIST * +rc_stringlist_new(void); /*! Duplicate the item, add it to end of the list and return a pointer to it. * @param list to add the item too @@ -575,16 +579,14 @@ RC_STRING *rc_stringlist_find(RC_STRINGLIST *, const char *); * @param string to split * @param separator * @return new list */ -__attribute__ ((warn_unused_result)) -RC_STRINGLIST *rc_stringlist_split(const char *, const char *); - +__attribute__((warn_unused_result)) RC_STRINGLIST * +rc_stringlist_split(const char *, const char *); /*! Sort the list according to C locale * @param list to sort */ void rc_stringlist_sort(RC_STRINGLIST **); -typedef struct rc_pid -{ +typedef struct rc_pid { pid_t pid; LIST_ENTRY(rc_pid) entries; } RC_PID; diff --git a/src/mark_service/mark_service.c b/src/mark_service/mark_service.c index eaab6eedb..0469cb3cf 100644 --- a/src/mark_service/mark_service.c +++ b/src/mark_service/mark_service.c @@ -10,24 +10,24 @@ * except according to the terms contained in the LICENSE file. */ -#include #include +#include -#include #include +#include #include +#include #include #include #include -#include #include #include #include #include #include "einfo.h" -#include "rc.h" #include "misc.h" +#include "rc.h" const char *applet = NULL; @@ -71,7 +71,7 @@ int main(int argc, char **argv) if (openrc_pid && sscanf(openrc_pid, "%d", &pid) == 1) if (kill(pid, SIGHUP) != 0) eerror("%s: failed to signal parent %d: %s", - applet, pid, strerror(errno)); + applet, pid, strerror(errno)); /* Remove the exclusive time test. This ensures that it's not in control as well */ diff --git a/src/mountinfo/mountinfo.c b/src/mountinfo/mountinfo.c index c55b12aba..c363d5ce3 100644 --- a/src/mountinfo/mountinfo.c +++ b/src/mountinfo/mountinfo.c @@ -15,20 +15,21 @@ * except according to the terms contained in the LICENSE file. */ -#include #include +#include #if defined(__DragonFly__) || defined(__FreeBSD__) -# include -# include -# define F_FLAGS f_flags +#include +#include +#define F_FLAGS f_flags #elif defined(BSD) && !defined(__GNU__) -# include -# define statfs statvfs -# define F_FLAGS f_flag -#elif defined(__linux__) || (defined(__FreeBSD_kernel__) && \ - defined(__GLIBC__)) || defined(__GNU__) -# include +#include +#define statfs statvfs +#define F_FLAGS f_flag +#elif defined(__linux__) || \ + (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) || \ + defined(__GNU__) +#include #endif #include @@ -39,62 +40,43 @@ #include #include +#include "_usage.h" #include "einfo.h" +#include "misc.h" #include "queue.h" #include "rc.h" -#include "misc.h" -#include "_usage.h" const char *applet = NULL; const char *procmounts = "/proc/mounts"; const char *extraopts = "[mount1] [mount2] ..."; const char getoptstring[] = "f:F:n:N:o:O:p:P:iste:E:" getoptstring_COMMON; const struct option longopts[] = { - { "fstype-regex", 1, NULL, 'f'}, - { "skip-fstype-regex", 1, NULL, 'F'}, - { "node-regex", 1, NULL, 'n'}, - { "skip-node-regex", 1, NULL, 'N'}, - { "options-regex", 1, NULL, 'o'}, - { "skip-options-regex", 1, NULL, 'O'}, - { "point-regex", 1, NULL, 'p'}, - { "skip-point-regex", 1, NULL, 'P'}, - { "options", 0, NULL, 'i'}, - { "fstype", 0, NULL, 's'}, - { "node", 0, NULL, 't'}, - { "netdev", 0, NULL, 'e'}, - { "nonetdev", 0, NULL, 'E'}, - longopts_COMMON -}; -const char * const longopts_help[] = { - "fstype regex to find", - "fstype regex to skip", - "node regex to find", - "node regex to skip", - "options regex to find", - "options regex to skip", - "point regex to find", - "point regex to skip", - "print options", - "print fstype", - "print node", - "is it a network device", - "is it not a network device", - longopts_help_COMMON -}; + {"fstype-regex", 1, NULL, 'f'}, {"skip-fstype-regex", 1, NULL, 'F'}, + {"node-regex", 1, NULL, 'n'}, {"skip-node-regex", 1, NULL, 'N'}, + {"options-regex", 1, NULL, 'o'}, {"skip-options-regex", 1, NULL, 'O'}, + {"point-regex", 1, NULL, 'p'}, {"skip-point-regex", 1, NULL, 'P'}, + {"options", 0, NULL, 'i'}, {"fstype", 0, NULL, 's'}, + {"node", 0, NULL, 't'}, {"netdev", 0, NULL, 'e'}, + {"nonetdev", 0, NULL, 'E'}, longopts_COMMON}; +const char *const longopts_help[] = {"fstype regex to find", + "fstype regex to skip", + "node regex to find", + "node regex to skip", + "options regex to find", + "options regex to skip", + "point regex to find", + "point regex to skip", + "print options", + "print fstype", + "print node", + "is it a network device", + "is it not a network device", + longopts_help_COMMON}; const char *usagestring = NULL; -typedef enum { - mount_from, - mount_to, - mount_fstype, - mount_options -} mount_type; +typedef enum { mount_from, mount_to, mount_fstype, mount_options } mount_type; -typedef enum { - net_ignore, - net_yes, - net_no -} net_opts; +typedef enum { net_ignore, net_yes, net_no } net_opts; struct args { regex_t *node_regex; @@ -108,10 +90,8 @@ struct args { net_opts netdev; }; -static int -process_mount(RC_STRINGLIST *list, struct args *args, - char *from, char *to, char *fstype, char *options, - int netdev) +static int process_mount(RC_STRINGLIST *list, struct args *args, char *from, + char *to, char *fstype, char *options, int netdev) { char *p; RC_STRING *s; @@ -125,13 +105,11 @@ process_mount(RC_STRINGLIST *list, struct args *args, #endif if (args->netdev == net_yes && - (netdev != -1 || TAILQ_FIRST(args->mounts))) - { + (netdev != -1 || TAILQ_FIRST(args->mounts))) { if (netdev != 0) return 1; } else if (args->netdev == net_no && - (netdev != -1 || TAILQ_FIRST(args->mounts))) - { + (netdev != -1 || TAILQ_FIRST(args->mounts))) { if (netdev != 1) return 1; } else { @@ -159,8 +137,8 @@ process_mount(RC_STRINGLIST *list, struct args *args, if (TAILQ_FIRST(args->mounts)) { TAILQ_FOREACH(s, args->mounts, entries) - if (strcmp(s->value, to) == 0) - break; + if (strcmp(s->value, to) == 0) + break; if (!s) return -1; } @@ -200,44 +178,41 @@ process_mount(RC_STRINGLIST *list, struct args *args, static struct opt { int o_opt; const char *o_name; -} optnames[] = { - { MNT_ASYNC, "asynchronous" }, - { MNT_EXPORTED, "NFS exported" }, - { MNT_LOCAL, "local" }, - { MNT_NOATIME, "noatime" }, - { MNT_NOEXEC, "noexec" }, - { MNT_NOSUID, "nosuid" }, +} optnames[] = {{MNT_ASYNC, "asynchronous"}, + {MNT_EXPORTED, "NFS exported"}, + {MNT_LOCAL, "local"}, + {MNT_NOATIME, "noatime"}, + {MNT_NOEXEC, "noexec"}, + {MNT_NOSUID, "nosuid"}, #ifdef MNT_NOSYMFOLLOW - { MNT_NOSYMFOLLOW, "nosymfollow" }, + {MNT_NOSYMFOLLOW, "nosymfollow"}, #endif - { MNT_QUOTA, "with quotas" }, - { MNT_RDONLY, "read-only" }, - { MNT_SYNCHRONOUS, "synchronous" }, - { MNT_UNION, "union" }, + {MNT_QUOTA, "with quotas"}, + {MNT_RDONLY, "read-only"}, + {MNT_SYNCHRONOUS, "synchronous"}, + {MNT_UNION, "union"}, #ifdef MNT_NOCLUSTERR - { MNT_NOCLUSTERR, "noclusterr" }, + {MNT_NOCLUSTERR, "noclusterr"}, #endif #ifdef MNT_NOCLUSTERW - { MNT_NOCLUSTERW, "noclusterw" }, + {MNT_NOCLUSTERW, "noclusterw"}, #endif #ifdef MNT_SUIDDIR - { MNT_SUIDDIR, "suiddir" }, + {MNT_SUIDDIR, "suiddir"}, #endif - { MNT_SOFTDEP, "soft-updates" }, + {MNT_SOFTDEP, "soft-updates"}, #ifdef MNT_MULTILABEL - { MNT_MULTILABEL, "multilabel" }, + {MNT_MULTILABEL, "multilabel"}, #endif #ifdef MNT_ACLS - { MNT_ACLS, "acls" }, + {MNT_ACLS, "acls"}, #endif #ifdef MNT_GJOURNAL - { MNT_GJOURNAL, "gjournal" }, + {MNT_GJOURNAL, "gjournal"}, #endif - { 0, NULL } -}; + {0, NULL}}; -static RC_STRINGLIST * -find_mounts(struct args *args) +static RC_STRINGLIST *find_mounts(struct args *args) { struct statfs *mnts; int nmnts; @@ -250,7 +225,7 @@ find_mounts(struct args *args) char *tmp; if ((nmnts = getmntinfo(&mnts, MNT_NOWAIT)) == 0) - eerrorx("getmntinfo: %s", strerror (errno)); + eerrorx("getmntinfo: %s", strerror(errno)); list = rc_stringlist_new(); for (i = 0; i < nmnts; i++) { @@ -263,7 +238,8 @@ find_mounts(struct args *args) if (!options) options = xstrdup(o->o_name); else { - xasprintf(&tmp, "%s,%s", options, o->o_name); + xasprintf(&tmp, "%s,%s", options, + o->o_name); free(options); options = tmp; } @@ -271,12 +247,9 @@ find_mounts(struct args *args) flags &= ~o->o_opt; } - process_mount(list, args, - mnts[i].f_mntfromname, - mnts[i].f_mntonname, - mnts[i].f_fstypename, - options, - netdev); + process_mount(list, args, mnts[i].f_mntfromname, + mnts[i].f_mntonname, mnts[i].f_fstypename, + options, netdev); free(options); options = NULL; @@ -285,10 +258,10 @@ find_mounts(struct args *args) return list; } -#elif defined(__linux__) || (defined(__FreeBSD_kernel__) && \ - defined(__GLIBC__)) || defined(__GNU__) -static struct mntent * -getmntfile(const char *file) +#elif defined(__linux__) || \ + (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) || \ + defined(__GNU__) +static struct mntent *getmntfile(const char *file) { struct mntent *ent = NULL; FILE *fp; @@ -305,8 +278,7 @@ 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; @@ -352,18 +324,16 @@ find_mounts(struct args *args) } #else -# error "Operating system not supported!" +#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)); + regex_t *reg = xmalloc(sizeof(*reg)); int result; char buffer[256]; - if ((result = regcomp(reg, string, REG_EXTENDED | REG_NOSUB)) != 0) - { + if ((result = regcomp(reg, string, REG_EXTENDED | REG_NOSUB)) != 0) { regerror(result, reg, buffer, sizeof(buffer)); eerrorx("%s: invalid regex `%s'", applet, buffer); } @@ -383,21 +353,24 @@ int main(int argc, char **argv) int result; char *this_path; -#define DO_REG(_var) \ - if (_var) free(_var); \ +#define DO_REG(_var) \ + if (_var) \ + free(_var); \ _var = get_regex(optarg); -#define REG_FREE(_var) \ - if (_var) { regfree(_var); free(_var); } +#define REG_FREE(_var) \ + if (_var) { \ + regfree(_var); \ + free(_var); \ + } applet = basename_c(argv[0]); - memset (&args, 0, sizeof(args)); + memset(&args, 0, sizeof(args)); args.mount_type = mount_to; args.netdev = net_ignore; args.mounts = rc_stringlist_new(); - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { case 'e': args.netdev = net_yes; @@ -439,14 +412,14 @@ int main(int argc, char **argv) args.mount_type = mount_from; break; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } } 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]); this_path = argv[optind++]; real_path = realpath(this_path, NULL); if (real_path) diff --git a/src/openrc-init/openrc-init.c b/src/openrc-init/openrc-init.c index 2e6d36773..c7a0ad115 100644 --- a/src/openrc-init/openrc-init.c +++ b/src/openrc-init/openrc-init.c @@ -25,22 +25,22 @@ #include #include #include -#include -#include -#include -#include #include +#include +#include #include +#include +#include #ifdef HAVE_SELINUX -# include +#include #endif #include "helpers.h" -#include "rc.h" #include "plugin.h" -#include "wtmp.h" +#include "rc.h" #include "version.h" +#include "wtmp.h" static const char *path_default = "/sbin:/usr/sbin:/bin:/usr/bin"; static const char *rc_default_runlevel = "default"; @@ -56,26 +56,26 @@ static void do_openrc(const char *runlevel) sigprocmask(SIG_BLOCK, &all_signals, &our_signals); pid = fork(); switch (pid) { - case -1: - perror("fork"); - exit(1); - break; - case 0: - setsid(); - /* unblock all signals */ - sigprocmask(SIG_UNBLOCK, &all_signals, NULL); - printf("Starting %s runlevel\n", runlevel); - execlp("openrc", "openrc", runlevel, NULL); - perror("exec"); - exit(1); - break; - default: - /* restore our signal mask */ - sigprocmask(SIG_SETMASK, &our_signals, NULL); - while (waitpid(pid, NULL, 0) != pid) - if (errno == ECHILD) - break; - break; + case -1: + perror("fork"); + exit(1); + break; + case 0: + setsid(); + /* unblock all signals */ + sigprocmask(SIG_UNBLOCK, &all_signals, NULL); + printf("Starting %s runlevel\n", runlevel); + execlp("openrc", "openrc", runlevel, NULL); + perror("exec"); + exit(1); + break; + default: + /* restore our signal mask */ + sigprocmask(SIG_SETMASK, &our_signals, NULL); + while (waitpid(pid, NULL, 0) != pid) + if (errno == ECHILD) + break; + break; } } @@ -156,9 +156,8 @@ static void open_shell(void) const char *sys = rc_sys(); /* VSERVER systems cannot really drop to shells */ - if (sys && strcmp(sys, RC_SYS_VSERVER) == 0) - { - execlp("halt", "halt", "-f", (char *) NULL); + if (sys && strcmp(sys, RC_SYS_VSERVER) == 0) { + execlp("halt", "halt", "-f", (char *)NULL); perror("init"); return; } @@ -204,21 +203,21 @@ static void reap_zombies(void) static void signal_handler(int sig) { switch (sig) { - case SIGINT: - handle_shutdown("reboot", RB_AUTOBOOT); - break; - case SIGTERM: + case SIGINT: + handle_shutdown("reboot", RB_AUTOBOOT); + break; + case SIGTERM: #ifdef SIGPWR - case SIGPWR: + case SIGPWR: #endif - handle_shutdown("shutdown", RB_HALT_SYSTEM); - break; - case SIGCHLD: - reap_zombies(); - break; - default: - printf("Unknown signal received, %d\n", sig); - break; + handle_shutdown("shutdown", RB_HALT_SYSTEM); + break; + case SIGCHLD: + reap_zombies(); + break; + default: + printf("Unknown signal received, %d\n", sig); + break; } } @@ -232,7 +231,7 @@ int main(int argc, char **argv) sigset_t signals; struct sigaction sa; #ifdef HAVE_SELINUX - int enforce = 0; + int enforce = 0; #endif if (getpid() != 1) @@ -251,9 +250,11 @@ int main(int argc, char **argv) * At this point, we probably can't open /dev/console, * so log() won't work */ - fprintf(stderr,"Unable to load SELinux Policy.\n"); - fprintf(stderr,"Machine is in enforcing mode.\n"); - fprintf(stderr,"Halting now.\n"); + fprintf(stderr, + "Unable to load SELinux Policy.\n"); + fprintf(stderr, + "Machine is in enforcing mode.\n"); + fprintf(stderr, "Halting now.\n"); exit(1); } } diff --git a/src/openrc-run/openrc-run.c b/src/openrc-run/openrc-run.c index 06d1a9d3f..d4e98d7ec 100644 --- a/src/openrc-run/openrc-run.c +++ b/src/openrc-run/openrc-run.c @@ -15,11 +15,11 @@ * except according to the terms contained in the LICENSE file. */ -#include -#include #include +#include #include #include +#include #include #include @@ -38,50 +38,47 @@ #include #include -#if defined(__linux__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) \ - || defined(__GNU__) -# include +#if defined(__linux__) || \ + (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) || \ + defined(__GNU__) +#include #elif defined(__NetBSD__) || defined(__OpenBSD__) -# include +#include #else -# include +#include #endif +#include "_usage.h" #include "einfo.h" -#include "queue.h" -#include "rc.h" #include "misc.h" #include "plugin.h" +#include "queue.h" +#include "rc.h" #include "selinux.h" -#include "_usage.h" -#define PREFIX_LOCK RC_SVCDIR "/prefix.lock" +#define PREFIX_LOCK RC_SVCDIR "/prefix.lock" -#define WAIT_INTERVAL 20000000 /* usecs to poll the lock file */ -#define WAIT_TIMEOUT 60 /* seconds until we timeout */ -#define WARN_TIMEOUT 10 /* warn about this every N seconds */ +#define WAIT_INTERVAL 20000000 /* usecs to poll the lock file */ +#define WAIT_TIMEOUT 60 /* seconds until we timeout */ +#define WARN_TIMEOUT 10 /* warn about this every N seconds */ const char *applet = NULL; const char *extraopts = "stop | start | restart | describe | zap"; const char getoptstring[] = "dDsSvl:Z" getoptstring_COMMON; -const struct option longopts[] = { - { "debug", 0, NULL, 'd'}, - { "dry-run", 0, NULL, 'Z'}, - { "ifstarted", 0, NULL, 's'}, - { "ifstopped", 0, NULL, 'S'}, - { "nodeps", 0, NULL, 'D'}, - { "lockfd", 1, NULL, 'l'}, - longopts_COMMON -}; -const char *const longopts_help[] = { - "set xtrace when running the script", - "show what would be done", - "only run commands when started", - "only run commands when stopped", - "ignore dependencies", - "fd of the exclusive lock from rc", - longopts_help_COMMON -}; +const struct option longopts[] = {{"debug", 0, NULL, 'd'}, + {"dry-run", 0, NULL, 'Z'}, + {"ifstarted", 0, NULL, 's'}, + {"ifstopped", 0, NULL, 'S'}, + {"nodeps", 0, NULL, 'D'}, + {"lockfd", 1, NULL, 'l'}, + longopts_COMMON}; +const char *const longopts_help[] = {"set xtrace when running the script", + "show what would be done", + "only run commands when started", + "only run commands when stopped", + "ignore dependencies", + "fd of the exclusive lock from rc", + longopts_help_COMMON}; const char *usagestring = NULL; static char *service, *runlevel, *ibsave, *prefix; @@ -95,18 +92,17 @@ static RC_HOOK hook_out; static int exclusive_fd = -1, master_tty = -1; static bool sighup, in_background, deps, dry_run; static pid_t service_pid; -static int signal_pipe[2] = { -1, -1 }; - -static RC_STRINGLIST *deptypes_b; /* broken deps */ -static RC_STRINGLIST *deptypes_n; /* needed deps */ -static RC_STRINGLIST *deptypes_nw; /* need+want deps */ -static RC_STRINGLIST *deptypes_nwu; /* need+want+use deps */ -static RC_STRINGLIST *deptypes_nwua; /* need+want+use+after deps */ -static RC_STRINGLIST *deptypes_m; /* needed deps for stopping */ -static RC_STRINGLIST *deptypes_mwua; /* need+want+use+after deps for stopping */ - -static void -handle_signal(int sig) +static int signal_pipe[2] = {-1, -1}; + +static RC_STRINGLIST *deptypes_b; /* broken deps */ +static RC_STRINGLIST *deptypes_n; /* needed deps */ +static RC_STRINGLIST *deptypes_nw; /* need+want deps */ +static RC_STRINGLIST *deptypes_nwu; /* need+want+use deps */ +static RC_STRINGLIST *deptypes_nwua; /* need+want+use+after deps */ +static RC_STRINGLIST *deptypes_m; /* needed deps for stopping */ +static RC_STRINGLIST *deptypes_mwua; /* need+want+use+after deps for stopping */ + +static void handle_signal(int sig) { int serrno = errno; char *signame = NULL; @@ -120,8 +116,8 @@ handle_signal(int sig) case SIGCHLD: if (signal_pipe[1] > -1) { if (write(signal_pipe[1], &sig, sizeof(sig)) == -1) - eerror("%s: send: %s", - service, strerror(errno)); + eerror("%s: send: %s", service, + strerror(errno)); } else rc_waitpid(-1); break; @@ -160,8 +156,7 @@ handle_signal(int sig) errno = serrno; } -static void -unhotplug() +static void unhotplug() { char *file = NULL; @@ -171,39 +166,32 @@ unhotplug() free(file); } -static void -start_services(RC_STRINGLIST *list) +static void start_services(RC_STRINGLIST *list) { RC_STRING *svc; - RC_SERVICE state = rc_service_state (service); + RC_SERVICE state = rc_service_state(service); if (!list) return; - if (state & RC_SERVICE_INACTIVE || - state & RC_SERVICE_WASINACTIVE || - state & RC_SERVICE_STARTING || - state & RC_SERVICE_STARTED) - { + if (state & RC_SERVICE_INACTIVE || state & RC_SERVICE_WASINACTIVE || + state & RC_SERVICE_STARTING || state & RC_SERVICE_STARTED) { TAILQ_FOREACH(svc, list, entries) { if (!(rc_service_state(svc->value) & - RC_SERVICE_STOPPED)) + RC_SERVICE_STOPPED)) continue; if (state & RC_SERVICE_INACTIVE || - state & RC_SERVICE_WASINACTIVE) - { - rc_service_schedule_start(service, - svc->value); + state & RC_SERVICE_WASINACTIVE) { + rc_service_schedule_start(service, svc->value); ewarn("WARNING: %s will start when %s has started", - svc->value, applet); + svc->value, applet); } else service_start(svc->value); } } } -static void -restore_state(void) +static void restore_state(void) { RC_SERVICE state; @@ -228,8 +216,7 @@ restore_state(void) exclusive_fd = svc_unlock(applet, exclusive_fd); } -static void -cleanup(void) +static void cleanup(void) { restore_state(); @@ -238,10 +225,9 @@ cleanup(void) rc_plugin_run(hook_out, applet); if (hook_out == RC_HOOK_SERVICE_START_DONE) rc_plugin_run(RC_HOOK_SERVICE_START_OUT, - applet); + applet); else if (hook_out == RC_HOOK_SERVICE_STOP_DONE) - rc_plugin_run(RC_HOOK_SERVICE_STOP_OUT, - applet); + rc_plugin_run(RC_HOOK_SERVICE_STOP_OUT, applet); } if (restart_services) @@ -278,8 +264,7 @@ cleanup(void) * Why don't we use (f)printf, as it is thread-safe through POSIX already? * Bug: 360013 */ -static int -write_prefix(const char *buffer, size_t bytes, bool *prefixed) +static int write_prefix(const char *buffer, size_t bytes, bool *prefixed) { size_t i, j; const char *ec = ecolor(ECOLOR_HILITE); @@ -300,8 +285,7 @@ write_prefix(const char *buffer, size_t bytes, bool *prefixed) break; } } - } - else + } else ewarnv("Couldn't open the prefix lock, please make sure you have enough permissions"); for (i = 0; i < bytes; i++) { @@ -334,8 +318,7 @@ write_prefix(const char *buffer, size_t bytes, bool *prefixed) return ret; } -static int -svc_exec(const char *arg1, const char *arg2) +static int svc_exec(const char *arg1, const char *arg2) { int ret, fdout = fileno(stdout); struct termios tt; @@ -356,7 +339,8 @@ svc_exec(const char *arg1, const char *arg2) eerrorx("%s: pipe: %s", service, applet); for (i = 0; i < 2; i++) if ((flags = fcntl(signal_pipe[i], F_GETFD, 0) == -1 || - fcntl(signal_pipe[i], F_SETFD, flags | FD_CLOEXEC) == -1)) + fcntl(signal_pipe[i], F_SETFD, + flags | FD_CLOEXEC) == -1)) eerrorx("%s: fcntl: %s", service, strerror(errno)); /* Open a pty for our prefixed output @@ -376,7 +360,7 @@ svc_exec(const char *arg1, const char *arg2) (flags = fcntl(master_tty, F_GETFD, 0)) == 0) fcntl(master_tty, F_SETFD, flags | FD_CLOEXEC); - if (slave_tty >=0 && + if (slave_tty >= 0 && (flags = fcntl(slave_tty, F_GETFD, 0)) == 0) fcntl(slave_tty, F_SETFD, flags | FD_CLOEXEC); } @@ -393,34 +377,37 @@ svc_exec(const char *arg1, const char *arg2) if (exists(RC_SVCDIR "/openrc-run.sh")) { if (arg2) einfov("Executing: %s %s %s %s %s", - RC_SVCDIR "/openrc-run.sh", RC_SVCDIR "/openrc-run.sh", - service, arg1, arg2); + RC_SVCDIR "/openrc-run.sh", + RC_SVCDIR "/openrc-run.sh", service, + arg1, arg2); else einfov("Executing: %s %s %s %s", - RC_SVCDIR "/openrc-run.sh", RC_SVCDIR "/openrc-run.sh", - service, arg1); + RC_SVCDIR "/openrc-run.sh", + RC_SVCDIR "/openrc-run.sh", service, + arg1); execl(RC_SVCDIR "/openrc-run.sh", - RC_SVCDIR "/openrc-run.sh", - service, arg1, arg2, (char *) NULL); + RC_SVCDIR "/openrc-run.sh", service, arg1, arg2, + (char *)NULL); eerror("%s: exec `" RC_SVCDIR "/openrc-run.sh': %s", - service, strerror(errno)); + service, strerror(errno)); _exit(EXIT_FAILURE); } else { if (arg2) einfov("Executing: %s %s %s %s %s", - RC_LIBEXECDIR "/sh/openrc-run.sh", - RC_LIBEXECDIR "/sh/openrc-run.sh", - service, arg1, arg2); + RC_LIBEXECDIR "/sh/openrc-run.sh", + RC_LIBEXECDIR "/sh/openrc-run.sh", + service, arg1, arg2); else einfov("Executing: %s %s %s %s", - RC_LIBEXECDIR "/sh/openrc-run.sh", - RC_LIBEXECDIR "/sh/openrc-run.sh", - service, arg1); + RC_LIBEXECDIR "/sh/openrc-run.sh", + RC_LIBEXECDIR "/sh/openrc-run.sh", + service, arg1); execl(RC_LIBEXECDIR "/sh/openrc-run.sh", - RC_LIBEXECDIR "/sh/openrc-run.sh", - service, arg1, arg2, (char *) NULL); - eerror("%s: exec `" RC_LIBEXECDIR "/sh/openrc-run.sh': %s", - service, strerror(errno)); + RC_LIBEXECDIR "/sh/openrc-run.sh", service, arg1, + arg2, (char *)NULL); + eerror("%s: exec `" RC_LIBEXECDIR + "/sh/openrc-run.sh': %s", + service, strerror(errno)); _exit(EXIT_FAILURE); } } @@ -438,8 +425,8 @@ svc_exec(const char *arg1, const char *arg2) for (;;) { if ((s = poll(fd, master_tty >= 0 ? 2 : 1, -1)) == -1) { if (errno != EINTR) { - eerror("%s: poll: %s", - service, strerror(errno)); + eerror("%s: poll: %s", service, + strerror(errno)); break; } } @@ -458,15 +445,15 @@ svc_exec(const char *arg1, const char *arg2) free(buffer); - sigemptyset (&sigchldmask); - sigaddset (&sigchldmask, SIGCHLD); - sigprocmask (SIG_BLOCK, &sigchldmask, &oldmask); + sigemptyset(&sigchldmask); + sigaddset(&sigchldmask, SIGCHLD); + sigprocmask(SIG_BLOCK, &sigchldmask, &oldmask); close(signal_pipe[0]); close(signal_pipe[1]); signal_pipe[0] = signal_pipe[1] = -1; - sigprocmask (SIG_SETMASK, &oldmask, NULL); + sigprocmask(SIG_SETMASK, &oldmask, NULL); if (master_tty >= 0) { /* Why did we do this? */ @@ -485,8 +472,7 @@ svc_exec(const char *arg1, const char *arg2) return ret; } -static bool -svc_wait(const char *svc) +static bool svc_wait(const char *svc) { char *file = NULL; int fd; @@ -525,7 +511,7 @@ svc_wait(const char *svc) } if (errno != EWOULDBLOCK) { eerror("%s: open `%s': %s", applet, file, - strerror(errno)); + strerror(errno)); free(file); exit(EXIT_FAILURE); } @@ -539,8 +525,8 @@ svc_wait(const char *svc) goto finish; timespecsub(&warn, &interval, &warn); if (warn.tv_sec <= 0) { - ewarn("%s: waiting for %s (%d seconds)", - applet, svc, (int)timeout.tv_sec); + ewarn("%s: waiting for %s (%d seconds)", applet, + svc, (int)timeout.tv_sec); warn.tv_sec = WARN_TIMEOUT; warn.tv_nsec = 0; } @@ -551,8 +537,7 @@ svc_wait(const char *svc) return false; } -static void -get_started_services(void) +static void get_started_services(void) { RC_STRINGLIST *tmp = rc_services_in_state(RC_SERVICE_INACTIVE); @@ -562,8 +547,7 @@ get_started_services(void) free(tmp); } -static void -setup_deptypes(void) +static void setup_deptypes(void) { deptypes_b = rc_stringlist_new(); rc_stringlist_add(deptypes_b, "broken"); @@ -596,8 +580,7 @@ setup_deptypes(void) rc_stringlist_add(deptypes_mwua, "beforeme"); } -static void -svc_start_check(void) +static void svc_start_check(void) { RC_SERVICE state; @@ -610,7 +593,8 @@ svc_start_check(void) rc_service_mark(service, RC_SERVICE_HOTPLUGGED); if (strcmp(runlevel, RC_LEVEL_SYSINIT) == 0) ewarnx("WARNING: %s will be started in the" - " next runlevel", applet); + " next runlevel", + applet); } if (exclusive_fd == -1) @@ -624,23 +608,21 @@ svc_start_check(void) ewarnx("WARNING: %s is already starting", applet); } fcntl(exclusive_fd, F_SETFD, - fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC); + fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC); if (state & RC_SERVICE_STARTED) { ewarn("WARNING: %s has already been started", applet); exit(EXIT_SUCCESS); - } - else if (state & RC_SERVICE_INACTIVE && !in_background) + } else if (state & RC_SERVICE_INACTIVE && !in_background) ewarnx("WARNING: %s has already started, but is inactive", - applet); + applet); rc_service_mark(service, RC_SERVICE_STARTING); hook_out = RC_HOOK_SERVICE_START_OUT; rc_plugin_run(RC_HOOK_SERVICE_START_IN, applet); } -static void -svc_start_deps(void) +static void svc_start_deps(void) { bool first; RC_STRING *svc, *svc2; @@ -660,7 +642,7 @@ svc_start_deps(void) setup_deptypes(); services = rc_deptree_depends(deptree, deptypes_b, applet_list, - runlevel, 0); + runlevel, 0); if (TAILQ_FIRST(services)) { eerrorn("ERROR: %s needs service(s) ", applet); first = true; @@ -677,12 +659,12 @@ svc_start_deps(void) rc_stringlist_free(services); services = NULL; - need_services = rc_deptree_depends(deptree, deptypes_n, - applet_list, runlevel, depoptions); - want_services = rc_deptree_depends(deptree, deptypes_nw, - applet_list, runlevel, depoptions); - use_services = rc_deptree_depends(deptree, deptypes_nwu, - applet_list, runlevel, depoptions); + need_services = rc_deptree_depends(deptree, deptypes_n, applet_list, + runlevel, depoptions); + want_services = rc_deptree_depends(deptree, deptypes_nw, applet_list, + runlevel, depoptions); + use_services = rc_deptree_depends(deptree, deptypes_nwu, applet_list, + runlevel, depoptions); if (!rc_runlevel_starting()) { TAILQ_FOREACH(svc, use_services, entries) { @@ -690,8 +672,7 @@ svc_start_deps(void) /* Don't stop failed services again. * If you remove this check, ensure that the * exclusive file isn't created. */ - if (state & RC_SERVICE_FAILED && - rc_runlevel_starting()) + if (state & RC_SERVICE_FAILED && rc_runlevel_starting()) continue; if (state & RC_SERVICE_STOPPED) { if (dry_run) { @@ -710,7 +691,7 @@ svc_start_deps(void) /* Now wait for them to start */ services = rc_deptree_depends(deptree, deptypes_nwua, applet_list, - runlevel, depoptions); + runlevel, depoptions); /* We use tmplist to hold our scheduled by list */ tmplist = rc_stringlist_new(); TAILQ_FOREACH(svc, services, entries) { @@ -721,8 +702,7 @@ svc_start_deps(void) /* Don't wait for services which went inactive but are * now in starting state which we are after */ if (state & RC_SERVICE_STARTING && - state & RC_SERVICE_WASINACTIVE) - { + state & RC_SERVICE_WASINACTIVE) { if (!rc_stringlist_find(need_services, svc->value) && !rc_stringlist_find(want_services, svc->value) && !rc_stringlist_find(use_services, svc->value)) @@ -730,20 +710,19 @@ svc_start_deps(void) } if (!svc_wait(svc->value)) - eerror("%s: timed out waiting for %s", - applet, svc->value); + eerror("%s: timed out waiting for %s", applet, + svc->value); state = rc_service_state(svc->value); if (state & RC_SERVICE_STARTED) continue; if (rc_stringlist_find(need_services, svc->value)) { if (state & RC_SERVICE_INACTIVE || - state & RC_SERVICE_WASINACTIVE) - { + state & RC_SERVICE_WASINACTIVE) { rc_stringlist_add(tmplist, svc->value); } else if (!TAILQ_FIRST(tmplist)) eerrorx("ERROR: cannot start %s as" - " %s would not start", - applet, svc->value); + " %s would not start", + applet, svc->value); } } @@ -758,10 +737,10 @@ svc_start_deps(void) n = 0; TAILQ_FOREACH(svc, tmplist, entries) { rc_service_schedule_start(svc->value, service); - use_services = rc_deptree_depend(deptree, - "iprovide", svc->value); + use_services = rc_deptree_depend(deptree, "iprovide", + svc->value); TAILQ_FOREACH(svc2, use_services, entries) - rc_service_schedule_start(svc2->value, service); + rc_service_schedule_start(svc2->value, service); rc_stringlist_free(use_services); use_services = NULL; len += strlen(svc->value) + 2; @@ -773,12 +752,12 @@ svc_start_deps(void) TAILQ_FOREACH(svc, tmplist, entries) { if (p != tmp) p += snprintf(p, len, ", "); - p += snprintf(p, len - (p - tmp), - "%s", svc->value); + p += snprintf(p, len - (p - tmp), "%s", svc->value); } rc_stringlist_free(tmplist); tmplist = NULL; - ewarnx("WARNING: %s will start when %s has started", applet, tmp); + ewarnx("WARNING: %s will start when %s has started", applet, + tmp); free(tmp); } @@ -814,8 +793,8 @@ static void svc_start_real() /* Now start any scheduled services */ services = rc_services_scheduled(service); TAILQ_FOREACH(svc, services, entries) - if (rc_service_state(svc->value) & RC_SERVICE_STOPPED) - service_start(svc->value); + if (rc_service_state(svc->value) & RC_SERVICE_STOPPED) + service_start(svc->value); rc_stringlist_free(services); services = NULL; @@ -825,9 +804,9 @@ static void svc_start_real() TAILQ_FOREACH(svc, tmplist, entries) { services = rc_services_scheduled(svc->value); TAILQ_FOREACH(svc2, services, entries) - if (rc_service_state(svc2->value) & - RC_SERVICE_STOPPED) - service_start(svc2->value); + if (rc_service_state(svc2->value) & + RC_SERVICE_STOPPED) + service_start(svc2->value); rc_stringlist_free(services); services = NULL; } @@ -839,8 +818,7 @@ static void svc_start_real() rc_plugin_run(RC_HOOK_SERVICE_START_OUT, applet); } -static void -svc_start(void) +static void svc_start(void) { if (dry_run) einfon("start:"); @@ -854,16 +832,14 @@ svc_start(void) svc_start_real(); } -static int -svc_stop_check(RC_SERVICE *state) +static int svc_stop_check(RC_SERVICE *state) { *state = rc_service_state(service); if (rc_runlevel_stopping() && *state & RC_SERVICE_FAILED) exit(EXIT_FAILURE); - if (in_background && - !(*state & RC_SERVICE_STARTED) && + if (in_background && !(*state & RC_SERVICE_STARTED) && !(*state & RC_SERVICE_INACTIVE)) exit(EXIT_FAILURE); @@ -877,7 +853,7 @@ svc_stop_check(RC_SERVICE *state) eerrorx("ERROR: %s stopped by something else", applet); } fcntl(exclusive_fd, F_SETFD, - fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC); + fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC); if (*state & RC_SERVICE_STOPPED) { ewarn("WARNING: %s is already stopped", applet); @@ -898,8 +874,7 @@ svc_stop_check(RC_SERVICE *state) return 0; } -static void -svc_stop_deps(RC_SERVICE state) +static void svc_stop_deps(RC_SERVICE state) { int depoptions = RC_DEP_TRACE; RC_STRING *svc; @@ -919,19 +894,16 @@ svc_stop_deps(RC_SERVICE state) setup_deptypes(); services = rc_deptree_depends(deptree, deptypes_m, applet_list, - runlevel, depoptions); + runlevel, depoptions); tmplist = rc_stringlist_new(); TAILQ_FOREACH_REVERSE(svc, services, rc_stringlist, entries) { state = rc_service_state(svc->value); /* Don't stop failed services again. * If you remove this check, ensure that the * exclusive file isn't created. */ - if (state & RC_SERVICE_FAILED && - rc_runlevel_stopping()) + if (state & RC_SERVICE_FAILED && rc_runlevel_stopping()) continue; - if (state & RC_SERVICE_STARTED || - state & RC_SERVICE_INACTIVE) - { + if (state & RC_SERVICE_STARTED || state & RC_SERVICE_INACTIVE) { if (dry_run) { printf(" %s", svc->value); continue; @@ -939,8 +911,7 @@ svc_stop_deps(RC_SERVICE state) svc_wait(svc->value); state = rc_service_state(svc->value); if (state & RC_SERVICE_STARTED || - state & RC_SERVICE_INACTIVE) - { + state & RC_SERVICE_INACTIVE) { pid = service_stop(svc->value); if (!rc_conf_yesno("rc_parallel")) rc_waitpid(pid); @@ -963,15 +934,14 @@ svc_stop_deps(RC_SERVICE state) /* If shutting down, we should stop even * if a dependant failed */ if (runlevel && - (strcmp(runlevel, - RC_LEVEL_SHUTDOWN) == 0 || - strcmp(runlevel, - RC_LEVEL_SINGLE) == 0)) + (strcmp(runlevel, RC_LEVEL_SHUTDOWN) == 0 || + strcmp(runlevel, RC_LEVEL_SINGLE) == 0)) continue; rc_service_mark(service, RC_SERVICE_FAILED); } eerrorx("ERROR: cannot stop %s as %s " - "is still up", applet, svc->value); + "is still up", + applet, svc->value); } rc_stringlist_free(tmplist); tmplist = NULL; @@ -979,7 +949,7 @@ svc_stop_deps(RC_SERVICE state) /* We now wait for other services that may use us and are * stopping. This is important when a runlevel stops */ services = rc_deptree_depends(deptree, deptypes_mwua, applet_list, - runlevel, depoptions); + runlevel, depoptions); TAILQ_FOREACH(svc, services, entries) { if (rc_service_state(svc->value) & RC_SERVICE_STOPPED) continue; @@ -989,8 +959,7 @@ svc_stop_deps(RC_SERVICE state) services = NULL; } -static void -svc_stop_real(void) +static void svc_stop_real(void) { bool stopped; @@ -1021,17 +990,15 @@ svc_stop_real(void) rc_plugin_run(RC_HOOK_SERVICE_STOP_OUT, applet); } -static int -svc_stop(void) +static int svc_stop(void) { RC_SERVICE state; state = 0; if (dry_run) einfon("stop:"); - else - if (svc_stop_check(&state) == 1) - return 1; /* Service has been stopped already */ + else if (svc_stop_check(&state) == 1) + return 1; /* Service has been stopped already */ if (deps) svc_stop_deps(state); if (dry_run) @@ -1042,8 +1009,7 @@ svc_stop(void) return 0; } -static void -svc_restart(void) +static void svc_restart(void) { /* This is hairy and a better way needs to be found I think! * The issue is this - openvpn need net and dns. net can restart @@ -1070,7 +1036,7 @@ svc_restart(void) svc_stop(); if (dry_run) ewarn("Cannot calculate restart start dependencies" - " on a dry-run"); + " on a dry-run"); } svc_start(); @@ -1079,8 +1045,7 @@ svc_restart(void) restart_services = NULL; } -static bool -service_plugable(void) +static bool service_plugable(void) { char *list, *p, *token; bool allow = true, truefalse; @@ -1134,8 +1099,8 @@ int main(int argc, char **argv) runscript = true; if (stat(argv[1], &stbuf) != 0) { - fprintf(stderr, "openrc-run `%s': %s\n", - argv[1], strerror(errno)); + fprintf(stderr, "openrc-run `%s': %s\n", argv[1], + strerror(errno)); exit(EXIT_FAILURE); } @@ -1152,7 +1117,7 @@ int main(int argc, char **argv) } lnk = xmalloc(4096); memset(lnk, 0, 4096); - if (readlink(argv[1], lnk, 4096-1)) { + if (readlink(argv[1], lnk, 4096 - 1)) { dir = dirname(path); if (strchr(lnk, '/')) { save = xstrdup(dir); @@ -1198,7 +1163,7 @@ int main(int argc, char **argv) /* Set an env var so that we always know our pid regardless of any subshells the init script may create so that our mark_service_* functions can always instruct us of this change */ - xasprintf(&pidstr, "%d", (int) getpid()); + xasprintf(&pidstr, "%d", (int)getpid()); setenv("RC_OPENRC_PID", pidstr, 1); /* * RC_RUNSCRIPT_PID is deprecated, but we will keep it for a while @@ -1239,8 +1204,8 @@ int main(int argc, char **argv) argv++; /* Right then, parse any options there may be */ - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *)0)) != -1) + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) switch (opt) { case 'd': setenv("RC_DEBUG", "YES", 1); @@ -1248,7 +1213,7 @@ int main(int argc, char **argv) case 'l': exclusive_fd = atoi(optarg); fcntl(exclusive_fd, F_SETFD, - fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC); + fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC); break; case 's': if (!(rc_service_state(service) & RC_SERVICE_STARTED)) @@ -1264,7 +1229,7 @@ int main(int argc, char **argv) case 'Z': dry_run = true; break; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } if (rc_yesno(getenv("RC_NODEPS"))) @@ -1285,7 +1250,7 @@ int main(int argc, char **argv) } if (rc_yesno(getenv("IN_DRYRUN"))) - dry_run = true; + dry_run = true; if (rc_yesno(getenv("IN_HOTPLUG"))) { if (!service_plugable()) eerrorx("%s: not allowed to be hotplugged", applet); @@ -1306,7 +1271,8 @@ int main(int argc, char **argv) rc_stringlist_add(applet_list, applet); if (runscript) - ewarn("%s uses runscript, please convert to openrc-run.", service); + ewarn("%s uses runscript, please convert to openrc-run.", + service); /* Now run each option */ retval = EXIT_SUCCESS; @@ -1315,7 +1281,7 @@ int main(int argc, char **argv) /* Abort on a sighup here */ if (sighup) - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); /* Export the command we're running. This is important as we stamp on the restart function now but @@ -1328,8 +1294,7 @@ int main(int argc, char **argv) if (strcmp(optarg, "describe") == 0 || strcmp(optarg, "help") == 0 || - strcmp(optarg, "depend") == 0) - { + strcmp(optarg, "depend") == 0) { save = prefix; eprefix(NULL); prefix = NULL; @@ -1337,15 +1302,14 @@ int main(int argc, char **argv) eprefix(save); prefix = save; } else if (strcmp(optarg, "ineed") == 0 || - strcmp(optarg, "iuse") == 0 || - strcmp(optarg, "iwant") == 0 || - strcmp(optarg, "needsme") == 0 || - strcmp(optarg, "usesme") == 0 || - strcmp(optarg, "wantsme") == 0 || - strcmp(optarg, "iafter") == 0 || - strcmp(optarg, "ibefore") == 0 || - strcmp(optarg, "iprovide") == 0) - { + strcmp(optarg, "iuse") == 0 || + strcmp(optarg, "iwant") == 0 || + strcmp(optarg, "needsme") == 0 || + strcmp(optarg, "usesme") == 0 || + strcmp(optarg, "wantsme") == 0 || + strcmp(optarg, "iafter") == 0 || + strcmp(optarg, "ibefore") == 0 || + strcmp(optarg, "iprovide") == 0) { errno = 0; if (rc_conf_yesno("rc_depend_strict") || errno == ENOENT) @@ -1358,24 +1322,23 @@ int main(int argc, char **argv) tmplist = rc_stringlist_new(); rc_stringlist_add(tmplist, optarg); services = rc_deptree_depends(deptree, tmplist, - applet_list, - runlevel, depoptions); + applet_list, runlevel, + depoptions); rc_stringlist_free(tmplist); tmplist = NULL; TAILQ_FOREACH(svc, services, entries) - printf("%s ", svc->value); - printf ("\n"); + printf("%s ", svc->value); + printf("\n"); rc_stringlist_free(services); services = NULL; - } else if (strcmp (optarg, "status") == 0) { + } else if (strcmp(optarg, "status") == 0) { save = prefix; eprefix(NULL); prefix = NULL; retval = svc_exec("status", NULL); } else { if (strcmp(optarg, "conditionalrestart") == 0 || - strcmp(optarg, "condrestart") == 0) - { + strcmp(optarg, "condrestart") == 0) { if (rc_service_state(service) & RC_SERVICE_STARTED) svc_restart(); @@ -1383,7 +1346,8 @@ int main(int argc, char **argv) svc_restart(); } else if (strcmp(optarg, "start") == 0) { svc_start(); - } else if (strcmp(optarg, "stop") == 0 || strcmp(optarg, "pause") == 0) { + } else if (strcmp(optarg, "stop") == 0 || + strcmp(optarg, "pause") == 0) { if (strcmp(optarg, "pause") == 0) { ewarn("WARNING: 'pause' is deprecated; please use '--nodeps stop'"); deps = false; @@ -1396,28 +1360,30 @@ int main(int argc, char **argv) if (!in_background && !rc_runlevel_stopping() && rc_service_state(service) & - RC_SERVICE_STOPPED) + RC_SERVICE_STOPPED) unhotplug(); if (in_background && rc_service_state(service) & - RC_SERVICE_INACTIVE) - { + RC_SERVICE_INACTIVE) { TAILQ_FOREACH(svc, - restart_services, - entries) - if (rc_service_state(svc->value) & - RC_SERVICE_STOPPED) - rc_service_schedule_start(service, svc->value); + restart_services, + entries) + if (rc_service_state( + svc->value) & + RC_SERVICE_STOPPED) + rc_service_schedule_start( + service, + svc->value); } } } else if (strcmp(optarg, "zap") == 0) { einfo("Manually resetting %s to stopped state", - applet); + applet); if (!rc_service_mark(applet, - RC_SERVICE_STOPPED)) + RC_SERVICE_STOPPED)) eerrorx("rc_service_mark: %s", - strerror(errno)); + strerror(errno)); unhotplug(); } else retval = svc_exec(optarg, NULL); diff --git a/src/openrc-shutdown/broadcast.c b/src/openrc-shutdown/broadcast.c index 402a9fb96..c6b06486d 100644 --- a/src/openrc-shutdown/broadcast.c +++ b/src/openrc-shutdown/broadcast.c @@ -13,29 +13,29 @@ * except according to the terms contained in the LICENSE file. */ #include -#include -#include -#include +#include #include +#include +#include +#include +#include +#include #include #include +#include +#include +#include +#include #include #include -#include #include #include -#include -#include -#include -#include -#include -#include #include "broadcast.h" #include "helpers.h" #ifndef _PATH_DEV -# define _PATH_DEV "/dev/" +#define _PATH_DEV "/dev/" #endif static sigjmp_buf jbuf; @@ -44,22 +44,22 @@ static sigjmp_buf jbuf; * Alarm handler */ /*ARGSUSED*/ -# ifdef __GNUC__ +#ifdef __GNUC__ static void handler(int arg __attribute__((unused))) -# else +#else static void handler(int arg) -# endif +#endif { siglongjmp(jbuf, 1); } static void getuidtty(char **userp, char **ttyp) { - struct passwd *pwd; - uid_t uid; - char *tty; - static char uidbuf[32]; - char *ttynm = NULL; + struct passwd *pwd; + uid_t uid; + char *tty; + static char uidbuf[32]; + char *ttynm = NULL; uid = getuid(); if ((pwd = getpwuid(uid)) != NULL) { @@ -67,7 +67,7 @@ static void getuidtty(char **userp, char **ttyp) strncat(uidbuf, pwd->pw_name, sizeof(uidbuf) - 1); } else { if (uid) - sprintf(uidbuf, "uid %d", (int) uid); + sprintf(uidbuf, "uid %d", (int)uid); else sprintf(uidbuf, "root"); } @@ -83,7 +83,7 @@ static void getuidtty(char **userp, char **ttyp) } *userp = uidbuf; - *ttyp = ttynm; + *ttyp = ttynm; } /* @@ -91,8 +91,8 @@ static void getuidtty(char **userp, char **ttyp) */ static int file_isatty(const char *fname) { - struct stat st; - int major; + struct stat st; + int major; if (stat(fname, &st) < 0) return 0; @@ -126,11 +126,11 @@ void broadcast(char *text) char *user; struct utsname name; time_t t; - char *date; + char *date; char *p; char *line = NULL; struct sigaction sa; - int flags; + int flags; char *term = NULL; struct utmpx *utmp; /* @@ -156,7 +156,7 @@ void broadcast(char *text) *p = 0; xasprintf(&line, "\007\r\nBroadcast message from %s@%s %s(%s):\r\n\r\n", - user, name.nodename, tty, date); + user, name.nodename, tty, date); free(tty); /* @@ -189,9 +189,11 @@ void broadcast(char *text) */ if (sigsetjmp(jbuf, 1) == 0) { alarm(2); - flags = O_WRONLY|O_NDELAY|O_NOCTTY; - if (file_isatty(term) && (fd = open(term, flags)) >= 0) { - if (isatty(fd) && (tp = fdopen(fd, "w")) != NULL) { + flags = O_WRONLY | O_NDELAY | O_NOCTTY; + if (file_isatty(term) && + (fd = open(term, flags)) >= 0) { + if (isatty(fd) && + (tp = fdopen(fd, "w")) != NULL) { fputs(line, tp); fputs(text, tp); fflush(tp); diff --git a/src/openrc-shutdown/openrc-shutdown.c b/src/openrc-shutdown/openrc-shutdown.c index 6255a4851..1bdb53476 100644 --- a/src/openrc-shutdown/openrc-shutdown.c +++ b/src/openrc-shutdown/openrc-shutdown.c @@ -25,62 +25,59 @@ #include #include #include -#include -#include #include #include +#include +#include +#include "_usage.h" #include "broadcast.h" #include "einfo.h" -#include "rc.h" #include "helpers.h" #include "misc.h" +#include "rc.h" #include "sysvinit.h" #include "wtmp.h" -#include "_usage.h" const char *applet = NULL; const char *extraopts = NULL; const char getoptstring[] = "cdDfFHKpRrsw" getoptstring_COMMON; -const struct option longopts[] = { - { "cancel", no_argument, NULL, 'c'}, - { "no-write", no_argument, NULL, 'd'}, - { "dry-run", no_argument, NULL, 'D'}, - { "halt", no_argument, NULL, 'H'}, - { "kexec", no_argument, NULL, 'K'}, - { "poweroff", no_argument, NULL, 'p'}, - { "reexec", no_argument, NULL, 'R'}, - { "reboot", no_argument, NULL, 'r'}, - { "single", no_argument, NULL, 's'}, - { "write-only", no_argument, NULL, 'w'}, - longopts_COMMON -}; -const char * const longopts_help[] = { - "cancel a pending shutdown", - "do not write wtmp record", - "print actions instead of executing them", - "halt the system", - "reboot the system using kexec", - "power off the system", - "re-execute init (use after upgrading)", - "reboot the system", - "single user mode", - "write wtmp boot record and exit", - longopts_help_COMMON -}; -const char *usagestring = "" \ - "Usage: openrc-shutdown -c | --cancel\n" \ - " or: openrc-shutdown -R | --reexec\n" \ - " or: openrc-shutdown -w | --write-only\n" \ - " or: openrc-shutdown -H | --halt time\n" \ - " or: openrc-shutdown -K | --kexec time\n" \ - " or: openrc-shutdown -p | --poweroff time\n" \ - " or: openrc-shutdown -r | --reboot time\n" \ - " or: openrc-shutdown -s | --single time"; -const char *exclusive = "Select one of " +const struct option longopts[] = {{"cancel", no_argument, NULL, 'c'}, + {"no-write", no_argument, NULL, 'd'}, + {"dry-run", no_argument, NULL, 'D'}, + {"halt", no_argument, NULL, 'H'}, + {"kexec", no_argument, NULL, 'K'}, + {"poweroff", no_argument, NULL, 'p'}, + {"reexec", no_argument, NULL, 'R'}, + {"reboot", no_argument, NULL, 'r'}, + {"single", no_argument, NULL, 's'}, + {"write-only", no_argument, NULL, 'w'}, + longopts_COMMON}; +const char *const longopts_help[] = {"cancel a pending shutdown", + "do not write wtmp record", + "print actions instead of executing them", + "halt the system", + "reboot the system using kexec", + "power off the system", + "re-execute init (use after upgrading)", + "reboot the system", + "single user mode", + "write wtmp boot record and exit", + longopts_help_COMMON}; +const char *usagestring = "" + "Usage: openrc-shutdown -c | --cancel\n" + " or: openrc-shutdown -R | --reexec\n" + " or: openrc-shutdown -w | --write-only\n" + " or: openrc-shutdown -H | --halt time\n" + " or: openrc-shutdown -K | --kexec time\n" + " or: openrc-shutdown -p | --poweroff time\n" + " or: openrc-shutdown -r | --reboot time\n" + " or: openrc-shutdown -s | --single time"; +const char *exclusive = + "Select one of " "--cancel, --halt, --kexec, --poweroff, --reexec, --reboot, --single or \n" "--write-only"; -const char *nologin_file = RC_SYSCONFDIR"/nologin"; +const char *nologin_file = RC_SYSCONFDIR "/nologin"; const char *shutdown_pid = "/run/openrc-shutdown.pid"; static bool do_cancel = false; @@ -169,11 +166,11 @@ static void sleep_no_interrupt(int seconds) static void stop_shutdown(int sig) { - (void) sig; + (void)sig; unlink(nologin_file); unlink(shutdown_pid); -einfo("Shutdown canceled"); -exit(0); + einfo("Shutdown canceled"); + exit(0); } int main(int argc, char **argv) @@ -194,17 +191,16 @@ int main(int argc, char **argv) FILE *fp; applet = basename_c(argv[0]); - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { - case 'c': - do_cancel = true; + case 'c': + do_cancel = true; cmd_count++; - break; - case 'd': - do_wtmp = false; - break; + break; + case 'd': + do_wtmp = false; + break; case 'D': do_dryrun = true; break; @@ -241,7 +237,7 @@ int main(int argc, char **argv) do_wtmp_only = true; cmd_count++; break; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } } if (geteuid() != 0) @@ -271,20 +267,21 @@ int main(int argc, char **argv) time_arg++; if (strcasecmp(time_arg, "now") == 0) strcpy(time_arg, "0"); - for (ch=time_arg; *ch; ch++) + for (ch = time_arg; *ch; ch++) if ((*ch < '0' || *ch > '9') && *ch != ':') { eerror("%s: invalid time %s", applet, time_arg); usage(EXIT_FAILURE); } if (strchr(time_arg, ':')) { if ((sscanf(time_arg, "%2d:%2d", &hour, &min) != 2) || - (hour > 23) || (min > 59)) { + (hour > 23) || (min > 59)) { eerror("%s: invalid time %s", applet, time_arg); usage(EXIT_FAILURE); } time(&tv); lt = localtime(&tv); - shutdown_delay = (hour * 60 + min) - (lt->tm_hour * 60 + lt->tm_min); + shutdown_delay = + (hour * 60 + min) - (lt->tm_hour * 60 + lt->tm_min); if (shutdown_delay < 0) shutdown_delay += 1440; } else { @@ -293,7 +290,8 @@ int main(int argc, char **argv) fp = fopen(shutdown_pid, "w"); if (!fp) - eerrorx("%s: fopen `%s': %s", applet, shutdown_pid, strerror(errno)); + eerrorx("%s: fopen `%s': %s", applet, shutdown_pid, + strerror(errno)); fprintf(fp, "%d\n", getpid()); fclose(fp); @@ -309,15 +307,16 @@ int main(int argc, char **argv) need_warning = (shutdown_delay % 60 == 0); else if (shutdown_delay > 60) need_warning = (shutdown_delay % 30 == 0); - else if (shutdown_delay > 10) + else if (shutdown_delay > 10) need_warning = (shutdown_delay % 15 == 0); else need_warning = true; if (shutdown_delay <= 5) create_nologin(shutdown_delay); if (need_warning) { - xasprintf(&msg, "\rThe system will %s in %d minutes\r\n", - state, shutdown_delay); + xasprintf(&msg, + "\rThe system will %s in %d minutes\r\n", + state, shutdown_delay); broadcast(msg); free(msg); } diff --git a/src/openrc-shutdown/sysvinit.c b/src/openrc-shutdown/sysvinit.c index 84fc9ec2c..3a0076832 100644 --- a/src/openrc-shutdown/sysvinit.c +++ b/src/openrc-shutdown/sysvinit.c @@ -19,9 +19,9 @@ #include #include #include -#include #include #include +#include #include "einfo.h" #include "sysvinit.h" @@ -33,20 +33,22 @@ static void sysvinit_send_cmd(struct init_request *request) size_t bytes; ssize_t r; - fd = open("/run/initctl", O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); + fd = open("/run/initctl", O_WRONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY); if (fd < 0) { if (errno != ENOENT) - eerror("Failed to open initctl fifo: %s", strerror(errno)); + eerror("Failed to open initctl fifo: %s", + strerror(errno)); return; } - p = (char *) request; + p = (char *)request; bytes = sizeof(*request); do { r = write(fd, p, bytes); if (r < 0) { if ((errno == EAGAIN) || (errno == EINTR)) continue; - eerror("Failed to write to /run/initctl: %s", strerror(errno)); + eerror("Failed to write to /run/initctl: %s", + strerror(errno)); return; } p += r; @@ -61,14 +63,14 @@ void sysvinit_runlevel(char rl) if (!rl) return; - request = (struct init_request) { + request = (struct init_request){ .magic = INIT_MAGIC, .sleeptime = 0, .cmd = INIT_CMD_RUNLVL, .runlevel = rl, }; sysvinit_send_cmd(&request); - return; + return; } /* @@ -76,7 +78,7 @@ void sysvinit_runlevel(char rl) */ void sysvinit_setenv(const char *name, const char *value) { - struct init_request request; + struct init_request request; size_t nl; size_t vl; @@ -86,7 +88,7 @@ void sysvinit_setenv(const char *name, const char *value) nl = strlen(name); if (value) vl = strlen(value); -else + else vl = 0; if (nl + vl + 3 >= (int)sizeof(request.i.data)) diff --git a/src/openrc-shutdown/sysvinit.h b/src/openrc-shutdown/sysvinit.h index 64b4a85cd..564e3362b 100644 --- a/src/openrc-shutdown/sysvinit.h +++ b/src/openrc-shutdown/sysvinit.h @@ -24,28 +24,28 @@ */ #define INIT_MAGIC 0x03091969 -#define INIT_CMD_START 0 -#define INIT_CMD_RUNLVL 1 -#define INIT_CMD_POWERFAIL 2 -#define INIT_CMD_POWERFAILNOW 3 -#define INIT_CMD_POWEROK 4 -#define INIT_CMD_BSD 5 -#define INIT_CMD_SETENV 6 -#define INIT_CMD_UNSETENV 7 +#define INIT_CMD_START 0 +#define INIT_CMD_RUNLVL 1 +#define INIT_CMD_POWERFAIL 2 +#define INIT_CMD_POWERFAILNOW 3 +#define INIT_CMD_POWEROK 4 +#define INIT_CMD_BSD 5 +#define INIT_CMD_SETENV 6 +#define INIT_CMD_UNSETENV 7 /* * This is what BSD 4.4 uses when talking to init. * Linux doesn't use this right now. */ struct init_request_bsd { - char gen_id[8]; /* Beats me.. telnetd uses "fe" */ - char tty_id[16]; /* Tty name minus /dev/tty */ - char host[64]; /* Hostname */ - char term_type[16]; /* Terminal type */ - int signal; /* Signal to send */ - int pid; /* Process to send to */ - char exec_name[128]; /* Program to execute */ - char reserved[128]; /* For future expansion. */ + char gen_id[8]; /* Beats me.. telnetd uses "fe" */ + char tty_id[16]; /* Tty name minus /dev/tty */ + char host[64]; /* Hostname */ + char term_type[16]; /* Terminal type */ + int signal; /* Signal to send */ + int pid; /* Process to send to */ + char exec_name[128]; /* Program to execute */ + char reserved[128]; /* For future expansion. */ }; /* @@ -56,13 +56,13 @@ struct init_request_bsd { * struct to be 384 bytes. */ struct init_request { - int magic; /* Magic number */ - int cmd; /* What kind of request */ - int runlevel; /* Runlevel to change to */ - int sleeptime; /* Time between TERM and KILL */ + int magic; /* Magic number */ + int cmd; /* What kind of request */ + int runlevel; /* Runlevel to change to */ + int sleeptime; /* Time between TERM and KILL */ union { - struct init_request_bsd bsd; - char data[368]; + struct init_request_bsd bsd; + char data[368]; } i; }; diff --git a/src/openrc/rc-logger.c b/src/openrc/rc-logger.c index b00550a7e..50d980e8e 100644 --- a/src/openrc/rc-logger.c +++ b/src/openrc/rc-logger.c @@ -16,8 +16,8 @@ * except according to the terms contained in the LICENSE file. */ -#include #include +#include #include #include @@ -31,25 +31,26 @@ #include #include -#if defined(__linux__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) \ - || defined(__GNU__) -# include +#if defined(__linux__) || \ + (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) || \ + defined(__GNU__) +#include #elif defined(__NetBSD__) || defined(__OpenBSD__) -# include +#include #else -# include +#include #endif #include "einfo.h" -#include "rc-logger.h" +#include "misc.h" #include "queue.h" +#include "rc-logger.h" #include "rc.h" -#include "misc.h" #define TMPLOG RC_SVCDIR "/rc.log" #define DEFAULTLOG "/var/log/rc.log" -static int signal_pipe[2] = { -1, -1 }; +static int signal_pipe[2] = {-1, -1}; static int fd_stdout = -1; static int fd_stderr = -1; static const char *runlevel = NULL; @@ -64,8 +65,7 @@ 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; @@ -87,7 +87,7 @@ write_log(int logfd, const char *buffer, size_t bytes) } if (!in_escape) { - if (!isprint((int) *p) && *p != '\n') + if (!isprint((int)*p) && *p != '\n') goto cont; if (write(logfd, p++, 1) == -1) eerror("write: %s", strerror(errno)); @@ -101,8 +101,7 @@ write_log(int logfd, const char *buffer, size_t bytes) } } -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); @@ -111,8 +110,7 @@ write_time(FILE *f, const char *s) fflush(f); } -void -rc_logger_close(void) +void rc_logger_close(void) { int sig = SIGTERM; @@ -132,8 +130,7 @@ 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; @@ -154,18 +151,17 @@ rc_logger_open(const char *level) if (pipe(signal_pipe) == -1) eerrorx("pipe: %s", strerror(errno)); for (i = 0; i < 2; i++) - if ((s = fcntl (signal_pipe[i], F_GETFD, 0) == -1 || - fcntl (signal_pipe[i], F_SETFD, s | FD_CLOEXEC) == -1)) - eerrorx("fcntl: %s", strerror (errno)); + if ((s = fcntl(signal_pipe[i], F_GETFD, 0) == -1 || + fcntl(signal_pipe[i], F_SETFD, s | FD_CLOEXEC) == -1)) + eerrorx("fcntl: %s", strerror(errno)); if (isatty(STDOUT_FILENO)) { tcgetattr(STDOUT_FILENO, &tt); ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); if (openpty(&rc_logger_tty, &slave_tty, NULL, &tt, &ws)) return; - } else - if (openpty(&rc_logger_tty, &slave_tty, NULL, NULL, NULL)) - return; + } else if (openpty(&rc_logger_tty, &slave_tty, NULL, NULL, NULL)) + return; if ((s = fcntl(rc_logger_tty, F_GETFD, 0)) == 0) fcntl(rc_logger_tty, F_SETFD, s | FD_CLOEXEC); @@ -189,7 +185,7 @@ rc_logger_open(const char *level) else { free(logbuf); logbuf_size = BUFSIZ * 10; - logbuf = xmalloc(sizeof (char) * logbuf_size); + logbuf = xmalloc(sizeof(char) * logbuf_size); logbuf_len = 0; } @@ -199,9 +195,8 @@ 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) @@ -214,17 +209,18 @@ rc_logger_open(const char *level) eerror("write: %s", strerror(errno)); if (log) - write_log(fileno (log), buffer, bytes); + write_log(fileno(log), buffer, bytes); else { if (logbuf_size - logbuf_len < bytes) { logbuf_size += BUFSIZ * 10; - logbuf = xrealloc(logbuf, - sizeof(char ) * - logbuf_size); + logbuf = xrealloc( + logbuf, + sizeof(char) * + logbuf_size); } - memcpy(logbuf + logbuf_len, - buffer, bytes); + memcpy(logbuf + logbuf_len, buffer, + bytes); logbuf_len += bytes; } } @@ -251,22 +247,28 @@ rc_logger_open(const char *level) logfile = DEFAULTLOG; if (!strcmp(logfile, TMPLOG)) { eerror("Cowardly refusing to concatenate a logfile into itself."); - eerrorx("Please change rc_log_path to something other than %s to get rid of this message", TMPLOG); + eerrorx("Please change rc_log_path to something other than %s to get rid of this message", + TMPLOG); } if ((plog = fopen(logfile, "ae"))) { if ((log = fopen(TMPLOG, "re"))) { - while ((bytes = fread(buffer, sizeof(*buffer), BUFSIZ, log)) > 0) { - if (fwrite(buffer, sizeof(*buffer), bytes, plog) < bytes) { + while ((bytes = fread(buffer, sizeof(*buffer), + BUFSIZ, log)) > 0) { + if (fwrite(buffer, sizeof(*buffer), + bytes, plog) < bytes) { log_error = 1; - eerror("Error: write(%s) failed: %s", logfile, strerror(errno)); + eerror("Error: write(%s) failed: %s", + logfile, + strerror(errno)); break; } } fclose(log); } else { log_error = 1; - eerror("Error: fopen(%s) failed: %s", TMPLOG, strerror(errno)); + eerror("Error: fopen(%s) failed: %s", TMPLOG, + strerror(errno)); } fclose(plog); @@ -275,19 +277,26 @@ rc_logger_open(const char *level) * logfile or its basedir may be read-only during sysinit and * shutdown so skip the error in this case */ - if (errno != EROFS && ((strcmp(level, RC_LEVEL_SHUTDOWN) != 0) && (strcmp(level, RC_LEVEL_SYSINIT) != 0))) { + if (errno != EROFS && + ((strcmp(level, RC_LEVEL_SHUTDOWN) != 0) && + (strcmp(level, RC_LEVEL_SYSINIT) != 0))) { log_error = 1; - eerror("Error: fopen(%s) failed: %s", logfile, strerror(errno)); + eerror("Error: fopen(%s) failed: %s", logfile, + strerror(errno)); } } /* Try to keep the temporary log in case of errors */ if (!log_error) { - if (errno != EROFS && ((strcmp(level, RC_LEVEL_SHUTDOWN) != 0) && (strcmp(level, RC_LEVEL_SYSINIT) != 0))) + if (errno != EROFS && + ((strcmp(level, RC_LEVEL_SHUTDOWN) != 0) && + (strcmp(level, RC_LEVEL_SYSINIT) != 0))) if (unlink(TMPLOG) == -1) - eerror("Error: unlink(%s) failed: %s", TMPLOG, strerror(errno)); + eerror("Error: unlink(%s) failed: %s", + TMPLOG, strerror(errno)); } else if (exists(TMPLOG)) - eerrorx("Warning: temporary logfile left behind: %s", TMPLOG); + eerrorx("Warning: temporary logfile left behind: %s", + TMPLOG); exit(0); /* NOTREACHED */ @@ -303,8 +312,7 @@ rc_logger_open(const char *level) fcntl(fd_stderr, F_SETFD, s | FD_CLOEXEC); dup2(slave_tty, STDOUT_FILENO); dup2(slave_tty, STDERR_FILENO); - if (slave_tty != STDIN_FILENO && - slave_tty != STDOUT_FILENO && + if (slave_tty != STDIN_FILENO && slave_tty != STDOUT_FILENO && slave_tty != STDERR_FILENO) close(slave_tty); close(signal_pipe[0]); diff --git a/src/openrc/rc.c b/src/openrc/rc.c index c1ca1bd7a..8b1156622 100644 --- a/src/openrc/rc.c +++ b/src/openrc/rc.c @@ -20,65 +20,62 @@ * except according to the terms contained in the LICENSE file. */ -#include #include #include #include +#include #include #include -#include -#include #include +#include +#include #include #include #include #include +#include #include #include #include -#include #include #include #include #include #include "einfo.h" -#include "queue.h" -#include "rc.h" -#include "rc-logger.h" #include "misc.h" #include "plugin.h" +#include "queue.h" +#include "rc-logger.h" +#include "rc.h" -#include "version.h" #include "_usage.h" +#include "version.h" const char *extraopts = NULL; const char getoptstring[] = "a:no:s:S" getoptstring_COMMON; -const struct option longopts[] = { - { "no-stop", 0, NULL, 'n' }, - { "override", 1, NULL, 'o' }, - { "service", 1, NULL, 's' }, - { "sys", 0, NULL, 'S' }, - longopts_COMMON -}; -const char * const longopts_help[] = { +const struct option longopts[] = {{"no-stop", 0, NULL, 'n'}, + {"override", 1, NULL, 'o'}, + {"service", 1, NULL, 's'}, + {"sys", 0, NULL, 'S'}, + longopts_COMMON}; +const char *const longopts_help[] = { "do not stop any services", "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 -}; -const char *usagestring = "" \ - "Usage: openrc [options] []"; + longopts_help_COMMON}; +const char *usagestring = "" + "Usage: openrc [options] []"; -#define INITSH RC_LIBEXECDIR "/sh/init.sh" -#define INITEARLYSH RC_LIBEXECDIR "/sh/init-early.sh" +#define INITSH RC_LIBEXECDIR "/sh/init.sh" +#define INITEARLYSH RC_LIBEXECDIR "/sh/init-early.sh" -#define INTERACTIVE RC_SVCDIR "/interactive" +#define INTERACTIVE RC_SVCDIR "/interactive" -#define DEVBOOT "/dev/.rcboot" +#define DEVBOOT "/dev/.rcboot" const char *applet = NULL; static RC_STRINGLIST *main_hotplugged_services; @@ -94,8 +91,7 @@ struct termios *termios_orig = NULL; RC_PIDLIST service_pids; -static void -clean_failed(void) +static void clean_failed(void) { DIR *dp; struct dirent *d; @@ -106,28 +102,26 @@ clean_failed(void) while ((d = readdir(dp))) { if (d->d_name[0] == '.' && (d->d_name[1] == '\0' || - (d->d_name[1] == '.' && d->d_name[2] == '\0'))) + (d->d_name[1] == '.' && d->d_name[2] == '\0'))) continue; xasprintf(&path, RC_SVCDIR "/failed/%s", d->d_name); if (unlink(path)) - eerror("%s: unlink `%s': %s", - applet, path, strerror(errno)); + eerror("%s: unlink `%s': %s", applet, path, + strerror(errno)); free(path); } closedir(dp); } } -static void -cleanup(void) +static void cleanup(void) { RC_PID *p1 = LIST_FIRST(&service_pids); RC_PID *p2; - if (!rc_in_logger && !rc_in_plugin && - applet && (strcmp(applet, "rc") == 0 || strcmp(applet, "openrc") == 0)) - { + if (!rc_in_logger && !rc_in_plugin && applet && + (strcmp(applet, "rc") == 0 || strcmp(applet, "openrc") == 0)) { if (hook_out) rc_plugin_run(hook_out, runlevel); @@ -160,8 +154,7 @@ cleanup(void) free(runlevel); } -static char -read_key(bool block) +static char read_key(bool block) { struct termios termios; char c = 0; @@ -192,8 +185,7 @@ read_key(bool block) return c; } -static bool -want_interactive(void) +static bool want_interactive(void) { char c; static bool gotinteractive; @@ -211,16 +203,14 @@ want_interactive(void) return (c == 'I' || c == 'i') ? true : false; } -static void -mark_interactive(void) +static void mark_interactive(void) { FILE *fp = fopen(INTERACTIVE, "w"); if (fp) fclose(fp); } -static void -run_program(const char *prog) +static void run_program(const char *prog) { struct sigaction sa; sigset_t full; @@ -255,7 +245,7 @@ run_program(const char *prog) execl(prog, prog, (char *)NULL); eerror("%s: unable to exec `%s': %s", applet, prog, - strerror(errno)); + strerror(errno)); _exit(EXIT_FAILURE); } @@ -265,8 +255,7 @@ run_program(const char *prog) eerrorx("%s: failed to exec `%s'", applet, prog); } -static void -open_shell(void) +static void open_shell(void) { const char *shell; struct passwd *pw; @@ -275,11 +264,10 @@ open_shell(void) const char *sys = rc_sys(); /* VSERVER systems cannot really drop to shells */ - if (sys && strcmp(sys, RC_SYS_VSERVER) == 0) - { - execlp("halt", "halt", "-f", (char *) NULL); - eerrorx("%s: unable to exec `halt -f': %s", - applet, strerror(errno)); + if (sys && strcmp(sys, RC_SYS_VSERVER) == 0) { + execlp("halt", "halt", "-f", (char *)NULL); + eerrorx("%s: unable to exec `halt -f': %s", applet, + strerror(errno)); } #endif @@ -298,20 +286,16 @@ open_shell(void) run_program(shell); } -static bool -set_krunlevel(const char *level) +static bool set_krunlevel(const char *level) { FILE *fp; - if (!level || - strcmp(level, getenv ("RC_BOOTLEVEL")) == 0 || + if (!level || strcmp(level, getenv("RC_BOOTLEVEL")) == 0 || strcmp(level, RC_LEVEL_SINGLE) == 0 || - strcmp(level, RC_LEVEL_SYSINIT) == 0) - { - if (exists(RC_KRUNLEVEL) && - unlink(RC_KRUNLEVEL) != 0) + strcmp(level, RC_LEVEL_SYSINIT) == 0) { + if (exists(RC_KRUNLEVEL) && unlink(RC_KRUNLEVEL) != 0) eerror("unlink `%s': %s", RC_KRUNLEVEL, - strerror(errno)); + strerror(errno)); return false; } @@ -347,29 +331,26 @@ static char *get_krunlevel(void) return buffer; } -static void -add_pid(pid_t pid) +static void add_pid(pid_t pid) { RC_PID *p = xmalloc(sizeof(*p)); p->pid = pid; LIST_INSERT_HEAD(&service_pids, p, entries); } -static void -remove_pid(pid_t pid) +static void remove_pid(pid_t pid) { RC_PID *p; LIST_FOREACH(p, &service_pids, entries) - if (p->pid == pid) { - LIST_REMOVE(p, entries); - free(p); - return; - } + if (p->pid == pid) { + LIST_REMOVE(p, entries); + free(p); + return; + } } -static void -wait_for_services(void) +static void wait_for_services(void) { for (;;) { while (waitpid(0, 0, 0) != -1) @@ -379,8 +360,7 @@ wait_for_services(void) } } -static void -handle_signal(int sig) +static void handle_signal(int sig) { int serrno = errno; char *signame = NULL; @@ -436,7 +416,7 @@ handle_signal(int sig) /* Kill any running services we have started */ LIST_FOREACH(pi, &service_pids, entries) - kill(pi->pid, SIGTERM); + kill(pi->pid, SIGTERM); /* Notify plugins we are aborting */ rc_plugin_run(RC_HOOK_ABORT, NULL); @@ -452,8 +432,7 @@ handle_signal(int sig) errno = serrno; } -static void -do_sysinit() +static void do_sysinit() { struct utsname uts; const char *sys; @@ -466,15 +445,12 @@ do_sysinit() uname(&uts); printf("\n %sOpenRC %s" VERSION "%s is starting up %s", - ecolor(ECOLOR_GOOD), ecolor(ECOLOR_HILITE), - ecolor(ECOLOR_NORMAL), ecolor(ECOLOR_BRACKET)); + ecolor(ECOLOR_GOOD), ecolor(ECOLOR_HILITE), + ecolor(ECOLOR_NORMAL), ecolor(ECOLOR_BRACKET)); #ifdef BRANDING printf(BRANDING " (%s)", uts.machine); #else - printf("%s %s (%s)", - uts.sysname, - uts.release, - uts.machine); + printf("%s %s (%s)", uts.sysname, uts.release, uts.machine); #endif if ((sys = rc_sys())) @@ -482,10 +458,9 @@ do_sysinit() printf("%s\n\n", ecolor(ECOLOR_NORMAL)); - if (!rc_yesno(getenv ("EINFO_QUIET")) && - rc_conf_yesno("rc_interactive")) + if (!rc_yesno(getenv("EINFO_QUIET")) && rc_conf_yesno("rc_interactive")) printf("Press %sI%s to enter interactive boot mode\n\n", - ecolor(ECOLOR_GOOD), ecolor(ECOLOR_NORMAL)); + ecolor(ECOLOR_GOOD), ecolor(ECOLOR_NORMAL)); setenv("RC_RUNLEVEL", RC_LEVEL_SYSINIT, 1); run_program(INITSH); @@ -499,8 +474,7 @@ do_sysinit() eerrorx("failed to load deptree"); } -static bool -runlevel_config(const char *service, const char *level) +static bool runlevel_config(const char *service, const char *level) { char *init = rc_service_resolve(service); char *conf, *dir; @@ -515,10 +489,11 @@ runlevel_config(const char *service, const char *level) return retval; } -static void -do_stop_services(RC_STRINGLIST *types_nw, RC_STRINGLIST *start_services, - const RC_STRINGLIST *stop_services, const RC_DEPTREE *deptree, - const char *newlevel, bool parallel, bool going_down) +static void do_stop_services(RC_STRINGLIST *types_nw, + RC_STRINGLIST *start_services, + const RC_STRINGLIST *stop_services, + const RC_DEPTREE *deptree, const char *newlevel, + bool parallel, bool going_down) { pid_t pid; RC_STRING *service, *svc1, *svc2; @@ -536,8 +511,7 @@ do_stop_services(RC_STRINGLIST *types_nw, RC_STRINGLIST *start_services, crashed = rc_conf_yesno("rc_crashed_stop"); nostop = rc_stringlist_split(rc_conf_value("rc_nostop"), " "); - TAILQ_FOREACH_REVERSE(service, stop_services, rc_stringlist, entries) - { + TAILQ_FOREACH_REVERSE(service, stop_services, rc_stringlist, entries) { state = rc_service_state(service->value); if (state & RC_SERVICE_STOPPED || state & RC_SERVICE_FAILED) continue; @@ -550,9 +524,8 @@ do_stop_services(RC_STRINGLIST *types_nw, RC_STRINGLIST *start_services, kwords = rc_deptree_depend(deptree, service->value, "keyword"); if (rc_stringlist_find(kwords, "-stop") || rc_stringlist_find(kwords, "nostop") || - (going_down && - (rc_stringlist_find(kwords, "-shutdown") || - rc_stringlist_find(kwords, "noshutdown")))) + (going_down && (rc_stringlist_find(kwords, "-shutdown") || + rc_stringlist_find(kwords, "noshutdown")))) nstop = true; else nstop = false; @@ -564,8 +537,7 @@ do_stop_services(RC_STRINGLIST *types_nw, RC_STRINGLIST *start_services, /* If the service has crashed, skip further checks and just stop it */ - if (crashed && - rc_service_daemons_crashed(service->value)) + if (crashed && rc_service_daemons_crashed(service->value)) goto stop; /* If we're in the start list then don't bother stopping us */ @@ -576,11 +548,11 @@ do_stop_services(RC_STRINGLIST *types_nw, RC_STRINGLIST *start_services, * be stopped if we have a runlevel * configuration file for either the current * or next so we use the correct one. */ - if (!runlevel_config(service->value,runlevel) && - !runlevel_config(service->value,newlevel)) + if (!runlevel_config(service->value, + runlevel) && + !runlevel_config(service->value, newlevel)) continue; - } - else + } else continue; } @@ -589,14 +561,15 @@ do_stop_services(RC_STRINGLIST *types_nw, RC_STRINGLIST *start_services, if (!svc1) { tmplist = rc_stringlist_new(); rc_stringlist_add(tmplist, service->value); - deporder = rc_deptree_depends(deptree, types_nw, - tmplist, newlevel ? newlevel : runlevel, - RC_DEP_STRICT | RC_DEP_TRACE); + deporder = rc_deptree_depends( + deptree, types_nw, 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); + svc1->value); if (svc2) break; } @@ -621,8 +594,8 @@ do_stop_services(RC_STRINGLIST *types_nw, RC_STRINGLIST *start_services, rc_stringlist_free(nostop); } -static void -do_start_services(const RC_STRINGLIST *start_services, bool parallel) +static void do_start_services(const RC_STRINGLIST *start_services, + bool parallel) { RC_STRING *service; pid_t pid; @@ -645,29 +618,35 @@ do_start_services(const RC_STRINGLIST *start_services, bool parallel) if (crashed && rc_service_daemons_crashed(service->value)) rc_service_mark(service->value, - RC_SERVICE_STOPPED); + RC_SERVICE_STOPPED); else - continue; + continue; } if (!interactive) interactive = want_interactive(); if (interactive) { - interactive_retry: +interactive_retry: printf("\n"); - einfo("About to start the service %s", - service->value); + einfo("About to start the service %s", service->value); eindent(); 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; - case '3': interactive = false; break; - case '4': open_shell(); goto interactive_retry; - default: goto interactive_option; + case '1': + break; + case '2': + continue; + case '3': + interactive = false; + break; + case '4': + open_shell(); + goto interactive_retry; + default: + goto interactive_option; } } @@ -685,20 +664,17 @@ do_start_services(const RC_STRINGLIST *start_services, bool parallel) } /* Store our interactive status for boot */ - if (interactive && - (strcmp(runlevel, RC_LEVEL_SYSINIT) == 0 || - strcmp(runlevel, getenv("RC_BOOTLEVEL")) == 0)) + if (interactive && (strcmp(runlevel, RC_LEVEL_SYSINIT) == 0 || + strcmp(runlevel, getenv("RC_BOOTLEVEL")) == 0)) mark_interactive(); else { if (exists(INTERACTIVE)) unlink(INTERACTIVE); } - } #ifdef RC_DEBUG -static void -handle_bad_signal(int sig) +static void handle_bad_signal(int sig) { char pid[10]; int status; @@ -711,8 +687,8 @@ handle_bad_signal(int sig) case 0: sprintf(pid, "%i", crashed_pid); printf("\nAuto launching gdb!\n\n"); - _exit(execlp("gdb", "gdb", "--quiet", "--pid", pid, - "-ex", "bt full", NULL)); + _exit(execlp("gdb", "gdb", "--quiet", "--pid", pid, "-ex", + "bt full", NULL)); /* NOTREACHED */ default: wait(&status); @@ -771,15 +747,15 @@ int main(int argc, char **argv) /* complain about old configuration settings if they exist */ if (exists(RC_CONF_OLD)) { ewarn("%s still exists on your system and should be removed.", - RC_CONF_OLD); - ewarn("Please migrate to the appropriate settings in %s", RC_CONF); + RC_CONF_OLD); + ewarn("Please migrate to the appropriate settings in %s", + RC_CONF); } argc++; argv--; - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { case 'n': nostop = true; @@ -800,7 +776,7 @@ int main(int argc, char **argv) newlevel = rc_service_resolve(optarg); if (!newlevel) eerrorx("%s: service `%s' does not exist", - applet, optarg); + applet, optarg); argv += optind - 1; *argv = newlevel; execv(*argv, argv); @@ -812,7 +788,7 @@ int main(int argc, char **argv) printf("%s\n", systype); exit(EXIT_SUCCESS); /* NOTREACHED */ - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } } @@ -866,10 +842,8 @@ int main(int argc, char **argv) /* Now we start handling our children */ signal_setup(SIGCHLD, handle_signal); - if (newlevel && - (strcmp(newlevel, RC_LEVEL_SHUTDOWN) == 0 || - strcmp(newlevel, RC_LEVEL_SINGLE) == 0)) - { + if (newlevel && (strcmp(newlevel, RC_LEVEL_SHUTDOWN) == 0 || + strcmp(newlevel, RC_LEVEL_SINGLE) == 0)) { going_down = true; if (!exists(RC_KRUNLEVEL)) set_krunlevel(runlevel); @@ -880,8 +854,7 @@ int main(int argc, char **argv) /* We should not use krunlevel in sysinit or boot runlevels */ if (!newlevel || (strcmp(newlevel, RC_LEVEL_SYSINIT) != 0 && - strcmp(newlevel, getenv("RC_BOOTLEVEL")) != 0)) - { + strcmp(newlevel, getenv("RC_BOOTLEVEL")) != 0)) { krunlevel = get_krunlevel(); if (krunlevel) { newlevel = krunlevel; @@ -942,8 +915,8 @@ int main(int argc, char **argv) if (mkdir(RC_STOPPING, 0755) != 0) { if (errno == EACCES) eerrorx("%s: superuser access required", applet); - eerrorx("%s: failed to create stopping dir `%s': %s", - applet, RC_STOPPING, strerror(errno)); + eerrorx("%s: failed to create stopping dir `%s': %s", applet, + RC_STOPPING, strerror(errno)); } /* Create a list of all services which we could stop (assuming @@ -970,8 +943,9 @@ int main(int argc, char **argv) rc_stringlist_add(main_types_nwua, "iafter"); if (main_stop_services) { - tmplist = rc_deptree_depends(main_deptree, main_types_nwua, main_stop_services, - runlevel, depoptions | RC_DEP_STOP); + tmplist = rc_deptree_depends(main_deptree, main_types_nwua, + main_stop_services, runlevel, + depoptions | RC_DEP_STOP); rc_stringlist_free(main_stop_services); main_stop_services = tmplist; } @@ -982,31 +956,29 @@ int main(int argc, char **argv) * won't actually be starting them all. */ main_hotplugged_services = rc_services_in_state(RC_SERVICE_HOTPLUGGED); - main_start_services = rc_services_in_runlevel_stacked(newlevel ? - newlevel : runlevel); + main_start_services = + rc_services_in_runlevel_stacked(newlevel ? newlevel : runlevel); if (strcmp(newlevel ? newlevel : runlevel, RC_LEVEL_SHUTDOWN) != 0 && - strcmp(newlevel ? newlevel : runlevel, RC_LEVEL_SYSINIT) != 0) - { + strcmp(newlevel ? newlevel : runlevel, RC_LEVEL_SYSINIT) != 0) { tmplist = rc_services_in_runlevel(RC_LEVEL_SYSINIT); TAILQ_CONCAT(main_start_services, tmplist, entries); free(tmplist); /* If we are NOT headed for the single-user runlevel... */ - if (strcmp(newlevel ? newlevel : runlevel, - RC_LEVEL_SINGLE) != 0) - { + if (strcmp(newlevel ? newlevel : runlevel, RC_LEVEL_SINGLE) != + 0) { /* If we are NOT headed for the boot runlevel... */ - if (strcmp(newlevel ? newlevel : runlevel, - bootlevel) != 0) - { + if (strcmp(newlevel ? newlevel : runlevel, bootlevel) != + 0) { tmplist = rc_services_in_runlevel(bootlevel); - TAILQ_CONCAT(main_start_services, tmplist, entries); + TAILQ_CONCAT(main_start_services, tmplist, + entries); free(tmplist); } if (main_hotplugged_services) { TAILQ_FOREACH(service, main_hotplugged_services, - entries) - rc_stringlist_addu(main_start_services, - service->value); + entries) + rc_stringlist_addu(main_start_services, + service->value); } } } @@ -1015,14 +987,16 @@ int main(int argc, char **argv) /* Now stop the services that shouldn't be running */ if (main_stop_services && !nostop) - do_stop_services(main_types_nw, main_start_services, main_stop_services, main_deptree, newlevel, parallel, going_down); + do_stop_services(main_types_nw, main_start_services, + main_stop_services, main_deptree, newlevel, + parallel, going_down); /* Wait for our services to finish */ wait_for_services(); /* Notify the plugins we have finished */ rc_plugin_run(RC_HOOK_RUNLEVEL_STOP_OUT, - going_down ? newlevel : runlevel); + going_down ? newlevel : runlevel); hook_out = 0; rmdir(RC_STOPPING); @@ -1049,7 +1023,7 @@ int main(int argc, char **argv) /* Re-add our hotplugged services if they stopped */ if (main_hotplugged_services) TAILQ_FOREACH(service, main_hotplugged_services, entries) - rc_service_mark(service->value, RC_SERVICE_HOTPLUGGED); + rc_service_mark(service->value, RC_SERVICE_HOTPLUGGED); #ifdef __linux__ /* If the "noinit" parameter was passed on the kernel command line then @@ -1070,14 +1044,17 @@ int main(int argc, char **argv) /* Loop through them in reverse order. */ RC_STRING *rlevel; - TAILQ_FOREACH_REVERSE(rlevel, runlevel_chain, rc_stringlist, entries) - { + TAILQ_FOREACH_REVERSE(rlevel, runlevel_chain, rc_stringlist, + entries) { /* Get a list of all the services in that runlevel */ - RC_STRINGLIST *run_services = rc_services_in_runlevel(rlevel->value); + RC_STRINGLIST *run_services = + rc_services_in_runlevel(rlevel->value); /* Start those services. */ rc_stringlist_sort(&run_services); - deporder = rc_deptree_depends(main_deptree, main_types_nwua, run_services, rlevel->value, depoptions | RC_DEP_START); + deporder = rc_deptree_depends( + main_deptree, main_types_nwua, run_services, + rlevel->value, depoptions | RC_DEP_START); rc_stringlist_free(run_services); run_services = deporder; do_start_services(run_services, parallel); diff --git a/src/rc-abort/rc-abort.c b/src/rc-abort/rc-abort.c index 39a22c944..1cee1786b 100644 --- a/src/rc-abort/rc-abort.c +++ b/src/rc-abort/rc-abort.c @@ -10,16 +10,16 @@ * except according to the terms contained in the LICENSE file. */ -#include #include +#include -#include #include +#include #include +#include #include #include #include -#include #include #include #include @@ -34,8 +34,8 @@ int main(void) if (p && sscanf(p, "%d", &pid) == 1) { if (kill(pid, SIGUSR1) != 0) - eerrorx("rc-abort: failed to signal parent %d: %s", - pid, strerror(errno)); + eerrorx("rc-abort: failed to signal parent %d: %s", pid, + strerror(errno)); return EXIT_SUCCESS; } diff --git a/src/rc-depend/rc-depend.c b/src/rc-depend/rc-depend.c index 9f833b70c..ebaae37f1 100644 --- a/src/rc-depend/rc-depend.c +++ b/src/rc-depend/rc-depend.c @@ -31,26 +31,21 @@ #include #include +#include "_usage.h" #include "einfo.h" +#include "misc.h" #include "queue.h" #include "rc.h" -#include "misc.h" -#include "_usage.h" const char *applet = NULL; const char *extraopts = NULL; const char getoptstring[] = "aot:suTF:" getoptstring_COMMON; const struct option longopts[] = { - { "starting", 0, NULL, 'a'}, - { "stopping", 0, NULL, 'o'}, - { "type", 1, NULL, 't'}, - { "notrace", 0, NULL, 'T'}, - { "strict", 0, NULL, 's'}, - { "update", 0, NULL, 'u'}, - { "deptree-file", 1, NULL, 'F'}, - longopts_COMMON -}; -const char * const longopts_help[] = { + {"starting", 0, NULL, 'a'}, {"stopping", 0, NULL, 'o'}, + {"type", 1, NULL, 't'}, {"notrace", 0, NULL, 'T'}, + {"strict", 0, NULL, 's'}, {"update", 0, NULL, 'u'}, + {"deptree-file", 1, NULL, 'F'}, longopts_COMMON}; +const char *const longopts_help[] = { "Order services as if runlevel is starting", "Order services as if runlevel is stopping", "Type(s) of dependency to list", @@ -58,8 +53,7 @@ const char * const longopts_help[] = { "Only use what is in the runlevels", "Force an update of the dependency tree", "File to load cached deptree from", - longopts_help_COMMON -}; + longopts_help_COMMON}; const char *usagestring = NULL; int main(int argc, char **argv) @@ -79,9 +73,8 @@ int main(int argc, char **argv) applet = basename_c(argv[0]); types = rc_stringlist_new(); - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { case 'a': options |= RC_DEP_START; @@ -106,7 +99,7 @@ int main(int argc, char **argv) deptree_file = xstrdup(optarg); break; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } } @@ -129,7 +122,7 @@ int main(int argc, char **argv) depends = rc_deptree_depends(deptree, NULL, list, runlevel, 0); if (!depends && errno == ENOENT) eerror("no dependency info for service `%s'", - argv[optind]); + argv[optind]); else rc_stringlist_add(services, argv[optind]); @@ -153,19 +146,18 @@ int main(int argc, char **argv) rc_stringlist_add(types, "iuse"); } - depends = rc_deptree_depends(deptree, types, services, - runlevel, options); + depends = + rc_deptree_depends(deptree, types, services, runlevel, options); if (TAILQ_FIRST(depends)) { TAILQ_FOREACH(s, depends, entries) { if (first) first = false; else - printf (" "); - printf ("%s", s->value); - + printf(" "); + printf("%s", s->value); } - printf ("\n"); + printf("\n"); } rc_stringlist_free(types); diff --git a/src/rc-service/rc-service.c b/src/rc-service/rc-service.c index 69acac192..c4a5cecc3 100644 --- a/src/rc-service/rc-service.c +++ b/src/rc-service/rc-service.c @@ -21,31 +21,29 @@ #include #include +#include "_usage.h" #include "einfo.h" +#include "misc.h" #include "queue.h" #include "rc.h" -#include "misc.h" -#include "_usage.h" const char *applet = NULL; const char *extraopts = NULL; const char getoptstring[] = "cdDe:ilr:INsSZ" getoptstring_COMMON; -const struct option longopts[] = { - { "debug", 0, NULL, 'd' }, - { "nodeps", 0, NULL, 'D' }, - { "exists", 1, NULL, 'e' }, - { "ifcrashed", 0, NULL, 'c' }, - { "ifexists", 0, NULL, 'i' }, - { "ifinactive", 0, NULL, 'I' }, - { "ifnotstarted", 0, NULL, 'N' }, - { "ifstarted", 0, NULL, 's' }, - { "ifstopped", 0, NULL, 'S' }, - { "list", 0, NULL, 'l' }, - { "resolve", 1, NULL, 'r' }, - { "dry-run", 0, NULL, 'Z' }, - longopts_COMMON -}; -const char * const longopts_help[] = { +const struct option longopts[] = {{"debug", 0, NULL, 'd'}, + {"nodeps", 0, NULL, 'D'}, + {"exists", 1, NULL, 'e'}, + {"ifcrashed", 0, NULL, 'c'}, + {"ifexists", 0, NULL, 'i'}, + {"ifinactive", 0, NULL, 'I'}, + {"ifnotstarted", 0, NULL, 'N'}, + {"ifstarted", 0, NULL, 's'}, + {"ifstopped", 0, NULL, 'S'}, + {"list", 0, NULL, 'l'}, + {"resolve", 1, NULL, 'r'}, + {"dry-run", 0, NULL, 'Z'}, + longopts_COMMON}; +const char *const longopts_help[] = { "set xtrace when running the command", "ignore dependencies", "tests if the service exists or not", @@ -58,12 +56,12 @@ const char * const longopts_help[] = { "list all available services", "resolve the service name to an init script", "dry run (show what would happen)", - longopts_help_COMMON -}; -const char *usagestring = "" \ - "Usage: rc-service [options] [-i] ...\n" \ - " or: rc-service [options] -e \n" \ - " or: rc-service [options] -l\n" \ + longopts_help_COMMON}; +const char *usagestring = + "" + "Usage: rc-service [options] [-i] ...\n" + " or: rc-service [options] -e \n" + " or: rc-service [options] -l\n" " or: rc-service [options] -r "; int main(int argc, char **argv) @@ -84,9 +82,8 @@ int main(int argc, char **argv) /* Ensure that we are only quiet when explicitly told to be */ unsetenv("EINFO_QUIET"); - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { case 'd': setenv("RC_DEBUG", "yes", 1); @@ -118,7 +115,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; rc_stringlist_sort(&list); TAILQ_FOREACH(s, list, entries) - printf("%s\n", s->value); + printf("%s\n", s->value); rc_stringlist_free(list); return EXIT_SUCCESS; /* NOTREACHED */ @@ -140,7 +137,7 @@ int main(int argc, char **argv) setenv("IN_DRYRUN", "yes", 1); break; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } } @@ -154,7 +151,8 @@ int main(int argc, char **argv) eerrorx("%s: service `%s' does not exist", applet, *argv); } state = rc_service_state(*argv); - if (if_crashed && !(rc_service_daemons_crashed(*argv) && errno != EACCES)) + if (if_crashed && + !(rc_service_daemons_crashed(*argv) && errno != EACCES)) return 0; if (if_inactive && !(state & RC_SERVICE_INACTIVE)) return 0; diff --git a/src/rc-status/rc-status.c b/src/rc-status/rc-status.c index d35c84290..dc36922d8 100644 --- a/src/rc-status/rc-status.c +++ b/src/rc-status/rc-status.c @@ -16,17 +16,17 @@ */ #include +#include #include #include #include #include -#include +#include "_usage.h" #include "einfo.h" +#include "misc.h" #include "queue.h" #include "rc.h" -#include "misc.h" -#include "_usage.h" enum format_t { FORMAT_DEFAULT, @@ -37,18 +37,12 @@ const char *applet = NULL; const char *extraopts = NULL; const char getoptstring[] = "acf:lmrsSu" getoptstring_COMMON; const struct option longopts[] = { - {"all", 0, NULL, 'a'}, - {"crashed", 0, NULL, 'c'}, - {"format", 1, NULL, 'f'}, - {"list", 0, NULL, 'l'}, - {"manual", 0, NULL, 'm'}, - {"runlevel", 0, NULL, 'r'}, - {"servicelist", 0, NULL, 's'}, - {"supervised", 0, NULL, 'S'}, - {"unused", 0, NULL, 'u'}, - longopts_COMMON -}; -const char * const longopts_help[] = { + {"all", 0, NULL, 'a'}, {"crashed", 0, NULL, 'c'}, + {"format", 1, NULL, 'f'}, {"list", 0, NULL, 'l'}, + {"manual", 0, NULL, 'm'}, {"runlevel", 0, NULL, 'r'}, + {"servicelist", 0, NULL, 's'}, {"supervised", 0, NULL, 'S'}, + {"unused", 0, NULL, 'u'}, longopts_COMMON}; +const char *const longopts_help[] = { "Show services from all run levels", "Show crashed services", "format status to be parsable (currently arg must be ini)", @@ -58,10 +52,10 @@ const char * const longopts_help[] = { "Show service list", "show supervised services", "Show services not assigned to any runlevel", - longopts_help_COMMON -}; -const char *usagestring = "" \ - "Usage: rc-status [options] -f ini ...\n" \ + longopts_help_COMMON}; +const char *usagestring = + "" + "Usage: rc-status [options] -f ini ...\n" " or: rc-status [options] [-a | -c | -l | -m | -r | -s | -u]"; static RC_DEPTREE *deptree; @@ -71,16 +65,16 @@ static RC_STRINGLIST *levels, *services, *tmp, *alist; static RC_STRINGLIST *sservices, *nservices, *needsme; static void print_level(const char *prefix, const char *level, - enum format_t format) + enum format_t format) { switch (format) { case FORMAT_DEFAULT: if (prefix) printf("%s ", prefix); - printf ("Runlevel: "); + printf("Runlevel: "); if (isatty(fileno(stdout))) - printf("%s%s%s\n", - ecolor(ECOLOR_HILITE), level, ecolor(ECOLOR_NORMAL)); + printf("%s%s%s\n", ecolor(ECOLOR_HILITE), level, + ecolor(ECOLOR_NORMAL)); else printf("%s\n", level); break; @@ -110,7 +104,7 @@ static char *get_uptime(const char *service) start_time_string = rc_service_value_get(service, "start_time"); if (start_count && start_time_string) { start_time = to_time_t(start_time_string); - diff_secs = (int64_t) difftime(time(NULL), start_time); + diff_secs = (int64_t)difftime(time(NULL), start_time); diff_days = diff_secs / 86400; diff_secs = diff_secs % 86400; diff_hours = diff_secs / 3600; @@ -119,13 +113,16 @@ static char *get_uptime(const char *service) diff_secs = diff_secs % 60; if (diff_days > 0) xasprintf(&uptime, - "%"PRId64" day(s) %02"PRId64":%02"PRId64":%02"PRId64" (%s)", - diff_days, diff_hours, diff_mins, diff_secs, - start_count); + "%" PRId64 " day(s) %02" PRId64 + ":%02" PRId64 ":%02" PRId64 " (%s)", + diff_days, diff_hours, diff_mins, + diff_secs, start_count); else xasprintf(&uptime, - "%02"PRId64":%02"PRId64":%02"PRId64" (%s)", - diff_hours, diff_mins, diff_secs, start_count); + "%02" PRId64 ":%02" PRId64 + ":%02" PRId64 " (%s)", + diff_hours, diff_mins, diff_secs, + start_count); } } return uptime; @@ -152,10 +149,10 @@ static void print_service(const char *service, enum format_t format) color = ECOLOR_WARN; } else if (state & RC_SERVICE_STARTED) { errno = 0; - if (rc_service_daemons_crashed(service) && errno != EACCES) - { + if (rc_service_daemons_crashed(service) && errno != EACCES) { child_pid = rc_service_value_get(service, "child_pid"); - start_time = rc_service_value_get(service, "start_time"); + start_time = + rc_service_value_get(service, "start_time"); if (start_time && child_pid) xasprintf(&status, " unsupervised "); else @@ -183,7 +180,7 @@ static void print_service(const char *service, enum format_t format) errno = 0; switch (format) { case FORMAT_DEFAULT: - cols = printf(" %s", service); + cols = printf(" %s", service); if (c && *c && isatty(fileno(stdout))) printf("\n"); ebracket(cols, color, status); @@ -196,7 +193,7 @@ static void print_service(const char *service, enum format_t format) } static void print_services(const char *runlevel, RC_STRINGLIST *svcs, - enum format_t format) + enum format_t format) { RC_STRINGLIST *l = NULL; RC_STRING *s; @@ -265,7 +262,7 @@ int main(int argc, char **argv) applet = basename_c(argv[0]); while ((opt = getopt_long(argc, argv, getoptstring, longopts, - (int *) 0)) != -1) + (int *)0)) != -1) switch (opt) { case 'a': show_all = true; @@ -286,7 +283,8 @@ int main(int argc, char **argv) format = FORMAT_INI; setenv("EINFO_QUIET", "YES", 1); } else - eerrorx("%s: invalid argument to --format switch\n", applet); + eerrorx("%s: invalid argument to --format switch\n", + applet); break; case 'l': levels = rc_runlevel_list(); @@ -298,8 +296,10 @@ int main(int argc, char **argv) levels = rc_runlevel_list(); TAILQ_FOREACH_SAFE(s, services, entries, t) { TAILQ_FOREACH(l, levels, entries) - if (rc_service_in_runlevel(s->value, l->value)) { - TAILQ_REMOVE(services, s, entries); + if (rc_service_in_runlevel(s->value, + l->value)) { + TAILQ_REMOVE(services, s, + entries); free(s->value); free(s); break; @@ -307,7 +307,8 @@ int main(int argc, char **argv) } TAILQ_FOREACH_SAFE(s, services, entries, t) if (rc_service_state(s->value) & - (RC_SERVICE_STOPPED | RC_SERVICE_HOTPLUGGED)) { + (RC_SERVICE_STOPPED | + RC_SERVICE_HOTPLUGGED)) { TAILQ_REMOVE(services, s, entries); free(s->value); free(s); @@ -322,7 +323,8 @@ int main(int argc, char **argv) case 'S': services = rc_services_in_state(RC_SERVICE_STARTED); TAILQ_FOREACH_SAFE(s, services, entries, t) - if (!rc_service_value_get(s->value, "child_pid")) + if (!rc_service_value_get(s->value, + "child_pid")) TAILQ_REMOVE(services, s, entries); print_services(NULL, services, FORMAT_DEFAULT); goto exit; @@ -337,8 +339,10 @@ int main(int argc, char **argv) levels = rc_runlevel_list(); TAILQ_FOREACH_SAFE(s, services, entries, t) { TAILQ_FOREACH(l, levels, entries) - if (rc_service_in_runlevel(s->value, l->value)) { - TAILQ_REMOVE(services, s, entries); + if (rc_service_in_runlevel(s->value, + l->value)) { + TAILQ_REMOVE(services, s, + entries); free(s->value); free(s); break; @@ -348,7 +352,7 @@ int main(int argc, char **argv) goto exit; /* NOTREACHED */ - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } if (!levels) @@ -411,7 +415,8 @@ int main(int argc, char **argv) TAILQ_FOREACH_SAFE(s, services, entries, t) { state = rc_service_state(s->value); if ((rc_stringlist_find(sservices, s->value) || - (state & ( RC_SERVICE_STOPPED | RC_SERVICE_HOTPLUGGED)))) { + (state & + (RC_SERVICE_STOPPED | RC_SERVICE_HOTPLUGGED)))) { if (!(state & RC_SERVICE_FAILED)) { TAILQ_REMOVE(services, s, entries); free(s->value); @@ -430,10 +435,13 @@ int main(int argc, char **argv) TAILQ_FOREACH_SAFE(s, services, entries, t) { l->value = s->value; setenv("RC_SVCNAME", l->value, 1); - tmp = rc_deptree_depends(deptree, needsme, alist, level->value, RC_DEP_TRACE); + tmp = rc_deptree_depends(deptree, needsme, + alist, level->value, + RC_DEP_TRACE); if (TAILQ_FIRST(tmp)) { TAILQ_REMOVE(services, s, entries); - TAILQ_INSERT_TAIL(nservices, s, entries); + TAILQ_INSERT_TAIL(nservices, s, + entries); } rc_stringlist_free(tmp); } diff --git a/src/rc-update/rc-update.c b/src/rc-update/rc-update.c index a6dea3543..847d3761d 100644 --- a/src/rc-update/rc-update.c +++ b/src/rc-update/rc-update.c @@ -24,87 +24,80 @@ #include #include +#include "_usage.h" #include "einfo.h" +#include "misc.h" #include "queue.h" #include "rc.h" -#include "misc.h" -#include "_usage.h" const char *applet = NULL; const char *extraopts = NULL; -const char *usagestring = "" \ - "Usage: rc-update [options] add [...]\n" \ - " or: rc-update [options] del [...]\n" \ +const char *usagestring = + "" + "Usage: rc-update [options] add [...]\n" + " or: rc-update [options] del [...]\n" " or: rc-update [options] [show [...]]"; const char getoptstring[] = "asu" getoptstring_COMMON; -const struct option longopts[] = { - { "all", 0, NULL, 'a' }, - { "stack", 0, NULL, 's' }, - { "update", 0, NULL, 'u' }, - longopts_COMMON -}; -const char * const longopts_help[] = { - "Process all runlevels", - "Stack a runlevel instead of a service", - "Force an update of the dependency tree", - longopts_help_COMMON -}; +const struct option longopts[] = {{"all", 0, NULL, 'a'}, + {"stack", 0, NULL, 's'}, + {"update", 0, NULL, 'u'}, + longopts_COMMON}; +const char *const longopts_help[] = {"Process all runlevels", + "Stack a runlevel instead of a service", + "Force an update of the dependency tree", + longopts_help_COMMON}; /* Return the number of changes made: * -1 = no changes (error) * 0 = no changes (nothing to do) * 1+ = number of runlevels updated */ -static int -add(const char *runlevel, const char *service) +static int add(const char *runlevel, const char *service) { int retval = -1; if (!rc_service_exists(service)) { if (errno == ENOEXEC) - eerror("%s: service `%s' is not executable", - applet, service); + eerror("%s: service `%s' is not executable", 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)) { einfo("%s: %s already installed in runlevel `%s'; skipping", - applet, service, runlevel); + applet, service, runlevel); retval = 0; } else if (rc_service_add(runlevel, service)) { einfo("service %s added to runlevel %s", service, runlevel); retval = 1; } else eerror("%s: failed to add service `%s' to runlevel `%s': %s", - applet, service, runlevel, strerror (errno)); + applet, service, runlevel, strerror(errno)); return retval; } -static int -delete(const char *runlevel, const char *service) +static int delete (const char *runlevel, const char *service) { int retval = -1; errno = 0; if (rc_service_delete(runlevel, service)) { - einfo("service %s removed from runlevel %s", - service, runlevel); + einfo("service %s removed from runlevel %s", service, runlevel); return 1; } if (errno == ENOENT) - eerror("%s: service `%s' is not in the runlevel `%s'", - applet, service, runlevel); + eerror("%s: service `%s' is not in the runlevel `%s'", applet, + service, runlevel); else eerror("%s: failed to remove service `%s' from runlevel `%s': %s", - applet, service, runlevel, strerror (errno)); + applet, service, runlevel, strerror(errno)); return retval; } -static int -addstack(const char *runlevel, const char *stack) +static int addstack(const char *runlevel, const char *stack) { if (!rc_runlevel_exists(runlevel)) { eerror("%s: runlevel `%s' does not exist", applet, runlevel); @@ -125,23 +118,21 @@ addstack(const char *runlevel, const char *stack) strcmp(runlevel, RC_LEVEL_SINGLE) == 0 || strcmp(stack, RC_LEVEL_SINGLE) == 0 || strcmp(runlevel, RC_LEVEL_SHUTDOWN) == 0 || - strcmp(stack, RC_LEVEL_SHUTDOWN) == 0) - { - eerror("%s: cannot stack the %s runlevel", - applet, RC_LEVEL_SYSINIT); + strcmp(stack, RC_LEVEL_SHUTDOWN) == 0) { + eerror("%s: cannot stack the %s runlevel", applet, + RC_LEVEL_SYSINIT); return -1; } if (!rc_runlevel_stack(runlevel, stack)) { - eerror("%s: failed to stack `%s' to `%s': %s", - applet, stack, runlevel, strerror(errno)); + eerror("%s: failed to stack `%s' to `%s': %s", applet, stack, + runlevel, strerror(errno)); return -1; } einfo("runlevel %s added to runlevel %s", stack, runlevel); return 1; } -static int -delstack(const char *runlevel, const char *stack) +static int delstack(const char *runlevel, const char *stack) { if (rc_runlevel_unstack(runlevel, stack)) { einfo("runlevel %s removed from runlevel %s", stack, runlevel); @@ -149,17 +140,16 @@ delstack(const char *runlevel, const char *stack) } if (errno == ENOENT) - eerror("%s: runlevel `%s' is not in the runlevel `%s'", - applet, stack, runlevel); + eerror("%s: runlevel `%s' is not in the runlevel `%s'", applet, + stack, runlevel); else eerror("%s: failed to remove runlevel `%s' from runlevel `%s': %s", - applet, stack, runlevel, strerror (errno)); + applet, stack, runlevel, strerror(errno)); return -1; } -static void -show(RC_STRINGLIST *runlevels, bool verbose) +static void show(RC_STRINGLIST *runlevels, bool verbose) { RC_STRINGLIST *services = rc_services_in_runlevel(NULL); RC_STRING *service; @@ -176,13 +166,12 @@ show(RC_STRINGLIST *runlevels, bool verbose) TAILQ_FOREACH(runlevel, runlevels, entries) { if (rc_service_in_runlevel(service->value, - runlevel->value)) - { + runlevel->value)) { rc_stringlist_add(in, runlevel->value); inone = true; } else { l = strlen(runlevel->value); - buffer = xmalloc(l+1); + buffer = xmalloc(l + 1); memset(buffer, ' ', l); buffer[l] = 0; rc_stringlist_add(in, buffer); @@ -193,7 +182,7 @@ show(RC_STRINGLIST *runlevels, bool verbose) if (inone || verbose) { printf(" %20s |", service->value); TAILQ_FOREACH(runlevel, in, entries) - printf (" %s", runlevel->value); + printf(" %s", runlevel->value); printf("\n"); } rc_stringlist_free(in); @@ -202,9 +191,9 @@ show(RC_STRINGLIST *runlevels, bool verbose) rc_stringlist_free(services); } -#define DOADD (1 << 1) +#define DOADD (1 << 1) #define DODELETE (1 << 2) -#define DOSHOW (1 << 3) +#define DOSHOW (1 << 3) int main(int argc, char **argv) { @@ -222,27 +211,27 @@ int main(int argc, char **argv) int ret; applet = basename_c(argv[0]); - while ((opt = getopt_long(argc, argv, getoptstring, - longopts, (int *)0)) != -1) + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) switch (opt) { case 'a': - all_runlevels = true; - break; + all_runlevels = true; + break; case 's': - stack = true; - break; + stack = true; + break; case 'u': deptree = _rc_deptree_load(-1, &ret); if (deptree) rc_deptree_free(deptree); return ret; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } - verbose = rc_yesno(getenv ("EINFO_VERBOSE")); + verbose = rc_yesno(getenv("EINFO_VERBOSE")); - if ((action & DOSHOW && action != DOSHOW) || - (action & DOADD && action != DOADD) || + if ((action & DOSHOW && action != DOSHOW) || + (action & DOADD && action != DOADD) || (action & DODELETE && action != DODELETE)) eerrorx("%s: cannot mix commands", applet); @@ -251,15 +240,15 @@ int main(int argc, char **argv) if (strcmp(argv[optind], "add") == 0) action = DOADD; else if (strcmp(argv[optind], "delete") == 0 || - strcmp(argv[optind], "del") == 0) + strcmp(argv[optind], "del") == 0) action = DODELETE; else if (strcmp(argv[optind], "show") == 0) action = DOSHOW; if (action) optind++; else - eerrorx("%s: invalid command `%s'", - applet, argv[optind]); + eerrorx("%s: invalid command `%s'", applet, + argv[optind]); } if (!action) action = DOSHOW; @@ -278,8 +267,8 @@ int main(int argc, char **argv) rc_stringlist_add(runlevels, argv[optind++]); else { rc_stringlist_free(runlevels); - eerrorx ("%s: `%s' is not a valid runlevel", - applet, argv[optind]); + eerrorx("%s: `%s' is not a valid runlevel", + applet, argv[optind]); } } @@ -293,15 +282,16 @@ int main(int argc, char **argv) } rc_stringlist_sort(&runlevels); - show (runlevels, verbose); + show(runlevels, verbose); } else { if (!service) - eerror ("%s: no service specified", applet); + eerror("%s: no service specified", applet); else { if (action & DOADD) { if (all_runlevels) { rc_stringlist_free(runlevels); - eerrorx("%s: the -a option is invalid with add", applet); + eerrorx("%s: the -a option is invalid with add", + applet); } actfunc = stack ? addstack : add; } else if (action & DODELETE) { @@ -329,8 +319,8 @@ int main(int argc, char **argv) TAILQ_FOREACH(runlevel, runlevels, entries) { if (!rc_runlevel_exists(runlevel->value)) { - eerror ("%s: runlevel `%s' does not exist", - applet, runlevel->value); + eerror("%s: runlevel `%s' does not exist", + applet, runlevel->value); continue; } @@ -340,11 +330,11 @@ int main(int argc, char **argv) num_updated += ret; } - if (retval == EXIT_SUCCESS && - num_updated == 0 && action & DODELETE) + if (retval == EXIT_SUCCESS && num_updated == 0 && + action & DODELETE) ewarnx("%s: service `%s' not found in any" - " of the specified runlevels", - applet, service); + " of the specified runlevels", + applet, service); } } diff --git a/src/seedrng/seedrng.c b/src/seedrng/seedrng.c index daca8952d..49980d1f1 100644 --- a/src/seedrng/seedrng.c +++ b/src/seedrng/seedrng.c @@ -16,32 +16,33 @@ * except according to the terms contained in the LICENSE file. */ -#include -#include -#include -#include -#include -#include +#include +#include #include #include +#include #include -#include -#include -#include -#include #include #include -#include #include #include +#include +#include +#include +#include +#include +#include +#include +#include -#include "rc.h" +#include "_usage.h" #include "einfo.h" #include "helpers.h" -#include "_usage.h" +#include "rc.h" #ifndef GRND_INSECURE -#define GRND_INSECURE 0x0004 /* Apparently some headers don't ship with this yet. */ +#define GRND_INSECURE \ + 0x0004 /* Apparently some headers don't ship with this yet. */ #endif #define DEFAULT_SEED_DIR "/var/lib/seedrng" @@ -62,16 +63,12 @@ enum long_opts { const char *applet = NULL; const char *extraopts = NULL; const char getoptstring[] = getoptstring_COMMON; -const struct option longopts[] = { - { "seed-dir", 1, NULL, LONGOPT_SEED_DIR }, - { "skip-credit", 0, NULL, LONGOPT_SKIP_CREDIT }, - longopts_COMMON -}; -const char * const longopts_help[] = { +const struct option longopts[] = {{"seed-dir", 1, NULL, LONGOPT_SEED_DIR}, + {"skip-credit", 0, NULL, LONGOPT_SKIP_CREDIT}, + longopts_COMMON}; +const char *const longopts_help[] = { "Directory for seed files (default: " DEFAULT_SEED_DIR ")", - "Skip crediting entropy of seeds", - longopts_help_COMMON -}; + "Skip crediting entropy of seeds", longopts_help_COMMON}; const char *usagestring = NULL; enum blake2s_lengths { @@ -80,10 +77,7 @@ enum blake2s_lengths { BLAKE2S_KEY_LEN = 32 }; -enum seedrng_lengths { - MAX_SEED_LEN = 512, - MIN_SEED_LEN = BLAKE2S_HASH_LEN -}; +enum seedrng_lengths { MAX_SEED_LEN = 512, MIN_SEED_LEN = BLAKE2S_HASH_LEN }; struct blake2s_state { uint32_t h[8]; @@ -100,7 +94,7 @@ struct blake2s_state { #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #endif #ifndef DIV_ROUND_UP -#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d)) #endif static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words) @@ -124,22 +118,21 @@ static inline uint32_t ror32(uint32_t word, unsigned int shift) return (word >> (shift & 31)) | (word << ((-shift) & 31)); } -static const uint32_t blake2s_iv[8] = { - 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, - 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL -}; +static const uint32_t blake2s_iv[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, + 0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL, + 0x1F83D9ABUL, 0x5BE0CD19UL}; static const uint8_t blake2s_sigma[10][16] = { - { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, - { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, - { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, - { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, - { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, - { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, - { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, - { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, - { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, - { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 }, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}, + {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4}, + {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8}, + {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13}, + {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9}, + {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11}, + {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10}, + {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5}, + {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0}, }; static void blake2s_set_lastblock(struct blake2s_state *state) @@ -147,13 +140,15 @@ static void blake2s_set_lastblock(struct blake2s_state *state) state->f[0] = -1; } -static void blake2s_increment_counter(struct blake2s_state *state, const uint32_t inc) +static void blake2s_increment_counter(struct blake2s_state *state, + const uint32_t inc) { state->t[0] += inc; state->t[1] += (state->t[0] < inc); } -static void blake2s_init_param(struct blake2s_state *state, const uint32_t param) +static void blake2s_init_param(struct blake2s_state *state, + const uint32_t param) { int i; @@ -169,7 +164,8 @@ static void blake2s_init(struct blake2s_state *state, const size_t outlen) state->outlen = outlen; } -static void blake2s_compress(struct blake2s_state *state, const uint8_t *block, size_t nblocks, const uint32_t inc) +static void blake2s_compress(struct blake2s_state *state, const uint8_t *block, + size_t nblocks, const uint32_t inc) { uint32_t m[16]; uint32_t v[16]; @@ -180,8 +176,8 @@ static void blake2s_compress(struct blake2s_state *state, const uint8_t *block, memcpy(m, block, BLAKE2S_BLOCK_LEN); le32_to_cpu_array(m, ARRAY_SIZE(m)); memcpy(v, state->h, 32); - v[ 8] = blake2s_iv[0]; - v[ 9] = blake2s_iv[1]; + v[8] = blake2s_iv[0]; + v[9] = blake2s_iv[1]; v[10] = blake2s_iv[2]; v[11] = blake2s_iv[3]; v[12] = blake2s_iv[4] ^ state->t[0]; @@ -189,27 +185,29 @@ static void blake2s_compress(struct blake2s_state *state, const uint8_t *block, v[14] = blake2s_iv[6] ^ state->f[0]; v[15] = blake2s_iv[7] ^ state->f[1]; -#define G(r, i, a, b, c, d) do { \ - a += b + m[blake2s_sigma[r][2 * i + 0]]; \ - d = ror32(d ^ a, 16); \ - c += d; \ - b = ror32(b ^ c, 12); \ - a += b + m[blake2s_sigma[r][2 * i + 1]]; \ - d = ror32(d ^ a, 8); \ - c += d; \ - b = ror32(b ^ c, 7); \ -} while (0) - -#define ROUND(r) do { \ - G(r, 0, v[0], v[ 4], v[ 8], v[12]); \ - G(r, 1, v[1], v[ 5], v[ 9], v[13]); \ - G(r, 2, v[2], v[ 6], v[10], v[14]); \ - G(r, 3, v[3], v[ 7], v[11], v[15]); \ - G(r, 4, v[0], v[ 5], v[10], v[15]); \ - G(r, 5, v[1], v[ 6], v[11], v[12]); \ - G(r, 6, v[2], v[ 7], v[ 8], v[13]); \ - G(r, 7, v[3], v[ 4], v[ 9], v[14]); \ -} while (0) +#define G(r, i, a, b, c, d) \ + do { \ + a += b + m[blake2s_sigma[r][2 * i + 0]]; \ + d = ror32(d ^ a, 16); \ + c += d; \ + b = ror32(b ^ c, 12); \ + a += b + m[blake2s_sigma[r][2 * i + 1]]; \ + d = ror32(d ^ a, 8); \ + c += d; \ + b = ror32(b ^ c, 7); \ + } while (0) + +#define ROUND(r) \ + do { \ + G(r, 0, v[0], v[4], v[8], v[12]); \ + G(r, 1, v[1], v[5], v[9], v[13]); \ + G(r, 2, v[2], v[6], v[10], v[14]); \ + G(r, 3, v[3], v[7], v[11], v[15]); \ + G(r, 4, v[0], v[5], v[10], v[15]); \ + G(r, 5, v[1], v[6], v[11], v[12]); \ + G(r, 6, v[2], v[7], v[8], v[13]); \ + G(r, 7, v[3], v[4], v[9], v[14]); \ + } while (0) ROUND(0); ROUND(1); ROUND(2); @@ -232,7 +230,8 @@ static void blake2s_compress(struct blake2s_state *state, const uint8_t *block, } } -static void blake2s_update(struct blake2s_state *state, const void *inp, size_t inlen) +static void blake2s_update(struct blake2s_state *state, const void *inp, + size_t inlen) { const size_t fill = BLAKE2S_BLOCK_LEN - state->buflen; const uint8_t *in = inp; @@ -259,7 +258,8 @@ static void blake2s_update(struct blake2s_state *state, const void *inp, size_t static void blake2s_final(struct blake2s_state *state, uint8_t *out) { blake2s_set_lastblock(state); - memset(state->buf + state->buflen, 0, BLAKE2S_BLOCK_LEN - state->buflen); + memset(state->buf + state->buflen, 0, + BLAKE2S_BLOCK_LEN - state->buflen); blake2s_compress(state, state->buf, 1, state->buflen); cpu_to_le32_array(state->h, ARRAY_SIZE(state->h)); memcpy(out, state->h, state->outlen); @@ -324,10 +324,11 @@ static ssize_t write_full(int fd, const void *buf, size_t count) static size_t determine_optimal_seed_len(void) { size_t ret = 0; - char poolsize_str[11] = { 0 }; + char poolsize_str[11] = {0}; int fd = open("/proc/sys/kernel/random/poolsize", O_RDONLY); - if (fd < 0 || read_full(fd, poolsize_str, sizeof(poolsize_str) - 1) < 0) { + if (fd < 0 || + read_full(fd, poolsize_str, sizeof(poolsize_str) - 1) < 0) { ewarn("%s: Unable to determine pool size, falling back to %u bits: %s", applet, MIN_SEED_LEN * 8, strerror(errno)); ret = MIN_SEED_LEN; @@ -353,10 +354,8 @@ static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable) *is_creditable = true; return 0; } else if (ret < 0 && errno == ENOSYS) { - struct pollfd random_fd = { - .fd = open("/dev/random", O_RDONLY), - .events = POLLIN - }; + struct pollfd random_fd = {.fd = open("/dev/random", O_RDONLY), + .events = POLLIN}; if (random_fd.fd < 0) return -errno; *is_creditable = poll(&random_fd, 1, 0) == 1; @@ -382,10 +381,7 @@ static int seed_rng(uint8_t *seed, size_t len, bool credit) int entropy_count; int buf_size; uint8_t buffer[MAX_SEED_LEN]; - } req = { - .entropy_count = credit ? len * 8 : 0, - .buf_size = len - }; + } req = {.entropy_count = credit ? len * 8 : 0, .buf_size = len}; int random_fd, ret; if (len > sizeof(req.buffer)) { @@ -405,7 +401,8 @@ static int seed_rng(uint8_t *seed, size_t len, bool credit) return ret ? -1 : 0; } -static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, struct blake2s_state *hash) +static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, + struct blake2s_state *hash) { uint8_t seed[MAX_SEED_LEN]; ssize_t seed_len; @@ -416,13 +413,15 @@ static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, return 0; else if (fd < 0) { ret = -errno; - eerror("%s: Unable to open seed file: %s", applet, strerror(errno)); + eerror("%s: Unable to open seed file: %s", applet, + strerror(errno)); goto out; } seed_len = read_full(fd, seed, sizeof(seed)); if (seed_len < 0) { ret = -errno; - eerror("%s: Unable to read seed file: %s", applet, strerror(errno)); + eerror("%s: Unable to read seed file: %s", applet, + strerror(errno)); goto out; } if ((unlinkat(dfd, filename, 0) < 0 || fsync(dfd) < 0) && seed_len) { @@ -437,7 +436,8 @@ static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, blake2s_update(hash, &seed_len, sizeof(seed_len)); blake2s_update(hash, seed, seed_len); - einfo("Seeding %zd bits %s crediting", seed_len * 8, credit ? "and" : "without"); + einfo("Seeding %zd bits %s crediting", seed_len * 8, + credit ? "and" : "without"); if (seed_rng(seed, seed_len, credit) < 0) { ret = -errno; eerror("%s: Unable to seed: %s", applet, strerror(errno)); @@ -459,14 +459,14 @@ int main(int argc, char **argv) uint8_t new_seed[MAX_SEED_LEN]; size_t new_seed_len; bool new_seed_creditable; - struct timespec realtime = { 0 }, boottime = { 0 }; + struct timespec realtime = {0}, boottime = {0}; struct blake2s_state hash; bool skip_credit = false; applet = basename_c(argv[0]); - while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1) - { + while ((opt = getopt_long(argc, argv, getoptstring, longopts, + (int *)0)) != -1) { switch (opt) { case LONGOPT_SEED_DIR: if (!seed_dir) @@ -475,7 +475,7 @@ int main(int argc, char **argv) case LONGOPT_SKIP_CREDIT: skip_credit = true; break; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } } if (!seed_dir) @@ -492,20 +492,25 @@ int main(int argc, char **argv) blake2s_update(&hash, &boottime, sizeof(boottime)); if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST) - eerrorx("%s: Unable to create seed directory: %s", applet, strerror(errno)); + eerrorx("%s: Unable to create seed directory: %s", applet, + strerror(errno)); dfd = open(seed_dir, O_DIRECTORY | O_RDONLY); if (dfd < 0 || flock(dfd, LOCK_EX) < 0) - eerrorx("%s: Unable to lock seed directory: %s", applet, strerror(errno)); + eerrorx("%s: Unable to lock seed directory: %s", applet, + strerror(errno)); - if (seed_from_file_if_exists(NON_CREDITABLE_SEED, dfd, false, &hash) < 0) + if (seed_from_file_if_exists(NON_CREDITABLE_SEED, dfd, false, &hash) < + 0) program_ret |= 1 << 1; - if (seed_from_file_if_exists(CREDITABLE_SEED, dfd, !skip_credit, &hash) < 0) + if (seed_from_file_if_exists(CREDITABLE_SEED, dfd, !skip_credit, + &hash) < 0) program_ret |= 1 << 2; new_seed_len = determine_optimal_seed_len(); if (read_new_seed(new_seed, new_seed_len, &new_seed_creditable) < 0) { - eerror("%s: Unable to read new seed: %s", applet, strerror(errno)); + eerror("%s: Unable to read new seed: %s", applet, + strerror(errno)); new_seed_len = BLAKE2S_HASH_LEN; strncpy((char *)new_seed, seedrng_failure, new_seed_len); program_ret |= 1 << 3; @@ -514,18 +519,25 @@ int main(int argc, char **argv) blake2s_update(&hash, new_seed, new_seed_len); blake2s_final(&hash, new_seed + new_seed_len - BLAKE2S_HASH_LEN); - einfo("Saving %zu bits of %s seed for next boot", new_seed_len * 8, new_seed_creditable ? "creditable" : "non-creditable"); - fd = openat(dfd, NON_CREDITABLE_SEED, O_WRONLY | O_CREAT | O_TRUNC, 0400); + einfo("Saving %zu bits of %s seed for next boot", new_seed_len * 8, + new_seed_creditable ? "creditable" : "non-creditable"); + fd = openat(dfd, NON_CREDITABLE_SEED, O_WRONLY | O_CREAT | O_TRUNC, + 0400); if (fd < 0) { - eerror("%s: Unable to open seed file for writing: %s", applet, strerror(errno)); + eerror("%s: Unable to open seed file for writing: %s", applet, + strerror(errno)); return program_ret | (1 << 4); } - if (write_full(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) { - eerror("%s: Unable to write seed file: %s", applet, strerror(errno)); + if (write_full(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || + fsync(fd) < 0) { + eerror("%s: Unable to write seed file: %s", applet, + strerror(errno)); return program_ret | (1 << 5); } - if (new_seed_creditable && renameat(dfd, NON_CREDITABLE_SEED, dfd, CREDITABLE_SEED) < 0) { - ewarn("%s: Unable to make new seed creditable: %s", applet, strerror(errno)); + if (new_seed_creditable && + renameat(dfd, NON_CREDITABLE_SEED, dfd, CREDITABLE_SEED) < 0) { + ewarn("%s: Unable to make new seed creditable: %s", applet, + strerror(errno)); return program_ret | (1 << 6); } return program_ret; diff --git a/src/service/service.c b/src/service/service.c index 001d3ad3d..8c3db4b49 100644 --- a/src/service/service.c +++ b/src/service/service.c @@ -10,24 +10,24 @@ * except according to the terms contained in the LICENSE file. */ -#include #include +#include -#include #include +#include #include +#include #include #include #include -#include #include #include #include #include #include "einfo.h" -#include "rc.h" #include "misc.h" +#include "rc.h" const char *applet = NULL; @@ -68,7 +68,7 @@ int main(int argc, char **argv) ok = rc_service_started_daemon(service, exec, NULL, idx); } else if (strcmp(applet, "service_crashed") == 0) { - ok = ( rc_service_daemons_crashed(service) && errno != EACCES); + ok = (rc_service_daemons_crashed(service) && errno != EACCES); } else eerrorx("%s: unknown applet", applet); diff --git a/src/shared/_usage.c b/src/shared/_usage.c index bba03b926..be4cc6134 100644 --- a/src/shared/_usage.c +++ b/src/shared/_usage.c @@ -10,34 +10,34 @@ * except according to the terms contained in the LICENSE file. */ +#include "_usage.h" +#include "misc.h" +#include "rc.h" +#include "version.h" #include #include #include -#include "rc.h" -#include "misc.h" -#include "_usage.h" -#include "version.h" #if lint -# define _noreturn +#define _noreturn #endif #if __GNUC__ > 2 || defined(__INTEL_COMPILER) -# define _noreturn __attribute__ ((__noreturn__)) +#define _noreturn __attribute__((__noreturn__)) #else -# define _noreturn +#define _noreturn #endif void set_quiet_options(void) { static int qcount = 0; - qcount ++; + qcount++; switch (qcount) { case 1: - setenv ("EINFO_QUIET", "YES", 1); + setenv("EINFO_QUIET", "YES", 1); break; case 2: - setenv ("EERROR_QUIET", "YES", 1); + setenv("EERROR_QUIET", "YES", 1); break; } } @@ -59,7 +59,7 @@ _noreturn void show_version(void) _noreturn void usage(int exit_status) { - const char * const has_arg[] = { "", "", "[arg]" }; + const char *const has_arg[] = {"", "", "[arg]"}; int i; int len; char *lo; @@ -78,8 +78,9 @@ _noreturn void usage(int exit_status) printf("\n\nOptions: [ %s ]\n", getoptstring); for (i = 0; longopts[i].name; ++i) { val[1] = longopts[i].val; - len = printf(" %3s --%s %s", isprint(longopts[i].val) ? val : "", - longopts[i].name, has_arg[longopts[i].has_arg]); + len = printf(" %3s --%s %s", + isprint(longopts[i].val) ? val : "", + longopts[i].name, has_arg[longopts[i].has_arg]); lo = p = xstrdup(longopts_help[i]); while ((token = strsep(&p, "\n"))) { diff --git a/src/shared/_usage.h b/src/shared/_usage.h index 62c131d41..4c3f3b593 100644 --- a/src/shared/_usage.h +++ b/src/shared/_usage.h @@ -14,41 +14,53 @@ #define getoptstring_COMMON "ChqVv" -#define longopts_COMMON \ - { "help", 0, NULL, 'h'}, \ - { "nocolor", 0, NULL, 'C'}, \ - { "version", 0, NULL, 'V'}, \ - { "verbose", 0, NULL, 'v'}, \ - { "quiet", 0, NULL, 'q'}, \ - { NULL, 0, NULL, 0 } - -#define longopts_help_COMMON \ - "Display this help output", \ - "Disable color output", \ - "Display software version", \ - "Run verbosely", \ - "Run quietly (repeat to suppress errors)" - -#define case_RC_COMMON_getopt_case_C setenv ("EINFO_COLOR", "NO", 1); -#define case_RC_COMMON_getopt_case_h usage (EXIT_SUCCESS); -#define case_RC_COMMON_getopt_case_V if (argc == 2) show_version(); -#define case_RC_COMMON_getopt_case_v setenv ("EINFO_VERBOSE", "YES", 1); -#define case_RC_COMMON_getopt_case_q set_quiet_options(); -#define case_RC_COMMON_getopt_default usage (EXIT_FAILURE); - -#define case_RC_COMMON_GETOPT \ - case 'C': case_RC_COMMON_getopt_case_C; break; \ - case 'h': case_RC_COMMON_getopt_case_h; break; \ - case 'V': case_RC_COMMON_getopt_case_V; break; \ - case 'v': case_RC_COMMON_getopt_case_v; break; \ - case 'q': case_RC_COMMON_getopt_case_q; break; \ - default: case_RC_COMMON_getopt_default; break; +#define longopts_COMMON \ + {"help", 0, NULL, 'h'}, {"nocolor", 0, NULL, 'C'}, \ + {"version", 0, NULL, 'V'}, {"verbose", 0, NULL, 'v'}, \ + {"quiet", 0, NULL, 'q'}, \ + { \ + NULL, 0, NULL, 0 \ + } + +#define longopts_help_COMMON \ + "Display this help output", "Disable color output", \ + "Display software version", "Run verbosely", \ + "Run quietly (repeat to suppress errors)" + +#define case_RC_COMMON_getopt_case_C setenv("EINFO_COLOR", "NO", 1); +#define case_RC_COMMON_getopt_case_h usage(EXIT_SUCCESS); +#define case_RC_COMMON_getopt_case_V \ + if (argc == 2) \ + show_version(); +#define case_RC_COMMON_getopt_case_v setenv("EINFO_VERBOSE", "YES", 1); +#define case_RC_COMMON_getopt_case_q set_quiet_options(); +#define case_RC_COMMON_getopt_default usage(EXIT_FAILURE); + +#define case_RC_COMMON_GETOPT \ + case 'C': \ + case_RC_COMMON_getopt_case_C; \ + break; \ + case 'h': \ + case_RC_COMMON_getopt_case_h; \ + break; \ + case 'V': \ + case_RC_COMMON_getopt_case_V; \ + break; \ + case 'v': \ + case_RC_COMMON_getopt_case_v; \ + break; \ + case 'q': \ + case_RC_COMMON_getopt_case_q; \ + break; \ + default: \ + case_RC_COMMON_getopt_default; \ + break; extern const char *applet; extern const char *extraopts; extern const char getoptstring[]; extern const struct option longopts[]; -extern const char * const longopts_help[]; +extern const char *const longopts_help[]; extern const char *usagestring; void set_quiet_options(void); diff --git a/src/shared/helpers.h b/src/shared/helpers.h index 44d76552d..151efea15 100644 --- a/src/shared/helpers.h +++ b/src/shared/helpers.h @@ -18,48 +18,53 @@ #ifndef __HELPERS_H__ #define __HELPERS_H__ -#define ERRX fprintf (stderr, "out of memory\n"); exit (1) +#define ERRX \ + fprintf(stderr, "out of memory\n"); \ + exit(1) -#define UNCONST(a) ((void *)(unsigned long)(const void *)(a)) +#define UNCONST(a) ((void *)(unsigned long)(const void *)(a)) #ifdef lint -# define _unused +#define _unused #endif #if __GNUC__ > 2 || defined(__INTEL_COMPILER) -# define _dead __attribute__((__noreturn__)) -# define _unused __attribute__((__unused__)) -# define _xasprintf(a, b) __attribute__((__format__(__printf__, a, b))) +#define _dead __attribute__((__noreturn__)) +#define _unused __attribute__((__unused__)) +#define _xasprintf(a, b) __attribute__((__format__(__printf__, a, b))) #else -# define _dead -# define _unused -# define _xasprintf(a, b) +#define _dead +#define _unused +#define _xasprintf(a, b) #endif #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #ifdef __GLIBC__ -# if !defined (__UCLIBC__) && !defined (__dietlibc__) -# define strlcpy(dst, src, size) snprintf(dst, size, "%s", src) -# endif +#if !defined(__UCLIBC__) && !defined(__dietlibc__) +#define strlcpy(dst, src, size) snprintf(dst, size, "%s", src) +#endif #endif #ifndef timespecsub -#define timespecsub(tsp, usp, vsp) \ - do { \ - (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ - (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ - if ((vsp)->tv_nsec < 0) { \ - (vsp)->tv_sec--; \ - (vsp)->tv_nsec += 1000000000L; \ - } \ +#define timespecsub(tsp, usp, vsp) \ + do { \ + (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ + (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ + if ((vsp)->tv_nsec < 0) { \ + (vsp)->tv_sec--; \ + (vsp)->tv_nsec += 1000000000L; \ + } \ } while (/* CONSTCOND */ 0) #endif #include #include +#include +#include +#include #include -_unused static void *xmalloc (size_t size) +_unused static void *xmalloc(size_t size) { void *value = malloc(size); @@ -134,7 +139,7 @@ _unused static bool existss(const char *pathname) * functions to handle memory allocation. * this function was originally written by Mike Frysinger. */ -_unused _xasprintf(2,3) static int xasprintf(char **strp, const char *fmt, ...) +_unused _xasprintf(2, 3) static int xasprintf(char **strp, const char *fmt, ...) { va_list ap; int len; diff --git a/src/shared/misc.c b/src/shared/misc.c index a83599e5c..9d0f67562 100644 --- a/src/shared/misc.c +++ b/src/shared/misc.c @@ -20,10 +20,9 @@ #include #ifdef __linux__ -# include +#include #endif -#include #include #include #include @@ -31,35 +30,39 @@ #include #include #include +#include #include #include #include #include "einfo.h" +#include "misc.h" #include "queue.h" #include "rc.h" -#include "misc.h" #include "version.h" extern char **environ; -bool -rc_conf_yesno(const char *setting) +bool rc_conf_yesno(const char *setting) { - return rc_yesno(rc_conf_value (setting)); + return rc_yesno(rc_conf_value(setting)); } -static const char *const env_whitelist[] = { - "EERROR_QUIET", "EINFO_QUIET", - "IN_BACKGROUND", "IN_DRYRUN", "IN_HOTPLUG", - "RC_DEBUG", "RC_NODEPS", - "LANG", "LC_MESSAGES", "TERM", - "EINFO_COLOR", "EINFO_VERBOSE", - NULL -}; - -void -env_filter(void) +static const char *const env_whitelist[] = {"EERROR_QUIET", + "EINFO_QUIET", + "IN_BACKGROUND", + "IN_DRYRUN", + "IN_HOTPLUG", + "RC_DEBUG", + "RC_NODEPS", + "LANG", + "LC_MESSAGES", + "TERM", + "EINFO_COLOR", + "EINFO_VERBOSE", + NULL}; + +void env_filter(void) { RC_STRINGLIST *env_allow; RC_STRINGLIST *profile; @@ -119,8 +122,7 @@ env_filter(void) rc_stringlist_free(profile); } -void -env_config(void) +void env_config(void) { size_t pplen = strlen(RC_PATH_PREFIX); char *path; @@ -142,7 +144,7 @@ env_config(void) path = getenv("PATH"); if (!path) setenv("PATH", RC_PATH_PREFIX, 1); - else if (strncmp (RC_PATH_PREFIX, path, pplen) != 0) { + else if (strncmp(RC_PATH_PREFIX, path, pplen) != 0) { l = strlen(path) + pplen + 3; e = p = xmalloc(sizeof(char) * l); p += snprintf(p, l, "%s", RC_PATH_PREFIX); @@ -156,7 +158,7 @@ env_config(void) break; if (!tok) p += snprintf(p, l - (p - e), ":%s", token); - free (np); + free(np); } *p++ = '\0'; unsetenv("PATH"); @@ -175,7 +177,7 @@ env_config(void) if ((fp = fopen(RC_KRUNLEVEL, "r"))) { if (getline(&buffer, &size, fp) != -1) { - l = strlen (buffer) - 1; + l = strlen(buffer) - 1; if (buffer[l] == '\n') buffer[l] = 0; setenv("RC_DEFAULTLEVEL", buffer, 1); @@ -210,31 +212,28 @@ env_config(void) setenv("EINFO_COLOR", "NO", 1); } -int -signal_setup(int sig, void (*handler)(int)) +int signal_setup(int sig, void (*handler)(int)) { struct sigaction sa; - memset(&sa, 0, sizeof (sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = handler; return sigaction(sig, &sa, NULL); } -int -signal_setup_restart(int sig, void (*handler)(int)) +int signal_setup_restart(int sig, void (*handler)(int)) { struct sigaction sa; - memset(&sa, 0, sizeof (sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = handler; sa.sa_flags = SA_RESTART; return sigaction(sig, &sa, NULL); } -int -svc_lock(const char *applet) +int svc_lock(const char *applet) { char *file = NULL; int fd; @@ -252,8 +251,7 @@ svc_lock(const char *applet) return fd; } -int -svc_unlock(const char *applet, int fd) +int svc_unlock(const char *applet, int fd) { char *file = NULL; @@ -264,8 +262,7 @@ svc_unlock(const char *applet, int fd) return -1; } -pid_t -exec_service(const char *service, const char *arg) +pid_t exec_service(const char *service, const char *arg) { char *file, sfd[32]; int fd; @@ -288,7 +285,7 @@ exec_service(const char *service, const char *arg) snprintf(sfd, sizeof(sfd), "%d", fd); /* We need to block signals until we have forked */ - memset(&sa, 0, sizeof (sa)); + memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_DFL; sigemptyset(&sa.sa_mask); sigfillset(&full); @@ -308,15 +305,15 @@ exec_service(const char *service, const char *arg) sigprocmask(SIG_SETMASK, &old, NULL); /* Safe to run now */ - execl(file, file, "--lockfd", sfd, arg, (char *) NULL); - fprintf(stderr, "unable to exec `%s': %s\n", - file, strerror(errno)); + execl(file, file, "--lockfd", sfd, arg, (char *)NULL); + fprintf(stderr, "unable to exec `%s': %s\n", file, + strerror(errno)); svc_unlock(basename_c(service), fd); _exit(EXIT_FAILURE); } if (pid == -1) { - fprintf(stderr, "fork: %s\n",strerror (errno)); + fprintf(stderr, "fork: %s\n", strerror(errno)); svc_unlock(basename_c(service), fd); } else fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); @@ -326,8 +323,7 @@ exec_service(const char *service, const char *arg) return pid; } -int -parse_mode(mode_t *mode, char *text) +int parse_mode(mode_t *mode, char *text) { char *p; unsigned long l; @@ -339,7 +335,7 @@ parse_mode(mode_t *mode, char *text) errno = EINVAL; return -1; } - *mode = (mode_t) l; + *mode = (mode_t)l; return 0; } @@ -348,8 +344,7 @@ parse_mode(mode_t *mode, char *text) return -1; } -int -is_writable(const char *path) +int is_writable(const char *path) { if (access(path, W_OK) == 0) return 1; @@ -357,7 +352,7 @@ is_writable(const char *path) return 0; } -RC_DEPTREE * _rc_deptree_load(int force, int *regen) +RC_DEPTREE *_rc_deptree_load(int force, int *regen) { int fd; int retval; @@ -383,17 +378,19 @@ RC_DEPTREE * _rc_deptree_load(int force, int *regen) *regen = 1; ebegin("Caching service dependencies"); retval = rc_deptree_update() ? 0 : -1; - eend (retval, "Failed to update the dependency tree"); + eend(retval, "Failed to update the dependency tree"); if (retval == 0) { if (stat(RC_DEPTREE_CACHE, &st) != 0) { - eerror("stat(%s): %s", RC_DEPTREE_CACHE, strerror(errno)); + eerror("stat(%s): %s", RC_DEPTREE_CACHE, + strerror(errno)); return NULL; } if (st.st_mtime < t) { eerror("Clock skew detected with `%s'", file); eerrorn("Adjusting mtime of `" RC_DEPTREE_CACHE - "' to %s", ctime(&t)); + "' to %s", + ctime(&t)); fp = fopen(RC_DEPTREE_SKEWED, "w"); if (fp != NULL) { fprintf(fp, "%s\n", file); @@ -414,18 +411,45 @@ RC_DEPTREE * _rc_deptree_load(int force, int *regen) } static const struct { - const char * const name; + const char *const name; RC_SERVICE bit; } service_bits[] = { - { "service_started", RC_SERVICE_STARTED, }, - { "service_stopped", RC_SERVICE_STOPPED, }, - { "service_inactive", RC_SERVICE_INACTIVE, }, - { "service_starting", RC_SERVICE_STARTING, }, - { "service_stopping", RC_SERVICE_STOPPING, }, - { "service_hotplugged", RC_SERVICE_HOTPLUGGED, }, - { "service_wasinactive", RC_SERVICE_WASINACTIVE, }, - { "service_failed", RC_SERVICE_FAILED, }, - { "service_crashed", RC_SERVICE_CRASHED, }, + { + "service_started", + RC_SERVICE_STARTED, + }, + { + "service_stopped", + RC_SERVICE_STOPPED, + }, + { + "service_inactive", + RC_SERVICE_INACTIVE, + }, + { + "service_starting", + RC_SERVICE_STARTING, + }, + { + "service_stopping", + RC_SERVICE_STOPPING, + }, + { + "service_hotplugged", + RC_SERVICE_HOTPLUGGED, + }, + { + "service_wasinactive", + RC_SERVICE_WASINACTIVE, + }, + { + "service_failed", + RC_SERVICE_FAILED, + }, + { + "service_crashed", + RC_SERVICE_CRASHED, + }, }; RC_SERVICE lookup_service_state(const char *service) @@ -454,8 +478,8 @@ time_t to_time_t(char *timestring) struct tm breakdown = {0}; time_t result = -1; - check = sscanf(timestring, "%4d-%2d-%2d %2d:%2d:%2d", - &year, &month, &day, &hour, &min, &sec); + check = sscanf(timestring, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, + &day, &hour, &min, &sec); if (check == 6) { breakdown.tm_year = year - 1900; /* years since 1900 */ breakdown.tm_mon = month - 1; @@ -469,7 +493,7 @@ time_t to_time_t(char *timestring) return result; } -pid_t get_pid(const char *applet,const char *pidfile) +pid_t get_pid(const char *applet, const char *pidfile) { FILE *fp; pid_t pid; diff --git a/src/shared/misc.h b/src/shared/misc.h index 6821038d5..5dc6069b3 100644 --- a/src/shared/misc.h +++ b/src/shared/misc.h @@ -18,29 +18,30 @@ #ifndef __RC_MISC_H__ #define __RC_MISC_H__ -#include #include #include #include #include +#include #include #include #include "helpers.h" +#include "rc.h" -#define RC_LEVEL_BOOT "boot" -#define RC_LEVEL_DEFAULT "default" +#define RC_LEVEL_BOOT "boot" +#define RC_LEVEL_DEFAULT "default" -#define RC_DEPTREE_CACHE RC_SVCDIR "/deptree" -#define RC_DEPTREE_SKEWED RC_SVCDIR "/clock-skewed" -#define RC_KRUNLEVEL RC_SVCDIR "/krunlevel" -#define RC_STARTING RC_SVCDIR "/rc.starting" -#define RC_STOPPING RC_SVCDIR "/rc.stopping" +#define RC_DEPTREE_CACHE RC_SVCDIR "/deptree" +#define RC_DEPTREE_SKEWED RC_SVCDIR "/clock-skewed" +#define RC_KRUNLEVEL RC_SVCDIR "/krunlevel" +#define RC_STARTING RC_SVCDIR "/rc.starting" +#define RC_STOPPING RC_SVCDIR "/rc.stopping" -#define RC_SVCDIR_STARTING RC_SVCDIR "/starting" -#define RC_SVCDIR_INACTIVE RC_SVCDIR "/inactive" -#define RC_SVCDIR_STARTED RC_SVCDIR "/started" -#define RC_SVCDIR_COLDPLUGGED RC_SVCDIR "/coldplugged" +#define RC_SVCDIR_STARTING RC_SVCDIR "/starting" +#define RC_SVCDIR_INACTIVE RC_SVCDIR "/inactive" +#define RC_SVCDIR_STARTED RC_SVCDIR "/started" +#define RC_SVCDIR_COLDPLUGGED RC_SVCDIR "/coldplugged" char *rc_conf_value(const char *var); bool rc_conf_yesno(const char *var); @@ -59,12 +60,12 @@ pid_t exec_service(const char *, const char *); int is_writable(const char *); #define service_start(service) exec_service(service, "start"); -#define service_stop(service) exec_service(service, "stop"); +#define service_stop(service) exec_service(service, "stop"); int parse_mode(mode_t *, char *); /* Handy function so we can wrap einfo around our deptree */ -RC_DEPTREE *_rc_deptree_load (int, int *); +RC_DEPTREE *_rc_deptree_load(int, int *); RC_SERVICE lookup_service_state(const char *service); void from_time_t(char *time_string, time_t tv); diff --git a/src/shared/plugin.c b/src/shared/plugin.c index cce845fa8..4f31e8c57 100644 --- a/src/shared/plugin.c +++ b/src/shared/plugin.c @@ -30,17 +30,16 @@ #include #include "einfo.h" -#include "queue.h" -#include "rc.h" #include "misc.h" #include "plugin.h" +#include "queue.h" +#include "rc.h" #define RC_PLUGIN_HOOK "rc_plugin_hook" bool rc_in_plugin = false; -typedef struct plugin -{ +typedef struct plugin { char *name; void *handle; int (*hook)(RC_HOOK, const char *); @@ -49,8 +48,7 @@ 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; @@ -62,8 +60,7 @@ dlfunc(void * __restrict handle, const char * __restrict symbol) } #endif -void -rc_plugin_load(void) +void rc_plugin_load(void) { DIR *dp; struct dirent *d; @@ -85,7 +82,7 @@ rc_plugin_load(void) if (d->d_name[0] == '.') continue; - xasprintf(&file, RC_PLUGINDIR "/%s", d->d_name); + xasprintf(&file, RC_PLUGINDIR "/%s", d->d_name); h = dlopen(file, RTLD_LAZY); free(file); if (h == NULL) { @@ -93,11 +90,11 @@ rc_plugin_load(void) continue; } - fptr = (int (*)(RC_HOOK, const char *)) - dlfunc(h, 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); + eerror("%s: cannot find symbol `%s'", d->d_name, + RC_PLUGIN_HOOK); dlclose(h); } else { plugin = xmalloc(sizeof(*plugin)); @@ -110,8 +107,7 @@ rc_plugin_load(void) closedir(dp); } -int -rc_waitpid(pid_t pid) +int rc_waitpid(pid_t pid) { int status; @@ -124,8 +120,7 @@ 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; @@ -165,8 +160,8 @@ rc_plugin_run(RC_HOOK hook, const char *value) * This is actually quite important as without this, the splash * plugin will probably hang when running in silent mode. */ for (i = 0; i < 2; i++) - if ((flags = fcntl (pfd[i], F_GETFD, 0)) < 0 || - fcntl (pfd[i], F_SETFD, flags | FD_CLOEXEC) < 0) + if ((flags = fcntl(pfd[i], F_GETFD, 0)) < 0 || + fcntl(pfd[i], F_SETFD, flags | FD_CLOEXEC) < 0) eerror("fcntl: %s", strerror(errno)); sigprocmask(SIG_SETMASK, &full, &old); @@ -181,8 +176,8 @@ rc_plugin_run(RC_HOOK hook, const char *value) if (pid == 0) { /* Restore default handlers */ sigaction(SIGCHLD, &sa, NULL); - sigaction(SIGHUP, &sa, NULL); - sigaction(SIGINT, &sa, NULL); + sigaction(SIGHUP, &sa, NULL); + sigaction(SIGINT, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGUSR1, &sa, NULL); @@ -228,8 +223,7 @@ 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; diff --git a/src/shared/plugin.h b/src/shared/plugin.h index 86ec1bfe3..c2233fb5a 100644 --- a/src/shared/plugin.h +++ b/src/shared/plugin.h @@ -18,6 +18,10 @@ #ifndef __LIBRC_PLUGIN_H__ #define __LIBRC_PLUGIN_H__ +#include + +#include "rc.h" + /* A simple flag to say if we're in a plugin process or not. * Mainly used in atexit code. */ extern bool rc_in_plugin; @@ -30,12 +34,12 @@ void rc_plugin_run(RC_HOOK, const char *value); /* dlfunc defines needed to avoid ISO errors. FreeBSD has this right :) */ #if !defined(__FreeBSD__) && !defined(__DragonFly__) struct __dlfunc_arg { - int __dlfunc_dummy; + int __dlfunc_dummy; }; -typedef void (*dlfunc_t)(struct __dlfunc_arg); +typedef void (*dlfunc_t)(struct __dlfunc_arg); -dlfunc_t dlfunc (void * __restrict handle, const char * __restrict symbol); +dlfunc_t dlfunc(void *__restrict handle, const char *__restrict symbol); #endif #endif diff --git a/src/shared/queue.h b/src/shared/queue.h index ddb7821d7..598c86adc 100644 --- a/src/shared/queue.h +++ b/src/shared/queue.h @@ -31,8 +31,8 @@ * @(#)queue.h 8.5 (Berkeley) 8/20/94 */ -#ifndef _SYS_QUEUE_H_ -#define _SYS_QUEUE_H_ +#ifndef _SYS_QUEUE_H_ +#define _SYS_QUEUE_H_ /* * This file defines five types of data structures: singly-linked lists, @@ -92,569 +92,616 @@ #endif #if defined(QUEUEDEBUG) -# if defined(_KERNEL) -# define QUEUEDEBUG_ABORT(...) panic(__VA_ARGS__) -# else -# include -# define QUEUEDEBUG_ABORT(...) err(1, __VA_ARGS__) -# endif +#if defined(_KERNEL) +#define QUEUEDEBUG_ABORT(...) panic(__VA_ARGS__) +#else +#include +#define QUEUEDEBUG_ABORT(...) err(1, __VA_ARGS__) +#endif #endif /* * Singly-linked List definitions. */ -#define SLIST_HEAD(name, type) \ -struct name { \ - struct type *slh_first; /* first element */ \ -} +#define SLIST_HEAD(name, type) \ + struct name { \ + struct type *slh_first; /* first element */ \ + } -#define SLIST_HEAD_INITIALIZER(head) \ - { NULL } +#define SLIST_HEAD_INITIALIZER(head) \ + { \ + NULL \ + } -#define SLIST_ENTRY(type) \ -struct { \ - struct type *sle_next; /* next element */ \ -} +#define SLIST_ENTRY(type) \ + struct { \ + struct type *sle_next; /* next element */ \ + } /* * Singly-linked List access methods. */ -#define SLIST_FIRST(head) ((head)->slh_first) -#define SLIST_END(head) NULL -#define SLIST_EMPTY(head) ((head)->slh_first == NULL) -#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) - -#define SLIST_FOREACH(var, head, field) \ - for((var) = (head)->slh_first; \ - (var) != SLIST_END(head); \ - (var) = (var)->field.sle_next) - -#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = SLIST_FIRST((head)); \ - (var) != SLIST_END(head) && \ - ((tvar) = SLIST_NEXT((var), field), 1); \ - (var) = (tvar)) +#define SLIST_FIRST(head) ((head)->slh_first) +#define SLIST_END(head) NULL +#define SLIST_EMPTY(head) ((head)->slh_first == NULL) +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) + +#define SLIST_FOREACH(var, head, field) \ + for ((var) = (head)->slh_first; (var) != SLIST_END(head); \ + (var) = (var)->field.sle_next) + +#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SLIST_FIRST((head)); \ + (var) != SLIST_END(head) && \ + ((tvar) = SLIST_NEXT((var), field), 1); \ + (var) = (tvar)) /* * Singly-linked List functions. */ -#define SLIST_INIT(head) do { \ - (head)->slh_first = SLIST_END(head); \ -} while (/*CONSTCOND*/0) - -#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ - (elm)->field.sle_next = (slistelm)->field.sle_next; \ - (slistelm)->field.sle_next = (elm); \ -} while (/*CONSTCOND*/0) - -#define SLIST_INSERT_HEAD(head, elm, field) do { \ - (elm)->field.sle_next = (head)->slh_first; \ - (head)->slh_first = (elm); \ -} while (/*CONSTCOND*/0) - -#define SLIST_REMOVE_AFTER(slistelm, field) do { \ - (slistelm)->field.sle_next = \ - SLIST_NEXT(SLIST_NEXT((slistelm), field), field); \ -} while (/*CONSTCOND*/0) - -#define SLIST_REMOVE_HEAD(head, field) do { \ - (head)->slh_first = (head)->slh_first->field.sle_next; \ -} while (/*CONSTCOND*/0) - -#define SLIST_REMOVE(head, elm, type, field) do { \ - if ((head)->slh_first == (elm)) { \ - SLIST_REMOVE_HEAD((head), field); \ - } \ - else { \ - struct type *curelm = (head)->slh_first; \ - while(curelm->field.sle_next != (elm)) \ - curelm = curelm->field.sle_next; \ - curelm->field.sle_next = \ - curelm->field.sle_next->field.sle_next; \ - } \ -} while (/*CONSTCOND*/0) - +#define SLIST_INIT(head) \ + do { \ + (head)->slh_first = SLIST_END(head); \ + } while (/*CONSTCOND*/ 0) + +#define SLIST_INSERT_AFTER(slistelm, elm, field) \ + do { \ + (elm)->field.sle_next = (slistelm)->field.sle_next; \ + (slistelm)->field.sle_next = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define SLIST_INSERT_HEAD(head, elm, field) \ + do { \ + (elm)->field.sle_next = (head)->slh_first; \ + (head)->slh_first = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define SLIST_REMOVE_AFTER(slistelm, field) \ + do { \ + (slistelm)->field.sle_next = \ + SLIST_NEXT(SLIST_NEXT((slistelm), field), field); \ + } while (/*CONSTCOND*/ 0) + +#define SLIST_REMOVE_HEAD(head, field) \ + do { \ + (head)->slh_first = (head)->slh_first->field.sle_next; \ + } while (/*CONSTCOND*/ 0) + +#define SLIST_REMOVE(head, elm, type, field) \ + do { \ + if ((head)->slh_first == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } else { \ + struct type *curelm = (head)->slh_first; \ + while (curelm->field.sle_next != (elm)) \ + curelm = curelm->field.sle_next; \ + curelm->field.sle_next = \ + curelm->field.sle_next->field.sle_next; \ + } \ + } while (/*CONSTCOND*/ 0) /* * List definitions. */ -#define LIST_HEAD(name, type) \ -struct name { \ - struct type *lh_first; /* first element */ \ -} +#define LIST_HEAD(name, type) \ + struct name { \ + struct type *lh_first; /* first element */ \ + } -#define LIST_HEAD_INITIALIZER(head) \ - { NULL } +#define LIST_HEAD_INITIALIZER(head) \ + { \ + NULL \ + } -#define LIST_ENTRY(type) \ -struct { \ - struct type *le_next; /* next element */ \ - struct type **le_prev; /* address of previous next element */ \ -} +#define LIST_ENTRY(type) \ + struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ + } /* * List access methods. */ -#define LIST_FIRST(head) ((head)->lh_first) -#define LIST_END(head) NULL -#define LIST_EMPTY(head) ((head)->lh_first == LIST_END(head)) -#define LIST_NEXT(elm, field) ((elm)->field.le_next) - -#define LIST_FOREACH(var, head, field) \ - for ((var) = ((head)->lh_first); \ - (var) != LIST_END(head); \ - (var) = ((var)->field.le_next)) - -#define LIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = LIST_FIRST((head)); \ - (var) != LIST_END(head) && \ - ((tvar) = LIST_NEXT((var), field), 1); \ - (var) = (tvar)) - -#define LIST_MOVE(head1, head2) do { \ - LIST_INIT((head2)); \ - if (!LIST_EMPTY((head1))) { \ - (head2)->lh_first = (head1)->lh_first; \ - LIST_INIT((head1)); \ - } \ -} while (/*CONSTCOND*/0) +#define LIST_FIRST(head) ((head)->lh_first) +#define LIST_END(head) NULL +#define LIST_EMPTY(head) ((head)->lh_first == LIST_END(head)) +#define LIST_NEXT(elm, field) ((elm)->field.le_next) + +#define LIST_FOREACH(var, head, field) \ + for ((var) = ((head)->lh_first); (var) != LIST_END(head); \ + (var) = ((var)->field.le_next)) + +#define LIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) != LIST_END(head) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define LIST_MOVE(head1, head2) \ + do { \ + LIST_INIT((head2)); \ + if (!LIST_EMPTY((head1))) { \ + (head2)->lh_first = (head1)->lh_first; \ + LIST_INIT((head1)); \ + } \ + } while (/*CONSTCOND*/ 0) /* * List functions. */ #if defined(QUEUEDEBUG) -#define QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field) \ - if ((head)->lh_first && \ - (head)->lh_first->field.le_prev != &(head)->lh_first) \ - QUEUEDEBUG_ABORT("LIST_INSERT_HEAD %p %s:%d", (head), \ - __FILE__, __LINE__); -#define QUEUEDEBUG_LIST_OP(elm, field) \ - if ((elm)->field.le_next && \ - (elm)->field.le_next->field.le_prev != \ - &(elm)->field.le_next) \ - QUEUEDEBUG_ABORT("LIST_* forw %p %s:%d", (elm), \ - __FILE__, __LINE__); \ - if (*(elm)->field.le_prev != (elm)) \ - QUEUEDEBUG_ABORT("LIST_* back %p %s:%d", (elm), \ - __FILE__, __LINE__); -#define QUEUEDEBUG_LIST_POSTREMOVE(elm, field) \ - (elm)->field.le_next = (void *)1L; \ +#define QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field) \ + if ((head)->lh_first && \ + (head)->lh_first->field.le_prev != &(head)->lh_first) \ + QUEUEDEBUG_ABORT("LIST_INSERT_HEAD %p %s:%d", (head), \ + __FILE__, __LINE__); +#define QUEUEDEBUG_LIST_OP(elm, field) \ + if ((elm)->field.le_next && \ + (elm)->field.le_next->field.le_prev != &(elm)->field.le_next) \ + QUEUEDEBUG_ABORT("LIST_* forw %p %s:%d", (elm), __FILE__, \ + __LINE__); \ + if (*(elm)->field.le_prev != (elm)) \ + QUEUEDEBUG_ABORT("LIST_* back %p %s:%d", (elm), __FILE__, \ + __LINE__); +#define QUEUEDEBUG_LIST_POSTREMOVE(elm, field) \ + (elm)->field.le_next = (void *)1L; \ (elm)->field.le_prev = (void *)1L; #else -#define QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field) -#define QUEUEDEBUG_LIST_OP(elm, field) -#define QUEUEDEBUG_LIST_POSTREMOVE(elm, field) +#define QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field) +#define QUEUEDEBUG_LIST_OP(elm, field) +#define QUEUEDEBUG_LIST_POSTREMOVE(elm, field) #endif -#define LIST_INIT(head) do { \ - (head)->lh_first = LIST_END(head); \ -} while (/*CONSTCOND*/0) - -#define LIST_INSERT_AFTER(listelm, elm, field) do { \ - QUEUEDEBUG_LIST_OP((listelm), field) \ - if (((elm)->field.le_next = (listelm)->field.le_next) != \ - LIST_END(head)) \ - (listelm)->field.le_next->field.le_prev = \ - &(elm)->field.le_next; \ - (listelm)->field.le_next = (elm); \ - (elm)->field.le_prev = &(listelm)->field.le_next; \ -} while (/*CONSTCOND*/0) - -#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ - QUEUEDEBUG_LIST_OP((listelm), field) \ - (elm)->field.le_prev = (listelm)->field.le_prev; \ - (elm)->field.le_next = (listelm); \ - *(listelm)->field.le_prev = (elm); \ - (listelm)->field.le_prev = &(elm)->field.le_next; \ -} while (/*CONSTCOND*/0) - -#define LIST_INSERT_HEAD(head, elm, field) do { \ - QUEUEDEBUG_LIST_INSERT_HEAD((head), (elm), field) \ - if (((elm)->field.le_next = (head)->lh_first) != LIST_END(head))\ - (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ - (head)->lh_first = (elm); \ - (elm)->field.le_prev = &(head)->lh_first; \ -} while (/*CONSTCOND*/0) - -#define LIST_REMOVE(elm, field) do { \ - QUEUEDEBUG_LIST_OP((elm), field) \ - if ((elm)->field.le_next != NULL) \ - (elm)->field.le_next->field.le_prev = \ - (elm)->field.le_prev; \ - *(elm)->field.le_prev = (elm)->field.le_next; \ - QUEUEDEBUG_LIST_POSTREMOVE((elm), field) \ -} while (/*CONSTCOND*/0) - -#define LIST_REPLACE(elm, elm2, field) do { \ - if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ - (elm2)->field.le_next->field.le_prev = \ - &(elm2)->field.le_next; \ - (elm2)->field.le_prev = (elm)->field.le_prev; \ - *(elm2)->field.le_prev = (elm2); \ - QUEUEDEBUG_LIST_POSTREMOVE((elm), field) \ -} while (/*CONSTCOND*/0) +#define LIST_INIT(head) \ + do { \ + (head)->lh_first = LIST_END(head); \ + } while (/*CONSTCOND*/ 0) + +#define LIST_INSERT_AFTER(listelm, elm, field) \ + do { \ + QUEUEDEBUG_LIST_OP((listelm), field) \ + if (((elm)->field.le_next = (listelm)->field.le_next) != \ + LIST_END(head)) \ + (listelm)->field.le_next->field.le_prev = \ + &(elm)->field.le_next; \ + (listelm)->field.le_next = (elm); \ + (elm)->field.le_prev = &(listelm)->field.le_next; \ + } while (/*CONSTCOND*/ 0) + +#define LIST_INSERT_BEFORE(listelm, elm, field) \ + do { \ + QUEUEDEBUG_LIST_OP((listelm), field) \ + (elm)->field.le_prev = (listelm)->field.le_prev; \ + (elm)->field.le_next = (listelm); \ + *(listelm)->field.le_prev = (elm); \ + (listelm)->field.le_prev = &(elm)->field.le_next; \ + } while (/*CONSTCOND*/ 0) + +#define LIST_INSERT_HEAD(head, elm, field) \ + do { \ + QUEUEDEBUG_LIST_INSERT_HEAD((head), (elm), field) \ + if (((elm)->field.le_next = (head)->lh_first) != \ + LIST_END(head)) \ + (head)->lh_first->field.le_prev = \ + &(elm)->field.le_next; \ + (head)->lh_first = (elm); \ + (elm)->field.le_prev = &(head)->lh_first; \ + } while (/*CONSTCOND*/ 0) + +#define LIST_REMOVE(elm, field) \ + do { \ + QUEUEDEBUG_LIST_OP((elm), field) \ + if ((elm)->field.le_next != NULL) \ + (elm)->field.le_next->field.le_prev = \ + (elm)->field.le_prev; \ + *(elm)->field.le_prev = (elm)->field.le_next; \ + QUEUEDEBUG_LIST_POSTREMOVE((elm), field) \ + } while (/*CONSTCOND*/ 0) + +#define LIST_REPLACE(elm, elm2, field) \ + do { \ + if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ + (elm2)->field.le_next->field.le_prev = \ + &(elm2)->field.le_next; \ + (elm2)->field.le_prev = (elm)->field.le_prev; \ + *(elm2)->field.le_prev = (elm2); \ + QUEUEDEBUG_LIST_POSTREMOVE((elm), field) \ + } while (/*CONSTCOND*/ 0) /* * Simple queue definitions. */ -#define SIMPLEQ_HEAD(name, type) \ -struct name { \ - struct type *sqh_first; /* first element */ \ - struct type **sqh_last; /* addr of last next element */ \ -} +#define SIMPLEQ_HEAD(name, type) \ + struct name { \ + struct type *sqh_first; /* first element */ \ + struct type **sqh_last; /* addr of last next element */ \ + } -#define SIMPLEQ_HEAD_INITIALIZER(head) \ - { NULL, &(head).sqh_first } +#define SIMPLEQ_HEAD_INITIALIZER(head) \ + { \ + NULL, &(head).sqh_first \ + } -#define SIMPLEQ_ENTRY(type) \ -struct { \ - struct type *sqe_next; /* next element */ \ -} +#define SIMPLEQ_ENTRY(type) \ + struct { \ + struct type *sqe_next; /* next element */ \ + } /* * Simple queue access methods. */ -#define SIMPLEQ_FIRST(head) ((head)->sqh_first) -#define SIMPLEQ_END(head) NULL -#define SIMPLEQ_EMPTY(head) ((head)->sqh_first == SIMPLEQ_END(head)) -#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) - -#define SIMPLEQ_FOREACH(var, head, field) \ - for ((var) = ((head)->sqh_first); \ - (var) != SIMPLEQ_END(head); \ - (var) = ((var)->field.sqe_next)) - -#define SIMPLEQ_FOREACH_SAFE(var, head, field, next) \ - for ((var) = ((head)->sqh_first); \ - (var) != SIMPLEQ_END(head) && \ - ((next = ((var)->field.sqe_next)), 1); \ - (var) = (next)) +#define SIMPLEQ_FIRST(head) ((head)->sqh_first) +#define SIMPLEQ_END(head) NULL +#define SIMPLEQ_EMPTY(head) ((head)->sqh_first == SIMPLEQ_END(head)) +#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) + +#define SIMPLEQ_FOREACH(var, head, field) \ + for ((var) = ((head)->sqh_first); (var) != SIMPLEQ_END(head); \ + (var) = ((var)->field.sqe_next)) + +#define SIMPLEQ_FOREACH_SAFE(var, head, field, next) \ + for ((var) = ((head)->sqh_first); \ + (var) != SIMPLEQ_END(head) && \ + ((next = ((var)->field.sqe_next)), 1); \ + (var) = (next)) /* * Simple queue functions. */ -#define SIMPLEQ_INIT(head) do { \ - (head)->sqh_first = NULL; \ - (head)->sqh_last = &(head)->sqh_first; \ -} while (/*CONSTCOND*/0) - -#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \ - if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ - (head)->sqh_last = &(elm)->field.sqe_next; \ - (head)->sqh_first = (elm); \ -} while (/*CONSTCOND*/0) - -#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \ - (elm)->field.sqe_next = NULL; \ - *(head)->sqh_last = (elm); \ - (head)->sqh_last = &(elm)->field.sqe_next; \ -} while (/*CONSTCOND*/0) - -#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ - if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\ - (head)->sqh_last = &(elm)->field.sqe_next; \ - (listelm)->field.sqe_next = (elm); \ -} while (/*CONSTCOND*/0) - -#define SIMPLEQ_REMOVE_HEAD(head, field) do { \ - if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \ - (head)->sqh_last = &(head)->sqh_first; \ -} while (/*CONSTCOND*/0) - -#define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \ - if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \ - == NULL) \ - (head)->sqh_last = &(elm)->field.sqe_next; \ -} while (/*CONSTCOND*/0) - -#define SIMPLEQ_REMOVE(head, elm, type, field) do { \ - if ((head)->sqh_first == (elm)) { \ - SIMPLEQ_REMOVE_HEAD((head), field); \ - } else { \ - struct type *curelm = (head)->sqh_first; \ - while (curelm->field.sqe_next != (elm)) \ - curelm = curelm->field.sqe_next; \ - if ((curelm->field.sqe_next = \ - curelm->field.sqe_next->field.sqe_next) == NULL) \ - (head)->sqh_last = &(curelm)->field.sqe_next; \ - } \ -} while (/*CONSTCOND*/0) - -#define SIMPLEQ_CONCAT(head1, head2) do { \ - if (!SIMPLEQ_EMPTY((head2))) { \ - *(head1)->sqh_last = (head2)->sqh_first; \ - (head1)->sqh_last = (head2)->sqh_last; \ - SIMPLEQ_INIT((head2)); \ - } \ -} while (/*CONSTCOND*/0) - -#define SIMPLEQ_LAST(head, type, field) \ - (SIMPLEQ_EMPTY((head)) ? \ - NULL : \ - ((struct type *)(void *) \ - ((char *)((head)->sqh_last) - offsetof(struct type, field)))) +#define SIMPLEQ_INIT(head) \ + do { \ + (head)->sqh_first = NULL; \ + (head)->sqh_last = &(head)->sqh_first; \ + } while (/*CONSTCOND*/ 0) + +#define SIMPLEQ_INSERT_HEAD(head, elm, field) \ + do { \ + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + (head)->sqh_first = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define SIMPLEQ_INSERT_TAIL(head, elm, field) \ + do { \ + (elm)->field.sqe_next = NULL; \ + *(head)->sqh_last = (elm); \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + } while (/*CONSTCOND*/ 0) + +#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \ + do { \ + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == \ + NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + (listelm)->field.sqe_next = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define SIMPLEQ_REMOVE_HEAD(head, field) \ + do { \ + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == \ + NULL) \ + (head)->sqh_last = &(head)->sqh_first; \ + } while (/*CONSTCOND*/ 0) + +#define SIMPLEQ_REMOVE_AFTER(head, elm, field) \ + do { \ + if (((elm)->field.sqe_next = \ + (elm)->field.sqe_next->field.sqe_next) == NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + } while (/*CONSTCOND*/ 0) + +#define SIMPLEQ_REMOVE(head, elm, type, field) \ + do { \ + if ((head)->sqh_first == (elm)) { \ + SIMPLEQ_REMOVE_HEAD((head), field); \ + } else { \ + struct type *curelm = (head)->sqh_first; \ + while (curelm->field.sqe_next != (elm)) \ + curelm = curelm->field.sqe_next; \ + if ((curelm->field.sqe_next = \ + curelm->field.sqe_next->field.sqe_next) == \ + NULL) \ + (head)->sqh_last = &(curelm)->field.sqe_next; \ + } \ + } while (/*CONSTCOND*/ 0) + +#define SIMPLEQ_CONCAT(head1, head2) \ + do { \ + if (!SIMPLEQ_EMPTY((head2))) { \ + *(head1)->sqh_last = (head2)->sqh_first; \ + (head1)->sqh_last = (head2)->sqh_last; \ + SIMPLEQ_INIT((head2)); \ + } \ + } while (/*CONSTCOND*/ 0) + +#define SIMPLEQ_LAST(head, type, field) \ + (SIMPLEQ_EMPTY((head)) ? \ + NULL : \ + ((struct type *)(void *)((char *)((head)->sqh_last) - \ + offsetof(struct type, field)))) /* * Tail queue definitions. */ -#define _TAILQ_HEAD(name, type, qual) \ -struct name { \ - qual type *tqh_first; /* first element */ \ - qual type *qual *tqh_last; /* addr of last next element */ \ -} -#define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type,) +#define _TAILQ_HEAD(name, type, qual) \ + struct name { \ + qual type *tqh_first; /* first element */ \ + qual type *qual *tqh_last; /* addr of last next element */ \ + } +#define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type, ) -#define TAILQ_HEAD_INITIALIZER(head) \ - { TAILQ_END(head), &(head).tqh_first } +#define TAILQ_HEAD_INITIALIZER(head) \ + { \ + TAILQ_END(head), &(head).tqh_first \ + } -#define _TAILQ_ENTRY(type, qual) \ -struct { \ - qual type *tqe_next; /* next element */ \ - qual type *qual *tqe_prev; /* address of previous next element */\ -} -#define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type,) +#define _TAILQ_ENTRY(type, qual) \ + struct { \ + qual type *tqe_next; /* next element */ \ + qual type *qual \ + *tqe_prev; /* address of previous next element */ \ + } +#define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type, ) /* * Tail queue access methods. */ -#define TAILQ_FIRST(head) ((head)->tqh_first) -#define TAILQ_END(head) (NULL) -#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) -#define TAILQ_LAST(head, headname) \ +#define TAILQ_FIRST(head) ((head)->tqh_first) +#define TAILQ_END(head) (NULL) +#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) +#define TAILQ_LAST(head, headname) \ (*(((struct headname *)((head)->tqh_last))->tqh_last)) -#define TAILQ_PREV(elm, headname, field) \ +#define TAILQ_PREV(elm, headname, field) \ (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) -#define TAILQ_EMPTY(head) (TAILQ_FIRST(head) == TAILQ_END(head)) - +#define TAILQ_EMPTY(head) (TAILQ_FIRST(head) == TAILQ_END(head)) -#define TAILQ_FOREACH(var, head, field) \ - for ((var) = ((head)->tqh_first); \ - (var) != TAILQ_END(head); \ - (var) = ((var)->field.tqe_next)) +#define TAILQ_FOREACH(var, head, field) \ + for ((var) = ((head)->tqh_first); (var) != TAILQ_END(head); \ + (var) = ((var)->field.tqe_next)) -#define TAILQ_FOREACH_SAFE(var, head, field, next) \ - for ((var) = ((head)->tqh_first); \ - (var) != TAILQ_END(head) && \ - ((next) = TAILQ_NEXT(var, field), 1); (var) = (next)) +#define TAILQ_FOREACH_SAFE(var, head, field, next) \ + for ((var) = ((head)->tqh_first); \ + (var) != TAILQ_END(head) && ((next) = TAILQ_NEXT(var, field), 1); \ + (var) = (next)) -#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ - for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));\ - (var) != TAILQ_END(head); \ - (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last))) +#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ + for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \ + (var) != TAILQ_END(head); \ + (var) = (*( \ + ((struct headname *)((var)->field.tqe_prev))->tqh_last))) -#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, prev) \ - for ((var) = TAILQ_LAST((head), headname); \ - (var) != TAILQ_END(head) && \ - ((prev) = TAILQ_PREV((var), headname, field), 1); (var) = (prev)) +#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, prev) \ + for ((var) = TAILQ_LAST((head), headname); \ + (var) != TAILQ_END(head) && \ + ((prev) = TAILQ_PREV((var), headname, field), 1); \ + (var) = (prev)) /* * Tail queue functions. */ #if defined(QUEUEDEBUG) -#define QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field) \ - if ((head)->tqh_first && \ - (head)->tqh_first->field.tqe_prev != &(head)->tqh_first) \ - QUEUEDEBUG_ABORT("TAILQ_INSERT_HEAD %p %s:%d", (head), \ - __FILE__, __LINE__); -#define QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field) \ - if (*(head)->tqh_last != NULL) \ - QUEUEDEBUG_ABORT("TAILQ_INSERT_TAIL %p %s:%d", (head), \ - __FILE__, __LINE__); -#define QUEUEDEBUG_TAILQ_OP(elm, field) \ - if ((elm)->field.tqe_next && \ - (elm)->field.tqe_next->field.tqe_prev != \ - &(elm)->field.tqe_next) \ - QUEUEDEBUG_ABORT("TAILQ_* forw %p %s:%d", (elm), \ - __FILE__, __LINE__); \ - if (*(elm)->field.tqe_prev != (elm)) \ - QUEUEDEBUG_ABORT("TAILQ_* back %p %s:%d", (elm), \ - __FILE__, __LINE__); -#define QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field) \ - if ((elm)->field.tqe_next == NULL && \ - (head)->tqh_last != &(elm)->field.tqe_next) \ - QUEUEDEBUG_ABORT("TAILQ_PREREMOVE head %p elm %p %s:%d",\ - (head), (elm), __FILE__, __LINE__); -#define QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field) \ - (elm)->field.tqe_next = (void *)1L; \ +#define QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field) \ + if ((head)->tqh_first && \ + (head)->tqh_first->field.tqe_prev != &(head)->tqh_first) \ + QUEUEDEBUG_ABORT("TAILQ_INSERT_HEAD %p %s:%d", (head), \ + __FILE__, __LINE__); +#define QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field) \ + if (*(head)->tqh_last != NULL) \ + QUEUEDEBUG_ABORT("TAILQ_INSERT_TAIL %p %s:%d", (head), \ + __FILE__, __LINE__); +#define QUEUEDEBUG_TAILQ_OP(elm, field) \ + if ((elm)->field.tqe_next && \ + (elm)->field.tqe_next->field.tqe_prev != &(elm)->field.tqe_next) \ + QUEUEDEBUG_ABORT("TAILQ_* forw %p %s:%d", (elm), __FILE__, \ + __LINE__); \ + if (*(elm)->field.tqe_prev != (elm)) \ + QUEUEDEBUG_ABORT("TAILQ_* back %p %s:%d", (elm), __FILE__, \ + __LINE__); +#define QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field) \ + if ((elm)->field.tqe_next == NULL && \ + (head)->tqh_last != &(elm)->field.tqe_next) \ + QUEUEDEBUG_ABORT("TAILQ_PREREMOVE head %p elm %p %s:%d", \ + (head), (elm), __FILE__, __LINE__); +#define QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field) \ + (elm)->field.tqe_next = (void *)1L; \ (elm)->field.tqe_prev = (void *)1L; #else -#define QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field) -#define QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field) -#define QUEUEDEBUG_TAILQ_OP(elm, field) -#define QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field) -#define QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field) +#define QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field) +#define QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field) +#define QUEUEDEBUG_TAILQ_OP(elm, field) +#define QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field) +#define QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field) #endif -#define TAILQ_INIT(head) do { \ - (head)->tqh_first = TAILQ_END(head); \ - (head)->tqh_last = &(head)->tqh_first; \ -} while (/*CONSTCOND*/0) - -#define TAILQ_INSERT_HEAD(head, elm, field) do { \ - QUEUEDEBUG_TAILQ_INSERT_HEAD((head), (elm), field) \ - if (((elm)->field.tqe_next = (head)->tqh_first) != TAILQ_END(head))\ - (head)->tqh_first->field.tqe_prev = \ - &(elm)->field.tqe_next; \ - else \ - (head)->tqh_last = &(elm)->field.tqe_next; \ - (head)->tqh_first = (elm); \ - (elm)->field.tqe_prev = &(head)->tqh_first; \ -} while (/*CONSTCOND*/0) - -#define TAILQ_INSERT_TAIL(head, elm, field) do { \ - QUEUEDEBUG_TAILQ_INSERT_TAIL((head), (elm), field) \ - (elm)->field.tqe_next = TAILQ_END(head); \ - (elm)->field.tqe_prev = (head)->tqh_last; \ - *(head)->tqh_last = (elm); \ - (head)->tqh_last = &(elm)->field.tqe_next; \ -} while (/*CONSTCOND*/0) - -#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ - QUEUEDEBUG_TAILQ_OP((listelm), field) \ - if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != \ - TAILQ_END(head)) \ - (elm)->field.tqe_next->field.tqe_prev = \ - &(elm)->field.tqe_next; \ - else \ - (head)->tqh_last = &(elm)->field.tqe_next; \ - (listelm)->field.tqe_next = (elm); \ - (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ -} while (/*CONSTCOND*/0) - -#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ - QUEUEDEBUG_TAILQ_OP((listelm), field) \ - (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ - (elm)->field.tqe_next = (listelm); \ - *(listelm)->field.tqe_prev = (elm); \ - (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ -} while (/*CONSTCOND*/0) - -#define TAILQ_REMOVE(head, elm, field) do { \ - QUEUEDEBUG_TAILQ_PREREMOVE((head), (elm), field) \ - QUEUEDEBUG_TAILQ_OP((elm), field) \ - if (((elm)->field.tqe_next) != TAILQ_END(head)) \ - (elm)->field.tqe_next->field.tqe_prev = \ - (elm)->field.tqe_prev; \ - else \ - (head)->tqh_last = (elm)->field.tqe_prev; \ - *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ - QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field); \ -} while (/*CONSTCOND*/0) - -#define TAILQ_REPLACE(head, elm, elm2, field) do { \ - if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != \ - TAILQ_END(head)) \ - (elm2)->field.tqe_next->field.tqe_prev = \ - &(elm2)->field.tqe_next; \ - else \ - (head)->tqh_last = &(elm2)->field.tqe_next; \ - (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ - *(elm2)->field.tqe_prev = (elm2); \ - QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field); \ -} while (/*CONSTCOND*/0) - -#define TAILQ_CONCAT(head1, head2, field) do { \ - if (!TAILQ_EMPTY(head2)) { \ - *(head1)->tqh_last = (head2)->tqh_first; \ - (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ - (head1)->tqh_last = (head2)->tqh_last; \ - TAILQ_INIT((head2)); \ - } \ -} while (/*CONSTCOND*/0) +#define TAILQ_INIT(head) \ + do { \ + (head)->tqh_first = TAILQ_END(head); \ + (head)->tqh_last = &(head)->tqh_first; \ + } while (/*CONSTCOND*/ 0) + +#define TAILQ_INSERT_HEAD(head, elm, field) \ + do { \ + QUEUEDEBUG_TAILQ_INSERT_HEAD((head), (elm), field) \ + if (((elm)->field.tqe_next = (head)->tqh_first) != \ + TAILQ_END(head)) \ + (head)->tqh_first->field.tqe_prev = \ + &(elm)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + (head)->tqh_first = (elm); \ + (elm)->field.tqe_prev = &(head)->tqh_first; \ + } while (/*CONSTCOND*/ 0) + +#define TAILQ_INSERT_TAIL(head, elm, field) \ + do { \ + QUEUEDEBUG_TAILQ_INSERT_TAIL((head), (elm), field) \ + (elm)->field.tqe_next = TAILQ_END(head); \ + (elm)->field.tqe_prev = (head)->tqh_last; \ + *(head)->tqh_last = (elm); \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + } while (/*CONSTCOND*/ 0) + +#define TAILQ_INSERT_AFTER(head, listelm, elm, field) \ + do { \ + QUEUEDEBUG_TAILQ_OP((listelm), field) \ + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != \ + TAILQ_END(head)) \ + (elm)->field.tqe_next->field.tqe_prev = \ + &(elm)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + (listelm)->field.tqe_next = (elm); \ + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ + } while (/*CONSTCOND*/ 0) + +#define TAILQ_INSERT_BEFORE(listelm, elm, field) \ + do { \ + QUEUEDEBUG_TAILQ_OP((listelm), field) \ + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ + (elm)->field.tqe_next = (listelm); \ + *(listelm)->field.tqe_prev = (elm); \ + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ + } while (/*CONSTCOND*/ 0) + +#define TAILQ_REMOVE(head, elm, field) \ + do { \ + QUEUEDEBUG_TAILQ_PREREMOVE((head), (elm), field) \ + QUEUEDEBUG_TAILQ_OP((elm), field) \ + if (((elm)->field.tqe_next) != TAILQ_END(head)) \ + (elm)->field.tqe_next->field.tqe_prev = \ + (elm)->field.tqe_prev; \ + else \ + (head)->tqh_last = (elm)->field.tqe_prev; \ + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ + QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field); \ + } while (/*CONSTCOND*/ 0) + +#define TAILQ_REPLACE(head, elm, elm2, field) \ + do { \ + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != \ + TAILQ_END(head)) \ + (elm2)->field.tqe_next->field.tqe_prev = \ + &(elm2)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm2)->field.tqe_next; \ + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ + *(elm2)->field.tqe_prev = (elm2); \ + QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field); \ + } while (/*CONSTCOND*/ 0) + +#define TAILQ_CONCAT(head1, head2, field) \ + do { \ + if (!TAILQ_EMPTY(head2)) { \ + *(head1)->tqh_last = (head2)->tqh_first; \ + (head2)->tqh_first->field.tqe_prev = \ + (head1)->tqh_last; \ + (head1)->tqh_last = (head2)->tqh_last; \ + TAILQ_INIT((head2)); \ + } \ + } while (/*CONSTCOND*/ 0) /* * Singly-linked Tail queue declarations. */ -#define STAILQ_HEAD(name, type) \ -struct name { \ - struct type *stqh_first; /* first element */ \ - struct type **stqh_last; /* addr of last next element */ \ -} +#define STAILQ_HEAD(name, type) \ + struct name { \ + struct type *stqh_first; /* first element */ \ + struct type **stqh_last; /* addr of last next element */ \ + } -#define STAILQ_HEAD_INITIALIZER(head) \ - { NULL, &(head).stqh_first } +#define STAILQ_HEAD_INITIALIZER(head) \ + { \ + NULL, &(head).stqh_first \ + } -#define STAILQ_ENTRY(type) \ -struct { \ - struct type *stqe_next; /* next element */ \ -} +#define STAILQ_ENTRY(type) \ + struct { \ + struct type *stqe_next; /* next element */ \ + } /* * Singly-linked Tail queue access methods. */ -#define STAILQ_FIRST(head) ((head)->stqh_first) -#define STAILQ_END(head) NULL -#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) -#define STAILQ_EMPTY(head) (STAILQ_FIRST(head) == STAILQ_END(head)) +#define STAILQ_FIRST(head) ((head)->stqh_first) +#define STAILQ_END(head) NULL +#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) +#define STAILQ_EMPTY(head) (STAILQ_FIRST(head) == STAILQ_END(head)) /* * Singly-linked Tail queue functions. */ -#define STAILQ_INIT(head) do { \ - (head)->stqh_first = NULL; \ - (head)->stqh_last = &(head)->stqh_first; \ -} while (/*CONSTCOND*/0) - -#define STAILQ_INSERT_HEAD(head, elm, field) do { \ - if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \ - (head)->stqh_last = &(elm)->field.stqe_next; \ - (head)->stqh_first = (elm); \ -} while (/*CONSTCOND*/0) - -#define STAILQ_INSERT_TAIL(head, elm, field) do { \ - (elm)->field.stqe_next = NULL; \ - *(head)->stqh_last = (elm); \ - (head)->stqh_last = &(elm)->field.stqe_next; \ -} while (/*CONSTCOND*/0) - -#define STAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ - if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\ - (head)->stqh_last = &(elm)->field.stqe_next; \ - (listelm)->field.stqe_next = (elm); \ -} while (/*CONSTCOND*/0) - -#define STAILQ_REMOVE_HEAD(head, field) do { \ - if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \ - (head)->stqh_last = &(head)->stqh_first; \ -} while (/*CONSTCOND*/0) - -#define STAILQ_REMOVE(head, elm, type, field) do { \ - if ((head)->stqh_first == (elm)) { \ - STAILQ_REMOVE_HEAD((head), field); \ - } else { \ - struct type *curelm = (head)->stqh_first; \ - while (curelm->field.stqe_next != (elm)) \ - curelm = curelm->field.stqe_next; \ - if ((curelm->field.stqe_next = \ - curelm->field.stqe_next->field.stqe_next) == NULL) \ - (head)->stqh_last = &(curelm)->field.stqe_next; \ - } \ -} while (/*CONSTCOND*/0) - -#define STAILQ_FOREACH(var, head, field) \ - for ((var) = ((head)->stqh_first); \ - (var); \ - (var) = ((var)->field.stqe_next)) - -#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = STAILQ_FIRST((head)); \ - (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ - (var) = (tvar)) - -#define STAILQ_CONCAT(head1, head2) do { \ - if (!STAILQ_EMPTY((head2))) { \ - *(head1)->stqh_last = (head2)->stqh_first; \ - (head1)->stqh_last = (head2)->stqh_last; \ - STAILQ_INIT((head2)); \ - } \ -} while (/*CONSTCOND*/0) - -#define STAILQ_LAST(head, type, field) \ - (STAILQ_EMPTY((head)) ? \ - NULL : \ - ((struct type *)(void *) \ - ((char *)((head)->stqh_last) - offsetof(struct type, field)))) - +#define STAILQ_INIT(head) \ + do { \ + (head)->stqh_first = NULL; \ + (head)->stqh_last = &(head)->stqh_first; \ + } while (/*CONSTCOND*/ 0) + +#define STAILQ_INSERT_HEAD(head, elm, field) \ + do { \ + if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \ + (head)->stqh_last = &(elm)->field.stqe_next; \ + (head)->stqh_first = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define STAILQ_INSERT_TAIL(head, elm, field) \ + do { \ + (elm)->field.stqe_next = NULL; \ + *(head)->stqh_last = (elm); \ + (head)->stqh_last = &(elm)->field.stqe_next; \ + } while (/*CONSTCOND*/ 0) + +#define STAILQ_INSERT_AFTER(head, listelm, elm, field) \ + do { \ + if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == \ + NULL) \ + (head)->stqh_last = &(elm)->field.stqe_next; \ + (listelm)->field.stqe_next = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define STAILQ_REMOVE_HEAD(head, field) \ + do { \ + if (((head)->stqh_first = \ + (head)->stqh_first->field.stqe_next) == NULL) \ + (head)->stqh_last = &(head)->stqh_first; \ + } while (/*CONSTCOND*/ 0) + +#define STAILQ_REMOVE(head, elm, type, field) \ + do { \ + if ((head)->stqh_first == (elm)) { \ + STAILQ_REMOVE_HEAD((head), field); \ + } else { \ + struct type *curelm = (head)->stqh_first; \ + while (curelm->field.stqe_next != (elm)) \ + curelm = curelm->field.stqe_next; \ + if ((curelm->field.stqe_next = \ + curelm->field.stqe_next->field \ + .stqe_next) == NULL) \ + (head)->stqh_last = \ + &(curelm)->field.stqe_next; \ + } \ + } while (/*CONSTCOND*/ 0) + +#define STAILQ_FOREACH(var, head, field) \ + for ((var) = ((head)->stqh_first); (var); \ + (var) = ((var)->field.stqe_next)) + +#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = STAILQ_FIRST((head)); \ + (var) && ((tvar) = STAILQ_NEXT((var), field), 1); (var) = (tvar)) + +#define STAILQ_CONCAT(head1, head2) \ + do { \ + if (!STAILQ_EMPTY((head2))) { \ + *(head1)->stqh_last = (head2)->stqh_first; \ + (head1)->stqh_last = (head2)->stqh_last; \ + STAILQ_INIT((head2)); \ + } \ + } while (/*CONSTCOND*/ 0) + +#define STAILQ_LAST(head, type, field) \ + (STAILQ_EMPTY((head)) ? \ + NULL : \ + ((struct type *)(void *)((char *)((head)->stqh_last) - \ + offsetof(struct type, field)))) #ifndef _KERNEL /* @@ -678,45 +725,44 @@ struct { \ * this by changing the head/tail sentinel values, but see the note above * this one. */ -static __inline const void * __launder_type(const void *); -static __inline const void * -__launder_type(const void *__x) +static __inline const void *__launder_type(const void *); +static __inline const void *__launder_type(const void *__x) { - __asm __volatile("" : "+r" (__x)); + __asm __volatile("" : "+r"(__x)); return __x; } #if defined(QUEUEDEBUG) -#define QUEUEDEBUG_CIRCLEQ_HEAD(head, field) \ - if ((head)->cqh_first != CIRCLEQ_ENDC(head) && \ - (head)->cqh_first->field.cqe_prev != CIRCLEQ_ENDC(head)) \ - QUEUEDEBUG_ABORT("CIRCLEQ head forw %p %s:%d", (head), \ - __FILE__, __LINE__); \ - if ((head)->cqh_last != CIRCLEQ_ENDC(head) && \ - (head)->cqh_last->field.cqe_next != CIRCLEQ_ENDC(head)) \ - QUEUEDEBUG_ABORT("CIRCLEQ head back %p %s:%d", (head), \ - __FILE__, __LINE__); -#define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field) \ - if ((elm)->field.cqe_next == CIRCLEQ_ENDC(head)) { \ - if ((head)->cqh_last != (elm)) \ - QUEUEDEBUG_ABORT("CIRCLEQ elm last %p %s:%d", \ - (elm), __FILE__, __LINE__); \ - } else { \ - if ((elm)->field.cqe_next->field.cqe_prev != (elm)) \ - QUEUEDEBUG_ABORT("CIRCLEQ elm forw %p %s:%d", \ - (elm), __FILE__, __LINE__); \ - } \ - if ((elm)->field.cqe_prev == CIRCLEQ_ENDC(head)) { \ - if ((head)->cqh_first != (elm)) \ - QUEUEDEBUG_ABORT("CIRCLEQ elm first %p %s:%d", \ - (elm), __FILE__, __LINE__); \ - } else { \ - if ((elm)->field.cqe_prev->field.cqe_next != (elm)) \ - QUEUEDEBUG_ABORT("CIRCLEQ elm prev %p %s:%d", \ - (elm), __FILE__, __LINE__); \ +#define QUEUEDEBUG_CIRCLEQ_HEAD(head, field) \ + if ((head)->cqh_first != CIRCLEQ_ENDC(head) && \ + (head)->cqh_first->field.cqe_prev != CIRCLEQ_ENDC(head)) \ + QUEUEDEBUG_ABORT("CIRCLEQ head forw %p %s:%d", (head), \ + __FILE__, __LINE__); \ + if ((head)->cqh_last != CIRCLEQ_ENDC(head) && \ + (head)->cqh_last->field.cqe_next != CIRCLEQ_ENDC(head)) \ + QUEUEDEBUG_ABORT("CIRCLEQ head back %p %s:%d", (head), \ + __FILE__, __LINE__); +#define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field) \ + if ((elm)->field.cqe_next == CIRCLEQ_ENDC(head)) { \ + if ((head)->cqh_last != (elm)) \ + QUEUEDEBUG_ABORT("CIRCLEQ elm last %p %s:%d", (elm), \ + __FILE__, __LINE__); \ + } else { \ + if ((elm)->field.cqe_next->field.cqe_prev != (elm)) \ + QUEUEDEBUG_ABORT("CIRCLEQ elm forw %p %s:%d", (elm), \ + __FILE__, __LINE__); \ + } \ + if ((elm)->field.cqe_prev == CIRCLEQ_ENDC(head)) { \ + if ((head)->cqh_first != (elm)) \ + QUEUEDEBUG_ABORT("CIRCLEQ elm first %p %s:%d", (elm), \ + __FILE__, __LINE__); \ + } else { \ + if ((elm)->field.cqe_prev->field.cqe_next != (elm)) \ + QUEUEDEBUG_ABORT("CIRCLEQ elm prev %p %s:%d", (elm), \ + __FILE__, __LINE__); \ } -#define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field) \ - (elm)->field.cqe_next = (void *)1L; \ +#define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field) \ + (elm)->field.cqe_next = (void *)1L; \ (elm)->field.cqe_prev = (void *)1L; #else #define QUEUEDEBUG_CIRCLEQ_HEAD(head, field) @@ -724,123 +770,128 @@ __launder_type(const void *__x) #define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field) #endif -#define CIRCLEQ_HEAD(name, type) \ -struct name { \ - struct type *cqh_first; /* first element */ \ - struct type *cqh_last; /* last element */ \ -} +#define CIRCLEQ_HEAD(name, type) \ + struct name { \ + struct type *cqh_first; /* first element */ \ + struct type *cqh_last; /* last element */ \ + } -#define CIRCLEQ_HEAD_INITIALIZER(head) \ - { CIRCLEQ_END(&head), CIRCLEQ_END(&head) } +#define CIRCLEQ_HEAD_INITIALIZER(head) \ + { \ + CIRCLEQ_END(&head), CIRCLEQ_END(&head) \ + } -#define CIRCLEQ_ENTRY(type) \ -struct { \ - struct type *cqe_next; /* next element */ \ - struct type *cqe_prev; /* previous element */ \ -} +#define CIRCLEQ_ENTRY(type) \ + struct { \ + struct type *cqe_next; /* next element */ \ + struct type *cqe_prev; /* previous element */ \ + } /* * Circular queue functions. */ -#define CIRCLEQ_INIT(head) do { \ - (head)->cqh_first = CIRCLEQ_END(head); \ - (head)->cqh_last = CIRCLEQ_END(head); \ -} while (/*CONSTCOND*/0) - -#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ - QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ - QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field) \ - (elm)->field.cqe_next = (listelm)->field.cqe_next; \ - (elm)->field.cqe_prev = (listelm); \ - if ((listelm)->field.cqe_next == CIRCLEQ_ENDC(head)) \ - (head)->cqh_last = (elm); \ - else \ - (listelm)->field.cqe_next->field.cqe_prev = (elm); \ - (listelm)->field.cqe_next = (elm); \ -} while (/*CONSTCOND*/0) - -#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ - QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ - QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field) \ - (elm)->field.cqe_next = (listelm); \ - (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ - if ((listelm)->field.cqe_prev == CIRCLEQ_ENDC(head)) \ - (head)->cqh_first = (elm); \ - else \ - (listelm)->field.cqe_prev->field.cqe_next = (elm); \ - (listelm)->field.cqe_prev = (elm); \ -} while (/*CONSTCOND*/0) - -#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ - QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ - (elm)->field.cqe_next = (head)->cqh_first; \ - (elm)->field.cqe_prev = CIRCLEQ_END(head); \ - if ((head)->cqh_last == CIRCLEQ_ENDC(head)) \ - (head)->cqh_last = (elm); \ - else \ - (head)->cqh_first->field.cqe_prev = (elm); \ - (head)->cqh_first = (elm); \ -} while (/*CONSTCOND*/0) - -#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ - QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ - (elm)->field.cqe_next = CIRCLEQ_END(head); \ - (elm)->field.cqe_prev = (head)->cqh_last; \ - if ((head)->cqh_first == CIRCLEQ_ENDC(head)) \ - (head)->cqh_first = (elm); \ - else \ - (head)->cqh_last->field.cqe_next = (elm); \ - (head)->cqh_last = (elm); \ -} while (/*CONSTCOND*/0) - -#define CIRCLEQ_REMOVE(head, elm, field) do { \ - QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ - QUEUEDEBUG_CIRCLEQ_ELM((head), (elm), field) \ - if ((elm)->field.cqe_next == CIRCLEQ_ENDC(head)) \ - (head)->cqh_last = (elm)->field.cqe_prev; \ - else \ - (elm)->field.cqe_next->field.cqe_prev = \ - (elm)->field.cqe_prev; \ - if ((elm)->field.cqe_prev == CIRCLEQ_ENDC(head)) \ - (head)->cqh_first = (elm)->field.cqe_next; \ - else \ - (elm)->field.cqe_prev->field.cqe_next = \ - (elm)->field.cqe_next; \ - QUEUEDEBUG_CIRCLEQ_POSTREMOVE((elm), field) \ -} while (/*CONSTCOND*/0) - -#define CIRCLEQ_FOREACH(var, head, field) \ - for ((var) = ((head)->cqh_first); \ - (var) != CIRCLEQ_ENDC(head); \ - (var) = ((var)->field.cqe_next)) - -#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ - for ((var) = ((head)->cqh_last); \ - (var) != CIRCLEQ_ENDC(head); \ - (var) = ((var)->field.cqe_prev)) +#define CIRCLEQ_INIT(head) \ + do { \ + (head)->cqh_first = CIRCLEQ_END(head); \ + (head)->cqh_last = CIRCLEQ_END(head); \ + } while (/*CONSTCOND*/ 0) + +#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) \ + do { \ + QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ + QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field) \ + (elm)->field.cqe_next = (listelm)->field.cqe_next; \ + (elm)->field.cqe_prev = (listelm); \ + if ((listelm)->field.cqe_next == CIRCLEQ_ENDC(head)) \ + (head)->cqh_last = (elm); \ + else \ + (listelm)->field.cqe_next->field.cqe_prev = (elm); \ + (listelm)->field.cqe_next = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) \ + do { \ + QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ + QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field) \ + (elm)->field.cqe_next = (listelm); \ + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ + if ((listelm)->field.cqe_prev == CIRCLEQ_ENDC(head)) \ + (head)->cqh_first = (elm); \ + else \ + (listelm)->field.cqe_prev->field.cqe_next = (elm); \ + (listelm)->field.cqe_prev = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define CIRCLEQ_INSERT_HEAD(head, elm, field) \ + do { \ + QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ + (elm)->field.cqe_next = (head)->cqh_first; \ + (elm)->field.cqe_prev = CIRCLEQ_END(head); \ + if ((head)->cqh_last == CIRCLEQ_ENDC(head)) \ + (head)->cqh_last = (elm); \ + else \ + (head)->cqh_first->field.cqe_prev = (elm); \ + (head)->cqh_first = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define CIRCLEQ_INSERT_TAIL(head, elm, field) \ + do { \ + QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ + (elm)->field.cqe_next = CIRCLEQ_END(head); \ + (elm)->field.cqe_prev = (head)->cqh_last; \ + if ((head)->cqh_first == CIRCLEQ_ENDC(head)) \ + (head)->cqh_first = (elm); \ + else \ + (head)->cqh_last->field.cqe_next = (elm); \ + (head)->cqh_last = (elm); \ + } while (/*CONSTCOND*/ 0) + +#define CIRCLEQ_REMOVE(head, elm, field) \ + do { \ + QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ + QUEUEDEBUG_CIRCLEQ_ELM((head), (elm), field) \ + if ((elm)->field.cqe_next == CIRCLEQ_ENDC(head)) \ + (head)->cqh_last = (elm)->field.cqe_prev; \ + else \ + (elm)->field.cqe_next->field.cqe_prev = \ + (elm)->field.cqe_prev; \ + if ((elm)->field.cqe_prev == CIRCLEQ_ENDC(head)) \ + (head)->cqh_first = (elm)->field.cqe_next; \ + else \ + (elm)->field.cqe_prev->field.cqe_next = \ + (elm)->field.cqe_next; \ + QUEUEDEBUG_CIRCLEQ_POSTREMOVE((elm), field) \ + } while (/*CONSTCOND*/ 0) + +#define CIRCLEQ_FOREACH(var, head, field) \ + for ((var) = ((head)->cqh_first); (var) != CIRCLEQ_ENDC(head); \ + (var) = ((var)->field.cqe_next)) + +#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ + for ((var) = ((head)->cqh_last); (var) != CIRCLEQ_ENDC(head); \ + (var) = ((var)->field.cqe_prev)) /* * Circular queue access methods. */ -#define CIRCLEQ_FIRST(head) ((head)->cqh_first) -#define CIRCLEQ_LAST(head) ((head)->cqh_last) +#define CIRCLEQ_FIRST(head) ((head)->cqh_first) +#define CIRCLEQ_LAST(head) ((head)->cqh_last) /* For comparisons */ -#define CIRCLEQ_ENDC(head) (__launder_type(head)) +#define CIRCLEQ_ENDC(head) (__launder_type(head)) /* For assignments */ -#define CIRCLEQ_END(head) ((void *)(head)) -#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) -#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) -#define CIRCLEQ_EMPTY(head) \ - (CIRCLEQ_FIRST(head) == CIRCLEQ_ENDC(head)) - -#define CIRCLEQ_LOOP_NEXT(head, elm, field) \ - (((elm)->field.cqe_next == CIRCLEQ_ENDC(head)) \ - ? ((head)->cqh_first) \ - : (elm->field.cqe_next)) -#define CIRCLEQ_LOOP_PREV(head, elm, field) \ - (((elm)->field.cqe_prev == CIRCLEQ_ENDC(head)) \ - ? ((head)->cqh_last) \ - : (elm->field.cqe_prev)) +#define CIRCLEQ_END(head) ((void *)(head)) +#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) +#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) +#define CIRCLEQ_EMPTY(head) (CIRCLEQ_FIRST(head) == CIRCLEQ_ENDC(head)) + +#define CIRCLEQ_LOOP_NEXT(head, elm, field) \ + (((elm)->field.cqe_next == CIRCLEQ_ENDC(head)) ? \ + ((head)->cqh_first) : \ + (elm->field.cqe_next)) +#define CIRCLEQ_LOOP_PREV(head, elm, field) \ + (((elm)->field.cqe_prev == CIRCLEQ_ENDC(head)) ? \ + ((head)->cqh_last) : \ + (elm->field.cqe_prev)) #endif /* !_KERNEL */ -#endif /* !_SYS_QUEUE_H_ */ +#endif /* !_SYS_QUEUE_H_ */ diff --git a/src/shared/schedules.c b/src/shared/schedules.c index b1eb85ade..409346177 100644 --- a/src/shared/schedules.c +++ b/src/shared/schedules.c @@ -16,10 +16,10 @@ */ /* nano seconds */ -#define POLL_INTERVAL 20000000 -#define WAIT_PIDFILE 500000000 -#define ONE_SECOND 1000000000 -#define ONE_MS 1000000 +#define POLL_INTERVAL 20000000 +#define WAIT_PIDFILE 500000000 +#define ONE_SECOND 1000000000 +#define ONE_MS 1000000 #include #include @@ -28,20 +28,20 @@ #include #include #include -#include -#include -#include #include #include #include #include +#include +#include +#include #include "einfo.h" +#include "helpers.h" +#include "misc.h" #include "queue.h" #include "rc.h" -#include "misc.h" #include "schedules.h" -#include "helpers.h" typedef struct scheduleitem { enum { @@ -72,52 +72,39 @@ void free_schedulelist(void) int parse_signal(const char *applet, const char *sig) { - typedef struct signalpair - { + typedef struct signalpair { const char *name; int signal; } SIGNALPAIR; -#define signalpair_item(name) { #name, SIG##name }, +#define signalpair_item(name) {#name, SIG##name}, static const SIGNALPAIR signallist[] = { - signalpair_item(HUP) - signalpair_item(INT) - signalpair_item(QUIT) - signalpair_item(ILL) - signalpair_item(TRAP) - signalpair_item(ABRT) - signalpair_item(BUS) - signalpair_item(FPE) - signalpair_item(KILL) - signalpair_item(USR1) - signalpair_item(SEGV) - signalpair_item(USR2) - signalpair_item(PIPE) - signalpair_item(ALRM) - signalpair_item(TERM) - signalpair_item(CHLD) - signalpair_item(CONT) - signalpair_item(STOP) - signalpair_item(TSTP) - signalpair_item(TTIN) - signalpair_item(TTOU) - signalpair_item(URG) - signalpair_item(XCPU) - signalpair_item(XFSZ) - signalpair_item(VTALRM) - signalpair_item(PROF) + signalpair_item(HUP) signalpair_item(INT) signalpair_item(QUIT) signalpair_item( + ILL) signalpair_item(TRAP) signalpair_item(ABRT) signalpair_item(BUS) + signalpair_item(FPE) signalpair_item(KILL) signalpair_item(USR1) signalpair_item( + SEGV) signalpair_item(USR2) signalpair_item(PIPE) + signalpair_item(ALRM) signalpair_item(TERM) signalpair_item( + CHLD) signalpair_item(CONT) signalpair_item(STOP) + signalpair_item(TSTP) signalpair_item(TTIN) signalpair_item( + TTOU) signalpair_item(URG) signalpair_item(XCPU) + signalpair_item(XFSZ) signalpair_item( + VTALRM) signalpair_item(PROF) #ifdef SIGWINCH - signalpair_item(WINCH) + signalpair_item(WINCH) #endif #ifdef SIGIO - signalpair_item(IO) + signalpair_item( + IO) #endif #ifdef SIGPWR - signalpair_item(PWR) + signalpair_item( + PWR) #endif - signalpair_item(SYS) - { "NULL", 0 }, + signalpair_item( + SYS){ + "NULL", + 0}, }; unsigned int i = 0; @@ -154,17 +141,16 @@ static SCHEDULEITEM *parse_schedule_item(const char *applet, const char *string) item->value = 0; item->gotoitem = NULL; - if (strcmp(string,"forever") == 0) + if (strcmp(string, "forever") == 0) item->type = SC_FOREVER; else if (isdigit((unsigned char)string[0])) { item->type = SC_TIMEOUT; errno = 0; if (sscanf(string, "%d", &item->value) != 1) eerrorx("%s: invalid timeout value in schedule `%s'", - applet, string); + applet, string); } else if ((after_hyph = string + (string[0] == '-')) && - ((sig = parse_signal(applet, after_hyph)) != -1)) - { + ((sig = parse_signal(applet, after_hyph)) != -1)) { item->type = SC_SIGNAL; item->value = (int)sig; } else @@ -204,7 +190,7 @@ void parse_schedule(const char *applet, const char *string, int timeout) if (string) { if (sscanf(string, "%d", &item->value) != 1) eerrorx("%s: invalid timeout in schedule", - applet); + applet); } else item->value = 5; @@ -219,7 +205,7 @@ void parse_schedule(const char *applet, const char *string, int timeout) if (len >= (ptrdiff_t)sizeof(buffer)) eerrorx("%s: invalid schedule item, far too long", - applet); + applet); memcpy(buffer, string, len); buffer[len] = 0; @@ -230,7 +216,8 @@ void parse_schedule(const char *applet, const char *string, int timeout) if (item->type == SC_FOREVER) { if (repeatat) eerrorx("%s: invalid schedule, `forever' " - "appears more than once", applet); + "appears more than once", + applet); repeatat = item; continue; @@ -250,7 +237,7 @@ void parse_schedule(const char *applet, const char *string, int timeout) /* return number of processes killed, -1 on error */ int do_stop(const char *applet, const char *exec, const char *const *argv, - pid_t pid, uid_t uid,int sig, bool test, bool quiet) + pid_t pid, uid_t uid, int sig, bool test, bool quiet) { RC_PIDLIST *pids; RC_PID *pi; @@ -272,20 +259,24 @@ int do_stop(const char *applet, const char *exec, const char *const *argv, nkilled++; } else { if (sig) { - syslog(LOG_DEBUG, "Sending signal %d to PID %d", sig, pi->pid); + syslog(LOG_DEBUG, "Sending signal %d to PID %d", + sig, pi->pid); if (!quiet) - ebeginv("Sending signal %d to PID %d", sig, pi->pid); + ebeginv("Sending signal %d to PID %d", + sig, pi->pid); } errno = 0; - killed = (kill(pi->pid, sig) == 0 || - errno == ESRCH ? true : false); + killed = (kill(pi->pid, sig) == 0 || errno == ESRCH ? + true : + false); if (!quiet) eendv(killed ? 0 : 1, - "%s: failed to send signal %d to PID %d: %s", - applet, sig, pi->pid, strerror(errno)); + "%s: failed to send signal %d to PID %d: %s", + applet, sig, pi->pid, strerror(errno)); else if (!killed) - syslog(LOG_ERR, "Failed to send signal %d to PID %d: %s", - sig, pi->pid, strerror(errno)); + syslog(LOG_ERR, + "Failed to send signal %d to PID %d: %s", + sig, pi->pid, strerror(errno)); if (!killed) { nkilled = -1; } else { @@ -300,10 +291,9 @@ int do_stop(const char *applet, const char *exec, const char *const *argv, return nkilled; } -int run_stop_schedule(const char *applet, - const char *exec, const char *const *argv, - pid_t pid, uid_t uid, - bool test, bool progress, bool quiet) +int run_stop_schedule(const char *applet, const char *exec, + const char *const *argv, pid_t pid, uid_t uid, bool test, + bool progress, bool quiet) { SCHEDULEITEM *item = TAILQ_FIRST(&schedule); int nkilled = 0; @@ -349,17 +339,17 @@ int run_stop_schedule(const char *applet, case SC_SIGNAL: nrunning = 0; - nkilled = do_stop(applet, exec, argv, pid, uid, item->value, test, - quiet); + nkilled = do_stop(applet, exec, argv, pid, uid, + item->value, test, quiet); if (nkilled == 0) { if (tkilled == 0) { if (progressed) printf("\n"); - eerror("%s: no matching processes found", applet); + eerror("%s: no matching processes found", + applet); } return tkilled; - } - else if (nkilled == -1) + } else if (nkilled == -1) return 0; tkilled += nkilled; @@ -374,16 +364,17 @@ int run_stop_schedule(const char *applet, ts.tv_sec = 0; ts.tv_nsec = POLL_INTERVAL; - for (nsecs = 0; item->type == SC_FOREVER || nsecs < item->value; nsecs++) { + for (nsecs = 0; + item->type == SC_FOREVER || nsecs < item->value; + nsecs++) { for (nloops = 0; nloops < ONE_SECOND / POLL_INTERVAL; - nloops++) - { - if ((nrunning = do_stop(applet, exec, argv, - pid, uid, 0, test, quiet)) == 0) + nloops++) { + if ((nrunning = do_stop( + applet, exec, argv, pid, + uid, 0, test, quiet)) == 0) return 0; - if (nanosleep(&ts, NULL) == -1) { if (progressed) { printf("\n"); @@ -391,7 +382,8 @@ int run_stop_schedule(const char *applet, } if (errno != EINTR) { eerror("%s: nanosleep: %s", - applet, strerror(errno)); + applet, + strerror(errno)); return 0; } } @@ -408,8 +400,8 @@ int run_stop_schedule(const char *applet, printf("\n"); progressed = false; } - eerror("%s: invalid schedule item `%d'", - applet, item->type); + eerror("%s: invalid schedule item `%d'", applet, + item->type); return 0; } @@ -424,9 +416,11 @@ int run_stop_schedule(const char *applet, printf("\n"); 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; diff --git a/src/shared/schedules.h b/src/shared/schedules.h index 5fb9e5265..312753659 100644 --- a/src/shared/schedules.h +++ b/src/shared/schedules.h @@ -17,10 +17,9 @@ void free_schedulelist(void); int parse_signal(const char *applet, const char *sig); void parse_schedule(const char *applet, const char *string, int timeout); int do_stop(const char *applet, const char *exec, const char *const *argv, - pid_t pid, uid_t uid,int sig, bool test, bool quiet); -int run_stop_schedule(const char *applet, - const char *exec, const char *const *argv, - pid_t pid, uid_t uid, - bool test, bool progress, bool quiet); + pid_t pid, uid_t uid, int sig, bool test, bool quiet); +int run_stop_schedule(const char *applet, const char *exec, + const char *const *argv, pid_t pid, uid_t uid, bool test, + bool progress, bool quiet); #endif diff --git a/src/shared/selinux.c b/src/shared/selinux.c index 213a00f13..365cbd0d2 100644 --- a/src/shared/selinux.c +++ b/src/shared/selinux.c @@ -15,27 +15,27 @@ * except according to the terms contained in the LICENSE file. */ -#include -#include -#include #include +#include +#include #include #include +#include #include -#include -#include -#include #include +#include +#include +#include #include #include #include "einfo.h" -#include "queue.h" -#include "rc.h" #include "misc.h" #include "plugin.h" +#include "queue.h" +#include "rc.h" #include "selinux.h" /* the context files for selinux */ @@ -47,17 +47,16 @@ /* PAM or shadow for authentication */ #ifdef HAVE_PAM -# define PAM_SERVICE_NAME "run_init" /* the name of this program for PAM */ -# include -# include +#define PAM_SERVICE_NAME "run_init" /* the name of this program for PAM */ +#include +#include #else -# define PASSWORD_PROMPT "Password:" -# include -# include -# include +#define PASSWORD_PROMPT "Password:" +#include +#include +#include #endif - /* The handle for the fcontext lookups */ static struct selabel_handle *hnd = NULL; @@ -155,10 +154,7 @@ static int check_password(char *username) #ifdef HAVE_PAM pam_handle_t *pamh; int pam_err = 0; - const struct pam_conv pconv = { - misc_conv, - NULL - }; + const struct pam_conv pconv = {misc_conv, NULL}; pam_err = pam_start(PAM_SERVICE_NAME, username, &pconv, &pamh); if (pam_err != PAM_SUCCESS) { @@ -226,7 +222,7 @@ static int check_auth() #ifdef HAVE_AUDIT uid = audit_getloginuid(); - if (uid == (uid_t) -1) + if (uid == (uid_t)-1) uid = getuid(); #else uid = getuid(); @@ -391,7 +387,8 @@ void selinux_setup(char **argv) /* Set the new context */ if (setexeccon(new_context) < 0) { - eerrorx("Could not set SELinux exec context to %s.", new_context); + eerrorx("Could not set SELinux exec context to %s.", + new_context); } free(new_context); diff --git a/src/shared/selinux.h b/src/shared/selinux.h index 627c87bcd..366ddad5c 100644 --- a/src/shared/selinux.h +++ b/src/shared/selinux.h @@ -25,12 +25,17 @@ void selinux_setup(char **argv); /* always return false for selinux_util_open() */ #define selinux_util_open() (0) -#define selinux_util_label(x) do { } while (0) -#define selinux_util_close() do { } while (0) +#define selinux_util_label(x) \ + do { \ + } while (0) +#define selinux_util_close() \ + do { \ + } while (0) -#define selinux_setup(x) do { } while (0) +#define selinux_setup(x) \ + do { \ + } while (0) #endif - #endif diff --git a/src/shared/wtmp.c b/src/shared/wtmp.c index 5881aebab..dcd65e2d4 100644 --- a/src/shared/wtmp.c +++ b/src/shared/wtmp.c @@ -19,15 +19,15 @@ #include #include #include -#include #include #include #include +#include #include "wtmp.h" void log_wtmp(const char *user, const char *id, pid_t pid, int type, - const char *line) + const char *line) { struct timeval tv; struct utmp utmp; @@ -37,10 +37,10 @@ void log_wtmp(const char *user, const char *id, pid_t pid, int type, gettimeofday(&tv, NULL); utmp.ut_tv.tv_sec = tv.tv_sec; utmp.ut_tv.tv_usec = tv.tv_usec; - utmp.ut_pid = pid; + utmp.ut_pid = pid; utmp.ut_type = type; strncpy(utmp.ut_name, user, sizeof(utmp.ut_name)); - strncpy(utmp.ut_id , id , sizeof(utmp.ut_id )); + strncpy(utmp.ut_id, id, sizeof(utmp.ut_id)); strncpy(utmp.ut_line, line, sizeof(utmp.ut_line)); /* Put the OS version in place of the hostname */ diff --git a/src/shared/wtmp.h b/src/shared/wtmp.h index edab322c4..073535d08 100644 --- a/src/shared/wtmp.h +++ b/src/shared/wtmp.h @@ -21,6 +21,6 @@ #include void log_wtmp(const char *user, const char *id, pid_t pid, int type, - const char *line); + const char *line); #endif diff --git a/src/shell_var/shell_var.c b/src/shell_var/shell_var.c index 707502450..19ac67054 100644 --- a/src/shell_var/shell_var.c +++ b/src/shell_var/shell_var.c @@ -10,8 +10,8 @@ * except according to the terms contained in the LICENSE file. */ -#include #include +#include #include #include #include diff --git a/src/start-stop-daemon/pipes.c b/src/start-stop-daemon/pipes.c index 506196d53..5251a4f0a 100644 --- a/src/start-stop-daemon/pipes.c +++ b/src/start-stop-daemon/pipes.c @@ -16,8 +16,8 @@ */ #include -#include #include +#include #include "pipes.h" diff --git a/src/start-stop-daemon/start-stop-daemon.c b/src/start-stop-daemon/start-stop-daemon.c index 9117f92f3..9678bf24b 100644 --- a/src/start-stop-daemon/start-stop-daemon.c +++ b/src/start-stop-daemon/start-stop-daemon.c @@ -19,32 +19,32 @@ * except according to the terms contained in the LICENSE file. */ -#define ONE_MS 1000000 +#define ONE_MS 1000000 #ifdef __linux__ /* For extra SCHED_* defines. */ -# define _GNU_SOURCE +#define _GNU_SOURCE #endif -#include #include #include #include -#include #include +#include #include +#include #ifdef __linux__ -#include /* For io priority */ #include /* For prctl */ +#include /* For io priority */ #endif #include #include #include #include -#include #include +#include #include #include #include @@ -58,7 +58,7 @@ #include /* We are not supporting authentication conversations */ -static struct pam_conv conv = { NULL, NULL}; +static struct pam_conv conv = {NULL, NULL}; #endif #ifdef HAVE_CAP @@ -67,73 +67,72 @@ static struct pam_conv conv = { NULL, NULL}; #include +#include "_usage.h" #include "einfo.h" -#include "queue.h" -#include "rc.h" +#include "helpers.h" #include "misc.h" #include "pipes.h" +#include "queue.h" +#include "rc.h" #include "schedules.h" -#include "_usage.h" -#include "helpers.h" /* Use long option value that is out of range for 8 bit getopt values. * The exact enum value is internal and can freely change, so we keep the * options sorted. */ enum { - /* This has to come first so following values stay in the 0x100+ range. */ - LONGOPT_BASE = 0x100, - - LONGOPT_CAPABILITIES, - LONGOPT_OOM_SCORE_ADJ, - LONGOPT_NO_NEW_PRIVS, - LONGOPT_SCHEDULER, - LONGOPT_SCHEDULER_PRIO, - LONGOPT_SECBITS, + /* This has to come first so following values stay in the 0x100+ range. */ + LONGOPT_BASE = 0x100, + + LONGOPT_CAPABILITIES, + LONGOPT_OOM_SCORE_ADJ, + LONGOPT_NO_NEW_PRIVS, + LONGOPT_SCHEDULER, + LONGOPT_SCHEDULER_PRIO, + LONGOPT_SECBITS, }; const char *applet = NULL; const char *extraopts = NULL; -const char getoptstring[] = "I:KN:PR:Sa:bc:d:e:g:ik:mn:op:s:tu:r:w:x:1:2:3:4:" \ - getoptstring_COMMON; +const char getoptstring[] = + "I:KN:PR:Sa:bc:d:e:g:ik:mn:op:s:tu:r:w:x:1:2:3:4:" getoptstring_COMMON; const struct option longopts[] = { - { "capabilities", 1, NULL, LONGOPT_CAPABILITIES}, - { "secbits", 1, NULL, LONGOPT_SECBITS}, - { "no-new-privs", 0, NULL, LONGOPT_NO_NEW_PRIVS}, - { "ionice", 1, NULL, 'I'}, - { "stop", 0, NULL, 'K'}, - { "nicelevel", 1, NULL, 'N'}, - { "oom-score-adj",1, NULL, LONGOPT_OOM_SCORE_ADJ}, - { "retry", 1, NULL, 'R'}, - { "start", 0, NULL, 'S'}, - { "startas", 1, NULL, 'a'}, - { "background", 0, NULL, 'b'}, - { "chuid", 1, NULL, 'c'}, - { "chdir", 1, NULL, 'd'}, - { "env", 1, NULL, 'e'}, - { "umask", 1, NULL, 'k'}, - { "group", 1, NULL, 'g'}, - { "interpreted", 0, NULL, 'i'}, - { "make-pidfile", 0, NULL, 'm'}, - { "name", 1, NULL, 'n'}, - { "oknodo", 0, NULL, 'o'}, - { "pidfile", 1, NULL, 'p'}, - { "signal", 1, NULL, 's'}, - { "test", 0, NULL, 't'}, - { "user", 1, NULL, 'u'}, - { "chroot", 1, NULL, 'r'}, - { "wait", 1, NULL, 'w'}, - { "exec", 1, NULL, 'x'}, - { "stdout", 1, NULL, '1'}, - { "stderr", 1, NULL, '2'}, - { "stdout-logger",1, NULL, '3'}, - { "stderr-logger",1, NULL, '4'}, - { "progress", 0, NULL, 'P'}, - { "scheduler", 1, NULL, LONGOPT_SCHEDULER}, - { "scheduler-priority", 1, NULL, LONGOPT_SCHEDULER_PRIO}, - longopts_COMMON -}; -const char * const longopts_help[] = { + {"capabilities", 1, NULL, LONGOPT_CAPABILITIES}, + {"secbits", 1, NULL, LONGOPT_SECBITS}, + {"no-new-privs", 0, NULL, LONGOPT_NO_NEW_PRIVS}, + {"ionice", 1, NULL, 'I'}, + {"stop", 0, NULL, 'K'}, + {"nicelevel", 1, NULL, 'N'}, + {"oom-score-adj", 1, NULL, LONGOPT_OOM_SCORE_ADJ}, + {"retry", 1, NULL, 'R'}, + {"start", 0, NULL, 'S'}, + {"startas", 1, NULL, 'a'}, + {"background", 0, NULL, 'b'}, + {"chuid", 1, NULL, 'c'}, + {"chdir", 1, NULL, 'd'}, + {"env", 1, NULL, 'e'}, + {"umask", 1, NULL, 'k'}, + {"group", 1, NULL, 'g'}, + {"interpreted", 0, NULL, 'i'}, + {"make-pidfile", 0, NULL, 'm'}, + {"name", 1, NULL, 'n'}, + {"oknodo", 0, NULL, 'o'}, + {"pidfile", 1, NULL, 'p'}, + {"signal", 1, NULL, 's'}, + {"test", 0, NULL, 't'}, + {"user", 1, NULL, 'u'}, + {"chroot", 1, NULL, 'r'}, + {"wait", 1, NULL, 'w'}, + {"exec", 1, NULL, 'x'}, + {"stdout", 1, NULL, '1'}, + {"stderr", 1, NULL, '2'}, + {"stdout-logger", 1, NULL, '3'}, + {"stderr-logger", 1, NULL, '4'}, + {"progress", 0, NULL, 'P'}, + {"scheduler", 1, NULL, LONGOPT_SCHEDULER}, + {"scheduler-priority", 1, NULL, LONGOPT_SCHEDULER_PRIO}, + longopts_COMMON}; +const char *const longopts_help[] = { "Set the inheritable, ambient and bounding capabilities", "Set the security-bits for the program", "Set the No New Privs flag for the program", @@ -168,8 +167,7 @@ const char * const longopts_help[] = { "Print dots each second while waiting", "Set process scheduler", "Set process scheduler priority", - longopts_help_COMMON -}; + longopts_help_COMMON}; const char *usagestring = NULL; static char **nav; @@ -179,11 +177,10 @@ static char *changeuser, *ch_root, *ch_dir; extern char **environ; #if !defined(SYS_ioprio_set) && defined(__NR_ioprio_set) -# define SYS_ioprio_set __NR_ioprio_set +#define SYS_ioprio_set __NR_ioprio_set #endif #if !defined(__DragonFly__) -static inline int ioprio_set(int which _unused, - int who _unused, +static inline int ioprio_set(int which _unused, int who _unused, int ioprio _unused) { #ifdef SYS_ioprio_set @@ -194,16 +191,14 @@ static inline int ioprio_set(int which _unused, } #endif -static void -cleanup(void) +static void cleanup(void) { free(changeuser); free(nav); free_schedulelist(); } -static void -handle_signal(int sig) +static void handle_signal(int sig) { int status; int serrno = errno; @@ -228,8 +223,8 @@ 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; } } @@ -246,8 +241,7 @@ handle_signal(int sig) errno = serrno; } -static char * -expand_home(const char *home, const char *path) +static char *expand_home(const char *home, const char *path) { char *opath, *ppath, *p, *nh; struct passwd *pw; @@ -272,7 +266,7 @@ expand_home(const char *home, const char *path) ppath++; if (!home) { - free(opath); + free(opath); return xstrdup(path); } if (!ppath) { @@ -365,12 +359,12 @@ int main(int argc, char **argv) if ((tmp = getenv("SSD_NICELEVEL"))) if (sscanf(tmp, "%d", &nicelevel) != 1) eerror("%s: invalid nice level `%s' (SSD_NICELEVEL)", - applet, tmp); + applet, tmp); if ((tmp = getenv("SSD_IONICELEVEL"))) { int n = sscanf(tmp, "%d:%d", &ionicec, &ioniced); if (n != 1 && n != 2) eerror("%s: invalid ionice level `%s' (SSD_IONICELEVEL)", - applet, tmp); + applet, tmp); if (ionicec == 0) ioniced = 0; else if (ionicec == 3) @@ -380,7 +374,7 @@ int main(int argc, char **argv) if ((tmp = getenv("SSD_OOM_SCORE_ADJ"))) if (sscanf(tmp, "%d", &oom_score_adj) != 1) eerror("%s: invalid oom_score_adj `%s' (SSD_OOM_SCORE_ADJ)", - applet, tmp); + applet, tmp); /* Get our user name and initial dir */ p = getenv("USER"); @@ -398,13 +392,14 @@ int main(int argc, char **argv) } while ((opt = getopt_long(argc, argv, getoptstring, longopts, - (int *) 0)) != -1) + (int *)0)) != -1) switch (opt) { case LONGOPT_CAPABILITIES: #ifdef HAVE_CAP cap_iab = cap_iab_from_text(optarg); if (cap_iab == NULL) - eerrorx("Could not parse iab: %s", strerror(errno)); + eerrorx("Could not parse iab: %s", + strerror(errno)); #else eerrorx("Capabilities support not enabled"); #endif @@ -418,7 +413,8 @@ int main(int argc, char **argv) tmp = NULL; secbits = strtoul(optarg, &tmp, 0); if (*tmp != '\0') - eerrorx("Could not parse secbits: invalid char %c", *tmp); + eerrorx("Could not parse secbits: invalid char %c", + *tmp); #else eerrorx("Capabilities support not enabled"); #endif @@ -434,8 +430,8 @@ int main(int argc, char **argv) case 'I': /* --ionice */ if (sscanf(optarg, "%d:%d", &ionicec, &ioniced) == 0) - eerrorx("%s: invalid ionice `%s'", - applet, optarg); + eerrorx("%s: invalid ionice `%s'", applet, + optarg); if (ionicec == 0) ioniced = 0; else if (ionicec == 3) @@ -443,43 +439,43 @@ int main(int argc, char **argv) ionicec <<= 13; /* class shift */ break; - case 'K': /* --stop */ + case 'K': /* --stop */ stop = true; break; - case 'N': /* --nice */ + case 'N': /* --nice */ if (sscanf(optarg, "%d", &nicelevel) != 1) - eerrorx("%s: invalid nice level `%s'", - applet, optarg); + eerrorx("%s: invalid nice level `%s'", applet, + optarg); break; case LONGOPT_OOM_SCORE_ADJ: /* --oom-score-adj */ if (sscanf(optarg, "%d", &oom_score_adj) != 1) eerrorx("%s: invalid oom-score-adj `%s'", - applet, optarg); + applet, optarg); break; - case 'P': /* --progress */ + case 'P': /* --progress */ progress = true; break; - case 'R': /* --retry | */ + case 'R': /* --retry | */ retry = optarg; break; - case 'S': /* --start */ + case 'S': /* --start */ start = true; break; - case 'b': /* --background */ + case 'b': /* --background */ background = true; break; - case 'c': /* --chuid | */ + case 'c': /* --chuid | */ /* DEPRECATED */ ewarn("WARNING: -c/--chuid is deprecated and will be removed in the future, please use -u/--user instead"); /* falls through */ - case 'u': /* --user | */ + case 'u': /* --user | */ { char dummy[2]; p = optarg; @@ -491,8 +487,7 @@ int main(int argc, char **argv) pw = getpwuid((uid_t)tid); if (pw == NULL) - eerrorx("%s: user `%s' not found", - applet, tmp); + eerrorx("%s: user `%s' not found", applet, tmp); uid = pw->pw_uid; home = pw->pw_dir; unsetenv("HOME"); @@ -505,22 +500,21 @@ int main(int argc, char **argv) gid = pw->pw_gid; if (p) { - tmp = strsep (&p, ":"); + tmp = strsep(&p, ":"); if (sscanf(tmp, "%d%1s", &tid, dummy) != 1) gr = getgrnam(tmp); else - gr = getgrgid((gid_t) tid); + gr = getgrgid((gid_t)tid); if (gr == NULL) eerrorx("%s: group `%s'" - " not found", - applet, tmp); + " not found", + applet, tmp); gid = gr->gr_gid; } - } - break; + } break; - case 'd': /* --chdir /new/dir */ + case 'd': /* --chdir /new/dir */ ch_dir = optarg; break; @@ -528,14 +522,14 @@ int main(int argc, char **argv) putenv(optarg); break; - case 'g': /* --group | */ + case 'g': /* --group | */ if (sscanf(optarg, "%d", &tid) != 1) gr = getgrnam(optarg); else gr = getgrgid((gid_t)tid); if (gr == NULL) - eerrorx("%s: group `%s' not found", - applet, optarg); + eerrorx("%s: group `%s' not found", applet, + optarg); gid = gr->gr_gid; break; @@ -545,37 +539,37 @@ int main(int argc, char **argv) case 'k': if (parse_mode(&numask, optarg)) - eerrorx("%s: invalid mode `%s'", - applet, optarg); + eerrorx("%s: invalid mode `%s'", applet, + optarg); break; - case 'm': /* --make-pidfile */ + case 'm': /* --make-pidfile */ makepidfile = true; break; - case 'n': /* --name */ + case 'n': /* --name */ name = optarg; break; - case 'o': /* --oknodo */ + case 'o': /* --oknodo */ /* DEPRECATED */ ewarn("WARNING: -o/--oknodo is deprecated and will be removed in the future"); oknodo = true; break; - case 'p': /* --pidfile */ + case 'p': /* --pidfile */ pidfile = optarg; break; - case 's': /* --signal */ + case 's': /* --signal */ sig = parse_signal(applet, optarg); break; - case 't': /* --test */ + case 't': /* --test */ test = true; break; - case 'r': /* --chroot /new/root */ + case 'r': /* --chroot /new/root */ ch_root = optarg; break; @@ -586,26 +580,26 @@ int main(int argc, char **argv) break; case 'w': if (sscanf(optarg, "%u", &start_wait) != 1) - eerrorx("%s: `%s' not a number", - applet, optarg); + eerrorx("%s: `%s' not a number", applet, + optarg); break; - case 'x': /* --exec */ + case 'x': /* --exec */ exec = optarg; break; - case '1': /* --stdout /path/to/stdout.lgfile */ + case '1': /* --stdout /path/to/stdout.lgfile */ redirect_stdout = optarg; break; - case '2': /* --stderr /path/to/stderr.logfile */ + case '2': /* --stderr /path/to/stderr.logfile */ redirect_stderr = optarg; break; - case '3': /* --stdout-logger "command to run for stdout logging" */ + case '3': /* --stdout-logger "command to run for stdout logging" */ stdout_process = optarg; break; - case '4': /* --stderr-logger "command to run for stderr logging" */ + case '4': /* --stderr-logger "command to run for stderr logging" */ stderr_process = optarg; break; @@ -617,7 +611,7 @@ int main(int argc, char **argv) sscanf(optarg, "%d", &sched_prio); break; - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } endpwent(); @@ -626,12 +620,8 @@ int main(int argc, char **argv) /* Allow start-stop-daemon --signal HUP --exec /usr/sbin/dnsmasq * instead of forcing --stop --oknodo as well */ - if (!start && - !stop && - sig != SIGINT && - sig != SIGTERM && - sig != SIGQUIT && - sig != SIGKILL) + if (!start && !stop && sig != SIGINT && sig != SIGTERM && + sig != SIGQUIT && sig != SIGKILL) oknodo = true; if (!exec) @@ -658,40 +648,48 @@ int main(int argc, char **argv) sig = SIGTERM; if (!*argv && !pidfile && !name && !uid) eerrorx("%s: --stop needs --exec, --pidfile," - " --name or --user", applet); + " --name or --user", + applet); if (background) eerrorx("%s: --background is only relevant with" - " --start", applet); + " --start", + applet); if (makepidfile) eerrorx("%s: --make-pidfile is only relevant with" - " --start", applet); + " --start", + applet); if (redirect_stdout || redirect_stderr) eerrorx("%s: --stdout and --stderr are only relevant" - " with --start", applet); + " with --start", + applet); if (stdout_process || stderr_process) eerrorx("%s: --stdout-logger and --stderr-logger are only relevant" - " with --start", applet); + " with --start", + applet); if (start_wait) ewarn("using --wait with --stop has no effect," - " use --retry instead"); + " use --retry instead"); } else { if (!exec) eerrorx("%s: nothing to start", applet); if (makepidfile && !pidfile) eerrorx("%s: --make-pidfile is only relevant with" - " --pidfile", applet); + " --pidfile", + applet); if ((redirect_stdout || redirect_stderr) && !background) eerrorx("%s: --stdout and --stderr are only relevant" - " with --background", applet); + " with --background", + applet); if ((stdout_process || stderr_process) && !background) eerrorx("%s: --stdout-logger and --stderr-logger are only relevant" - " with --background", applet); + " with --background", + applet); if (redirect_stdout && stdout_process) eerrorx("%s: do not use --stdout and --stdout-logger together", - applet); + applet); if (redirect_stderr && stderr_process) eerrorx("%s: do not use --stderr and --stderr-logger together", - applet); + applet); } /* Expand ~ */ @@ -716,9 +714,11 @@ int main(int argc, char **argv) exec_file = NULL; while ((token = strsep(&p, ":"))) { if (ch_root) - xasprintf(&exec_file, "%s/%s/%s", ch_root, token, exec); + xasprintf(&exec_file, "%s/%s/%s", + ch_root, token, exec); else - xasprintf(&exec_file, "%s/%s", token, exec); + xasprintf(&exec_file, "%s/%s", token, + exec); if (exec_file && exists(exec_file)) break; free(exec_file); @@ -729,13 +729,13 @@ int main(int argc, char **argv) } if (start && !exists(exec_file)) { eerror("%s: %s does not exist", applet, - exec_file ? exec_file : exec); + exec_file ? exec_file : exec); free(exec_file); exit(EXIT_FAILURE); } if (start && retry) ewarn("using --retry with --start has no effect," - " use --wait instead"); + " use --wait instead"); /* If we don't have a pidfile we should check if it's interpreted * or not. If it we, we need to pass the interpreter through @@ -795,7 +795,7 @@ int main(int argc, char **argv) pid = 0; } i = run_stop_schedule(applet, exec, (const char *const *)margv, - pid, uid, test, progress, false); + pid, uid, test, progress, false); if (i < 0) /* We failed to stop something */ @@ -811,8 +811,8 @@ int main(int argc, char **argv) unlink(pidfile); if (svcname) rc_service_daemon_set(svcname, exec, - (const char *const *)argv, - pidfile, false); + (const char *const *)argv, + pidfile, false); exit(EXIT_SUCCESS); } @@ -824,14 +824,14 @@ int main(int argc, char **argv) if (pid) pids = rc_find_pids(NULL, NULL, 0, pid); else - pids = rc_find_pids(exec, (const char * const *) argv, uid, 0); + pids = rc_find_pids(exec, (const char *const *)argv, uid, 0); if (pids) eerrorx("%s: %s is already running", applet, exec); free(pids); if (test) { if (rc_yesno(getenv("EINFO_QUIET"))) - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); einfon("Would start"); while (argc-- > 0) @@ -849,7 +849,7 @@ int main(int argc, char **argv) if (nicelevel != 0) einfo("with a priority of %d", nicelevel); if (name) - einfo ("with a process name of %s", name); + einfo("with a process name of %s", name); eoutdent(); exit(EXIT_SUCCESS); } @@ -880,70 +880,67 @@ int main(int argc, char **argv) if (nicelevel != INT_MIN) { if (setpriority(PRIO_PROCESS, mypid, nicelevel) == -1) - eerrorx("%s: setpriority %d: %s", - applet, nicelevel, - strerror(errno)); + eerrorx("%s: setpriority %d: %s", applet, + nicelevel, strerror(errno)); } if (ionicec != -1 && ioprio_set(1, mypid, ionicec | ioniced) == -1) - eerrorx("%s: ioprio_set %d %d: %s", applet, - ionicec, ioniced, strerror(errno)); + eerrorx("%s: ioprio_set %d %d: %s", applet, ionicec, + ioniced, strerror(errno)); if (oom_score_adj != INT_MIN) { fp = fopen("/proc/self/oom_score_adj", "w"); if (!fp) eerrorx("%s: oom_score_adj %d: %s", applet, - oom_score_adj, strerror(errno)); + oom_score_adj, strerror(errno)); fprintf(fp, "%d\n", oom_score_adj); fclose(fp); } 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"); if (!fp) eerrorx("%s: fopen `%s': %s", applet, pidfile, - strerror(errno)); + strerror(errno)); fprintf(fp, "%d\n", mypid); fclose(fp); } #ifdef HAVE_PAM if (changeuser != NULL) { - pamr = pam_start("start-stop-daemon", - changeuser, &conv, &pamh); + pamr = pam_start("start-stop-daemon", changeuser, &conv, + &pamh); if (pamr == PAM_SUCCESS) pamr = pam_acct_mgmt(pamh, PAM_SILENT); 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); #ifdef HAVE_CAP if (uid && cap_setuid(uid)) #else if (uid && setuid(uid)) #endif - 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(); @@ -953,26 +950,29 @@ int main(int argc, char **argv) i = cap_iab_set_proc(cap_iab); if (cap_free(cap_iab) != 0) - eerrorx("Could not releasable memory: %s", strerror(errno)); + eerrorx("Could not releasable memory: %s", + strerror(errno)); if (i != 0) - eerrorx("Could not set iab: %s", strerror(errno)); + eerrorx("Could not set iab: %s", + strerror(errno)); } if (secbits != 0) { if (cap_set_secbits(secbits) < 0) - eerrorx("Could not set securebits to 0x%x: %s", secbits, strerror(errno)); + eerrorx("Could not set securebits to 0x%x: %s", + secbits, strerror(errno)); } #endif #ifdef PR_SET_NO_NEW_PRIVS if (no_new_privs) { if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) - eerrorx("Could not set No New Privs flag: %s", strerror(errno)); + eerrorx("Could not set No New Privs flag: %s", + strerror(errno)); } #endif - #ifdef TIOCNOTTY ioctl(tty_fd, TIOCNOTTY, 0); close(tty_fd); @@ -1002,12 +1002,12 @@ int main(int argc, char **argv) TAILQ_FOREACH(env, env_list, entries) { if ((strncmp(env->value, "RC_", 3) == 0 && - strncmp(env->value, "RC_SERVICE=", 11) != 0 && - strncmp(env->value, "RC_SVCNAME=", 11) != 0) || - strncmp(env->value, "SSD_NICELEVEL=", 14) == 0 || - strncmp(env->value, "SSD_IONICELEVEL=", 16) == 0 || - strncmp(env->value, "SSD_OOM_SCORE_ADJ=", 18) == 0) - { + strncmp(env->value, "RC_SERVICE=", 11) != 0 && + strncmp(env->value, "RC_SVCNAME=", 11) != 0) || + strncmp(env->value, "SSD_NICELEVEL=", 14) == 0 || + strncmp(env->value, "SSD_IONICELEVEL=", 16) == 0 || + strncmp(env->value, "SSD_OOM_SCORE_ADJ=", 18) == + 0) { p = strchr(env->value, '='); *p = '\0'; unsetenv(env->value); @@ -1028,8 +1028,7 @@ int main(int argc, char **argv) p++; } if (strcmp(token, RC_LIBEXECDIR "/bin") != 0 && - strcmp(token, RC_LIBEXECDIR "/sbin") != 0) - { + strcmp(token, RC_LIBEXECDIR "/sbin") != 0) { len = strlen(token); if (np != newpath) *np++ = ':'; @@ -1048,40 +1047,46 @@ int main(int argc, char **argv) stderr_fd = devnull_fd; if (redirect_stdout) { if ((stdout_fd = open(redirect_stdout, - O_WRONLY | O_CREAT | O_APPEND, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == -1) + O_WRONLY | O_CREAT | O_APPEND, + S_IRUSR | S_IWUSR | S_IRGRP | + S_IWGRP)) == -1) eerrorx("%s: unable to open the logfile" - " for stdout `%s': %s", - applet, redirect_stdout, strerror(errno)); - }else if (stdout_process) { + " for stdout `%s': %s", + applet, redirect_stdout, + strerror(errno)); + } else if (stdout_process) { stdout_fd = rc_pipe_command(stdout_process); if (stdout_fd == -1) eerrorx("%s: unable to open the logging process" - " for stdout `%s': %s", - applet, stdout_process, strerror(errno)); + " for stdout `%s': %s", + applet, stdout_process, + strerror(errno)); } if (redirect_stderr) { if ((stderr_fd = open(redirect_stderr, - O_WRONLY | O_CREAT | O_APPEND, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == -1) + O_WRONLY | O_CREAT | O_APPEND, + S_IRUSR | S_IWUSR | S_IRGRP | + S_IWGRP)) == -1) eerrorx("%s: unable to open the logfile" - " for stderr `%s': %s", - applet, redirect_stderr, strerror(errno)); - }else if (stderr_process) { + " for stderr `%s': %s", + applet, redirect_stderr, + strerror(errno)); + } else if (stderr_process) { stderr_fd = rc_pipe_command(stderr_process); if (stderr_fd == -1) eerrorx("%s: unable to open the logging process" - " for stderr `%s': %s", - applet, stderr_process, strerror(errno)); + " for stderr `%s': %s", + applet, stderr_process, + strerror(errno)); } if (background) dup2(stdin_fd, STDIN_FILENO); - if (background || redirect_stdout || stdout_process - || rc_yesno(getenv("EINFO_QUIET"))) + if (background || redirect_stdout || stdout_process || + rc_yesno(getenv("EINFO_QUIET"))) dup2(stdout_fd, STDOUT_FILENO); - if (background || redirect_stderr || stderr_process - || rc_yesno(getenv("EINFO_QUIET"))) + if (background || redirect_stderr || stderr_process || + rc_yesno(getenv("EINFO_QUIET"))) dup2(stderr_fd, STDERR_FILENO); for (i = getdtablesize() - 1; i >= 3; --i) @@ -1089,7 +1094,8 @@ int main(int argc, char **argv) if (scheduler != NULL) { int scheduler_index; - struct sched_param sched = {.sched_priority = sched_prio}; + struct sched_param sched = {.sched_priority = + sched_prio}; if (strcmp(scheduler, "fifo") == 0) scheduler_index = SCHED_FIFO; else if (strcmp(scheduler, "rr") == 0) @@ -1108,14 +1114,18 @@ int main(int argc, char **argv) eerrorx("Unknown scheduler: %s", scheduler); if (sched_prio == -1) - sched.sched_priority = sched_get_priority_min(scheduler_index); + sched.sched_priority = + sched_get_priority_min(scheduler_index); if (sched_setscheduler(mypid, scheduler_index, &sched)) - eerrorx("Failed to set scheduler: %s", strerror(errno)); + eerrorx("Failed to set scheduler: %s", + strerror(errno)); } else if (sched_prio != -1) { - const struct sched_param sched = {.sched_priority = sched_prio}; + const struct sched_param sched = {.sched_priority = + sched_prio}; if (sched_setparam(mypid, &sched)) - eerrorx("Failed to set scheduler parameters: %s", strerror(errno)); + eerrorx("Failed to set scheduler parameters: %s", + strerror(errno)); } setsid(); @@ -1124,8 +1134,8 @@ int main(int argc, char **argv) if (changeuser != NULL && pamr == PAM_SUCCESS) pam_close_session(pamh, PAM_SILENT); #endif - eerrorx("%s: failed to exec `%s': %s", - applet, exec,strerror(errno)); + eerrorx("%s: failed to exec `%s': %s", applet, exec, + strerror(errno)); } /* Parent process */ @@ -1138,8 +1148,7 @@ int main(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)); @@ -1152,10 +1161,8 @@ int main(int argc, char **argv) /* Wait a little bit and check that process is still running We do this as some badly written daemons fork and then barf */ - if (start_wait == 0 && - ((p = getenv("SSD_STARTWAIT")) || - (p = rc_conf_value("rc_start_wait")))) - { + if (start_wait == 0 && ((p = getenv("SSD_STARTWAIT")) || + (p = rc_conf_value("rc_start_wait")))) { if (sscanf(p, "%u", &start_wait) != 1) start_wait = 0; } @@ -1168,8 +1175,8 @@ int main(int argc, char **argv) ts.tv_nsec = (start_wait % 1000) * ONE_MS; if (nanosleep(&ts, NULL) == -1) { if (errno != EINTR) { - eerror("%s: nanosleep: %s", - applet, strerror(errno)); + eerror("%s: nanosleep: %s", applet, + strerror(errno)); return 0; } } @@ -1181,14 +1188,14 @@ int main(int argc, char **argv) pid = get_pid(applet, pidfile); if (pid == -1) { eerrorx("%s: did not " - "create a valid" - " pid in `%s'", - applet, pidfile); + "create a valid" + " pid in `%s'", + applet, pidfile); } } else pid = 0; if (do_stop(applet, exec, (const char *const *)margv, - pid, uid, 0, test, false) > 0) + pid, uid, 0, test, false) > 0) alive = true; } @@ -1197,8 +1204,8 @@ int main(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 */ diff --git a/src/supervise-daemon/supervise-daemon.c b/src/supervise-daemon/supervise-daemon.c index 58946716f..2e3bbe3b0 100644 --- a/src/supervise-daemon/supervise-daemon.c +++ b/src/supervise-daemon/supervise-daemon.c @@ -17,30 +17,30 @@ */ /* nano seconds */ -#define POLL_INTERVAL 20000000 -#define WAIT_PIDFILE 500000000 -#define ONE_SECOND 1000000000 -#define ONE_MS 1000000 +#define POLL_INTERVAL 20000000 +#define WAIT_PIDFILE 500000000 +#define ONE_SECOND 1000000000 +#define ONE_MS 1000000 -#include #include #include #include -#include #include +#include #include +#include #ifdef __linux__ -#include /* For io priority */ #include /* For prctl */ +#include /* For io priority */ #endif #include #include #include #include -#include #include +#include #include #include #include @@ -55,69 +55,68 @@ #include /* We are not supporting authentication conversations */ -static struct pam_conv conv = { NULL, NULL}; +static struct pam_conv conv = {NULL, NULL}; #endif #ifdef HAVE_CAP #include #endif +#include "_usage.h" #include "einfo.h" -#include "queue.h" -#include "rc.h" +#include "helpers.h" #include "misc.h" #include "plugin.h" +#include "queue.h" +#include "rc.h" #include "schedules.h" -#include "_usage.h" -#include "helpers.h" /* Use long option value that is out of range for 8 bit getopt values. * The exact enum value is internal and can freely change, so we keep the * options sorted. */ enum { - /* This has to come first so following values stay in the 0x100+ range. */ - LONGOPT_BASE = 0x100, + /* This has to come first so following values stay in the 0x100+ range. */ + LONGOPT_BASE = 0x100, - LONGOPT_CAPABILITIES, - LONGOPT_OOM_SCORE_ADJ, - LONGOPT_NO_NEW_PRIVS, - LONGOPT_SECBITS, + LONGOPT_CAPABILITIES, + LONGOPT_OOM_SCORE_ADJ, + LONGOPT_NO_NEW_PRIVS, + LONGOPT_SECBITS, }; const char *applet = NULL; const char *extraopts = NULL; -const char getoptstring[] = "A:a:D:d:e:g:H:I:Kk:m:N:p:R:r:s:Su:1:2:3" \ - getoptstring_COMMON; +const char getoptstring[] = + "A:a:D:d:e:g:H:I:Kk:m:N:p:R:r:s:Su:1:2:3" getoptstring_COMMON; const struct option longopts[] = { - { "healthcheck-timer", 1, NULL, 'a'}, - { "healthcheck-delay", 1, NULL, 'A'}, - { "capabilities", 1, NULL, LONGOPT_CAPABILITIES}, - { "secbits", 1, NULL, LONGOPT_SECBITS}, - { "no-new-privs", 0, NULL, LONGOPT_NO_NEW_PRIVS}, - { "respawn-delay", 1, NULL, 'D'}, - { "chdir", 1, NULL, 'd'}, - { "env", 1, NULL, 'e'}, - { "group", 1, NULL, 'g'}, - { "ionice", 1, NULL, 'I'}, - { "stop", 0, NULL, 'K'}, - { "umask", 1, NULL, 'k'}, - { "respawn-max", 1, NULL, 'm'}, - { "nicelevel", 1, NULL, 'N'}, - { "oom-score-adj",1, NULL, LONGOPT_OOM_SCORE_ADJ}, - { "pidfile", 1, NULL, 'p'}, - { "respawn-period", 1, NULL, 'P'}, - { "retry", 1, NULL, 'R'}, - { "chroot", 1, NULL, 'r'}, - { "signal", 1, NULL, 's'}, - { "start", 0, NULL, 'S'}, - { "user", 1, NULL, 'u'}, - { "stdout", 1, NULL, '1'}, - { "stderr", 1, NULL, '2'}, - { "reexec", 0, NULL, '3'}, - longopts_COMMON -}; -const char * const longopts_help[] = { + {"healthcheck-timer", 1, NULL, 'a'}, + {"healthcheck-delay", 1, NULL, 'A'}, + {"capabilities", 1, NULL, LONGOPT_CAPABILITIES}, + {"secbits", 1, NULL, LONGOPT_SECBITS}, + {"no-new-privs", 0, NULL, LONGOPT_NO_NEW_PRIVS}, + {"respawn-delay", 1, NULL, 'D'}, + {"chdir", 1, NULL, 'd'}, + {"env", 1, NULL, 'e'}, + {"group", 1, NULL, 'g'}, + {"ionice", 1, NULL, 'I'}, + {"stop", 0, NULL, 'K'}, + {"umask", 1, NULL, 'k'}, + {"respawn-max", 1, NULL, 'm'}, + {"nicelevel", 1, NULL, 'N'}, + {"oom-score-adj", 1, NULL, LONGOPT_OOM_SCORE_ADJ}, + {"pidfile", 1, NULL, 'p'}, + {"respawn-period", 1, NULL, 'P'}, + {"retry", 1, NULL, 'R'}, + {"chroot", 1, NULL, 'r'}, + {"signal", 1, NULL, 's'}, + {"start", 0, NULL, 'S'}, + {"user", 1, NULL, 'u'}, + {"stdout", 1, NULL, '1'}, + {"stderr", 1, NULL, '2'}, + {"reexec", 0, NULL, '3'}, + longopts_COMMON}; +const char *const longopts_help[] = { "set an initial health check delay", "set a health check timer", "Set the inheritable, ambient and bounding capabilities", @@ -143,8 +142,7 @@ const char * const longopts_help[] = { "Redirect stdout to file", "Redirect stderr to file", "reexec (used internally)", - longopts_help_COMMON -}; + longopts_help_COMMON}; const char *usagestring = NULL; static int healthcheckdelay = 0; @@ -188,7 +186,7 @@ static bool no_new_privs = false; extern char **environ; #if !defined(SYS_ioprio_set) && defined(__NR_ioprio_set) -# define SYS_ioprio_set __NR_ioprio_set +#define SYS_ioprio_set __NR_ioprio_set #endif #if !defined(__DragonFly__) static inline int ioprio_set(int which _unused, int who _unused, @@ -211,9 +209,9 @@ static void re_exec_supervisor(void) { syslog(LOG_WARNING, "Re-executing for %s", svcname); execlp("supervise-daemon", "supervise-daemon", svcname, "--reexec", - (char *) NULL); + (char *)NULL); syslog(LOG_ERR, "Unable to execute supervise-daemon: %s", - strerror(errno)); + strerror(errno)); exit(EXIT_FAILURE); } @@ -228,9 +226,11 @@ static void handle_signal(int sig) break; case SIGCHLD: if (exiting) - while (waitpid((pid_t)(-1), NULL, WNOHANG) > 0) {} + while (waitpid((pid_t)(-1), NULL, WNOHANG) > 0) { + } else { - while ((pid = waitpid((pid_t)(-1), NULL, WNOHANG|WNOWAIT)) > 0) { + while ((pid = waitpid((pid_t)(-1), NULL, + WNOHANG | WNOWAIT)) > 0) { if (pid == child_pid) break; pid = waitpid(pid, NULL, WNOHANG); @@ -248,7 +248,7 @@ static void handle_signal(int sig) errno = serrno; } -static char * expand_home(const char *home, const char *path) +static char *expand_home(const char *home, const char *path) { char *opath, *ppath, *p, *nh; struct passwd *pw; @@ -294,8 +294,8 @@ static char *make_cmdline(char **argv) for (c = argv; c && *c; c++) len += (strlen(*c) + 1); - cmdline = xmalloc(len+1); - memset(cmdline, 0, len+1); + cmdline = xmalloc(len + 1); + memset(cmdline, 0, len + 1); for (c = argv; c && *c; c++) { strcat(cmdline, *c); strcat(cmdline, " "); @@ -318,7 +318,7 @@ static pid_t exec_command(const char *cmd) } /* We need to block signals until we have forked */ - memset(&sa, 0, sizeof (sa)); + memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_DFL; sigemptyset(&sa.sa_mask); sigfillset(&full); @@ -339,14 +339,14 @@ static pid_t exec_command(const char *cmd) sigprocmask(SIG_SETMASK, &old, NULL); /* Safe to run now */ - execl(file, file, cmd, (char *) NULL); - syslog(LOG_ERR, "unable to exec `%s': %s\n", - file, strerror(errno)); + execl(file, file, cmd, (char *)NULL); + syslog(LOG_ERR, "unable to exec `%s': %s\n", file, + strerror(errno)); _exit(EXIT_FAILURE); } if (pid == -1) - syslog(LOG_ERR, "fork: %s\n",strerror (errno)); + syslog(LOG_ERR, "fork: %s\n", strerror(errno)); sigprocmask(SIG_SETMASK, &old, NULL); free(file); @@ -382,7 +382,8 @@ static void child_process(char *exec, char **argv) from_time_t(start_time_string, start_time); rc_service_value_set(svcname, "start_time", start_time_string); sprintf(start_count_string, "%i", respawn_count); - rc_service_value_set(svcname, "start_count", start_count_string); + rc_service_value_set(svcname, "start_count", + start_count_string); sprintf(start_count_string, "%d", getpid()); rc_service_value_set(svcname, "child_pid", start_count_string); } @@ -390,39 +391,40 @@ static void child_process(char *exec, char **argv) if (nicelevel != INT_MIN) { if (setpriority(PRIO_PROCESS, getpid(), nicelevel) == -1) eerrorx("%s: setpriority %d: %s", applet, nicelevel, - strerror(errno)); + strerror(errno)); } if (ionicec != -1 && ioprio_set(1, getpid(), ionicec | ioniced) == -1) eerrorx("%s: ioprio_set %d %d: %s", applet, ionicec, ioniced, - strerror(errno)); + strerror(errno)); if (oom_score_adj != INT_MIN) { fp = fopen("/proc/self/oom_score_adj", "w"); if (!fp) eerrorx("%s: oom_score_adj %d: %s", applet, - oom_score_adj, strerror(errno)); + oom_score_adj, strerror(errno)); fprintf(fp, "%d\n", oom_score_adj); fclose(fp); } 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)); #ifdef HAVE_PAM if (changeuser != NULL) { - pamr = pam_start("supervise-daemon", - changeuser, &conv, &pamh); + pamr = pam_start("supervise-daemon", changeuser, &conv, &pamh); if (pamr == PAM_SUCCESS) pamr = pam_acct_mgmt(pamh, PAM_SILENT); 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 @@ -435,7 +437,7 @@ static void child_process(char *exec, char **argv) #else if (uid && setuid(uid)) #endif - 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(); @@ -445,7 +447,8 @@ static void child_process(char *exec, char **argv) i = cap_iab_set_proc(cap_iab); if (cap_free(cap_iab) != 0) - eerrorx("Could not releasable memory: %s", strerror(errno)); + eerrorx("Could not releasable memory: %s", + strerror(errno)); if (i != 0) eerrorx("Could not set iab: %s", strerror(errno)); @@ -453,14 +456,16 @@ static void child_process(char *exec, char **argv) if (secbits != 0) { if (cap_set_secbits(secbits) < 0) - eerrorx("Could not set securebits to 0x%x: %s", secbits, strerror(errno)); + eerrorx("Could not set securebits to 0x%x: %s", secbits, + strerror(errno)); } #endif #ifdef PR_SET_NO_NEW_PRIVS if (no_new_privs) { if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) - eerrorx("Could not set No New Privs flag: %s", strerror(errno)); + eerrorx("Could not set No New Privs flag: %s", + strerror(errno)); } #endif @@ -494,12 +499,11 @@ static void child_process(char *exec, char **argv) TAILQ_FOREACH(env, env_list, entries) { if ((strncmp(env->value, "RC_", 3) == 0 && - strncmp(env->value, "RC_SERVICE=", 11) != 0 && - strncmp(env->value, "RC_SVCNAME=", 11) != 0) || + strncmp(env->value, "RC_SERVICE=", 11) != 0 && + strncmp(env->value, "RC_SVCNAME=", 11) != 0) || strncmp(env->value, "SSD_NICELEVEL=", 14) == 0 || strncmp(env->value, "SSD_IONICELEVEL=", 16) == 0 || - strncmp(env->value, "SSD_OOM_SCORE_ADJ=", 18) == 0) - { + strncmp(env->value, "SSD_OOM_SCORE_ADJ=", 18) == 0) { p = strchr(env->value, '='); *p = '\0'; unsetenv(env->value); @@ -520,14 +524,13 @@ static void child_process(char *exec, char **argv) p++; } if (strcmp(token, RC_LIBEXECDIR "/bin") != 0 && - strcmp(token, RC_LIBEXECDIR "/sbin") != 0) - { + strcmp(token, RC_LIBEXECDIR "/sbin") != 0) { len = strlen(token); if (np != newpath) *np++ = ':'; memcpy(np, token, len); np += len; - } + } token = p; } *np = '\0'; @@ -540,19 +543,19 @@ static void child_process(char *exec, char **argv) stderr_fd = devnull_fd; if (redirect_stdout) { if ((stdout_fd = open(redirect_stdout, - O_WRONLY | O_CREAT | O_APPEND, - S_IRUSR | S_IWUSR)) == -1) + O_WRONLY | O_CREAT | O_APPEND, + S_IRUSR | S_IWUSR)) == -1) eerrorx("%s: unable to open the logfile" - " for stdout `%s': %s", - applet, redirect_stdout, strerror(errno)); + " for stdout `%s': %s", + applet, redirect_stdout, strerror(errno)); } if (redirect_stderr) { if ((stderr_fd = open(redirect_stderr, - O_WRONLY | O_CREAT | O_APPEND, - S_IRUSR | S_IWUSR)) == -1) + O_WRONLY | O_CREAT | O_APPEND, + S_IRUSR | S_IWUSR)) == -1) eerrorx("%s: unable to open the logfile" - " for stderr `%s': %s", - applet, redirect_stderr, strerror(errno)); + " for stderr `%s': %s", + applet, redirect_stderr, strerror(errno)); } dup2(stdin_fd, STDIN_FILENO); @@ -572,7 +575,7 @@ static void child_process(char *exec, char **argv) if (changeuser != NULL && pamr == PAM_SUCCESS) pam_close_session(pamh, PAM_SILENT); #endif - eerrorx("%s: failed to exec `%s': %s", applet, exec,strerror(errno)); + eerrorx("%s: failed to exec `%s': %s", applet, exec, strerror(errno)); } static void supervisor(char *exec, char **argv) @@ -593,8 +596,8 @@ static void supervisor(char *exec, char **argv) sigset_t signals; struct sigaction sa; struct timespec ts; - time_t respawn_now= 0; - time_t first_spawn= 0; + time_t respawn_now = 0; + time_t first_spawn = 0; /* block all signals we do not handle */ sigfillset(&signals); @@ -617,10 +620,10 @@ static void supervisor(char *exec, char **argv) fclose(fp); if (svcname) - rc_service_daemon_set(svcname, exec, (const char * const *) argv, - pidfile, true); + rc_service_daemon_set(svcname, exec, (const char *const *)argv, + pidfile, true); - /* remove the controlling tty */ + /* remove the controlling tty */ #ifdef TIOCNOTTY ioctl(tty_fd, TIOCNOTTY, 0); close(tty_fd); @@ -648,13 +651,16 @@ static void supervisor(char *exec, char **argv) if (verbose) syslog(LOG_DEBUG, "Received %s from fifo", buf); if (strncasecmp(buf, "sig", 3) == 0) { - if ((sscanf(buf, "%s %d", cmd, &sig_send) == 2) - && (sig_send >= 0 && sig_send < NSIG)) { - syslog(LOG_INFO, "Sending signal %d to %d", sig_send, - child_pid); + if ((sscanf(buf, "%s %d", cmd, &sig_send) == + 2) && + (sig_send >= 0 && sig_send < NSIG)) { + syslog(LOG_INFO, + "Sending signal %d to %d", + sig_send, child_pid); if (kill(child_pid, sig_send) == -1) - syslog(LOG_ERR, "Unable to send signal %d to %d", - sig_send, child_pid); + syslog(LOG_ERR, + "Unable to send signal %d to %d", + sig_send, child_pid); } } continue; @@ -663,42 +669,53 @@ static void supervisor(char *exec, char **argv) do_healthcheck = 0; alarm(0); if (verbose) - syslog(LOG_DEBUG, "running health check for %s", svcname); + syslog(LOG_DEBUG, "running health check for %s", + svcname); health_pid = exec_command("healthcheck"); health_status = rc_waitpid(health_pid); - if (WIFEXITED(health_status) && WEXITSTATUS(health_status) == 0) + if (WIFEXITED(health_status) && + WEXITSTATUS(health_status) == 0) alarm(healthchecktimer); else { - syslog(LOG_WARNING, "health check for %s failed", svcname); + syslog(LOG_WARNING, + "health check for %s failed", svcname); health_pid = exec_command("unhealthy"); rc_waitpid(health_pid); - syslog(LOG_INFO, "stopping %s, pid %d", exec, child_pid); - nkilled = run_stop_schedule(applet, NULL, NULL, child_pid, 0, - false, false, true); + syslog(LOG_INFO, "stopping %s, pid %d", exec, + child_pid); + nkilled = run_stop_schedule(applet, NULL, NULL, + child_pid, 0, false, + false, true); if (nkilled < 0) - syslog(LOG_INFO, "Unable to kill %d: %s", - child_pid, strerror(errno)); + syslog(LOG_INFO, + "Unable to kill %d: %s", + child_pid, strerror(errno)); else healthcheck_respawn = 1; } } if (exiting) { alarm(0); - syslog(LOG_INFO, "stopping %s, pid %d", exec, child_pid); - nkilled = run_stop_schedule(applet, NULL, NULL, child_pid, 0, - false, false, true); + syslog(LOG_INFO, "stopping %s, pid %d", exec, + child_pid); + nkilled = run_stop_schedule(applet, NULL, NULL, + child_pid, 0, false, false, + true); if (nkilled > 0) - syslog(LOG_INFO, "killed %d processes", nkilled); + syslog(LOG_INFO, "killed %d processes", + nkilled); continue; } wait_pid = waitpid(child_pid, &i, WNOHANG); if (wait_pid == child_pid) { if (WIFEXITED(i)) - syslog(LOG_WARNING, "%s, pid %d, exited with return code %d", - exec, child_pid, WEXITSTATUS(i)); + syslog(LOG_WARNING, + "%s, pid %d, exited with return code %d", + exec, child_pid, WEXITSTATUS(i)); else if (WIFSIGNALED(i)) - syslog(LOG_WARNING, "%s, pid %d, terminated by signal %d", - exec, child_pid, WTERMSIG(i)); + syslog(LOG_WARNING, + "%s, pid %d, terminated by signal %d", + exec, child_pid, WTERMSIG(i)); } if (wait_pid == child_pid || healthcheck_respawn) { do_healthcheck = 0; @@ -707,15 +724,16 @@ static void supervisor(char *exec, char **argv) respawn_now = time(NULL); if (first_spawn == 0) first_spawn = respawn_now; - if ((respawn_period > 0) - && (respawn_now - first_spawn > respawn_period)) { + if ((respawn_period > 0) && + (respawn_now - first_spawn > respawn_period)) { respawn_count = 0; first_spawn = 0; } else respawn_count++; if (respawn_max > 0 && respawn_count > respawn_max) { - syslog(LOG_WARNING, "respawned \"%s\" too many times, exiting", - exec); + syslog(LOG_WARNING, + "respawned \"%s\" too many times, exiting", + exec); exiting = 1; failing = 1; continue; @@ -727,7 +745,8 @@ static void supervisor(char *exec, char **argv) continue; child_pid = fork(); if (child_pid == -1) { - syslog(LOG_ERR, "%s: fork: %s", applet, strerror(errno)); + syslog(LOG_ERR, "%s: fork: %s", applet, + strerror(errno)); exit(EXIT_FAILURE); } if (child_pid == 0) { @@ -748,7 +767,7 @@ static void supervisor(char *exec, char **argv) if (svcname) { rc_service_daemon_set(svcname, exec, (const char *const *)argv, - pidfile, false); + pidfile, false); rc_service_value_set(svcname, "child_pid", NULL); rc_service_mark(svcname, RC_SERVICE_STOPPED); if (failing) @@ -797,22 +816,23 @@ int main(int argc, char **argv) atexit(cleanup); svcname = getenv("RC_SVCNAME"); if (!svcname) - eerrorx("%s: The RC_SVCNAME environment variable is not set", applet); + eerrorx("%s: The RC_SVCNAME environment variable is not set", + applet); openlog(applet, LOG_PID, LOG_DAEMON); if (argc <= 1 || strcmp(argv[1], svcname)) - eerrorx("%s: the first argument is %s and must be %s", - applet, argv[1], svcname); + eerrorx("%s: the first argument is %s and must be %s", applet, + argv[1], svcname); if ((tmp = getenv("SSD_NICELEVEL"))) if (sscanf(tmp, "%d", &nicelevel) != 1) eerror("%s: invalid nice level `%s' (SSD_NICELEVEL)", - applet, tmp); + applet, tmp); if ((tmp = getenv("SSD_IONICELEVEL"))) { n = sscanf(tmp, "%d:%d", &ionicec, &ioniced); if (n != 1 && n != 2) eerror("%s: invalid ionice level `%s' (SSD_IONICELEVEL)", - applet, tmp); + applet, tmp); if (ionicec == 0) ioniced = 0; else if (ionicec == 3) @@ -822,7 +842,7 @@ int main(int argc, char **argv) if ((tmp = getenv("SSD_OOM_SCORE_ADJ"))) if (sscanf(tmp, "%d", &oom_score_adj) != 1) eerror("%s: invalid oom_score_adj `%s' (SSD_OOM_SCORE_ADJ)", - applet, tmp); + applet, tmp); /* Get our user name and initial dir */ p = getenv("USER"); @@ -845,29 +865,34 @@ int main(int argc, char **argv) argv++; } while ((opt = getopt_long(argc, argv, getoptstring, longopts, - (int *) 0)) != -1) + (int *)0)) != -1) switch (opt) { - case 'a': /* --healthcheck-timer