openrc/src/librc-daemon.c

539 lines
11 KiB
C
Raw Normal View History

/*
librc-daemon
Finds PID for given daemon criteria
Copyright 2007 Gentoo Foundation
Released under the GPLv2
*/
#include "librc.h"
#if defined(__linux__)
static bool pid_is_cmd (pid_t pid, const char *cmd)
{
2007-04-11 18:14:47 +05:30
char buffer[32];
FILE *fp;
int c;
2007-04-11 18:14:47 +05:30
snprintf(buffer, sizeof (buffer), "/proc/%d/stat", pid);
if ((fp = fopen (buffer, "r")) == NULL)
return (false);
2007-04-11 18:14:47 +05:30
while ((c = getc (fp)) != EOF && c != '(')
;
2007-04-11 18:14:47 +05:30
if (c != '(') {
fclose(fp);
return (false);
}
2007-04-11 18:14:47 +05:30
while ((c = getc (fp)) != EOF && c == *cmd)
cmd++;
2007-04-11 18:14:47 +05:30
fclose (fp);
2007-04-11 18:14:47 +05:30
return ((c == ')' && *cmd == '\0') ? true : false);
}
static bool pid_is_exec (pid_t pid, const char *exec)
{
2007-04-11 18:14:47 +05:30
char cmdline[32];
char buffer[PATH_MAX];
char *p;
int fd = -1;
int r;
snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid);
memset (buffer, 0, sizeof (buffer));
if (readlink (cmdline, buffer, sizeof (buffer)) != -1) {
if (strcmp (exec, buffer) == 0)
return (true);
/* We should cater for deleted binaries too */
if (strlen (buffer) > 10) {
p = buffer + (strlen (buffer) - 10);
if (strcmp (p, " (deleted)") == 0) {
*p = 0;
if (strcmp (buffer, exec) == 0)
return (true);
}
}
}
snprintf (cmdline, sizeof (cmdline), "/proc/%u/cmdline", pid);
if ((fd = open (cmdline, O_RDONLY)) < 0)
return (false);
r = read(fd, buffer, sizeof (buffer));
close (fd);
if (r == -1)
return 0;
buffer[r] = 0;
return (strcmp (exec, buffer) == 0 ? true : false);
}
pid_t *rc_find_pids (const char *exec, const char *cmd,
2007-04-11 18:14:47 +05:30
uid_t uid, pid_t pid)
{
2007-04-11 18:14:47 +05:30
DIR *procdir;
struct dirent *entry;
int npids = 0;
pid_t p;
pid_t *pids = NULL;
pid_t *tmp = NULL;
2007-04-11 18:14:47 +05:30
char buffer[PATH_MAX];
struct stat sb;
pid_t runscript_pid = 0;
char *pp;
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
*/
if ((pp = getenv ("RC_RUNSCRIPT_PID"))) {
if (sscanf (pp, "%d", &runscript_pid) != 1)
runscript_pid = 0;
}
while ((entry = readdir (procdir)) != NULL) {
if (sscanf (entry->d_name, "%d", &p) != 1)
continue;
if (runscript_pid != 0 && runscript_pid == p)
continue;
if (pid != 0 && pid != p)
continue;
if (uid) {
snprintf (buffer, sizeof (buffer), "/proc/%d", p);
2007-04-11 18:14:47 +05:30
if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
continue;
}
if (cmd && ! pid_is_cmd (p, cmd))
continue;
if (exec && ! cmd && ! pid_is_exec (p, exec))
continue;
tmp = realloc (pids, sizeof (pid_t) * (npids + 2));
if (! tmp) {
free (pids);
closedir (procdir);
errno = ENOMEM;
return (NULL);
}
pids = tmp;
2007-04-11 18:14:47 +05:30
pids[npids] = p;
pids[npids + 1] = 0;
npids++;
}
closedir (procdir);
return (pids);
}
librc_hidden_def(rc_find_pids)
#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
2007-10-04 19:41:45 +05:30
defined(__NetBSD__) || defined(__OpenBSD__)
# if defined(__DragonFly__) || defined(__FreeBSD__)
# ifndef KERN_PROC_PROC
# define KERN_PROC_PROC KERN_PROC_ALL
# endif
# 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)
# else
# 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)
# endif
pid_t *rc_find_pids (const char *exec, const char *cmd,
2007-04-11 18:14:47 +05:30
uid_t uid, pid_t pid)
{
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;
int argc = 0;
char **argv;
pid_t *pids = NULL;
pid_t *tmp;
2007-04-11 18:14:47 +05:30
int npids = 0;
2007-10-01 14:12:27 +05:30
if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) {
fprintf (stderr, "kvm_open: %s", errbuf);
return (NULL);
}
#if defined(__DragonFly__) || defined( __FreeBSD__)
2007-04-11 18:14:47 +05:30
kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes);
#else
2007-04-11 18:14:47 +05:30
kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
&processes);
#endif
2007-04-11 18:14:47 +05:30
for (i = 0; i < processes; i++) {
pid_t p = _GET_KINFO_PID (kp[i]);
if (pid != 0 && pid != p)
continue;
if (uid != 0 && uid != _GET_KINFO_UID (kp[i]))
continue;
if (cmd) {
if (! _GET_KINFO_COMM (kp[i]) ||
strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0)
continue;
}
if (exec && ! cmd) {
if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv)
continue;
if (strcmp (*argv, exec) != 0)
continue;
}
tmp = realloc (pids, sizeof (pid_t) * (npids + 2));
if (! tmp) {
free (pids);
kvm_close (kd);
errno = ENOMEM;
return (NULL);
}
pids = tmp;
2007-04-11 18:14:47 +05:30
pids[npids] = p;
pids[npids + 1] = 0;
npids++;
}
kvm_close (kd);
2007-04-11 18:14:47 +05:30
return (pids);
}
librc_hidden_def(rc_find_pids)
#else
# error "Platform not supported!"
#endif
2007-09-25 23:00:07 +05:30
static bool _match_daemon (const char *path, const char *file,
const char *mexec, const char *mname,
const char *mpidfile)
{
2007-04-11 18:14:47 +05:30
char buffer[RC_LINEBUFFER];
char *ffile = rc_strcatpaths (path, file, (char *) NULL);
FILE *fp;
int lc = 0;
int m = 0;
if ((fp = fopen (ffile, "r")) == NULL) {
free (ffile);
2007-09-25 23:00:07 +05:30
return (false);
2007-04-11 18:14:47 +05:30
}
if (! mname)
m += 10;
if (! mpidfile)
m += 100;
memset (buffer, 0, sizeof (buffer));
while ((fgets (buffer, RC_LINEBUFFER, fp))) {
int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n')
buffer[lb] = 0;
if (strcmp (buffer, mexec) == 0)
m += 1;
else if (mname && strcmp (buffer, mname) == 0)
m += 10;
else if (mpidfile && strcmp (buffer, mpidfile) == 0)
m += 100;
if (m == 111)
break;
lc++;
if (lc > 5)
break;
}
fclose (fp);
free (ffile);
2007-09-25 23:00:07 +05:30
return (m == 111 ? true : false);
}
2007-10-04 19:41:45 +05:30
void rc_service_daemon_set (const char *service, const char *exec,
const char *name, const char *pidfile,
bool started)
{
2007-04-13 21:30:25 +05:30
char *svc = rc_xstrdup (service);
char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
2007-04-11 18:14:47 +05:30
(char *) NULL);
char **files = NULL;
char *file;
char *ffile = NULL;
int i;
char *mexec;
char *mname;
char *mpidfile;
int nfiles = 0;
2007-04-13 21:30:25 +05:30
free (svc);
2007-04-11 18:14:47 +05:30
if (! exec && ! name && ! pidfile)
2007-09-25 23:00:07 +05:30
return;
2007-04-11 18:14:47 +05:30
if (exec) {
i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec);
} else
2007-04-19 20:24:35 +05:30
mexec = rc_xstrdup ("exec=");
2007-04-11 18:14:47 +05:30
if (name) {
i = strlen (name) + 6;
mname = rc_xmalloc (sizeof (char *) * i);
snprintf (mname, i, "name=%s", name);
} else
2007-04-19 20:24:35 +05:30
mname = rc_xstrdup ("name=");
2007-04-11 18:14:47 +05:30
if (pidfile) {
i = strlen (pidfile) + 9;
mpidfile = rc_xmalloc (sizeof (char *) * i);
snprintf (mpidfile, i, "pidfile=%s", pidfile);
} else
2007-04-19 20:24:35 +05:30
mpidfile = rc_xstrdup ("pidfile=");
2007-04-11 18:14:47 +05:30
/* Regardless, erase any existing daemon info */
2007-09-25 23:00:07 +05:30
if (rc_is_dir (dirpath)) {
2007-04-11 18:14:47 +05:30
char *oldfile = NULL;
files = rc_ls_dir (dirpath, 0);
2007-04-11 18:14:47 +05:30
STRLIST_FOREACH (files, file, i) {
ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
nfiles++;
if (! oldfile) {
2007-09-25 23:00:07 +05:30
if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) {
2007-04-11 18:14:47 +05:30
unlink (ffile);
oldfile = ffile;
nfiles--;
}
} else {
rename (ffile, oldfile);
free (oldfile);
oldfile = ffile;
}
}
2007-09-24 14:37:00 +05:30
free (ffile);
rc_strlist_free (files);
2007-04-11 18:14:47 +05:30
}
/* Now store our daemon info */
if (started) {
char buffer[10];
FILE *fp;
if (mkdir (dirpath, 0755) == 0 || errno == EEXIST) {
snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
if ((fp = fopen (file, "w")))
fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
2007-04-11 18:14:47 +05:30
fclose (fp);
free (file);
2007-04-11 18:14:47 +05:30
}
}
free (mexec);
free (mname);
free (mpidfile);
free (dirpath);
}
2007-10-04 19:41:45 +05:30
librc_hidden_def(rc_service_daemon_set)
2007-09-25 23:00:07 +05:30
bool rc_service_started_daemon (const char *service, const char *exec,
int indx)
{
2007-04-11 18:14:47 +05:30
char *dirpath;
char *file;
int i;
char *mexec;
2007-09-25 23:00:07 +05:30
bool retval = false;
2007-04-13 21:30:25 +05:30
char *svc;
2007-04-11 18:14:47 +05:30
if (! service || ! exec)
2007-09-25 23:00:07 +05:30
return (false);
2007-04-11 18:14:47 +05:30
2007-04-13 21:30:25 +05:30
svc = rc_xstrdup (service);
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
2007-04-11 18:14:47 +05:30
(char *) NULL);
2007-04-13 21:30:25 +05:30
free (svc);
2007-09-25 23:00:07 +05:30
if (! rc_is_dir (dirpath)) {
2007-04-11 18:14:47 +05:30
free (dirpath);
2007-09-25 23:00:07 +05:30
return (false);
2007-04-11 18:14:47 +05:30
}
i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec);
if (indx > 0) {
int len = sizeof (char *) * 10;
file = rc_xmalloc (len);
snprintf (file, len, "%03d", indx);
retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
free (file);
} else {
char **files = rc_ls_dir (dirpath, 0);
2007-04-11 18:14:47 +05:30
STRLIST_FOREACH (files, file, i) {
retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
2007-09-25 23:00:07 +05:30
if (retval)
2007-04-11 18:14:47 +05:30
break;
}
2007-09-24 14:37:00 +05:30
rc_strlist_free (files);
2007-04-11 18:14:47 +05:30
}
free (mexec);
return (retval);
}
librc_hidden_def(rc_service_started_daemon)
2007-09-25 23:00:07 +05:30
bool rc_service_daemons_crashed (const char *service)
{
2007-04-11 18:14:47 +05:30
char *dirpath;
char **files;
char *file;
char *path;
int i;
FILE *fp;
char buffer[RC_LINEBUFFER];
char *exec = NULL;
char *name = NULL;
char *pidfile = NULL;
pid_t pid = 0;
pid_t *pids = NULL;
char *p;
char *token;
2007-09-25 23:00:07 +05:30
bool retval = false;
2007-04-13 21:30:25 +05:30
char *svc;
2007-04-11 18:14:47 +05:30
if (! service)
2007-09-25 23:00:07 +05:30
return (false);
2007-04-11 18:14:47 +05:30
2007-04-13 21:30:25 +05:30
svc = rc_xstrdup (service);
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
2007-04-11 18:14:47 +05:30
(char *) NULL);
2007-04-13 21:30:25 +05:30
free (svc);
2007-09-25 23:00:07 +05:30
if (! rc_is_dir (dirpath)) {
2007-04-11 18:14:47 +05:30
free (dirpath);
2007-09-25 23:00:07 +05:30
return (false);
2007-04-11 18:14:47 +05:30
}
memset (buffer, 0, sizeof (buffer));
files = rc_ls_dir (dirpath, 0);
2007-04-11 18:14:47 +05:30
STRLIST_FOREACH (files, file, i) {
path = rc_strcatpaths (dirpath, file, (char *) NULL);
fp = fopen (path, "r");
free (path);
if (! fp)
break;
2007-04-11 18:14:47 +05:30
while ((fgets (buffer, RC_LINEBUFFER, fp))) {
int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n')
buffer[lb] = 0;
p = buffer;
if ((token = strsep (&p, "=")) == NULL || ! p)
continue;
if (strlen (p) == 0)
continue;
if (strcmp (token, "exec") == 0) {
if (exec)
free (exec);
2007-04-19 20:24:35 +05:30
exec = rc_xstrdup (p);
2007-04-11 18:14:47 +05:30
} else if (strcmp (token, "name") == 0) {
if (name)
free (name);
2007-04-19 20:24:35 +05:30
name = rc_xstrdup (p);
2007-04-11 18:14:47 +05:30
} else if (strcmp (token, "pidfile") == 0) {
if (pidfile)
free (pidfile);
2007-04-19 20:24:35 +05:30
pidfile = rc_xstrdup (p);
2007-04-11 18:14:47 +05:30
}
}
fclose (fp);
pid = 0;
if (pidfile) {
2007-09-25 23:00:07 +05:30
if (! rc_exists (pidfile)) {
retval = true;
2007-04-11 18:14:47 +05:30
break;
}
if ((fp = fopen (pidfile, "r")) == NULL) {
2007-09-25 23:00:07 +05:30
retval = true;
2007-04-11 18:14:47 +05:30
break;
}
if (fscanf (fp, "%d", &pid) != 1) {
fclose (fp);
2007-09-25 23:00:07 +05:30
retval = true;
2007-04-11 18:14:47 +05:30
break;
}
fclose (fp);
free (pidfile);
pidfile = NULL;
/* We have the pid, so no need to match on name */
free (exec);
exec = NULL;
free (name);
name = NULL;
2007-04-11 18:14:47 +05:30
}
if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) {
2007-09-25 23:00:07 +05:30
retval = true;
2007-04-11 18:14:47 +05:30
break;
}
free (pids);
free (exec);
exec = NULL;
free (name);
name = NULL;
}
free (exec);
free (name);
2007-04-11 18:14:47 +05:30
free (dirpath);
rc_strlist_free (files);
return (retval);
}
librc_hidden_def(rc_service_daemons_crashed)