diff options
author | nobody <nobody@localhost> | 2004-10-13 09:42:10 +0000 |
---|---|---|
committer | nobody <nobody@localhost> | 2004-10-13 09:42:10 +0000 |
commit | 8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373 (patch) | |
tree | 1826706cd4fd009fcd14f4f8021005ec8ec0fa59 /busybox/debianutils | |
download | busybox-w32-8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373.tar.gz busybox-w32-8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373.tar.bz2 busybox-w32-8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373.zip |
This commit was manufactured by cvs2svn to create tag 'busybox_1_00'.
Diffstat (limited to 'busybox/debianutils')
-rw-r--r-- | busybox/debianutils/Config.in | 58 | ||||
-rw-r--r-- | busybox/debianutils/Makefile | 32 | ||||
-rw-r--r-- | busybox/debianutils/Makefile.in | 41 | ||||
-rw-r--r-- | busybox/debianutils/mktemp.c | 63 | ||||
-rw-r--r-- | busybox/debianutils/pipe_progress.c | 55 | ||||
-rw-r--r-- | busybox/debianutils/readlink.c | 46 | ||||
-rw-r--r-- | busybox/debianutils/run_parts.c | 114 | ||||
-rw-r--r-- | busybox/debianutils/start_stop_daemon.c | 296 | ||||
-rw-r--r-- | busybox/debianutils/which.c | 96 |
9 files changed, 801 insertions, 0 deletions
diff --git a/busybox/debianutils/Config.in b/busybox/debianutils/Config.in new file mode 100644 index 000000000..7cf7cadb5 --- /dev/null +++ b/busybox/debianutils/Config.in | |||
@@ -0,0 +1,58 @@ | |||
1 | # | ||
2 | # For a description of the syntax of this configuration file, | ||
3 | # see scripts/kbuild/config-language.txt. | ||
4 | # | ||
5 | |||
6 | menu "Debian Utilities" | ||
7 | |||
8 | config CONFIG_MKTEMP | ||
9 | bool "mktemp" | ||
10 | default n | ||
11 | help | ||
12 | mktemp is used to create unique temporary files | ||
13 | |||
14 | config CONFIG_PIPE_PROGRESS | ||
15 | bool "pipe_progress" | ||
16 | default n | ||
17 | help | ||
18 | Display a dot to indicate pipe activity. | ||
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_RUN_PARTS | ||
28 | bool "run-parts" | ||
29 | default n | ||
30 | help | ||
31 | run-parts is a utility designed to run all the scripts in a directory. | ||
32 | |||
33 | It is useful to set up a directory like cron.daily, where you need to | ||
34 | execute all the scripts in that directory. | ||
35 | |||
36 | In this implementation of run-parts some features (such as report mode) | ||
37 | are not implemented. | ||
38 | |||
39 | Unless you know that run-parts is used in some of your scripts | ||
40 | you can safely say N here. | ||
41 | |||
42 | config CONFIG_START_STOP_DAEMON | ||
43 | bool "start-stop-daemon" | ||
44 | default y | ||
45 | help | ||
46 | start-stop-daemon is used to control the creation and | ||
47 | termination of system-level processes, usually the ones | ||
48 | started during the startup of the system. | ||
49 | |||
50 | config CONFIG_WHICH | ||
51 | bool "which" | ||
52 | default n | ||
53 | help | ||
54 | which is used to find programs in your PATH and | ||
55 | print out their pathnames. | ||
56 | |||
57 | endmenu | ||
58 | |||
diff --git a/busybox/debianutils/Makefile b/busybox/debianutils/Makefile new file mode 100644 index 000000000..10ec1cc58 --- /dev/null +++ b/busybox/debianutils/Makefile | |||
@@ -0,0 +1,32 @@ | |||
1 | # Makefile for busybox | ||
2 | # | ||
3 | # Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
4 | # | ||
5 | # This program is free software; you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License as published by | ||
7 | # the Free Software Foundation; either version 2 of the License, or | ||
8 | # (at your option) any later version. | ||
9 | # | ||
10 | # This program is distributed in the hope that it will be useful, | ||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | # General Public License for more details. | ||
14 | # | ||
15 | # You should have received a copy of the GNU General Public License | ||
16 | # along with this program; if not, write to the Free Software | ||
17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | # | ||
19 | |||
20 | top_srcdir=.. | ||
21 | top_builddir=.. | ||
22 | srcdir=$(top_srcdir)/debianutils | ||
23 | DEBIANUTILS_DIR:=./ | ||
24 | include $(top_builddir)/Rules.mak | ||
25 | include $(top_builddir)/.config | ||
26 | include $(srcdir)/Makefile.in | ||
27 | all: $(libraries-y) | ||
28 | -include $(top_builddir)/.depend | ||
29 | |||
30 | clean: | ||
31 | rm -f *.o *.a $(AR_TARGET) | ||
32 | |||
diff --git a/busybox/debianutils/Makefile.in b/busybox/debianutils/Makefile.in new file mode 100644 index 000000000..3a204033e --- /dev/null +++ b/busybox/debianutils/Makefile.in | |||
@@ -0,0 +1,41 @@ | |||
1 | # Makefile for busybox | ||
2 | # | ||
3 | # Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
4 | # | ||
5 | # This program is free software; you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License as published by | ||
7 | # the Free Software Foundation; either version 2 of the License, or | ||
8 | # (at your option) any later version. | ||
9 | # | ||
10 | # This program is distributed in the hope that it will be useful, | ||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | # General Public License for more details. | ||
14 | # | ||
15 | # You should have received a copy of the GNU General Public License | ||
16 | # along with this program; if not, write to the Free Software | ||
17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | # | ||
19 | |||
20 | DEBIANUTILS_AR:=debianutils.a | ||
21 | ifndef $(DEBIANUTILS_DIR) | ||
22 | DEBIANUTILS_DIR:=$(top_builddir)/debianutils/ | ||
23 | endif | ||
24 | srcdir=$(top_srcdir)/debianutils | ||
25 | |||
26 | DEBIANUTILS-y:= | ||
27 | DEBIANUTILS-$(CONFIG_MKTEMP) += mktemp.o | ||
28 | DEBIANUTILS-$(CONFIG_PIPE_PROGRESS) += pipe_progress.o | ||
29 | DEBIANUTILS-$(CONFIG_READLINK) += readlink.o | ||
30 | DEBIANUTILS-$(CONFIG_RUN_PARTS) += run_parts.o | ||
31 | DEBIANUTILS-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o | ||
32 | DEBIANUTILS-$(CONFIG_WHICH) += which.o | ||
33 | |||
34 | libraries-y+=$(DEBIANUTILS_DIR)$(DEBIANUTILS_AR) | ||
35 | |||
36 | $(DEBIANUTILS_DIR)$(DEBIANUTILS_AR): $(patsubst %,$(DEBIANUTILS_DIR)%, $(DEBIANUTILS-y)) | ||
37 | $(AR) -ro $@ $(patsubst %,$(DEBIANUTILS_DIR)%, $(DEBIANUTILS-y)) | ||
38 | |||
39 | $(DEBIANUTILS_DIR)%.o: $(srcdir)/%.c | ||
40 | $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $< | ||
41 | |||
diff --git a/busybox/debianutils/mktemp.c b/busybox/debianutils/mktemp.c new file mode 100644 index 000000000..9fdf79bfa --- /dev/null +++ b/busybox/debianutils/mktemp.c | |||
@@ -0,0 +1,63 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini mktemp implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2000 by Daniel Jacobowitz | ||
7 | * Written by Daniel Jacobowitz <dan@debian.org> | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | */ | ||
24 | |||
25 | #include <stdio.h> | ||
26 | #include <errno.h> | ||
27 | #include <string.h> | ||
28 | #include <unistd.h> | ||
29 | #include <stdlib.h> | ||
30 | #include "busybox.h" | ||
31 | |||
32 | extern int mktemp_main(int argc, char **argv) | ||
33 | { | ||
34 | unsigned char dir_flag = 0; | ||
35 | int opt; | ||
36 | |||
37 | while ((opt = getopt(argc, argv, "qd")) != -1) { | ||
38 | if (opt == 'd') { | ||
39 | dir_flag = 1; | ||
40 | } | ||
41 | else if (opt != 'q') { | ||
42 | bb_show_usage(); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | if (optind + 1 != argc) { | ||
47 | bb_show_usage(); | ||
48 | } | ||
49 | |||
50 | if (dir_flag) { | ||
51 | if (mkdtemp(argv[argc-1]) == NULL) { | ||
52 | return EXIT_FAILURE; | ||
53 | } | ||
54 | } else { | ||
55 | if (mkstemp(argv[argc-1]) < 0) { | ||
56 | return EXIT_FAILURE; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | (void) puts(argv[argc-1]); | ||
61 | |||
62 | return EXIT_SUCCESS; | ||
63 | } | ||
diff --git a/busybox/debianutils/pipe_progress.c b/busybox/debianutils/pipe_progress.c new file mode 100644 index 000000000..ab05202eb --- /dev/null +++ b/busybox/debianutils/pipe_progress.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | * Monitor a pipe with a simple progress display. | ||
3 | * | ||
4 | * Copyright (C) 2003 by Rob Landley <rob@landley.net>, Joey Hess | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | #include <stdio.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <unistd.h> | ||
25 | #include <time.h> | ||
26 | |||
27 | #include "busybox.h" | ||
28 | |||
29 | #define PIPE_PROGRESS_SIZE 4096 | ||
30 | |||
31 | /* Read a block of data from stdin, write it to stdout. | ||
32 | * Activity is indicated by a '.' to stderr | ||
33 | */ | ||
34 | extern int pipe_progress_main(int argc, char **argv) | ||
35 | { | ||
36 | RESERVE_CONFIG_BUFFER(buf, PIPE_PROGRESS_SIZE); | ||
37 | time_t t = time(NULL); | ||
38 | size_t len; | ||
39 | |||
40 | while ((len = fread(buf, 1, PIPE_PROGRESS_SIZE, stdin)) > 0) { | ||
41 | time_t new_time = time(NULL); | ||
42 | if (new_time != t) { | ||
43 | t = new_time; | ||
44 | fputc('.', stderr); | ||
45 | } | ||
46 | fwrite(buf, len, 1, stdout); | ||
47 | } | ||
48 | |||
49 | fputc('\n', stderr); | ||
50 | |||
51 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
52 | RELEASE_CONFIG_BUFFER(buf); | ||
53 | #endif | ||
54 | return 0; | ||
55 | } | ||
diff --git a/busybox/debianutils/readlink.c b/busybox/debianutils/readlink.c new file mode 100644 index 000000000..d8d7e8c2d --- /dev/null +++ b/busybox/debianutils/readlink.c | |||
@@ -0,0 +1,46 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini readlink implementation for busybox | ||
4 | * | ||
5 | * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | #include <errno.h> | ||
24 | #include <unistd.h> | ||
25 | #include <stdlib.h> | ||
26 | #include "busybox.h" | ||
27 | |||
28 | int readlink_main(int argc, char **argv) | ||
29 | { | ||
30 | char *buf = NULL; | ||
31 | |||
32 | /* no options, no getopt */ | ||
33 | |||
34 | if (argc != 2) | ||
35 | bb_show_usage(); | ||
36 | |||
37 | buf = xreadlink(argv[1]); | ||
38 | if (!buf) | ||
39 | return EXIT_FAILURE; | ||
40 | puts(buf); | ||
41 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
42 | free(buf); | ||
43 | #endif | ||
44 | |||
45 | return EXIT_SUCCESS; | ||
46 | } | ||
diff --git a/busybox/debianutils/run_parts.c b/busybox/debianutils/run_parts.c new file mode 100644 index 000000000..6205595bf --- /dev/null +++ b/busybox/debianutils/run_parts.c | |||
@@ -0,0 +1,114 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini run-parts implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> | ||
7 | * | ||
8 | * Based on the Debian run-parts program, version 1.15 | ||
9 | * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>, | ||
10 | * Copyright (C) 1996-1999 Guy Maor <maor@debian.org> | ||
11 | * | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or modify | ||
14 | * it under the terms of the GNU General Public License as published by | ||
15 | * the Free Software Foundation; either version 2 of the License, or | ||
16 | * (at your option) any later version. | ||
17 | * | ||
18 | * This program is distributed in the hope that it will be useful, | ||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
21 | * General Public License for more details. | ||
22 | * | ||
23 | * You should have received a copy of the GNU General Public License | ||
24 | * along with this program; if not, write to the Free Software | ||
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | ||
26 | * 02111-1307 USA | ||
27 | * | ||
28 | */ | ||
29 | |||
30 | /* This is my first attempt to write a program in C (well, this is my first | ||
31 | * attempt to write a program! :-) . */ | ||
32 | |||
33 | /* This piece of code is heavily based on the original version of run-parts, | ||
34 | * taken from debian-utils. I've only removed the long options and a the | ||
35 | * report mode. As the original run-parts support only long options, I've | ||
36 | * broken compatibility because the BusyBox policy doesn't allow them. | ||
37 | * The supported options are: | ||
38 | * -t test. Print the name of the files to be executed, without | ||
39 | * execute them. | ||
40 | * -a ARG argument. Pass ARG as an argument the program executed. It can | ||
41 | * be repeated to pass multiple arguments. | ||
42 | * -u MASK umask. Set the umask of the program executed to MASK. */ | ||
43 | |||
44 | /* TODO | ||
45 | * done - convert calls to error in perror... and remove error() | ||
46 | * done - convert malloc/realloc to their x... counterparts | ||
47 | * done - remove catch_sigchld | ||
48 | * done - use bb's concat_path_file() | ||
49 | * done - declare run_parts_main() as extern and any other function as static? | ||
50 | */ | ||
51 | |||
52 | #include <getopt.h> | ||
53 | #include <stdlib.h> | ||
54 | |||
55 | #include "libbb.h" | ||
56 | |||
57 | static const struct option runparts_long_options[] = { | ||
58 | { "test", 0, NULL, 't' }, | ||
59 | { "umask", 1, NULL, 'u' }, | ||
60 | { "arg", 1, NULL, 'a' }, | ||
61 | { 0, 0, 0, 0 } | ||
62 | }; | ||
63 | |||
64 | extern char **environ; | ||
65 | |||
66 | /* run_parts_main */ | ||
67 | /* Process options */ | ||
68 | int run_parts_main(int argc, char **argv) | ||
69 | { | ||
70 | char **args = xmalloc(2 * sizeof(char *)); | ||
71 | unsigned char test_mode = 0; | ||
72 | unsigned short argcount = 1; | ||
73 | int opt; | ||
74 | |||
75 | umask(022); | ||
76 | |||
77 | while ((opt = getopt_long (argc, argv, "tu:a:", | ||
78 | runparts_long_options, NULL)) > 0) | ||
79 | { | ||
80 | switch (opt) { | ||
81 | /* Enable test mode */ | ||
82 | case 't': | ||
83 | test_mode++; | ||
84 | break; | ||
85 | /* Set the umask of the programs executed */ | ||
86 | case 'u': | ||
87 | /* Check and set the umask of the program executed. As stated in the original | ||
88 | * run-parts, the octal conversion in libc is not foolproof; it will take the | ||
89 | * 8 and 9 digits under some circumstances. We'll just have to live with it. | ||
90 | */ | ||
91 | umask(bb_xgetlarg(optarg, 8, 0, 07777)); | ||
92 | break; | ||
93 | /* Pass an argument to the programs */ | ||
94 | case 'a': | ||
95 | /* Add an argument to the commands that we will call. | ||
96 | * Called once for every argument. */ | ||
97 | args = xrealloc(args, (argcount + 2) * (sizeof(char *))); | ||
98 | args[argcount++] = optarg; | ||
99 | break; | ||
100 | default: | ||
101 | bb_show_usage(); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | /* We require exactly one argument: the directory name */ | ||
106 | if (optind != (argc - 1)) { | ||
107 | bb_show_usage(); | ||
108 | } | ||
109 | |||
110 | args[0] = argv[optind]; | ||
111 | args[argcount] = 0; | ||
112 | |||
113 | return(run_parts(args, test_mode, environ)); | ||
114 | } | ||
diff --git a/busybox/debianutils/start_stop_daemon.c b/busybox/debianutils/start_stop_daemon.c new file mode 100644 index 000000000..e15944c59 --- /dev/null +++ b/busybox/debianutils/start_stop_daemon.c | |||
@@ -0,0 +1,296 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini start-stop-daemon implementation(s) for busybox | ||
4 | * | ||
5 | * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, | ||
6 | * public domain. | ||
7 | * Adapted for busybox David Kimdon <dwhedon@gordian.com> | ||
8 | */ | ||
9 | |||
10 | #include <stdio.h> | ||
11 | #include <stdlib.h> | ||
12 | #include <string.h> | ||
13 | #include <stdarg.h> | ||
14 | #include <signal.h> | ||
15 | #include <errno.h> | ||
16 | #include <sys/stat.h> | ||
17 | #include <dirent.h> | ||
18 | #include <unistd.h> | ||
19 | #include <getopt.h> | ||
20 | |||
21 | #include "busybox.h" | ||
22 | #include "pwd_.h" | ||
23 | |||
24 | static int signal_nr = 15; | ||
25 | static int user_id = -1; | ||
26 | static int quiet = 0; | ||
27 | static char *userspec = NULL; | ||
28 | static char *cmdname = NULL; | ||
29 | static char *execname = NULL; | ||
30 | static char *pidfile = NULL; | ||
31 | |||
32 | struct pid_list { | ||
33 | struct pid_list *next; | ||
34 | pid_t pid; | ||
35 | }; | ||
36 | |||
37 | static struct pid_list *found = NULL; | ||
38 | |||
39 | static inline void | ||
40 | push(pid_t pid) | ||
41 | { | ||
42 | struct pid_list *p; | ||
43 | |||
44 | p = xmalloc(sizeof(*p)); | ||
45 | p->next = found; | ||
46 | p->pid = pid; | ||
47 | found = p; | ||
48 | } | ||
49 | |||
50 | static int | ||
51 | pid_is_exec(pid_t pid, const char *name) | ||
52 | { | ||
53 | char buf[32]; | ||
54 | struct stat sb, exec_stat; | ||
55 | |||
56 | if (name && stat(name, &exec_stat)) | ||
57 | bb_perror_msg_and_die("stat %s", name); | ||
58 | |||
59 | sprintf(buf, "/proc/%d/exe", pid); | ||
60 | if (stat(buf, &sb) != 0) | ||
61 | return 0; | ||
62 | return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino); | ||
63 | } | ||
64 | |||
65 | static int | ||
66 | pid_is_user(int pid, int uid) | ||
67 | { | ||
68 | struct stat sb; | ||
69 | char buf[32]; | ||
70 | |||
71 | sprintf(buf, "/proc/%d", pid); | ||
72 | if (stat(buf, &sb) != 0) | ||
73 | return 0; | ||
74 | return (sb.st_uid == uid); | ||
75 | } | ||
76 | |||
77 | static int | ||
78 | pid_is_cmd(pid_t pid, const char *name) | ||
79 | { | ||
80 | char buf[32]; | ||
81 | FILE *f; | ||
82 | int c; | ||
83 | |||
84 | sprintf(buf, "/proc/%d/stat", pid); | ||
85 | f = fopen(buf, "r"); | ||
86 | if (!f) | ||
87 | return 0; | ||
88 | while ((c = getc(f)) != EOF && c != '(') | ||
89 | ; | ||
90 | if (c != '(') { | ||
91 | fclose(f); | ||
92 | return 0; | ||
93 | } | ||
94 | /* this hopefully handles command names containing ')' */ | ||
95 | while ((c = getc(f)) != EOF && c == *name) | ||
96 | name++; | ||
97 | fclose(f); | ||
98 | return (c == ')' && *name == '\0'); | ||
99 | } | ||
100 | |||
101 | |||
102 | static void | ||
103 | check(int pid) | ||
104 | { | ||
105 | if (execname && !pid_is_exec(pid, execname)) { | ||
106 | return; | ||
107 | } | ||
108 | if (userspec && !pid_is_user(pid, user_id)) { | ||
109 | return; | ||
110 | } | ||
111 | if (cmdname && !pid_is_cmd(pid, cmdname)) { | ||
112 | return; | ||
113 | } | ||
114 | push(pid); | ||
115 | } | ||
116 | |||
117 | |||
118 | static void | ||
119 | do_pidfile(void) | ||
120 | { | ||
121 | FILE *f; | ||
122 | pid_t pid; | ||
123 | |||
124 | f = fopen(pidfile, "r"); | ||
125 | if (f) { | ||
126 | if (fscanf(f, "%d", &pid) == 1) | ||
127 | check(pid); | ||
128 | fclose(f); | ||
129 | } else if (errno != ENOENT) | ||
130 | bb_perror_msg_and_die("open pidfile %s", pidfile); | ||
131 | |||
132 | } | ||
133 | |||
134 | static void | ||
135 | do_procinit(void) | ||
136 | { | ||
137 | DIR *procdir; | ||
138 | struct dirent *entry; | ||
139 | int foundany, pid; | ||
140 | |||
141 | if (pidfile) { | ||
142 | do_pidfile(); | ||
143 | return; | ||
144 | } | ||
145 | |||
146 | procdir = opendir("/proc"); | ||
147 | if (!procdir) | ||
148 | bb_perror_msg_and_die ("opendir /proc"); | ||
149 | |||
150 | foundany = 0; | ||
151 | while ((entry = readdir(procdir)) != NULL) { | ||
152 | if (sscanf(entry->d_name, "%d", &pid) != 1) | ||
153 | continue; | ||
154 | foundany++; | ||
155 | check(pid); | ||
156 | } | ||
157 | closedir(procdir); | ||
158 | if (!foundany) | ||
159 | bb_error_msg_and_die ("nothing in /proc - not mounted?"); | ||
160 | } | ||
161 | |||
162 | |||
163 | static void | ||
164 | do_stop(void) | ||
165 | { | ||
166 | char what[1024]; | ||
167 | struct pid_list *p; | ||
168 | int killed = 0; | ||
169 | |||
170 | do_procinit(); | ||
171 | |||
172 | if (cmdname) | ||
173 | strcpy(what, cmdname); | ||
174 | else if (execname) | ||
175 | strcpy(what, execname); | ||
176 | else if (pidfile) | ||
177 | sprintf(what, "process in pidfile `%.200s'", pidfile); | ||
178 | else if (userspec) | ||
179 | sprintf(what, "process(es) owned by `%s'", userspec); | ||
180 | else | ||
181 | bb_error_msg_and_die ("internal error, please report"); | ||
182 | |||
183 | if (!found) { | ||
184 | if (!quiet) | ||
185 | printf("no %s found; none killed.\n", what); | ||
186 | return; | ||
187 | } | ||
188 | for (p = found; p; p = p->next) { | ||
189 | if (kill(p->pid, signal_nr) == 0) { | ||
190 | p->pid = -p->pid; | ||
191 | killed++; | ||
192 | } else { | ||
193 | bb_perror_msg("warning: failed to kill %d", p->pid); | ||
194 | } | ||
195 | } | ||
196 | if (!quiet && killed) { | ||
197 | printf("stopped %s (pid", what); | ||
198 | for (p = found; p; p = p->next) | ||
199 | if(p->pid < 0) | ||
200 | printf(" %d", -p->pid); | ||
201 | printf(").\n"); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | |||
206 | static const struct option ssd_long_options[] = { | ||
207 | { "stop", 0, NULL, 'K' }, | ||
208 | { "start", 0, NULL, 'S' }, | ||
209 | { "background", 0, NULL, 'b' }, | ||
210 | { "quiet", 0, NULL, 'q' }, | ||
211 | { "make-pidfile", 0, NULL, 'm' }, | ||
212 | { "startas", 1, NULL, 'a' }, | ||
213 | { "name", 1, NULL, 'n' }, | ||
214 | { "signal", 1, NULL, 's' }, | ||
215 | { "user", 1, NULL, 'u' }, | ||
216 | { "exec", 1, NULL, 'x' }, | ||
217 | { "pidfile", 1, NULL, 'p' }, | ||
218 | { 0, 0, 0, 0 } | ||
219 | }; | ||
220 | |||
221 | #define SSD_CTX_STOP 1 | ||
222 | #define SSD_CTX_START 2 | ||
223 | #define SSD_OPT_BACKGROUND 4 | ||
224 | #define SSD_OPT_QUIET 8 | ||
225 | #define SSD_OPT_MAKEPID 16 | ||
226 | |||
227 | int | ||
228 | start_stop_daemon_main(int argc, char **argv) | ||
229 | { | ||
230 | unsigned long opt; | ||
231 | char *signame = NULL; | ||
232 | char *startas = NULL; | ||
233 | |||
234 | bb_applet_long_options = ssd_long_options; | ||
235 | |||
236 | bb_opt_complementaly = "K~S:S~K"; | ||
237 | opt = bb_getopt_ulflags(argc, argv, "KSbqma:n:s:u:x:p:", | ||
238 | &startas, &cmdname, &signame, &userspec, &execname, &pidfile); | ||
239 | |||
240 | /* Check one and only one context option was given */ | ||
241 | if ((opt & 0x80000000UL) || (opt & (SSD_CTX_STOP | SSD_CTX_START)) == 0) { | ||
242 | bb_show_usage(); | ||
243 | } | ||
244 | |||
245 | if (signame) { | ||
246 | signal_nr = bb_xgetlarg(signame, 10, 0, NSIG); | ||
247 | } | ||
248 | |||
249 | if (!execname && !pidfile && !userspec && !cmdname) | ||
250 | bb_error_msg_and_die ("need at least one of -x, -p, -u, or -n"); | ||
251 | |||
252 | if (!startas) | ||
253 | startas = execname; | ||
254 | |||
255 | if ((opt & SSD_CTX_START) && !startas) | ||
256 | bb_error_msg_and_die ("-S needs -x or -a"); | ||
257 | |||
258 | if ((opt & SSD_OPT_MAKEPID) && pidfile == NULL) | ||
259 | bb_error_msg_and_die ("-m needs -p"); | ||
260 | |||
261 | argc -= optind; | ||
262 | argv += optind; | ||
263 | |||
264 | if (userspec && sscanf(userspec, "%d", &user_id) != 1) | ||
265 | user_id = my_getpwnam(userspec); | ||
266 | |||
267 | if (opt & SSD_CTX_STOP) { | ||
268 | do_stop(); | ||
269 | return EXIT_SUCCESS; | ||
270 | } | ||
271 | |||
272 | do_procinit(); | ||
273 | |||
274 | if (found) { | ||
275 | if (!quiet) | ||
276 | printf("%s already running.\n%d\n", execname ,found->pid); | ||
277 | return EXIT_SUCCESS; | ||
278 | } | ||
279 | *--argv = startas; | ||
280 | if (opt & SSD_OPT_BACKGROUND) { | ||
281 | if (daemon(0, 0) == -1) | ||
282 | bb_perror_msg_and_die ("unable to fork"); | ||
283 | setsid(); | ||
284 | } | ||
285 | if (opt & SSD_OPT_MAKEPID) { | ||
286 | /* user wants _us_ to make the pidfile */ | ||
287 | FILE *pidf = fopen(pidfile, "w"); | ||
288 | pid_t pidt = getpid(); | ||
289 | if (pidf == NULL) | ||
290 | bb_perror_msg_and_die("Unable to write pidfile '%s'", pidfile); | ||
291 | fprintf(pidf, "%d\n", pidt); | ||
292 | fclose(pidf); | ||
293 | } | ||
294 | execv(startas, argv); | ||
295 | bb_perror_msg_and_die ("unable to start %s", startas); | ||
296 | } | ||
diff --git a/busybox/debianutils/which.c b/busybox/debianutils/which.c new file mode 100644 index 000000000..999dded36 --- /dev/null +++ b/busybox/debianutils/which.c | |||
@@ -0,0 +1,96 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Which implementation for busybox | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | * | ||
21 | * Based on which from debianutils | ||
22 | */ | ||
23 | |||
24 | |||
25 | #include <string.h> | ||
26 | #include <stdio.h> | ||
27 | #include <stdlib.h> | ||
28 | #include <unistd.h> | ||
29 | |||
30 | #include "busybox.h" | ||
31 | |||
32 | |||
33 | extern int which_main(int argc, char **argv) | ||
34 | { | ||
35 | char *path_list; | ||
36 | int i, count=1, status = EXIT_SUCCESS; | ||
37 | |||
38 | if (argc <= 1 || **(argv + 1) == '-') { | ||
39 | bb_show_usage(); | ||
40 | } | ||
41 | argc--; | ||
42 | |||
43 | path_list = getenv("PATH"); | ||
44 | if (path_list != NULL) { | ||
45 | for (i=strlen(path_list); i > 0; i--) { | ||
46 | if (path_list[i]==':') { | ||
47 | path_list[i]=0; | ||
48 | count++; | ||
49 | } | ||
50 | } | ||
51 | } else { | ||
52 | path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; | ||
53 | count = 5; | ||
54 | } | ||
55 | |||
56 | while (argc-- > 0) { | ||
57 | char *buf; | ||
58 | char *path_n; | ||
59 | char found = 0; | ||
60 | argv++; | ||
61 | |||
62 | /* | ||
63 | * Check if we were given the full path, first. | ||
64 | * Otherwise see if the file exists in our $PATH. | ||
65 | */ | ||
66 | path_n = path_list; | ||
67 | buf = *argv; | ||
68 | if (access(buf, X_OK) == 0) { | ||
69 | found = 1; | ||
70 | } else { | ||
71 | for (i = 0; i < count; i++) { | ||
72 | buf = concat_path_file(path_n, *argv); | ||
73 | if (access(buf, X_OK) == 0) { | ||
74 | found = 1; | ||
75 | break; | ||
76 | } | ||
77 | free(buf); | ||
78 | path_n += (strlen(path_n) + 1); | ||
79 | } | ||
80 | } | ||
81 | if (found) { | ||
82 | puts(buf); | ||
83 | } else { | ||
84 | status = EXIT_FAILURE; | ||
85 | } | ||
86 | } | ||
87 | return status; | ||
88 | } | ||
89 | |||
90 | /* | ||
91 | Local Variables: | ||
92 | c-file-style: "linux" | ||
93 | c-basic-offset: 4 | ||
94 | tab-width: 4 | ||
95 | End: | ||
96 | */ | ||