summaryrefslogtreecommitdiff
path: root/src/basic/process-util.c
Commit message (Collapse)AuthorAge
* Prep v239: Uncomment header inclusions that are new or needed now.Sven Eden2018-08-24
|
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-08-24
| | | | | | | | | | | These lines are generally out-of-date, incomplete and unnecessary. With SPDX and git repository much more accurate and fine grained information about licensing and authorship is available, hence let's drop the per-file copyright notice. Of course, removing copyright lines of others is problematic, hence this commit only removes my own lines and leaves all others untouched. It might be nicer if sooner or later those could go away too, making git the only and accurate source of authorship information.
* tree-wide: drop 'This file is part of systemd' blurbLennart Poettering2018-08-24
| | | | | | | | | | | | | | | | This part of the copyright blurb stems from the GPL use recommendations: https://www.gnu.org/licenses/gpl-howto.en.html The concept appears to originate in times where version control was per file, instead of per tree, and was a way to glue the files together. Ultimately, we nowadays don't live in that world anymore, and this information is entirely useless anyway, as people are very welcome to copy these files into any projects they like, and they shouldn't have to change bits that are part of our copyright header for that. hence, let's just get rid of this old cruft, and shorten our codebase a bit.
* process-util: make use of delete_trailing_chars() in get_process_cmdline()Lennart Poettering2018-08-24
|
* Add macro for checking if some flags are setZbigniew Jędrzejewski-Szmek2018-08-24
| | | | | | | | | This way we don't need to repeat the argument twice. I didn't replace all instances. I think it's better to leave out: - asserts - comparisons like x & y == x, which are mathematically equivalent, but here we aren't checking if flags are set, but if the argument fits in the flags.
* process-util: also filter non-printable characters in get_process_com()Lennart Poettering2018-08-24
| | | | | | | | | | | | We already do that in get_process_cmdline(), which is very similar in behaviour otherwise. Hence, let's be safe and also filter them in get_process_comm(). Let's try to retain as much information as we can though and escape rather than suppress unprintable characters. Let's not increase comm names beyond the kernel limit on such names however. Also see discussion about this here: https://marc.info/?l=linux-api&m=152649570404881&w=2
* missing: define kernel internal limit TASK_COMM_LEN in userspace tooLennart Poettering2018-08-24
| | | | | We already use it at two places, and we are about to add one too. Arbitrary literally hardcoded limits suck.
* process-util: mention that wait_for_terminate_with_timeout() should be ↵Lennart Poettering2018-08-24
| | | | called with SIGCHLD blocked
* process-util: add a new FORK_MOUNTNS_SLAVE flag for safe_fork()Lennart Poettering2018-08-24
| | | | | | | | We already have a flag for creating a new mount namespace for the child. Let's add an extension to that: a new FORK_MOUNTNFS_SLAVE flag. When used in combination will mark all mounts in the child namespace as MS_SLAVE so that the child can freely mount or unmount stuff but it won't leak into the parent.
* process-util: add new helper call for adjusting the OOM scoreLennart Poettering2018-08-24
| | | | And let's make use of it in execute.c
* tree-wide: use newa() rather than alloca() where we canLennart Poettering2018-08-24
|
* tree-wide: be more careful with the type of array sizesLennart Poettering2018-08-24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously we were a bit sloppy with the index and size types of arrays, we'd regularly use unsigned. While I don't think this ever resulted in real issues I think we should be more careful there and follow a stricter regime: unless there's a strong reason not to use size_t for array sizes and indexes, size_t it should be. Any allocations we do ultimately will use size_t anyway, and converting forth and back between unsigned and size_t will always be a source of problems. Note that on 32bit machines "unsigned" and "size_t" are equivalent, and on 64bit machines our arrays shouldn't grow that large anyway, and if they do we have a problem, however that kind of overly large allocation we have protections for usually, but for overflows we do not have that so much, hence let's add it. So yeah, it's a story of the current code being already "good enough", but I think some extra type hygiene is better. This patch tries to be comprehensive, but it probably isn't and I missed a few cases. But I guess we can cover that later as we notice it. Among smaller fixes, this changes: 1. strv_length()' return type becomes size_t 2. the unit file changes array size becomes size_t 3. DNS answer and query array sizes become size_t Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
* tree-wide: drop license boilerplateZbigniew Jędrzejewski-Szmek2018-08-24
| | | | | | | | | | Files which are installed as-is (any .service and other unit files, .conf files, .policy files, etc), are left as is. My assumption is that SPDX identifiers are not yet that well known, so it's better to retain the extended header to avoid any doubt. I also kept any copyright lines. We can probably remove them, but it'd nice to obtain explicit acks from all involved authors before doing that.
* macro: introduce TAKE_PTR() macroLennart Poettering2018-08-24
| | | | | | | | | | | | | | | | This macro will read a pointer of any type, return it, and set the pointer to NULL. This is useful as an explicit concept of passing ownership of a memory area between pointers. This takes inspiration from Rust: https://doc.rust-lang.org/std/option/enum.Option.html#method.take and was suggested by Alan Jenkins (@sourcejedi). It drops ~160 lines of code from our codebase, which makes me like it. Also, I think it clarifies passing of ownership, and thus helps readability a bit (at least for the initiated who know the new macro)
* basic/macros: rename noreturn into _noreturn_ (#8456)Franck Bui2018-06-28
| | | | | | | | | | | | | | | "noreturn" is reserved and can be used in other header files we include: [ 16s] In file included from /usr/include/gcrypt.h:30:0, [ 16s] from ../src/journal/journal-file.h:26, [ 16s] from ../src/journal/journal-vacuum.c:31: [ 16s] /usr/include/gpg-error.h:1544:46: error: expected ‘,’ or ‘;’ before ‘)’ token [ 16s] void gpgrt_log_bug (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); Here we include grcrypt.h (which in turns include gpg-error.h) *after* we "noreturn" was defined in macro.h. (cherry picked from commit 848e863acc51ecfb0f3955c498874588201d9130)
* Prep v238: Uncomment now needed headers and unmask now needed functions in ↵Sven Eden2018-06-05
| | | | src/basic (1/6)
* process-util: don't install atfork() handler more than onceLennart Poettering2018-05-30
|
* util: add new safe_close_above_stdio() wrapperLennart Poettering2018-05-30
| | | | | | At various places we only want to close fds if they are not stdin/stdout/stderr, i.e. fds 0, 1, 2. Let's add a unified helper call for that, and port everything over.
* process: shortcut getenv_for_pid() for our own processLennart Poettering2018-05-30
|
* process-util: be more careful in is_kernel_thread()Lennart Poettering2018-05-30
| | | | | | | | | | | | This reworks is_kernel_thread() a bit. Instead of checking whether /proc/$pid/cmdline is entirely empty we now parse the 'flags' field from /proc/$pid/stat and check the PF_KTHREAD flag, which directly encodes whether something is a kernel thread. Why all this? With current kernels userspace processes can set their command line to empty too (through PR_SET_MM_ARG_START and friends), and could potentially confuse us. Hence, let's use a more reliable way to detect kernels like this.
* sd-bus: cleanup ssh sessions (Closes: #8076)Shawn Landden2018-05-30
| | | | | | | | | | | | | | | | | | | we still invoke ssh unnecessarily when there in incompatible or erreneous input The fallow-up to finish that would make the code a bit more verbose, as it would require repeating this bit: ``` r = bus_connect_transport(arg_transport, arg_host, false, &bus); if (r < 0) { log_error_errno(r, "Failed to create bus connection: %m"); goto finish; } sd_bus_set_allow_interactive_authorization(bus, arg_ask_password); ``` in every verb, after parsing. v2: add waitpid() to avoid a zombie process, switch to SIGTERM from SIGKILL v3: refactor, wait in bus_start_address()
* process-util: use raw_getpid() in getpid_cache() internally (#8115)Lennart Poettering2018-05-30
| | | | | | | | | We have the raw_getpid() definition in place anyway, and it's certainly beneficial to expose the same semantics on pre glibc 2.24 and after it too, hence always bypass glibc for this, and always cache things on our side. Fixes: #8113
* sd-bus: cleanup ssh sessions (Closes: #8076)Shawn Landden2018-05-30
| | | | | | | | | | | | | | | | | | | | we still invoke ssh unnecessarily when there in incompatible or erreneous input The fallow-up to finish that would make the code a bit more verbose, as it would require repeating this bit: ``` r = bus_connect_transport(arg_transport, arg_host, false, &bus); if (r < 0) { log_error_errno(r, "Failed to create bus connection: %m"); goto finish; } sd_bus_set_allow_interactive_authorization(bus, arg_ask_password); ``` in every verb, after parsing. v2: add waitpid() to avoid a zombie process, switch to SIGTERM from SIGKILL v3: refactor, wait in bus_start_address() (cherry picked from commit 392cf1d05dbfa1395f6d99102e5ea41debb58fec)
* process-util: use raw_getpid() in getpid_cache() internally (#8115)Lennart Poettering2018-05-30
| | | | | | | | | | We have the raw_getpid() definition in place anyway, and it's certainly beneficial to expose the same semantics on pre glibc 2.24 and after it too, hence always bypass glibc for this, and always cache things on our side. Fixes: #8113 (cherry picked from commit 996def17f99bb3f41f82032860dfcb98ff19c3ae)
* process-util: make our freeze() routine do something usefulMichal Sekletar2018-05-30
| | | | | | | | | | | | | When we crash we freeze() our-self (or possibly we reboot the machine if that is configured). However, calling pause() is very unhelpful thing to do. We should at least continue to do what init systems being doing since 70's and that is reaping zombies. Otherwise zombies start to accumulate on the system which is a very bad thing. As that can prevent admin from taking manual steps to reboot the machine in somewhat graceful manner (e.g. manually stopping services, unmounting data volumes and calling reboot -f). Fixes #7783
* util-lib: save/restore errno in cleanup callsLennart Poettering2018-05-30
| | | | | | | | | | | | | | We should be careful with errno in cleanup functions, and not alter it under any circumstances. In the safe_close cleanup handlers we are already safe in that regard, but let's add similar protections on other cleanup handlers that invoke system calls. Why bother? Cleanup handlers insert code at function return in non-obvious ways. Hence, code that sets errno and returns should not be confused by us overrding the errno from a cleanup handler. This is a paranoia fix only, I am not aware where this actually mattered in real-life situations.
* process-util: initialize block_signalsYu Watanabe2018-05-30
| | | | CID 1384240.
* process-util: add new FORK_NEW_MOUNTNS flag to safe_fork()Lennart Poettering2018-05-30
| | | | That way we can move one more code location to use safe_fork()
* process-spec: add another flag FORK_WAIT to safe_fork()Lennart Poettering2018-05-30
| | | | | | | | | This new flag will cause safe_fork() to wait for the forked off child before returning. This allows us to unify a number of cases where we immediately wait on the forked off child, witout running any code in the parent after the fork, and without direct interest in the precise exit status of the process, except recgonizing EXIT_SUCCESS vs everything else.
* process-util: rework wait_for_terminate_and_warn() to take a flags parameterLennart Poettering2018-05-30
| | | | | | | | | | | | | This renames wait_for_terminate_and_warn() to wait_for_terminate_and_check(), and adds a flags parameter, that controls how much to log: there's one flag that means we log about abnormal stuff, and another one that controls whether we log about non-zero exit codes. Finally, there's a shortcut flag value for logging in both cases, as that's what we usually use. All callers are accordingly updated. At three occasions duplicate logging is removed, i.e. where the old function was called but logged in the caller, too.
* process-util: add another fork_safe() flag for enabling LOG_ERR/LOG_WARN loggingLennart Poettering2018-05-30
|
* raw-clone: beef up raw_clone() wrapper a bitLennart Poettering2018-05-30
| | | | | | | | First of all, let's return pid_t, which appears to be the correct type given that we return PIDs, and it#s what fork() uses too. Most importantly though, flush out our PID cache, so that the call becomes compatible with our getpid_cached() logic.
* fix machinectl shell (in machined) (#7785)Shawn Landden2018-05-30
| | | | | | | | | | 4c253ed broke machined $machinectl shell arch Failed to get shell PTY: Input/output error Closes: #7779 v2: do not drop DEATHSIG flag
* process-util: debug log if PR_SET_NAME fails.Lennart Poettering2018-05-30
|
* process-util: allow rename_process() only in the main threadLennart Poettering2018-05-30
| | | | | | | | | We make assumptions about the comm name we set via PR_SET_NAME: that it would reflect the process name, but that's only the case for the main thread. Moreover, we cache the mmap() region without locking. Let's hence be safe rather than sorry and support all this only in the main thread.
* process-util: move fork_agent() to process-util.[ch]Lennart Poettering2018-05-30
| | | | | | It's a relatively small wrapper around safe_fork() now, hence let's move it over, and make its signature even more alike. Also, set a different process name for the polkit and askpw agents.
* 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.
* core: Implement timeout based umount/remount limitKyle Walker2018-05-30
| | | | | | | | | | | | | Remount, and subsequent umount, attempts can hang for inaccessible network based mount points. This can leave a system in a hard hang state that requires a hard reset in order to recover. This change moves the remount, and umount attempts into separate child processes. The remount and umount operations will block for up to 90 seconds (DEFAULT_TIMEOUT_USEC). Should those waits fail, the parent will issue a SIGKILL to the child and continue with the shutdown efforts. In addition, instead of only reporting some additional errors on the final attempt, failures are reported as they occur.
* basic: turn off stdio locking for a couple of helper callsLennart Poettering2018-05-30
| | | | | | These helper calls are potentially called often, and allocate FILE* objects internally for a very short period of time, let's turn off locking for them too.
* Prep v236: Update build system and adapt to eloginds needs.Sven Eden2018-03-13
|
* work-around usage of glibc-specific __register_atfork for musl systemsmaxice82018-01-24
| | | | | | | __register_atfork is glibc-specific but is roughly equivalent to pthread_atfork, add a definition of it on musl_missing.h and guard against the definition of __register_atfork on src/basic/process-util.c using #ifdef __GLIBC__
* Apply missing updates from upstreamSven Eden2017-12-08
|
* tree-wide: use IN_SET macro (#6977)Yu Watanabe2017-12-08
|
* build-sys: use #if Y instead of #ifdef Y everywhereZbigniew Jędrzejewski-Szmek2017-11-23
| | | | | | | | | | | | | | | The advantage is that is the name is mispellt, cpp will warn us. $ git grep -Ee "conf.set\('(HAVE|ENABLE)_" -l|xargs sed -r -i "s/conf.set\('(HAVE|ENABLE)_/conf.set10('\1_/" $ git grep -Ee '#ifn?def (HAVE|ENABLE)' -l|xargs sed -r -i 's/#ifdef (HAVE|ENABLE)/#if \1/; s/#ifndef (HAVE|ENABLE)/#if ! \1/;' $ git grep -Ee 'if.*defined\(HAVE' -l|xargs sed -i -r 's/defined\((HAVE_[A-Z0-9_]*)\)/\1/g' $ git grep -Ee 'if.*defined\(ENABLE' -l|xargs sed -i -r 's/defined\((ENABLE_[A-Z0-9_]*)\)/\1/g' + manual changes to meson.build squash! build-sys: use #if Y instead of #ifdef Y everywhere v2: - fix incorrect setting of HAVE_LIBIDN2
* Revert "tree-wide: use pid_is_valid() at more places"Alan Jenkins2017-10-03
| | | | | | | | | | | This reverts commit ee043777be58251e7441b4f04594e9e3792d7fb2. It broke almost everywhere it touched. The places that handn't been converted, were mostly followed by special handling for the invalid PID `0`. That explains why they tested for `pid < 0` instead of `pid <= 0`. I think that one was the first commit I reviewed, heh.
* tree-wide: use IN_SET where possibleAndreas Rammhold2017-09-29
| | | | | In addition to the changes from #6933 this handles cases that could be matched with the included cocci file.
* Link to the right glibc commit in comment (#6884)Zbigniew Jędrzejewski-Szmek2017-09-21
| | | | | | Reported by Marcos Mello. Fixes #6882.
* util-lib: wrap personality() to fix up broken glibc error handling (#6766)Lennart Poettering2017-09-08
| | | | | | | | | glibc appears to propagate different errors in different ways, let's fix this up, so that our own code doesn't get confused by this. See #6752 + #6737 for details. Fixes: #6755
* tree-wide: use pid_is_valid() at more placesLennart Poettering2017-08-31
|
* seccomp: default to something resembling the current personality when locking itLennart Poettering2017-08-09
| | | | | | | Let's lock the personality to the currently set one, if nothing is specifically specified. But do so with a grain of salt, and never default to any exotic personality here, but only PER_LINUX or PER_LINUX32.