Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/debianutils


Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (16 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

Location:
branches/2.2.5/mindi-busybox/debianutils
Files:
1 added
3 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/debianutils/Config.in

    r821 r1765  
    66menu "Debian Utilities"
    77
    8 config CONFIG_MKTEMP
     8config MKTEMP
    99    bool "mktemp"
    1010    default n
     
    1212      mktemp is used to create unique temporary files
    1313
    14 config CONFIG_PIPE_PROGRESS
     14config PIPE_PROGRESS
    1515    bool "pipe_progress"
    1616    default n
     
    1818      Display a dot to indicate pipe activity.
    1919
    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
     20config RUN_PARTS
    3521    bool "run-parts"
    3622    default n
     
    4733      you can safely say N here.
    4834
    49 config CONFIG_FEATURE_RUN_PARTS_LONG_OPTIONS
     35config FEATURE_RUN_PARTS_LONG_OPTIONS
    5036    bool "Enable long options"
    5137    default n
    52     depends on CONFIG_RUN_PARTS && CONFIG_GETOPT_LONG
     38    depends on RUN_PARTS && GETOPT_LONG
    5339    help
    5440      Support long options for the run-parts applet.
    5541
    56 config CONFIG_START_STOP_DAEMON
     42config 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
     51config START_STOP_DAEMON
    5752    bool "start-stop-daemon"
    5853    default y
     
    6257      started during the startup of the system.
    6358
    64 config CONFIG_FEATURE_START_STOP_DAEMON_FANCY
     59config FEATURE_START_STOP_DAEMON_FANCY
    6560    bool "Support additional arguments"
    6661    default y
    67     depends on CONFIG_START_STOP_DAEMON
     62    depends on START_STOP_DAEMON
    6863    help
    6964      Support additional arguments.
     
    7166      -v|--verbose
    7267
    73 config CONFIG_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
     68config FEATURE_START_STOP_DAEMON_LONG_OPTIONS
    7469    bool "Enable long options"
    7570    default n
    76     depends on CONFIG_START_STOP_DAEMON && CONFIG_GETOPT_LONG
     71    depends on START_STOP_DAEMON && GETOPT_LONG
    7772    help
    7873      Support long options for the start-stop-daemon applet.
    7974
    80 config CONFIG_WHICH
     75config WHICH
    8176    bool "which"
    8277    default n
  • branches/2.2.5/mindi-busybox/debianutils/mktemp.c

    r821 r1765  
    1010 */
    1111
    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"
    1813
     14int mktemp_main(int argc, char **argv);
    1915int mktemp_main(int argc, char **argv)
    2016{
    21     unsigned long flags = bb_getopt_ulflags(argc, argv, "dq");
     17    unsigned long flags = getopt32(argv, "dqt");
     18    char *chp;
    2219
    2320    if (optind + 1 != argc)
    2421        bb_show_usage();
    2522
     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
    2633    if (flags & 1) {
    27         if (mkdtemp(argv[optind]) == NULL)
     34        if (mkdtemp(chp) == NULL)
    2835            return EXIT_FAILURE;
    29     }
    30     else {
    31         if (mkstemp(argv[optind]) < 0)
     36    } else {
     37        if (mkstemp(chp) < 0)
    3238            return EXIT_FAILURE;
    3339    }
    3440
    35     puts(argv[optind]);
     41    puts(chp);
    3642
    3743    return EXIT_SUCCESS;
  • branches/2.2.5/mindi-busybox/debianutils/pipe_progress.c

    r821 r1765  
    88 */
    99
    10 #include "busybox.h"
    11 #include <stdio.h>
    12 #include <stdlib.h>
    13 #include <unistd.h>
    14 #include <time.h>
     10#include "libbb.h"
    1511
    1612#define PIPE_PROGRESS_SIZE 4096
     
    1915 * Activity is indicated by a '.' to stderr
    2016 */
     17int pipe_progress_main(int argc, char **argv);
    2118int pipe_progress_main(int argc, char **argv)
    2219{
  • branches/2.2.5/mindi-busybox/debianutils/run_parts.c

    r821 r1765  
    33 * Mini run-parts implementation for busybox
    44 *
     5 * Copyright (C) 2007 Bernhard Fischer
    56 *
    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>
    79 *
    810 * Based on the Debian run-parts program, version 1.15
     
    1113 *
    1214 *
    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.
    1416 */
    1517
     
    2628 * -a ARG       argument. Pass ARG as an argument the program executed. It can
    2729 *              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.
    3631 */
    3732
    38 #include "busybox.h"
    3933#include <getopt.h>
    40 #include <stdlib.h>
    4134
     35#include "libbb.h"
    4236
    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
     38static 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
     50struct globals {
     51    smalluint mode;
     52    char *cmd[10]; /* merely arbitrary arg count */
    4853};
     54#define G (*(struct globals*)&bb_common_bufsiz1)
    4955
    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 */
     60static bool invalid_name(const char *c)
     61{
     62    c = bb_basename(c);
    5163
    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
     84static 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
     111int run_parts_main(int argc, char **argv);
    54112int run_parts_main(int argc, char **argv)
    55113{
    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;
    60117
    61118    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));
    89133    }
    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;
    100146}
  • branches/2.2.5/mindi-busybox/debianutils/start_stop_daemon.c

    r821 r1765  
    44 *
    55 * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
    6  * public domain.
    76 * Adapted for busybox David Kimdon <dwhedon@gordian.com>
     7 *
     8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
    89 */
    910
    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"
    2220
    2321static int signal_nr = 15;
    2422static 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;
     23static char *userspec;
     24static char *cmdname;
     25static char *execname;
     26static char *pidfile;
     27static smallint quiet;
    3028
    3129struct pid_list {
     
    3432};
    3533
    36 static struct pid_list *found = NULL;
    37 
    38 static inline void push(pid_t pid)
     34static struct pid_list *found;
     35
     36static 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
     54static 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
     65static 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
     87static void check(int pid)
    3988{
    4089    struct pid_list *p;
    4190
     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    }
    42100    p = xmalloc(sizeof(*p));
    43101    p->next = found;
     
    46104}
    47105
    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];
     106static void do_pidfile(void)
     107{
    76108    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;
    116110
    117111    f = fopen(pidfile, "r");
    118112    if (f) {
    119         if (fscanf(f, "%d", &pid) == 1)
     113        if (fscanf(f, "%u", &pid) == 1)
    120114            check(pid);
    121115        fclose(f);
    122116    } else if (errno != ENOENT)
    123117        bb_perror_msg_and_die("open pidfile %s", pidfile);
    124 
    125118}
    126119
     
    136129    }
    137130
    138     procdir = bb_xopendir("/proc");
     131    procdir = xopendir("/proc");
    139132
    140133    foundany = 0;
    141134    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)
    143137            continue;
    144138        foundany++;
     
    147141    closedir(procdir);
    148142    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}
    152145
    153146static int do_stop(void)
    154147{
    155     RESERVE_CONFIG_BUFFER(what, 1024);
     148    char *what;
    156149    struct pid_list *p;
    157150    int killed = 0;
     
    159152    do_procinit();
    160153
    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);
    167162    else if (userspec)
    168         sprintf(what, "process(es) owned by `%s'", userspec);
     163        what = xasprintf("process(es) owned by '%s'", userspec);
    169164    else
    170         bb_error_msg_and_die ("internal error, please report");
     165        bb_error_msg_and_die("internal error, please report");
    171166
    172167    if (!found) {
    173168        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;
    178172    }
    179173    for (p = found; p; p = p->next) {
    180174        if (kill(p->pid, signal_nr) == 0) {
    181             p->pid = -p->pid;
     175            p->pid = - p->pid;
    182176            killed++;
    183177        } else {
    184             bb_perror_msg("warning: failed to kill %d", p->pid);
     178            bb_perror_msg("warning: killing process %u", p->pid);
    185179        }
    186180    }
     
    188182        printf("stopped %s (pid", what);
    189183        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:
    194189    if (ENABLE_FEATURE_CLEAN_UP)
    195         RELEASE_CONFIG_BUFFER(what);
     190        free(what);
    196191    return killed;
    197192}
    198193
    199194#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' },
     195static 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"
    206201#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"
    216213#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
     219enum {
     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
    220235};
    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
     237int start_stop_daemon_main(int argc, char **argv);
    234238int start_stop_daemon_main(int argc, char **argv)
    235239{
    236     unsigned long opt;
    237     char *signame = NULL;
    238     char *startas = NULL;
     240    unsigned opt;
     241    char *signame;
     242    char *startas;
     243    char *chuid;
    239244#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
    240245//  char *retry_arg = NULL;
    241246//  int retries = -1;
     247    char *opt_N;
    242248#endif
    243249#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
    244     bb_applet_long_options = ssd_long_options;
     250    applet_long_options = start_stop_daemon_longopts;
    245251#endif
    246252
    247253    /* 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)
    253260//      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))
    264271        startas = execname;
    265272
    266273//  USE_FEATURE_START_STOP_DAEMON_FANCY(
    267274//      if (retry_arg)
    268 //          retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
     275//          retries = xatoi_u(retry_arg);
    269276//  )
    270277    argc -= optind;
    271278    argv += optind;
    272279
    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) {
    277287        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);
    281289    }
    282290
     
    285293    if (found) {
    286294        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);
    290297    }
    291298    *--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) {
    297324        /* 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
    304342    execv(startas, argv);
    305     bb_perror_msg_and_die ("unable to start %s", startas);
    306 }
     343    bb_perror_msg_and_die("cannot start %s", startas);
     344}
  • branches/2.2.5/mindi-busybox/debianutils/which.c

    r821 r1765  
    44 *
    55 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
     6 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
    67 *
    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.
    89 *
    910 * Based on which from debianutils
    1011 */
    1112
    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"
    1814
    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 
     15int which_main(int argc, char **argv);
    2516int which_main(int argc, char **argv)
    2617{
    27     int status;
    28     size_t i, count;
    29     char *path_list;
     18    int status = EXIT_SUCCESS;
     19    char *p;
    3020
    31     if (argc <= 1 || **(argv + 1) == '-') {
     21    if (argc <= 1 || argv[1][0] == '-') {
    3222        bb_show_usage();
    3323    }
    34     argc--;
    3524
    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*/
    4130
    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;
    6544            }
    6645        }
    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;
    7047    }
    7148
    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);
    10650}
Note: See TracChangeset for help on using the changeset viewer.