2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
start-stop-daemon
|
|
|
|
Starts, stops, tests and signals daemons
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2008-01-14 10:35:22 +05:30
|
|
|
/*
|
2008-02-22 17:37:34 +05:30
|
|
|
* Copyright 2007-2008 Roy Marples
|
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-08-09 20:03:20 +05:30
|
|
|
/* nano seconds */
|
|
|
|
#define POLL_INTERVAL 20000000
|
2007-10-23 14:35:19 +05:30
|
|
|
#define WAIT_PIDFILE 500000000
|
|
|
|
#define START_WAIT 100000000
|
2007-08-09 20:03:20 +05:30
|
|
|
#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 */
|
2007-11-28 21:15:03 +05:30
|
|
|
static struct pam_conv conv = { NULL, NULL};
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
#include "builtins.h"
|
2008-01-06 19:17:39 +05:30
|
|
|
#include "einfo.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
#include "rc.h"
|
2008-01-06 19:17:39 +05:30
|
|
|
#include "rc-misc.h"
|
|
|
|
#include "strlist.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
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;
|
|
|
|
|
2008-02-12 01:44:09 +05:30
|
|
|
extern const 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;
|
2008-01-22 16:37:39 +05:30
|
|
|
const char *s;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-01-21 21:00:40 +05:30
|
|
|
if (! sig || *sig == '\0')
|
2007-04-11 18:14:47 +05:30
|
|
|
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)
|
2008-01-22 16:37:39 +05:30
|
|
|
s = sig + 3;
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
|
|
|
s = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (signallist) / sizeof (signallist[0]); i++)
|
|
|
|
if (strcmp (sig, signallist[i].name) == 0 ||
|
2008-01-11 21:21:40 +05:30
|
|
|
(s && strcmp (s, signallist[i].name) == 0))
|
2007-04-11 18:14:47 +05:30
|
|
|
return (signallist[i].signal);
|
|
|
|
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: `%s' is not a valid signal", applet, sig);
|
2008-01-22 16:37:39 +05:30
|
|
|
/* NOTREACHED */
|
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;
|
2008-01-10 04:52:04 +05:30
|
|
|
else if (isdigit ((int) string[0])) {
|
2007-04-11 18:14:47 +05:30
|
|
|
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,
|
2008-01-11 21:21:40 +05:30
|
|
|
string);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else if ((after_hyph = string + (string[0] == '-')) &&
|
2008-01-11 21:21:40 +05:30
|
|
|
((sig = parse_signal (after_hyph)) != -1))
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
|
|
|
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;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t len;
|
2007-04-11 18:14:47 +05:30
|
|
|
schedulelist_t *next;
|
|
|
|
|
|
|
|
if (string)
|
|
|
|
for (slash = string; *slash; slash++)
|
|
|
|
if (*slash == '/')
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if (schedule)
|
|
|
|
free_schedulelist (&schedule);
|
|
|
|
|
2008-01-30 20:31:37 +05:30
|
|
|
schedule = xmalloc (sizeof (*schedule));
|
2007-04-11 18:14:47 +05:30
|
|
|
schedule->gotolist = NULL;
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
schedule->type = schedule_signal;
|
|
|
|
schedule->value = default_signal;
|
2008-01-30 20:31:37 +05:30
|
|
|
schedule->next = xmalloc (sizeof (*schedule->next));
|
2007-04-11 18:14:47 +05:30
|
|
|
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",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
repeatat = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string) {
|
2008-01-30 20:31:37 +05:30
|
|
|
next->next = xmalloc (sizeof (*next->next));
|
2007-04-11 18:14:47 +05:30
|
|
|
next = next->next;
|
|
|
|
next->gotolist = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repeatat) {
|
2008-01-30 20:31:37 +05:30
|
|
|
next->next = xmalloc (sizeof (*next->next));
|
2007-04-11 18:14:47 +05:30
|
|
|
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 */
|
2008-02-17 20:42:00 +05:30
|
|
|
static int do_stop (const char *const *argv, const char *cmd,
|
2008-01-11 21:21:40 +05:30
|
|
|
const char *pidfile, uid_t uid,int sig,
|
|
|
|
bool quiet, bool verbose, bool test)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-01-14 10:35:22 +05:30
|
|
|
pid_t *pids;
|
2007-04-11 18:14:47 +05:30
|
|
|
bool killed;
|
|
|
|
int nkilled = 0;
|
|
|
|
pid_t pid = 0;
|
|
|
|
int i;
|
|
|
|
|
2007-11-14 19:15:20 +05:30
|
|
|
if (pidfile) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if ((pid = get_pid (pidfile, quiet)) == -1)
|
|
|
|
return (quiet ? 0 : -1);
|
2007-11-14 19:15:20 +05:30
|
|
|
pids = rc_find_pids (NULL, NULL, 0, pid);
|
|
|
|
} else
|
2008-02-17 20:42:00 +05:30
|
|
|
pids = rc_find_pids (argv, cmd, uid, pid);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-11-14 19:15:20 +05:30
|
|
|
if (! pids)
|
2007-04-11 18:14:47 +05:30
|
|
|
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);
|
2007-11-14 19:34:34 +05:30
|
|
|
if (verbose)
|
|
|
|
eend (killed ? 0 : 1, "%s: failed to send signal %d to PID %d: %s",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet, sig, pids[i], strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! killed) {
|
|
|
|
nkilled = -1;
|
|
|
|
} else {
|
|
|
|
if (nkilled != -1)
|
|
|
|
nkilled++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free (pids);
|
|
|
|
return (nkilled);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-02-17 20:42:00 +05:30
|
|
|
static int run_stop_schedule (const char *const *argv, const char *cmd,
|
2008-01-11 21:21:40 +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);
|
2008-02-20 03:15:27 +05:30
|
|
|
if (argv && *argv)
|
2008-02-17 20:42:00 +05:30
|
|
|
einfo ("Will stop processes of `%s'", *argv);
|
2007-04-11 18:14:47 +05:30
|
|
|
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;
|
2008-02-17 20:42:00 +05:30
|
|
|
nkilled = do_stop (argv, cmd, pidfile, uid, item->value,
|
2008-01-11 21:21:40 +05:30
|
|
|
quiet, verbose, test);
|
2007-04-11 18:14:47 +05:30
|
|
|
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) {
|
2008-02-17 20:42:00 +05:30
|
|
|
if ((nrunning = do_stop (argv, cmd, pidfile,
|
2008-01-11 21:21:40 +05:30
|
|
|
uid, 0, true, false, true)) == 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
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-10-23 20:53:10 +05:30
|
|
|
eerror ("%s: caught an interrupt", 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 status;
|
|
|
|
int serrno = errno;
|
|
|
|
char signame[10] = { '\0' };
|
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGINT");
|
2008-01-22 16:37:39 +05:30
|
|
|
/* FALLTHROUGH */
|
2007-04-11 18:14:47 +05:30
|
|
|
case SIGTERM:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGTERM");
|
2008-01-22 16:37:39 +05:30
|
|
|
/* FALLTHROUGH */
|
2007-04-11 18:14:47 +05:30
|
|
|
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);
|
2008-01-22 16:37:39 +05:30
|
|
|
/* NOTREACHED */
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
case SIGCHLD:
|
2008-01-22 16:37:39 +05:30
|
|
|
for (;;) {
|
|
|
|
if (waitpid (-1, &status, WNOHANG) < 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
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-26 04:47:25 +05:30
|
|
|
#define getoptstring "KN:R:Sbc:d:g:mn:op:s:tu:r:x:1:2:" getoptstring_COMMON
|
2008-02-02 01:24:46 +05:30
|
|
|
static const struct option longopts[] = {
|
2007-08-17 16:31:18 +05:30
|
|
|
{ "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'},
|
2008-02-17 20:42:00 +05:30
|
|
|
{ "env", 1, NULL, 'e'},
|
2007-08-17 16:31:18 +05:30
|
|
|
{ "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'},
|
|
|
|
{ "exec", 1, NULL, 'x'},
|
|
|
|
{ "stdout", 1, NULL, '1'},
|
|
|
|
{ "stderr", 1, NULL, '2'},
|
|
|
|
longopts_COMMON
|
|
|
|
};
|
2007-09-25 21:51:38 +05:30
|
|
|
static const char * const longopts_help[] = {
|
2007-09-26 16:16:44 +05:30
|
|
|
"Stop daemon",
|
|
|
|
"Set a nicelevel when starting",
|
|
|
|
"Retry schedule to use when stopping",
|
|
|
|
"Start daemon",
|
|
|
|
"deprecated, use --exec",
|
|
|
|
"Force daemon to background",
|
|
|
|
"deprecated, use --user",
|
|
|
|
"Change the PWD",
|
2008-02-17 20:42:00 +05:30
|
|
|
"Set an environment string",
|
2007-09-26 16:16:44 +05:30
|
|
|
"Change the process group",
|
|
|
|
"Create a pidfile",
|
|
|
|
"Match process name",
|
|
|
|
"deprecated",
|
|
|
|
"Match pid found in this file",
|
|
|
|
"Send a different signal",
|
|
|
|
"Test actions, don't do them",
|
|
|
|
"Change the process user",
|
|
|
|
"Chroot to this directory",
|
|
|
|
"Binary to start/stop",
|
|
|
|
"Redirect stdout to file",
|
|
|
|
"Redirect stderr to file",
|
2007-09-25 21:51:38 +05:30
|
|
|
longopts_help_COMMON
|
|
|
|
};
|
2007-08-17 16:31:18 +05:30
|
|
|
#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;
|
2008-02-17 20:42:00 +05:30
|
|
|
bool sethome = false;
|
|
|
|
bool setuser = false;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
atexit (cleanup);
|
|
|
|
|
2008-02-01 18:50:19 +05:30
|
|
|
signal_setup (SIGINT, handle_signal);
|
|
|
|
signal_setup (SIGQUIT, handle_signal);
|
|
|
|
signal_setup (SIGTERM, handle_signal);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
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
|
|
|
|
2008-02-17 20:42:00 +05:30
|
|
|
while ((opt = getopt_long (argc, argv, "e:" getoptstring, longopts,
|
2008-01-11 21:21:40 +05:30
|
|
|
(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-10-08 16:46:22 +05:30
|
|
|
changeuser = xstrdup (cu);
|
2007-04-13 12:13:00 +05:30
|
|
|
if (sscanf (cu, "%d", &tid) != 1)
|
|
|
|
pw = getpwnam (cu);
|
|
|
|
else
|
2008-01-22 16:37:39 +05:30
|
|
|
pw = getpwuid ((uid_t) tid);
|
2007-04-13 12:13:00 +05:30
|
|
|
|
|
|
|
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
|
2008-01-22 16:37:39 +05:30
|
|
|
gr = getgrgid ((gid_t) tid);
|
2007-04-13 12:13:00 +05:30
|
|
|
|
|
|
|
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;
|
|
|
|
|
2008-02-17 20:42:00 +05:30
|
|
|
case 'e': /* --env */
|
|
|
|
if (putenv (optarg) == 0) {
|
|
|
|
if (strncmp ("HOME=", optarg, 5) == 0)
|
|
|
|
sethome = true;
|
|
|
|
else if (strncmp ("USER=", optarg, 5) == 0)
|
|
|
|
setuser = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
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
|
2008-01-22 16:37:39 +05:30
|
|
|
gr = getgrgid ((gid_t) tid);
|
2007-04-13 12:13:00 +05:30
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
2008-01-11 21:21:40 +05:30
|
|
|
case_RC_COMMON_GETOPT
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2007-12-06 16:18:00 +05:30
|
|
|
quiet = rc_yesno (getenv ("EINFO_QUIET"));
|
|
|
|
verbose = rc_yesno (getenv ("EINFO_VERBOSE"));
|
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 &&
|
2008-01-11 21:21:40 +05:30
|
|
|
sig != SIGTERM &&
|
|
|
|
sig != SIGQUIT &&
|
|
|
|
sig != SIGKILL)
|
2007-04-13 14:51:16 +05:30
|
|
|
{
|
|
|
|
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",
|
2008-01-11 21:21:40 +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 */
|
2008-02-25 04:39:15 +05:30
|
|
|
if (exec) {
|
2007-04-11 18:14:47 +05:30
|
|
|
char *tmp;
|
|
|
|
if (ch_root)
|
|
|
|
tmp = rc_strcatpaths (ch_root, exec, (char *) NULL);
|
|
|
|
else
|
|
|
|
tmp = exec;
|
2008-02-25 04:39:15 +05:30
|
|
|
if (start && ! exists (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);
|
|
|
|
}
|
2008-02-25 04:39:15 +05:30
|
|
|
|
|
|
|
/* If we don't have a pidfile or name, check it's not
|
|
|
|
* interpreted, otherwise we should fail */
|
|
|
|
if (! pidfile && ! cmd) {
|
|
|
|
char line[130];
|
|
|
|
FILE *fp = fopen (tmp, "r");
|
|
|
|
|
|
|
|
if (fp) {
|
|
|
|
fgets (line, sizeof (line), fp);
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
if (line[0] == '#' && line[1] == '!') {
|
|
|
|
size_t len = strlen (line) - 1;
|
|
|
|
|
|
|
|
/* Remove the trailing newline */
|
|
|
|
if (line[len] == '\n')
|
|
|
|
line[len] = '\0';
|
|
|
|
|
|
|
|
eerror ("%s: %s is a script",
|
|
|
|
applet, exec);
|
|
|
|
eerror ("%s: and should be started, stopped or signalled with ",
|
|
|
|
applet);
|
|
|
|
eerror ("%s: --exec %s %s",
|
|
|
|
applet, line + 2, exec);
|
|
|
|
eerror ("%s: or you should specify a pidfile or process name",
|
|
|
|
applet);
|
|
|
|
if (ch_root)
|
|
|
|
free (tmp);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ch_root)
|
|
|
|
free (tmp);
|
|
|
|
}
|
|
|
|
|
2008-02-17 20:42:00 +05:30
|
|
|
/* Add exec to our arguments */
|
|
|
|
*--argv = exec;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (stop) {
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (! schedule) {
|
|
|
|
if (test || oknodo)
|
|
|
|
parse_schedule ("0", sig);
|
|
|
|
else
|
|
|
|
parse_schedule (NULL, sig);
|
|
|
|
}
|
|
|
|
|
2008-02-17 20:42:00 +05:30
|
|
|
result = run_stop_schedule ((const char *const *)argv, cmd,
|
|
|
|
pidfile, uid, quiet, verbose, test);
|
2008-02-19 15:48:26 +05:30
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
/* We failed to stop something */
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (test || oknodo)
|
|
|
|
return (result > 0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
|
2008-02-19 15:48:26 +05:30
|
|
|
/* Even if we have not actually killed anything, we should
|
|
|
|
* remove information about it as it may have unexpectedly
|
|
|
|
* crashed out. We should also return success as the end
|
|
|
|
* result would be the same. */
|
2007-10-08 16:41:21 +05:30
|
|
|
if (pidfile && exists (pidfile))
|
2007-04-11 18:14:47 +05:30
|
|
|
unlink (pidfile);
|
|
|
|
|
|
|
|
if (svcname)
|
2008-02-17 20:42:00 +05:30
|
|
|
rc_service_daemon_set (svcname,
|
|
|
|
(const char *const *)argv,
|
|
|
|
cmd, pidfile, false);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2008-02-17 20:42:00 +05:30
|
|
|
if (do_stop ((const char * const *)argv, 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);
|
|
|
|
|
2008-02-17 20:42:00 +05:30
|
|
|
einfon ("Would start");
|
|
|
|
while (argc-- >= 0)
|
|
|
|
printf(" %s", *argv++);
|
2007-04-11 18:14:47 +05:30
|
|
|
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)
|
2008-02-01 18:50:19 +05:30
|
|
|
signal_setup (SIGCHLD, handle_signal);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
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,
|
2008-01-11 21:21:40 +05:30
|
|
|
strerror(errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
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
|
2008-01-11 21:21:40 +05:30
|
|
|
(errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
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) {
|
2008-02-17 20:42:00 +05:30
|
|
|
if (! sethome) {
|
|
|
|
unsetenv ("HOME");
|
|
|
|
if (passwd->pw_dir)
|
|
|
|
setenv ("HOME", passwd->pw_dir, 1);
|
|
|
|
}
|
|
|
|
if (! setuser) {
|
|
|
|
unsetenv ("USER");
|
|
|
|
if (passwd->pw_name)
|
|
|
|
setenv ("USER", passwd->pw_name, 1);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2007-09-24 17:39:43 +05:30
|
|
|
STRLIST_FOREACH (environ, env, i) {
|
|
|
|
if (strncmp (env, "RC_", 3) == 0 ||
|
2008-01-11 21:21:40 +05:30
|
|
|
strncmp (env, "SSD_NICELEVEL=", strlen ("SSD_NICELEVEL=")) == 0)
|
2007-09-24 17:39:43 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/* For the path, remove the rcscript bin dir from it */
|
|
|
|
if (strncmp (env, "PATH=", 5) == 0) {
|
2007-10-08 16:46:22 +05:30
|
|
|
char *path = xstrdup (env);
|
2007-09-24 17:39:43 +05:30
|
|
|
char *newpath = NULL;
|
|
|
|
char *p = path;
|
|
|
|
char *token;
|
2007-10-24 02:13:45 +05:30
|
|
|
char *np;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t l;
|
2007-11-28 19:10:15 +05:30
|
|
|
int t;
|
2007-09-24 17:39:43 +05:30
|
|
|
|
|
|
|
p += 5;
|
|
|
|
while ((token = strsep (&p, ":"))) {
|
|
|
|
if (strcmp (token, RC_LIBDIR "/bin") == 0 ||
|
2008-01-11 21:21:40 +05:30
|
|
|
strcmp (token, RC_LIBDIR "/sbin") == 0)
|
2007-09-24 17:39:43 +05:30
|
|
|
continue;
|
2008-01-11 21:21:40 +05:30
|
|
|
|
2007-11-28 19:10:15 +05:30
|
|
|
t = strlen (token);
|
2007-10-24 02:13:45 +05:30
|
|
|
if (newpath) {
|
|
|
|
l = strlen (newpath);
|
2007-11-28 19:10:15 +05:30
|
|
|
newpath = xrealloc (newpath, sizeof (char) * (l + t + 2));
|
2007-10-24 02:13:45 +05:30
|
|
|
np = newpath + l;
|
|
|
|
*np++ = ':';
|
|
|
|
memcpy (np, token, sizeof (char) * strlen (token));
|
2007-11-28 19:10:15 +05:30
|
|
|
np += t;
|
2008-01-14 10:35:22 +05:30
|
|
|
*np = '\0';
|
2007-10-24 02:13:45 +05:30
|
|
|
} else {
|
2007-11-28 19:10:15 +05:30
|
|
|
l = strlen ("PATH=") + t + 1;
|
2007-10-24 02:13:45 +05:30
|
|
|
newpath = xmalloc (sizeof (char) * l);
|
|
|
|
snprintf (newpath, l, "PATH=%s", token);
|
|
|
|
}
|
2007-09-24 17:39:43 +05:30
|
|
|
}
|
|
|
|
rc_strlist_add (&newenv, newpath);
|
|
|
|
free (path);
|
|
|
|
free (newpath);
|
|
|
|
} else
|
|
|
|
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,
|
2008-01-11 21:21:40 +05:30
|
|
|
S_IRUSR | S_IWUSR)) == -1)
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: unable to open the logfile for stdout `%s': %s",
|
2008-01-11 21:21:40 +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,
|
2008-01-11 21:21:40 +05:30
|
|
|
S_IRUSR | S_IWUSR)) == -1)
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: unable to open the logfile for stderr `%s': %s",
|
2008-01-11 21:21:40 +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;
|
2007-10-23 14:35:19 +05:30
|
|
|
int nloopsp = WAIT_PIDFILE / POLL_INTERVAL;
|
2007-08-09 20:03:20 +05:30
|
|
|
bool alive = false;
|
2008-01-11 21:21:40 +05:30
|
|
|
|
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-10-23 20:53:10 +05:30
|
|
|
eerror ("%s: caught an interrupt", 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-10-23 14:35:19 +05:30
|
|
|
|
|
|
|
/* We wait for a specific amount of time for a pidfile to be
|
|
|
|
* created. Once everything is in place we then wait some more
|
|
|
|
* to ensure that the daemon really is running and won't abort due
|
|
|
|
* to a config error. */
|
|
|
|
if (! background && pidfile && nloopsp)
|
|
|
|
nloopsp --;
|
|
|
|
else
|
|
|
|
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 */
|
2007-12-03 19:08:23 +05:30
|
|
|
if (get_pid (pidfile, true) == -1) {
|
|
|
|
if (! nloopsp)
|
|
|
|
eerrorx ("%s: did not create a valid pid in `%s'",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet, pidfile);
|
2007-04-11 18:14:47 +05:30
|
|
|
alive = true;
|
2007-12-03 19:08:23 +05:30
|
|
|
} else
|
2007-10-23 14:35:19 +05:30
|
|
|
nloopsp = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-02-17 20:42:00 +05:30
|
|
|
if (do_stop ((const char *const *)argv, cmd,
|
|
|
|
pidfile, uid, 0, true, false, true) > 0)
|
2007-11-14 19:15:20 +05:30
|
|
|
alive = true;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (! alive)
|
2007-08-17 16:31:18 +05:30
|
|
|
eerrorx ("%s: %s died", applet, exec);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (svcname)
|
2008-02-17 20:42:00 +05:30
|
|
|
rc_service_daemon_set (svcname, (const char *const *)argv, cmd, pidfile, true);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
2008-01-22 16:37:39 +05:30
|
|
|
/* NOTREACHED */
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|