mirror of
https://github.com/troglobit/finit.git
synced 2026-11-12 16:51:25 +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
+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