Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'busybox' into merge
  • Loading branch information
rmyorston committed Sep 10, 2018
2 parents f72845d + 05b1806 commit d89ced7
Show file tree
Hide file tree
Showing 60 changed files with 978 additions and 496 deletions.
2 changes: 1 addition & 1 deletion applets/install.sh
Expand Up @@ -83,7 +83,7 @@ install -m 755 busybox "$prefix/bin/busybox" || exit 1
for i in $h; do
appdir=`dirname "$i"`
app=`basename "$i"`
if [ x"$noclobber" = x"1" ] && [ -e "$prefix/$i" ]; then
if [ x"$noclobber" = x"1" ] && ([ -e "$prefix/$i" ] || [ -h "$prefix/$i" ]); then
echo " $prefix/$i already exists"
continue
fi
Expand Down
80 changes: 42 additions & 38 deletions archival/tar.c
Expand Up @@ -601,56 +601,65 @@ static int FAST_FUNC writeFileToTarball(const char *fileName, struct stat *statb
/* Don't inline: vfork scares gcc and pessimizes code */
static void NOINLINE vfork_compressor(int tar_fd, const char *gzip)
{
pid_t gzipPid;

// On Linux, vfork never unpauses parent early, although standard
// allows for that. Do we want to waste bytes checking for it?
# define WAIT_FOR_CHILD 0
volatile int vfork_exec_errno = 0;
struct fd_pair gzipDataPipe;
struct fd_pair data;
# if WAIT_FOR_CHILD
struct fd_pair gzipStatusPipe;
xpiped_pair(gzipStatusPipe);
struct fd_pair status;
xpiped_pair(status);
# endif
xpiped_pair(gzipDataPipe);
xpiped_pair(data);

signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */

gzipPid = xvfork();

if (gzipPid == 0) {
if (xvfork() == 0) {
/* child */
int tfd;
/* NB: close _first_, then move fds! */
close(gzipDataPipe.wr);
close(data.wr);
# if WAIT_FOR_CHILD
close(gzipStatusPipe.rd);
/* gzipStatusPipe.wr will close only on exec -
close(status.rd);
/* status.wr will close only on exec -
* parent waits for this close to happen */
fcntl(gzipStatusPipe.wr, F_SETFD, FD_CLOEXEC);
fcntl(status.wr, F_SETFD, FD_CLOEXEC);
# endif
xmove_fd(gzipDataPipe.rd, 0);
xmove_fd(tar_fd, 1);
/* copy it: parent's tar_fd variable must not change */
tfd = tar_fd;
if (tfd == 0) {
/* Output tar fd may be zero.
* xmove_fd(data.rd, 0) would destroy it.
* Reproducer:
* exec 0>&-
* exec 1>&-
* tar czf Z.tar.gz FILE
* Swapping move_fd's order wouldn't work:
* data.rd is 1 and _it_ would be destroyed.
*/
tfd = dup(tfd);
}
xmove_fd(data.rd, 0);
xmove_fd(tfd, 1);
/* exec gzip/bzip2 program/applet */
BB_EXECLP(gzip, gzip, "-f", (char *)0);
vfork_exec_errno = errno;
_exit(EXIT_FAILURE);
}

/* parent */
xmove_fd(gzipDataPipe.wr, tar_fd);
close(gzipDataPipe.rd);
xmove_fd(data.wr, tar_fd);
close(data.rd);
# if WAIT_FOR_CHILD
close(gzipStatusPipe.wr);
close(status.wr);
while (1) {
char buf;
int n;

/* Wait until child execs (or fails to) */
n = full_read(gzipStatusPipe.rd, &buf, 1);
char buf;
int n = full_read(status.rd, &buf, 1);
if (n < 0 /* && errno == EAGAIN */)
continue; /* try it again */
}
close(gzipStatusPipe.rd);
close(status.rd);
# endif
if (vfork_exec_errno) {
errno = vfork_exec_errno;
Expand Down Expand Up @@ -754,11 +763,7 @@ static NOINLINE int writeTarFile(
return errorFlag;
}

#else /* !FEATURE_TAR_CREATE */

# define writeTarFile(...) 0

#endif
#endif /* FEATURE_TAR_CREATE */

#if ENABLE_FEATURE_TAR_FROM
static llist_t *append_file_list_to_list(llist_t *list)
Expand Down Expand Up @@ -1190,17 +1195,16 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
if (LONE_DASH(tar_filename)) {
tar_handle->src_fd = tar_fd;
tar_handle->seek = seek_by_read;
} else
if (ENABLE_FEATURE_TAR_AUTODETECT
&& flags == O_RDONLY
&& !(opt & OPT_ANY_COMPRESS)
) {
tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
if (tar_handle->src_fd < 0)
bb_perror_msg_and_die("can't open '%s'", tar_filename);
} else {
if (ENABLE_FEATURE_TAR_AUTODETECT
&& flags == O_RDONLY
&& !(opt & OPT_ANY_COMPRESS)
) {
tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
if (tar_handle->src_fd < 0)
bb_perror_msg_and_die("can't open '%s'", tar_filename);
} else {
tar_handle->src_fd = xopen(tar_filename, flags);
}
tar_handle->src_fd = xopen(tar_filename, flags);
}
}

Expand Down
2 changes: 1 addition & 1 deletion coreutils/cp.c
Expand Up @@ -114,7 +114,7 @@ int cp_main(int argc, char **argv)
# endif
);
# if ENABLE_FEATURE_CP_REFLINK
BUILD_BUG_ON(OPT_reflink != FILEUTILS_REFLINK);
BUILD_BUG_ON((int)OPT_reflink != (int)FILEUTILS_REFLINK);
if (flags & FILEUTILS_REFLINK) {
if (!reflink)
flags |= FILEUTILS_REFLINK_ALWAYS;
Expand Down
10 changes: 9 additions & 1 deletion coreutils/ls.c
Expand Up @@ -1018,7 +1018,15 @@ static void scan_and_display_dirs_recur(struct dnode **dn, int first)
subdnp = scan_one_dir((*dn)->fullname, &nfiles);
#if ENABLE_DESKTOP
if (option_mask32 & (OPT_s|OPT_l)) {
printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp));
if (option_mask32 & OPT_h) {
printf("total %-"HUMAN_READABLE_MAX_WIDTH_STR"s\n",
/* print size, no fractions, use suffixes */
make_human_readable_str(calculate_blocks(subdnp) * 1024,
0, 0)
);
} else {
printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp));
}
}
#endif
if (nfiles > 0) {
Expand Down
78 changes: 5 additions & 73 deletions coreutils/sleep.c
Expand Up @@ -32,13 +32,6 @@
//config: depends on SLEEP
//config: help
//config: Allow sleep to pause for specified minutes, hours, and days.
//config:
//config:config FEATURE_FLOAT_SLEEP
//config: bool "Enable fractional arguments"
//config: default y
//config: depends on FEATURE_FANCY_SLEEP
//config: help
//config: Allow for fractional numeric parameters.

/* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */
//applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP))
Expand Down Expand Up @@ -66,89 +59,28 @@

#include "libbb.h"

#if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP
static const struct suffix_mult sfx[] = {
{ "s", 1 },
{ "m", 60 },
{ "h", 60*60 },
{ "d", 24*60*60 },
{ "", 0 }
};
#endif

int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int sleep_main(int argc UNUSED_PARAM, char **argv)
{
#if ENABLE_FEATURE_FLOAT_SLEEP
double duration;
struct timespec ts;
#else
unsigned duration;
#endif
duration_t duration;

++argv;
if (!*argv)
bb_show_usage();

#if ENABLE_FEATURE_FLOAT_SLEEP

# if ENABLE_LOCALE_SUPPORT
#if ENABLE_FEATURE_FANCY_SLEEP
# if ENABLE_FLOAT_DURATION
/* undo busybox.c setlocale */
setlocale(LC_NUMERIC, "C");
# endif
duration = 0;
do {
char *arg = *argv;
if (strchr(arg, '.')) {
double d;
char *pp;
int len = strspn(arg, "0123456789.");
char sv = arg[len];
arg[len] = '\0';
errno = 0;
d = strtod(arg, &pp);
if (errno || *pp)
bb_show_usage();
arg += len;
*arg-- = sv;
sv = *arg;
*arg = '1';
duration += d * xatoul_sfx(arg, sfx);
*arg = sv;
} else {
duration += xatoul_sfx(arg, sfx);
}
duration += parse_duration_str(*argv);
} while (*++argv);

ts.tv_sec = MAXINT(typeof(ts.tv_sec));
ts.tv_nsec = 0;
if (duration >= 0 && duration < ts.tv_sec) {
ts.tv_sec = duration;
ts.tv_nsec = (duration - ts.tv_sec) * 1000000000;
}
do {
errno = 0;
nanosleep(&ts, &ts);
} while (errno == EINTR);

#elif ENABLE_FEATURE_FANCY_SLEEP

duration = 0;
do {
duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx);
} while (*++argv);
sleep(duration);

sleep_for_duration(duration);
#else /* simple */

duration = xatou(*argv);
sleep(duration);
// Off. If it's really needed, provide example why
//if (sleep(duration)) {
// bb_perror_nomsg_and_die();
//}

#endif

return EXIT_SUCCESS;
}
30 changes: 16 additions & 14 deletions coreutils/timeout.c
Expand Up @@ -39,10 +39,10 @@
//kbuild:lib-$(CONFIG_TIMEOUT) += timeout.o

//usage:#define timeout_trivial_usage
//usage: "[-t SECS] [-s SIG] PROG ARGS"
//usage: "[-s SIG] SECS PROG ARGS"
//usage:#define timeout_full_usage "\n\n"
//usage: "Runs PROG. Sends SIG to it if it is not gone in SECS seconds.\n"
//usage: "Defaults: SECS: 10, SIG: TERM."
//usage: "Default SIG: TERM."

#include "libbb.h"

Expand All @@ -68,7 +68,7 @@ int timeout_main(int argc UNUSED_PARAM, char **argv)
DWORD status = EXIT_SUCCESS;
#endif
int parent = 0;
int timeout = 10;
int timeout;
pid_t pid;
#if !BB_MMU
char *sv1, *sv2;
Expand All @@ -83,25 +83,34 @@ int timeout_main(int argc UNUSED_PARAM, char **argv)

/* -t SECONDS; -p PARENT_PID */
/* '+': stop at first non-option */
getopt32(argv, "+s:t:+" USE_FOR_NOMMU("p:+"), &opt_s, &timeout, &parent);
getopt32(argv, "+s:" USE_FOR_NOMMU("p:+"), &opt_s, &parent);
/*argv += optind; - no, wait for bb_daemonize_or_rexec! */

signo = get_signum(opt_s);
#if !ENABLE_PLATFORM_MINGW32
if (signo < 0)
#else
if (signo != SIGTERM && signo != SIGKILL && signo != 0)
#endif
bb_error_msg_and_die("unknown signal '%s'", opt_s);

if (!argv[optind])
bb_show_usage();
timeout = parse_duration_str(argv[optind++]);
if (!argv[optind]) /* no PROG? */
bb_show_usage();

#if !ENABLE_PLATFORM_MINGW32
/* We want to create a grandchild which will watch
* and kill the grandparent. Other methods:
* making parent watch child disrupts parent<->child link
* (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
* it's better if service_prog is a child of tcpsvd!),
* making child watch parent results in programs having
* unexpected children. */
* unexpected children. */

if (parent) /* we were re-execed, already grandchild */
goto grandchild;
if (!argv[optind]) /* no PROG? */
bb_show_usage();

#if !BB_MMU
sv1 = argv[optind];
Expand Down Expand Up @@ -146,13 +155,6 @@ int timeout_main(int argc UNUSED_PARAM, char **argv)
#endif
BB_EXECVP_or_die(argv);
#else /* ENABLE_PLATFORM_MINGW32 */
if (signo != SIGTERM && signo != SIGKILL && signo != 0)
bb_error_msg_and_die("unknown signal '%s'", opt_s);

argv += optind;
if (argv[0] == NULL)
bb_show_usage();

if ((ret=mingw_spawn_proc((const char **)argv)) == -1) {
xfunc_error_retval = errno == EACCES ? 126 : 127;
bb_perror_msg_and_die("can't execute '%s'", argv[0]);
Expand Down
4 changes: 3 additions & 1 deletion examples/var_service/ntpd/ntp.script
@@ -1,5 +1,4 @@
#!/bin/sh

# Note that there is no provision to prevent several copies of the script
# to be run in quick succession. In fact, it happens rather often
# if initial syncronization results in a step.
Expand All @@ -8,6 +7,9 @@
#
# Script should be ready to deal with this.

# For other parts of the system which prefer to run only on the stable clock
echo "$1" >rundir/sync_status

dt=`date '+%Y-%m-%d %H:%M:%S'`

echo "`tail -n 199 -- "$0.log" 2>/dev/null`" >"$0.log.$$"
Expand Down
8 changes: 8 additions & 0 deletions include/libbb.h
Expand Up @@ -1062,6 +1062,14 @@ int xatoi_positive(const char *numstr) FAST_FUNC;
/* Useful for reading port numbers */
uint16_t xatou16(const char *numstr) FAST_FUNC;

#if ENABLE_FLOAT_DURATION
typedef double duration_t;
void sleep_for_duration(duration_t duration) FAST_FUNC;
#else
typedef unsigned duration_t;
#define sleep_for_duration(duration) sleep(duration)
#endif
duration_t parse_duration_str(char *str) FAST_FUNC;

/* These parse entries in /etc/passwd and /etc/group. This is desirable
* for BusyBox since we want to avoid using the glibc NSS stuff, which
Expand Down
3 changes: 2 additions & 1 deletion libbb/copy_file.c
Expand Up @@ -391,14 +391,15 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
char *lpath = xmalloc_readlink_or_warn(source);
if (lpath) {
int r = symlink(lpath, dest);
free(lpath);
if (r < 0) {
/* shared message */
bb_perror_msg("can't create %slink '%s' to '%s'",
"sym", dest, lpath
);
free(lpath);
return -1;
}
free(lpath);
if (flags & FILEUTILS_PRESERVE_STATUS)
if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
Expand Down

0 comments on commit d89ced7

Please sign in to comment.