summaryrefslogtreecommitdiff
path: root/src/basic/process-util.c
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #12628 from keszybz/dbus-executeMichal Sekletar2019-05-301-37/+2
|\ | | | | Rework cpu affinity parsing
| * Move cpus_in_affinity_mask() to cpu-set-util.[ch]Zbigniew Jędrzejewski-Szmek2019-05-291-37/+2
| | | | | | | | | | It just seems to fit better there and it's always better to have things in shared/ rather than basic/.
* | Add 8bit-version of get_process_cmdline() and use in cgroup-show.cZbigniew Jędrzejewski-Szmek2019-05-221-1/+4
| | | | | | | | | | | | | | | | | | | | | | This restores show_pid_array() output in legacy locales on the console. Only one call to get_process_cmdline() is changed, all others retain utf8-only mode. This affects systemd-cgls, systemctl status, etc, when working locally. Calls to get_process_cmdline() that cross a process boundary always use utf8. It's the callers responsibility to convert this to some encoding that they use. This means that we always pass utf8 over the bus.
* | basic/process-util: convert bool arg to flagsZbigniew Jędrzejewski-Szmek2019-05-221-5/+6
| | | | | | | | In preparation for the next commit…
* | util-lib: do not truncate kernel comm namesZbigniew Jędrzejewski-Szmek2019-05-221-3/+8
| | | | | | | | | | | | | | | | | | | | | | It turns out that the kernel allows comm names higher than our expected limit of 16. $ wc -c /proc/*/comm|sort -g|tail -n3 35 /proc/1292317/comm 35 /proc/1293610/comm 36 /proc/1287112/comm $ cat /proc/1287112/comm kworker/u9:3-kcryptd/253:0
* | Rework cmdline printing to use unicodeZbigniew Jędrzejewski-Szmek2019-05-221-117/+47
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The functions to retrieve and print process cmdlines were based on the assumption that they contain printable ASCII, and everything else should be filtered out. That assumption doesn't hold in today's world, where people are free to use unicode everywhere. This replaces the custom cmdline reading code with a more generic approach using utf8_escape_non_printable_full(). For kernel threads, truncation is done on the parenthesized name, so we'll get "[worker]", "[worker…]", …, "[w…]", "[…", "…" as we reduce the number of available columns. This implementation is most likely slower for very long cmdlines, but I don't think this is very important. The common case is to have short commandlines, and should print those properly. Absurdly long cmdlines are the exception, which needs to be handled correctly and safely, but speed is not too important. Fixes #12532. v2: - use size_t for the number of columns. This change propagates into various other functions that call get_process_cmdline(), increasing the size of the patch, but the changes are rather trivial.
* | Introduce sc_arg_max() helperZbigniew Jędrzejewski-Szmek2019-05-211-5/+3
| | | | | | | | Just a cast and an assert.
* | small fixes: make get_process_state() static and fix typoTopi Miettinen2019-05-201-1/+1
|/
* Make fopen_temporary and fopen_temporary_label unlockedZbigniew Jędrzejewski-Szmek2019-04-121-1/+0
| | | | | | | | This is partially a refactoring, but also makes many more places use unlocked operations implicitly, i.e. all users of fopen_temporary(). AFAICT, the uses are always for short-lived files which are not shared externally, and are just used within the same context. Locking is not necessary.
* Add fopen_unlocked() wrapperZbigniew Jędrzejewski-Szmek2019-04-121-35/+21
|
* Merge pull request #12030 from poettering/condition-memoryYu Watanabe2019-04-021-0/+34
|\ | | | | add ConditionCPUs= + ConditionMemory=
| * process-util: add new helper cpus_in_affinity_mask()Lennart Poettering2019-03-191-0/+34
| | | | | | | | | | Let's add a new helper for determining how many CPUs are configured for us
* | process-util: introduce pid_is_my_child() helperFranck Bui2019-03-201-0/+14
|/ | | | No functional changes.
* Handle or voidify all calls to close_all_fds()Zbigniew Jędrzejewski-Szmek2019-03-151-1/+1
| | | | In activate, it is important that we close the fds. In other cases, meh.
* util: split out memcmp()/memset() related calls into memory-util.[ch]Lennart Poettering2019-03-131-1/+1
| | | | Just some source rearranging.
* util: split out namespace related stuff into a new namespace-util.[ch] pairLennart Poettering2019-03-131-0/+1
| | | | Just some minor reorganiztion.
* process-util: don't use overly large buffer to store process command lineMichal Sekletar2019-01-261-4/+14
| | | | | | | | Allocate new string as a return value and free our "scratch pad" buffer that is potentially much larger than needed (up to _SC_ARG_MAX). Fixes #11502
* basic/process-util: limit command line lengths to _SC_ARG_MAXZbigniew Jędrzejewski-Szmek2019-01-091-48/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This affects systemd-journald and systemd-coredump. Example entry: $ journalctl -o export -n1 'MESSAGE=Something logged' __CURSOR=s=976542d120c649f494471be317829ef9;i=34e;b=4871e4c474574ce4a462dfe3f1c37f06;m=c7d0c37dd2;t=57c4ac58f3b98;x=67598e942bd23dc0 __REALTIME_TIMESTAMP=1544035467475864 __MONOTONIC_TIMESTAMP=858200964562 _BOOT_ID=4871e4c474574ce4a462dfe3f1c37f06 PRIORITY=6 _UID=1000 _GID=1000 _CAP_EFFECTIVE=0 _SELINUX_CONTEXT=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 _AUDIT_SESSION=1 _AUDIT_LOGINUID=1000 _SYSTEMD_OWNER_UID=1000 _SYSTEMD_UNIT=user@1000.service _SYSTEMD_SLICE=user-1000.slice _SYSTEMD_USER_SLICE=-.slice _SYSTEMD_INVOCATION_ID=1c4a469986d448719cb0f9141a10810e _MACHINE_ID=08a5690a2eed47cf92ac0a5d2e3cf6b0 _HOSTNAME=krowka _TRANSPORT=syslog SYSLOG_FACILITY=17 SYSLOG_IDENTIFIER=syslog-caller MESSAGE=Something logged _COMM=poc _EXE=/home/zbyszek/src/systemd-work3/poc _SYSTEMD_CGROUP=/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service _SYSTEMD_USER_UNIT=gnome-terminal-server.service SYSLOG_PID=4108 SYSLOG_TIMESTAMP=Dec 5 19:44:27 _PID=4108 _CMDLINE=./poc AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA> _SOURCE_REALTIME_TIMESTAMP=1544035467475848 $ journalctl -o export -n1 'MESSAGE=Something logged' --output-fields=_CMDLINE|wc 6 2053 2097410 2MB might be hard for some clients to use meaningfully, but OTOH, it is important to log the full commandline sometimes. For example, when the program is crashing, the exact argument list is useful.
* tree-wide: use assert_se() for signal operations with constantsZbigniew Jędrzejewski-Szmek2018-12-211-11/+3
| | | | | | | | | | | | | | | | Continuation of a3ebe5eb620e49f0d24082876cafc7579261e64f: in other places we sometimes use assert_se(), and sometimes normal error handling. sigfillset and sigaddset can only fail if mask is NULL (which cannot happen if we are passing in a reference), or if the signal number is invalid (which really shouldn't happen when we are using a constant like SIGCHLD. If SIGCHLD is invalid, we have a bigger problem). So let's simplify things and always use assert_se() in those cases. In sigset_add_many() we could conceivably pass an invalid signal, so let's keep normal error handling here. The caller can do assert_se() around the sigprocmask_many() call if appropriate. '>= 0' is used for consistency with the rest of the codebase.
* process-util: rework getenv_for_pid() to use read_nul_string()Lennart Poettering2018-12-181-19/+16
|
* process-util: make get_process_environ() saferLennart Poettering2018-12-181-10/+17
| | | | Let's add a size limit, and let's use safe_fgetc().
* Merge pull request #10935 from poettering/rlimit-nofile-safeZbigniew Jędrzejewski-Szmek2018-12-061-0/+11
|\ | | | | | | Merged by hand to resolve a trivial conflict in TODO.
| * tree-wide: invoke rlimit_nofile_safe() before various exec{v,ve,l}() invocationsLennart Poettering2018-12-011-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Whenever we invoke external, foreign code from code that has RLIMIT_NOFILE's soft limit bumped to high values, revert it to 1024 first. This is a safety precaution for compatibility with programs using select() which cannot operate with fds > 1024. This commit adds the call to rlimit_nofile_safe() to all invocations of exec{v,ve,l}() and friends that either are in code that we know runs with RLIMIT_NOFILE bumped up (which is PID 1 and all journal code for starters) or that is part of shared code that might end up there. The calls are placed as early as we can in processes invoking a flavour of execve(), but after the last time we do fd manipulations, so that we can still take benefit of the high fd limits for that.
| * process-util: add new FORK_RLIMIT_NOFILE_SAFE flag for safe_fork()Lennart Poettering2018-12-011-0/+9
| | | | | | | | | | The new flag simply means rlimit_nofile_safe() is called in the child after all fds are rearranged.
* | tree-wide: use gcc attribute macros where appropriateLennart Poettering2018-12-031-1/+1
|/ | | | We have these macros already, hence use them.
* coccinelle: make use of SYNTHETIC_ERRNOZbigniew Jędrzejewski-Szmek2018-11-221-2/+1
| | | | | | | | | | | Ideally, coccinelle would strip unnecessary braces too. But I do not see any option in coccinelle for this, so instead, I edited the patch text using search&replace to remove the braces. Unfortunately this is not fully automatic, in particular it didn't deal well with if-else-if-else blocks and ifdefs, so there is an increased likelikehood be some bugs in such spots. I also removed part of the patch that coccinelle generated for udev, where we returns -1 for failure. This should be fixed independently.
* process-util: check for correct kill return value (#10841)David Leeds2018-11-201-2/+2
| | | Code was not doing a wait() after kill() due to checking for a return value > 0, and was leaving zombie processes. This affected things like sd-bus unixexec connections.
* Merge pull request #10753 from keszybz/pager-no-interruptLennart Poettering2018-11-141-1/+0
|\ | | | | Add mode in journalctl where ^C is handled by the pager
| * Move LONG_LINE_MAX definition to fileio.hZbigniew Jędrzejewski-Szmek2018-11-141-1/+0
| | | | | | | | | | | | | | | | | | | | | | All users of the macro (except for one, in serialize.c), use the macro in connection with read_line(), so they must include fileio.h. Let's not play libc games and require multiple header file to be included for the most common use of a function. The removal of def.h includes is not exact. I mostly went over the commits that switch over to use read_line() and add def.h at the same time and reverted the addition of def.h in those files.
* | core: add namespace_fork() helper, that forks, joins a set of namespaces and ↵Lennart Poettering2018-11-131-0/+54
|/ | | | | | | | | forks again This helper is useful to ensure pidns/userns joining is properly executed (as that requires a fork after the setns()). This is particularly important when it comes to /proc/self/ access or SCM_CREDENTIALS, but is generally the safer mode of operation.
* process-util: FOREACH_LINE excorcismLennart Poettering2018-10-181-2/+10
|
* * hack around deficiencies in prctl() PR_SET_MM_*Frank Schaefer2018-10-051-8/+26
|
* trivial: fix spelling in code commentsThomas Haller2018-09-301-1/+1
| | | | Based-on-patch-by: Rafael Fontenelle <rafaelff@gnome.org>
* tree-wide: use typesafe_qsort()Yu Watanabe2018-09-191-4/+2
|
* tree-wide: Convert compare_func's to use CMP() macro wherever possible.Filipe Brandenburger2018-08-061-6/+1
| | | | | | | | Looked for definitions of functions using the *_compare_func() suffix. Tested: - Unit tests passed (ninja -C build/ test) - Installed this build and booted with it.
* process-util: do not hide global variableYu Watanabe2018-07-171-1/+1
| | | | Suggested by LGTM.
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-06-141-3/+0
| | | | | | | | | | | 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-06-141-2/+0
| | | | | | | | | | | | | | | | 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-06-061-6/+1
|
* Add macro for checking if some flags are setZbigniew Jędrzejewski-Szmek2018-06-041-1/+1
| | | | | | | | | 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-06-011-4/+15
| | | | | | | | | | | | 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-06-011-1/+1
| | | | | 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-06-011-6/+9
| | | | called with SIGCHLD blocked
* process-util: add a new FORK_MOUNTNS_SLAVE flag for safe_fork()Lennart Poettering2018-05-241-0/+11
| | | | | | | | 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-05-171-0/+9
| | | | And let's make use of it in execute.c
* tree-wide: use newa() rather than alloca() where we canLennart Poettering2018-04-271-1/+1
|
* tree-wide: be more careful with the type of array sizesLennart Poettering2018-04-271-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-04-061-13/+0
| | | | | | | | | | 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-03-221-2/+1
| | | | | | | | | | | | | | | | 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)
* Merge pull request #8461 from keszybz/oss-fuzz-fixesEvgeny Vereshchagin2018-03-191-1/+1
|\ | | | | Oss fuzz fixes