Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/debianutils
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- Location:
- branches/stable/mindi-busybox/debianutils
- Files:
-
- 3 deleted
- 6 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/debianutils/Config.in
r821 r1770 6 6 menu "Debian Utilities" 7 7 8 config CONFIG_MKTEMP8 config MKTEMP 9 9 bool "mktemp" 10 10 default n … … 12 12 mktemp is used to create unique temporary files 13 13 14 config CONFIG_PIPE_PROGRESS14 config PIPE_PROGRESS 15 15 bool "pipe_progress" 16 16 default n … … 18 18 Display a dot to indicate pipe activity. 19 19 20 config CONFIG_READLINK 21 bool "readlink" 22 default n 23 help 24 This program reads a symbolic link and returns the name 25 of the file it points to 26 27 config CONFIG_FEATURE_READLINK_FOLLOW 28 bool "Enable canonicalization by following all symlinks (-f)" 29 default n 30 depends on CONFIG_READLINK 31 help 32 Enable the readlink option (-f). 33 34 config CONFIG_RUN_PARTS 20 config RUN_PARTS 35 21 bool "run-parts" 36 22 default n … … 47 33 you can safely say N here. 48 34 49 config CONFIG_FEATURE_RUN_PARTS_LONG_OPTIONS35 config FEATURE_RUN_PARTS_LONG_OPTIONS 50 36 bool "Enable long options" 51 37 default n 52 depends on CONFIG_RUN_PARTS && CONFIG_GETOPT_LONG38 depends on RUN_PARTS && GETOPT_LONG 53 39 help 54 40 Support long options for the run-parts applet. 55 41 56 config CONFIG_START_STOP_DAEMON 42 config FEATURE_RUN_PARTS_FANCY 43 bool "Support additional arguments" 44 default n 45 depends on RUN_PARTS 46 help 47 Support additional options: 48 -l --list print the names of the all matching files (not 49 limited to executables), but don't actually run them. 50 51 config START_STOP_DAEMON 57 52 bool "start-stop-daemon" 58 53 default y … … 62 57 started during the startup of the system. 63 58 64 config CONFIG_FEATURE_START_STOP_DAEMON_FANCY59 config FEATURE_START_STOP_DAEMON_FANCY 65 60 bool "Support additional arguments" 66 61 default y 67 depends on CONFIG_START_STOP_DAEMON62 depends on START_STOP_DAEMON 68 63 help 69 64 Support additional arguments. … … 71 66 -v|--verbose 72 67 73 config CONFIG_FEATURE_START_STOP_DAEMON_LONG_OPTIONS68 config FEATURE_START_STOP_DAEMON_LONG_OPTIONS 74 69 bool "Enable long options" 75 70 default n 76 depends on CONFIG_START_STOP_DAEMON && CONFIG_GETOPT_LONG71 depends on START_STOP_DAEMON && GETOPT_LONG 77 72 help 78 73 Support long options for the start-stop-daemon applet. 79 74 80 config CONFIG_WHICH75 config WHICH 81 76 bool "which" 82 77 default n -
branches/stable/mindi-busybox/debianutils/mktemp.c
r821 r1770 10 10 */ 11 11 12 #include "busybox.h" 13 #include <stdio.h> 14 #include <errno.h> 15 #include <string.h> 16 #include <unistd.h> 17 #include <stdlib.h> 12 #include "libbb.h" 18 13 14 int mktemp_main(int argc, char **argv); 19 15 int mktemp_main(int argc, char **argv) 20 16 { 21 unsigned long flags = bb_getopt_ulflags(argc, argv, "dq"); 17 unsigned long flags = getopt32(argv, "dqt"); 18 char *chp; 22 19 23 20 if (optind + 1 != argc) 24 21 bb_show_usage(); 25 22 23 chp = argv[optind]; 24 25 if (flags & 4) { 26 char *dir = getenv("TMPDIR"); 27 if (dir && *dir != '\0') 28 chp = concat_path_file(dir, chp); 29 else 30 chp = concat_path_file("/tmp/", chp); 31 } 32 26 33 if (flags & 1) { 27 if (mkdtemp( argv[optind]) == NULL)34 if (mkdtemp(chp) == NULL) 28 35 return EXIT_FAILURE; 29 } 30 else { 31 if (mkstemp(argv[optind]) < 0) 36 } else { 37 if (mkstemp(chp) < 0) 32 38 return EXIT_FAILURE; 33 39 } 34 40 35 puts( argv[optind]);41 puts(chp); 36 42 37 43 return EXIT_SUCCESS; -
branches/stable/mindi-busybox/debianutils/pipe_progress.c
r821 r1770 8 8 */ 9 9 10 #include "busybox.h" 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <unistd.h> 14 #include <time.h> 10 #include "libbb.h" 15 11 16 12 #define PIPE_PROGRESS_SIZE 4096 … … 19 15 * Activity is indicated by a '.' to stderr 20 16 */ 17 int pipe_progress_main(int argc, char **argv); 21 18 int pipe_progress_main(int argc, char **argv) 22 19 { -
branches/stable/mindi-busybox/debianutils/run_parts.c
r821 r1770 3 3 * Mini run-parts implementation for busybox 4 4 * 5 * Copyright (C) 2007 Bernhard Fischer 5 6 * 6 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> 7 * Based on a older version that was in busybox which was 1k big.. 8 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> 7 9 * 8 10 * Based on the Debian run-parts program, version 1.15 … … 11 13 * 12 14 * 13 * Licensed under GPL v2 , see file LICENSE in this tarball for details.15 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. 14 16 */ 15 17 … … 26 28 * -a ARG argument. Pass ARG as an argument the program executed. It can 27 29 * be repeated to pass multiple arguments. 28 * -u MASK umask. Set the umask of the program executed to MASK. */ 29 30 /* TODO 31 * done - convert calls to error in perror... and remove error() 32 * done - convert malloc/realloc to their x... counterparts 33 * done - remove catch_sigchld 34 * done - use bb's concat_path_file() 35 * done - declare run_parts_main() as extern and any other function as static? 30 * -u MASK umask. Set the umask of the program executed to MASK. 36 31 */ 37 32 38 #include "busybox.h"39 33 #include <getopt.h> 40 #include <stdlib.h>41 34 35 #include "libbb.h" 42 36 43 static const struct option runparts_long_options[] = { 44 { "test", 0, NULL, 't' }, 45 { "umask", 1, NULL, 'u' }, 46 { "arg", 1, NULL, 'a' }, 47 { 0, 0, 0, 0 } 37 #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS 38 static const char runparts_longopts[] ALIGN1 = 39 "arg\0" Required_argument "a" 40 "umask\0" Required_argument "u" 41 "test\0" No_argument "t" 42 #if ENABLE_FEATURE_RUN_PARTS_FANCY 43 "list\0" No_argument "l" 44 //TODO: "reverse\0" No_argument "r" 45 //TODO: "verbose\0" No_argument "v" 46 #endif 47 ; 48 #endif 49 50 struct globals { 51 smalluint mode; 52 char *cmd[10]; /* merely arbitrary arg count */ 48 53 }; 54 #define G (*(struct globals*)&bb_common_bufsiz1) 49 55 50 extern char **environ; 56 /* valid_name */ 57 /* True or false? Is this a valid filename (upper/lower alpha, digits, 58 * underscores, and hyphens only?) 59 */ 60 static bool invalid_name(const char *c) 61 { 62 c = bb_basename(c); 51 63 52 /* run_parts_main */ 53 /* Process options */ 64 while (*c && (isalnum(*c) || *c == '_' || *c == '-')) 65 c++; 66 67 return *c; /* TRUE (!0) if terminating NUL is not reached */ 68 } 69 70 #define RUN_PARTS_OPT_a (1<<0) 71 #define RUN_PARTS_OPT_u (1<<1) 72 #define RUN_PARTS_OPT_t (1<<2) 73 #if ENABLE_FEATURE_RUN_PARTS_FANCY 74 #define RUN_PARTS_OPT_l (1<<3) 75 #endif 76 77 #define test_mode (G.mode & RUN_PARTS_OPT_t) 78 #if ENABLE_FEATURE_RUN_PARTS_FANCY 79 #define list_mode (G.mode & RUN_PARTS_OPT_l) 80 #else 81 #define list_mode (0) 82 #endif 83 84 static int act(const char *file, struct stat *statbuf, void *args, int depth) 85 { 86 int ret; 87 88 if (depth == 1) 89 return TRUE; 90 91 if (depth == 2 && 92 ((!list_mode && access(file, X_OK)) || 93 invalid_name(file) || 94 !(statbuf->st_mode & (S_IFREG | S_IFLNK))) ) 95 return SKIP; 96 97 if (test_mode || list_mode) { 98 puts(file); 99 return TRUE; 100 } 101 G.cmd[0] = (char*)file; 102 ret = wait4pid(spawn(G.cmd)); 103 if (ret < 0) { 104 bb_perror_msg("failed to exec %s", file); 105 } else if (ret > 0) { 106 bb_error_msg("%s exited with return code %d", file, ret); 107 } 108 return !ret; 109 } 110 111 int run_parts_main(int argc, char **argv); 54 112 int run_parts_main(int argc, char **argv) 55 113 { 56 char **args = xmalloc(2 * sizeof(char *)); 57 unsigned char test_mode = 0; 58 unsigned short argcount = 1; 59 int opt; 114 char *umask_p; 115 llist_t *arg_list = NULL; 116 unsigned tmp; 60 117 61 118 umask(022); 62 63 while ((opt = getopt_long (argc, argv, "tu:a:", 64 runparts_long_options, NULL)) > 0) 65 { 66 switch (opt) { 67 /* Enable test mode */ 68 case 't': 69 test_mode++; 70 break; 71 /* Set the umask of the programs executed */ 72 case 'u': 73 /* Check and set the umask of the program executed. As stated in the original 74 * run-parts, the octal conversion in libc is not foolproof; it will take the 75 * 8 and 9 digits under some circumstances. We'll just have to live with it. 76 */ 77 umask(bb_xgetlarg(optarg, 8, 0, 07777)); 78 break; 79 /* Pass an argument to the programs */ 80 case 'a': 81 /* Add an argument to the commands that we will call. 82 * Called once for every argument. */ 83 args = xrealloc(args, (argcount + 2) * (sizeof(char *))); 84 args[argcount++] = optarg; 85 break; 86 default: 87 bb_show_usage(); 88 } 119 /* We require exactly one argument: the directory name */ 120 opt_complementary = "=1:a::"; 121 #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS 122 applet_long_options = runparts_longopts; 123 #endif 124 tmp = getopt32(argv, "a:u:t"USE_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p); 125 G.mode = tmp &~ (RUN_PARTS_OPT_a|RUN_PARTS_OPT_u); 126 if (tmp & RUN_PARTS_OPT_u) { 127 /* Check and set the umask of the program executed. 128 * As stated in the original run-parts, the octal conversion in 129 * libc is not foolproof; it will take the 8 and 9 digits under 130 * some circumstances. We'll just have to live with it. 131 */ 132 umask(xstrtoul_range(umask_p, 8, 0, 07777)); 89 133 } 90 91 /* We require exactly one argument: the directory name */ 92 if (optind != (argc - 1)) { 93 bb_show_usage(); 94 } 95 96 args[0] = argv[optind]; 97 args[argcount] = 0; 98 99 return(run_parts(args, test_mode, environ)); 134 for (tmp = 1; arg_list; arg_list = arg_list->link, tmp++) 135 G.cmd[tmp] = arg_list->data; 136 /* G.cmd[tmp] = NULL; - G is already zeroed out */ 137 if (!recursive_action(argv[argc - 1], 138 ACTION_RECURSE|ACTION_FOLLOWLINKS, 139 act, /* file action */ 140 act, /* dir action */ 141 NULL, /* user data */ 142 1 /* depth */ 143 )) 144 return EXIT_FAILURE; 145 return EXIT_SUCCESS; 100 146 } -
branches/stable/mindi-busybox/debianutils/start_stop_daemon.c
r821 r1770 4 4 * 5 5 * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, 6 * public domain.7 6 * Adapted for busybox David Kimdon <dwhedon@gordian.com> 7 * 8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 8 9 */ 9 10 10 #include "busybox.h" 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <stdarg.h> 15 #include <signal.h> 16 #include <errno.h> 17 #include <sys/stat.h> 18 #include <dirent.h> 19 #include <unistd.h> 20 #include <getopt.h> /* struct option */ 21 #include "pwd_.h" 11 /* NB: we have a problem here with /proc/NN/exe usage, similar to 12 * one fixed in killall/pidof */ 13 14 #include <getopt.h> 15 #include <sys/resource.h> 16 17 /* Override ENABLE_FEATURE_PIDFILE */ 18 #define WANT_PIDFILE 1 19 #include "libbb.h" 22 20 23 21 static int signal_nr = 15; 24 22 static int user_id = -1; 25 static int quiet;26 static char * userspec = NULL;27 static char * cmdname = NULL;28 static char * execname = NULL;29 static char *pidfile = NULL;23 static char *userspec; 24 static char *cmdname; 25 static char *execname; 26 static char *pidfile; 27 static smallint quiet; 30 28 31 29 struct pid_list { … … 34 32 }; 35 33 36 static struct pid_list *found = NULL; 37 38 static inline void push(pid_t pid) 34 static struct pid_list *found; 35 36 static int pid_is_exec(pid_t pid, const char *name) 37 { 38 char buf[sizeof("/proc//exe") + sizeof(int)*3]; 39 char *execbuf; 40 int n; 41 42 sprintf(buf, "/proc/%u/exe", pid); 43 n = strlen(name) + 1; 44 execbuf = xzalloc(n + 1); 45 readlink(buf, execbuf, n); 46 47 /* if readlink fails, execbuf still contains "" */ 48 n = strcmp(execbuf, name); 49 if (ENABLE_FEATURE_CLEAN_UP) 50 free(execbuf); 51 return !n; /* nonzero (true) if execbuf == name */ 52 } 53 54 static int pid_is_user(int pid, int uid) 55 { 56 struct stat sb; 57 char buf[sizeof("/proc/") + sizeof(int)*3]; 58 59 sprintf(buf, "/proc/%u", pid); 60 if (stat(buf, &sb) != 0) 61 return 0; 62 return (sb.st_uid == uid); 63 } 64 65 static int pid_is_cmd(pid_t pid, const char *name) 66 { 67 char fname[sizeof("/proc//stat") + sizeof(int)*3]; 68 char *buf; 69 int r = 0; 70 71 sprintf(fname, "/proc/%u/stat", pid); 72 buf = xmalloc_open_read_close(fname, NULL); 73 if (buf) { 74 char *p = strchr(buf, '('); 75 if (p) { 76 char *pe = strrchr(++p, ')'); 77 if (pe) { 78 *pe = '\0'; 79 r = !strcmp(p, name); 80 } 81 } 82 free(buf); 83 } 84 return r; 85 } 86 87 static void check(int pid) 39 88 { 40 89 struct pid_list *p; 41 90 91 if (execname && !pid_is_exec(pid, execname)) { 92 return; 93 } 94 if (userspec && !pid_is_user(pid, user_id)) { 95 return; 96 } 97 if (cmdname && !pid_is_cmd(pid, cmdname)) { 98 return; 99 } 42 100 p = xmalloc(sizeof(*p)); 43 101 p->next = found; … … 46 104 } 47 105 48 static int pid_is_exec(pid_t pid, const char *name) 49 { 50 char buf[32]; 51 struct stat sb, exec_stat; 52 53 if (name) 54 xstat(name, &exec_stat); 55 56 sprintf(buf, "/proc/%d/exe", pid); 57 if (stat(buf, &sb) != 0) 58 return 0; 59 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino); 60 } 61 62 static int pid_is_user(int pid, int uid) 63 { 64 struct stat sb; 65 char buf[32]; 66 67 sprintf(buf, "/proc/%d", pid); 68 if (stat(buf, &sb) != 0) 69 return 0; 70 return (sb.st_uid == uid); 71 } 72 73 static int pid_is_cmd(pid_t pid, const char *name) 74 { 75 char buf[32]; 106 static void do_pidfile(void) 107 { 76 108 FILE *f; 77 int c; 78 79 sprintf(buf, "/proc/%d/stat", pid); 80 f = fopen(buf, "r"); 81 if (!f) 82 return 0; 83 while ((c = getc(f)) != EOF && c != '(') 84 ; 85 if (c != '(') { 86 fclose(f); 87 return 0; 88 } 89 /* this hopefully handles command names containing ')' */ 90 while ((c = getc(f)) != EOF && c == *name) 91 name++; 92 fclose(f); 93 return (c == ')' && *name == '\0'); 94 } 95 96 97 static void check(int pid) 98 { 99 if (execname && !pid_is_exec(pid, execname)) { 100 return; 101 } 102 if (userspec && !pid_is_user(pid, user_id)) { 103 return; 104 } 105 if (cmdname && !pid_is_cmd(pid, cmdname)) { 106 return; 107 } 108 push(pid); 109 } 110 111 112 static void do_pidfile(void) 113 { 114 FILE *f; 115 pid_t pid; 109 unsigned pid; 116 110 117 111 f = fopen(pidfile, "r"); 118 112 if (f) { 119 if (fscanf(f, "% d", &pid) == 1)113 if (fscanf(f, "%u", &pid) == 1) 120 114 check(pid); 121 115 fclose(f); 122 116 } else if (errno != ENOENT) 123 117 bb_perror_msg_and_die("open pidfile %s", pidfile); 124 125 118 } 126 119 … … 136 129 } 137 130 138 procdir = bb_xopendir("/proc");131 procdir = xopendir("/proc"); 139 132 140 133 foundany = 0; 141 134 while ((entry = readdir(procdir)) != NULL) { 142 if (sscanf(entry->d_name, "%d", &pid) != 1) 135 pid = bb_strtou(entry->d_name, NULL, 10); 136 if (errno) 143 137 continue; 144 138 foundany++; … … 147 141 closedir(procdir); 148 142 if (!foundany) 149 bb_error_msg_and_die ("nothing in /proc - not mounted?"); 150 } 151 143 bb_error_msg_and_die("nothing in /proc - not mounted?"); 144 } 152 145 153 146 static int do_stop(void) 154 147 { 155 RESERVE_CONFIG_BUFFER(what, 1024);148 char *what; 156 149 struct pid_list *p; 157 150 int killed = 0; … … 159 152 do_procinit(); 160 153 161 if (cmdname) 162 strcpy(what, cmdname); 163 else if (execname) 164 strcpy(what, execname); 165 else if (pidfile) 166 sprintf(what, "process in pidfile `%.200s'", pidfile); 154 if (cmdname) { 155 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname); 156 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname; 157 } else if (execname) { 158 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname); 159 if (!ENABLE_FEATURE_CLEAN_UP) what = execname; 160 } else if (pidfile) 161 what = xasprintf("process in pidfile '%s'", pidfile); 167 162 else if (userspec) 168 sprintf(what, "process(es) owned by `%s'", userspec);163 what = xasprintf("process(es) owned by '%s'", userspec); 169 164 else 170 bb_error_msg_and_die 165 bb_error_msg_and_die("internal error, please report"); 171 166 172 167 if (!found) { 173 168 if (!quiet) 174 printf("no %s found; none killed.\n", what); 175 if (ENABLE_FEATURE_CLEAN_UP) 176 RELEASE_CONFIG_BUFFER(what); 177 return -1; 169 printf("no %s found; none killed\n", what); 170 killed = -1; 171 goto ret; 178 172 } 179 173 for (p = found; p; p = p->next) { 180 174 if (kill(p->pid, signal_nr) == 0) { 181 p->pid = - p->pid;175 p->pid = - p->pid; 182 176 killed++; 183 177 } else { 184 bb_perror_msg("warning: failed to kill %d", p->pid);178 bb_perror_msg("warning: killing process %u", p->pid); 185 179 } 186 180 } … … 188 182 printf("stopped %s (pid", what); 189 183 for (p = found; p; p = p->next) 190 if(p->pid < 0) 191 printf(" %d", -p->pid); 192 printf(").\n"); 193 } 184 if (p->pid < 0) 185 printf(" %u", - p->pid); 186 puts(")"); 187 } 188 ret: 194 189 if (ENABLE_FEATURE_CLEAN_UP) 195 RELEASE_CONFIG_BUFFER(what);190 free(what); 196 191 return killed; 197 192 } 198 193 199 194 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS 200 static const struct option ssd_long_options[] = {201 { "stop", 0, NULL, 'K' },202 { "start", 0, NULL, 'S' },203 { "background", 0, NULL, 'b' },204 { "quiet", 0, NULL, 'q' },205 { "make-pidfile", 0, NULL, 'm' },195 static const char start_stop_daemon_longopts[] ALIGN1 = 196 "stop\0" No_argument "K" 197 "start\0" No_argument "S" 198 "background\0" No_argument "b" 199 "quiet\0" No_argument "q" 200 "make-pidfile\0" No_argument "m" 206 201 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 207 { "oknodo", 0, NULL, 'o' }, 208 { "verbose", 0, NULL, 'v' }, 209 #endif 210 { "startas", 1, NULL, 'a' }, 211 { "name", 1, NULL, 'n' }, 212 { "signal", 1, NULL, 's' }, 213 { "user", 1, NULL, 'u' }, 214 { "exec", 1, NULL, 'x' }, 215 { "pidfile", 1, NULL, 'p' }, 202 "oknodo\0" No_argument "o" 203 "verbose\0" No_argument "v" 204 "nicelevel\0" Required_argument "N" 205 #endif 206 "startas\0" Required_argument "a" 207 "name\0" Required_argument "n" 208 "signal\0" Required_argument "s" 209 "user\0" Required_argument "u" 210 "chuid\0" Required_argument "c" 211 "exec\0" Required_argument "x" 212 "pidfile\0" Required_argument "p" 216 213 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 217 { "retry", 1, NULL, 'R' }, 218 #endif 219 { 0, 0, 0, 0 } 214 "retry\0" Required_argument "R" 215 #endif 216 ; 217 #endif 218 219 enum { 220 CTX_STOP = 0x1, 221 CTX_START = 0x2, 222 OPT_BACKGROUND = 0x4, // -b 223 OPT_QUIET = 0x8, // -q 224 OPT_MAKEPID = 0x10, // -m 225 OPT_a = 0x20, // -a 226 OPT_n = 0x40, // -n 227 OPT_s = 0x80, // -s 228 OPT_u = 0x100, // -u 229 OPT_c = 0x200, // -c 230 OPT_x = 0x400, // -x 231 OPT_p = 0x800, // -p 232 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o 233 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v 234 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N 220 235 }; 221 #endif 222 223 #define SSD_CTX_STOP 1 224 #define SSD_CTX_START 2 225 #define SSD_OPT_BACKGROUND 4 226 #define SSD_OPT_QUIET 8 227 #define SSD_OPT_MAKEPID 16 228 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 229 #define SSD_OPT_OKNODO 32 230 #define SSD_OPT_VERBOSE 64 231 232 #endif 233 236 237 int start_stop_daemon_main(int argc, char **argv); 234 238 int start_stop_daemon_main(int argc, char **argv) 235 239 { 236 unsigned long opt; 237 char *signame = NULL; 238 char *startas = NULL; 240 unsigned opt; 241 char *signame; 242 char *startas; 243 char *chuid; 239 244 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 240 245 // char *retry_arg = NULL; 241 246 // int retries = -1; 247 char *opt_N; 242 248 #endif 243 249 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS 244 bb_applet_long_options = ssd_long_options;250 applet_long_options = start_stop_daemon_longopts; 245 251 #endif 246 252 247 253 /* Check required one context option was given */ 248 bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa"; 249 opt = bb_getopt_ulflags(argc, argv, "KSbqm" 250 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:") 251 USE_FEATURE_START_STOP_DAEMON_FANCY("ov") 252 "a:n:s:u:x:p:" 254 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"; 255 opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:" 256 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"), 257 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"), 258 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile 259 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) 253 260 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg) 254 ,&startas, &cmdname, &signame, &userspec, &execname, &pidfile);255 256 quiet = (opt & SSD_OPT_QUIET)257 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE)); 258 259 if (signame) {260 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);261 } 262 263 if (! startas)261 ); 262 263 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE); 264 265 if (opt & OPT_s) { 266 signal_nr = get_signum(signame); 267 if (signal_nr < 0) bb_show_usage(); 268 } 269 270 if (!(opt & OPT_a)) 264 271 startas = execname; 265 272 266 273 // USE_FEATURE_START_STOP_DAEMON_FANCY( 267 274 // if (retry_arg) 268 // retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);275 // retries = xatoi_u(retry_arg); 269 276 // ) 270 277 argc -= optind; 271 278 argv += optind; 272 279 273 if (userspec && sscanf(userspec, "%d", &user_id) != 1) 274 user_id = bb_xgetpwnam(userspec); 275 276 if (opt & SSD_CTX_STOP) { 280 if (userspec) { 281 user_id = bb_strtou(userspec, NULL, 10); 282 if (errno) 283 user_id = xuname2uid(userspec); 284 } 285 286 if (opt & CTX_STOP) { 277 287 int i = do_stop(); 278 return 279 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO) 280 ? 0 :) !!(i<=0); 288 return (opt & OPT_OKNODO) ? 0 : (i <= 0); 281 289 } 282 290 … … 285 293 if (found) { 286 294 if (!quiet) 287 printf("%s already running.\n%d\n", execname ,found->pid); 288 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);) 289 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;) 295 printf("%s already running\n%d\n", execname, found->pid); 296 return !(opt & OPT_OKNODO); 290 297 } 291 298 *--argv = startas; 292 if (opt & SSD_OPT_BACKGROUND) { 293 bb_xdaemon(0, 0); 294 setsid(); 295 } 296 if (opt & SSD_OPT_MAKEPID) { 299 if (opt & OPT_BACKGROUND) { 300 #if BB_MMU 301 bb_daemonize(0); 302 #else 303 pid_t pid = vfork(); 304 if (pid < 0) /* error */ 305 bb_perror_msg_and_die("vfork"); 306 if (pid != 0) { 307 /* parent */ 308 /* why _exit? the child may have changed the stack, 309 * so "return 0" may do bad things */ 310 _exit(0); 311 } 312 /* child */ 313 setsid(); /* detach from controlling tty */ 314 /* Redirect stdio to /dev/null, close extra FDs. 315 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */ 316 bb_daemonize_or_rexec( 317 DAEMON_DEVNULL_STDIO 318 + DAEMON_CLOSE_EXTRA_FDS 319 + DAEMON_ONLY_SANITIZE, 320 NULL /* argv, unused */ ); 321 #endif 322 } 323 if (opt & OPT_MAKEPID) { 297 324 /* user wants _us_ to make the pidfile */ 298 FILE *pidf = bb_xfopen(pidfile, "w"); 299 300 pid_t pidt = getpid(); 301 fprintf(pidf, "%d\n", pidt); 302 fclose(pidf); 303 } 325 write_pidfile(pidfile); 326 } 327 if (opt & OPT_c) { 328 struct bb_uidgid_t ugid; 329 parse_chown_usergroup_or_die(&ugid, chuid); 330 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid); 331 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid); 332 } 333 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY 334 if (opt & OPT_NICELEVEL) { 335 /* Set process priority */ 336 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2); 337 if (setpriority(PRIO_PROCESS, 0, prio) < 0) { 338 bb_perror_msg_and_die("setpriority(%d)", prio); 339 } 340 } 341 #endif 304 342 execv(startas, argv); 305 bb_perror_msg_and_die ("unable tostart %s", startas);306 } 343 bb_perror_msg_and_die("cannot start %s", startas); 344 } -
branches/stable/mindi-busybox/debianutils/which.c
r821 r1770 4 4 * 5 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu> 6 7 * 7 * Licensed under the GPL v2 , see the file LICENSE in this tarball.8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. 8 9 * 9 10 * Based on which from debianutils 10 11 */ 11 12 12 #include "busybox.h" 13 #include <string.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <unistd.h> 17 #include <sys/stat.h> 13 #include "libbb.h" 18 14 19 20 static int is_executable_file(const char const * a, struct stat *b) 21 { 22 return (!access(a,X_OK) && !stat(a, b) && S_ISREG(b->st_mode)); 23 } 24 15 int which_main(int argc, char **argv); 25 16 int which_main(int argc, char **argv) 26 17 { 27 int status; 28 size_t i, count; 29 char *path_list; 18 int status = EXIT_SUCCESS; 19 char *p; 30 20 31 if (argc <= 1 || **(argv + 1)== '-') {21 if (argc <= 1 || argv[1][0] == '-') { 32 22 bb_show_usage(); 33 23 } 34 argc--;35 24 36 path_list = getenv("PATH"); 37 if ( path_list != NULL) {38 size_t path_len = strlen(path_list);39 char *new_list = NULL;40 count = 1; 25 /* We shouldn't do this. Ever. Not our business. 26 if (!getenv("PATH")) { 27 putenv((char*)bb_PATH_root_path); 28 } 29 */ 41 30 42 for (i = 0; i <= path_len; i++) { 43 char *this_i = &path_list[i]; 44 if (*this_i == ':') { 45 /* ^::[^:] == \.: */ 46 if (!i && (*(this_i + 1) == ':')) { 47 *this_i = '.'; 48 continue; 49 } 50 *this_i = 0; 51 count++; 52 /* ^:[^:] == \.0 and [^:]::[^:] == 0\.0 and [^:]:$ == 0\.0 */ 53 if (!i || (*(this_i + 1) == ':') || (i == path_len-1)) { 54 new_list = xrealloc(new_list, path_len += 1); 55 if (i) { 56 memmove(&new_list[i+2], &path_list[i+1], path_len-i); 57 new_list[i+1] = '.'; 58 memmove(new_list, path_list, i); 59 } else { 60 memmove(&new_list[i+1], &path_list[i], path_len-i); 61 new_list[i] = '.'; 62 } 63 path_list = new_list; 64 } 31 while (--argc > 0) { 32 argv++; 33 if (strchr(*argv, '/')) { 34 if (execable_file(*argv)) { 35 puts(*argv); 36 continue; 37 } 38 } else { 39 p = find_execable(*argv); 40 if (p) { 41 puts(p); 42 free(p); 43 continue; 65 44 } 66 45 } 67 } else { 68 path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; 69 count = 5; 46 status = EXIT_FAILURE; 70 47 } 71 48 72 status = EXIT_SUCCESS; 73 while (argc-- > 0) { 74 struct stat stat_b; 75 char *buf; 76 char *path_n; 77 int found = 0; 78 79 argv++; 80 path_n = path_list; 81 buf = *argv; 82 83 /* if filename is either absolute or contains slashes, 84 * stat it */ 85 if (strchr(buf, '/') != NULL && is_executable_file(buf, &stat_b)) { 86 found++; 87 } else { 88 /* Couldn't access file and file doesn't contain slashes */ 89 for (i = 0; i < count; i++) { 90 buf = concat_path_file(path_n, *argv); 91 if (is_executable_file(buf, &stat_b)) { 92 found++; 93 break; 94 } 95 free(buf); 96 path_n += (strlen(path_n) + 1); 97 } 98 } 99 if (found) { 100 puts(buf); 101 } else { 102 status = EXIT_FAILURE; 103 } 104 } 105 bb_fflush_stdout_and_exit(status); 49 fflush_stdout_and_exit(status); 106 50 }
Note:
See TracChangeset
for help on using the changeset viewer.