sysmgr

a simplistic service supervisor (deprecated)
git clone git://git.ckyln.com/~cem/sysmgr.git
Log | Files | Refs | README | LICENSE

commit ed796688bdeed6db0fb53db1146ae4baf0bf4228
parent 5d4dfcbf010358a22bcdd1b5696f71e86e44a8e2
Author: Cem Keylan <cem@ckyln.com>
Date:   Wed, 16 Sep 2020 19:11:48 +0300

{sysmgr-needs,util}.c: change C style

Diffstat:
Mutils/sysmgr-needs.c | 72+++++++++++++++++++++++++++++++++++++-----------------------------------
Mutils/util.c | 85+++++++++++++++++++++++++++++++++++++++++--------------------------------------
2 files changed, 81 insertions(+), 76 deletions(-)

diff --git a/utils/sysmgr-needs.c b/utils/sysmgr-needs.c @@ -7,39 +7,41 @@ #include "util.h" -int main(int argc, char *argv[]) { - if (argc < 2) { - printf("usage: %s [service...]\n", argv[0]); - return 1; - } - - int i; - struct service sv; - - /* We are doing two iterations, because we want to exit as soon as possible if - * any of the given services do not exist. - */ - for (i = 1; i < argc; i++) { - sv_init(&sv, argv[i]); - if (access(sv.svfile, R_OK) == -1) - die("sysmgr: service file for '%s' doesn't exist.", sv.name); - } - - for (i = 1; i < argc; i++) { - while(1) { - int pid; - - sv_init(&sv, argv[i]); - FILE *pidfile; - - pidfile = fopen(sv.pidfile, "r"); - if (pidfile != NULL) { - fscanf(pidfile, "%d", &pid); - fclose(pidfile); - - if (checkprocess(pid) == 0) break; - } - sleep(1); - } - } +int +main(int argc, char *argv[]) +{ + if (argc < 2) { + printf("usage: %s [service...]\n", argv[0]); + return 1; + } + + int i; + struct service sv; + + /* We are doing two iterations, because we want to exit as soon as possible if + * any of the given services do not exist. + */ + for (i = 1; i < argc; i++) { + sv_init(&sv, argv[i]); + if (access(sv.svfile, R_OK) == -1) + die("sysmgr: service file for '%s' doesn't exist.", sv.name); + } + + for (i = 1; i < argc; i++) { + while(1) { + int pid; + + sv_init(&sv, argv[i]); + FILE *pidfile; + + pidfile = fopen(sv.pidfile, "r"); + if (pidfile != NULL) { + fscanf(pidfile, "%d", &pid); + fclose(pidfile); + + if (checkprocess(pid) == 0) break; + } + sleep(1); + } + } } diff --git a/utils/util.c b/utils/util.c @@ -14,55 +14,58 @@ extern int errno; -char *getenv_fallback(char *name, char *fallback) { - char *value = getenv(name); - if ( ! value ) value = fallback; - return value; +char* +getenv_fallback(char *name, char *fallback) +{ + char *value = getenv(name); + if ( ! value ) value = fallback; + return value; } -service *sv_init(service *sv, char *sv_name) { - sprintf(sv->name, "%s", sv_name); - sprintf(sv->sysdir, "%s", getenv_fallback("SYSDIR", "/var/sysmgr")); - sprintf(sv->rundir, "%s", getenv_fallback("RUNDIR", "/run/sysmgr")); - sprintf(sv->pidfile, "%s/%s/pid", sv->rundir, sv->name); - sprintf(sv->svfile, "%s/%s", sv->sysdir, sv_name); +service* +sv_init(service *sv, char *sv_name) +{ + sprintf(sv->name, "%s", sv_name); + sprintf(sv->sysdir, "%s", getenv_fallback("SYSDIR", "/var/sysmgr")); + sprintf(sv->rundir, "%s", getenv_fallback("RUNDIR", "/run/sysmgr")); + sprintf(sv->pidfile, "%s/%s/pid", sv->rundir, sv->name); + sprintf(sv->svfile, "%s/%s", sv->sysdir, sv_name); - return sv; + return sv; } -void die(const char *msg, ...) { - va_list ap; +void +die(const char *msg, ...) +{ + va_list ap; - va_start(ap, msg); - vfprintf(stderr, msg, ap); - va_end(ap); + va_start(ap, msg); + vfprintf(stderr, msg, ap); + va_end(ap); - if (msg[0] && msg[strlen(msg)-1] == ':') { - fputc(' ', stderr); - perror(NULL); - } else { - fputc('\n', stderr); - } - exit(1); + if (msg[0] && msg[strlen(msg)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + exit(1); } -int checkprocess(int pid) { - if (kill(pid, 0) == 0) return 0; - else { +int +checkprocess(int pid) +{ + if (kill(pid, 0) == 0) return 0; + else { + switch (errno) { + case 1: + die("kill:"); + break; + default: + perror("kill"); + break; + } - switch (errno) { - case 1: - /* - * Instead of a continuous loop, exit the program if we don't have the - * permissions to check the process - */ - die("kill:"); - break; - default: - perror("kill"); - break; - } - - return 1; - } + return 1; + } }