aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-04-11 23:20:53 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-04-11 23:20:53 +0000
commit50f7f446ecaadef6895a4ee601567e0b68330637 (patch)
treea10f1d7fd192b8c7ee6cb3d5aa46a0295dd08f2a
parentd7c8196c1bba74d1e27ac9cb5e2eb57413daa463 (diff)
downloadbusybox-w32-50f7f446ecaadef6895a4ee601567e0b68330637.tar.gz
busybox-w32-50f7f446ecaadef6895a4ee601567e0b68330637.tar.bz2
busybox-w32-50f7f446ecaadef6895a4ee601567e0b68330637.zip
bb_full_fd_action: remove potential xmalloc from NOFORK path
cat: stop using stdio.h opens libbb: introduce & use open[3]_or_warn function old new delta open3_or_warn - 54 +54 bb_cat 115 144 +29 open_or_warn - 25 +25 unlzma 2404 2412 +8 chattr_main 334 339 +5 xstrtoul_range_sfx 251 255 +4 telnet_main 1514 1510 -4 static.opt 4 - -4 qgravechar 122 118 -4 fuser_add_pid 61 54 -7 fuser_add_inode 154 147 -7 writeFileToTarball 1542 1534 -8 refresh 1156 1148 -8 do_show 856 846 -10 read_leases 212 200 -12 setup_redirects 236 222 -14 iproute_list_or_flush 1582 1568 -14 read_config 427 411 -16 write_leases 284 264 -20 hash_file 338 318 -20 copy_file 1760 1740 -20 do_iproute 2610 2588 -22 bb_full_fd_action 320 269 -51 open_to_or_warn 103 49 -54 fuser_main 1660 1596 -64 .rodata 131160 131096 -64 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 4/19 up/down: 125/-423) Total: -298 bytes
-rw-r--r--archival/bbunzip.c9
-rw-r--r--archival/tar.c5
-rw-r--r--coreutils/cat.c13
-rw-r--r--coreutils/dos2unix.c6
-rw-r--r--coreutils/md5_sha1_sum.c3
-rw-r--r--e2fsprogs/fsck.c1
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/copy_file.c10
-rw-r--r--libbb/copyfd.c6
-rw-r--r--libbb/wfopen_input.c2
-rw-r--r--libbb/xfuncs.c32
-rw-r--r--miscutils/crond.c13
-rw-r--r--networking/libiproute/iproute.c3
-rw-r--r--networking/libiproute/iptunnel.c3
-rw-r--r--networking/udhcp/files.c9
-rw-r--r--procps/fuser.c217
-rw-r--r--procps/sysctl.c3
-rw-r--r--shell/hush.c3
-rw-r--r--shell/lash.c3
-rw-r--r--util-linux/switch_root.c2
20 files changed, 183 insertions, 162 deletions
diff --git a/archival/bbunzip.c b/archival/bbunzip.c
index 3c3cc2886..4dba498b8 100644
--- a/archival/bbunzip.c
+++ b/archival/bbunzip.c
@@ -20,16 +20,11 @@ enum {
20static 20static
21int open_to_or_warn(int to_fd, const char *filename, int flags, int mode) 21int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
22{ 22{
23 int fd = open(filename, flags, mode); 23 int fd = open3_or_warn(filename, flags, mode);
24 if (fd < 0) { 24 if (fd < 0) {
25 bb_perror_msg("%s", filename);
26 return 1; 25 return 1;
27 } 26 }
28 if (fd != to_fd) { 27 xmove_fd(fd, to_fd);
29 if (dup2(fd, to_fd) < 0)
30 bb_perror_msg_and_die("cannot dup");
31 close(fd);
32 }
33 return 0; 28 return 0;
34} 29}
35 30
diff --git a/archival/tar.c b/archival/tar.c
index f2f1ccd06..11a74dfe9 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -440,9 +440,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
440 /* Is this a regular file? */ 440 /* Is this a regular file? */
441 if (tbInfo->hlInfo == NULL && S_ISREG(statbuf->st_mode)) { 441 if (tbInfo->hlInfo == NULL && S_ISREG(statbuf->st_mode)) {
442 /* open the file we want to archive, and make sure all is well */ 442 /* open the file we want to archive, and make sure all is well */
443 inputFileFd = open(fileName, O_RDONLY); 443 inputFileFd = open_or_warn(fileName, O_RDONLY);
444 if (inputFileFd < 0) { 444 if (inputFileFd < 0) {
445 bb_perror_msg("%s: cannot open", fileName);
446 return FALSE; 445 return FALSE;
447 } 446 }
448 } 447 }
@@ -455,7 +454,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
455 /* If it was a regular file, write out the body */ 454 /* If it was a regular file, write out the body */
456 if (inputFileFd >= 0) { 455 if (inputFileFd >= 0) {
457 size_t readSize; 456 size_t readSize;
458 /* Wwrite the file to the archive. */ 457 /* Write the file to the archive. */
459 /* We record size into header first, */ 458 /* We record size into header first, */
460 /* and then write out file. If file shrinks in between, */ 459 /* and then write out file. If file shrinks in between, */
461 /* tar will be corrupted. So we don't allow for that. */ 460 /* tar will be corrupted. So we don't allow for that. */
diff --git a/coreutils/cat.c b/coreutils/cat.c
index eb141dc79..ed3f33650 100644
--- a/coreutils/cat.c
+++ b/coreutils/cat.c
@@ -19,18 +19,21 @@ int bb_cat(char **argv)
19{ 19{
20 static const char *const argv_dash[] = { "-", NULL }; 20 static const char *const argv_dash[] = { "-", NULL };
21 21
22 FILE *f; 22 int fd;
23 int retval = EXIT_SUCCESS; 23 int retval = EXIT_SUCCESS;
24 24
25 if (!*argv) 25 if (!*argv)
26 argv = (char**) &argv_dash; 26 argv = (char**) &argv_dash;
27 27
28 do { 28 do {
29 f = fopen_or_warn_stdin(*argv); 29 fd = STDIN_FILENO;
30 if (f) { 30 if (!LONE_DASH(*argv))
31 fd = open_or_warn(*argv, O_RDONLY);
32 if (fd >= 0) {
31 /* This is not an xfunc - never exits */ 33 /* This is not an xfunc - never exits */
32 off_t r = bb_copyfd_eof(fileno(f), STDOUT_FILENO); 34 off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
33 fclose_if_not_stdin(f); 35 if (fd != STDIN_FILENO)
36 close(fd);
34 if (r >= 0) 37 if (r >= 0)
35 continue; 38 continue;
36 } 39 }
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index 1d35d524a..04c56ac9c 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -30,7 +30,7 @@ static int convert(char *fn)
30 /* 30 /*
31 The file is then created with mode read/write and 31 The file is then created with mode read/write and
32 permissions 0666 for glibc 2.0.6 and earlier or 32 permissions 0666 for glibc 2.0.6 and earlier or
33 0600 for glibc 2.0.7 and later. 33 0600 for glibc 2.0.7 and later.
34 */ 34 */
35 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn); 35 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn);
36 /* 36 /*
@@ -38,8 +38,8 @@ static int convert(char *fn)
38 hold the full path. However if the output is truncated the 38 hold the full path. However if the output is truncated the
39 subsequent call to mkstemp would fail. 39 subsequent call to mkstemp would fail.
40 */ 40 */
41 if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1 41 i = mkstemp(&bb_common_bufsiz1[0]);
42 || chmod(bb_common_bufsiz1, 0600) == -1) { 42 if (i == -1 || chmod(bb_common_bufsiz1, 0600) == -1) {
43 bb_perror_nomsg_and_die(); 43 bb_perror_nomsg_and_die();
44 } 44 }
45 out = fdopen(i, "w+"); 45 out = fdopen(i, "w+");
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index 417e90b00..a3818d519 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -38,9 +38,8 @@ static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
38 38
39 src_fd = STDIN_FILENO; 39 src_fd = STDIN_FILENO;
40 if (NOT_LONE_DASH(filename)) { 40 if (NOT_LONE_DASH(filename)) {
41 src_fd = open(filename, O_RDONLY); 41 src_fd = open_or_warn(filename, O_RDONLY);
42 if (src_fd < 0) { 42 if (src_fd < 0) {
43 bb_perror_msg("%s", filename);
44 return NULL; 43 return NULL;
45 } 44 }
46 } 45 }
diff --git a/e2fsprogs/fsck.c b/e2fsprogs/fsck.c
index cf7f6f070..446541e84 100644
--- a/e2fsprogs/fsck.c
+++ b/e2fsprogs/fsck.c
@@ -396,7 +396,6 @@ static void load_fs_info(const char *filename)
396 396
397 f = fopen_or_warn(filename, "r"); 397 f = fopen_or_warn(filename, "r");
398 if (f == NULL) { 398 if (f == NULL) {
399 /*bb_perror_msg("WARNING: cannot open %s", filename);*/
400 return; 399 return;
401 } 400 }
402 while (1) { 401 while (1) {
diff --git a/include/libbb.h b/include/libbb.h
index c3baf9e7c..e8f1cf653 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -288,6 +288,8 @@ void xunlink(const char *pathname);
288void xstat(const char *pathname, struct stat *buf); 288void xstat(const char *pathname, struct stat *buf);
289int xopen(const char *pathname, int flags); 289int xopen(const char *pathname, int flags);
290int xopen3(const char *pathname, int flags, int mode); 290int xopen3(const char *pathname, int flags, int mode);
291int open_or_warn(const char *pathname, int flags);
292int open3_or_warn(const char *pathname, int flags, int mode);
291off_t xlseek(int fd, off_t offset, int whence); 293off_t xlseek(int fd, off_t offset, int whence);
292off_t fdlength(int fd); 294off_t fdlength(int fd);
293 295
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 7d85920d9..700564212 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -217,9 +217,8 @@ int copy_file(const char *source, const char *dest, int flags)
217 add_to_ino_dev_hashtable(&source_stat, dest); 217 add_to_ino_dev_hashtable(&source_stat, dest);
218 } 218 }
219 219
220 src_fd = open(source, O_RDONLY); 220 src_fd = open_or_warn(source, O_RDONLY);
221 if (src_fd == -1) { 221 if (src_fd < 0) {
222 bb_perror_msg("cannot open '%s'", source);
223 return -1; 222 return -1;
224 } 223 }
225 224
@@ -237,9 +236,8 @@ int copy_file(const char *source, const char *dest, int flags)
237 return ovr; 236 return ovr;
238 } 237 }
239 /* It shouldn't exist. If it exists, do not open (symlink attack?) */ 238 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
240 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode); 239 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
241 if (dst_fd == -1) { 240 if (dst_fd < 0) {
242 bb_perror_msg("cannot open '%s'", dest);
243 close(src_fd); 241 close(src_fd);
244 return -1; 242 return -1;
245 } 243 }
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index aa8fbb967..3255e424a 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -14,14 +14,13 @@
14#define BUFSIZ 4096 14#define BUFSIZ 4096
15#endif 15#endif
16 16
17/* Used by NOFORK applets (e.g. cat) - must be very careful 17/* Used by NOFORK applets (e.g. cat) - must not use xmalloc */
18 * when calling xfuncs, allocating memory, with signals, termios, etc... */
19 18
20static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) 19static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
21{ 20{
22 int status = -1; 21 int status = -1;
23 off_t total = 0; 22 off_t total = 0;
24 RESERVE_CONFIG_BUFFER(buffer, BUFSIZ); 23 char buffer[BUFSIZ];
25 24
26 if (src_fd < 0) 25 if (src_fd < 0)
27 goto out; 26 goto out;
@@ -63,7 +62,6 @@ static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
63 } 62 }
64 } 63 }
65 out: 64 out:
66 RELEASE_CONFIG_BUFFER(buffer);
67 return status ? -1 : total; 65 return status ? -1 : total;
68} 66}
69 67
diff --git a/libbb/wfopen_input.c b/libbb/wfopen_input.c
index 7a11dacd7..1b4928e1f 100644
--- a/libbb/wfopen_input.c
+++ b/libbb/wfopen_input.c
@@ -11,7 +11,7 @@
11 * is a command line arg. Since often that arg is '-' (meaning stdin), 11 * is a command line arg. Since often that arg is '-' (meaning stdin),
12 * we avoid testing everywhere by consolidating things in this routine. 12 * we avoid testing everywhere by consolidating things in this routine.
13 * 13 *
14 * Note: We also consider "" to main stdin (for 'cmp' at least). 14 * Note: we also consider "" to mean stdin (for 'cmp' at least).
15 */ 15 */
16 16
17#include "libbb.h" 17#include "libbb.h"
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index dde91a2ba..870d736b3 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -106,31 +106,46 @@ FILE *xfopen(const char *path, const char *mode)
106{ 106{
107 FILE *fp = fopen(path, mode); 107 FILE *fp = fopen(path, mode);
108 if (fp == NULL) 108 if (fp == NULL)
109 bb_perror_msg_and_die("%s", path); 109 bb_perror_msg_and_die("cannot open '%s'", path);
110 return fp; 110 return fp;
111} 111}
112 112
113// Die if we can't open an existing file and return an fd. 113// Die if we can't open a file and return a fd.
114int xopen(const char *pathname, int flags) 114int xopen3(const char *pathname, int flags, int mode)
115{ 115{
116 //if (ENABLE_DEBUG && (flags & O_CREAT)) 116 int ret;
117 // bb_error_msg_and_die("xopen() with O_CREAT"); 117
118 ret = open(pathname, flags, mode);
119 if (ret < 0) {
120 bb_perror_msg_and_die("cannot open '%s'", pathname);
121 }
122 return ret;
123}
118 124
125// Die if we can't open an existing file and return a fd.
126int xopen(const char *pathname, int flags)
127{
119 return xopen3(pathname, flags, 0666); 128 return xopen3(pathname, flags, 0666);
120} 129}
121 130
122// Die if we can't open a new file and return an fd. 131// Warn if we can't open a file and return a fd.
123int xopen3(const char *pathname, int flags, int mode) 132int open3_or_warn(const char *pathname, int flags, int mode)
124{ 133{
125 int ret; 134 int ret;
126 135
127 ret = open(pathname, flags, mode); 136 ret = open(pathname, flags, mode);
128 if (ret < 0) { 137 if (ret < 0) {
129 bb_perror_msg_and_die("%s", pathname); 138 bb_perror_msg("cannot open '%s'", pathname);
130 } 139 }
131 return ret; 140 return ret;
132} 141}
133 142
143// Warn if we can't open a file and return a fd.
144int open_or_warn(const char *pathname, int flags)
145{
146 return open3_or_warn(pathname, flags, 0666);
147}
148
134void xunlink(const char *pathname) 149void xunlink(const char *pathname)
135{ 150{
136 if (unlink(pathname)) 151 if (unlink(pathname))
@@ -184,6 +199,7 @@ off_t xlseek(int fd, off_t offset, int whence)
184void die_if_ferror(FILE *fp, const char *fn) 199void die_if_ferror(FILE *fp, const char *fn)
185{ 200{
186 if (ferror(fp)) { 201 if (ferror(fp)) {
202 /* doesn't set useful errno */
187 bb_error_msg_and_die("%s: I/O error", fn); 203 bb_error_msg_and_die("%s: I/O error", fn);
188 } 204 }
189} 205}
diff --git a/miscutils/crond.c b/miscutils/crond.c
index 24aa3dcc3..c7d1a6c50 100644
--- a/miscutils/crond.c
+++ b/miscutils/crond.c
@@ -114,14 +114,14 @@ static void crondlog(const char *ctl, ...)
114 if (LogFile == 0) { 114 if (LogFile == 0) {
115 vsyslog(type, fmt, va); 115 vsyslog(type, fmt, va);
116 } else { 116 } else {
117#if !ENABLE_DEBUG_CROND_OPTION
117 int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600); 118 int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
119#else
120 int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
121#endif
118 if (logfd >= 0) { 122 if (logfd >= 0) {
119 vdprintf(logfd, fmt, va); 123 vdprintf(logfd, fmt, va);
120 close(logfd); 124 close(logfd);
121#if ENABLE_DEBUG_CROND_OPTION
122 } else {
123 bb_perror_msg("can't open log file");
124#endif
125 } 125 }
126 } 126 }
127 } 127 }
@@ -281,10 +281,9 @@ static void startlogger(void)
281 else { /* test logfile */ 281 else { /* test logfile */
282 int logfd; 282 int logfd;
283 283
284 if ((logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600)) >= 0) { 284 logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
285 if (logfd >= 0) {
285 close(logfd); 286 close(logfd);
286 } else {
287 bb_perror_msg("failed to open log file '%s': ", LogFile);
288 } 287 }
289 } 288 }
290#endif 289#endif
diff --git a/networking/libiproute/iproute.c b/networking/libiproute/iproute.c
index a19586c91..23d416344 100644
--- a/networking/libiproute/iproute.c
+++ b/networking/libiproute/iproute.c
@@ -484,10 +484,9 @@ static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
484static void iproute_flush_cache(void) 484static void iproute_flush_cache(void)
485{ 485{
486 static const char fn[] = "/proc/sys/net/ipv4/route/flush"; 486 static const char fn[] = "/proc/sys/net/ipv4/route/flush";
487 int flush_fd = open(fn, O_WRONLY); 487 int flush_fd = open_or_warn(fn, O_WRONLY);
488 488
489 if (flush_fd < 0) { 489 if (flush_fd < 0) {
490 bb_perror_msg("cannot open '%s'", fn);
491 return; 490 return;
492 } 491 }
493 492
diff --git a/networking/libiproute/iptunnel.c b/networking/libiproute/iptunnel.c
index 52a50993d..a2eb0ccda 100644
--- a/networking/libiproute/iptunnel.c
+++ b/networking/libiproute/iptunnel.c
@@ -426,10 +426,9 @@ static void do_tunnels_list(struct ip_tunnel_parm *p)
426 int type; 426 int type;
427 struct ip_tunnel_parm p1; 427 struct ip_tunnel_parm p1;
428 char buf[512]; 428 char buf[512];
429 FILE *fp = fopen("/proc/net/dev", "r"); 429 FILE *fp = fopen_or_warn("/proc/net/dev", "r");
430 430
431 if (fp == NULL) { 431 if (fp == NULL) {
432 bb_perror_msg("fopen");
433 return; 432 return;
434 } 433 }
435 434
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index ab6f4a3e4..41c8717cd 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -311,9 +311,8 @@ int read_config(const char *file)
311 if (keywords[i].def[0]) 311 if (keywords[i].def[0])
312 keywords[i].handler(keywords[i].def, keywords[i].var); 312 keywords[i].handler(keywords[i].def, keywords[i].var);
313 313
314 in = fopen(file, "r"); 314 in = fopen_or_warn(file, "r");
315 if (!in) { 315 if (!in) {
316 bb_error_msg("cannot open config file: %s", file);
317 return 0; 316 return 0;
318 } 317 }
319 318
@@ -360,9 +359,8 @@ void write_leases(void)
360 time_t curr = time(0); 359 time_t curr = time(0);
361 unsigned long tmp_time; 360 unsigned long tmp_time;
362 361
363 fp = open(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666); 362 fp = open3_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
364 if (fp < 0) { 363 if (fp < 0) {
365 bb_error_msg("cannot open %s for writing", server_config.lease_file);
366 return; 364 return;
367 } 365 }
368 366
@@ -401,9 +399,8 @@ void read_leases(const char *file)
401 unsigned int i = 0; 399 unsigned int i = 0;
402 struct dhcpOfferedAddr lease; 400 struct dhcpOfferedAddr lease;
403 401
404 fp = open(file, O_RDONLY); 402 fp = open_or_warn(file, O_RDONLY);
405 if (fp < 0) { 403 if (fp < 0) {
406 bb_error_msg("cannot open %s for reading", file);
407 return; 404 return;
408 } 405 }
409 406
diff --git a/procps/fuser.c b/procps/fuser.c
index c91ae217b..40789dd5e 100644
--- a/procps/fuser.c
+++ b/procps/fuser.c
@@ -34,19 +34,19 @@ static int fuser_option(char *option)
34{ 34{
35 int opt = 0; 35 int opt = 0;
36 36
37 if(!(strlen(option))) return 0; 37 if (!option[0])
38 if(option[0] != '-') return 0; 38 return 0;
39 if (option[0] != '-')
40 return 0;
39 ++option; 41 ++option;
40 while(*option != '\0') { 42 while (*option != '\0') {
41 if(*option == 'm') opt |= FUSER_OPT_MOUNT; 43 if (*option == 'm') opt |= FUSER_OPT_MOUNT;
42 else if(*option == 'k') opt |= FUSER_OPT_KILL; 44 else if (*option == 'k') opt |= FUSER_OPT_KILL;
43 else if(*option == 's') opt |= FUSER_OPT_SILENT; 45 else if (*option == 's') opt |= FUSER_OPT_SILENT;
44 else if(*option == '6') opt |= FUSER_OPT_IP6; 46 else if (*option == '6') opt |= FUSER_OPT_IP6;
45 else if(*option == '4') opt |= FUSER_OPT_IP4; 47 else if (*option == '4') opt |= FUSER_OPT_IP4;
46 else { 48 else
47 bb_error_msg_and_die( 49 bb_error_msg_and_die("unsupported option '%c'", *option);
48 "Unsupported option '%c'", *option);
49 }
50 ++option; 50 ++option;
51 } 51 }
52 return opt; 52 return opt;
@@ -56,7 +56,8 @@ static int fuser_file_to_dev_inode(const char *filename,
56 dev_t *dev, ino_t *inode) 56 dev_t *dev, ino_t *inode)
57{ 57{
58 struct stat f_stat; 58 struct stat f_stat;
59 if((stat(filename, &f_stat)) < 0) return 0; 59 if ((stat(filename, &f_stat)) < 0)
60 return 0;
60 *inode = f_stat.st_ino; 61 *inode = f_stat.st_ino;
61 *dev = f_stat.st_dev; 62 *dev = f_stat.st_dev;
62 return 1; 63 return 1;
@@ -68,7 +69,7 @@ static int fuser_find_socket_dev(dev_t *dev)
68 struct stat buf; 69 struct stat buf;
69 70
70 if (fd >= 0 && (fstat(fd, &buf)) == 0) { 71 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
71 *dev = buf.st_dev; 72 *dev = buf.st_dev;
72 close(fd); 73 close(fd);
73 return 1; 74 return 1;
74 } 75 }
@@ -80,9 +81,11 @@ static int fuser_parse_net_arg(const char *filename,
80{ 81{
81 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5]; 82 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
82 83
83 if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0; 84 if ((sscanf(filename, "%d/%4s", port, tproto)) != 2)
84 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto); 85 return 0;
85 if((access(path, R_OK)) != 0) return 0; 86 sprintf(path, FUSER_PROC_DIR "/net/%s", tproto);
87 if ((access(path, R_OK)) != 0)
88 return 0;
86 *proto = xstrdup(tproto); 89 *proto = xstrdup(tproto);
87 return 1; 90 return 1;
88} 91}
@@ -91,17 +94,19 @@ static int fuser_add_pid(pid_list *plist, pid_t pid)
91{ 94{
92 pid_list *curr = NULL, *last = NULL; 95 pid_list *curr = NULL, *last = NULL;
93 96
94 if(plist->pid == 0) plist->pid = pid; 97 if (plist->pid == 0)
98 plist->pid = pid;
95 curr = plist; 99 curr = plist;
96 while(curr != NULL) { 100 while (curr != NULL) {
97 if(curr->pid == pid) return 1; 101 if (curr->pid == pid)
102 return 1;
98 last = curr; 103 last = curr;
99 curr = curr->next; 104 curr = curr->next;
100 } 105 }
101 curr = xmalloc(sizeof(pid_list)); 106 curr = xzalloc(sizeof(pid_list));
102 last->next = curr; 107 last->next = curr;
103 curr->pid = pid; 108 curr->pid = pid;
104 curr->next = NULL; 109 /*curr->next = NULL;*/
105 return 1; 110 return 1;
106} 111}
107 112
@@ -109,21 +114,22 @@ static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
109{ 114{
110 inode_list *curr = NULL, *last = NULL; 115 inode_list *curr = NULL, *last = NULL;
111 116
112 if(!ilist->inode && !ilist->dev) { 117 if (!ilist->inode && !ilist->dev) {
113 ilist->dev = dev; 118 ilist->dev = dev;
114 ilist->inode = inode; 119 ilist->inode = inode;
115 } 120 }
116 curr = ilist; 121 curr = ilist;
117 while(curr != NULL) { 122 while (curr != NULL) {
118 if(curr->inode == inode && curr->dev == dev) return 1; 123 if (curr->inode == inode && curr->dev == dev)
124 return 1;
119 last = curr; 125 last = curr;
120 curr = curr->next; 126 curr = curr->next;
121 } 127 }
122 curr = xmalloc(sizeof(inode_list)); 128 curr = xzalloc(sizeof(inode_list));
123 last->next = curr; 129 last->next = curr;
124 curr->dev = dev; 130 curr->dev = dev;
125 curr->inode = inode; 131 curr->inode = inode;
126 curr->next = NULL; 132 /*curr->next = NULL;*/
127 return 1; 133 return 1;
128} 134}
129 135
@@ -134,29 +140,31 @@ static int fuser_scan_proc_net(int opts, const char *proto,
134 char addr[128]; 140 char addr[128];
135 ino_t tmp_inode; 141 ino_t tmp_inode;
136 dev_t tmp_dev; 142 dev_t tmp_dev;
137 long long uint64_inode; 143 long long uint64_inode;
138 int tmp_port; 144 int tmp_port;
139 FILE *f; 145 FILE *f;
140 146
141 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0; 147 if (!fuser_find_socket_dev(&tmp_dev))
142 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto); 148 tmp_dev = 0;
143 149 sprintf(path, FUSER_PROC_DIR "/net/%s", proto);
144 if (!(f = fopen(path, "r"))) return 0; 150
145 while(fgets(line, FUSER_MAX_LINE, f)) { 151 f = fopen(path, "r");
146 if(sscanf(line, 152 if (!f)
147 "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x " 153 return 0;
148 "%*x:%*x %*x %*d %*d %llu", 154 while (fgets(line, FUSER_MAX_LINE, f)) {
149 addr, &tmp_port, &uint64_inode) == 3) { 155 if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
150 if((strlen(addr) == 8) && 156 "%*x:%*x %*x %*d %*d %llu",
151 (opts & FUSER_OPT_IP6)) continue; 157 addr, &tmp_port, &uint64_inode) == 3
152 else if((strlen(addr) > 8) && 158 ) {
153 (opts & FUSER_OPT_IP4)) continue; 159 if (strlen(addr) == 8 && (opts & FUSER_OPT_IP6))
154 if(tmp_port == port) { 160 continue;
161 if (strlen(addr) > 8 && (opts & FUSER_OPT_IP4))
162 continue;
163 if (tmp_port == port) {
155 tmp_inode = uint64_inode; 164 tmp_inode = uint64_inode;
156 fuser_add_inode(ilist, tmp_dev, tmp_inode); 165 fuser_add_inode(ilist, tmp_dev, tmp_inode);
157 } 166 }
158 } 167 }
159
160 } 168 }
161 fclose(f); 169 fclose(f);
162 return 1; 170 return 1;
@@ -168,10 +176,10 @@ static int fuser_search_dev_inode(int opts, inode_list *ilist,
168 inode_list *curr; 176 inode_list *curr;
169 curr = ilist; 177 curr = ilist;
170 178
171 while(curr) { 179 while (curr) {
172 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev) 180 if ((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
173 return 1; 181 return 1;
174 if(curr->inode == inode && curr->dev == dev) 182 if (curr->inode == inode && curr->dev == dev)
175 return 1; 183 return 1;
176 curr = curr->next; 184 curr = curr->next;
177 } 185 }
@@ -188,17 +196,19 @@ static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
188 long long uint64_inode; 196 long long uint64_inode;
189 dev_t dev; 197 dev_t dev;
190 198
191 if (!(file = fopen(fname, "r"))) return 0; 199 file = fopen(fname, "r");
200 if (!file)
201 return 0;
192 while (fgets(line, FUSER_MAX_LINE, file)) { 202 while (fgets(line, FUSER_MAX_LINE, file)) {
193 if(sscanf(line, "%*s %*s %*s %x:%x %llu", 203 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
194 &major, &minor, &uint64_inode) != 3) continue; 204 continue;
195 inode = uint64_inode; 205 inode = uint64_inode;
196 if(major == 0 && minor == 0 && inode == 0) continue; 206 if (major == 0 && minor == 0 && inode == 0)
207 continue;
197 dev = makedev(major, minor); 208 dev = makedev(major, minor);
198 if(fuser_search_dev_inode(opts, ilist, dev, inode)) { 209 if (fuser_search_dev_inode(opts, ilist, dev, inode)) {
199 fuser_add_pid(plist, pid); 210 fuser_add_pid(plist, pid);
200 } 211 }
201
202 } 212 }
203 fclose(file); 213 fclose(file);
204 return 1; 214 return 1;
@@ -210,8 +220,9 @@ static int fuser_scan_link(int opts, const char *lname, pid_t pid,
210 ino_t inode; 220 ino_t inode;
211 dev_t dev; 221 dev_t dev;
212 222
213 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0; 223 if (!fuser_file_to_dev_inode(lname, &dev, &inode))
214 if(fuser_search_dev_inode(opts, ilist, dev, inode)) 224 return 0;
225 if (fuser_search_dev_inode(opts, ilist, dev, inode))
215 fuser_add_pid(plist, pid); 226 fuser_add_pid(plist, pid);
216 return 1; 227 return 1;
217} 228}
@@ -223,19 +234,18 @@ static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
223 struct dirent *de; 234 struct dirent *de;
224 char *lname; 235 char *lname;
225 236
226 if((d = opendir(dname))) { 237 d = opendir(dname);
227 while((de = readdir(d)) != NULL) { 238 if (!d)
228 lname = concat_subpath_file(dname, de->d_name); 239 return 0;
229 if(lname == NULL) 240 while ((de = readdir(d)) != NULL) {
230 continue; 241 lname = concat_subpath_file(dname, de->d_name);
231 fuser_scan_link(opts, lname, pid, ilist, plist); 242 if (lname == NULL)
232 free(lname); 243 continue;
233 } 244 fuser_scan_link(opts, lname, pid, ilist, plist);
234 closedir(d); 245 free(lname);
235 } 246 }
236 else return 0; 247 closedir(d);
237 return 1; 248 return 1;
238
239} 249}
240 250
241static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist) 251static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
@@ -245,12 +255,15 @@ static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
245 pid_t pid; 255 pid_t pid;
246 char *dname; 256 char *dname;
247 257
248 if(!(d = opendir(FUSER_PROC_DIR))) return 0; 258 d = opendir(FUSER_PROC_DIR);
249 while((de = readdir(d)) != NULL) { 259 if (!d)
260 return 0;
261 while ((de = readdir(d)) != NULL) {
250 pid = (pid_t)atoi(de->d_name); 262 pid = (pid_t)atoi(de->d_name);
251 if(!pid) continue; 263 if (!pid)
264 continue;
252 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name); 265 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
253 if(chdir(dname) < 0) { 266 if (chdir(dname) < 0) {
254 free(dname); 267 free(dname);
255 continue; 268 continue;
256 } 269 }
@@ -272,9 +285,11 @@ static int fuser_print_pid_list(pid_list *plist)
272{ 285{
273 pid_list *curr = plist; 286 pid_list *curr = plist;
274 287
275 if(plist == NULL) return 0; 288 if (plist == NULL)
276 while(curr != NULL) { 289 return 0;
277 if(curr->pid > 0) printf("%d ", curr->pid); 290 while (curr != NULL) {
291 if (curr->pid > 0)
292 printf("%d ", curr->pid);
278 curr = curr->next; 293 curr = curr->next;
279 } 294 }
280 puts(""); 295 puts("");
@@ -287,12 +302,12 @@ static int fuser_kill_pid_list(pid_list *plist, int sig)
287 pid_t mypid = getpid(); 302 pid_t mypid = getpid();
288 int success = 1; 303 int success = 1;
289 304
290 if(plist == NULL) return 0; 305 if (plist == NULL)
291 while(curr != NULL) { 306 return 0;
292 if(curr->pid > 0 && curr->pid != mypid) { 307 while (curr != NULL) {
308 if (curr->pid > 0 && curr->pid != mypid) {
293 if (kill(curr->pid, sig) != 0) { 309 if (kill(curr->pid, sig) != 0) {
294 bb_perror_msg( 310 bb_perror_msg("kill pid '%d'", curr->pid);
295 "cannot kill pid '%d'", curr->pid);
296 success = 0; 311 success = 0;
297 } 312 }
298 } 313 }
@@ -304,11 +319,12 @@ static int fuser_kill_pid_list(pid_list *plist, int sig)
304int fuser_main(int argc, char **argv); 319int fuser_main(int argc, char **argv);
305int fuser_main(int argc, char **argv) 320int fuser_main(int argc, char **argv)
306{ 321{
322 /*static -- huh???*/ int opt = 0; /* FUSER_OPT_ */
323
307 int port, i, optn; 324 int port, i, optn;
308 int* fni; /* file name indexes of argv */ 325 int* fni; /* file name indexes of argv */
309 int fnic = 0; /* file name index count */ 326 int fnic = 0; /* file name index count */
310 const char *proto; 327 const char *proto;
311 static int opt = 0; /* FUSER_OPT_ */
312 dev_t dev; 328 dev_t dev;
313 ino_t inode; 329 ino_t inode;
314 pid_list *pids; 330 pid_list *pids;
@@ -320,30 +336,31 @@ int fuser_main(int argc, char **argv)
320 bb_show_usage(); 336 bb_show_usage();
321 337
322 fni = xmalloc(sizeof(int)); 338 fni = xmalloc(sizeof(int));
323 for (i=1;i<argc;i++) { 339 for (i = 1; i < argc; i++) {
324 optn = fuser_option(argv[i]); 340 optn = fuser_option(argv[i]);
325 if(optn) opt |= optn; 341 if (optn)
326 else if(argv[i][0] == '-') { 342 opt |= optn;
343 else if (argv[i][0] == '-') {
327 killsig = get_signum(argv[i]+1); 344 killsig = get_signum(argv[i]+1);
328 if(0 > killsig) 345 if (killsig < 0)
329 killsig = SIGTERM; 346 killsig = SIGTERM;
330 } 347 } else {
331 else {
332 fni = xrealloc(fni, sizeof(int) * (fnic+2)); 348 fni = xrealloc(fni, sizeof(int) * (fnic+2));
333 fni[fnic++] = i; 349 fni[fnic++] = i;
334 } 350 }
335 } 351 }
336 if(!fnic) return 1; 352
353 if (!fnic)
354 return 1;
337 355
338 inodes = xmalloc(sizeof(inode_list)); 356 inodes = xmalloc(sizeof(inode_list));
339 for (i=0;i<fnic;i++) { 357 for (i = 0; i < fnic; i++) {
340 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) { 358 if (fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
341 fuser_scan_proc_net(opt, proto, port, inodes); 359 fuser_scan_proc_net(opt, proto, port, inodes);
342 } 360 } else {
343 else { 361 if (!fuser_file_to_dev_inode(argv[fni[i]], &dev, &inode)) {
344 if(!fuser_file_to_dev_inode( 362 if (ENABLE_FEATURE_CLEAN_UP)
345 argv[fni[i]], &dev, &inode)) { 363 free(inodes);
346 if (ENABLE_FEATURE_CLEAN_UP) free(inodes);
347 bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]); 364 bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]);
348 } 365 }
349 fuser_add_inode(inodes, dev, inode); 366 fuser_add_inode(inodes, dev, inode);
@@ -352,17 +369,19 @@ int fuser_main(int argc, char **argv)
352 pids = xmalloc(sizeof(pid_list)); 369 pids = xmalloc(sizeof(pid_list));
353 success = fuser_scan_proc_pids(opt, inodes, pids); 370 success = fuser_scan_proc_pids(opt, inodes, pids);
354 /* if the first pid in the list is 0, none have been found */ 371 /* if the first pid in the list is 0, none have been found */
355 if(pids->pid == 0) success = 0; 372 if (pids->pid == 0)
356 if(success) { 373 success = 0;
357 if(opt & FUSER_OPT_KILL) { 374 if (success) {
375 if (opt & FUSER_OPT_KILL) {
358 success = fuser_kill_pid_list(pids, killsig); 376 success = fuser_kill_pid_list(pids, killsig);
359 } 377 } else if (!(opt & FUSER_OPT_SILENT)) {
360 else if(!(opt & FUSER_OPT_SILENT)) {
361 success = fuser_print_pid_list(pids); 378 success = fuser_print_pid_list(pids);
362 } 379 }
363 } 380 }
364 free(pids); 381 if (ENABLE_FEATURE_CLEAN_UP) {
365 free(inodes); 382 free(pids);
383 free(inodes);
384 }
366 /* return 0 on (success == 1) 1 otherwise */ 385 /* return 0 on (success == 1) 1 otherwise */
367 return (success != 1); 386 return (success != 1);
368} 387}
diff --git a/procps/sysctl.c b/procps/sysctl.c
index b5a01894f..7c72ac933 100644
--- a/procps/sysctl.c
+++ b/procps/sysctl.c
@@ -202,7 +202,8 @@ int sysctl_write_setting(const char *setting, int output)
202 while ((cptr = strchr(outname, '/')) != NULL) 202 while ((cptr = strchr(outname, '/')) != NULL)
203 *cptr = '.'; 203 *cptr = '.';
204 204
205 if ((fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { 205 fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
206 if (fd < 0) {
206 switch (errno) { 207 switch (errno) {
207 case ENOENT: 208 case ENOENT:
208 bb_error_msg(ERR_INVALID_KEY, outname); 209 bb_error_msg(ERR_INVALID_KEY, outname);
diff --git a/shell/hush.c b/shell/hush.c
index 331d591c8..1ad61e54e 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -1019,11 +1019,10 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
1019 } 1019 }
1020 if (redir->dup == -1) { 1020 if (redir->dup == -1) {
1021 mode = redir_table[redir->type].mode; 1021 mode = redir_table[redir->type].mode;
1022 openfd = open(redir->word.gl_pathv[0], mode, 0666); 1022 openfd = open3_or_warn(redir->word.gl_pathv[0], mode, 0666);
1023 if (openfd < 0) { 1023 if (openfd < 0) {
1024 /* this could get lost if stderr has been redirected, but 1024 /* this could get lost if stderr has been redirected, but
1025 bash and ash both lose it as well (though zsh doesn't!) */ 1025 bash and ash both lose it as well (though zsh doesn't!) */
1026 bb_perror_msg("error opening %s", redir->word.gl_pathv[0]);
1027 return 1; 1026 return 1;
1028 } 1027 }
1029 } else { 1028 } else {
diff --git a/shell/lash.c b/shell/lash.c
index aba9c0a2e..c72a65639 100644
--- a/shell/lash.c
+++ b/shell/lash.c
@@ -571,11 +571,10 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
571 break; 571 break;
572 } 572 }
573 573
574 openfd = open(redir->filename, mode, 0666); 574 openfd = open3_or_warn(redir->filename, mode, 0666);
575 if (openfd < 0) { 575 if (openfd < 0) {
576 /* this could get lost if stderr has been redirected, but 576 /* this could get lost if stderr has been redirected, but
577 bash and ash both lose it as well (though zsh doesn't!) */ 577 bash and ash both lose it as well (though zsh doesn't!) */
578 bb_perror_msg("error opening %s", redir->filename);
579 return 1; 578 return 1;
580 } 579 }
581 580
diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c
index 43c2a6991..43377e16e 100644
--- a/util-linux/switch_root.c
+++ b/util-linux/switch_root.c
@@ -111,7 +111,7 @@ int switch_root_main(int argc, char **argv)
111 111
112 if (console) { 112 if (console) {
113 close(0); 113 close(0);
114 if(open(console, O_RDWR) < 0) 114 if (open(console, O_RDWR) < 0)
115 bb_error_msg_and_die("bad console '%s'", console); 115 bb_error_msg_and_die("bad console '%s'", console);
116 dup2(0, 1); 116 dup2(0, 1);
117 dup2(0, 2); 117 dup2(0, 2);