aboutsummaryrefslogtreecommitdiff
path: root/lib
AgeCommit message (Collapse)Author
2020-01-16The expand should expand.Rob Landley
2020-01-08Implement readelf(1).Elliott Hughes
Basic readelf(1) implementation, with output close enough to the binutils version to be usable with scripts that expect the binutils version. This started as an implementation of nm(1) until I realized that I almost always want readelf instead, and that you actually have to do much of the work needed for readelf just to implement nm. Arguably nm (being part of POSIX) belongs in toybox while readelf doesn't. An argument could also be made that neither really belongs in toybox, belonging in a separate set of development tools (such as binutils or the LLVM binutils). Doesn't support most of the architecture-specific stuff, most notably relocations, but is aware of things like ARM exidx sections and the common register state notes in core dumps for the "big four" architectures: arm, arm64, x86, and x86-64. Doesn't support symbol versions (but probably should). Doesn't support section groups or the -t "section details" (which is a long form of -S "section headers" that I've never seen used in practice and which isn't part of -a). Doesn't support dumping unwind info or the hash table bucket histograms. Reuses the table of ELF architectures from file(1). Not fuzzed, but successfully parses all the ELF files in my Ubuntu 18.04 system's lib directories. Attempts to exit with an error when presented with an invalid ELF file rather than struggle on as binutils seems to.
2020-01-06SebiderSushi reported that chmod g+s wasn't working.Rob Landley
2020-01-03Only free the environment variables we didn't inherit from exec.Rob Landley
2020-01-03No, optflags gets cleared by toy_init(), use toys.rebound instead.Rob Landley
2020-01-03Add MAYFORK to "help", teach it to behave differently when called as aRob Landley
builtin, and add -u.
2019-12-28Allow dlist_terminate() to be called repeatedly on same list.Rob Landley
2019-12-14Bugfix: xgetline() wasn't returning NULL at EOF.Rob Landley
2019-12-14dirtree.c: avoid spurious EINVAL warnings.Elliott Hughes
An Android engineer complained that they were seeing this when not running as root: $ adb shell ls ls: ./postinstall: Invalid argument ls: ./init: Permission denied ls: ./data_mirror: Invalid argument ls: ./init.environ.rc: Invalid argument ls: ./metadata: Invalid argument acct adb_keys apex From strace, it was here: newfstatat(4, "adb_keys", 0x7fc67eca88, AT_SYMLINK_NOFOLLOW) = -1 EACCES (Permission denied) readlinkat(4, "adb_keys", 0x5e843c7720, 4095) = -1 EINVAL (Invalid argument) So stop looking at st.st_mode (and then deciding to do a readlinkat()) if we didn't actually successfully stat().
2019-12-12Next round of toysh work, with associated lib plumbing.Rob Landley
2019-12-06Squash a warning.Rob Landley
2019-12-06stat: fix filesystem type on macOS.Elliott Hughes
(Also show unknown values on Linux in hex rather than just "unknown".)
2019-11-23Fix dev_minor()/dev_major()/dev_makedev() for Mac.Elliott Hughes
Major/minor device encoding is not portable. No two BSDs agree with each other, and Darwin is different again. Everyone does agree on having major()/minor()/makedev() macros, but they disagree whether they should be in <sys/types.h> (the BSDs including Darwin, and old versions of glibc) or <sys/sysmacros.h> (glibc >= 2.26 and bionic). This fixes `ls -l /dev/zero` and `stat /dev/zero` on Mac.
2019-11-23Elliott pointed out a codepath that could use uninitialized data.Rob Landley
(If DIRTREE_SYMFOLLOW returns ENOENT, skipping the second fstatat() would also skip the else goto error)
2019-11-22Add getopt(1).Elliott Hughes
Includes new tests.
2019-11-13Old gcc versions are confused by __has_include(), so nest the #ifdefsRob Landley
and #define/#undef a second symbol for the else case.
2019-10-29Let "find -L -type -l" find dangling symlinks.Rob Landley
2019-10-27macOS: implement posix_fallocate().Elliott Hughes
2019-10-27Tar extract should delete files or symlinks where it's making a directory,Rob Landley
but --restrict checking should run on the path up to the last component before unlinking so tar can't be tricked into deleting random files off the system.
2019-10-26Add ln -r and relative_path() to lib, plus test.Rob Landley
And a few small cleanups while I was there.
2019-10-23Use adjtimex instead of adjtime (bionic hasn't got adjtime).Rob Landley
And check in more multicast support that's been sitting in the tree, I don't have a test enviornment for it anymore but somebody wanted this...
2019-10-21Remove getdirname(), it's _not_ a drop-in replacement for dirnameRob Landley
(it returns a malloc), and doesn't match the object lifetime of getbasename() (which always returns some or all of its argument string). The dirname() in libc modifies its argument string, but that's what posix says to do: https://pubs.opengroup.org/onlinepubs/9699919799.2008edition/functions/dirname.html so I guess we can live with it.
2019-10-21lib: getdirname fix seqfault on null ptrJarno Mäkipää
In order to be used as drop in replacement for dirname() If path is a null pointer or points to an empty string, dirname() shall return a pointer to the string "." .
2019-10-19Use the MKPATH macros in the mkpathat() implementation.Rob Landley
2019-10-18Teach getdirname() to return "." as appropriate.Rob Landley
2019-10-12Sigh, second attempt at removing strlen() from strstart().Rob Landley
2019-10-10The __ANDROID_NDK__ symbol is missing from ndk-r20 but Dan Albert pointedRob Landley
out clang's __has_include(), and && shorts out the eval for gcc.
2019-10-09Don't strlen() potentially long target string each call to strstart().Rob Landley
The downside is we don't use assembly optimized libc comparison functions, but the common case is short/no matches until full match. Probably net win.
2019-10-03Oops, part of the old tar support fix is in lib, check that in too.Rob Landley
2019-09-18Get exit code right when command intercepts signal, make timeout useRob Landley
xwaitpid(), fix off by one in xwaitpid().
2019-08-26xargs: add --max-args synonym, -o option, and fix -p.Elliott Hughes
The Linux kernel uses the --max-args synonym for -n. Barbarians who use vi need xargs' -o to be able to do something like: find -name xargs.c | xargs vi # Sad vi. find -name xargs.c | xargs -o vi # Happy vi. The -p option needs fixing to read from /dev/tty because stdin is otherwise occupied in xargs. I think xargs is the only place that needs this, so it didn't seem sensible to make all callers to yesno() be specific about which they wanted, hence the new function. Also remove the documentation for the build-time XARGS_PEDANTIC option which isn't actually implemented. Also add a TODO for -P (which is used by at least one script in the Linux kernel).
2019-08-26Try to make ls failure more graceful. Print ? ? ? entries instead of error msg.Rob Landley
Tweak DIRTREE_STATLESS so it returns zero stat for any error (I'm testing that dev, ino, and blksize are all zero), and fill in file type from readdir()
2019-08-21Add xgetline (which the last sh checkin used, oops).Rob Landley
2019-08-15linestack.c: fix buffer length.Elliott Hughes
GCC 8.2 (which my laptop appears to have been quietly upgraded to) points out that the %04X of an int might actually take 8 characters; between that, the "U+", and the trailing NUL, we're gonna need a bigger boat...
2019-08-12sort: move off get_rawline.Elliott Hughes
This was the last user of get_rawline, which lets us remove it.
2019-08-09find: fix dangling symlink behavior.Elliott Hughes
ENOENT is ignored, but other errors are reported.
2019-08-04Add TOYFLAG_MAYFORK and annotate a couple commands.Rob Landley
A TOYFLAG_NOFORK command must run in the context of toysh, but a MAYFORK can either run standalone or run in the toysh process. MAYFORK means it cleans up after itself: no leaked resources (malloc, mmap, filehandles, etc), even in error_exit() paths that would longjmp() back to the shell. It also doesn't discard anything we need to retain (don't close stdout, change toys.optargs[] so we can't free it, etc)...
2019-07-31Move the empty regex workaround into xregcomp.Elliott Hughes
No current caller except grep needs this, but consistency seems like a good idea. Also change the xregcomp error message to be a bit more human-readable, rather than mention an implementation detail.
2019-07-31Fix error path and tweak comments.Rob Landley
2019-07-24Avoid double-close of fd in loopfiles_lines.Elliott Hughes
Test: no EBADF in `strace -e close ./toybox tac /proc/version`
2019-07-22Start replacing get_line() with getline().Elliott Hughes
I started this last night, but thought I'd aim to send multiple small patches rather than work through all the callers and send one big patch. I've deliberately chosen the ugly name `allocated_length` because we've had historical bugs where folks think this a line length in the sense of the return value. I do wonder whether we should actually have some kind of getline() wrapper that hides the `char *`/`size_t` pair in lib/, which makes the function easier to use in most cases but does add the less common gotcha that you wouldn't be able to getline() through multiple files at once (which does happen in at least one toy). But maybe the real fix is to look harder for places where we can just use loopfiles_lines? Speaking of which, should we actually add two more arguments to that? Specifically: switch it to getdelim() rather than getline() behind the scenes, and also add a way to have the trailing '\n' automatically removed, since that seems to be what most callers want? Anyway, that seemed like enough questions that it was time to send this initial patch out before doing too much more...
2019-07-13net.c: add missing static.Elliott Hughes
Pointed out during code review of the recent refactor.
2019-07-12pidof: fix default behavior, add -x.Elliott Hughes
Before this patch, we're effectively doing `pidof -x` all the time. This patch changes names_to_pid() to allow us to say whether or not we want to include scripts, and adjusts the callers appropriately. Also add tests for `pidof` versus `pidof -x` which pass after this patch, without regressing the existing killall tests.
2019-07-11bind/connect cleanup.Elliott Hughes
Rename the existing xbind/xconnect to xbindany/xconnectany, to make room for new xbind/xconnect that are more like 'x' versions of the regular bind and connect. Move explicit bind/connect callers over to xbind/xconnect. Of the affected commands, only netcat is actually used by Android. It was the most recent patch to netcat that made the lack of a more traditional xbind/xconnect apparent.
2019-07-10dd: iflags, oflags, fix ^C, fix the fundamental loop.Elliott Hughes
Investigating why the toybox tar tests fail on Android with toybox dd, I realized I was rewriting a part of dd I'd rewritten before! This is a re-send of my 2019-02-22 patch, rebased against the current ToT... This patch was originally motivated because after suggesting to the author of https://stackoverflow.com/questions/17157820/access-vdsolinux/54797221#54797221 that he could tell dd to work in bytes rather than blocks, I realized that our dd doesn't actually support that. But the rewrite of the main loop is necessary to fix the incorrect output from the dd calls in the tar test. Without this patch, `yes | dd bs=65536 count=1 > fweep` basically gives random output, based on how many bytes the pipe feels like giving you in your first read. (As far as I know, dd *without* bs= was fine, but I can't guarantee that that's true, just that I haven't seen it fail.) Also switch to TAGGED_ARRAY and comma_* for conv rather than add two more copies of an undesired idiom. It turned out -- contrary to the belief of cp(1) -- that comma_scan isn't suitable for this because of its magic handling of "no" prefixes. (It's actually harmless in cp because none of the --preserve options begin with "no", but some dd options do.) To this end, comma_remove is a less-magic comma_scan. I've also changed an `if` to a `while` because other implementations allow things like `--preserve=mode,mode` or `conv=sync,sync`. (If we decide this is a bug rather than a feature, we should at least fix the error message to be clear that we're rejecting the *duplication*, not the option itself.) I've also fixed the ^C behavior by simply adding a direct SIGINT handler rather than trying to be clever inside the read loop (which is why we weren't handling the SIGINT until the read returned). I've also removed `strstarteq` and just added the '=' to each literal when calling regular `strstart`. Plus basic tests.
2019-07-10Add dlist_lpop() to remove last entry (use dlist as stack).Rob Landley
2019-07-06killall: better handling of long names.Elliott Hughes
Change names_to_pid() so that we can actually match shell scripts with long names (the code to get the shell script's name was correct, but there was an extra test preventing us from actually comparing it to the sought name). In kill.c itself, remove a dead test for -l and switch to the FLAG() macro. Also extend the tests to explicitly cover long and short names.
2019-06-25lib/args.c: add quotes.Elliott Hughes
This failure was a little hard to parse: tar: Unknown option mode (see "tar --help") This would have been clearer: tar: Unknown option 'mode' (see "tar --help")
2019-06-20The O_PATH breaks stuff, have to work around Android's selinux rules for /Rob Landley
some other way.
2019-06-20Teach dlist_pop() to work on a dlist_terminate()d listRob Landley