VERSION = 1
PATCHLEVEL = 7
SUBLEVEL = 1
-EXTRAVERSION = .svn
+EXTRAVERSION =
NAME = Unnamed
# *DOCUMENTATION*
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 */
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;
}
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')
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
/* 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) /**/;
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) /**/;
/* 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) /**/;
#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 */
}
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();
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);
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;
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 */;
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?
};
/* 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);
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
// (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())
*/
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;
if (S_ISDIR(source_stat.st_mode)) {
DIR *dp;
+ const char *tp;
struct dirent *d;
mode_t saved_umask = 0;
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);
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) {
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);
}
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
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) {
}
#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: */
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) {
bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
}
- return status;
+ return retval;
}
* 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 */
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__)
return xstrdup(realpath(path, buf));
#endif
}
+#endif
/* 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 {
/* this peer is gone */
remove_peer(state, peer);
} else if (TIMEOUT) {
- TIMEO_TBL[peer] = monotonic_time();
+ TIMEO_TBL[peer] = monotonic_sec();
}
}
}
break;
if (timeout) {
- time_t t = monotonic_time();
+ time_t t = monotonic_sec();
if (t != CURTIME) {
CURTIME = t;
handle_timeout(state, do_timeout);
#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 */
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;
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
#{ 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