summaryrefslogtreecommitdiff
path: root/src/test
Commit message (Collapse)AuthorAge
* fd-util: add new call rearrange_stdio()Lennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | | Quite often we need to set up a number of fds as stdin/stdout/stderr of a process we are about to start. Add a generic implementation for a routine doing that that takes care to do so properly: 1. Can handle the case where stdin/stdout/stderr where previously closed, and the fds to set as stdin/stdout/stderr hence likely in the 0..2 range. handling this properly is nasty, since we need to first move the fds out of this range in order to later move them back in, to make things fully robust. 2. Can optionally open /dev/null in case for one or more of the fds, in a smart way, sharing the open file if possible between multiple of the fds. 3. Guarantees that O_CLOEXEC is not set on the three fds, even if the fds already were in the 0..2 range and hence possibly weren't moved.
* procfs-util: add APIs to get consumed CPU time and used memory from /procLennart Poettering2018-05-30
| | | | | | This is preparation for emulating the "usage_usec" keyed attribute of the "cpu.stat" property of the root cgroup from data in /proc. Similar, for emulating the "memory.current" attribute.
* khash: try to detect broken AF_ALG support in centos kernelsLennart Poettering2018-05-30
| | | | Fixes: #8278
* basic/log: make sure header is printed correctly, add testZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | | | | | | | | | | | If log_do_header() was called with overly long parameters, it'd generate improper output. Essentially, it'd be truncated at random point, in particular missing a newline at the end, so it'd run with the next field, usually MESSAGE=. log_do_header is called with parameters from compiled code (file name, lien nubmer, etc), so in practice this was unlikely to ever be a problem, but it is possible. In particular, if systemd was compiled from sources in some deeply nested directory (which happens for example in mock and other build roots), the filename could be very long. As a safety measure, let's truncate all parameters to 256 bytes. So we have 5 fields which are 256 bytes (plus the field name prefix), and a few other fields with fixed width. This must always fit in the 2048 byte buffer. I don't think there's much gain in calculating the required length precisely, since it's a lot of fields and a few bytes allocated on the stack don't matter.
* Fix format-truncation compile failure by typecasting USB IDs (#8250)Patrick Uiterwijk2018-05-30
| | | | | | | | | | | | | | | This patch adds safe_atoux16 for parsing an unsigned hexadecimal 16bit int, and uses that for parsing USB device and vendor IDs. This fixes a compile error with gcc-8 because while we know that USB IDs are 2 bytes, the compiler does not know that. ../src/udev/udev-builtin-hwdb.c:80:38: error: '%04X' directive output may be truncated writing between 4 and 8 bytes into a region of size between 2 and 6 [-Werror=format-truncation=] Signed-off-by: Adam Williamson <awilliam@redhat.com> Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
* test-user-util: skip most tests for nobody if synthentization is offZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | | | | | When synthetisation is turned off, there's just too many ways those tests can go wrong. We are not interested in verifying that the db on disk is correct, let's just skip all checks. In the first version of this patch, I recorded if we detected a mismatch during configuration and only skipped tests in that case, but actually it is possible to change the host configuration between our configuration phase and running of the tests. It's just more robust to skip always. (This is particularly true if tests are installed.)
* test-user-util: print function delimitersZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | This makes it easier to see what is going on. Crashes may happen in a nested test_{uid,gid}_to_name_one() function, and the default backtrace doesn't show the actual string being tested.
* fs-util: move fsync_directory_of_file() into generic codeLennart Poettering2018-05-30
| | | | | This function used by the journal code is pretty useful generically, let's move it to fs-util.c to make it useful for other code too.
* tests: add a simple test for utf8_n_codepoints()Zbigniew Jędrzejewski-Szmek2018-05-30
|
* pid1: do not initialize join_controllers by defaultZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | We're moving towards unified cgroup hierarchy where this is not necessary. This makes main.c a bit simpler.
* Move config_parse_join_controllers to shared, add testZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | config_parse_join_controllers would free the destination argument on failure, which is contrary to our normal style, where failed parsing has no effect. Moving it to shared also allows a test to be added.
* test-process-util: one more (void)Zbigniew Jędrzejewski-Szmek2018-05-30
|
* hash-func: add generic hash_ops implementation for hashing pathsLennart Poettering2018-05-30
| | | | | | | | | | | | | | | This is similar to string_hash_ops but operates one file system paths specifically. It will ensure that "/foo//bar" and "///foo/bar" are considered to be the same path for hashmap purposes. This makes use of the existing path_compare() API, and adds a matching hashing function for it. Note that relative and absolute paths will hash to different values, however whether the path is suffixed with a slash or not is not detected. This matches the existing path_compare() behaviour, and follows the logic that on Linux there can't be two different objects at path /foo/bar and /foo/bar/ either.
* fs-util: drop readlink_and_canonicalize()Yu Watanabe2018-05-30
|
* fd-util: move certain fds above fd #2 (#8129)Lennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This adds some paranoia code that moves some of the fds we allocate for longer periods of times to fds > 2 if they are allocated below this boundary. This is a paranoid safety thing, in order to avoid that external code might end up erroneously use our fds under the assumption they were valid stdin/stdout/stderr. Think: some app closes stdin/stdout/stderr and then invokes 'fprintf(stderr, …' which causes writes on our fds. This both adds the helper to do the moving as well as ports over a number of users to this new logic. Since we don't want to litter all our code with invocations of this I tried to strictly focus on fds we keep open for long periods of times only and only in code that is frequently loaded into foreign programs (under the assumptions that in our own codebase we are smart enough to always keep stdin/stdout/stderr allocated to avoid this pitfall). Specifically this means all code used by NSS and our sd-xyz API: 1. our logging APIs 2. sd-event 3. sd-bus 4. sd-resolve 5. sd-netlink This changed was inspired by this: https://github.com/systemd/systemd/issues/8075#issuecomment-363689755 This shows that apparently IRL there are programs that do close stdin/stdout/stderr, and we should accomodate for that. Note that this won't fix any bugs, this just makes sure that buggy programs are less likely to interfere with out own code.
* test-hashmap: test IteratedCacheVito Caputo2018-05-30
| | | | Add some rudimentary testing of the new IteratedCache
* test-user-util: skip most tests for nobody if synthentization is offZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | | | | | | | When synthetisation is turned off, there's just too many ways those tests can go wrong. We are not interested in verifying that the db on disk is correct, let's just skip all checks. In the first version of this patch, I recorded if we detected a mismatch during configuration and only skipped tests in that case, but actually it is possible to change the host configuration between our configuration phase and running of the tests. It's just more robust to skip always. (This is particularly true if tests are installed.) (cherry picked from commit 7559b2da10b1513849f22312d09a2381569b4f06)
* test-user-util: print function delimitersZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | This makes it easier to see what is going on. Crashes may happen in a nested test_{uid,gid}_to_name_one() function, and the default backtrace doesn't show the actual string being tested. (cherry picked from commit 52c6e6a8a0221530659c65090f18b16c45a9fc04)
* fs-util: add new unlinkat_deallocate() helperLennart Poettering2018-05-30
| | | | | | | | | | | This new helper not only removes a file from a directory but also ensures its space on disk is deallocated, by either punching a hole over the full file or truncating the file afterwards if the file's link counter is 0. This is useful in "vacuuming" algorithms to ensure that client's can't keep the disk space the vacuuming is supposed to recover pinned simply by keeping an fd open to it. (cherry picked from commit 43767d9d5e0ce8923828aebf9154da7af83916f7)
* hash-func: add generic hash_ops implementation for hashing pathsLennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | This is similar to string_hash_ops but operates one file system paths specifically. It will ensure that "/foo//bar" and "///foo/bar" are considered to be the same path for hashmap purposes. This makes use of the existing path_compare() API, and adds a matching hashing function for it. Note that relative and absolute paths will hash to different values, however whether the path is suffixed with a slash or not is not detected. This matches the existing path_compare() behaviour, and follows the logic that on Linux there can't be two different objects at path /foo/bar and /foo/bar/ either. (cherry picked from commit 46e16b347f83d809ed3d34f26286f580dfd086ce)
* test: add a simple smoke test for string_hashsum()Zbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | This is enough to show memory leakages pointed out by Stef Bon <stefbon@gmail.com>. (cherry picked from commit bd181f27d4d0c16c500c9f49394213d1fbad1f09)
* journalctl: add highlighting for matched substringZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | | Red is used for highligting, the same as grep does. Except when the line is highlighted red already, because it has high priority, in which case plain ansi highlight is used for the matched substring. Coloring is implemented for short and cat outputs, and not for other types. I guess we could also add it for verbose output in the future.
* journalctl: make matching optionally case sensitiveZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | | | | | | Case sensitive or case insensitive matching can be requested using --case-sensitive[=yes|no]. Unless specified, matching is case sensitive if the pattern contains any uppercase letters, and case insensitive otherwise. This matches what forward-search does in emacs, and recently also --ignore-case in less. This works surprisingly well, because usually when one is wants to do case-sensitive matching, the pattern is usually camel-cased. In the less frequent case when case-sensitive matching is required with an all-lowercase pattern, --case-sensitive can be used to override the automatic logic.
* util-lib: add new procfs-util.[ch] API for dealing with tasks limitsLennart Poettering2018-05-30
| | | | | As it turns out the limit on concurrent tasks on Linux nasty to determine, hence let's appropriate helpers for this.
* core: rework how we track which PIDs to watch for a unitLennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously, we'd maintain two hashmaps keyed by PIDs, pointing to Unit interested in SIGCHLD events for them. This scheme allowed a specific PID to be watched by exactly 0, 1 or 2 units. With this rework this is replaced by a single hashmap which is primarily keyed by the PID and points to a Unit interested in it. However, it optionally also keyed by the negated PID, in which case it points to a NULL terminated array of additional Unit objects also interested. This scheme means arbitrary numbers of Units may now watch the same PID. Runtime and memory behaviour should not be impact by this change, as for the common case (i.e. each PID only watched by a single unit) behaviour stays the same, but for the uncommon case (a PID watched by more than one unit) we only pay with a single additional memory allocation for the array. Why this all? Primarily, because allowing exactly two units to watch a specific PID is not sufficient for some niche cases, as processes can belong to more than one unit these days: 1. sd_notify() with MAINPID= can be used to attach a process from a different cgroup to multiple units. 2. Similar, the PIDFile= setting in unit files can be used for similar setups, 3. By creating a scope unit a main process of a service may join a different unit, too. 4. On cgroupsv1 we frequently end up watching all processes remaining in a scope, and if a process opens lots of scopes one after the other it might thus end up being watch by many of them. This patch hence removes the 2-unit-per-PID limit. It also makes a couple of other changes, some of them quite relevant: - manager_get_unit_by_pid() (and the bus call wrapping it) when there's ambiguity will prefer returning the Unit the process belongs to based on cgroup membership, and only check the watch-pids hashmap if that fails. This change in logic is probably more in line with what people expect and makes things more stable as each process can belong to exactly one cgroup only. - Every SIGCHLD event is now dispatched to all units interested in its PID. Previously, there was some magic conditionalization: the SIGCHLD would only be dispatched to the unit if it was only interested in a single PID only, or the PID belonged to the control or main PID or we didn't dispatch a signle SIGCHLD to the unit in the current event loop iteration yet. These rules were quite arbitrary and also redundant as the the per-unit handlers would filter the PIDs anyway a second time. With this change we'll hence relax the rules: all we do now is dispatch every SIGCHLD event exactly once to each unit interested in it, and it's up to the unit to then use or ignore this. We use a generation counter in the unit to ensure that we only invoke the unit handler once for each event, protecting us from confusion if a unit is both associated with a specific PID through cgroup membership and through the "watch_pids" logic. It also protects us from being confused if the "watch_pids" hashmap is altered while we are dispatching to it (which is a very likely case). - sd_notify() message dispatching has been reworked to be very similar to SIGCHLD handling now. A generation counter is used for dispatching as well. This also adds a new test that validates that "watch_pid" registration and unregstration works correctly.
* test: test-process-util additionsLennart Poettering2018-05-30
| | | | | Add some extra paranoia tests that PTR_TO_PID() and PID_TO_PTR() deals correctly with negative PID.
* tree-wide: port all code to use safe_getcwd()Lennart Poettering2018-05-30
|
* fs-util: add new chase_symlinks() flag CHASE_OPENLennart Poettering2018-05-30
| | | | | | | | | | | The new flag returns the O_PATH fd of the final component, which may be converted into a proper fd by open()ing it again through the /proc/self/fd/xyz path. Together with O_SAFE this provides us with a somewhat safe way to open() files in directories potentially owned by unprivileged code, where we want to refuse operation if any symlink tricks are played pointing to privileged files.
* fs-util: add new CHASE_SAFE flag to chase_symlinks()Lennart Poettering2018-05-30
| | | | | | | | | When the flag is specified we won't transition to a privilege-owned file or directory from an unprivileged-owned one. This is useful when privileged code wants to load data from a file unprivileged users have write access to, and validates the ownership, but want's to make sure that no symlink games are played to read a root-owned system file belonging to a different context.
* log: minimize includes in log.hLennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | | | | | | | | | log.h really should only include the bare minimum of other headers, as it is really pulled into pretty much everything else and already in itself one of the most basic pieces of code we have. Let's hence drop inclusion of: 1. sd-id128.h because it's entirely unneeded in current log.h 2. errno.h, dito. 3. sys/signalfd.h which we can replace by a simple struct forward declaration 4. process-util.h which was needed for getpid_cached() which we now hide in a funciton log_emergency_level() instead, which nicely abstracts the details away. 5. sys/socket.h which was needed for struct iovec, but a simple struct forward declaration suffices for that too. Ultimately this actually makes our source tree larger (since users of the functionality above must now include it themselves, log.h won't do that for them), but I think it helps to untangle our web of includes a tiny bit. (Background: I'd like to isolate the generic bits of src/basic/ enough so that we can do a git submodule import into casync for it)
* sd-bus: optionally, use inotify to wait for bus sockets to appearLennart Poettering2018-05-30
| | | | | | | | | | | This adds a "watch-bind" feature to sd-bus connections. If set and the AF_UNIX socket we are connecting to doesn't exist yet, we'll establish an inotify watch instead, and wait for the socket to appear. In other words, a missing AF_UNIX just makes connecting slower. This is useful for daemons such as networkd or resolved that shall be able to run during early-boot, before dbus-daemon is up, and want to connect to dbus-daemon as soon as it becomes ready.
* fs-util: rework touch_file() so that it can touch socket file nodesLennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | Let's rework touch_file() so that it works correctly on sockets, fifos, and device nodes: let's open an O_PATH file descriptor first and operate based on that, if we can. This is usually the better option as it this means we can open AF_UNIX nodes in the file system, and update their timestamps and ownership correctly. It also means we can correctly touch symlinks and block/character devices without triggering their drivers. Moreover, by operating on an O_PATH fd we can make sure that we operate on the same inode the whole time, and it can't be swapped out in the middle. While we are at it, rework the call so that we try to adjust as much as we can before returning on error. This is a good idea as we call the function quite often without checking its result, and hence it's best to leave the files around in the most "correct" fashion possible.
* test-sizeof: also add uid_tZbigniew Jędrzejewski-Szmek2018-05-30
| | | | We had gid_t, and pid_t, but not uid_t. Add for completeness.
* meson: rename libudev_internal to libudev_static and link into libudevZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | | | | | | This reduces the meson man=false target count to 1281. v2: - link test-engine with libshared instead of libsystemd_static Previous version built fine on F27, but fails on F26 with the following error: /usr/bin/ld: /tmp/ccr8HRGw.ltrans6.ltrans.o: undefined reference to symbol '__start_BUS_ERROR_MAP@@SD_SHARED' /home/zbyszek/fedora/systemd/systemd-9d5aae75c64f5583a110f03b94816aacc03bbf4d/x86_64-redhat-linux-gnu/src/shared/libsystemd-shared-236.so: error adding symbols: DSO missing from command line v3: - add libudev_basic
* meson: link libbasic and libshared_static into libsharedZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | | | | | | | gcrypt_util_sources had to be moved because otherwise they appeared twice in libshared.so halfproducts, causing an error. -fvisibility=default is added to libbasic, libshared_static so that the symbols appear properly in the exported symbol list in libshared. The advantage is that files are not compiled twice. When configured with -Dman=false, the ninja target list is reduced from 1588 to 1347 targets. The difference in compilation time is small (<10%). I think this is because of -O0 and ccache and multiple cores, and in different settings the compilation time could be reduced. The main advantage is that errors and warnings are not reported twice.
* meson: rename libsystemd_internal to libsystem_staticZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | | | We already use the "_static" suffix for libshared_static ("shared" is the name of the library, "static" is the format) and other libs, so let's rename for consistency. Also change libsystemd_static_sources to libsystemd_sources, since the same list is used for both and shorter is better.
* tree-wide: use EXIT_SUCCESS/EXIT_FAILURE in exit() where we canLennart Poettering2018-05-30
|
* tree-wide: introduce new safe_fork() helper and port everything overLennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | | | | | | | | | | This adds a new safe_fork() wrapper around fork() and makes use of it everywhere. The new wrapper does a couple of things we previously did manually and separately in a safer, more correct and automatic way: 1. Optionally resets signal handlers/mask in the child 2. Sets a name on all processes we fork off right after forking off (and the patch assigns useful names for all processes we fork off now, following a systematic naming scheme: always enclosed in () – in order to indicate that these are not proper, exec()ed processes, but only forked off children, and if the process is long-running with only our own code, without execve()'ing something else, it gets am "sd-" prefix.) 3. Optionally closes all file descriptors in the child 4. Optionally sets a PR_SET_DEATHSIG to SIGTERM in the child, in a safe way so that the parent dying before this happens being handled safely. 5. Optionally reopens the logs 6. Optionally connects stdin/stdout/stderr to /dev/null 7. Debug logs about the forked off processes.
* Prep v236 : Add missing SPDX-License-Identifier (8/9) src/testSven Eden2018-03-26
|
* Prep v236: Update build system and adapt to eloginds needs.Sven Eden2018-03-13
|
* Prep v236: Apply missing upstream updates to the build systemSven Eden2018-03-13
|
* string-util: rework strextend() to optionally inset separators between each ↵Lennart Poettering2017-11-28
| | | | | | | | | | | | | | appended string This adds a new flavour of strextend(), called strextend_with_separator(), which takes an optional separator string. If specified, the separator is inserted between each appended string, as well as before the first one, but only if the original string was non-empty. This new call is particularly useful when appending new options to mount option strings and suchlike, which need to be comma-separated, and initially start out from an empty string.
* util-lib,tests: rework unbase64 so that we skip over whitespace ↵Lennart Poettering2017-12-03
| | | | | | | | | | automatically (#7522) Let's optimize things a bit, and instead of having to strip whitespace first before decoding base64, let's do that implicitly while doing so. Given that base64 was designed the way it was designed specifically to be tolerant to whitespace changes, it's a good idea to do this automatically and implicitly.
* test-mount-util: move test_path_is_mount_point hereZbigniew Jędrzejewski-Szmek2017-10-31
| | | | | | | path-util.c and mount-util.c are intertwined, but path_is_mount_point() is defined in mount-util.c. No functional difference.
* specifier: add helper for escaping '%' characters to avoid making them ↵Lennart Poettering2017-11-21
| | | | | | | subject for expansion This is ultimately just a wrapper around strreplace(), but it makes things a bit more self-descriptive.
* Add set/hashmap helpers for non-trivial freeing and use where straighforwardZbigniew Jędrzejewski-Szmek2017-11-28
| | | | | | A macro is needed because otherwise we couldn't ensure type safety. Some simple tests are included. No functional change intended.
* test-process-util: add (void) cast to make coverity happyZbigniew Jędrzejewski-Szmek2017-11-27
| | | | | The intent is for the call to succeed only when privileged, so make that clear.
* elogind-firstboot: add vconsole keymap support (#7035)tblume2017-11-10
| | | | | | | | Enable elogind-firstboot to set the keymap. RFE: https://github.com/elogind/elogind/issues/6346
* tree-wide: do not work in assert() (#7271)Yu Watanabe2017-11-07
| | | | Fixes #7270.
* test-util: silence clang warning about unaligned accessZbigniew Jędrzejewski-Szmek2017-11-01
|