2007-04-05 16:48:42 +05:30
|
|
|
/*
|
2009-04-24 03:01:22 +05:30
|
|
|
start-stop-daemon
|
|
|
|
Starts, stops, tests and signals daemons
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
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-04-05 16:48:42 +05:30
|
|
|
|
2008-01-14 10:35:22 +05:30
|
|
|
/*
|
2009-05-01 19:41:40 +05:30
|
|
|
* Copyright (c) 2007-2009 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-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
|
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>
|
2008-03-16 22:30:56 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
2008-03-17 18:55:56 +05:30
|
|
|
#include <limits.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#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"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-17 15:29:52 +05:30
|
|
|
/* Some libc implementations don't define this */
|
|
|
|
#ifndef LIST_FOREACH_SAFE
|
2009-04-24 03:01:22 +05:30
|
|
|
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
|
|
|
|
for ((var) = LIST_FIRST((head)); \
|
|
|
|
(var) && ((tvar) = LIST_NEXT((var), field), 1); \
|
2008-03-17 15:29:52 +05:30
|
|
|
(var) = (tvar))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
typedef struct scheduleitem
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
enum
|
2009-04-24 03:01:22 +05:30
|
|
|
{
|
|
|
|
SC_TIMEOUT,
|
|
|
|
SC_SIGNAL,
|
|
|
|
SC_GOTO,
|
|
|
|
SC_FOREVER
|
|
|
|
} type;
|
2007-04-11 18:14:47 +05:30
|
|
|
int value;
|
2008-03-16 22:30:56 +05:30
|
|
|
struct scheduleitem *gotoitem;
|
2008-07-08 16:18:21 +05:30
|
|
|
TAILQ_ENTRY(scheduleitem) entries;
|
2008-03-16 22:30:56 +05:30
|
|
|
} SCHEDULEITEM;
|
2008-07-08 16:18:21 +05:30
|
|
|
TAILQ_HEAD(, scheduleitem) schedule;
|
2008-10-30 23:10:48 +05:30
|
|
|
static char **nav;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-02-12 01:44:09 +05:30
|
|
|
extern const char *applet;
|
2008-10-30 23:10:48 +05:30
|
|
|
static char *changeuser, *ch_root, *ch_dir;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static void
|
|
|
|
free_schedulelist(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-07-08 16:18:21 +05:30
|
|
|
SCHEDULEITEM *s1 = TAILQ_FIRST(&schedule);
|
2008-03-16 22:30:56 +05:30
|
|
|
SCHEDULEITEM *s2;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
while (s1) {
|
2008-07-08 16:18:21 +05:30
|
|
|
s2 = TAILQ_NEXT(s1, entries);
|
2008-03-16 22:30:56 +05:30
|
|
|
free(s1);
|
|
|
|
s1 = s2;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-07-08 16:18:21 +05:30
|
|
|
TAILQ_INIT(&schedule);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-06 20:32:32 +05:30
|
|
|
#ifdef DEBUG_MEMORY
|
2009-04-24 03:01:22 +05:30
|
|
|
static void
|
|
|
|
cleanup(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-09-17 22:26:23 +05:30
|
|
|
free(changeuser);
|
|
|
|
free(nav);
|
2008-03-16 22:30:56 +05:30
|
|
|
free_schedulelist();
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2008-10-06 20:32:32 +05:30
|
|
|
#endif
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static int
|
|
|
|
parse_signal(const char *sig)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
typedef struct signalpair
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
int signal;
|
2008-03-16 22:30:56 +05:30
|
|
|
} SIGNALPAIR;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static const SIGNALPAIR signallist[] = {
|
2007-04-11 18:14:47 +05:30
|
|
|
{ "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 },
|
2009-09-01 18:27:45 +05:30
|
|
|
{ "TTOU", SIGTTOU },
|
|
|
|
{ "NULL", 0 },
|
2007-04-11 18:14:47 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int i = 0;
|
2008-01-22 16:37:39 +05:30
|
|
|
const char *s;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!sig || *sig == '\0')
|
2008-03-16 22:30:56 +05:30
|
|
|
return -1;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (sscanf(sig, "%u", &i) == 1) {
|
2009-09-01 18:27:45 +05:30
|
|
|
if (i >= 0 && i < NSIG)
|
2008-03-16 22:30:56 +05:30
|
|
|
return i;
|
|
|
|
eerrorx("%s: `%s' is not a valid signal", applet, sig);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +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;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
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-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +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
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static SCHEDULEITEM *
|
|
|
|
parse_schedule_item(const char *string)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
const char *after_hyph;
|
|
|
|
int sig;
|
2008-03-16 22:30:56 +05:30
|
|
|
SCHEDULEITEM *item = xmalloc(sizeof(*item));
|
|
|
|
|
|
|
|
item->value = 0;
|
|
|
|
item->gotoitem = NULL;
|
|
|
|
if (strcmp(string,"forever") == 0)
|
|
|
|
item->type = SC_FOREVER;
|
2008-04-17 15:49:58 +05:30
|
|
|
else if (isdigit((unsigned char)string[0])) {
|
2008-03-16 22:30:56 +05:30
|
|
|
item->type = SC_TIMEOUT;
|
2007-04-11 18:14:47 +05:30
|
|
|
errno = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
if (sscanf(string, "%d", &item->value) != 1)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: invalid timeout value in schedule `%s'",
|
|
|
|
applet, string);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else if ((after_hyph = string + (string[0] == '-')) &&
|
2009-04-24 03:01:22 +05:30
|
|
|
((sig = parse_signal(after_hyph)) != -1))
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
item->type = SC_SIGNAL;
|
|
|
|
item->value = (int)sig;
|
2009-04-24 03:01:22 +05:30
|
|
|
} else
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("%s: invalid schedule item `%s'", applet, string);
|
|
|
|
|
|
|
|
return item;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static void
|
|
|
|
parse_schedule(const char *string, int timeout)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char buffer[20];
|
|
|
|
const char *slash;
|
|
|
|
int count = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
SCHEDULEITEM *repeatat = NULL;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t len;
|
2008-03-16 22:30:56 +05:30
|
|
|
SCHEDULEITEM *item;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (string)
|
|
|
|
for (slash = string; *slash; slash++)
|
|
|
|
if (*slash == '/')
|
|
|
|
count++;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
free_schedulelist();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (count == 0) {
|
2008-03-16 22:30:56 +05:30
|
|
|
item = xmalloc(sizeof(*item));
|
|
|
|
item->type = SC_SIGNAL;
|
|
|
|
item->value = timeout;
|
|
|
|
item->gotoitem = NULL;
|
2008-07-08 16:18:21 +05:30
|
|
|
TAILQ_INSERT_TAIL(&schedule, item, entries);
|
2008-03-16 22:30:56 +05:30
|
|
|
|
|
|
|
item = xmalloc(sizeof(*item));
|
|
|
|
item->type = SC_TIMEOUT;
|
|
|
|
item->gotoitem = NULL;
|
2008-07-08 16:18:21 +05:30
|
|
|
TAILQ_INSERT_TAIL(&schedule, item, entries);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (string) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (sscanf(string, "%d", &item->value) != 1)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: invalid timeout in schedule",
|
|
|
|
applet);
|
2008-03-16 22:30:56 +05:30
|
|
|
} else
|
|
|
|
item->value = 5;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (string != NULL) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((slash = strchr(string, '/')))
|
2007-04-11 18:14:47 +05:30
|
|
|
len = slash - string;
|
|
|
|
else
|
2008-03-16 22:30:56 +05:30
|
|
|
len = strlen(string);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
if (len >= (ptrdiff_t)sizeof(buffer))
|
|
|
|
eerrorx("%s: invalid schedule item, far too long",
|
|
|
|
applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
memcpy(buffer, string, len);
|
2007-04-11 18:14:47 +05:30
|
|
|
buffer[len] = 0;
|
|
|
|
string = slash ? slash + 1 : NULL;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
item = parse_schedule_item(buffer);
|
2008-07-08 16:18:21 +05:30
|
|
|
TAILQ_INSERT_TAIL(&schedule, item, entries);
|
2008-03-16 22:30:56 +05:30
|
|
|
if (item->type == SC_FOREVER) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if (repeatat)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("%s: invalid schedule, `forever' "
|
2009-04-24 03:01:22 +05:30
|
|
|
"appears more than once", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
repeatat = item;
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repeatat) {
|
2008-03-16 22:30:56 +05:30
|
|
|
item = xmalloc(sizeof(*item));
|
|
|
|
item->type = SC_GOTO;
|
|
|
|
item->value = 0;
|
|
|
|
item->gotoitem = repeatat;
|
2008-07-08 16:18:21 +05:30
|
|
|
TAILQ_INSERT_TAIL(&schedule, item, entries);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static pid_t
|
|
|
|
get_pid(const char *pidfile, bool quiet)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if (! pidfile)
|
2008-03-16 22:30:56 +05:30
|
|
|
return -1;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((fp = fopen(pidfile, "r")) == NULL) {
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!quiet)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerror("%s: fopen `%s': %s",
|
|
|
|
applet, pidfile, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
return -1;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (fscanf(fp, "%d", &pid) != 1) {
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!quiet)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerror("%s: no pid found in `%s'", applet, pidfile);
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
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
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return pid;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* return number of processed killed, -1 on error */
|
2009-04-24 03:01:22 +05:30
|
|
|
static int
|
|
|
|
do_stop(const char *exec, const char *const *argv,
|
|
|
|
pid_t pid, uid_t uid,int sig,
|
|
|
|
bool quiet, bool verbose, bool test)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PIDLIST *pids;
|
|
|
|
RC_PID *pi;
|
|
|
|
RC_PID *np;
|
2007-04-11 18:14:47 +05:30
|
|
|
bool killed;
|
|
|
|
int nkilled = 0;
|
|
|
|
|
2008-05-27 22:12:28 +05:30
|
|
|
if (pid)
|
2008-03-16 22:30:56 +05:30
|
|
|
pids = rc_find_pids(NULL, NULL, 0, pid);
|
2008-05-27 22:12:28 +05:30
|
|
|
else
|
2008-07-27 17:00:49 +05:30
|
|
|
pids = rc_find_pids(exec, argv, uid, pid);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!pids)
|
2008-03-16 22:30:56 +05:30
|
|
|
return 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-17 15:29:52 +05:30
|
|
|
LIST_FOREACH_SAFE(pi, pids, entries, np) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if (test) {
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!quiet)
|
2008-03-17 15:29:52 +05:30
|
|
|
einfo("Would send signal %d to PID %d",
|
2009-04-24 03:01:22 +05:30
|
|
|
sig, pi->pid);
|
2007-04-11 18:14:47 +05:30
|
|
|
nkilled++;
|
|
|
|
} else {
|
2008-03-17 15:29:52 +05:30
|
|
|
if (verbose)
|
|
|
|
ebegin("Sending signal %d to PID %d",
|
2009-04-24 03:01:22 +05:30
|
|
|
sig, pi->pid);
|
2008-03-17 15:29:52 +05:30
|
|
|
errno = 0;
|
|
|
|
killed = (kill(pi->pid, sig) == 0 ||
|
2009-04-24 03:01:22 +05:30
|
|
|
errno == ESRCH ? true : false);
|
2008-03-17 15:29:52 +05:30
|
|
|
if (verbose)
|
|
|
|
eend(killed ? 0 : 1,
|
2009-04-24 03:01:22 +05:30
|
|
|
"%s: failed to send signal %d to PID %d: %s",
|
|
|
|
applet, sig, pi->pid, strerror(errno));
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!killed) {
|
2008-03-17 15:29:52 +05:30
|
|
|
nkilled = -1;
|
|
|
|
} else {
|
|
|
|
if (nkilled != -1)
|
|
|
|
nkilled++;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-03-17 15:29:52 +05:30
|
|
|
free(pi);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
free(pids);
|
|
|
|
return nkilled;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static int
|
|
|
|
run_stop_schedule(const char *exec, const char *const *argv,
|
|
|
|
const char *pidfile, uid_t uid,
|
|
|
|
bool quiet, bool verbose, bool test)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-07-08 16:18:21 +05:30
|
|
|
SCHEDULEITEM *item = TAILQ_FIRST(&schedule);
|
2007-04-11 18:14:47 +05:30
|
|
|
int nkilled = 0;
|
|
|
|
int tkilled = 0;
|
|
|
|
int nrunning = 0;
|
2007-08-09 20:03:20 +05:30
|
|
|
long nloops;
|
|
|
|
struct timespec ts;
|
2008-05-27 22:12:28 +05:30
|
|
|
pid_t pid = 0;
|
2008-12-23 15:45:41 +05:30
|
|
|
const char *const *p;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (verbose) {
|
2008-07-27 17:00:49 +05:30
|
|
|
if (exec)
|
2008-10-30 21:56:05 +05:30
|
|
|
einfo ("Will stop %s", exec);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pidfile)
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("Will stop PID in pidfile `%s'", pidfile);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (uid)
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("Will stop processes owned by UID %d", uid);
|
2008-12-23 15:45:41 +05:30
|
|
|
if (argv && *argv) {
|
|
|
|
einfon("Will stop processes of `");
|
|
|
|
for (p = argv; p && *p; p++) {
|
|
|
|
if (p != argv)
|
|
|
|
printf(" ");
|
|
|
|
printf("%s", *p);
|
|
|
|
}
|
|
|
|
printf("'\n");
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-05-27 22:12:28 +05:30
|
|
|
if (pidfile) {
|
|
|
|
pid = get_pid(pidfile, quiet);
|
|
|
|
if (pid == -1)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
while (item) {
|
2008-03-16 22:30:56 +05:30
|
|
|
switch (item->type) {
|
|
|
|
case SC_GOTO:
|
|
|
|
item = item->gotoitem;
|
|
|
|
continue;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case SC_SIGNAL:
|
|
|
|
nrunning = 0;
|
2008-07-27 17:00:49 +05:30
|
|
|
nkilled = do_stop(exec, argv, pid, uid, item->value,
|
2009-04-24 03:01:22 +05:30
|
|
|
quiet, verbose, test);
|
2008-03-16 22:30:56 +05:30
|
|
|
if (nkilled == 0) {
|
|
|
|
if (tkilled == 0) {
|
|
|
|
if (! quiet)
|
|
|
|
eerror("%s: no matching "
|
2009-04-24 03:01:22 +05:30
|
|
|
"processes found", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
return tkilled;
|
|
|
|
}
|
|
|
|
else if (nkilled == -1)
|
|
|
|
return 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
tkilled += nkilled;
|
|
|
|
break;
|
|
|
|
case SC_TIMEOUT:
|
|
|
|
if (item->value < 1) {
|
|
|
|
item = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
break;
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
nloops = (ONE_SECOND / POLL_INTERVAL) * item->value;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = POLL_INTERVAL;
|
|
|
|
|
|
|
|
while (nloops) {
|
2008-07-27 17:00:49 +05:30
|
|
|
if ((nrunning = do_stop(exec, argv, pid,
|
2009-04-24 03:01:22 +05:30
|
|
|
uid, 0, true, false, true)) == 0)
|
2008-05-27 22:12:28 +05:30
|
|
|
return 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
|
|
|
|
if (nanosleep(&ts, NULL) == -1) {
|
|
|
|
if (errno == EINTR)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerror("%s: caught an"
|
|
|
|
" interrupt", applet);
|
2008-03-16 22:30:56 +05:30
|
|
|
else {
|
|
|
|
eerror("%s: nanosleep: %s",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
return 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
2009-04-24 03:01:22 +05:30
|
|
|
nloops --;
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
default:
|
2009-04-24 03:01:22 +05:30
|
|
|
eerror("%s: invalid schedule item `%d'",
|
|
|
|
applet, item->type);
|
2008-03-16 22:30:56 +05:30
|
|
|
return 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (item)
|
2008-07-08 16:18:21 +05:30
|
|
|
item = TAILQ_NEXT(item, entries);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (test || (tkilled > 0 && nrunning == 0))
|
2008-03-16 22:30:56 +05:30
|
|
|
return nkilled;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (! quiet) {
|
|
|
|
if (nrunning == 1)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerror("%s: %d process refused to stop",
|
|
|
|
applet, nrunning);
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
2009-04-24 03:01:22 +05:30
|
|
|
eerror("%s: %d process(es) refused to stop",
|
|
|
|
applet, nrunning);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return -nrunning;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static void
|
|
|
|
handle_signal(int sig)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int status;
|
|
|
|
int serrno = errno;
|
|
|
|
char signame[10] = { '\0' };
|
|
|
|
|
|
|
|
switch (sig) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case SIGINT:
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!signame[0])
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(signame, sizeof(signame), "SIGINT");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SIGTERM:
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!signame[0])
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(signame, sizeof(signame), "SIGTERM");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SIGQUIT:
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!signame[0])
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(signame, sizeof(signame), "SIGQUIT");
|
|
|
|
eerrorx("%s: caught %s, aborting", applet, signame);
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
case SIGCHLD:
|
|
|
|
for (;;) {
|
|
|
|
if (waitpid(-1, &status, WNOHANG) < 0) {
|
|
|
|
if (errno != ECHILD)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerror("%s: waitpid: %s",
|
|
|
|
applet, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
default:
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-10-30 23:10:48 +05:30
|
|
|
static char *
|
|
|
|
expand_home(const char *home, const char *path)
|
|
|
|
{
|
2008-10-30 23:27:58 +05:30
|
|
|
char *opath, *ppath, *p, *nh;
|
2008-10-30 23:10:48 +05:30
|
|
|
size_t len;
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
if (!path || *path != '~')
|
|
|
|
return xstrdup(path);
|
|
|
|
|
2008-10-30 23:27:58 +05:30
|
|
|
opath = ppath = xstrdup(path);
|
|
|
|
if (ppath[1] != '/' && ppath[1] != '\0') {
|
|
|
|
p = strchr(ppath + 1, '/');
|
2008-10-30 23:10:48 +05:30
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
2008-10-30 23:27:58 +05:30
|
|
|
pw = getpwnam(ppath + 1);
|
2008-10-30 23:10:48 +05:30
|
|
|
if (pw) {
|
|
|
|
home = pw->pw_dir;
|
2008-10-30 23:27:58 +05:30
|
|
|
ppath = p;
|
|
|
|
if (ppath)
|
|
|
|
*ppath = '/';
|
2008-10-30 23:10:48 +05:30
|
|
|
} else
|
|
|
|
home = NULL;
|
|
|
|
} else
|
2008-10-30 23:27:58 +05:30
|
|
|
ppath++;
|
2008-10-30 23:10:48 +05:30
|
|
|
|
|
|
|
if (!home) {
|
2008-10-30 23:27:58 +05:30
|
|
|
free(opath);
|
2008-10-30 23:10:48 +05:30
|
|
|
return xstrdup(path);
|
|
|
|
}
|
2008-10-30 23:27:58 +05:30
|
|
|
if (!ppath) {
|
|
|
|
free(opath);
|
2008-10-30 23:10:48 +05:30
|
|
|
return xstrdup(home);
|
2008-10-30 23:27:58 +05:30
|
|
|
}
|
2008-10-30 23:10:48 +05:30
|
|
|
|
2008-10-30 23:27:58 +05:30
|
|
|
len = strlen(ppath) + strlen(home) + 1;
|
2008-10-30 23:10:48 +05:30
|
|
|
nh = xmalloc(len);
|
2008-10-30 23:27:58 +05:30
|
|
|
snprintf(nh, len, "%s%s", home, ppath);
|
|
|
|
free(opath);
|
2008-10-30 23:10:48 +05:30
|
|
|
return nh;
|
|
|
|
}
|
2007-08-17 16:31:18 +05:30
|
|
|
|
|
|
|
#include "_usage.h"
|
2009-04-22 16:15:01 +05:30
|
|
|
#define getoptstring "KN:R:Sbc:d:e:g:ik:mn:op:s:tu:r:w: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'},
|
2008-08-20 15:32:11 +05:30
|
|
|
{ "umask", 1, NULL, 'k'},
|
2007-08-17 16:31:18 +05:30
|
|
|
{ "group", 1, NULL, 'g'},
|
2009-04-22 16:15:01 +05:30
|
|
|
{ "interpreted", 0, NULL, 'i'},
|
2007-08-17 16:31:18 +05:30
|
|
|
{ "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'},
|
2009-04-19 23:50:49 +05:30
|
|
|
{ "wait", 1, NULL, 'w'},
|
2007-08-17 16:31:18 +05:30
|
|
|
{ "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",
|
2008-07-27 17:00:49 +05:30
|
|
|
"deprecated, use --exec or --name",
|
2007-09-26 16:16:44 +05:30
|
|
|
"Force daemon to background",
|
|
|
|
"deprecated, use --user",
|
|
|
|
"Change the PWD",
|
2008-02-17 20:42:00 +05:30
|
|
|
"Set an environment string",
|
2008-08-20 15:32:11 +05:30
|
|
|
"Set the umask for the daemon",
|
2007-09-26 16:16:44 +05:30
|
|
|
"Change the process group",
|
2009-04-22 16:15:01 +05:30
|
|
|
"Match process name by interpreter",
|
2007-09-26 16:16:44 +05:30
|
|
|
"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",
|
2009-04-19 23:50:49 +05:30
|
|
|
"Seconds to wait for daemon start",
|
2007-09-26 16:16:44 +05:30
|
|
|
"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"
|
|
|
|
|
2009-04-24 03:01:22 +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;
|
2008-10-30 03:05:45 +05:30
|
|
|
const char *const *pamenv = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-05-14 17:54:18 +05:30
|
|
|
int opt;
|
2008-11-04 02:36:45 +05:30
|
|
|
bool start = false;
|
|
|
|
bool stop = false;
|
2007-04-11 18:14:47 +05:30
|
|
|
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;
|
2008-07-27 17:00:49 +05:30
|
|
|
char *startas = NULL;
|
|
|
|
char *name = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *pidfile = NULL;
|
2008-12-28 19:35:33 +05:30
|
|
|
char *retry = NULL;
|
2008-11-04 02:36:45 +05:30
|
|
|
int sig = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
int nicelevel = 0;
|
|
|
|
bool background = false;
|
|
|
|
bool makepidfile = false;
|
2009-04-22 16:15:01 +05:30
|
|
|
bool interpreted = false;
|
2007-08-17 16:24:04 +05:30
|
|
|
uid_t uid = 0;
|
|
|
|
gid_t gid = 0;
|
2008-10-30 23:10:48 +05:30
|
|
|
char *home = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
int tid = 0;
|
|
|
|
char *redirect_stderr = NULL;
|
|
|
|
char *redirect_stdout = NULL;
|
|
|
|
int stdout_fd;
|
|
|
|
int stderr_fd;
|
2008-07-27 17:00:49 +05:30
|
|
|
pid_t pid, spid;
|
2007-04-11 18:14:47 +05:30
|
|
|
int i;
|
2008-03-20 01:27:24 +05:30
|
|
|
char *svcname = getenv("RC_SVCNAME");
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRINGLIST *env_list;
|
|
|
|
RC_STRING *env;
|
2008-07-27 17:00:49 +05:30
|
|
|
char *tmp, *newpath, *np;
|
2008-03-16 22:30:56 +05:30
|
|
|
char *p;
|
2008-07-27 17:00:49 +05:30
|
|
|
char *token;
|
2008-03-17 18:55:56 +05:30
|
|
|
char exec_file[PATH_MAX];
|
2008-03-16 22:30:56 +05:30
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
char line[130];
|
|
|
|
FILE *fp;
|
|
|
|
size_t len;
|
2009-02-11 04:41:17 +05:30
|
|
|
mode_t numask = 022;
|
2008-09-22 17:44:47 +05:30
|
|
|
char **margv;
|
2008-09-27 05:28:57 +05:30
|
|
|
unsigned int start_wait = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-07-08 16:18:21 +05:30
|
|
|
TAILQ_INIT(&schedule);
|
2008-10-06 20:32:32 +05:30
|
|
|
#ifdef DEBUG_MEMORY
|
2008-03-16 22:30:56 +05:30
|
|
|
atexit(cleanup);
|
2008-10-06 20:32:32 +05:30
|
|
|
#endif
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +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
|
|
|
|
2008-07-27 17:00:49 +05:30
|
|
|
if ((tmp = getenv("SSD_NICELEVEL")))
|
|
|
|
if (sscanf(tmp, "%d", &nicelevel) != 1)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerror("%s: invalid nice level `%s' (SSD_NICELEVEL)",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, tmp);
|
2007-05-02 18:03:56 +05:30
|
|
|
|
2009-01-11 22:39:13 +05:30
|
|
|
/* Get our user name and initial dir */
|
|
|
|
p = getenv("USER");
|
2008-10-30 23:10:48 +05:30
|
|
|
home = getenv("HOME");
|
2009-01-11 22:39:13 +05:30
|
|
|
if (home == NULL || p == NULL) {
|
2008-10-30 23:10:48 +05:30
|
|
|
pw = getpwuid(getuid());
|
2009-01-11 22:39:13 +05:30
|
|
|
if (pw != NULL) {
|
|
|
|
if (p == NULL)
|
|
|
|
setenv("USER", pw->pw_name, 1);
|
|
|
|
if (home == NULL) {
|
2009-01-12 15:54:47 +05:30
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
2009-01-11 22:39:13 +05:30
|
|
|
home = pw->pw_dir;
|
|
|
|
}
|
|
|
|
}
|
2008-10-30 23:10:48 +05:30
|
|
|
}
|
|
|
|
|
2008-07-27 17:00:49 +05:30
|
|
|
while ((opt = getopt_long(argc, argv, getoptstring, longopts,
|
2009-04-24 03:01:22 +05:30
|
|
|
(int *) 0)) != -1)
|
2007-05-14 17:54:18 +05:30
|
|
|
switch (opt) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'K': /* --stop */
|
2008-11-04 02:36:45 +05:30
|
|
|
stop = true;
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
|
|
|
case 'N': /* --nice */
|
|
|
|
if (sscanf(optarg, "%d", &nicelevel) != 1)
|
2008-10-30 23:10:48 +05:30
|
|
|
eerrorx("%s: invalid nice level `%s'",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, optarg);
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'R': /* --retry <schedule>|<timeout> */
|
2008-12-28 19:35:33 +05:30
|
|
|
retry = optarg;
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'S': /* --start */
|
|
|
|
start = true;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'b': /* --background */
|
|
|
|
background = true;
|
|
|
|
break;
|
2007-04-13 12:13:00 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'u': /* --user <username>|<uid> */
|
|
|
|
case 'c': /* --chuid <username>|<uid> */
|
2009-04-24 03:01:22 +05:30
|
|
|
{
|
|
|
|
p = optarg;
|
|
|
|
tmp = strsep(&p, ":");
|
|
|
|
changeuser = xstrdup(tmp);
|
|
|
|
if (sscanf(tmp, "%d", &tid) != 1)
|
|
|
|
pw = getpwnam(tmp);
|
|
|
|
else
|
|
|
|
pw = getpwuid((uid_t)tid);
|
|
|
|
|
|
|
|
if (pw == NULL)
|
|
|
|
eerrorx("%s: user `%s' not found",
|
|
|
|
applet, tmp);
|
|
|
|
uid = pw->pw_uid;
|
|
|
|
home = pw->pw_dir;
|
|
|
|
unsetenv("HOME");
|
|
|
|
if (pw->pw_dir)
|
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
|
|
|
unsetenv("USER");
|
|
|
|
if (pw->pw_name)
|
|
|
|
setenv("USER", pw->pw_name, 1);
|
|
|
|
if (gid == 0)
|
|
|
|
gid = pw->pw_gid;
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
tmp = strsep (&p, ":");
|
2008-03-16 22:30:56 +05:30
|
|
|
if (sscanf(tmp, "%d", &tid) != 1)
|
2009-04-24 03:01:22 +05:30
|
|
|
gr = getgrnam(tmp);
|
2008-03-16 22:30:56 +05:30
|
|
|
else
|
2009-04-24 03:01:22 +05:30
|
|
|
gr = getgrgid((gid_t) tid);
|
2008-03-16 22:30:56 +05:30
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
if (gr == NULL)
|
|
|
|
eerrorx("%s: group `%s'"
|
|
|
|
" not found",
|
|
|
|
applet, tmp);
|
|
|
|
gid = gr->gr_gid;
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
2009-04-24 03:01:22 +05:30
|
|
|
}
|
|
|
|
break;
|
2008-02-17 20:42:00 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'd': /* --chdir /new/dir */
|
|
|
|
ch_dir = optarg;
|
|
|
|
break;
|
2007-04-13 12:13:00 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'e': /* --env */
|
2009-01-11 22:39:13 +05:30
|
|
|
putenv(optarg);
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2007-04-13 12:13:00 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'g': /* --group <group>|<gid> */
|
2009-01-11 22:39:13 +05:30
|
|
|
if (sscanf(optarg, "%d", &tid) != 1)
|
|
|
|
gr = getgrnam(optarg);
|
|
|
|
else
|
|
|
|
gr = getgrgid((gid_t)tid);
|
|
|
|
if (gr == NULL)
|
|
|
|
eerrorx("%s: group `%s' not found",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, optarg);
|
2009-01-11 22:39:13 +05:30
|
|
|
gid = gr->gr_gid;
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-04-22 16:15:01 +05:30
|
|
|
case 'i': /* --interpreted */
|
|
|
|
interpreted = true;
|
|
|
|
break;
|
|
|
|
|
2008-08-20 15:32:11 +05:30
|
|
|
case 'k':
|
|
|
|
if (parse_mode(&numask, optarg))
|
|
|
|
eerrorx("%s: invalid mode `%s'",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, optarg);
|
2008-08-20 15:32:11 +05:30
|
|
|
break;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'm': /* --make-pidfile */
|
|
|
|
makepidfile = true;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'n': /* --name <process-name> */
|
2008-07-27 17:00:49 +05:30
|
|
|
name = optarg;
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'o': /* --oknodo */
|
|
|
|
oknodo = true;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'p': /* --pidfile <pid-file> */
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 's': /* --signal <signal> */
|
|
|
|
sig = parse_signal(optarg);
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 't': /* --test */
|
|
|
|
test = true;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'r': /* --chroot /new/root */
|
|
|
|
ch_root = optarg;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-07-27 17:00:49 +05:30
|
|
|
case 'a': /* --startas <name> */
|
|
|
|
startas = optarg;
|
|
|
|
break;
|
2009-04-19 23:50:49 +05:30
|
|
|
case 'w':
|
|
|
|
if (sscanf(optarg, "%d", &start_wait) != 1)
|
|
|
|
eerrorx("%s: `%s' not a number",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, optarg);
|
2009-04-19 23:50:49 +05:30
|
|
|
break;
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'x': /* --exec <executable> */
|
|
|
|
exec = optarg;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case '1': /* --stdout /path/to/stdout.lgfile */
|
|
|
|
redirect_stdout = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '2': /* --stderr /path/to/stderr.logfile */
|
|
|
|
redirect_stderr = optarg;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case_RC_COMMON_GETOPT
|
2009-04-24 03:01:22 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-09-17 22:26:23 +05:30
|
|
|
endpwent();
|
2008-07-27 17:00:49 +05:30
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2008-03-16 22:30:56 +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 */
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!start &&
|
2008-11-11 02:27:03 +05:30
|
|
|
!stop &&
|
2008-07-27 17:00:49 +05:30
|
|
|
sig != SIGINT &&
|
|
|
|
sig != SIGTERM &&
|
|
|
|
sig != SIGQUIT &&
|
|
|
|
sig != SIGKILL)
|
|
|
|
oknodo = true;
|
|
|
|
|
|
|
|
if (!exec)
|
|
|
|
exec = startas;
|
|
|
|
else if (!name)
|
|
|
|
name = startas;
|
|
|
|
|
|
|
|
if (!exec) {
|
|
|
|
exec = *argv;
|
2008-10-30 21:56:05 +05:30
|
|
|
if (!exec)
|
|
|
|
exec = name;
|
|
|
|
if (name && start)
|
2008-07-27 17:00:49 +05:30
|
|
|
*argv = name;
|
2008-12-23 15:45:41 +05:30
|
|
|
} else if (name)
|
2008-07-27 17:00:49 +05:30
|
|
|
*--argv = name;
|
2008-12-23 15:45:41 +05:30
|
|
|
else if (exec)
|
2008-07-27 17:00:49 +05:30
|
|
|
*--argv = exec;
|
2007-04-13 14:51:16 +05:30
|
|
|
|
2008-12-28 19:35:33 +05:30
|
|
|
if (stop || sig != 0) {
|
|
|
|
if (sig == 0)
|
|
|
|
sig = SIGTERM;
|
2008-12-23 15:45:41 +05:30
|
|
|
if (!*argv && !pidfile && !name && !uid)
|
2008-11-04 02:36:45 +05:30
|
|
|
eerrorx("%s: --stop needs --exec, --pidfile,"
|
2009-04-24 03:01:22 +05:30
|
|
|
" --name or --user", applet);
|
2008-11-04 02:36:45 +05:30
|
|
|
if (background)
|
|
|
|
eerrorx("%s: --background is only relevant with"
|
2009-04-24 03:01:22 +05:30
|
|
|
" --start", applet);
|
2008-11-04 02:36:45 +05:30
|
|
|
if (makepidfile)
|
|
|
|
eerrorx("%s: --make-pidfile is only relevant with"
|
2009-04-24 03:01:22 +05:30
|
|
|
" --start", applet);
|
2008-11-04 02:36:45 +05:30
|
|
|
if (redirect_stdout || redirect_stderr)
|
|
|
|
eerrorx("%s: --stdout and --stderr are only relevant"
|
2009-04-24 03:01:22 +05:30
|
|
|
" with --start", applet);
|
2008-11-04 02:36:45 +05:30
|
|
|
} else {
|
|
|
|
if (!exec)
|
|
|
|
eerrorx("%s: nothing to start", applet);
|
|
|
|
if (makepidfile && !pidfile)
|
|
|
|
eerrorx("%s: --make-pidfile is only relevant with"
|
2009-04-24 03:01:22 +05:30
|
|
|
" --pidfile", applet);
|
2008-11-04 02:36:45 +05:30
|
|
|
if ((redirect_stdout || redirect_stderr) && !background)
|
|
|
|
eerrorx("%s: --stdout and --stderr are only relevant"
|
2009-04-24 03:01:22 +05:30
|
|
|
" with --background", applet);
|
2008-11-04 02:36:45 +05:30
|
|
|
}
|
2008-12-28 19:35:33 +05:30
|
|
|
|
2008-10-30 23:10:48 +05:30
|
|
|
/* Expand ~ */
|
|
|
|
if (ch_dir && *ch_dir == '~')
|
|
|
|
ch_dir = expand_home(home, ch_dir);
|
|
|
|
if (ch_root && *ch_root == '~')
|
|
|
|
ch_root = expand_home(home, ch_root);
|
2008-12-23 15:45:41 +05:30
|
|
|
if (exec) {
|
|
|
|
if (*exec == '~')
|
|
|
|
exec = expand_home(home, exec);
|
2008-10-30 23:10:48 +05:30
|
|
|
|
2008-12-23 15:45:41 +05:30
|
|
|
/* Validate that the binary exists if we are starting */
|
|
|
|
if (*exec == '/' || *exec == '.') {
|
|
|
|
/* Full or relative path */
|
2008-07-27 17:00:49 +05:30
|
|
|
if (ch_root)
|
2008-12-23 15:45:41 +05:30
|
|
|
snprintf(exec_file, sizeof(exec_file),
|
2009-04-24 03:01:22 +05:30
|
|
|
"%s/%s", ch_root, exec);
|
2008-07-27 17:00:49 +05:30
|
|
|
else
|
2008-12-23 15:45:41 +05:30
|
|
|
snprintf(exec_file, sizeof(exec_file),
|
2009-04-24 03:01:22 +05:30
|
|
|
"%s", exec);
|
2008-12-23 15:45:41 +05:30
|
|
|
} else {
|
|
|
|
/* Something in $PATH */
|
|
|
|
p = tmp = xstrdup(getenv("PATH"));
|
2008-07-27 17:00:49 +05:30
|
|
|
*exec_file = '\0';
|
2008-12-23 15:45:41 +05:30
|
|
|
while ((token = strsep(&p, ":"))) {
|
|
|
|
if (ch_root)
|
|
|
|
snprintf(exec_file, sizeof(exec_file),
|
2009-04-24 03:01:22 +05:30
|
|
|
"%s/%s/%s",
|
|
|
|
ch_root, token, exec);
|
2008-12-23 15:45:41 +05:30
|
|
|
else
|
|
|
|
snprintf(exec_file, sizeof(exec_file),
|
2009-04-24 03:01:22 +05:30
|
|
|
"%s/%s", token, exec);
|
2008-12-23 15:45:41 +05:30
|
|
|
if (exists(exec_file))
|
|
|
|
break;
|
|
|
|
*exec_file = '\0';
|
|
|
|
}
|
|
|
|
free(tmp);
|
2008-07-27 17:00:49 +05:30
|
|
|
}
|
|
|
|
}
|
2008-12-23 15:45:41 +05:30
|
|
|
if (start && !exists(exec_file)) {
|
2008-07-27 17:00:49 +05:30
|
|
|
eerror("%s: %s does not exist", applet,
|
2009-04-24 03:01:22 +05:30
|
|
|
*exec_file ? exec_file : exec);
|
2008-07-27 17:00:49 +05:30
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2008-09-17 22:26:23 +05:30
|
|
|
/* If we don't have a pidfile we should check if it's interpreted
|
|
|
|
* or not. If it we, we need to pass the interpreter through
|
|
|
|
* to our daemon calls to find it correctly. */
|
2009-04-22 16:15:01 +05:30
|
|
|
if (interpreted && !pidfile) {
|
2008-09-17 22:26:23 +05:30
|
|
|
fp = fopen(exec_file, "r");
|
2008-07-27 17:00:49 +05:30
|
|
|
if (fp) {
|
2009-05-01 00:26:43 +05:30
|
|
|
p = fgets(line, sizeof(line), fp);
|
2008-07-27 17:00:49 +05:30
|
|
|
fclose(fp);
|
2009-05-01 00:26:43 +05:30
|
|
|
if (p != NULL && line[0] == '#' && line[1] == '!') {
|
2008-09-22 16:15:01 +05:30
|
|
|
p = line + 2;
|
|
|
|
/* Strip leading spaces */
|
2008-09-22 17:44:47 +05:30
|
|
|
while (*p == ' ' || *p == '\t')
|
2008-09-22 16:15:01 +05:30
|
|
|
p++;
|
2008-07-27 17:00:49 +05:30
|
|
|
/* Remove the trailing newline */
|
2008-09-22 16:15:01 +05:30
|
|
|
len = strlen(p) - 1;
|
|
|
|
if (p[len] == '\n')
|
|
|
|
p[len] = '\0';
|
|
|
|
token = strsep(&p, " ");
|
|
|
|
strncpy(exec_file, token, sizeof(exec_file));
|
2008-09-17 22:26:23 +05:30
|
|
|
opt = 0;
|
|
|
|
for (nav = argv; *nav; nav++)
|
|
|
|
opt++;
|
2008-09-22 17:44:47 +05:30
|
|
|
nav = xmalloc(sizeof(char *) * (opt + 3));
|
2008-09-17 22:26:23 +05:30
|
|
|
nav[0] = exec_file;
|
2008-09-22 17:44:47 +05:30
|
|
|
len = 1;
|
|
|
|
if (p)
|
|
|
|
nav[len++] = p;
|
2008-09-17 22:26:23 +05:30
|
|
|
for (i = 0; i < opt; i++)
|
2008-09-22 17:44:47 +05:30
|
|
|
nav[i + len] = argv[i];
|
|
|
|
nav[i + len] = '\0';
|
2008-07-27 17:00:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-22 17:44:47 +05:30
|
|
|
margv = nav ? nav : argv;
|
|
|
|
|
2008-12-23 15:45:41 +05:30
|
|
|
if (stop || sig) {
|
2008-12-28 19:35:33 +05:30
|
|
|
if (sig == 0)
|
2008-12-23 15:45:41 +05:30
|
|
|
sig = SIGTERM;
|
|
|
|
if (!stop)
|
|
|
|
oknodo = true;
|
2008-12-28 19:35:33 +05:30
|
|
|
if (retry)
|
|
|
|
parse_schedule(retry, sig);
|
|
|
|
else if (test || oknodo)
|
|
|
|
parse_schedule("0", sig);
|
|
|
|
else
|
|
|
|
parse_schedule(NULL, sig);
|
2008-12-23 15:45:41 +05:30
|
|
|
i = run_stop_schedule(exec, (const char *const *)margv,
|
2009-04-24 03:01:22 +05:30
|
|
|
pidfile, uid, quiet, verbose, test);
|
2008-12-23 15:45:41 +05:30
|
|
|
|
|
|
|
if (i < 0)
|
|
|
|
/* We failed to stop something */
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
if (test || oknodo)
|
|
|
|
return i > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (pidfile && exists(pidfile))
|
|
|
|
unlink(pidfile);
|
|
|
|
if (svcname)
|
|
|
|
rc_service_daemon_set(svcname, exec,
|
2009-04-24 03:01:22 +05:30
|
|
|
(const char *const *)argv,
|
|
|
|
pidfile, false);
|
2008-12-23 15:45:41 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2008-07-27 17:00:49 +05:30
|
|
|
if (pidfile)
|
2008-05-27 22:18:47 +05:30
|
|
|
pid = get_pid(pidfile, true);
|
2008-07-27 17:00:49 +05:30
|
|
|
else
|
2008-05-27 22:12:28 +05:30
|
|
|
pid = 0;
|
|
|
|
|
2008-09-22 17:44:47 +05:30
|
|
|
if (do_stop(exec, (const char * const *)margv, pid, uid,
|
2009-04-24 03:01:22 +05:30
|
|
|
0, true, false, true) > 0)
|
2008-03-16 22:30:56 +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-03-16 22:30:56 +05:30
|
|
|
einfon("Would start");
|
2008-02-17 20:42:00 +05:30
|
|
|
while (argc-- >= 0)
|
|
|
|
printf(" %s", *argv++);
|
2008-03-16 22:30:56 +05:30
|
|
|
printf("\n");
|
|
|
|
eindent();
|
2007-08-17 16:24:04 +05:30
|
|
|
if (uid != 0)
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("as user id %d", uid);
|
2007-08-17 16:24:04 +05:30
|
|
|
if (gid != 0)
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("as group id %d", gid);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ch_root)
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("in root `%s'", ch_root);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ch_dir)
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("in dir `%s'", ch_dir);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (nicelevel != 0)
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("with a priority of %d", nicelevel);
|
2008-07-27 17:00:49 +05:30
|
|
|
if (name)
|
|
|
|
einfo ("with a process name of %s", name);
|
2008-03-16 22:30:56 +05:30
|
|
|
eoutdent();
|
|
|
|
exit(EXIT_SUCCESS);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose) {
|
2008-03-16 22:30:56 +05:30
|
|
|
ebegin("Detaching to start `%s'", exec);
|
|
|
|
eindent();
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2009-04-19 23:50:49 +05:30
|
|
|
/* Remove existing pidfile */
|
|
|
|
if (pidfile)
|
|
|
|
unlink(pidfile);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (background)
|
2008-03-16 22:30:56 +05:30
|
|
|
signal_setup(SIGCHLD, handle_signal);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((pid = fork()) == -1)
|
|
|
|
eerrorx("%s: fork: %s", applet, strerror(errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Child process - lets go! */
|
|
|
|
if (pid == 0) {
|
2008-03-16 22:30:56 +05:30
|
|
|
pid_t mypid = getpid();
|
2009-02-11 04:41:17 +05:30
|
|
|
umask(numask);
|
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) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (setpriority(PRIO_PROCESS, mypid, nicelevel) == -1)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: setpritory %d: %s",
|
|
|
|
applet, nicelevel,
|
|
|
|
strerror(errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (ch_root && chroot(ch_root) < 0)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: chroot `%s': %s",
|
|
|
|
applet, ch_root, strerror(errno));
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-07-27 17:00:49 +05:30
|
|
|
if (ch_dir && chdir(ch_dir) < 0)
|
2009-04-24 03:01:22 +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) {
|
2008-03-16 22:30:56 +05:30
|
|
|
fp = fopen(pidfile, "w");
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! fp)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("%s: fopen `%s': %s", applet, pidfile,
|
2009-04-24 03:01:22 +05:30
|
|
|
strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(fp, "%d\n", mypid);
|
|
|
|
fclose(fp);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef HAVE_PAM
|
2007-04-11 18:14:47 +05:30
|
|
|
if (changeuser != NULL)
|
2009-04-24 03:01:22 +05:30
|
|
|
pamr = pam_start("start-stop-daemon",
|
|
|
|
changeuser, &conv, &pamh);
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
2009-04-24 03:01:22 +05:30
|
|
|
pamr = pam_start("start-stop-daemon",
|
|
|
|
"nobody", &conv, &pamh);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (pamr == PAM_SUCCESS)
|
2008-03-16 22:30:56 +05:30
|
|
|
pamr = pam_authenticate(pamh, PAM_SILENT);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pamr == PAM_SUCCESS)
|
2008-03-16 22:30:56 +05:30
|
|
|
pamr = pam_acct_mgmt(pamh, PAM_SILENT);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pamr == PAM_SUCCESS)
|
2008-03-16 22:30:56 +05:30
|
|
|
pamr = pam_open_session(pamh, PAM_SILENT);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pamr != PAM_SUCCESS)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: pam error: %s",
|
|
|
|
applet, pam_strerror(pamh, pamr));
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (gid && setgid(gid))
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: unable to set groupid to %d",
|
|
|
|
applet, gid);
|
2008-03-16 22:30:56 +05:30
|
|
|
if (changeuser && initgroups(changeuser, gid))
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: initgroups (%s, %d)",
|
|
|
|
applet, changeuser, gid);
|
2009-01-11 22:39:13 +05:30
|
|
|
if (uid && setuid(uid))
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx ("%s: unable to set userid to %d",
|
|
|
|
applet, uid);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Close any fd's to the passwd database */
|
2008-03-16 22:30:56 +05:30
|
|
|
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 */
|
2008-03-16 22:30:56 +05:30
|
|
|
env_list = rc_stringlist_new();
|
|
|
|
i = 0;
|
|
|
|
while(environ[i])
|
|
|
|
rc_stringlist_add(env_list, environ[i++]);
|
2008-10-30 03:05:45 +05:30
|
|
|
|
|
|
|
#ifdef HAVE_PAM
|
|
|
|
pamenv = (const char *const *)pam_getenvlist(pamh);
|
|
|
|
if (pamenv) {
|
|
|
|
while (*pamenv) {
|
|
|
|
/* Don't add strings unless they set a var */
|
|
|
|
if (strchr(*pamenv, '='))
|
|
|
|
putenv(xstrdup(*pamenv));
|
2009-05-07 16:50:59 +05:30
|
|
|
else
|
|
|
|
unsetenv(*pamenv);
|
2008-10-30 03:05:45 +05:30
|
|
|
pamenv++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
TAILQ_FOREACH(env, env_list, entries) {
|
|
|
|
if ((strncmp(env->value, "RC_", 3) == 0 &&
|
2009-04-24 03:01:22 +05:30
|
|
|
strncmp(env->value, "RC_SERVICE=", 10) != 0 &&
|
|
|
|
strncmp(env->value, "RC_SVCNAME=", 10) != 0) ||
|
2008-03-20 01:29:50 +05:30
|
|
|
strncmp(env->value, "SSD_NICELEVEL=", 14) == 0)
|
2008-03-16 22:30:56 +05:30
|
|
|
{
|
|
|
|
p = strchr(env->value, '=');
|
|
|
|
*p = '\0';
|
|
|
|
unsetenv(env->value);
|
2007-09-24 17:39:43 +05:30
|
|
|
continue;
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
rc_stringlist_free(env_list);
|
|
|
|
|
|
|
|
/* For the path, remove the rcscript bin dir from it */
|
2008-11-12 16:25:42 +05:30
|
|
|
if ((token = getenv("PATH"))) {
|
|
|
|
len = strlen(token);
|
|
|
|
newpath = np = xmalloc(len + 1);
|
|
|
|
while (token && *token) {
|
|
|
|
p = strchr(token, ':');
|
|
|
|
if (p) {
|
|
|
|
*p++ = '\0';
|
|
|
|
while (*p == ':')
|
|
|
|
p++;
|
|
|
|
}
|
2009-05-24 01:08:12 +05:30
|
|
|
if (strcmp(token, RC_LIBEXECDIR "/bin") != 0 &&
|
|
|
|
strcmp(token, RC_LIBEXECDIR "/sbin") != 0)
|
2008-11-12 16:25:42 +05:30
|
|
|
{
|
|
|
|
len = strlen(token);
|
|
|
|
if (np != newpath)
|
|
|
|
*np++ = ':';
|
|
|
|
memcpy(np, token, len);
|
|
|
|
np += len;
|
|
|
|
}
|
|
|
|
token = p;
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
2008-11-12 16:25:42 +05:30
|
|
|
*np = '\0';
|
2008-03-16 22:30:56 +05:30
|
|
|
unsetenv("PATH");
|
|
|
|
setenv("PATH", newpath, 1);
|
2007-09-24 17:39:43 +05:30
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
stdout_fd = devnull_fd;
|
|
|
|
stderr_fd = devnull_fd;
|
|
|
|
if (redirect_stdout) {
|
2009-04-24 03:01:22 +05:30
|
|
|
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",
|
|
|
|
applet, redirect_stdout, strerror(errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
if (redirect_stderr) {
|
2009-04-24 03:01:22 +05:30
|
|
|
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",
|
|
|
|
applet, redirect_stderr, strerror(errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-02-25 16:21:05 +05:30
|
|
|
/* We don't redirect stdin as some daemons may need it */
|
|
|
|
if (background || quiet || redirect_stdout)
|
2008-03-16 22:30:56 +05:30
|
|
|
dup2(stdout_fd, STDOUT_FILENO);
|
2008-02-25 16:21:05 +05:30
|
|
|
if (background || quiet || redirect_stderr)
|
2008-03-16 22:30:56 +05:30
|
|
|
dup2(stderr_fd, STDERR_FILENO);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
for (i = getdtablesize() - 1; i >= 3; --i)
|
2007-04-11 18:14:47 +05:30
|
|
|
close(i);
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
setsid();
|
2008-07-27 17:00:49 +05:30
|
|
|
execvp(exec, argv);
|
2007-04-05 16:48:42 +05:30
|
|
|
#ifdef HAVE_PAM
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pamr == PAM_SUCCESS)
|
2008-03-16 22:30:56 +05:30
|
|
|
pam_close_session(pamh, PAM_SILENT);
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
2008-09-27 05:28:57 +05:30
|
|
|
eerrorx("%s: failed to exec `%s': %s",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, exec,strerror(errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent process */
|
2008-07-27 17:00:49 +05:30
|
|
|
if (!background) {
|
2008-09-27 05:28:57 +05:30
|
|
|
/* As we're not backgrounding the process, wait for our pid
|
|
|
|
* to return */
|
2008-07-27 17:00:49 +05:30
|
|
|
i = 0;
|
|
|
|
spid = pid;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
do {
|
2008-07-27 17:00:49 +05:30
|
|
|
pid = waitpid(spid, &i, 0);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pid < 1) {
|
2009-04-24 03:01:22 +05:30
|
|
|
eerror("waitpid %d: %s",
|
|
|
|
spid, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
return -1;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-07-27 17:00:49 +05:30
|
|
|
} while (!WIFEXITED(i) && !WIFSIGNALED(i));
|
|
|
|
if (!WIFEXITED(i) || WEXITSTATUS(i) != 0) {
|
|
|
|
if (!quiet)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: failed to start `%s'",
|
|
|
|
applet, exec);
|
2008-03-16 22:30:56 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-07-27 17:00:49 +05:30
|
|
|
pid = spid;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a little bit and check that process is still running
|
|
|
|
We do this as some badly written daemons fork and then barf */
|
2009-04-19 23:50:49 +05:30
|
|
|
if (start_wait == 0 &&
|
|
|
|
((p = getenv("SSD_STARTWAIT")) ||
|
2009-04-24 03:01:22 +05:30
|
|
|
(p = rc_conf_value("rc_start_wait"))))
|
2008-09-27 05:28:57 +05:30
|
|
|
{
|
2009-04-19 23:50:49 +05:30
|
|
|
if (sscanf(p, "%u", &start_wait) != 1)
|
2008-09-27 05:28:57 +05:30
|
|
|
start_wait = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start_wait > 0) {
|
2007-08-09 20:03:20 +05:30
|
|
|
struct timespec ts;
|
|
|
|
bool alive = false;
|
2009-04-19 23:50:49 +05:30
|
|
|
|
|
|
|
ts.tv_sec = start_wait;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
if (nanosleep(&ts, NULL) == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
eerror("%s: caught an interrupt", applet);
|
|
|
|
else {
|
|
|
|
eerror("%s: nanosleep: %s",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, strerror(errno));
|
2009-04-19 23:50:49 +05:30
|
|
|
return 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
2009-04-19 23:50:49 +05:30
|
|
|
if (background) {
|
|
|
|
if (kill(pid, 0) == 0)
|
|
|
|
alive = true;
|
|
|
|
} else {
|
|
|
|
if (pidfile) {
|
|
|
|
pid = get_pid(pidfile, true);
|
|
|
|
if (pid == -1) {
|
|
|
|
eerrorx("%s: did not "
|
2009-04-24 03:01:22 +05:30
|
|
|
"create a valid"
|
|
|
|
" pid in `%s'",
|
|
|
|
applet, pidfile);
|
2009-04-19 23:50:49 +05:30
|
|
|
}
|
|
|
|
} else
|
|
|
|
pid = 0;
|
|
|
|
if (do_stop(exec, (const char *const *)margv,
|
2009-04-24 03:01:22 +05:30
|
|
|
pid, uid, 0, true, false, true) > 0)
|
2009-04-19 23:50:49 +05:30
|
|
|
alive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alive)
|
|
|
|
eerrorx("%s: %s died", applet, exec);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (svcname)
|
2009-04-24 03:01:22 +05:30
|
|
|
rc_service_daemon_set(svcname, exec,
|
|
|
|
(const char *const *)margv, pidfile, true);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
2008-01-22 16:37:39 +05:30
|
|
|
/* NOTREACHED */
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|