diff options
Diffstat (limited to 'debianutils')
| -rw-r--r-- | debianutils/Config.in | 88 | ||||
| -rw-r--r-- | debianutils/Kbuild | 13 | ||||
| -rw-r--r-- | debianutils/mktemp.c | 48 | ||||
| -rw-r--r-- | debianutils/pipe_progress.c | 42 | ||||
| -rw-r--r-- | debianutils/readlink.c | 51 | ||||
| -rw-r--r-- | debianutils/run_parts.c | 191 | ||||
| -rw-r--r-- | debianutils/start_stop_daemon.c | 319 | ||||
| -rw-r--r-- | debianutils/which.c | 43 |
8 files changed, 795 insertions, 0 deletions
diff --git a/debianutils/Config.in b/debianutils/Config.in new file mode 100644 index 000000000..3d85999ff --- /dev/null +++ b/debianutils/Config.in @@ -0,0 +1,88 @@ +# +# For a description of the syntax of this configuration file, +# see scripts/kbuild/config-language.txt. +# + +menu "Debian Utilities" + +config MKTEMP + bool "mktemp" + default n + help + mktemp is used to create unique temporary files + +config PIPE_PROGRESS + bool "pipe_progress" + default n + help + Display a dot to indicate pipe activity. + +config READLINK + bool "readlink" + default n + help + This program reads a symbolic link and returns the name + of the file it points to + +config FEATURE_READLINK_FOLLOW + bool "Enable canonicalization by following all symlinks (-f)" + default n + depends on READLINK + help + Enable the readlink option (-f). + +config RUN_PARTS + bool "run-parts" + default n + help + run-parts is a utility designed to run all the scripts in a directory. + + It is useful to set up a directory like cron.daily, where you need to + execute all the scripts in that directory. + + In this implementation of run-parts some features (such as report mode) + are not implemented. + + Unless you know that run-parts is used in some of your scripts + you can safely say N here. + +config FEATURE_RUN_PARTS_LONG_OPTIONS + bool "Enable long options" + default n + depends on RUN_PARTS && GETOPT_LONG + help + Support long options for the run-parts applet. + +config START_STOP_DAEMON + bool "start-stop-daemon" + default y + help + start-stop-daemon is used to control the creation and + termination of system-level processes, usually the ones + started during the startup of the system. + +config FEATURE_START_STOP_DAEMON_FANCY + bool "Support additional arguments" + default y + depends on START_STOP_DAEMON + help + Support additional arguments. + -o|--oknodo ignored since we exit with 0 anyway + -v|--verbose + +config FEATURE_START_STOP_DAEMON_LONG_OPTIONS + bool "Enable long options" + default n + depends on START_STOP_DAEMON && GETOPT_LONG + help + Support long options for the start-stop-daemon applet. + +config WHICH + bool "which" + default n + help + which is used to find programs in your PATH and + print out their pathnames. + +endmenu + diff --git a/debianutils/Kbuild b/debianutils/Kbuild new file mode 100644 index 000000000..99df6a536 --- /dev/null +++ b/debianutils/Kbuild @@ -0,0 +1,13 @@ +# Makefile for busybox +# +# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org> +# +# Licensed under the GPL v2, see the file LICENSE in this tarball. + +lib-y:= +lib-$(CONFIG_MKTEMP) += mktemp.o +lib-$(CONFIG_PIPE_PROGRESS) += pipe_progress.o +lib-$(CONFIG_READLINK) += readlink.o +lib-$(CONFIG_RUN_PARTS) += run_parts.o +lib-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o +lib-$(CONFIG_WHICH) += which.o diff --git a/debianutils/mktemp.c b/debianutils/mktemp.c new file mode 100644 index 000000000..d47d5a0bf --- /dev/null +++ b/debianutils/mktemp.c @@ -0,0 +1,48 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini mktemp implementation for busybox + * + * + * Copyright (C) 2000 by Daniel Jacobowitz + * Written by Daniel Jacobowitz <dan@debian.org> + * + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. + */ + +#include "busybox.h" +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> + +int mktemp_main(int argc, char **argv) +{ + unsigned long flags = getopt32(argc, argv, "dqt"); + char *chp; + + if (optind + 1 != argc) + bb_show_usage(); + + chp = argv[optind]; + + if (flags & 4) { + char *dir = getenv("TMPDIR"); + if (dir && *dir != '\0') + chp = concat_path_file(dir, chp); + else + chp = concat_path_file("/tmp/", chp); + } + + if (flags & 1) { + if (mkdtemp(chp) == NULL) + return EXIT_FAILURE; + } else { + if (mkstemp(chp) < 0) + return EXIT_FAILURE; + } + + puts(chp); + + return EXIT_SUCCESS; +} diff --git a/debianutils/pipe_progress.c b/debianutils/pipe_progress.c new file mode 100644 index 000000000..75d26e20f --- /dev/null +++ b/debianutils/pipe_progress.c @@ -0,0 +1,42 @@ +/* vi: set sw=4 ts=4: */ +/* + * Monitor a pipe with a simple progress display. + * + * Copyright (C) 2003 by Rob Landley <rob@landley.net>, Joey Hess + * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + */ + +#include "busybox.h" +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <time.h> + +#define PIPE_PROGRESS_SIZE 4096 + +/* Read a block of data from stdin, write it to stdout. + * Activity is indicated by a '.' to stderr + */ +int pipe_progress_main(int argc, char **argv) +{ + RESERVE_CONFIG_BUFFER(buf, PIPE_PROGRESS_SIZE); + time_t t = time(NULL); + size_t len; + + while ((len = fread(buf, 1, PIPE_PROGRESS_SIZE, stdin)) > 0) { + time_t new_time = time(NULL); + if (new_time != t) { + t = new_time; + fputc('.', stderr); + } + fwrite(buf, len, 1, stdout); + } + + fputc('\n', stderr); + + if (ENABLE_FEATURE_CLEAN_UP) + RELEASE_CONFIG_BUFFER(buf); + + return 0; +} diff --git a/debianutils/readlink.c b/debianutils/readlink.c new file mode 100644 index 000000000..400259080 --- /dev/null +++ b/debianutils/readlink.c @@ -0,0 +1,51 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini readlink implementation for busybox + * + * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> + * + * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. + */ + +#include "busybox.h" +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <getopt.h> + +int readlink_main(int argc, char **argv) +{ + char *buf; + char *fname; + + USE_FEATURE_READLINK_FOLLOW( + unsigned opt; + /* We need exactly one non-option argument. */ + opt_complementary = "=1"; + opt = getopt32(argc, argv, "f"); + fname = argv[optind]; + ) + SKIP_FEATURE_READLINK_FOLLOW( + const unsigned opt = 0; + if (argc != 2) bb_show_usage(); + fname = argv[1]; + ) + + /* compat: coreutils readlink reports errors silently via exit code */ + logmode = LOGMODE_NONE; + + if (opt) { + buf = realpath(fname, bb_common_bufsiz1); + } else { + buf = xreadlink(fname); + } + + if (!buf) + return EXIT_FAILURE; + puts(buf); + + if (ENABLE_FEATURE_CLEAN_UP && buf != bb_common_bufsiz1) + free(buf); + + fflush_stdout_and_exit(EXIT_SUCCESS); +} diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c new file mode 100644 index 000000000..2b3b18854 --- /dev/null +++ b/debianutils/run_parts.c @@ -0,0 +1,191 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini run-parts implementation for busybox + * + * + * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> + * + * Based on the Debian run-parts program, version 1.15 + * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>, + * Copyright (C) 1996-1999 Guy Maor <maor@debian.org> + * + * + * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. + */ + +/* This is my first attempt to write a program in C (well, this is my first + * attempt to write a program! :-) . */ + +/* This piece of code is heavily based on the original version of run-parts, + * taken from debian-utils. I've only removed the long options and a the + * report mode. As the original run-parts support only long options, I've + * broken compatibility because the BusyBox policy doesn't allow them. + * The supported options are: + * -t test. Print the name of the files to be executed, without + * execute them. + * -a ARG argument. Pass ARG as an argument the program executed. It can + * be repeated to pass multiple arguments. + * -u MASK umask. Set the umask of the program executed to MASK. */ + +/* TODO + * done - convert calls to error in perror... and remove error() + * done - convert malloc/realloc to their x... counterparts + * done - remove catch_sigchld + * done - use bb's concat_path_file() + * done - declare run_parts_main() as extern and any other function as static? + */ + +#include "busybox.h" +#include <getopt.h> + +static const struct option runparts_long_options[] = { + { "test", 0, NULL, 't' }, + { "umask", 1, NULL, 'u' }, + { "arg", 1, NULL, 'a' }, + { 0, 0, 0, 0 } +}; + +/* valid_name */ +/* True or false? Is this a valid filename (upper/lower alpha, digits, + * underscores, and hyphens only?) + */ +static int valid_name(const struct dirent *d) +{ + const char *c = d->d_name; + + while (*c) { + if (!isalnum(*c) && (*c != '_') && (*c != '-')) { + return 0; + } + ++c; + } + return 1; +} + +/* test mode = 1 is the same as official run_parts + * test_mode = 2 means to fail silently on missing directories + */ +static int run_parts(char **args, const unsigned char test_mode) +{ + struct dirent **namelist = 0; + struct stat st; + char *filename; + char *arg0 = args[0]; + int entries; + int i; + int exitstatus = 0; + +#if __GNUC__ + /* Avoid longjmp clobbering */ + (void) &i; + (void) &exitstatus; +#endif + /* scandir() isn't POSIX, but it makes things easy. */ + entries = scandir(arg0, &namelist, valid_name, alphasort); + + if (entries == -1) { + if (test_mode & 2) { + return 2; + } + bb_perror_msg_and_die("cannot open '%s'", arg0); + } + + for (i = 0; i < entries; i++) { + filename = concat_path_file(arg0, namelist[i]->d_name); + + xstat(filename, &st); + if (S_ISREG(st.st_mode) && !access(filename, X_OK)) { + if (test_mode) { + puts(filename); + } else { + /* exec_errno is common vfork variable */ + volatile int exec_errno = 0; + int result; + int pid; + + if ((pid = vfork()) < 0) { + bb_perror_msg_and_die("failed to fork"); + } else if (!pid) { + args[0] = filename; + execve(filename, args, environ); + exec_errno = errno; + _exit(1); + } + + waitpid(pid, &result, 0); + if (exec_errno) { + errno = exec_errno; + bb_perror_msg("failed to exec %s", filename); + exitstatus = 1; + } + if (WIFEXITED(result) && WEXITSTATUS(result)) { + bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result)); + exitstatus = 1; + } else if (WIFSIGNALED(result)) { + bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result)); + exitstatus = 1; + } + } + } else if (!S_ISDIR(st.st_mode)) { + bb_error_msg("component %s is not an executable plain file", filename); + exitstatus = 1; + } + + free(namelist[i]); + free(filename); + } + free(namelist); + + return exitstatus; +} + + +/* run_parts_main */ +/* Process options */ +int run_parts_main(int argc, char **argv) +{ + char **args = xmalloc(2 * sizeof(char *)); + unsigned char test_mode = 0; + unsigned short argcount = 1; + int opt; + + umask(022); + + while ((opt = getopt_long(argc, argv, "tu:a:", + runparts_long_options, NULL)) > 0) + { + switch (opt) { + /* Enable test mode */ + case 't': + test_mode++; + break; + /* Set the umask of the programs executed */ + case 'u': + /* Check and set the umask of the program executed. As stated in the original + * run-parts, the octal conversion in libc is not foolproof; it will take the + * 8 and 9 digits under some circumstances. We'll just have to live with it. + */ + umask(xstrtoul_range(optarg, 8, 0, 07777)); + break; + /* Pass an argument to the programs */ + case 'a': + /* Add an argument to the commands that we will call. + * Called once for every argument. */ + args = xrealloc(args, (argcount + 2) * (sizeof(char *))); + args[argcount++] = optarg; + break; + default: + bb_show_usage(); + } + } + + /* We require exactly one argument: the directory name */ + if (optind != (argc - 1)) { + bb_show_usage(); + } + + args[0] = argv[optind]; + args[argcount] = 0; + + return run_parts(args, test_mode); +} diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c new file mode 100644 index 000000000..f52352995 --- /dev/null +++ b/debianutils/start_stop_daemon.c @@ -0,0 +1,319 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini start-stop-daemon implementation(s) for busybox + * + * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, + * Adapted for busybox David Kimdon <dwhedon@gordian.com> + * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + */ + +#include "busybox.h" +#include <getopt.h> +#include <sys/resource.h> + +static int signal_nr = 15; +static int user_id = -1; +static int quiet; +static char *userspec; +static char *chuid; +static char *cmdname; +static char *execname; +static char *pidfile; + +struct pid_list { + struct pid_list *next; + pid_t pid; +}; + +static struct pid_list *found = NULL; + +static inline void push(pid_t pid) +{ + struct pid_list *p; + + p = xmalloc(sizeof(*p)); + p->next = found; + p->pid = pid; + found = p; +} + +static int pid_is_exec(pid_t pid, const char *name) +{ + char buf[sizeof("/proc//exe") + sizeof(int)*3]; + char *execbuf; + int equal; + + sprintf(buf, "/proc/%d/exe", pid); + execbuf = xstrdup(name); + readlink(buf, execbuf, strlen(name)+1); + + equal = ! strcmp(execbuf, name); + if (ENABLE_FEATURE_CLEAN_UP) + free(execbuf); + return equal; +} + +static int pid_is_user(int pid, int uid) +{ + struct stat sb; + char buf[sizeof("/proc/") + sizeof(int)*3]; + + sprintf(buf, "/proc/%d", pid); + if (stat(buf, &sb) != 0) + return 0; + return (sb.st_uid == uid); +} + +static int pid_is_cmd(pid_t pid, const char *name) +{ + char buf[sizeof("/proc//stat") + sizeof(int)*3]; + FILE *f; + int c; + + sprintf(buf, "/proc/%d/stat", pid); + f = fopen(buf, "r"); + if (!f) + return 0; + while ((c = getc(f)) != EOF && c != '(') + ; + if (c != '(') { + fclose(f); + return 0; + } + /* this hopefully handles command names containing ')' */ + while ((c = getc(f)) != EOF && c == *name) + name++; + fclose(f); + return (c == ')' && *name == '\0'); +} + + +static void check(int pid) +{ + if (execname && !pid_is_exec(pid, execname)) { + return; + } + if (userspec && !pid_is_user(pid, user_id)) { + return; + } + if (cmdname && !pid_is_cmd(pid, cmdname)) { + return; + } + push(pid); +} + + +static void do_pidfile(void) +{ + FILE *f; + pid_t pid; + + f = fopen(pidfile, "r"); + if (f) { + if (fscanf(f, "%d", &pid) == 1) + check(pid); + fclose(f); + } else if (errno != ENOENT) + bb_perror_msg_and_die("open pidfile %s", pidfile); +} + +static void do_procinit(void) +{ + DIR *procdir; + struct dirent *entry; + int foundany, pid; + + if (pidfile) { + do_pidfile(); + return; + } + + procdir = xopendir("/proc"); + + foundany = 0; + while ((entry = readdir(procdir)) != NULL) { + if (sscanf(entry->d_name, "%d", &pid) != 1) + continue; + foundany++; + check(pid); + } + closedir(procdir); + if (!foundany) + bb_error_msg_and_die ("nothing in /proc - not mounted?"); +} + + +static int do_stop(void) +{ + char *what; + struct pid_list *p; + int killed = 0; + + do_procinit(); + + if (cmdname) + what = xstrdup(cmdname); + else if (execname) + what = xstrdup(execname); + else if (pidfile) + what = xasprintf("process in pidfile '%s'", pidfile); + else if (userspec) + what = xasprintf("process(es) owned by '%s'", userspec); + else + bb_error_msg_and_die("internal error, please report"); + + if (!found) { + if (!quiet) + printf("no %s found; none killed\n", what); + if (ENABLE_FEATURE_CLEAN_UP) + free(what); + return -1; + } + for (p = found; p; p = p->next) { + if (kill(p->pid, signal_nr) == 0) { + p->pid = -p->pid; + killed++; + } else { + bb_perror_msg("warning: failed to kill %d", p->pid); + } + } + if (!quiet && killed) { + printf("stopped %s (pid", what); + for (p = found; p; p = p->next) + if(p->pid < 0) + printf(" %d", -p->pid); + puts(")"); + } + if (ENABLE_FEATURE_CLEAN_UP) + free(what); + return killed; +} + +#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS +static const struct option ssd_long_options[] = { + { "stop", 0, NULL, 'K' }, + { "start", 0, NULL, 'S' }, + { "background", 0, NULL, 'b' }, + { "quiet", 0, NULL, 'q' }, + { "make-pidfile", 0, NULL, 'm' }, +#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY + { "oknodo", 0, NULL, 'o' }, + { "verbose", 0, NULL, 'v' }, + { "nicelevel", 1, NULL, 'N' }, +#endif + { "startas", 1, NULL, 'a' }, + { "name", 1, NULL, 'n' }, + { "signal", 1, NULL, 's' }, + { "user", 1, NULL, 'u' }, + { "chuid", 1, NULL, 'c' }, + { "exec", 1, NULL, 'x' }, + { "pidfile", 1, NULL, 'p' }, +#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY + { "retry", 1, NULL, 'R' }, +#endif + { 0, 0, 0, 0 } +}; +#endif + +#define SSD_CTX_STOP 0x1 +#define SSD_CTX_START 0x2 +#define SSD_OPT_BACKGROUND 0x4 +#define SSD_OPT_QUIET 0x8 +#define SSD_OPT_MAKEPID 0x10 +#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY +#define SSD_OPT_OKNODO 0x20 +#define SSD_OPT_VERBOSE 0x40 +#define SSD_OPT_NICELEVEL 0x80 +#endif + +int start_stop_daemon_main(int argc, char **argv) +{ + unsigned opt; + char *signame = NULL; + char *startas = NULL; +#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY +// char *retry_arg = NULL; +// int retries = -1; + char *opt_N; +#endif +#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS + applet_long_options = ssd_long_options; +#endif + + /* Check required one context option was given */ + opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa"; + opt = getopt32(argc, argv, "KSbqm" +// USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:") + USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:") + "a:n:s:u:c:x:p:" + USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) +// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg) + ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile); + + quiet = (opt & SSD_OPT_QUIET) + USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE)); + + if (signame) { + signal_nr = get_signum(signame); + if (signal_nr < 0) bb_show_usage(); + } + + if (!startas) + startas = execname; + +// USE_FEATURE_START_STOP_DAEMON_FANCY( +// if (retry_arg) +// retries = xatoi_u(retry_arg); +// ) + argc -= optind; + argv += optind; + + if (userspec && sscanf(userspec, "%d", &user_id) != 1) + user_id = bb_xgetpwnam(userspec); + + if (opt & SSD_CTX_STOP) { + int i = do_stop(); + return + USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO) + ? 0 :) !!(i<=0); + } + + do_procinit(); + + if (found) { + if (!quiet) + printf("%s already running\n%d\n", execname, found->pid); + USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);) + SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;) + } + *--argv = startas; + if (opt & SSD_OPT_BACKGROUND) { + xdaemon(0, 0); + setsid(); + } + if (opt & SSD_OPT_MAKEPID) { + /* user wants _us_ to make the pidfile */ + FILE *pidf = xfopen(pidfile, "w"); + + pid_t pidt = getpid(); + fprintf(pidf, "%d\n", pidt); + fclose(pidf); + } + if (chuid) { + if (sscanf(chuid, "%d", &user_id) != 1) + user_id = bb_xgetpwnam(chuid); + xsetuid(user_id); + } +#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY + if (opt & SSD_OPT_NICELEVEL) { + /* Set process priority */ + int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2); + if (setpriority(PRIO_PROCESS, 0, prio) < 0) { + bb_perror_msg_and_die("setpriority(%d)", prio); + } + } +#endif + execv(startas, argv); + bb_perror_msg_and_die("cannot start %s", startas); +} diff --git a/debianutils/which.c b/debianutils/which.c new file mode 100644 index 000000000..a715a2664 --- /dev/null +++ b/debianutils/which.c @@ -0,0 +1,43 @@ +/* vi: set sw=4 ts=4: */ +/* + * Which implementation for busybox + * + * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> + * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu> + * + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. + * + * Based on which from debianutils + */ + +#include "busybox.h" + +int which_main(int argc, char **argv) +{ + int status = EXIT_SUCCESS; + char *p; + + if (argc <= 1 || argv[1][0] == '-') { + bb_show_usage(); + } + + while (--argc > 0) { + argv++; + if (strchr(*argv, '/')) { + if (execable_file(*argv)) { + puts(*argv); + continue; + } + } else { + p = find_execable(*argv); + if (p) { + puts(p); + free(p); + continue; + } + } + status = EXIT_FAILURE; + } + + fflush_stdout_and_exit(status); +} |
