aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2010-09-14 13:08:20 +1000
committerNguyễn Thái Ngọc Duy <pclouds@gmail.com>2010-09-14 13:08:20 +1000
commit6a6efd31038d7afe977e3059508ae863e65cbdf5 (patch)
tree5cd69a751e893b83176751c80fcea7a7afeed1ae /libbb
parenta6a2325ecf402054132daae169f71edb0fb849e3 (diff)
parent29082231d0cb1a5b327de5d515b16f332d4dbdaf (diff)
downloadbusybox-w32-6a6efd31038d7afe977e3059508ae863e65cbdf5.tar.gz
busybox-w32-6a6efd31038d7afe977e3059508ae863e65cbdf5.tar.bz2
busybox-w32-6a6efd31038d7afe977e3059508ae863e65cbdf5.zip
Merge branch 'origin/master' (early part)
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Kbuild.src6
-rw-r--r--libbb/appletlib.c10
-rw-r--r--libbb/copyfd.c23
-rw-r--r--libbb/error_msg.c19
-rw-r--r--libbb/error_msg_and_die.c20
-rw-r--r--libbb/execable.c8
-rw-r--r--libbb/herror_msg.c11
-rw-r--r--libbb/herror_msg_and_die.c20
-rw-r--r--libbb/parse_config.c20
-rw-r--r--libbb/perror_msg.c17
-rw-r--r--libbb/perror_msg_and_die.c26
-rw-r--r--libbb/platform.c27
-rw-r--r--libbb/read_printf.c10
-rw-r--r--libbb/run_shell.c20
-rw-r--r--libbb/setup_environment.c4
-rw-r--r--libbb/verror_msg.c23
-rw-r--r--libbb/vfork_daemon_rexec.c52
-rw-r--r--libbb/xconnect.c1
-rw-r--r--libbb/xfuncs.c34
-rw-r--r--libbb/xfuncs_printf.c16
20 files changed, 190 insertions, 177 deletions
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index 2a50a1ae3..a4f77414b 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -4,6 +4,8 @@
4# 4#
5# Licensed under the GPL v2, see the file LICENSE in this tarball. 5# Licensed under the GPL v2, see the file LICENSE in this tarball.
6 6
7libbb/appletlib.o: include/usage_compressed.h
8
7lib-y:= 9lib-y:=
8 10
9INSERT 11INSERT
@@ -30,8 +32,6 @@ lib-y += create_icmp_socket.o
30lib-y += default_error_retval.o 32lib-y += default_error_retval.o
31lib-y += device_open.o 33lib-y += device_open.o
32lib-y += dump.o 34lib-y += dump.o
33lib-y += error_msg.o
34lib-y += error_msg_and_die.o
35lib-y += execable.o 35lib-y += execable.o
36lib-y += fclose_nonstdin.o 36lib-y += fclose_nonstdin.o
37lib-y += fflush_stdout_and_exit.o 37lib-y += fflush_stdout_and_exit.o
@@ -44,7 +44,6 @@ lib-y += get_line_from_file.o
44lib-y += getopt32.o 44lib-y += getopt32.o
45lib-y += get_volsize.o 45lib-y += get_volsize.o
46lib-y += herror_msg.o 46lib-y += herror_msg.o
47lib-y += herror_msg_and_die.o
48lib-y += human_readable.o 47lib-y += human_readable.o
49lib-y += info_msg.o 48lib-y += info_msg.o
50lib-y += inode_hash.o 49lib-y += inode_hash.o
@@ -63,7 +62,6 @@ lib-y += obscure.o
63lib-y += parse_mode.o 62lib-y += parse_mode.o
64lib-y += parse_config.o 63lib-y += parse_config.o
65lib-y += perror_msg.o 64lib-y += perror_msg.o
66lib-y += perror_msg_and_die.o
67lib-y += perror_nomsg.o 65lib-y += perror_nomsg.o
68lib-y += perror_nomsg_and_die.o 66lib-y += perror_nomsg_and_die.o
69lib-y += pidfile.o 67lib-y += pidfile.o
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 922f7d26b..cae7da48a 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -61,11 +61,11 @@
61static const char usage_messages[] ALIGN1 = UNPACKED_USAGE; 61static const char usage_messages[] ALIGN1 = UNPACKED_USAGE;
62#else 62#else
63# define usage_messages 0 63# define usage_messages 0
64#endif /* SHOW_USAGE */ 64#endif
65 65
66#if ENABLE_FEATURE_COMPRESS_USAGE 66#if ENABLE_FEATURE_COMPRESS_USAGE
67 67
68static const char packed_usage[] = { PACKED_USAGE }; 68static const char packed_usage[] ALIGN1 = { PACKED_USAGE };
69# include "unarchive.h" 69# include "unarchive.h"
70static const char *unpack_usage_messages(void) 70static const char *unpack_usage_messages(void)
71{ 71{
@@ -195,7 +195,11 @@ void lbb_prepare(const char *applet
195#if ENABLE_FEATURE_INDIVIDUAL 195#if ENABLE_FEATURE_INDIVIDUAL
196 /* Redundant for busybox (run_applet_and_exit covers that case) 196 /* Redundant for busybox (run_applet_and_exit covers that case)
197 * but needed for "individual applet" mode */ 197 * but needed for "individual applet" mode */
198 if (argv[1] && !argv[2] && strcmp(argv[1], "--help") == 0) { 198 if (argv[1]
199 && !argv[2]
200 && strcmp(argv[1], "--help") == 0
201 && strncmp(applet, "busybox", 7) != 0
202 ) {
199 /* Special case. POSIX says "test --help" 203 /* Special case. POSIX says "test --help"
200 * should be no different from e.g. "test --foo". */ 204 * should be no different from e.g. "test --foo". */
201 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0) 205 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index f42eb7623..82622c06f 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -9,19 +9,29 @@
9 9
10#include "libbb.h" 10#include "libbb.h"
11 11
12/* Used by NOFORK applets (e.g. cat) - must not use xmalloc */ 12/* Used by NOFORK applets (e.g. cat) - must not use xmalloc.
13 13 * size < 0 means "ignore write errors", used by tar --to-command
14 * size = 0 means "copy till EOF"
15 */
14static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) 16static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
15{ 17{
16 int status = -1; 18 int status = -1;
17 off_t total = 0; 19 off_t total = 0;
20 bool continue_on_write_error = 0;
18#if CONFIG_FEATURE_COPYBUF_KB <= 4 21#if CONFIG_FEATURE_COPYBUF_KB <= 4
19 char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024]; 22 char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
20 enum { buffer_size = sizeof(buffer) }; 23 enum { buffer_size = sizeof(buffer) };
21#else 24#else
22 char *buffer; 25 char *buffer;
23 int buffer_size; 26 int buffer_size;
27#endif
24 28
29 if (size < 0) {
30 size = -size;
31 continue_on_write_error = 1;
32 }
33
34#if CONFIG_FEATURE_COPYBUF_KB > 4
25 if (size > 0 && size <= 4 * 1024) 35 if (size > 0 && size <= 4 * 1024)
26 goto use_small_buf; 36 goto use_small_buf;
27 /* We want page-aligned buffer, just in case kernel is clever 37 /* We want page-aligned buffer, just in case kernel is clever
@@ -63,8 +73,11 @@ static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
63 if (dst_fd >= 0) { 73 if (dst_fd >= 0) {
64 ssize_t wr = full_write(dst_fd, buffer, rd); 74 ssize_t wr = full_write(dst_fd, buffer, rd);
65 if (wr < rd) { 75 if (wr < rd) {
66 bb_perror_msg(bb_msg_write_error); 76 if (!continue_on_write_error) {
67 break; 77 bb_perror_msg(bb_msg_write_error);
78 break;
79 }
80 dst_fd = -1;
68 } 81 }
69 } 82 }
70 total += rd; 83 total += rd;
@@ -108,7 +121,7 @@ off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
108void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size) 121void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
109{ 122{
110 off_t sz = bb_copyfd_size(fd1, fd2, size); 123 off_t sz = bb_copyfd_size(fd1, fd2, size);
111 if (sz == size) 124 if (sz == (size >= 0 ? size : -size))
112 return; 125 return;
113 if (sz != -1) 126 if (sz != -1)
114 bb_error_msg_and_die("short read"); 127 bb_error_msg_and_die("short read");
diff --git a/libbb/error_msg.c b/libbb/error_msg.c
deleted file mode 100644
index 802fd5715..000000000
--- a/libbb/error_msg.c
+++ /dev/null
@@ -1,19 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include "libbb.h"
11
12void FAST_FUNC bb_error_msg(const char *s, ...)
13{
14 va_list p;
15
16 va_start(p, s);
17 bb_verror_msg(s, p, NULL);
18 va_end(p);
19}
diff --git a/libbb/error_msg_and_die.c b/libbb/error_msg_and_die.c
deleted file mode 100644
index 243433b2d..000000000
--- a/libbb/error_msg_and_die.c
+++ /dev/null
@@ -1,20 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include "libbb.h"
11
12void FAST_FUNC bb_error_msg_and_die(const char *s, ...)
13{
14 va_list p;
15
16 va_start(p, s);
17 bb_verror_msg(s, p, NULL);
18 va_end(p);
19 xfunc_die();
20}
diff --git a/libbb/execable.c b/libbb/execable.c
index 06b1c534b..de368fad0 100644
--- a/libbb/execable.c
+++ b/libbb/execable.c
@@ -109,3 +109,11 @@ int FAST_FUNC bb_execvp(const char *file, char *const argv[])
109 argv); 109 argv);
110} 110}
111#endif 111#endif
112
113int FAST_FUNC BB_EXECVP_or_die(char **argv)
114{
115 BB_EXECVP(argv[0], argv);
116 /* SUSv3-mandated exit codes */
117 xfunc_error_retval = (errno == ENOENT) ? 127 : 126;
118 bb_perror_msg_and_die("can't execute '%s'", argv[0]);
119}
diff --git a/libbb/herror_msg.c b/libbb/herror_msg.c
index 7e4f64045..ca9274cf7 100644
--- a/libbb/herror_msg.c
+++ b/libbb/herror_msg.c
@@ -6,7 +6,6 @@
6 * 6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */ 8 */
9
10#include "libbb.h" 9#include "libbb.h"
11 10
12void FAST_FUNC bb_herror_msg(const char *s, ...) 11void FAST_FUNC bb_herror_msg(const char *s, ...)
@@ -17,3 +16,13 @@ void FAST_FUNC bb_herror_msg(const char *s, ...)
17 bb_verror_msg(s, p, hstrerror(h_errno)); 16 bb_verror_msg(s, p, hstrerror(h_errno));
18 va_end(p); 17 va_end(p);
19} 18}
19
20void FAST_FUNC bb_herror_msg_and_die(const char *s, ...)
21{
22 va_list p;
23
24 va_start(p, s);
25 bb_verror_msg(s, p, hstrerror(h_errno));
26 va_end(p);
27 xfunc_die();
28}
diff --git a/libbb/herror_msg_and_die.c b/libbb/herror_msg_and_die.c
deleted file mode 100644
index 230fe645a..000000000
--- a/libbb/herror_msg_and_die.c
+++ /dev/null
@@ -1,20 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include "libbb.h"
11
12void FAST_FUNC bb_herror_msg_and_die(const char *s, ...)
13{
14 va_list p;
15
16 va_start(p, s);
17 bb_verror_msg(s, p, hstrerror(h_errno));
18 va_end(p);
19 xfunc_die();
20}
diff --git a/libbb/parse_config.c b/libbb/parse_config.c
index c511d97fb..b7c3a00e0 100644
--- a/libbb/parse_config.c
+++ b/libbb/parse_config.c
@@ -128,8 +128,8 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
128 int ntokens, mintokens; 128 int ntokens, mintokens;
129 int t, len; 129 int t, len;
130 130
131 ntokens = flags & 0xFF; 131 ntokens = (uint8_t)flags;
132 mintokens = (flags & 0xFF00) >> 8; 132 mintokens = (uint8_t)(flags >> 8);
133 133
134 if (parser == NULL) 134 if (parser == NULL)
135 return 0; 135 return 0;
@@ -159,7 +159,8 @@ again:
159 parser->data = xstrdup(line); 159 parser->data = xstrdup(line);
160 160
161 /* Tokenize the line */ 161 /* Tokenize the line */
162 for (t = 0; *line && *line != delims[0] && t < ntokens; t++) { 162 t = 0;
163 do {
163 /* Pin token */ 164 /* Pin token */
164 tokens[t] = line; 165 tokens[t] = line;
165 166
@@ -179,10 +180,10 @@ again:
179 } 180 }
180 181
181 /* Token not terminated? */ 182 /* Token not terminated? */
182 if (line[0] == delims[0]) 183 if (*line == delims[0])
183 *line = '\0'; 184 *line = '\0';
184 else if (line[0] != '\0') 185 else if (*line != '\0')
185 *(line++) = '\0'; 186 *line++ = '\0';
186 187
187#if 0 /* unused so far */ 188#if 0 /* unused so far */
188 if (flags & PARSE_ESCAPE) { 189 if (flags & PARSE_ESCAPE) {
@@ -201,17 +202,20 @@ again:
201 *to = '\0'; 202 *to = '\0';
202 } 203 }
203#endif 204#endif
204
205 /* Skip possible delimiters */ 205 /* Skip possible delimiters */
206 if (flags & PARSE_COLLAPSE) 206 if (flags & PARSE_COLLAPSE)
207 line += strspn(line, delims + 1); 207 line += strspn(line, delims + 1);
208 } 208
209 t++;
210 } while (*line && *line != delims[0] && t < ntokens);
209 211
210 if (t < mintokens) { 212 if (t < mintokens) {
211 bb_error_msg("bad line %u: %d tokens found, %d needed", 213 bb_error_msg("bad line %u: %d tokens found, %d needed",
212 parser->lineno, t, mintokens); 214 parser->lineno, t, mintokens);
213 if (flags & PARSE_MIN_DIE) 215 if (flags & PARSE_MIN_DIE)
214 xfunc_die(); 216 xfunc_die();
217 if (flags & PARSE_KEEP_COPY)
218 free(parser->data);
215 goto again; 219 goto again;
216 } 220 }
217 221
diff --git a/libbb/perror_msg.c b/libbb/perror_msg.c
index 6c8e1b51e..cbba805fb 100644
--- a/libbb/perror_msg.c
+++ b/libbb/perror_msg.c
@@ -6,7 +6,6 @@
6 * 6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */ 8 */
9
10#include "libbb.h" 9#include "libbb.h"
11 10
12void FAST_FUNC bb_perror_msg(const char *s, ...) 11void FAST_FUNC bb_perror_msg(const char *s, ...)
@@ -19,7 +18,23 @@ void FAST_FUNC bb_perror_msg(const char *s, ...)
19 va_end(p); 18 va_end(p);
20} 19}
21 20
21void FAST_FUNC bb_perror_msg_and_die(const char *s, ...)
22{
23 va_list p;
24
25 va_start(p, s);
26 /* Guard against "<error message>: Success" */
27 bb_verror_msg(s, p, errno ? strerror(errno) : NULL);
28 va_end(p);
29 xfunc_die();
30}
31
22void FAST_FUNC bb_simple_perror_msg(const char *s) 32void FAST_FUNC bb_simple_perror_msg(const char *s)
23{ 33{
24 bb_perror_msg("%s", s); 34 bb_perror_msg("%s", s);
25} 35}
36
37void FAST_FUNC bb_simple_perror_msg_and_die(const char *s)
38{
39 bb_perror_msg_and_die("%s", s);
40}
diff --git a/libbb/perror_msg_and_die.c b/libbb/perror_msg_and_die.c
deleted file mode 100644
index 15615fa22..000000000
--- a/libbb/perror_msg_and_die.c
+++ /dev/null
@@ -1,26 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include "libbb.h"
11
12void FAST_FUNC bb_perror_msg_and_die(const char *s, ...)
13{
14 va_list p;
15
16 va_start(p, s);
17 /* Guard against "<error message>: Success" */
18 bb_verror_msg(s, p, errno ? strerror(errno) : NULL);
19 va_end(p);
20 xfunc_die();
21}
22
23void FAST_FUNC bb_simple_perror_msg_and_die(const char *s)
24{
25 bb_perror_msg_and_die("%s", s);
26}
diff --git a/libbb/platform.c b/libbb/platform.c
index 8642337d4..67048648f 100644
--- a/libbb/platform.c
+++ b/libbb/platform.c
@@ -120,3 +120,30 @@ char* FAST_FUNC strcasestr(const char *s, const char *pattern)
120 return 0; 120 return 0;
121} 121}
122#endif 122#endif
123
124#ifndef HAVE_STRSEP
125/* Copyright (C) 2004 Free Software Foundation, Inc. */
126char* FAST_FUNC strsep(char **stringp, const char *delim)
127{
128 char *start = *stringp;
129 char *ptr;
130
131 if (!start)
132 return NULL;
133
134 if (!*delim)
135 ptr = start + strlen(start);
136 else {
137 ptr = strpbrk(start, delim);
138 if (!ptr) {
139 *stringp = NULL;
140 return start;
141 }
142 }
143
144 *ptr = '\0';
145 *stringp = ptr + 1;
146
147 return start;
148}
149#endif
diff --git a/libbb/read_printf.c b/libbb/read_printf.c
index 53f528f5a..1b215f97a 100644
--- a/libbb/read_printf.c
+++ b/libbb/read_printf.c
@@ -265,7 +265,7 @@ void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
265 265
266 /* .gz and .bz2 both have 2-byte signature, and their 266 /* .gz and .bz2 both have 2-byte signature, and their
267 * unpack_XXX_stream wants this header skipped. */ 267 * unpack_XXX_stream wants this header skipped. */
268 xread(fd, magic.b16, sizeof(magic.b16)); 268 xread(fd, magic.b16, sizeof(magic.b16[0]));
269 if (ENABLE_FEATURE_SEAMLESS_GZ 269 if (ENABLE_FEATURE_SEAMLESS_GZ
270 && magic.b16[0] == GZIP_MAGIC 270 && magic.b16[0] == GZIP_MAGIC
271 ) { 271 ) {
@@ -289,15 +289,13 @@ void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
289 if (ENABLE_FEATURE_SEAMLESS_XZ 289 if (ENABLE_FEATURE_SEAMLESS_XZ
290 && magic.b16[0] == XZ_MAGIC1 290 && magic.b16[0] == XZ_MAGIC1
291 ) { 291 ) {
292 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
293 /* More info at: http://tukaani.org/xz/xz-file-format.txt */
294 offset = -6; 292 offset = -6;
295 xread(fd, magic.b32, sizeof(magic.b32)); 293 xread(fd, magic.b32, sizeof(magic.b32[0]));
296 if (magic.b32[0] == XZ_MAGIC2) { 294 if (magic.b32[0] == XZ_MAGIC2) {
297# if BB_MMU 295# if BB_MMU
298 xformer = unpack_xz_stream; 296 xformer = unpack_xz_stream;
299 /* unpack_xz_stream wants fd at position 0 */ 297 /* unpack_xz_stream wants fd at position 6, no need to seek */
300 xlseek(fd, offset, SEEK_CUR); 298 //xlseek(fd, offset, SEEK_CUR);
301# else 299# else
302 xformer_prog = "unxz"; 300 xformer_prog = "unxz";
303# endif 301# endif
diff --git a/libbb/run_shell.c b/libbb/run_shell.c
index 6f98bd695..4d92c3caa 100644
--- a/libbb/run_shell.c
+++ b/libbb/run_shell.c
@@ -49,15 +49,14 @@ void FAST_FUNC set_current_security_context(security_context_t sid)
49 49
50#endif 50#endif
51 51
52/* Run SHELL, or DEFAULT_SHELL if SHELL is empty. 52/* Run SHELL, or DEFAULT_SHELL if SHELL is "" or NULL.
53 If COMMAND is nonzero, pass it to the shell with the -c option. 53 * If COMMAND is nonzero, pass it to the shell with the -c option.
54 If ADDITIONAL_ARGS is nonzero, pass it to the shell as more 54 * If ADDITIONAL_ARGS is nonzero, pass it to the shell as more
55 arguments. */ 55 * arguments. */
56
57void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) 56void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command, const char **additional_args)
58{ 57{
59 const char **args; 58 const char **args;
60 int argno = 1; 59 int argno;
61 int additional_args_cnt = 0; 60 int additional_args_cnt = 0;
62 61
63 for (args = additional_args; args && *args; args++) 62 for (args = additional_args; args && *args; args++)
@@ -65,11 +64,13 @@ void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command,
65 64
66 args = xmalloc(sizeof(char*) * (4 + additional_args_cnt)); 65 args = xmalloc(sizeof(char*) * (4 + additional_args_cnt));
67 66
68 args[0] = bb_get_last_path_component_nostrip(xstrdup(shell)); 67 if (!shell || !shell[0])
68 shell = DEFAULT_SHELL;
69 69
70 args[0] = bb_get_last_path_component_nostrip(shell);
70 if (loginshell) 71 if (loginshell)
71 args[0] = xasprintf("-%s", args[0]); 72 args[0] = xasprintf("-%s", args[0]);
72 73 argno = 1;
73 if (command) { 74 if (command) {
74 args[argno++] = "-c"; 75 args[argno++] = "-c";
75 args[argno++] = command; 76 args[argno++] = command;
@@ -79,6 +80,7 @@ void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command,
79 args[argno++] = *additional_args; 80 args[argno++] = *additional_args;
80 } 81 }
81 args[argno] = NULL; 82 args[argno] = NULL;
83
82#if ENABLE_SELINUX 84#if ENABLE_SELINUX
83 if (current_sid) 85 if (current_sid)
84 setexeccon(current_sid); 86 setexeccon(current_sid);
@@ -86,5 +88,5 @@ void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command,
86 freecon(current_sid); 88 freecon(current_sid);
87#endif 89#endif
88 execv(shell, (char **) args); 90 execv(shell, (char **) args);
89 bb_perror_msg_and_die("can't run '%s'", shell); 91 bb_perror_msg_and_die("can't execute '%s'", shell);
90} 92}
diff --git a/libbb/setup_environment.c b/libbb/setup_environment.c
index 13e60d8e4..a95fbc5bf 100644
--- a/libbb/setup_environment.c
+++ b/libbb/setup_environment.c
@@ -43,7 +43,7 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
43 const char *term; 43 const char *term;
44 44
45 /* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH. 45 /* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH.
46 Unset all other environment variables. */ 46 * Unset all other environment variables. */
47 term = getenv("TERM"); 47 term = getenv("TERM");
48 clearenv(); 48 clearenv();
49 if (term) 49 if (term)
@@ -57,7 +57,7 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
57 //xsetenv("SHELL", shell); 57 //xsetenv("SHELL", shell);
58 } else if (flags & SETUP_ENV_CHANGEENV) { 58 } else if (flags & SETUP_ENV_CHANGEENV) {
59 /* Set HOME, SHELL, and if not becoming a super-user, 59 /* Set HOME, SHELL, and if not becoming a super-user,
60 USER and LOGNAME. */ 60 * USER and LOGNAME. */
61 if (pw->pw_uid) { 61 if (pw->pw_uid) {
62 shortcut: 62 shortcut:
63 xsetenv("USER", pw->pw_name); 63 xsetenv("USER", pw->pw_name);
diff --git a/libbb/verror_msg.c b/libbb/verror_msg.c
index 613432906..c5fbc380c 100644
--- a/libbb/verror_msg.c
+++ b/libbb/verror_msg.c
@@ -76,12 +76,9 @@ void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
76 free(msg); 76 free(msg);
77} 77}
78 78
79
80#ifdef VERSION_WITH_WRITEV 79#ifdef VERSION_WITH_WRITEV
81
82/* Code size is approximately the same, but currently it's the only user 80/* Code size is approximately the same, but currently it's the only user
83 * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */ 81 * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */
84
85void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr) 82void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
86{ 83{
87 int strerr_len, msgeol_len; 84 int strerr_len, msgeol_len;
@@ -139,3 +136,23 @@ void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
139 free(msgc); 136 free(msgc);
140} 137}
141#endif 138#endif
139
140
141void FAST_FUNC bb_error_msg_and_die(const char *s, ...)
142{
143 va_list p;
144
145 va_start(p, s);
146 bb_verror_msg(s, p, NULL);
147 va_end(p);
148 xfunc_die();
149}
150
151void FAST_FUNC bb_error_msg(const char *s, ...)
152{
153 va_list p;
154
155 va_start(p, s);
156 bb_verror_msg(s, p, NULL);
157 va_end(p);
158}
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index 08d9199c1..2b6ee9e74 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -70,40 +70,6 @@ pid_t FAST_FUNC xspawn(char **argv)
70 return pid; 70 return pid;
71} 71}
72 72
73pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options)
74{
75 pid_t r;
76
77 do
78 r = waitpid(pid, wstat, options);
79 while ((r == -1) && (errno == EINTR));
80 return r;
81}
82
83pid_t FAST_FUNC wait_any_nohang(int *wstat)
84{
85 return safe_waitpid(-1, wstat, WNOHANG);
86}
87
88// Wait for the specified child PID to exit, returning child's error return.
89int FAST_FUNC wait4pid(pid_t pid)
90{
91 int status;
92
93 if (pid <= 0) {
94 /*errno = ECHILD; -- wrong. */
95 /* we expect errno to be already set from failed [v]fork/exec */
96 return -1;
97 }
98 if (safe_waitpid(pid, &status, 0) == -1)
99 return -1;
100 if (WIFEXITED(status))
101 return WEXITSTATUS(status);
102 if (WIFSIGNALED(status))
103 return WTERMSIG(status) + 0x180;
104 return 0;
105}
106
107#if ENABLE_FEATURE_PREFER_APPLETS 73#if ENABLE_FEATURE_PREFER_APPLETS
108void FAST_FUNC save_nofork_data(struct nofork_save_area *save) 74void FAST_FUNC save_nofork_data(struct nofork_save_area *save)
109{ 75{
@@ -252,7 +218,7 @@ void FAST_FUNC re_exec(char **argv)
252 * "we have (already) re-execed, don't do it again" flag */ 218 * "we have (already) re-execed, don't do it again" flag */
253 argv[0][0] |= 0x80; 219 argv[0][0] |= 0x80;
254 execv(bb_busybox_exec_path, argv); 220 execv(bb_busybox_exec_path, argv);
255 bb_perror_msg_and_die("exec %s", bb_busybox_exec_path); 221 bb_perror_msg_and_die("can't execute '%s'", bb_busybox_exec_path);
256} 222}
257 223
258pid_t FAST_FUNC fork_or_rexec(char **argv) 224pid_t FAST_FUNC fork_or_rexec(char **argv)
@@ -261,26 +227,12 @@ pid_t FAST_FUNC fork_or_rexec(char **argv)
261 /* Maybe we are already re-execed and come here again? */ 227 /* Maybe we are already re-execed and come here again? */
262 if (re_execed) 228 if (re_execed)
263 return 0; 229 return 0;
264 pid = vfork(); 230 pid = xvfork();
265 if (pid < 0) /* wtf? */
266 bb_perror_msg_and_die("vfork");
267 if (pid) /* parent */ 231 if (pid) /* parent */
268 return pid; 232 return pid;
269 /* child - re-exec ourself */ 233 /* child - re-exec ourself */
270 re_exec(argv); 234 re_exec(argv);
271} 235}
272#else
273/* Dance around (void)...*/
274#undef fork_or_rexec
275pid_t FAST_FUNC fork_or_rexec(void)
276{
277 pid_t pid;
278 pid = fork();
279 if (pid < 0) /* wtf? */
280 bb_perror_msg_and_die("fork");
281 return pid;
282}
283#define fork_or_rexec(argv) fork_or_rexec()
284#endif 236#endif
285 237
286/* Due to a #define in libbb.h on MMU systems we actually have 1 argument - 238/* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index c3ee633e4..2de6de7c5 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -7,6 +7,7 @@
7 * Licensed under GPLv2, see file LICENSE in this tarball for details. 7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 */ 8 */
9 9
10#include <sys/types.h>
10#include <sys/socket.h> /* netinet/in.h needs it */ 11#include <sys/socket.h> /* netinet/in.h needs it */
11#include <netinet/in.h> 12#include <netinet/in.h>
12#include <net/if.h> 13#include <net/if.h>
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 65437211d..275dd4b62 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -268,3 +268,37 @@ int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
268{ 268{
269 return tcsetattr(STDIN_FILENO, TCSANOW, tp); 269 return tcsetattr(STDIN_FILENO, TCSANOW, tp);
270} 270}
271
272pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options)
273{
274 pid_t r;
275
276 do
277 r = waitpid(pid, wstat, options);
278 while ((r == -1) && (errno == EINTR));
279 return r;
280}
281
282pid_t FAST_FUNC wait_any_nohang(int *wstat)
283{
284 return safe_waitpid(-1, wstat, WNOHANG);
285}
286
287// Wait for the specified child PID to exit, returning child's error return.
288int FAST_FUNC wait4pid(pid_t pid)
289{
290 int status;
291
292 if (pid <= 0) {
293 /*errno = ECHILD; -- wrong. */
294 /* we expect errno to be already set from failed [v]fork/exec */
295 return -1;
296 }
297 if (safe_waitpid(pid, &status, 0) == -1)
298 return -1;
299 if (WIFEXITED(status))
300 return WEXITSTATUS(status);
301 if (WIFSIGNALED(status))
302 return WTERMSIG(status) + 0x180;
303 return 0;
304}
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 7feb58036..3e189c2d1 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -337,6 +337,11 @@ void FAST_FUNC bb_unsetenv(const char *var)
337 free(tp); 337 free(tp);
338} 338}
339 339
340void FAST_FUNC bb_unsetenv_and_free(char *var)
341{
342 bb_unsetenv(var);
343 free(var);
344}
340 345
341// Die with an error message if we can't set gid. (Because resource limits may 346// Die with an error message if we can't set gid. (Because resource limits may
342// limit this user to a given number of processes, and if that fills up the 347// limit this user to a given number of processes, and if that fills up the
@@ -598,3 +603,14 @@ void FAST_FUNC generate_uuid(uint8_t *buf)
598 /* variant = 10x */ 603 /* variant = 10x */
599 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; 604 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
600} 605}
606
607#if BB_MMU
608pid_t FAST_FUNC xfork(void)
609{
610 pid_t pid;
611 pid = fork();
612 if (pid < 0) /* wtf? */
613 bb_perror_msg_and_die("vfork"+1);
614 return pid;
615}
616#endif