diff --git a/src/Makefile.am b/src/Makefile.am index e75bc9f1..69257d1f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -16,7 +16,8 @@ finit_SOURCES = api.c \ getty.c stty.c \ helpers.c helpers.h \ log.c log.h \ - mdadm.c mount.c pid.c \ + mdadm.c mount.c \ + pid.c pid.h \ plugin.c plugin.h private.h \ service.c service.h \ sig.c sig.h \ diff --git a/src/helpers.h b/src/helpers.h index ee15f87b..442bf88e 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -57,9 +57,6 @@ int sh (char *tty); int stty (int fd, speed_t speed); speed_t stty_parse_speed(char *baud); -int pid_alive (pid_t pid); -char *pid_get_name (pid_t pid, char *name, size_t len); - void printv (const char *fmt, va_list ap); void print (int action, const char *fmt, ...); void print_desc (char *action, char *desc); diff --git a/src/pid.c b/src/pid.c index 8dc5ef7c..a598b469 100644 --- a/src/pid.c +++ b/src/pid.c @@ -1,7 +1,7 @@ -/* Misc. utility functions and C-library extensions for finit and its plugins +/* PID and PID file helpers * * Copyright (c) 2008-2010 Claudio Matsuoka - * Copyright (c) 2008-2015 Joachim Nilsson + * Copyright (c) 2008-2018 Joachim Nilsson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,11 +23,13 @@ */ #include +#include #include #include #include #include +#include "svc.h" #include "helpers.h" @@ -90,6 +92,75 @@ char *pid_get_name(pid_t pid, char *name, size_t len) return pname; } +char *pid_file(svc_t *svc) +{ + static char fn[MAX_ARG_LEN]; + + if (svc->pidfile[0]) { + if (svc->pidfile[0] == '!') + return &svc->pidfile[1]; + return svc->pidfile; + } + + snprintf(fn, sizeof(fn), "%s%s.pid", _PATH_VARRUN, basename(svc->cmd)); + return fn; +} + +int pid_file_create(svc_t *svc) +{ + FILE *fp; + + if (!svc->pidfile[0] || svc->pidfile[0] == '!') + return 1; + + fp = fopen(svc->pidfile, "w"); + if (!fp) + return 1; + fprintf(fp, "%d\n", svc->pid); + + return fclose(fp); +} + +int pid_file_parse(svc_t *svc, char *arg) +{ + int len, not = 0; + + /* Always clear, called with some sort of 'pid[:..]' argument */ + svc->pidfile[0] = 0; + + /* Sanity check ... */ + if (!arg || !arg[0]) + return 1; + + /* 'pid:' imples argument following*/ + if (!strncmp(arg, "pid:", 4)) { + arg += 4; + if ((arg[0] == '!' && arg[1] == '/') || arg[0] == '/') { + strlcpy(svc->pidfile, arg, sizeof(svc->pidfile)); + return 0; + } + + if (arg[0] == '!') { + arg++; + not++; + } + len = snprintf(svc->pidfile, sizeof(svc->pidfile), "%s%s%s", + not ? "!" : "", _PATH_VARRUN, arg); + if (len > 4 && strcmp(&svc->pidfile[len - 4], ".pid")) + strlcat(svc->pidfile, ".pid", sizeof(svc->pidfile)); + + return 0; + } + + /* 'pid' arg, no argument following */ + if (!strcmp(arg, "pid")) { + strlcpy(svc->pidfile, pid_file(svc), sizeof(svc->pidfile)); + return 0; + } + + return 1; +} + /** * Local Variables: * indent-tabs-mode: t diff --git a/src/pid.h b/src/pid.h new file mode 100644 index 00000000..4ba04e21 --- /dev/null +++ b/src/pid.h @@ -0,0 +1,37 @@ +/* PID and PID file helpers + * + * Copyright (c) 2008-2010 Claudio Matsuoka + * Copyright (c) 2008-2018 Joachim Nilsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +int pid_alive (pid_t pid); +char *pid_get_name (pid_t pid, char *name, size_t len); + +char *pid_file (svc_t *svc); +int pid_file_create (svc_t *svc); +int pid_file_parse (svc_t *svc, char *arg); + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */ diff --git a/src/service.c b/src/service.c index 5e4a4c27..79f1f7ac 100644 --- a/src/service.c +++ b/src/service.c @@ -36,6 +36,7 @@ #include "finit.h" #include "helpers.h" #include "inetd.h" +#include "pid.h" #include "private.h" #include "sig.h" #include "service.h" @@ -114,75 +115,6 @@ static int is_norespawn(void) fexist("/tmp/norespawn"); } -static char *pid_file(svc_t *svc) -{ - static char fn[MAX_ARG_LEN]; - - if (svc->pidfile[0]) { - if (svc->pidfile[0] == '!') - return &svc->pidfile[1]; - return svc->pidfile; - } - - snprintf(fn, sizeof(fn), "%s%s.pid", _PATH_VARRUN, basename(svc->cmd)); - return fn; -} - -static int pid_file_create(svc_t *svc) -{ - FILE *fp; - - if (!svc->pidfile[0] || svc->pidfile[0] == '!') - return 1; - - fp = fopen(svc->pidfile, "w"); - if (!fp) - return 1; - fprintf(fp, "%d\n", svc->pid); - - return fclose(fp); -} - -static int pid_file_parse(svc_t *svc, char *arg) -{ - int len, not = 0; - - /* Always clear, called with some sort of 'pid[:..]' argument */ - svc->pidfile[0] = 0; - - /* Sanity check ... */ - if (!arg || !arg[0]) - return 1; - - /* 'pid:' imples argument following*/ - if (!strncmp(arg, "pid:", 4)) { - arg += 4; - if ((arg[0] == '!' && arg[1] == '/') || arg[0] == '/') { - strlcpy(svc->pidfile, arg, sizeof(svc->pidfile)); - return 0; - } - - if (arg[0] == '!') { - arg++; - not++; - } - len = snprintf(svc->pidfile, sizeof(svc->pidfile), "%s%s%s", - not ? "!" : "", _PATH_VARRUN, arg); - if (len > 4 && strcmp(&svc->pidfile[len - 4], ".pid")) - strlcat(svc->pidfile, ".pid", sizeof(svc->pidfile)); - - return 0; - } - - /* 'pid' arg, no argument following */ - if (!strcmp(arg, "pid")) { - strlcpy(svc->pidfile, pid_file(svc), sizeof(svc->pidfile)); - return 0; - } - - return 1; -} - /** * service_start - Start service * @svc: Service to start