2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
start-stop-daemon
|
|
|
|
Starts, stops, tests and signals daemons
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
Released under the GPLv2
|
|
|
|
|
|
|
|
This is essentially a ground up re-write of Debians
|
|
|
|
start-stop-daemon for cleaner code and to integrate into our RC
|
|
|
|
system so we can monitor daemons a little.
|
|
|
|
*/
|
|
|
|
|
2007-08-17 16:31:18 +05:30
|
|
|
#define APPLET "start-stop-daemon"
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
/* nano seconds */
|
|
|
|
#define POLL_INTERVAL 20000000
|
|
|
|
#define START_WAIT 100000000
|
|
|
|
#define ONE_SECOND 1000000000
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-05 20:31:09 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/termios.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-05-16 16:35:42 +05:30
|
|
|
#include <time.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_PAM
|
|
|
|
#include <security/pam_appl.h>
|
|
|
|
|
|
|
|
/* We are not supporting authentication conversations */
|
|
|
|
static struct pam_conv conv = { NULL, NULL} ;
|
|
|
|
#endif
|
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
#include "builtins.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
#include "einfo.h"
|
|
|
|
#include "rc.h"
|
|
|
|
#include "rc-misc.h"
|
|
|
|
#include "strlist.h"
|
|
|
|
|
|
|
|
typedef struct schedulelist
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
enum
|
|
|
|
{
|
|
|
|
schedule_timeout,
|
|
|
|
schedule_signal,
|
|
|
|
schedule_goto,
|
|
|
|
schedule_forever
|
|
|
|
} type;
|
|
|
|
int value;
|
|
|
|
struct schedulelist *gotolist;
|
|
|
|
struct schedulelist *next;
|
2007-04-05 16:48:42 +05:30
|
|
|
} schedulelist_t;
|
|
|
|
static schedulelist_t *schedule;
|
|
|
|
|
2007-08-17 16:31:18 +05:30
|
|
|
static char *applet;
|
2007-04-05 16:48:42 +05:30
|
|
|
static char *changeuser;
|
|
|
|
static char **newenv;
|
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
static void free_schedulelist (schedulelist_t **list)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
schedulelist_t *here;
|
|
|
|
schedulelist_t *next;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
for (here = *list; here; here = next) {
|
|
|
|
next = here->next;
|
|
|
|
free (here);
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
*list = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup (void)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
if (changeuser)
|
|
|
|
free (changeuser);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (schedule)
|
|
|
|
free_schedulelist (&schedule);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (newenv)
|
|
|
|
rc_strlist_free (newenv);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_signal (const char *sig)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
typedef struct signalpair
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
int signal;
|
|
|
|
} signalpair_t;
|
|
|
|
|
|
|
|
static const signalpair_t signallist[] = {
|
|
|
|
{ "ABRT", SIGABRT },
|
|
|
|
{ "ALRM", SIGALRM },
|
|
|
|
{ "FPE", SIGFPE },
|
|
|
|
{ "HUP", SIGHUP },
|
|
|
|
{ "ILL", SIGILL },
|
|
|
|
{ "INT", SIGINT },
|
|
|
|
{ "KILL", SIGKILL },
|
|
|
|
{ "PIPE", SIGPIPE },
|
|
|
|
{ "QUIT", SIGQUIT },
|
|
|
|
{ "SEGV", SIGSEGV },
|
|
|
|
{ "TERM", SIGTERM },
|
|
|
|
{ "USR1", SIGUSR1 },
|
|
|
|
{ "USR2", SIGUSR2 },
|
|
|
|
{ "CHLD", SIGCHLD },
|
|
|
|
{ "CONT", SIGCONT },
|
|
|
|
{ "STOP", SIGSTOP },
|
|
|
|
{ "TSTP", SIGTSTP },
|
|
|
|
{ "TTIN", SIGTTIN },
|
|
|
|
{ "TTOU", SIGTTOU }
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int i = 0;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (! sig || strlen (sig) == 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (sscanf (sig, "%u", &i) == 1) {
|
|
|
|
if (i > 0 && i < sizeof (signallist) / sizeof (signallist[0]))
|
|
|
|
return (i);
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: `%s' is not a valid signal", applet, sig);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp (sig, "SIG", 3) == 0)
|
|
|
|
s = (char *) sig + 3;
|
|
|
|
else
|
|
|
|
s = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (signallist) / sizeof (signallist[0]); i++)
|
|
|
|
if (strcmp (sig, signallist[i].name) == 0 ||
|
|
|
|
(s && strcmp (s, signallist[i].name) == 0))
|
|
|
|
return (signallist[i].signal);
|
|
|
|
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: `%s' is not a valid signal", applet, sig);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_schedule_item (schedulelist_t *item, const char *string)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
const char *after_hyph;
|
|
|
|
int sig;
|
|
|
|
|
|
|
|
if (strcmp (string,"forever") == 0)
|
|
|
|
item->type = schedule_forever;
|
|
|
|
else if (isdigit (string[0])) {
|
|
|
|
item->type = schedule_timeout;
|
|
|
|
errno = 0;
|
|
|
|
if (sscanf (string, "%d", &item->value) != 1)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: invalid timeout value in schedule `%s'", applet,
|
2007-04-11 18:14:47 +05:30
|
|
|
string);
|
|
|
|
} else if ((after_hyph = string + (string[0] == '-')) &&
|
|
|
|
((sig = parse_signal (after_hyph)) != -1))
|
|
|
|
{
|
|
|
|
item->type = schedule_signal;
|
|
|
|
item->value = (int) sig;
|
|
|
|
}
|
|
|
|
else
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: invalid schedule item `%s'", applet, string);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_schedule (const char *string, int default_signal)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char buffer[20];
|
|
|
|
const char *slash;
|
|
|
|
int count = 0;
|
|
|
|
schedulelist_t *repeatat = NULL;
|
|
|
|
ptrdiff_t len;
|
|
|
|
schedulelist_t *next;
|
|
|
|
|
|
|
|
if (string)
|
|
|
|
for (slash = string; *slash; slash++)
|
|
|
|
if (*slash == '/')
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if (schedule)
|
|
|
|
free_schedulelist (&schedule);
|
|
|
|
|
|
|
|
schedule = rc_xmalloc (sizeof (schedulelist_t));
|
|
|
|
schedule->gotolist = NULL;
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
schedule->type = schedule_signal;
|
|
|
|
schedule->value = default_signal;
|
|
|
|
schedule->next = rc_xmalloc (sizeof (schedulelist_t));
|
|
|
|
next = schedule->next;
|
|
|
|
next->type = schedule_timeout;
|
|
|
|
next->gotolist = NULL;
|
|
|
|
if (string) {
|
|
|
|
if (sscanf (string, "%d", &next->value) != 1)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: invalid timeout value in schedule", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
next->value = 5;
|
|
|
|
next->next = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next = schedule;
|
|
|
|
while (string != NULL) {
|
|
|
|
if ((slash = strchr (string, '/')))
|
|
|
|
len = slash - string;
|
|
|
|
else
|
|
|
|
len = strlen (string);
|
|
|
|
|
|
|
|
if (len >= (ptrdiff_t) sizeof (buffer))
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: invalid schedule item, far too long", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
memcpy (buffer, string, len);
|
|
|
|
buffer[len] = 0;
|
|
|
|
string = slash ? slash + 1 : NULL;
|
|
|
|
|
|
|
|
parse_schedule_item (next, buffer);
|
|
|
|
if (next->type == schedule_forever) {
|
|
|
|
if (repeatat)
|
|
|
|
eerrorx ("%s: invalid schedule, `forever' appears more than once",
|
2007-08-17 16:31:18 +05:30
|
|
|
applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
repeatat = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string) {
|
|
|
|
next->next = rc_xmalloc (sizeof (schedulelist_t));
|
|
|
|
next = next->next;
|
|
|
|
next->gotolist = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repeatat) {
|
|
|
|
next->next = rc_xmalloc (sizeof (schedulelist_t));
|
|
|
|
next = next->next;
|
|
|
|
next->type = schedule_goto;
|
|
|
|
next->value = 0;
|
|
|
|
next->gotolist = repeatat;
|
|
|
|
}
|
|
|
|
|
|
|
|
next->next = NULL;
|
|
|
|
return;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static pid_t get_pid (const char *pidfile, bool quiet)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if (! pidfile)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if ((fp = fopen (pidfile, "r")) == NULL) {
|
|
|
|
if (! quiet)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: fopen `%s': %s", applet, pidfile, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fscanf (fp, "%d", &pid) != 1) {
|
|
|
|
if (! quiet)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: no pid found in `%s'", applet, pidfile);
|
2007-04-11 18:14:47 +05:30
|
|
|
fclose (fp);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return (pid);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* return number of processed killed, -1 on error */
|
|
|
|
static int do_stop (const char *exec, const char *cmd,
|
2007-04-11 18:14:47 +05:30
|
|
|
const char *pidfile, uid_t uid,int sig,
|
|
|
|
bool quiet, bool verbose, bool test)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
pid_t *pids;
|
|
|
|
bool killed;
|
|
|
|
int nkilled = 0;
|
|
|
|
pid_t pid = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pidfile)
|
|
|
|
if ((pid = get_pid (pidfile, quiet)) == -1)
|
|
|
|
return (quiet ? 0 : -1);
|
|
|
|
|
|
|
|
if ((pids = rc_find_pids (exec, cmd, uid, pid)) == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
for (i = 0; pids[i]; i++) {
|
|
|
|
if (test) {
|
|
|
|
if (! quiet)
|
|
|
|
einfo ("Would send signal %d to PID %d", sig, pids[i]);
|
|
|
|
nkilled++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
ebegin ("Sending signal %d to PID %d", sig, pids[i]);
|
|
|
|
errno = 0;
|
|
|
|
killed = (kill (pids[i], sig) == 0 || errno == ESRCH ? true : false);
|
|
|
|
if (! killed) {
|
|
|
|
if (! quiet)
|
|
|
|
eerror ("%s: failed to send signal %d to PID %d: %s",
|
2007-08-17 16:31:18 +05:30
|
|
|
applet, sig, pids[i], strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
if (verbose)
|
|
|
|
eend (1, NULL);
|
|
|
|
nkilled = -1;
|
|
|
|
} else {
|
|
|
|
if (verbose)
|
|
|
|
eend (0, NULL);
|
|
|
|
if (nkilled != -1)
|
|
|
|
nkilled++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free (pids);
|
|
|
|
return (nkilled);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int run_stop_schedule (const char *exec, const char *cmd,
|
2007-04-11 18:14:47 +05:30
|
|
|
const char *pidfile, uid_t uid,
|
|
|
|
bool quiet, bool verbose, bool test)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
schedulelist_t *item = schedule;
|
|
|
|
int nkilled = 0;
|
|
|
|
int tkilled = 0;
|
|
|
|
int nrunning = 0;
|
2007-08-09 20:03:20 +05:30
|
|
|
long nloops;
|
|
|
|
struct timespec ts;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
if (pidfile)
|
|
|
|
einfo ("Will stop PID in pidfile `%s'", pidfile);
|
|
|
|
if (uid)
|
|
|
|
einfo ("Will stop processes owned by UID %d", uid);
|
|
|
|
if (exec)
|
|
|
|
einfo ("Will stop processes of `%s'", exec);
|
|
|
|
if (cmd)
|
|
|
|
einfo ("Will stop processes called `%s'", cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (item) {
|
|
|
|
switch (item->type) {
|
|
|
|
case schedule_goto:
|
|
|
|
item = item->gotolist;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case schedule_signal:
|
|
|
|
nrunning = 0;
|
|
|
|
nkilled = do_stop (exec, cmd, pidfile, uid, item->value,
|
|
|
|
quiet, verbose, test);
|
|
|
|
if (nkilled == 0) {
|
|
|
|
if (tkilled == 0) {
|
|
|
|
if (! quiet)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: no matching processes found", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
return (tkilled);
|
|
|
|
}
|
|
|
|
else if (nkilled == -1)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
tkilled += nkilled;
|
|
|
|
break;
|
|
|
|
case schedule_timeout:
|
|
|
|
if (item->value < 1) {
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
nloops = (ONE_SECOND / POLL_INTERVAL) * item->value;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = POLL_INTERVAL;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
while (nloops) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if ((nrunning = do_stop (exec, cmd, pidfile,
|
|
|
|
uid, 0, true, false, true)) == 0)
|
|
|
|
return (true);
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
if (nanosleep (&ts, NULL) == -1) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if (errno == EINTR)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: caught an interupt", applet);
|
2007-04-25 23:42:49 +05:30
|
|
|
else {
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: nanosleep: %s", applet, strerror (errno));
|
2007-04-25 23:42:49 +05:30
|
|
|
return (0);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-08-09 20:03:20 +05:30
|
|
|
nloops --;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: invalid schedule item `%d'", applet, item->type);
|
2007-04-11 18:14:47 +05:30
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item)
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test || (tkilled > 0 && nrunning == 0))
|
|
|
|
return (nkilled);
|
|
|
|
|
|
|
|
if (! quiet) {
|
|
|
|
if (nrunning == 1)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: %d process refused to stop", applet, nrunning);
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: %d process(es) refused to stop", applet, nrunning);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return (-nrunning);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_signal (int sig)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int pid;
|
|
|
|
int status;
|
|
|
|
int serrno = errno;
|
|
|
|
char signame[10] = { '\0' };
|
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGINT");
|
|
|
|
case SIGTERM:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGTERM");
|
|
|
|
case SIGQUIT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGQUIT");
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: caught %s, aborting", applet, signame);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
case SIGCHLD:
|
|
|
|
while (1) {
|
|
|
|
if ((pid = waitpid (-1, &status, WNOHANG)) < 0) {
|
|
|
|
if (errno != ECHILD)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: waitpid: %s", applet, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: caught unknown signal %d", applet, sig);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore errno */
|
|
|
|
errno = serrno;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-08-17 16:31:18 +05:30
|
|
|
|
|
|
|
#include "_usage.h"
|
2007-09-19 21:57:37 +05:30
|
|
|
#define getoptstring "KN:R:Sbc:d:g:mn:op:s:tu:r:vx:1:2:" getoptstring_COMMON
|
2007-08-17 16:31:18 +05:30
|
|
|
static struct option longopts[] = {
|
|
|
|
{ "stop", 0, NULL, 'K'},
|
|
|
|
{ "nicelevel", 1, NULL, 'N'},
|
|
|
|
{ "retry", 1, NULL, 'R'},
|
|
|
|
{ "start", 0, NULL, 'S'},
|
|
|
|
{ "startas", 1, NULL, 'a'},
|
|
|
|
{ "background", 0, NULL, 'b'},
|
|
|
|
{ "chuid", 1, NULL, 'c'},
|
|
|
|
{ "chdir", 1, NULL, 'd'},
|
|
|
|
{ "group", 1, NULL, 'g'},
|
|
|
|
{ "make-pidfile", 0, NULL, 'm'},
|
|
|
|
{ "name", 1, NULL, 'n'},
|
|
|
|
{ "oknodo", 0, NULL, 'o'},
|
|
|
|
{ "pidfile", 1, NULL, 'p'},
|
|
|
|
{ "signal", 1, NULL, 's'},
|
|
|
|
{ "test", 0, NULL, 't'},
|
|
|
|
{ "user", 1, NULL, 'u'},
|
|
|
|
{ "chroot", 1, NULL, 'r'},
|
|
|
|
{ "verbose", 0, NULL, 'v'},
|
|
|
|
{ "exec", 1, NULL, 'x'},
|
|
|
|
{ "stdout", 1, NULL, '1'},
|
|
|
|
{ "stderr", 1, NULL, '2'},
|
|
|
|
longopts_COMMON
|
|
|
|
{ NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
#include "_usage.c"
|
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
int start_stop_daemon (int argc, char **argv)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int devnull_fd = -1;
|
2007-04-05 16:48:42 +05:30
|
|
|
#ifdef TIOCNOTTY
|
2007-04-11 18:14:47 +05:30
|
|
|
int tty_fd = -1;
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
2007-08-17 16:31:18 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#ifdef HAVE_PAM
|
2007-04-11 18:14:47 +05:30
|
|
|
pam_handle_t *pamh = NULL;
|
|
|
|
int pamr;
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-05-14 17:54:18 +05:30
|
|
|
int opt;
|
2007-04-11 18:14:47 +05:30
|
|
|
bool start = false;
|
|
|
|
bool stop = false;
|
|
|
|
bool oknodo = false;
|
|
|
|
bool test = false;
|
2007-09-19 21:57:37 +05:30
|
|
|
bool quiet;
|
2007-04-11 18:14:47 +05:30
|
|
|
bool verbose = false;
|
|
|
|
char *exec = NULL;
|
|
|
|
char *cmd = NULL;
|
|
|
|
char *pidfile = NULL;
|
|
|
|
int sig = SIGTERM;
|
|
|
|
int nicelevel = 0;
|
|
|
|
bool background = false;
|
|
|
|
bool makepidfile = false;
|
2007-08-17 16:24:04 +05:30
|
|
|
uid_t uid = 0;
|
|
|
|
gid_t gid = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *ch_root = NULL;
|
|
|
|
char *ch_dir = NULL;
|
|
|
|
int tid = 0;
|
|
|
|
char *redirect_stderr = NULL;
|
|
|
|
char *redirect_stdout = NULL;
|
|
|
|
int stdout_fd;
|
|
|
|
int stderr_fd;
|
|
|
|
pid_t pid;
|
|
|
|
int i;
|
|
|
|
char *svcname = getenv ("SVCNAME");
|
|
|
|
char *env;
|
|
|
|
|
2007-08-17 16:31:18 +05:30
|
|
|
applet = argv[0];
|
2007-04-11 18:14:47 +05:30
|
|
|
atexit (cleanup);
|
|
|
|
|
|
|
|
signal (SIGINT, handle_signal);
|
|
|
|
signal (SIGQUIT, handle_signal);
|
|
|
|
signal (SIGTERM, handle_signal);
|
|
|
|
|
2007-05-02 18:03:56 +05:30
|
|
|
if ((env = getenv ("SSD_NICELEVEL")))
|
|
|
|
if (sscanf (env, "%d", &nicelevel) != 1)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: invalid nice level `%s' (SSD_NICELEVEL)", applet, env);
|
2007-05-02 18:03:56 +05:30
|
|
|
|
2007-08-17 16:31:18 +05:30
|
|
|
while ((opt = getopt_long (argc, argv, getoptstring, longopts,
|
|
|
|
(int *) 0)) != -1)
|
2007-05-14 17:54:18 +05:30
|
|
|
switch (opt) {
|
2007-04-11 18:14:47 +05:30
|
|
|
case 'K': /* --stop */
|
|
|
|
stop = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N': /* --nice */
|
|
|
|
if (sscanf (optarg, "%d", &nicelevel) != 1)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: invalid nice level `%s'", applet, optarg);
|
2007-04-11 18:14:47 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case 'R': /* --retry <schedule>|<timeout> */
|
|
|
|
parse_schedule (optarg, sig);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S': /* --start */
|
|
|
|
start = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b': /* --background */
|
|
|
|
background = true;
|
|
|
|
break;
|
|
|
|
|
2007-08-20 14:38:06 +05:30
|
|
|
case 'u': /* --user <username>|<uid> */
|
2007-04-11 18:14:47 +05:30
|
|
|
case 'c': /* --chuid <username>|<uid> */
|
|
|
|
{
|
|
|
|
char *p = optarg;
|
|
|
|
char *cu = strsep (&p, ":");
|
2007-04-13 12:13:00 +05:30
|
|
|
struct passwd *pw = NULL;
|
|
|
|
|
2007-04-19 20:24:35 +05:30
|
|
|
changeuser = rc_xstrdup (cu);
|
2007-04-13 12:13:00 +05:30
|
|
|
if (sscanf (cu, "%d", &tid) != 1)
|
|
|
|
pw = getpwnam (cu);
|
|
|
|
else
|
|
|
|
pw = getpwuid (tid);
|
|
|
|
|
|
|
|
if (! pw)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: user `%s' not found", applet, cu);
|
2007-08-17 16:24:04 +05:30
|
|
|
uid = pw->pw_uid;
|
|
|
|
if (! gid)
|
|
|
|
gid = pw->pw_gid;
|
2007-04-13 12:13:00 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (p) {
|
2007-04-13 12:13:00 +05:30
|
|
|
struct group *gr = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *cg = strsep (&p, ":");
|
2007-04-13 12:13:00 +05:30
|
|
|
|
|
|
|
if (sscanf (cg, "%d", &tid) != 1)
|
|
|
|
gr = getgrnam (cg);
|
|
|
|
else
|
|
|
|
gr = getgrgid (tid);
|
|
|
|
|
|
|
|
if (! gr)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: group `%s' not found", applet, cg);
|
2007-08-17 16:24:04 +05:30
|
|
|
gid = gr->gr_gid;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd': /* --chdir /new/dir */
|
|
|
|
ch_dir = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g': /* --group <group>|<gid> */
|
2007-04-13 12:13:00 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct group *gr = getgrnam (optarg);
|
2007-04-13 12:13:00 +05:30
|
|
|
|
|
|
|
if (sscanf (optarg, "%d", &tid) != 1)
|
|
|
|
gr = getgrnam (optarg);
|
|
|
|
else
|
|
|
|
gr = getgrgid (tid);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! gr)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: group `%s' not found", applet, optarg);
|
2007-08-17 16:24:04 +05:30
|
|
|
gid = gr->gr_gid;
|
2007-04-13 12:13:00 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm': /* --make-pidfile */
|
|
|
|
makepidfile = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n': /* --name <process-name> */
|
|
|
|
cmd = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o': /* --oknodo */
|
|
|
|
oknodo = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p': /* --pidfile <pid-file> */
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's': /* --signal <signal> */
|
|
|
|
sig = parse_signal (optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't': /* --test */
|
|
|
|
test = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r': /* --chroot /new/root */
|
|
|
|
ch_root = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* --verbose */
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
|
2007-05-14 18:07:13 +05:30
|
|
|
case 'a':
|
2007-04-11 18:14:47 +05:30
|
|
|
case 'x': /* --exec <executable> */
|
|
|
|
exec = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '1': /* --stdout /path/to/stdout.lgfile */
|
|
|
|
redirect_stdout = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '2': /* --stderr /path/to/stderr.logfile */
|
|
|
|
redirect_stderr = optarg;
|
|
|
|
break;
|
|
|
|
|
2007-08-17 16:31:18 +05:30
|
|
|
case_RC_COMMON_GETOPT
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2007-09-19 21:57:37 +05:30
|
|
|
quiet = rc_is_env ("RC_QUIET", "yes");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-13 14:51:16 +05:30
|
|
|
/* Allow start-stop-daemon --signal HUP --exec /usr/sbin/dnsmasq
|
|
|
|
* instead of forcing --stop --oknodo as well */
|
|
|
|
if (! start && ! stop)
|
|
|
|
if (sig != SIGINT &&
|
|
|
|
sig != SIGTERM &&
|
|
|
|
sig != SIGQUIT &&
|
|
|
|
sig != SIGKILL)
|
|
|
|
{
|
|
|
|
oknodo = true;
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (start == stop)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: need one of --start or --stop", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (start && ! exec)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: --start needs --exec", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (stop && ! exec && ! pidfile && ! cmd && ! uid)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: --stop needs --exec, --pidfile, --name or --user", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (makepidfile && ! pidfile)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: --make-pidfile is only relevant with --pidfile", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (background && ! start)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: --background is only relevant with --start", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if ((redirect_stdout || redirect_stderr) && ! background)
|
|
|
|
eerrorx ("%s: --stdout and --stderr are only relevant with --background",
|
2007-08-17 16:31:18 +05:30
|
|
|
applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
/* Validate that the binary exists if we are starting */
|
2007-04-11 18:14:47 +05:30
|
|
|
if (exec && start) {
|
|
|
|
char *tmp;
|
|
|
|
if (ch_root)
|
|
|
|
tmp = rc_strcatpaths (ch_root, exec, (char *) NULL);
|
|
|
|
else
|
|
|
|
tmp = exec;
|
|
|
|
if (! rc_is_file (tmp)) {
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: %s does not exist", applet, tmp);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ch_root)
|
|
|
|
free (tmp);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (ch_root)
|
|
|
|
free (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop) {
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (! schedule) {
|
|
|
|
if (test || oknodo)
|
|
|
|
parse_schedule ("0", sig);
|
|
|
|
else
|
|
|
|
parse_schedule (NULL, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = run_stop_schedule (exec, cmd, pidfile, uid, quiet, verbose, test);
|
|
|
|
if (test || oknodo)
|
|
|
|
return (result > 0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
if (result < 1)
|
|
|
|
exit (result == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
|
|
|
|
if (pidfile && rc_is_file (pidfile))
|
|
|
|
unlink (pidfile);
|
|
|
|
|
|
|
|
if (svcname)
|
|
|
|
rc_set_service_daemon (svcname, exec, cmd, pidfile, false);
|
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_stop (exec, cmd, pidfile, uid, 0, true, false, true) > 0)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: %s is already running", applet, exec);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (test) {
|
|
|
|
if (quiet)
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
|
|
|
|
einfon ("Would start %s", exec);
|
|
|
|
while (argc-- > 0)
|
|
|
|
printf("%s ", *argv++);
|
|
|
|
printf ("\n");
|
|
|
|
eindent ();
|
2007-08-17 16:24:04 +05:30
|
|
|
if (uid != 0)
|
|
|
|
einfo ("as user id %d", uid);
|
|
|
|
if (gid != 0)
|
|
|
|
einfo ("as group id %d", gid);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ch_root)
|
|
|
|
einfo ("in root `%s'", ch_root);
|
|
|
|
if (ch_dir)
|
|
|
|
einfo ("in dir `%s'", ch_dir);
|
|
|
|
if (nicelevel != 0)
|
|
|
|
einfo ("with a priority of %d", nicelevel);
|
|
|
|
eoutdent ();
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure this is unset, so if the daemon does /etc/init.d/foo
|
|
|
|
Then we filter the environment accordingly */
|
|
|
|
unsetenv ("RC_SOFTLEVEL");
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
ebegin ("Detaching to start `%s'", exec);
|
|
|
|
eindent ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (background)
|
|
|
|
signal (SIGCHLD, handle_signal);
|
|
|
|
|
|
|
|
*--argv = exec;
|
|
|
|
if ((pid = fork ()) == -1)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: fork: %s", applet, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Child process - lets go! */
|
|
|
|
if (pid == 0) {
|
|
|
|
pid_t mypid = getpid ();
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef TIOCNOTTY
|
2007-04-11 18:14:47 +05:30
|
|
|
tty_fd = open("/dev/tty", O_RDWR);
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
devnull_fd = open("/dev/null", O_RDWR);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (nicelevel) {
|
|
|
|
if (setpriority (PRIO_PROCESS, mypid, nicelevel) == -1)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: setpritory %d: %s", applet, nicelevel,
|
2007-04-11 18:14:47 +05:30
|
|
|
strerror(errno));
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ch_root && chroot (ch_root) < 0)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: chroot `%s': %s", applet, ch_root, strerror (errno));
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ch_dir && chdir (ch_dir) < 0)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: chdir `%s': %s", applet, ch_dir, strerror (errno));
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (makepidfile && pidfile) {
|
|
|
|
FILE *fp = fopen (pidfile, "w");
|
|
|
|
if (! fp)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: fopen `%s': %s", applet, pidfile, strerror
|
2007-04-11 18:14:47 +05:30
|
|
|
(errno));
|
|
|
|
fprintf (fp, "%d\n", mypid);
|
|
|
|
fclose (fp);
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef HAVE_PAM
|
2007-04-11 18:14:47 +05:30
|
|
|
if (changeuser != NULL)
|
|
|
|
pamr = pam_start ("start-stop-daemon", changeuser, &conv, &pamh);
|
|
|
|
else
|
|
|
|
pamr = pam_start ("start-stop-daemon", "nobody", &conv, &pamh);
|
|
|
|
|
|
|
|
if (pamr == PAM_SUCCESS)
|
|
|
|
pamr = pam_authenticate (pamh, PAM_SILENT);
|
|
|
|
if (pamr == PAM_SUCCESS)
|
|
|
|
pamr = pam_acct_mgmt (pamh, PAM_SILENT);
|
|
|
|
if (pamr == PAM_SUCCESS)
|
|
|
|
pamr = pam_open_session (pamh, PAM_SILENT);
|
|
|
|
if (pamr != PAM_SUCCESS)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: pam error: %s", applet, pam_strerror(pamh, pamr));
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-08-17 16:24:04 +05:30
|
|
|
if (gid && setgid (gid))
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: unable to set groupid to %d", applet, gid);
|
2007-08-17 16:24:04 +05:30
|
|
|
if (changeuser && initgroups (changeuser, gid))
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: initgroups (%s, %d)", applet, changeuser, gid);
|
2007-08-17 16:24:04 +05:30
|
|
|
if (uid && setuid (uid))
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: unable to set userid to %d", applet, uid);
|
2007-04-11 18:14:47 +05:30
|
|
|
else {
|
2007-08-17 16:24:04 +05:30
|
|
|
struct passwd *passwd = getpwuid (uid);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (passwd) {
|
|
|
|
unsetenv ("HOME");
|
|
|
|
if (passwd->pw_dir)
|
|
|
|
setenv ("HOME", passwd->pw_dir, 1);
|
|
|
|
unsetenv ("USER");
|
|
|
|
if (passwd->pw_name)
|
|
|
|
setenv ("USER", passwd->pw_name, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close any fd's to the passwd database */
|
|
|
|
endpwent ();
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef TIOCNOTTY
|
2007-04-11 18:14:47 +05:30
|
|
|
ioctl(tty_fd, TIOCNOTTY, 0);
|
|
|
|
close(tty_fd);
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Clean the environment of any RC_ variables */
|
|
|
|
STRLIST_FOREACH (environ, env, i)
|
|
|
|
if (env && strncmp (env, "RC_", 3) != 0) {
|
2007-08-09 20:03:20 +05:30
|
|
|
/* For the path r, remove the rcscript bin dir from it */
|
|
|
|
if (strncmp (env, "PATH=" RC_LIBDIR "/bin:",
|
|
|
|
strlen ("PATH=" RC_LIBDIR "/bin:")) == 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
|
|
|
char *path = env;
|
|
|
|
char *newpath;
|
|
|
|
int len;
|
2007-08-09 20:03:20 +05:30
|
|
|
path += strlen ("PATH=" RC_LIBDIR "/bin:");
|
2007-04-11 18:14:47 +05:30
|
|
|
len = sizeof (char *) * strlen (path) + 6;
|
|
|
|
newpath = rc_xmalloc (len);
|
|
|
|
snprintf (newpath, len, "PATH=%s", path);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&newenv, newpath);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (newpath);
|
|
|
|
} else
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&newenv, env);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
umask (022);
|
|
|
|
|
|
|
|
stdout_fd = devnull_fd;
|
|
|
|
stderr_fd = devnull_fd;
|
|
|
|
if (redirect_stdout) {
|
|
|
|
if ((stdout_fd = open (redirect_stdout, O_WRONLY | O_CREAT | O_APPEND,
|
|
|
|
S_IRUSR | S_IWUSR)) == -1)
|
|
|
|
eerrorx ("%s: unable to open the logfile for stdout `%s': %s",
|
2007-08-17 16:31:18 +05:30
|
|
|
applet, redirect_stdout, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
if (redirect_stderr) {
|
|
|
|
if ((stderr_fd = open (redirect_stderr, O_WRONLY | O_CREAT | O_APPEND,
|
|
|
|
S_IRUSR | S_IWUSR)) == -1)
|
|
|
|
eerrorx ("%s: unable to open the logfile for stderr `%s': %s",
|
2007-08-17 16:31:18 +05:30
|
|
|
applet, redirect_stderr, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (background) {
|
|
|
|
/* Hmmm, some daemons may need stdin? */
|
|
|
|
dup2 (devnull_fd, STDIN_FILENO);
|
|
|
|
dup2 (stdout_fd, STDOUT_FILENO);
|
|
|
|
dup2 (stderr_fd, STDERR_FILENO);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = getdtablesize () - 1; i >= 3; --i)
|
|
|
|
close(i);
|
|
|
|
|
|
|
|
setsid ();
|
|
|
|
|
|
|
|
execve (exec, argv, newenv);
|
2007-04-05 16:48:42 +05:30
|
|
|
#ifdef HAVE_PAM
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pamr == PAM_SUCCESS)
|
|
|
|
pam_close_session (pamh, PAM_SILENT);
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: failed to exec `%s': %s", applet, exec, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent process */
|
|
|
|
if (! background) {
|
|
|
|
/* As we're not backgrounding the process, wait for our pid to return */
|
|
|
|
int status = 0;
|
|
|
|
int savepid = pid;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
do {
|
|
|
|
pid = waitpid (savepid, &status, 0);
|
|
|
|
if (pid < 1) {
|
|
|
|
eerror ("waitpid %d: %s", savepid, strerror (errno));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} while (! WIFEXITED (status) && ! WIFSIGNALED (status));
|
|
|
|
|
|
|
|
if (! WIFEXITED (status) || WEXITSTATUS (status) != 0) {
|
|
|
|
if (! quiet)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: failed to start `%s'", applet, exec);
|
2007-04-11 18:14:47 +05:30
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid = savepid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a little bit and check that process is still running
|
|
|
|
We do this as some badly written daemons fork and then barf */
|
|
|
|
if (START_WAIT > 0) {
|
2007-08-09 20:03:20 +05:30
|
|
|
struct timespec ts;
|
|
|
|
int nloops = START_WAIT / POLL_INTERVAL;
|
|
|
|
bool alive = false;
|
2007-05-14 17:54:18 +05:30
|
|
|
bool retestpid = false;
|
2007-08-09 20:03:20 +05:30
|
|
|
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = POLL_INTERVAL;
|
|
|
|
|
|
|
|
while (nloops) {
|
|
|
|
if (nanosleep (&ts, NULL) == -1) {
|
|
|
|
if (errno == EINTR)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: caught an interupt", applet);
|
2007-08-09 20:03:20 +05:30
|
|
|
else {
|
2007-08-17 16:31:18 +05:30
|
|
|
eerror ("%s: nanosleep: %s", applet, strerror (errno));
|
2007-08-09 20:03:20 +05:30
|
|
|
return (0);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-08-09 20:03:20 +05:30
|
|
|
nloops --;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* This is knarly.
|
|
|
|
If we backgrounded then we know the exact pid.
|
|
|
|
Otherwise if we have a pidfile then it *may* know the exact pid.
|
|
|
|
Failing that, we'll have to query processes.
|
|
|
|
We sleep first as some programs like ntp like to fork, and write
|
|
|
|
their pidfile a LONG time later. */
|
|
|
|
if (background) {
|
|
|
|
if (kill (pid, 0) == 0)
|
|
|
|
alive = true;
|
|
|
|
} else {
|
2007-05-14 17:54:18 +05:30
|
|
|
if (pidfile) {
|
|
|
|
/* The pidfile may not have been written yet - give it some time */
|
|
|
|
if (get_pid (pidfile, true) == -1) {
|
2007-04-11 18:14:47 +05:30
|
|
|
alive = true;
|
2007-05-14 17:54:18 +05:30
|
|
|
retestpid = true;
|
|
|
|
} else {
|
|
|
|
retestpid = false;
|
|
|
|
if (do_stop (NULL, NULL, pidfile, uid, 0,
|
|
|
|
true, false, true) > 0)
|
|
|
|
alive = true;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
} else {
|
2007-05-14 17:54:18 +05:30
|
|
|
if (do_stop (exec, cmd, NULL, uid, 0, true, false, true)
|
|
|
|
> 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
alive = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! alive)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: %s died", applet, exec);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-05-14 17:54:18 +05:30
|
|
|
|
|
|
|
if (retestpid) {
|
|
|
|
if (do_stop (NULL, NULL, pidfile, uid, 0, true,
|
|
|
|
false, true) < 1)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: %s died", applet, exec);
|
2007-05-14 17:54:18 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (svcname)
|
|
|
|
rc_set_service_daemon (svcname, exec, cmd, pidfile, true);
|
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|