diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | applets/applets.c | 7 | ||||
-rw-r--r-- | archival/bbunzip.c | 2 | ||||
-rw-r--r-- | archival/libunarchive/Kbuild | 60 | ||||
-rw-r--r-- | archival/libunarchive/get_header_tar_bz2.c | 2 | ||||
-rw-r--r-- | archival/libunarchive/get_header_tar_gz.c | 9 | ||||
-rw-r--r-- | archival/libunarchive/get_header_tar_lzma.c | 2 | ||||
-rw-r--r-- | archival/libunarchive/open_transformer.c | 31 | ||||
-rw-r--r-- | archival/rpm.c | 26 | ||||
-rw-r--r-- | archival/tar.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 9 | ||||
-rw-r--r-- | include/unarchive.h | 6 | ||||
-rw-r--r-- | libbb/copy_file.c | 191 | ||||
-rw-r--r-- | libbb/xreadlink.c | 22 | ||||
-rw-r--r-- | networking/isrv.c | 18 | ||||
-rw-r--r-- | runit/runsv.c | 36 | ||||
-rwxr-xr-x | scripts/trylink | 58 |
17 files changed, 283 insertions, 200 deletions
@@ -1,7 +1,7 @@ VERSION = 1 PATCHLEVEL = 7 SUBLEVEL = 1 -EXTRAVERSION = .svn +EXTRAVERSION = NAME = Unnamed # *DOCUMENTATION* diff --git a/applets/applets.c b/applets/applets.c index 6de6db3..b40f018 100644 --- a/applets/applets.c +++ b/applets/applets.c @@ -546,7 +546,7 @@ static int busybox_main(char **argv) help: output_width = 80; if (ENABLE_FEATURE_AUTOWIDTH) { - /* Obtain the terminal width. */ + /* Obtain the terminal width */ get_terminal_width_height(0, &output_width, NULL); } /* leading tab and room to wrap */ @@ -580,12 +580,11 @@ static int busybox_main(char **argv) if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) { const char *busybox; - busybox = xmalloc_readlink_or_warn(bb_busybox_exec_path); + busybox = xmalloc_readlink(bb_busybox_exec_path); if (!busybox) busybox = bb_busybox_exec_path; /* -s makes symlinks */ - install_links(busybox, - argv[2] && strcmp(argv[2], "-s") == 0); + install_links(busybox, argv[2] && strcmp(argv[2], "-s") == 0); return 0; } diff --git a/archival/bbunzip.c b/archival/bbunzip.c index 56c742a..a4e525f 100644 --- a/archival/bbunzip.c +++ b/archival/bbunzip.c @@ -298,7 +298,7 @@ USE_DESKTOP(long long) int unpack_unlzma(void) int unlzma_main(int argc, char **argv); int unlzma_main(int argc, char **argv) { - getopt32(argv, "c"); + getopt32(argv, "cf"); argv += optind; /* lzmacat? */ if (applet_name[4] == 'c') diff --git a/archival/libunarchive/Kbuild b/archival/libunarchive/Kbuild index 3c29d54..d104524 100644 --- a/archival/libunarchive/Kbuild +++ b/archival/libunarchive/Kbuild @@ -35,36 +35,34 @@ DPKG_FILES:= \ get_header_tar.o \ filter_accept_list_reassign.o -# open_transformer uses fork(). Compile it only if absolutely necessary lib-$(CONFIG_RPM) += open_transformer.o -lib-$(CONFIG_FEATURE_TAR_BZIP2) += open_transformer.o -lib-$(CONFIG_FEATURE_TAR_LZMA) += open_transformer.o -lib-$(CONFIG_FEATURE_TAR_GZIP) += open_transformer.o -lib-$(CONFIG_FEATURE_TAR_COMPRESS) += open_transformer.o -lib-$(CONFIG_FEATURE_DEB_TAR_GZ) += open_transformer.o -lib-$(CONFIG_FEATURE_DEB_TAR_BZ2) += open_transformer.o -lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += open_transformer.o - -lib-$(CONFIG_AR) += get_header_ar.o unpack_ar_archive.o -lib-$(CONFIG_BUNZIP2) += decompress_bunzip2.o -lib-$(CONFIG_UNLZMA) += decompress_unlzma.o -lib-$(CONFIG_CPIO) += get_header_cpio.o -lib-$(CONFIG_DPKG) += $(DPKG_FILES) -lib-$(CONFIG_DPKG_DEB) += $(DPKG_FILES) -lib-$(CONFIG_FEATURE_DEB_TAR_GZ) += $(GUNZIP_FILES) get_header_tar_gz.o -lib-$(CONFIG_FEATURE_DEB_TAR_BZ2) += decompress_bunzip2.o get_header_tar_bz2.o -lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += decompress_unlzma.o get_header_tar_lzma.o -lib-$(CONFIG_GUNZIP) += $(GUNZIP_FILES) -lib-$(CONFIG_FEATURE_GUNZIP_UNCOMPRESS) += decompress_uncompress.o -lib-$(CONFIG_RPM2CPIO) += $(GUNZIP_FILES) get_header_cpio.o -lib-$(CONFIG_RPM) += $(GUNZIP_FILES) get_header_cpio.o -lib-$(CONFIG_FEATURE_RPM_BZ2) += decompress_bunzip2.o -lib-$(CONFIG_TAR) += get_header_tar.o -lib-$(CONFIG_FEATURE_TAR_BZIP2) += decompress_bunzip2.o get_header_tar_bz2.o -lib-$(CONFIG_FEATURE_TAR_LZMA) += decompress_unlzma.o get_header_tar_lzma.o -lib-$(CONFIG_FEATURE_TAR_GZIP) += $(GUNZIP_FILES) get_header_tar_gz.o -lib-$(CONFIG_FEATURE_TAR_COMPRESS) += decompress_uncompress.o -lib-$(CONFIG_UNCOMPRESS) += decompress_uncompress.o -lib-$(CONFIG_UNZIP) += $(GUNZIP_FILES) -lib-$(CONFIG_FEATURE_COMPRESS_USAGE) += decompress_bunzip2.o +lib-$(CONFIG_FEATURE_TAR_BZIP2) += open_transformer.o +lib-$(CONFIG_FEATURE_TAR_LZMA) += open_transformer.o +lib-$(CONFIG_FEATURE_TAR_GZIP) += open_transformer.o +lib-$(CONFIG_FEATURE_TAR_COMPRESS) += open_transformer.o +lib-$(CONFIG_FEATURE_DEB_TAR_GZ) += open_transformer.o +lib-$(CONFIG_FEATURE_DEB_TAR_BZ2) += open_transformer.o +lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += open_transformer.o +lib-$(CONFIG_AR) += get_header_ar.o unpack_ar_archive.o +lib-$(CONFIG_BUNZIP2) += decompress_bunzip2.o +lib-$(CONFIG_UNLZMA) += decompress_unlzma.o +lib-$(CONFIG_CPIO) += get_header_cpio.o +lib-$(CONFIG_DPKG) += $(DPKG_FILES) +lib-$(CONFIG_DPKG_DEB) += $(DPKG_FILES) +lib-$(CONFIG_FEATURE_DEB_TAR_GZ) += $(GUNZIP_FILES) get_header_tar_gz.o +lib-$(CONFIG_FEATURE_DEB_TAR_BZ2) += decompress_bunzip2.o get_header_tar_bz2.o +lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += decompress_unlzma.o get_header_tar_lzma.o +lib-$(CONFIG_GUNZIP) += $(GUNZIP_FILES) +lib-$(CONFIG_FEATURE_GUNZIP_UNCOMPRESS) += decompress_uncompress.o +lib-$(CONFIG_RPM2CPIO) += $(GUNZIP_FILES) get_header_cpio.o +lib-$(CONFIG_RPM) += $(GUNZIP_FILES) get_header_cpio.o +lib-$(CONFIG_FEATURE_RPM_BZ2) += decompress_bunzip2.o +lib-$(CONFIG_TAR) += get_header_tar.o +lib-$(CONFIG_FEATURE_TAR_BZIP2) += decompress_bunzip2.o get_header_tar_bz2.o +lib-$(CONFIG_FEATURE_TAR_LZMA) += decompress_unlzma.o get_header_tar_lzma.o +lib-$(CONFIG_FEATURE_TAR_GZIP) += $(GUNZIP_FILES) get_header_tar_gz.o +lib-$(CONFIG_FEATURE_TAR_COMPRESS) += decompress_uncompress.o +lib-$(CONFIG_UNCOMPRESS) += decompress_uncompress.o +lib-$(CONFIG_UNZIP) += $(GUNZIP_FILES) +lib-$(CONFIG_FEATURE_COMPRESS_USAGE) += decompress_bunzip2.o diff --git a/archival/libunarchive/get_header_tar_bz2.c b/archival/libunarchive/get_header_tar_bz2.c index e11f44c..8f7f83f 100644 --- a/archival/libunarchive/get_header_tar_bz2.c +++ b/archival/libunarchive/get_header_tar_bz2.c @@ -11,7 +11,7 @@ char get_header_tar_bz2(archive_handle_t *archive_handle) /* Can't lseek over pipes */ archive_handle->seek = seek_by_read; - archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_bz2_stream); + archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_bz2_stream, "bunzip2", "bunzip2", "-cf", "-", NULL); archive_handle->offset = 0; while (get_header_tar(archive_handle) == EXIT_SUCCESS) /**/; diff --git a/archival/libunarchive/get_header_tar_gz.c b/archival/libunarchive/get_header_tar_gz.c index 85070d9..a990614 100644 --- a/archival/libunarchive/get_header_tar_gz.c +++ b/archival/libunarchive/get_header_tar_gz.c @@ -8,19 +8,26 @@ char get_header_tar_gz(archive_handle_t *archive_handle) { +#if BB_MMU unsigned char magic[2]; +#endif /* Can't lseek over pipes */ archive_handle->seek = seek_by_read; + /* Check gzip magic only if open_transformer will invoke unpack_gz_stream (MMU case). + * Otherwise, it will invoke an external helper "gunzip -cf" (NOMMU case) which will + * need the header. */ +#if BB_MMU xread(archive_handle->src_fd, &magic, 2); if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { bb_error_msg_and_die("invalid gzip magic"); } check_header_gzip_or_die(archive_handle->src_fd); +#endif - archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_gz_stream); + archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip", "gunzip", "-cf", "-", NULL); archive_handle->offset = 0; while (get_header_tar(archive_handle) == EXIT_SUCCESS) /**/; diff --git a/archival/libunarchive/get_header_tar_lzma.c b/archival/libunarchive/get_header_tar_lzma.c index 771f664..f0e45f2 100644 --- a/archival/libunarchive/get_header_tar_lzma.c +++ b/archival/libunarchive/get_header_tar_lzma.c @@ -14,7 +14,7 @@ char get_header_tar_lzma(archive_handle_t * archive_handle) /* Can't lseek over pipes */ archive_handle->seek = seek_by_read; - archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_lzma_stream); + archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_lzma_stream, "unlzma", "unlzma", "-cf", "-", NULL); archive_handle->offset = 0; while (get_header_tar(archive_handle) == EXIT_SUCCESS) /**/; diff --git a/archival/libunarchive/open_transformer.c b/archival/libunarchive/open_transformer.c index 0ee0806..f8f587e 100644 --- a/archival/libunarchive/open_transformer.c +++ b/archival/libunarchive/open_transformer.c @@ -7,27 +7,48 @@ #include "unarchive.h" /* transformer(), more than meets the eye */ +/* + * On MMU machine, the transform_prog and ... are stripped + * by a macro in include/unarchive.h. On NOMMU, transformer is stripped. + */ int open_transformer(int src_fd, - USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd)) + USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd), + const char *transform_prog, ...) { int fd_pipe[2]; int pid; xpipe(fd_pipe); +#if BB_MMU pid = fork(); - if (pid == -1) { +#else + pid = vfork(); +#endif + if (pid == -1) bb_perror_msg_and_die("fork failed"); - } if (pid == 0) { +#if !BB_MMU + va_list ap; +#endif /* child process */ close(fd_pipe[0]); /* We don't wan't to read from the parent */ // FIXME: error check? +#if BB_MMU transformer(src_fd, fd_pipe[1]); - close(fd_pipe[1]); /* Send EOF */ - close(src_fd); + if (ENABLE_FEATURE_CLEAN_UP) { + close(fd_pipe[1]); /* Send EOF */ + close(src_fd); + } exit(0); +#else + xmove_fd(src_fd, 0); + xmove_fd(fd_pipe[1], 1); + va_start(ap, transform_prog); + BB_EXECVP(transform_prog, ap); + bb_perror_and_die("exec failed"); +#endif /* notreached */ } diff --git a/archival/rpm.c b/archival/rpm.c index 674ee26..4d723b7 100644 --- a/archival/rpm.c +++ b/archival/rpm.c @@ -187,9 +187,15 @@ int rpm_main(int argc, char **argv) static void extract_cpio_gz(int fd) { - USE_DESKTOP(long long) int (*xformer)(int src_fd, int dst_fd); archive_handle_t *archive_handle; unsigned char magic[2]; +#if BB_MMU + USE_DESKTOP(long long) int (*xformer)(int src_fd, int dst_fd); + enum { xformer_prog = 0 }; +#else + enum { xformer = 0 }; + const char *xformer_prog; +#endif /* Initialize */ archive_handle = init_handle(); @@ -202,11 +208,19 @@ static void extract_cpio_gz(int fd) archive_handle->offset = 0; xread(archive_handle->src_fd, &magic, 2); +#if BB_MMU xformer = unpack_gz_stream; +#else + xformer_prog = "gunzip"; +#endif if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { if (ENABLE_FEATURE_RPM_BZ2 && (magic[0] == 0x42) && (magic[1] == 0x5a)) { +#if BB_MMU xformer = unpack_bz2_stream; +#else + xformer_prog = "bunzip2"; +#endif /* We can do better, need modifying unpack_bz2_stream to not require * first 2 bytes. Not very hard to do... I mean, TODO :) */ xlseek(archive_handle->src_fd, -2, SEEK_CUR); @@ -214,11 +228,17 @@ static void extract_cpio_gz(int fd) bb_error_msg_and_die("no gzip" USE_FEATURE_RPM_BZ2("/bzip") " magic"); - } else + } else { check_header_gzip_or_die(archive_handle->src_fd); +#if !BB_MMU + /* NOMMU version of open_transformer execs an external unzipper that should + * have the file position at the start of the file */ + xlseek(archive_handle->src_fd, 0, SEEK_SET); +#endif + } xchdir("/"); /* Install RPM's to root */ - archive_handle->src_fd = open_transformer(archive_handle->src_fd, xformer); + archive_handle->src_fd = open_transformer(archive_handle->src_fd, xformer, xformer_prog, xformer_prog, "-cf", "-", NULL); archive_handle->offset = 0; while (get_header_cpio(archive_handle) == EXIT_SUCCESS) continue; diff --git a/archival/tar.c b/archival/tar.c index 9bf9058..7235616 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -648,7 +648,7 @@ static char get_header_tar_Z(archive_handle_t *archive_handle) bb_error_msg_and_die("invalid magic"); } - archive_handle->src_fd = open_transformer(archive_handle->src_fd, uncompress); + archive_handle->src_fd = open_transformer(archive_handle->src_fd, uncompress, "uncompress", "uncompress", "-cf", "-", NULL); archive_handle->offset = 0; while (get_header_tar(archive_handle) == EXIT_SUCCESS) /* nothing */; diff --git a/include/libbb.h b/include/libbb.h index e514fe2..140e21d 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -248,9 +248,10 @@ void xmove_fd(int, int); DIR *xopendir(const char *path); DIR *warn_opendir(const char *path); -char *xrealloc_getcwd_or_warn(char *cwd); +/* UNUSED: char *xmalloc_realpath(const char *path); */ +char *xmalloc_readlink(const char *path); char *xmalloc_readlink_or_warn(const char *path); -char *xmalloc_realpath(const char *path); +char *xrealloc_getcwd_or_warn(char *cwd); //TODO: signal(sid, f) is the same? then why? @@ -316,8 +317,8 @@ enum { }; /* Create stream socket, and allocate suitable lsa. * (lsa of correct size and lsa->sa.sa_family (AF_INET/AF_INET6)) - * af == AF_UNSPEC will result in trying to create IPv6, and - * if kernel doesn't support it, IPv4. + * af == AF_UNSPEC will result in trying to create IPv6 socket, + * and if kernel doesn't support it, IPv4. */ int xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int af,) int sock_type); int xsocket_stream(len_and_sockaddr **lsap); diff --git a/include/unarchive.h b/include/unarchive.h index bea0558..51ec89c 100644 --- a/include/unarchive.h +++ b/include/unarchive.h @@ -115,7 +115,13 @@ extern USE_DESKTOP(long long) int inflate_unzip(inflate_unzip_result *res, unsig extern USE_DESKTOP(long long) int unpack_gz_stream(int src_fd, int dst_fd); extern USE_DESKTOP(long long) int unpack_lzma_stream(int src_fd, int dst_fd); +#if BB_MMU extern int open_transformer(int src_fd, USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd)); +#define open_transformer(src_fd, transformer, transform_prog, ...) open_transformer(src_fd, transformer) +#else +extern int open_transformer(int src_fd, const char *transform_prog, ...); +#define open_transformer(src_fd, transformer, transform_prog, ...) open_transformer(src_fd, transform_prog, __VA_ARGS__) +#endif #endif diff --git a/libbb/copy_file.c b/libbb/copy_file.c index b68a257..3da8a35 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -19,28 +19,31 @@ // (or fail, if it points to dir/nonexistent location/etc). // This is strange, but POSIX-correct. // coreutils cp has --remove-destination to override this... +// +// NB: we have special code which still allows for "cp file /dev/node" +// to work POSIX-ly (the only realistic case where it makes sense) #define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */ - +// errno must be set to relevant value ("why we cannot create dest?") +// for POSIX mode to give reasonable error message static int ask_and_unlink(const char *dest, int flags) { - // If !DO_POSIX_CP, act as if -f is always in effect - we don't want - // "'file' exists" msg, we want unlink to be done (silently unless -i - // is also in effect). - // This prevents safe way from asking more questions than POSIX does. #if DO_POSIX_CP if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) { - fprintf(stderr, "'%s' exists\n", dest); + // Either it exists, or the *path* doesnt exist + bb_perror_msg("cannot create '%s'", dest); return -1; } #endif + // If !DO_POSIX_CP, act as if -f is always in effect - we don't want + // "cannot create" msg, we want unlink to be done (silently unless -i). // TODO: maybe we should do it only if ctty is present? if (flags & FILEUTILS_INTERACTIVE) { // We would not do POSIX insanity. -i asks, // then _unlinks_ the offender. Presto. - // (No opening without O_EXCL, no unlinks only if -f) + // (No "opening without O_EXCL", no "unlink only if -f") // Or else we will end up having 3 open()s! fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest); if (!bb_ask_confirmation()) @@ -60,9 +63,11 @@ static int ask_and_unlink(const char *dest, int flags) */ int copy_file(const char *source, const char *dest, int flags) { + /* This is a recursive function, try to minimize stack usage */ + /* NB: each struct stat is ~100 bytes */ struct stat source_stat; struct stat dest_stat; - int status = 0; + signed char retval = 0; signed char dest_exists = 0; signed char ovr; @@ -112,6 +117,7 @@ int copy_file(const char *source, const char *dest, int flags) if (S_ISDIR(source_stat.st_mode)) { DIR *dp; + const char *tp; struct dirent *d; mode_t saved_umask = 0; @@ -120,12 +126,23 @@ int copy_file(const char *source, const char *dest, int flags) return -1; } - /* Create DEST. */ + /* Did we ever create source ourself before? */ + tp = is_in_ino_dev_hashtable(&source_stat); + if (tp) { + /* We did! it's a recursion! man the lifeboats... */ + bb_error_msg("recursion detected, omitting directory '%s'", + source); + return -1; + } + + /* Create DEST */ if (dest_exists) { if (!S_ISDIR(dest_stat.st_mode)) { bb_error_msg("target '%s' is not a directory", dest); return -1; } + /* race here: user can substitute a symlink between + * this check and actual creation of files inside dest */ } else { mode_t mode; saved_umask = umask(0); @@ -133,22 +150,29 @@ int copy_file(const char *source, const char *dest, int flags) mode = source_stat.st_mode; if (!(flags & FILEUTILS_PRESERVE_STATUS)) mode = source_stat.st_mode & ~saved_umask; + /* Allow owner to access new dir (at least for now) */ mode |= S_IRWXU; - if (mkdir(dest, mode) < 0) { umask(saved_umask); bb_perror_msg("cannot create directory '%s'", dest); return -1; } - umask(saved_umask); + /* need stat info for add_to_ino_dev_hashtable */ + if (lstat(dest, &dest_stat) < 0) { + bb_perror_msg("cannot stat '%s'", dest); + return -1; + } } + /* remember (dev,inode) of each created dir. + * NULL: name is not remembered */ + add_to_ino_dev_hashtable(&dest_stat, NULL); - /* Recursively copy files in SOURCE. */ + /* Recursively copy files in SOURCE */ dp = opendir(source); if (dp == NULL) { - status = -1; - goto preserve_status; + retval = -1; + goto preserve_mode_ugid_time; } while ((d = readdir(dp)) != NULL) { @@ -159,7 +183,7 @@ int copy_file(const char *source, const char *dest, int flags) continue; new_dest = concat_path_file(dest, d->d_name); if (copy_file(new_source, new_dest, flags) < 0) - status = -1; + retval = -1; free(new_source); free(new_dest); } @@ -168,11 +192,13 @@ int copy_file(const char *source, const char *dest, int flags) if (!dest_exists && chmod(dest, source_stat.st_mode & ~saved_umask) < 0 ) { - bb_perror_msg("cannot change permissions of '%s'", dest); - status = -1; + bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest); + /* retval = -1; - WRONG! copy *WAS* made */ } + goto preserve_mode_ugid_time; + } - } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) { + if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) { int (*lf)(const char *oldpath, const char *newpath); make_links: // Hmm... maybe @@ -188,47 +214,52 @@ int copy_file(const char *source, const char *dest, int flags) return -1; } } + /* _Not_ jumping to preserve_mode_ugid_time: + * hard/softlinks don't have those */ return 0; + } - } else if (S_ISREG(source_stat.st_mode) - /* Huh? DEREF uses stat, which never returns links! */ + if (S_ISREG(source_stat.st_mode) + /* DEREF uses stat, which never returns S_ISLNK() == true. */ /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */ ) { int src_fd; int dst_fd; - if (ENABLE_FEATURE_PRESERVE_HARDLINKS) { - char *link_target; - if (!FLAGS_DEREF) { - link_target = is_in_ino_dev_hashtable(&source_stat); - if (link_target) { + if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) { + const char *link_target; + link_target = is_in_ino_dev_hashtable(&source_stat); + if (link_target) { + if (link(link_target, dest) < 0) { + ovr = ask_and_unlink(dest, flags); + if (ovr <= 0) + return ovr; if (link(link_target, dest) < 0) { - ovr = ask_and_unlink(dest, flags); - if (ovr <= 0) - return ovr; - if (link(link_target, dest) < 0) { - bb_perror_msg("cannot create link '%s'", dest); - return -1; - } + bb_perror_msg("cannot create link '%s'", dest); + return -1; } - return 0; } + return 0; } add_to_ino_dev_hashtable(&source_stat, dest); } src_fd = open_or_warn(source, O_RDONLY); - if (src_fd < 0) { + if (src_fd < 0) return -1; - } -#if DO_POSIX_CP /* POSIX way (a security problem versus symlink attacks!): */ - dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE) - ? O_WRONLY|O_CREAT|O_EXCL - : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode); -#else /* safe way: */ - dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode); -#endif + /* POSIX way is a security problem versus symlink attacks, + * we do it only for non-symlinks, and only for non-recursive, + * non-interactive cp. NB: it is still racy + * for "cp file /home/bad_user/file" case + * (user can rm file and create a link to /etc/passwd) */ + if (DO_POSIX_CP + || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE)) + && !S_ISLNK(dest_stat.st_mode)) + ) { + dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode); + } else /* safe way: */ + dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode); if (dst_fd == -1) { ovr = ask_and_unlink(dest, flags); if (ovr <= 0) { @@ -264,60 +295,55 @@ int copy_file(const char *source, const char *dest, int flags) } #endif if (bb_copyfd_eof(src_fd, dst_fd) == -1) - status = -1; + retval = -1; + /* Ok, writing side I can understand... */ if (close(dst_fd) < 0) { bb_perror_msg("cannot close '%s'", dest); - status = -1; - } - if (close(src_fd) < 0) { - bb_perror_msg("cannot close '%s'", source); - status = -1; + retval = -1; } + /* ...but read size is already checked by bb_copyfd_eof */ + close(src_fd); + goto preserve_mode_ugid_time; + } - } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) - || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) - || S_ISLNK(source_stat.st_mode) - ) { - // We are lazy here, a bit lax with races... - if (dest_exists) { - ovr = ask_and_unlink(dest, flags); - if (ovr <= 0) - return ovr; - } - if (S_ISFIFO(source_stat.st_mode)) { - if (mkfifo(dest, source_stat.st_mode) < 0) { - bb_perror_msg("cannot create fifo '%s'", dest); - return -1; - } - } else if (S_ISLNK(source_stat.st_mode)) { - char *lpath; - - lpath = xmalloc_readlink_or_warn(source); - if (lpath && symlink(lpath, dest) < 0) { + /* Source is a symlink or a special file */ + /* We are lazy here, a bit lax with races... */ + if (dest_exists) { + errno = EEXIST; + ovr = ask_and_unlink(dest, flags); + if (ovr <= 0) + return ovr; + } + if (S_ISLNK(source_stat.st_mode)) { + char *lpath = xmalloc_readlink_or_warn(source); + if (lpath) { + int r = symlink(lpath, dest); + free(lpath); + if (r < 0) { bb_perror_msg("cannot create symlink '%s'", dest); - 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("cannot preserve %s of '%s'", "ownership", dest); - - return 0; - - } else { - if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { - bb_perror_msg("cannot create '%s'", dest); - return -1; - } + } + /* _Not_ jumping to preserve_mode_ugid_time: + * symlinks don't have those */ + return 0; + } + if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) + || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) + ) { + if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { + bb_perror_msg("cannot create '%s'", dest); + return -1; } } else { - bb_error_msg("internal error: unrecognized file type"); + bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode); return -1; } - preserve_status: + preserve_mode_ugid_time: if (flags & FILEUTILS_PRESERVE_STATUS /* Cannot happen: */ @@ -327,6 +353,7 @@ int copy_file(const char *source, const char *dest, int flags) times.actime = source_stat.st_atime; times.modtime = source_stat.st_mtime; + /* BTW, utimes sets usec-precision time - just FYI */ if (utime(dest, ×) < 0) bb_perror_msg("cannot preserve %s of '%s'", "times", dest); if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { @@ -337,5 +364,5 @@ int copy_file(const char *source, const char *dest, int flags) bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest); } - return status; + return retval; } diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c index 18a8b94..4d87b94 100644 --- a/libbb/xreadlink.c +++ b/libbb/xreadlink.c @@ -10,8 +10,7 @@ * NOTE: This function returns a malloced char* that you will have to free * yourself. You have been warned. */ - -char *xmalloc_readlink_or_warn(const char *path) +char *xmalloc_readlink(const char *path) { enum { GROWBY = 80 }; /* how large we will grow strings by */ @@ -20,20 +19,30 @@ char *xmalloc_readlink_or_warn(const char *path) do { buf = xrealloc(buf, bufsize += GROWBY); - readsize = readlink(path, buf, bufsize); /* 1st try */ + readsize = readlink(path, buf, bufsize); if (readsize == -1) { - bb_perror_msg("%s", path); free(buf); return NULL; } - } - while (bufsize < readsize + 1); + } while (bufsize < readsize + 1); buf[readsize] = '\0'; return buf; } +char *xmalloc_readlink_or_warn(const char *path) +{ + char *buf = xmalloc_readlink(path); + if (!buf) { + /* EINVAL => "file: Invalid argument" => puzzled user */ + bb_error_msg("%s: cannot read link (not a symlink?)", path); + } + return buf; +} + +/* UNUSED */ +#if 0 char *xmalloc_realpath(const char *path) { #if defined(__GLIBC__) && !defined(__UCLIBC__) @@ -46,3 +55,4 @@ char *xmalloc_realpath(const char *path) return xstrdup(realpath(path, buf)); #endif } +#endif diff --git a/networking/isrv.c b/networking/isrv.c index 1a41dd4..080c60f 100644 --- a/networking/isrv.c +++ b/networking/isrv.c @@ -21,20 +21,6 @@ /* Helpers */ -/* Even if _POSIX_MONOTONIC_CLOCK is defined, this - * may require librt */ -#if 0 /*def _POSIX_MONOTONIC_CLOCK*/ -static time_t monotonic_time(void) -{ - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) - time(&ts.tv_sec); - return ts.tv_sec; -} -#else -#define monotonic_time() (time(NULL)) -#endif - /* Opaque structure */ struct isrv_state_t { @@ -258,7 +244,7 @@ static void handle_fd_set(isrv_state_t *state, fd_set *fds, int (*h)(int, void * /* this peer is gone */ remove_peer(state, peer); } else if (TIMEOUT) { - TIMEO_TBL[peer] = monotonic_time(); + TIMEO_TBL[peer] = monotonic_sec(); } } } @@ -335,7 +321,7 @@ void isrv_run( break; if (timeout) { - time_t t = monotonic_time(); + time_t t = monotonic_sec(); if (t != CURTIME) { CURTIME = t; handle_timeout(state, do_timeout); diff --git a/runit/runsv.c b/runit/runsv.c index baef6e1..b35c266 100644 --- a/runit/runsv.c +++ b/runit/runsv.c @@ -33,6 +33,34 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "libbb.h" #include "runit_lib.h" +#if ENABLE_MONOTONIC_SYSCALL +#include <sys/syscall.h> + +/* libc has incredibly messy way of doing this, + * typically requiring -lrt. We just skip all this mess */ +static void gettimeofday_ns(struct timespec *ts) +{ + syscall(__NR_clock_gettime, CLOCK_REALTIME, ts); +} +#else +static void gettimeofday_ns(struct timespec *ts) +{ + if (sizeof(struct timeval) == sizeof(struct timespec) + && sizeof(((struct timeval*)ts)->tv_usec) == sizeof(ts->tv_nsec) + ) { + /* Cheat */ + gettimeofday((void*)ts, NULL); + ts->tv_nsec *= 1000; + } else { + extern void BUG_need_to_implement_gettimeofday_ns(void); + BUG_need_to_implement_gettimeofday_ns(); + } +} +#endif + +/* Compare possibly overflowing unsigned counters */ +#define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0) + static int selfpipe[2]; /* state */ @@ -126,14 +154,6 @@ static int rename_or_warn(const char *old, const char *new) return 0; } -#define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0) - -#include <sys/syscall.h> -static void gettimeofday_ns(struct timespec *ts) -{ - syscall(__NR_clock_gettime, CLOCK_REALTIME, ts); -} - static void update_status(struct svdir *s) { ssize_t sz; diff --git a/scripts/trylink b/scripts/trylink index 4eaa334..0b8f6c0 100755 --- a/scripts/trylink +++ b/scripts/trylink @@ -14,37 +14,30 @@ try() { BBOX_LIB_LIST=`echo "$BBOX_LIB_LIST" | xargs -n1 | sort | uniq | xargs` # First link with all libs. If it fails, bail out -l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/'` echo "Trying libraries: $BBOX_LIB_LIST" -try "-Wl,--start-group $l_list -Wl,--end-group" "$@" \ +l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'` +test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group" +try "$l_list" "$@" \ || { echo "Failed: $* -Wl,--start-group $l_list -Wl,--end-group" cat busybox_ld.err exit 1 } -#### Hack disabled: conflicts with ld --verbose flag in last link phase - -##### Hack: we are not supposed to know executable name, -##### but this hack cuts down link time -####mv busybox_unstripped busybox_unstripped.tmp -####mv busybox.map busybox.map.tmp - # Now try to remove each lib and build without it. # Stop when no lib can be removed. -####ever_discarded=false while test "$BBOX_LIB_LIST"; do $debug && echo "Trying libraries: $BBOX_LIB_LIST" all_needed=true for one in $BBOX_LIB_LIST; do without_one=`echo " $BBOX_LIB_LIST " | sed "s/ $one / /g" | xargs` - l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/'` - $debug && echo "Trying -l options: $l_list" - if try "-Wl,--start-group $l_list -Wl,--end-group" "$@"; then + l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'` + test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group" + $debug && echo "Trying -l options: '$l_list'" + if try "$l_list" "$@"; then echo "Library $one is not needed" BBOX_LIB_LIST="$without_one" all_needed=false -#### ever_discarded=true else echo "Library $one is needed" fi @@ -57,24 +50,19 @@ while test "$BBOX_LIB_LIST"; do #{ echo "$BBOX_LIB_LIST" | grep -q ' '; } || break done -####mv busybox_unstripped.tmp busybox_unstripped -####mv busybox.map.tmp busybox.map -####$ever_discarded && { - # Make the binary with final, minimal list of libs - echo "Final link with: $BBOX_LIB_LIST" - l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/'` - # --verbose gives us gobs of info to stdout (e.g. linker script used) - if ! test -f busybox_ldscript; then - try "-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose" "$@" >busybox_ld.out ####|| exit 1 - else - echo "Custom linker script 'busybox_ldscript' found, using it" - # Add SORT_BY_ALIGNMENT to linker script (found in busybox_ld.out): - # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) } - # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*) - # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*) - # This will eliminate most of the data padding (~3kb). - try "-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose -Wl,-T -Wl,busybox_ldscript" "$@" >busybox_ld.out - fi -####} -####rm busybox_ld.err -####exit 0 # Ensure "success" exit code +# Make the binary with final, minimal list of libs +echo "Final link with: $BBOX_LIB_LIST" +l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'` +test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose" +# --verbose gives us gobs of info to stdout (e.g. linker script used) +if ! test -f busybox_ldscript; then + try "$l_list -Wl,--verbose" "$@" >busybox_ld.out +else + echo "Custom linker script 'busybox_ldscript' found, using it" + # Add SORT_BY_ALIGNMENT to linker script (found in busybox_ld.out): + # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) } + # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*) + # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*) + # This will eliminate most of the data padding (~3kb). + try "$l_list -Wl,--verbose -Wl,-T -Wl,busybox_ldscript" "$@" >busybox_ld.out +fi |