aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2014-05-06 20:41:10 +0100
committerRon Yorston <rmy@pobox.com>2014-05-06 20:41:10 +0100
commitd3bef66324a8ca5eed9ad7c15ead3a1cc9a9151e (patch)
tree4b364ba4b6b9e96c2629fe382fef0248d76833dd /libbb
parent7905d97aeece18da362a5a1e066abff2d2e5c16b (diff)
parentd257608a8429b64e1a04c7cb6d99975eeb2c3955 (diff)
downloadbusybox-w32-d3bef66324a8ca5eed9ad7c15ead3a1cc9a9151e.tar.gz
busybox-w32-d3bef66324a8ca5eed9ad7c15ead3a1cc9a9151e.tar.bz2
busybox-w32-d3bef66324a8ca5eed9ad7c15ead3a1cc9a9151e.zip
Merge branch 'busybox' into merge
Conflicts: debianutils/which.c editors/vi.c libbb/executable.c
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Kbuild.src2
-rw-r--r--libbb/executable.c (renamed from libbb/execable.c)50
-rw-r--r--libbb/getpty.c2
-rw-r--r--libbb/obscure.c2
-rw-r--r--libbb/platform.c18
-rw-r--r--libbb/rtc.c52
-rw-r--r--libbb/verror_msg.c5
-rw-r--r--libbb/xreadlink.c11
8 files changed, 102 insertions, 40 deletions
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index d81ae8468..107c80689 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -30,7 +30,7 @@ lib-y += crc32.o
30lib-y += default_error_retval.o 30lib-y += default_error_retval.o
31lib-y += device_open.o 31lib-y += device_open.o
32lib-y += dump.o 32lib-y += dump.o
33lib-y += execable.o 33lib-y += executable.o
34lib-y += fclose_nonstdin.o 34lib-y += fclose_nonstdin.o
35lib-y += fflush_stdout_and_exit.o 35lib-y += fflush_stdout_and_exit.o
36lib-y += fgets_str.o 36lib-y += fgets_str.o
diff --git a/libbb/execable.c b/libbb/executable.c
index ae61a8800..2e5f6a1b7 100644
--- a/libbb/execable.c
+++ b/libbb/executable.c
@@ -13,7 +13,7 @@
13 * return 1 if found; 13 * return 1 if found;
14 * return 0 otherwise; 14 * return 0 otherwise;
15 */ 15 */
16int FAST_FUNC execable_file(const char *name) 16int FAST_FUNC file_is_executable(const char *name)
17{ 17{
18 struct stat s; 18 struct stat s;
19 return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode)); 19 return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode));
@@ -23,7 +23,7 @@ int FAST_FUNC execable_file(const char *name)
23 * return allocated string containing full path if found; 23 * return allocated string containing full path if found;
24 * PATHp points to the component after the one where it was found 24 * PATHp points to the component after the one where it was found
25 * (or NULL), 25 * (or NULL),
26 * you may call find_execable again with this PATHp to continue 26 * you may call find_executable again with this PATHp to continue
27 * (if it's not NULL). 27 * (if it's not NULL).
28 * return NULL otherwise; (PATHp is undefined) 28 * return NULL otherwise; (PATHp is undefined)
29 * in all cases (*PATHp) contents will be trashed (s/:/NUL/). 29 * in all cases (*PATHp) contents will be trashed (s/:/NUL/).
@@ -32,8 +32,16 @@ int FAST_FUNC execable_file(const char *name)
32#define next_path_sep(s) strchr(s, ':') 32#define next_path_sep(s) strchr(s, ':')
33#endif 33#endif
34 34
35char* FAST_FUNC find_execable(const char *filename, char **PATHp) 35char* FAST_FUNC find_executable(const char *filename, char **PATHp)
36{ 36{
37 /* About empty components in $PATH:
38 * http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
39 * 8.3 Other Environment Variables - PATH
40 * A zero-length prefix is a legacy feature that indicates the current
41 * working directory. It appears as two adjacent colons ( "::" ), as an
42 * initial colon preceding the rest of the list, or as a trailing colon
43 * following the rest of the list.
44 */
37 char *p, *n; 45 char *p, *n;
38#if ENABLE_PLATFORM_MINGW32 46#if ENABLE_PLATFORM_MINGW32
39 char *w; 47 char *w;
@@ -44,21 +52,22 @@ char* FAST_FUNC find_execable(const char *filename, char **PATHp)
44 n = (char*)next_path_sep(p); 52 n = (char*)next_path_sep(p);
45 if (n) 53 if (n)
46 *n++ = '\0'; 54 *n++ = '\0';
47 if (*p != '\0') { /* it's not a PATH="foo::bar" situation */ 55 p = concat_path_file(
48 p = concat_path_file(p, filename); 56 p[0] ? p : ".", /* handle "::" case */
49 if (execable_file(p)) { 57 filename
50 *PATHp = n; 58 );
51 return p; 59 if (file_is_executable(p)) {
52 } 60 *PATHp = n;
61 return p;
62 }
53#if ENABLE_PLATFORM_MINGW32 63#if ENABLE_PLATFORM_MINGW32
54 else if ((w=win32_execable_file(p))) { 64 else if ((w=win32_execable_file(p))) {
55 *PATHp = n; 65 *PATHp = n;
56 free(p);
57 return w;
58 }
59#endif
60 free(p); 66 free(p);
67 return w;
61 } 68 }
69#endif
70 free(p);
62 p = n; 71 p = n;
63 } /* on loop exit p == NULL */ 72 } /* on loop exit p == NULL */
64 return p; 73 return p;
@@ -68,17 +77,14 @@ char* FAST_FUNC find_execable(const char *filename, char **PATHp)
68 * return 1 if found; 77 * return 1 if found;
69 * return 0 otherwise; 78 * return 0 otherwise;
70 */ 79 */
71int FAST_FUNC exists_execable(const char *filename) 80int FAST_FUNC executable_exists(const char *filename)
72{ 81{
73 char *path = xstrdup(getenv("PATH")); 82 char *path = xstrdup(getenv("PATH"));
74 char *tmp = path; 83 char *tmp = path;
75 char *ret = find_execable(filename, &tmp); 84 char *ret = find_executable(filename, &tmp);
76 free(path); 85 free(path);
77 if (ret) { 86 free(ret);
78 free(ret); 87 return ret != NULL;
79 return 1;
80 }
81 return 0;
82} 88}
83 89
84#if ENABLE_FEATURE_PREFER_APPLETS 90#if ENABLE_FEATURE_PREFER_APPLETS
diff --git a/libbb/getpty.c b/libbb/getpty.c
index 435e4d09f..391d729f2 100644
--- a/libbb/getpty.c
+++ b/libbb/getpty.c
@@ -16,7 +16,7 @@ int FAST_FUNC xgetpty(char *line)
16 16
17#if ENABLE_FEATURE_DEVPTS 17#if ENABLE_FEATURE_DEVPTS
18 p = open("/dev/ptmx", O_RDWR); 18 p = open("/dev/ptmx", O_RDWR);
19 if (p > 0) { 19 if (p >= 0) {
20 grantpt(p); /* chmod+chown corresponding slave pty */ 20 grantpt(p); /* chmod+chown corresponding slave pty */
21 unlockpt(p); /* (what does this do?) */ 21 unlockpt(p); /* (what does this do?) */
22# ifndef HAVE_PTSNAME_R 22# ifndef HAVE_PTSNAME_R
diff --git a/libbb/obscure.c b/libbb/obscure.c
index 9ecc1f672..24c4ac917 100644
--- a/libbb/obscure.c
+++ b/libbb/obscure.c
@@ -76,7 +76,7 @@ static int string_checker(const char *p1, const char *p2)
76 ret |= string_checker_helper(p, p2); 76 ret |= string_checker_helper(p, p2);
77 77
78 /* clean up */ 78 /* clean up */
79 memset(p, 0, size); 79 nuke_str(p);
80 free(p); 80 free(p);
81 81
82 return ret; 82 return ret;
diff --git a/libbb/platform.c b/libbb/platform.c
index 19734517b..8d90ca4e9 100644
--- a/libbb/platform.c
+++ b/libbb/platform.c
@@ -17,6 +17,24 @@ char* FAST_FUNC strchrnul(const char *s, int c)
17} 17}
18#endif 18#endif
19 19
20#ifndef HAVE_USLEEP
21int FAST_FUNC usleep(unsigned usec)
22{
23 struct timespec ts;
24 ts.tv_sec = usec / 1000000u;
25 ts.tv_nsec = (usec % 1000000u) * 1000u;
26 /*
27 * If a signal has non-default handler, nanosleep returns early.
28 * Our version of usleep doesn't return early
29 * if interrupted by such signals:
30 *
31 */
32 while (nanosleep(&ts, &ts) != 0)
33 continue;
34 return 0;
35}
36#endif
37
20#ifndef HAVE_VASPRINTF 38#ifndef HAVE_VASPRINTF
21int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p) 39int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
22{ 40{
diff --git a/libbb/rtc.c b/libbb/rtc.c
index 97455e86a..6d06d57f9 100644
--- a/libbb/rtc.c
+++ b/libbb/rtc.c
@@ -33,23 +33,55 @@ int FAST_FUNC rtc_adjtime_is_utc(void)
33 return utc; 33 return utc;
34} 34}
35 35
36/* rtc opens are exclusive.
37 * Try to run two "hwclock -w" at the same time to see it.
38 * Users wouldn't expect that to fail merely because /dev/rtc
39 * was momentarily busy, let's try a bit harder on errno == EBUSY.
40 */
41static int open_loop_on_busy(const char *name, int flags)
42{
43 int rtc;
44 /*
45 * Tested with two parallel "hwclock -w" loops.
46 * With try = 10, no failures with 2x1000000 loop iterations.
47 */
48 int try = 1000 / 20;
49 again:
50 errno = 0;
51 rtc = open(name, flags);
52 if (errno == EBUSY) {
53 usleep(20 * 1000);
54 if (--try != 0)
55 goto again;
56 /* EBUSY. Last try, exit on error instead of returning -1 */
57 return xopen(name, flags);
58 }
59 return rtc;
60}
61
62/* Never fails */
36int FAST_FUNC rtc_xopen(const char **default_rtc, int flags) 63int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
37{ 64{
38 int rtc; 65 int rtc;
66 const char *name =
67 "/dev/rtc""\0"
68 "/dev/rtc0""\0"
69 "/dev/misc/rtc""\0";
39 70
40 if (!*default_rtc) { 71 if (!*default_rtc)
41 *default_rtc = "/dev/rtc"; 72 goto try_name;
42 rtc = open(*default_rtc, flags); 73 name = ""; /*else: we have rtc name, don't try other names */
43 if (rtc >= 0) 74
44 return rtc; 75 for (;;) {
45 *default_rtc = "/dev/rtc0"; 76 rtc = open_loop_on_busy(*default_rtc, flags);
46 rtc = open(*default_rtc, flags);
47 if (rtc >= 0) 77 if (rtc >= 0)
48 return rtc; 78 return rtc;
49 *default_rtc = "/dev/misc/rtc"; 79 if (!name[0])
80 return xopen(*default_rtc, flags);
81 try_name:
82 *default_rtc = name;
83 name += strlen(name) + 1;
50 } 84 }
51
52 return xopen(*default_rtc, flags);
53} 85}
54 86
55void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd) 87void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd)
diff --git a/libbb/verror_msg.c b/libbb/verror_msg.c
index ee95be3e3..0ef2a311f 100644
--- a/libbb/verror_msg.c
+++ b/libbb/verror_msg.c
@@ -11,6 +11,9 @@
11# include <syslog.h> 11# include <syslog.h>
12#endif 12#endif
13 13
14#if ENABLE_FEATURE_SYSLOG
15smallint syslog_level = LOG_ERR;
16#endif
14smallint logmode = LOGMODE_STDIO; 17smallint logmode = LOGMODE_STDIO;
15const char *msg_eol = "\n"; 18const char *msg_eol = "\n";
16 19
@@ -70,7 +73,7 @@ void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
70 } 73 }
71#if ENABLE_FEATURE_SYSLOG 74#if ENABLE_FEATURE_SYSLOG
72 if (logmode & LOGMODE_SYSLOG) { 75 if (logmode & LOGMODE_SYSLOG) {
73 syslog(LOG_ERR, "%s", msg + applet_len); 76 syslog(syslog_level, "%s", msg + applet_len);
74 } 77 }
75#endif 78#endif
76 free(msg); 79 free(msg);
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c
index ea09f20b8..2c5a9ef39 100644
--- a/libbb/xreadlink.c
+++ b/libbb/xreadlink.c
@@ -1,14 +1,14 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * xreadlink.c - safe implementation of readlink. 3 * xreadlink.c - safe implementation of readlink.
4 * Returns a NULL on failure... 4 * Returns a NULL on failure.
5 * 5 *
6 * Licensed under GPLv2, see file LICENSE in this source tree. 6 * Licensed under GPLv2, see file LICENSE in this source tree.
7 */ 7 */
8 8
9#include "libbb.h" 9#include "libbb.h"
10 10
11/* some systems (eg Hurd) does not have MAXSYMLINKS definition, 11/* Some systems (eg Hurd) do not have MAXSYMLINKS definition,
12 * set it to some reasonable value if it isn't defined */ 12 * set it to some reasonable value if it isn't defined */
13#ifndef MAXSYMLINKS 13#ifndef MAXSYMLINKS
14# define MAXSYMLINKS 20 14# define MAXSYMLINKS 20
@@ -108,8 +108,11 @@ char* FAST_FUNC xmalloc_readlink_or_warn(const char *path)
108 108
109char* FAST_FUNC xmalloc_realpath(const char *path) 109char* FAST_FUNC xmalloc_realpath(const char *path)
110{ 110{
111#if defined(__GLIBC__) || \ 111/* NB: uclibc also defines __GLIBC__
112 (defined(__UCLIBC__) && UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31)) 112 * Therefore the test "if glibc, or uclibc >= 0.9.31" looks a bit weird:
113 */
114#if defined(__GLIBC__) && \
115 (!defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31))
113 /* glibc provides a non-standard extension */ 116 /* glibc provides a non-standard extension */
114 /* new: POSIX.1-2008 specifies this behavior as well */ 117 /* new: POSIX.1-2008 specifies this behavior as well */
115 return realpath(path, NULL); 118 return realpath(path, NULL);