Systemd/src/test
Lennart Poettering 62a769136d core: rework how we track which PIDs to watch for a unit
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.
2018-01-23 21:29:31 +01:00
..
generate-sym-test.py Mark python scripts executable 2017-05-07 20:16:47 -04:00
meson.build core: rework how we track which PIDs to watch for a unit 2018-01-23 21:29:31 +01:00
test-acl-util.c test: use NOBODY_USER_NAME instead of hard-coded string "noody" 2017-12-07 17:12:09 +09:00
test-af-list.c tests: add tests for empty string 2017-11-28 20:21:25 +09:00
test-alloc-util.c test-alloc-util: add tests for memdup_suffix0() and greedy_realloc() 2017-11-28 20:33:58 +09:00
test-architecture.c test-architecture: add more tests 2017-11-28 20:34:32 +09:00
test-arphrd-list.c tests: add tests for empty string 2017-11-28 20:21:25 +09:00
test-ask-password-api.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-async.c shutdown: unify shutdown.c's and async.c's sync() helper process 2017-12-25 11:48:21 +01:00
test-barrier.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-bitmap.c test-bitmap: add tests for bitmap_copy() 2017-11-28 20:34:36 +09:00
test-boot-timestamps.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-bpf.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-btrfs.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-calendarspec.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-cap-list.c test-cap-list: add more tests about capability_set_{from,to}_string() 2017-11-28 22:17:00 +09:00
test-capability.c tree-wide: use EXIT_SUCCESS/EXIT_FAILURE in exit() where we can 2017-12-25 11:48:21 +01:00
test-cgroup-mask.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-cgroup-util.c tree-wide: use SPECIAL_ROOT_SLICE 2017-12-15 14:57:07 +01:00
test-cgroup.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-clock.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-condition.c test: fix condition test if there are no controllers 2018-01-05 13:58:32 +01:00
test-conf-files.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-conf-parser.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-copy.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-cpu-set-util.c tree-wide: use cpu_set_mfree() 2017-12-06 10:32:38 +09:00
test-daemon.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-date.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-device-nodes.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-dissect-image.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-dlopen.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-dns-domain.c resolved: fix "in-between" logic when boundaries are equal (#7590) 2017-12-14 14:08:21 +09:00
test-ellipsize.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-engine.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-env-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-escape.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-exec-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-execute.c test-execute: use the "nogroup" group if it exists for testing 2017-12-06 13:40:50 +01:00
test-extract-word.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-fd-util.c test-fd-util: add assert_se to make coverity happy 2017-11-28 09:25:39 +01:00
test-fdset.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-fileio.c tree-wide: make use of new STRLEN() macro everywhere (#7639) 2017-12-14 19:02:29 +01:00
test-firewall-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-fs-util.c tree-wide: port all code to use safe_getcwd() 2018-01-17 11:17:38 +01:00
test-fstab-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-glob-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-hash.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-hashmap-ordered.awk meson: also indent scripts with 8 spaces 2017-04-25 08:49:16 -04:00
test-hashmap-plain.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-hashmap.c Add set/hashmap helpers for non-trivial freeing and use where straighforward 2017-11-28 21:30:30 +01:00
test-helper.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-helper.h Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-hexdecoct.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-hostname-util.c test-hostname-util: add assert_se's to make coverity happy 2017-11-28 09:26:14 +01:00
test-hostname.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-id128.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-in-addr-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-install-root.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-install.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-io-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-ipcrm.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-job-type.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-journal-importer.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-libudev.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-list.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-locale-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-log.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-loopback.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-mount-util.c util-lib: use trailing slash in chase_symlinks, fd_is_mount_point, path_is_mount_point 2017-11-30 20:43:25 +01:00
test-namespace.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-netlink-manual.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-ns.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-nss.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-parse-util.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-path-lookup.c tree-wide: use strv_isempty() instead of strv_length() == 0 2017-11-29 12:41:08 +01:00
test-path-util.c util-lib: handle empty string in last_path_component 2017-11-30 20:54:31 +01:00
test-path.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-prioq.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-proc-cmdline.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-process-util.c test: test-process-util additions 2018-01-23 21:22:50 +01:00
test-random-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-ratelimit.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-replace-var.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-rlimit-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-sched-prio.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-seccomp.c process-util: rework wait_for_terminate_and_warn() to take a flags parameter 2018-01-04 13:27:27 +01:00
test-selinux.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-set.c Add set/hashmap helpers for non-trivial freeing and use where straighforward 2017-11-28 21:30:30 +01:00
test-sigbus.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-signal-util.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-siphash24.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-sizeof.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-sleep.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-socket-util.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-specifier.c specifier: add helper for escaping '%' characters to avoid making them subject for expansion 2017-11-29 12:32:56 +01:00
test-stat-util.c util-lib: rename path_check_fstype to path_is_fs_type 2017-11-30 20:43:25 +01:00
test-strbuf.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-string-util.c string-util: rework strextend() to optionally inset separators between each appended string 2017-12-05 13:49:12 +01:00
test-strip-tab-ansi.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-strv.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-strxcpyx.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-systemd-tmpfiles.py tests: allow passing more than one argument to test-systemd-tmpfiles.py 2017-12-06 19:30:08 +00:00
test-tables.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-terminal-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-time-util.c util: add new helper in_utc_timezone() 2017-11-20 10:53:26 +01:00
test-tmpfiles.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-udev.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-uid-range.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-unaligned.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-unit-file.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-unit-name.c tree-wide: use SPECIAL_ROOT_SLICE 2017-12-15 14:57:07 +01:00
test-user-util.c user-util: synthesize user records for "nobody" the same way as for "root" 2017-12-06 13:40:50 +01:00
test-utf8.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-util.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-verbs.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-watch-pid.c core: rework how we track which PIDs to watch for a unit 2018-01-23 21:29:31 +01:00
test-watchdog.c log: minimize includes in log.h 2018-01-11 14:44:31 +01:00
test-web-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-xattr-util.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00
test-xml.c Add SPDX license identifiers to source files under the LGPL 2017-11-19 19:08:15 +01:00