- add fancy mode to start-stop-daemon to support --oknodo and --verbose

This commit is contained in:
Bernhard Reutner-Fischer 2006-06-11 17:24:01 +00:00
parent 081b1ac6b4
commit a926f8e50a
3 changed files with 72 additions and 47 deletions

View File

@ -61,6 +61,15 @@ config CONFIG_START_STOP_DAEMON
termination of system-level processes, usually the ones termination of system-level processes, usually the ones
started during the startup of the system. started during the startup of the system.
config CONFIG_FEATURE_START_STOP_DAEMON_FANCY
bool "Support additional arguments"
default y
depends on CONFIG_START_STOP_DAEMON
help
Support additional arguments.
-o|--oknodo ignored since we exit with 0 anyway
-v|--verbose
config CONFIG_FEATURE_START_STOP_DAEMON_LONG_OPTIONS config CONFIG_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
bool "Enable long options" bool "Enable long options"
default n default n

View File

@ -22,7 +22,7 @@
static int signal_nr = 15; static int signal_nr = 15;
static int user_id = -1; static int user_id = -1;
static int quiet = 0; static int quiet;
static char *userspec = NULL; static char *userspec = NULL;
static char *cmdname = NULL; static char *cmdname = NULL;
static char *execname = NULL; static char *execname = NULL;
@ -35,8 +35,7 @@ struct pid_list {
static struct pid_list *found = NULL; static struct pid_list *found = NULL;
static inline void static inline void push(pid_t pid)
push(pid_t pid)
{ {
struct pid_list *p; struct pid_list *p;
@ -46,8 +45,7 @@ push(pid_t pid)
found = p; found = p;
} }
static int static int pid_is_exec(pid_t pid, const char *name)
pid_is_exec(pid_t pid, const char *name)
{ {
char buf[32]; char buf[32];
struct stat sb, exec_stat; struct stat sb, exec_stat;
@ -61,8 +59,7 @@ pid_is_exec(pid_t pid, const char *name)
return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino); return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
} }
static int static int pid_is_user(int pid, int uid)
pid_is_user(int pid, int uid)
{ {
struct stat sb; struct stat sb;
char buf[32]; char buf[32];
@ -73,8 +70,7 @@ pid_is_user(int pid, int uid)
return (sb.st_uid == uid); return (sb.st_uid == uid);
} }
static int static int pid_is_cmd(pid_t pid, const char *name)
pid_is_cmd(pid_t pid, const char *name)
{ {
char buf[32]; char buf[32];
FILE *f; FILE *f;
@ -98,8 +94,7 @@ pid_is_cmd(pid_t pid, const char *name)
} }
static void static void check(int pid)
check(int pid)
{ {
if (execname && !pid_is_exec(pid, execname)) { if (execname && !pid_is_exec(pid, execname)) {
return; return;
@ -114,8 +109,7 @@ check(int pid)
} }
static void static void do_pidfile(void)
do_pidfile(void)
{ {
FILE *f; FILE *f;
pid_t pid; pid_t pid;
@ -130,8 +124,7 @@ do_pidfile(void)
} }
static void static void do_procinit(void)
do_procinit(void)
{ {
DIR *procdir; DIR *procdir;
struct dirent *entry; struct dirent *entry;
@ -157,17 +150,16 @@ do_procinit(void)
} }
static void static int do_stop(void)
do_stop(void)
{ {
char what[1024]; RESERVE_CONFIG_BUFFER(what, 1024);
struct pid_list *p; struct pid_list *p;
int killed = 0; int killed = 0;
do_procinit(); do_procinit();
if (cmdname) if (cmdname)
strcpy(what, cmdname); what=cmdname;//strcpy(what, cmdname);
else if (execname) else if (execname)
strcpy(what, execname); strcpy(what, execname);
else if (pidfile) else if (pidfile)
@ -180,7 +172,9 @@ do_stop(void)
if (!found) { if (!found) {
if (!quiet) if (!quiet)
printf("no %s found; none killed.\n", what); printf("no %s found; none killed.\n", what);
return; if (ENABLE_FEATURE_CLEAN_UP)
RELEASE_CONFIG_BUFFER(what);
return -1;
} }
for (p = found; p; p = p->next) { for (p = found; p; p = p->next) {
if (kill(p->pid, signal_nr) == 0) { if (kill(p->pid, signal_nr) == 0) {
@ -197,21 +191,31 @@ do_stop(void)
printf(" %d", -p->pid); printf(" %d", -p->pid);
printf(").\n"); printf(").\n");
} }
if (ENABLE_FEATURE_CLEAN_UP)
RELEASE_CONFIG_BUFFER(what);
return killed;
} }
#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
static const struct option ssd_long_options[] = { static const struct option ssd_long_options[] = {
{ "stop", 0, NULL, 'K' }, { "stop", 0, NULL, 'K' },
{ "start", 0, NULL, 'S' }, { "start", 0, NULL, 'S' },
{ "background", 0, NULL, 'b' }, { "background", 0, NULL, 'b' },
{ "quiet", 0, NULL, 'q' }, { "quiet", 0, NULL, 'q' },
{ "make-pidfile", 0, NULL, 'm' }, { "make-pidfile", 0, NULL, 'm' },
{ "startas", 1, NULL, 'a' }, #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
{ "oknodo", 0, NULL, 'o' },
{ "verbose", 0, NULL, 'v' },
#endif
{ "startas", 1, NULL, 'a' },
{ "name", 1, NULL, 'n' }, { "name", 1, NULL, 'n' },
{ "signal", 1, NULL, 's' }, { "signal", 1, NULL, 's' },
{ "user", 1, NULL, 'u' }, { "user", 1, NULL, 'u' },
{ "exec", 1, NULL, 'x' }, { "exec", 1, NULL, 'x' },
{ "pidfile", 1, NULL, 'p' }, { "pidfile", 1, NULL, 'p' },
#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
{ "retry", 1, NULL, 'R' },
#endif
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
#endif #endif
@ -221,42 +225,48 @@ static const struct option ssd_long_options[] = {
#define SSD_OPT_BACKGROUND 4 #define SSD_OPT_BACKGROUND 4
#define SSD_OPT_QUIET 8 #define SSD_OPT_QUIET 8
#define SSD_OPT_MAKEPID 16 #define SSD_OPT_MAKEPID 16
#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
#define SSD_OPT_OKNODO 32
#define SSD_OPT_VERBOSE 64
int #endif
start_stop_daemon_main(int argc, char **argv)
int start_stop_daemon_main(int argc, char **argv)
{ {
unsigned long opt; unsigned long opt;
char *signame = NULL; char *signame = NULL;
char *startas = NULL; char *startas = NULL;
#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
// char *retry_arg = NULL;
// int retries = -1;
#endif
#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
bb_applet_long_options = ssd_long_options; bb_applet_long_options = ssd_long_options;
#endif #endif
/* Check required one context option was given */ /* Check required one context option was given */
bb_opt_complementally = "K:S:?:K--S:S--K"; bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:", opt = bb_getopt_ulflags(argc, argv, "KSbqm"
&startas, &cmdname, &signame, &userspec, &execname, &pidfile); // USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
"a:n:s:u:x:p:"
// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
,&startas, &cmdname, &signame, &userspec, &execname, &pidfile);
quiet = (opt & SSD_OPT_QUIET)
quiet = opt & SSD_OPT_QUIET; USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
if (signame) { if (signame) {
signal_nr = bb_xgetlarg(signame, 10, 0, NSIG); signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
} }
if (!execname && !pidfile && !userspec && !cmdname)
bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n");
if (!startas) if (!startas)
startas = execname; startas = execname;
if ((opt & SSD_CTX_START) && !startas) // USE_FEATURE_START_STOP_DAEMON_FANCY(
bb_error_msg_and_die ("-S needs -x or -a"); // if (retry_arg)
// retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL) // )
bb_error_msg_and_die ("-m needs -p");
argc -= optind; argc -= optind;
argv += optind; argv += optind;
@ -264,8 +274,10 @@ start_stop_daemon_main(int argc, char **argv)
user_id = bb_xgetpwnam(userspec); user_id = bb_xgetpwnam(userspec);
if (opt & SSD_CTX_STOP) { if (opt & SSD_CTX_STOP) {
do_stop(); int i = do_stop();
return EXIT_SUCCESS; return
USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
? 0 :) !!(i<=0);
} }
do_procinit(); do_procinit();
@ -273,7 +285,8 @@ start_stop_daemon_main(int argc, char **argv)
if (found) { if (found) {
if (!quiet) if (!quiet)
printf("%s already running.\n%d\n", execname ,found->pid); printf("%s already running.\n%d\n", execname ,found->pid);
return EXIT_SUCCESS; USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
} }
*--argv = startas; *--argv = startas;
if (opt & SSD_OPT_BACKGROUND) { if (opt & SSD_OPT_BACKGROUND) {
@ -282,10 +295,9 @@ start_stop_daemon_main(int argc, char **argv)
} }
if (opt & SSD_OPT_MAKEPID) { if (opt & SSD_OPT_MAKEPID) {
/* user wants _us_ to make the pidfile */ /* user wants _us_ to make the pidfile */
FILE *pidf = fopen(pidfile, "w"); FILE *pidf = bb_xfopen(pidfile, "w");
pid_t pidt = getpid(); pid_t pidt = getpid();
if (pidf == NULL)
bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile);
fprintf(pidf, "%d\n", pidt); fprintf(pidf, "%d\n", pidt);
fclose(pidf); fclose(pidf);
} }

View File

@ -2681,6 +2681,10 @@ USE_FEATURE_DATE_ISOFMT( \
"\n\t-n|--name <process-name>\tstop processes with this name" \ "\n\t-n|--name <process-name>\tstop processes with this name" \
"\n\t-p|--pidfile <pid-file>\t\tsave or load pid using a pid-file" \ "\n\t-p|--pidfile <pid-file>\t\tsave or load pid using a pid-file" \
"\n\t-q|--quiet\t\t\tbe quiet" \ "\n\t-q|--quiet\t\t\tbe quiet" \
USE_FEATURE_START_STOP_DAEMON_FANCY( \
"\n\t-o|--oknodo\t\t\texit status 0 if nothing done" \
"\n\t-v|--verbose\t\t\tbe verbose" \
) \
"\n\t-s|--signal <signal>\t\tsignal to send (default TERM)" "\n\t-s|--signal <signal>\t\tsignal to send (default TERM)"
#ifdef CONFIG_FEATURE_STAT_FORMAT #ifdef CONFIG_FEATURE_STAT_FORMAT