busybox/coreutils/truncate.c

88 lines
2.1 KiB
C
Raw Normal View History

/*
* Mini truncate implementation for busybox
*
* Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
//config:config TRUNCATE
//config: bool "truncate"
//config: default y
//config: help
//config: truncate truncates files to a given size. If a file does
//config: not exist, it is created unless told otherwise.
//kbuild:lib-$(CONFIG_TRUNCATE) += truncate.o
//applet:IF_TRUNCATE(APPLET_NOFORK(truncate, truncate, BB_DIR_USR_BIN, BB_SUID_DROP, truncate))
//usage:#define truncate_trivial_usage
//usage: "[-c] -s SIZE FILE..."
//usage:#define truncate_full_usage "\n\n"
//usage: "Truncate FILEs to the given size\n"
//usage: "\n -c Do not create files"
//usage: "\n -s SIZE Truncate to SIZE"
//usage:
//usage:#define truncate_example_usage
//usage: "$ truncate -s 1G foo"
#include "libbb.h"
#if ENABLE_LFS
# define XATOU_SFX xatoull_sfx
#else
# define XATOU_SFX xatoul_sfx
#endif
/* This is a NOFORK applet. Be very careful! */
int truncate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int truncate_main(int argc UNUSED_PARAM, char **argv)
{
unsigned opts;
int flags = O_WRONLY | O_NONBLOCK;
int ret = EXIT_SUCCESS;
char *size_str;
off_t size;
enum {
OPT_NOCREATE = (1 << 0),
OPT_SIZE = (1 << 1),
};
opt_complementary = "s:-1";
opts = getopt32(argv, "cs:", &size_str);
if (!(opts & OPT_NOCREATE))
flags |= O_CREAT;
// TODO: coreutils 8.17 also support "m" (lowercase) suffix
// with truncate, but not with dd!
// We share kMG_suffixes[], so we can't make both tools
// compatible at once...
size = XATOU_SFX(size_str, kMG_suffixes);
argv += optind;
while (*argv) {
truncate: always set mode when opening file to avoid fortify errors Busybox crashes due to no mode being given when opening: $ ./busybox truncate -s 1M foo *** invalid open64 call: O_CREAT without mode ***: ./busybox terminated ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x7338f)[0x7f66d921338f] /lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7f66d92aac9c] /lib/x86_64-linux-gnu/libc.so.6(+0xeb6aa)[0x7f66d928b6aa] ./busybox[0x4899f9] ======= Memory map: ======== 00400000-004d0000 r-xp 00000000 00:1a 137559 /home/ari/busybox/busybox 006cf000-006d0000 r--p 000cf000 00:1a 137559 /home/ari/busybox/busybox 006d0000-006d1000 rw-p 000d0000 00:1a 137559 /home/ari/busybox/busybox 006d1000-006d4000 rw-p 00000000 00:00 0 014e7000-01508000 rw-p 00000000 00:00 0 [heap] 7f66d8f8a000-7f66d8fa0000 r-xp 00000000 08:07 1579008 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f66d8fa0000-7f66d919f000 ---p 00016000 08:07 1579008 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f66d919f000-7f66d91a0000 rw-p 00015000 08:07 1579008 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f66d91a0000-7f66d935b000 r-xp 00000000 08:07 1578994 /lib/x86_64-linux-gnu/libc-2.19.so 7f66d935b000-7f66d955a000 ---p 001bb000 08:07 1578994 /lib/x86_64-linux-gnu/libc-2.19.so 7f66d955a000-7f66d955e000 r--p 001ba000 08:07 1578994 /lib/x86_64-linux-gnu/libc-2.19.so 7f66d955e000-7f66d9560000 rw-p 001be000 08:07 1578994 /lib/x86_64-linux-gnu/libc-2.19.so 7f66d9560000-7f66d9565000 rw-p 00000000 00:00 0 7f66d9565000-7f66d966a000 r-xp 00000000 08:07 1579020 /lib/x86_64-linux-gnu/libm-2.19.so 7f66d966a000-7f66d9869000 ---p 00105000 08:07 1579020 /lib/x86_64-linux-gnu/libm-2.19.so 7f66d9869000-7f66d986a000 r--p 00104000 08:07 1579020 /lib/x86_64-linux-gnu/libm-2.19.so 7f66d986a000-7f66d986b000 rw-p 00105000 08:07 1579020 /lib/x86_64-linux-gnu/libm-2.19.so 7f66d986b000-7f66d988e000 r-xp 00000000 08:07 1578981 /lib/x86_64-linux-gnu/ld-2.19.so 7f66d9a64000-7f66d9a67000 rw-p 00000000 00:00 0 7f66d9a8a000-7f66d9a8d000 rw-p 00000000 00:00 0 7f66d9a8d000-7f66d9a8e000 r--p 00022000 08:07 1578981 /lib/x86_64-linux-gnu/ld-2.19.so 7f66d9a8e000-7f66d9a8f000 rw-p 00023000 08:07 1578981 /lib/x86_64-linux-gnu/ld-2.19.so 7f66d9a8f000-7f66d9a90000 rw-p 00000000 00:00 0 7ffc47761000-7ffc47782000 rw-p 00000000 00:00 0 [stack] 7ffc477ab000-7ffc477ad000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Aborted (core dumped) $ Fix this by simply always setting the mode, as it doesn't hurt even when O_CREAT is not specified. This bug is a regression introduced in fc3e40e, as xopen(), which was originally used, would automatically set the mode. Signed-off-by: Ari Sundholm <ari@tuxera.com> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2016-01-04 19:10:37 +05:30
int fd = open(*argv, flags, 0666);
if (fd < 0) {
if (errno != ENOENT || !(opts & OPT_NOCREATE)) {
bb_perror_msg("%s: open", *argv);
ret = EXIT_FAILURE;
}
/* else: ENOENT && OPT_NOCREATE:
* do not report error, exitcode is also 0.
*/
} else {
if (ftruncate(fd, size) == -1) {
bb_perror_msg("%s: truncate", *argv);
ret = EXIT_FAILURE;
}
xclose(fd);
}
++argv;
}
return ret;
}