2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
librc-daemon
|
|
|
|
Finds PID for given daemon criteria
|
|
|
|
*/
|
|
|
|
|
2008-01-14 10:35:22 +05:30
|
|
|
/*
|
2008-03-26 23:23:37 +05:30
|
|
|
* Copyright 2007-2008 Roy Marples <roy@marples.name>
|
2007-11-14 20:52:04 +05:30
|
|
|
* All rights reserved
|
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-04-13 19:38:16 +05:30
|
|
|
#include "librc.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
#ifdef __GLIBC__
|
|
|
|
# if ! defined (__UCLIBC__) && ! defined (__dietlibc__)
|
|
|
|
static size_t strlcpy(char *dst, const char *src, size_t size)
|
|
|
|
{
|
|
|
|
const char *s = src;
|
|
|
|
size_t n = size;
|
|
|
|
|
|
|
|
if (n && --n)
|
|
|
|
do {
|
|
|
|
if (! (*dst++ = *src++))
|
|
|
|
break;
|
|
|
|
} while (--n);
|
|
|
|
|
|
|
|
if (! n) {
|
|
|
|
if (size)
|
|
|
|
*dst = '\0';
|
|
|
|
while (*src++);
|
|
|
|
}
|
|
|
|
|
|
|
|
return src - s - 1;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#if defined(__linux__)
|
2008-04-30 16:25:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static bool pid_is_cmd(pid_t pid, const char *cmd)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char buffer[32];
|
|
|
|
FILE *fp;
|
|
|
|
int c;
|
2008-04-30 16:25:42 +05:30
|
|
|
bool retval = false;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(buffer, sizeof(buffer), "/proc/%d/stat", pid);
|
2008-04-30 16:25:42 +05:30
|
|
|
if ((fp = fopen(buffer, "r"))) {
|
|
|
|
while ((c = getc(fp)) != EOF && c != '(')
|
|
|
|
;
|
|
|
|
if (c == '(') {
|
|
|
|
while ((c = getc(fp)) != EOF && c == *cmd)
|
|
|
|
cmd++;
|
|
|
|
if (c == ')' && *cmd == '\0')
|
|
|
|
retval = true;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
fclose(fp);
|
|
|
|
}
|
2008-04-30 16:25:42 +05:30
|
|
|
return retval;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static bool pid_is_exec(pid_t pid, const char *const *argv)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char cmdline[32];
|
|
|
|
char buffer[PATH_MAX];
|
|
|
|
char *p;
|
2008-04-30 17:56:38 +05:30
|
|
|
int fd;
|
2007-04-11 18:14:47 +05:30
|
|
|
int r;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
/* Check it's the right binary */
|
2008-04-30 16:01:06 +05:30
|
|
|
snprintf(cmdline, sizeof(cmdline), "/proc/%u/exe", pid);
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
2008-03-16 22:30:56 +05:30
|
|
|
if (readlink(cmdline, buffer, sizeof(buffer)) != -1) {
|
|
|
|
if (strcmp(*argv, buffer) == 0)
|
|
|
|
return true;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* We should cater for deleted binaries too */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strlen(buffer) > 10) {
|
|
|
|
p = buffer + (strlen(buffer) - 10);
|
|
|
|
if (strcmp(p, " (deleted)") == 0) {
|
2008-04-30 16:25:42 +05:30
|
|
|
*p = '\0';
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strcmp(buffer, *argv) == 0)
|
|
|
|
return true;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(cmdline, sizeof(cmdline), "/proc/%u/cmdline", pid);
|
|
|
|
if ((fd = open(cmdline, O_RDONLY)) < 0)
|
|
|
|
return false;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
r = read(fd, buffer, sizeof(buffer));
|
|
|
|
close(fd);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (r == -1)
|
2008-04-30 16:25:42 +05:30
|
|
|
return false;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-04-30 16:25:42 +05:30
|
|
|
buffer[r] = '\0';
|
2008-02-17 20:42:00 +05:30
|
|
|
p = buffer;
|
|
|
|
while (*argv) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strcmp(*argv, p) != 0)
|
|
|
|
return false;
|
2008-02-17 20:42:00 +05:30
|
|
|
argv++;
|
2008-03-16 22:30:56 +05:30
|
|
|
p += strlen(p) + 1;
|
2008-04-30 16:01:06 +05:30
|
|
|
if ((unsigned)(p - buffer) > sizeof(buffer))
|
2008-03-16 22:30:56 +05:30
|
|
|
return false;
|
2008-02-17 20:42:00 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
return true;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PIDLIST *rc_find_pids(const char *const *argv, const char *cmd,
|
|
|
|
uid_t uid, pid_t pid)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
DIR *procdir;
|
|
|
|
struct dirent *entry;
|
|
|
|
pid_t p;
|
|
|
|
char buffer[PATH_MAX];
|
|
|
|
struct stat sb;
|
|
|
|
pid_t runscript_pid = 0;
|
|
|
|
char *pp;
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PIDLIST *pids = NULL;
|
|
|
|
RC_PID *pi;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((procdir = opendir("/proc")) == NULL)
|
|
|
|
return NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
We never match RC_RUNSCRIPT_PID if present so we avoid the below
|
|
|
|
scenario
|
|
|
|
|
|
|
|
/etc/init.d/ntpd stop does
|
|
|
|
start-stop-daemon --stop --name ntpd
|
|
|
|
catching /etc/init.d/ntpd stop
|
|
|
|
|
|
|
|
nasty
|
|
|
|
*/
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((pp = getenv("RC_RUNSCRIPT_PID"))) {
|
|
|
|
if (sscanf(pp, "%d", &runscript_pid) != 1)
|
2007-04-11 18:14:47 +05:30
|
|
|
runscript_pid = 0;
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
while ((entry = readdir(procdir)) != NULL) {
|
|
|
|
if (sscanf(entry->d_name, "%d", &p) != 1)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
if (runscript_pid != 0 && runscript_pid == p)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pid != 0 && pid != p)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (uid) {
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(buffer, sizeof(buffer), "/proc/%d", p);
|
|
|
|
if (stat(buffer, &sb) != 0 || sb.st_uid != uid)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (cmd && ! pid_is_cmd(p, cmd))
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
2008-03-17 15:29:52 +05:30
|
|
|
if (argv && ! cmd && !
|
|
|
|
pid_is_exec(p, (const char *const *)argv))
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
2008-03-17 15:29:52 +05:30
|
|
|
if (! pids) {
|
|
|
|
pids = xmalloc(sizeof(*pids));
|
|
|
|
LIST_INIT(pids);
|
2007-09-29 22:12:08 +05:30
|
|
|
}
|
2008-03-17 15:29:52 +05:30
|
|
|
pi = xmalloc(sizeof(*pi));
|
|
|
|
pi->pid = p;
|
|
|
|
LIST_INSERT_HEAD(pids, pi, entries);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
closedir(procdir);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return pids;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_find_pids)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-11-28 15:36:31 +05:30
|
|
|
#elif BSD
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-04 01:25:29 +05:30
|
|
|
# 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
|
2007-04-05 20:31:09 +05:30
|
|
|
# ifndef KERN_PROC_PROC
|
|
|
|
# define KERN_PROC_PROC KERN_PROC_ALL
|
|
|
|
# endif
|
2007-04-05 16:48:42 +05:30
|
|
|
# define _KINFO_PROC kinfo_proc
|
|
|
|
# define _KVM_GETARGV kvm_getargv
|
|
|
|
# define _GET_KINFO_UID(kp) (kp.ki_ruid)
|
|
|
|
# define _GET_KINFO_COMM(kp) (kp.ki_comm)
|
|
|
|
# define _GET_KINFO_PID(kp) (kp.ki_pid)
|
2008-02-14 05:14:17 +05:30
|
|
|
# define _KVM_PATH _PATH_DEVNULL
|
2008-03-03 15:27:48 +05:30
|
|
|
# define _KVM_FLAGS O_RDONLY
|
2007-04-05 16:48:42 +05:30
|
|
|
# endif
|
|
|
|
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PIDLIST *rc_find_pids(const char *const *argv, const char *cmd,
|
|
|
|
uid_t uid, pid_t pid)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
static kvm_t *kd = NULL;
|
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
struct _KINFO_PROC *kp;
|
|
|
|
int i;
|
|
|
|
int processes = 0;
|
2008-02-17 20:42:00 +05:30
|
|
|
int pargc = 0;
|
|
|
|
char **pargv;
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PIDLIST *pids = NULL;
|
|
|
|
RC_PID *pi;
|
2008-03-16 22:30:56 +05:30
|
|
|
pid_t p;
|
2008-02-18 21:56:49 +05:30
|
|
|
const char *const *arg;
|
2008-02-17 20:42:00 +05:30
|
|
|
int match;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((kd = kvm_openfiles(_KVM_PATH, _KVM_PATH,
|
|
|
|
NULL, _KVM_FLAGS, errbuf)) == NULL)
|
2008-02-14 05:14:17 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(stderr, "kvm_open: %s\n", errbuf);
|
|
|
|
return NULL;
|
2007-10-01 14:12:27 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-11-28 15:36:31 +05:30
|
|
|
#ifdef _KVM_GETPROC2
|
2008-03-16 22:30:56 +05:30
|
|
|
kp = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*kp), &processes);
|
2007-11-28 15:36:31 +05:30
|
|
|
#else
|
2008-03-16 22:30:56 +05:30
|
|
|
kp = kvm_getprocs(kd, KERN_PROC_PROC, 0, &processes);
|
2007-04-05 20:31:09 +05:30
|
|
|
#endif
|
2008-02-14 05:14:17 +05:30
|
|
|
if ((kp == NULL && processes > 0) || (kp != NULL && processes < 0)) {
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(stderr, "kvm_getprocs: %s\n", kvm_geterr(kd));
|
|
|
|
kvm_close(kd);
|
|
|
|
return NULL;
|
2008-02-14 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
for (i = 0; i < processes; i++) {
|
2008-03-16 22:30:56 +05:30
|
|
|
p = _GET_KINFO_PID(kp[i]);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pid != 0 && pid != p)
|
|
|
|
continue;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (uid != 0 && uid != _GET_KINFO_UID(kp[i]))
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cmd) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (! _GET_KINFO_COMM(kp[i]) ||
|
|
|
|
strcmp(cmd, _GET_KINFO_COMM(kp[i])) != 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-18 21:56:49 +05:30
|
|
|
if (argv && *argv && ! cmd) {
|
2008-03-16 22:30:56 +05:30
|
|
|
pargv = _KVM_GETARGV(kd, &kp[i], pargc);
|
2008-02-17 20:42:00 +05:30
|
|
|
if (! pargv || ! *pargv)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
2008-02-17 20:42:00 +05:30
|
|
|
arg = argv;
|
|
|
|
match = 1;
|
|
|
|
|
2008-02-18 21:56:49 +05:30
|
|
|
while (*arg && *pargv)
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strcmp(*arg++, *pargv++) != 0) {
|
2008-02-17 20:42:00 +05:30
|
|
|
match = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! match)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-17 15:29:52 +05:30
|
|
|
if (! pids) {
|
|
|
|
pids = xmalloc(sizeof(*pids));
|
|
|
|
LIST_INIT(pids);
|
2007-09-29 22:12:08 +05:30
|
|
|
}
|
2008-03-17 15:29:52 +05:30
|
|
|
pi = xmalloc(sizeof(*pi));
|
|
|
|
pi->pid = p;
|
|
|
|
LIST_INSERT_HEAD(pids, pi, entries);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
kvm_close(kd);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return pids;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_find_pids)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#else
|
|
|
|
# error "Platform not supported!"
|
|
|
|
#endif
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static bool _match_daemon(const char *path, const char *file,
|
|
|
|
RC_STRINGLIST *match)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-18 02:57:37 +05:30
|
|
|
char *line = NULL;
|
|
|
|
size_t len = 0;
|
2008-03-17 18:55:56 +05:30
|
|
|
char ffile[PATH_MAX];
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRING *m;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
snprintf(ffile, sizeof(ffile), "%s/%s", path, file);
|
2008-03-16 22:30:56 +05:30
|
|
|
fp = fopen(ffile, "r");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-02-23 05:56:11 +05:30
|
|
|
if (! fp)
|
2008-03-16 22:30:56 +05:30
|
|
|
return false;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-18 02:57:37 +05:30
|
|
|
while ((rc_getline(&line, &len, fp))) {
|
2008-03-16 22:30:56 +05:30
|
|
|
TAILQ_FOREACH(m, match, entries)
|
|
|
|
if (strcmp(line, m->value) == 0) {
|
|
|
|
TAILQ_REMOVE(match, m, entries);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! TAILQ_FIRST(match))
|
2007-04-11 18:14:47 +05:30
|
|
|
break;
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
fclose(fp);
|
2008-03-18 02:57:37 +05:30
|
|
|
free(line);
|
2008-03-16 22:30:56 +05:30
|
|
|
if (TAILQ_FIRST(match))
|
|
|
|
return false;
|
|
|
|
return true;
|
2008-02-23 05:56:11 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static RC_STRINGLIST *_match_list(const char* const* argv,
|
|
|
|
const char *name, const char *pidfile)
|
2008-02-23 05:56:11 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRINGLIST *match = rc_stringlist_new();
|
2008-02-23 05:56:11 +05:30
|
|
|
int i = 0;
|
|
|
|
size_t l;
|
|
|
|
char *m;
|
|
|
|
|
|
|
|
while (argv && argv[i]) {
|
2008-03-16 22:30:56 +05:30
|
|
|
l = strlen(*argv) + strlen("argv_=") + 16;
|
|
|
|
m = xmalloc(sizeof(char) * l);
|
|
|
|
snprintf(m, l, "argv_0=%s", argv[i++]);
|
|
|
|
rc_stringlist_add(match, m);
|
|
|
|
free(m);
|
2008-02-23 05:56:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (name) {
|
2008-03-16 22:30:56 +05:30
|
|
|
l = strlen(name) + 6;
|
|
|
|
m = xmalloc(sizeof (char) * l);
|
|
|
|
snprintf(m, l, "name=%s", name);
|
|
|
|
rc_stringlist_add(match, m);
|
|
|
|
free(m);
|
2008-02-23 05:56:11 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-02-23 05:56:11 +05:30
|
|
|
if (pidfile) {
|
2008-03-16 22:30:56 +05:30
|
|
|
l = strlen(pidfile) + 9;
|
|
|
|
m = xmalloc(sizeof (char) * l);
|
|
|
|
snprintf(m, l, "pidfile=%s", pidfile);
|
|
|
|
rc_stringlist_add(match, m);
|
2008-02-23 05:56:11 +05:30
|
|
|
free (m);
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return match;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
bool rc_service_daemon_set(const char *service, const char *const *argv,
|
|
|
|
const char *name, const char *pidfile, bool started)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-17 18:55:56 +05:30
|
|
|
char dirpath[PATH_MAX];
|
|
|
|
char file[PATH_MAX];
|
2007-04-11 18:14:47 +05:30
|
|
|
int nfiles = 0;
|
2008-03-17 18:55:56 +05:30
|
|
|
char oldfile[PATH_MAX] = { '\0' };
|
2007-10-05 01:19:12 +05:30
|
|
|
bool retval = false;
|
2007-10-05 15:46:14 +05:30
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRINGLIST *match;
|
2008-02-23 05:56:11 +05:30
|
|
|
int i = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
FILE *fp;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (!(argv && *argv) && ! name && ! pidfile) {
|
2007-10-05 01:19:12 +05:30
|
|
|
errno = EINVAL;
|
2008-03-16 22:30:56 +05:30
|
|
|
return false;
|
2007-10-05 01:19:12 +05:30
|
|
|
}
|
2007-12-19 18:16:08 +05:30
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s",
|
|
|
|
basename_c(service));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Regardless, erase any existing daemon info */
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((dp = opendir(dirpath))) {
|
2008-03-17 18:55:56 +05:30
|
|
|
match = _match_list(argv, name, pidfile);
|
2008-03-16 22:30:56 +05:30
|
|
|
while ((d = readdir(dp))) {
|
2007-10-05 15:46:14 +05:30
|
|
|
if (d->d_name[0] == '.')
|
|
|
|
continue;
|
2008-03-17 18:55:56 +05:30
|
|
|
|
|
|
|
snprintf(file, sizeof(file), "%s/%s",
|
|
|
|
dirpath, d->d_name);
|
2007-10-05 15:46:14 +05:30
|
|
|
nfiles++;
|
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
if (! *oldfile) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (_match_daemon(dirpath, d->d_name, match)) {
|
2008-03-17 18:55:56 +05:30
|
|
|
unlink(file);
|
|
|
|
strlcpy(oldfile, file, sizeof(oldfile));
|
2007-10-05 15:46:14 +05:30
|
|
|
nfiles--;
|
|
|
|
}
|
|
|
|
} else {
|
2008-03-16 22:30:56 +05:30
|
|
|
rename(file, oldfile);
|
2008-03-17 18:55:56 +05:30
|
|
|
strlcpy(oldfile, file, sizeof(oldfile));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
closedir(dp);
|
2008-03-17 18:55:56 +05:30
|
|
|
rc_stringlist_free(match);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Now store our daemon info */
|
|
|
|
if (started) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (mkdir(dirpath, 0755) == 0 || errno == EEXIST) {
|
2008-03-17 18:55:56 +05:30
|
|
|
snprintf(file, sizeof(file), "%s/%03d",
|
|
|
|
dirpath, nfiles + 1);
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((fp = fopen(file, "w"))) {
|
2008-02-23 05:56:11 +05:30
|
|
|
while (argv && argv[i]) {
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(fp, "argv_%d=%s\n", i, argv[i]);
|
2008-02-23 05:56:11 +05:30
|
|
|
i++;
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(fp, "name=");
|
2008-02-23 05:56:11 +05:30
|
|
|
if (name)
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(fp, "%s", name);
|
|
|
|
fprintf(fp, "\npidfile=");
|
2008-02-23 05:56:11 +05:30
|
|
|
if (pidfile)
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(fp, "%s", pidfile);
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fclose(fp);
|
2007-10-05 01:19:12 +05:30
|
|
|
retval = true;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-10-05 01:19:12 +05:30
|
|
|
} else
|
|
|
|
retval = true;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return retval;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-10-04 19:41:45 +05:30
|
|
|
librc_hidden_def(rc_service_daemon_set)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
bool rc_service_started_daemon(const char *service, const char *const *argv,
|
|
|
|
int indx)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-17 18:55:56 +05:30
|
|
|
char dirpath[PATH_MAX];
|
|
|
|
char file[16];
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRINGLIST *match;
|
2007-09-25 23:00:07 +05:30
|
|
|
bool retval = false;
|
2007-10-05 15:46:14 +05:30
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (!service || !(argv && *argv))
|
|
|
|
return false;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s",
|
|
|
|
basename_c(service));
|
2008-03-16 22:30:56 +05:30
|
|
|
match = _match_list(argv, NULL, NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (indx > 0) {
|
2008-03-17 18:55:56 +05:30
|
|
|
snprintf(file, sizeof(file), "%03d", indx);
|
2008-03-16 22:30:56 +05:30
|
|
|
retval = _match_daemon(dirpath, file, match);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else {
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((dp = opendir(dirpath))) {
|
|
|
|
while ((d = readdir(dp))) {
|
2008-01-28 18:46:33 +05:30
|
|
|
if (d->d_name[0] == '.')
|
2007-10-05 15:46:14 +05:30
|
|
|
continue;
|
2008-03-16 22:30:56 +05:30
|
|
|
retval = _match_daemon(dirpath, d->d_name, match);
|
2007-10-05 15:46:14 +05:30
|
|
|
if (retval)
|
|
|
|
break;
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
closedir(dp);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_free(match);
|
|
|
|
return retval;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_service_started_daemon)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
bool rc_service_daemons_crashed(const char *service)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-17 18:55:56 +05:30
|
|
|
char dirpath[PATH_MAX];
|
2007-10-05 15:46:14 +05:30
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
2008-03-17 18:55:56 +05:30
|
|
|
char *path = dirpath;
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
2008-03-18 02:57:37 +05:30
|
|
|
char *line = NULL;
|
|
|
|
size_t len = 0;
|
2008-02-17 20:42:00 +05:30
|
|
|
char **argv = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *exec = NULL;
|
|
|
|
char *name = NULL;
|
|
|
|
char *pidfile = NULL;
|
|
|
|
pid_t pid = 0;
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PIDLIST *pids;
|
|
|
|
RC_PID *p1;
|
|
|
|
RC_PID *p2;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *p;
|
|
|
|
char *token;
|
2007-09-25 23:00:07 +05:30
|
|
|
bool retval = false;
|
2008-03-17 20:01:44 +05:30
|
|
|
RC_STRINGLIST *list = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRING *s;
|
|
|
|
size_t i;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
path += snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s",
|
|
|
|
basename_c(service));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
if (! (dp = opendir(dirpath)))
|
2008-03-16 22:30:56 +05:30
|
|
|
return false;
|
2007-10-05 15:46:14 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
while ((d = readdir(dp))) {
|
2007-10-05 15:46:14 +05:30
|
|
|
if (d->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
snprintf(path, sizeof(dirpath) - (path - dirpath), "/%s",
|
|
|
|
d->d_name);
|
|
|
|
fp = fopen(dirpath, "r");
|
2007-09-29 22:12:08 +05:30
|
|
|
if (! fp)
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-18 02:57:37 +05:30
|
|
|
while ((rc_getline(&line, &len, fp))) {
|
2008-01-07 17:59:30 +05:30
|
|
|
p = line;
|
2008-03-18 02:57:37 +05:30
|
|
|
if ((token = strsep(&p, "=")) == NULL || ! p)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
2008-03-18 02:57:37 +05:30
|
|
|
if (! *p)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strncmp(token, "argv_", 5) == 0) {
|
2008-03-17 20:01:44 +05:30
|
|
|
if (! list)
|
|
|
|
list = rc_stringlist_new();
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_add(list, p);
|
|
|
|
} else if (strcmp(token, "exec") == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if (exec)
|
2008-03-16 22:30:56 +05:30
|
|
|
free(exec);
|
|
|
|
exec = xstrdup(p);
|
|
|
|
} else if (strcmp(token, "name") == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if (name)
|
2008-03-16 22:30:56 +05:30
|
|
|
free(name);
|
|
|
|
name = xstrdup(p);
|
|
|
|
} else if (strcmp(token, "pidfile") == 0) {
|
|
|
|
pidfile = xstrdup(p);
|
2008-03-17 20:01:44 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
fclose(fp);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
pid = 0;
|
|
|
|
if (pidfile) {
|
2008-03-17 20:01:44 +05:30
|
|
|
retval = true;
|
|
|
|
if ((fp = fopen(pidfile, "r"))) {
|
|
|
|
if (fscanf(fp, "%d", &pid) == 1)
|
|
|
|
retval = false;
|
2007-04-11 18:14:47 +05:30
|
|
|
fclose (fp);
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
free(pidfile);
|
2007-04-11 18:14:47 +05:30
|
|
|
pidfile = NULL;
|
2007-08-08 08:37:09 +05:30
|
|
|
|
|
|
|
/* We have the pid, so no need to match on name */
|
2008-04-21 16:26:28 +05:30
|
|
|
free(name);
|
2007-08-08 08:37:09 +05:30
|
|
|
name = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
} else {
|
2008-03-17 20:01:44 +05:30
|
|
|
if (exec) {
|
|
|
|
if (! list)
|
|
|
|
list = rc_stringlist_new();
|
|
|
|
if (! TAILQ_FIRST(list))
|
|
|
|
rc_stringlist_add(list, exec);
|
|
|
|
|
|
|
|
free(exec);
|
|
|
|
exec = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-17 20:01:44 +05:30
|
|
|
if (list) {
|
|
|
|
/* We need to flatten our linked list into an array */
|
|
|
|
i = 0;
|
|
|
|
TAILQ_FOREACH(s, list, entries)
|
|
|
|
i++;
|
|
|
|
argv = xmalloc(sizeof(char *) * (i + 1));
|
|
|
|
i = 0;
|
|
|
|
TAILQ_FOREACH(s, list, entries)
|
|
|
|
argv[i++] = s->value;
|
|
|
|
argv[i] = '\0';
|
2008-03-17 15:29:52 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-17 20:01:44 +05:30
|
|
|
|
|
|
|
if (! retval) {
|
|
|
|
if ((pids = rc_find_pids((const char *const *)argv,
|
|
|
|
name, 0, pid)))
|
|
|
|
{
|
|
|
|
p1 = LIST_FIRST(pids);
|
|
|
|
while (p1) {
|
|
|
|
p2 = LIST_NEXT(p1, entries);
|
|
|
|
free(p1);
|
|
|
|
p1 = p2;
|
|
|
|
}
|
|
|
|
free(pids);
|
|
|
|
} else
|
|
|
|
retval = true;
|
|
|
|
}
|
|
|
|
rc_stringlist_free(list);
|
|
|
|
list = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
free(argv);
|
2008-02-17 20:42:00 +05:30
|
|
|
argv = NULL;
|
2008-03-17 20:01:44 +05:30
|
|
|
free(exec);
|
|
|
|
exec = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
free(name);
|
2007-04-11 18:14:47 +05:30
|
|
|
name = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
if (retval)
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
closedir(dp);
|
2008-03-21 14:01:04 +05:30
|
|
|
free(line);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return retval;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_service_daemons_crashed)
|