mirror of
https://github.com/troglobit/finit.git
synced 2026-10-25 12:04:36 +00:00
Refactor, move all PID file related functions to the same module
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
df303c6243
commit
a1c33dec21
5 files changed
+113
-75
No files matched your search
+2
-1
@@ -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 \
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2015 Joachim Nilsson <troglobit@gmail.com>
|
||||
* Copyright (c) 2008-2018 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* 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 <errno.h>
|
||||
#include <paths.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <lite/lite.h>
|
||||
|
||||
#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
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/* PID and PID file helpers
|
||||
*
|
||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||
* Copyright (c) 2008-2018 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* 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:
|
||||
*/
|
||||
+1
-69
@@ -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
|
||||
|
||||
Reference in new issue
Block a user