Commit Graph

244 Commits

Author SHA1 Message Date
Lennart Poettering
d4ffb37bb1 nspawn, vmspawn, run0: add env var for turning off background tinting
Some people are just sad, sad lost souls who don't like even the tiniest
ray of color in their life. Let's add an env var knob for allowing them
to turn the background tinting off, to drive the last bit of color from
their life so that they can stay in their grey grey life.
2024-05-02 23:33:39 +02:00
Daan De Meyer
aa976d8788 tree-wide: Use log_setup() everywhere
Otherwise the default log target is the console and we won't use
the journal socket even if it is available.
2024-04-25 17:06:43 +02:00
Yu Watanabe
90db1582a9 run/run: fix invocation ID handling
Fixes a regression introduced by 8157cc0e3e.
2024-04-25 10:58:56 +02:00
Luca Boccassi
9dd174dc23 run: query for SoftRebootsCount only for system scope runs
Only the system manager records soft reboots, and the user session is
restarted anyway so it doesn't suffer from the ID clash issue

Follow-up for ed35851693
2024-04-07 10:20:04 +01:00
Luca Boccassi
ed35851693 run: fix generated unit name clash after soft-reboot
When sd-run connects to D-Bus rather than the private socket, it will
generate the transient unit name using the bus ID assigned by the D-Bus
broker/daemon. The issue is that this ID is only unique per D-Bus run,
if the broker/daemon restarts it starts again from 1, and it's a simple
incremental counter for each client.
So if a transient unit run-u6.service starts and fails, and it is not
collected (default on failure), and the system soft-reboots, any new
transient unit might conflict as the counter will restart:

Failed to start transient service unit: Unit run-u6.service was already loaded or has a fragment file.

Get the soft-reboot counter, and if it's greater than zero, append it
to the autogenerated unit name to avoid clashes.
2024-03-28 11:19:46 +09:00
Zbigniew Jędrzejewski-Szmek
c38e4e2fda Merge pull request #29721 from poettering/systemd-project
New capsule@.service feature
2024-03-26 13:19:33 +01:00
Daan De Meyer
66b574b0a2 tree-wide: Add allow_pidfd argument to bus_append_scope_pidref() 2024-03-22 17:03:35 +01:00
Zbigniew Jędrzejewski-Szmek
7aed434371 Rename uid0 to run0
Naming is always a matter of preference, and the old name would certainly work,
but I think the new one has the following advantages:
- A verb is better than a noun.
- The name more similar to "the competition", i.e. 'sudo', 'pkexec', 'runas',
  'doas', which generally include an action verb.
- The connection between 'systemd-run' and 'run0' is more obvious.

There has been no release yet with the old name, so we can rename without
caring for backwards compatibility.
2024-03-19 16:37:22 +01:00
Lennart Poettering
759b3c082d run: allow connecting to capsule instances with --capsule=/-C 2024-03-14 11:34:04 +01:00
Lennart Poettering
8157cc0e3e bus-util: add generic parser for extracting id128 values from bus messages 2024-03-01 22:37:38 +01:00
Lennart Poettering
2537407075 run: use sd_event_set_signal_exit() at one more place 2024-02-26 11:53:31 +01:00
Lennart Poettering
d4ece77f5e ptyfwd: optionally prefix window title with colored dot
in uid0/systemd-run/nspawn we already set a window title with a colorful
unicode dot indicating the changed privileges/execution context. This typically
gets overriden by the shell inside the environment however.

Let's tweak this a bit: when we see the window title OSC ANSI sequence
passing through, let's patch in the unicode dot as a prefix to the
title.

This is super pretty, since it makes sure root sessions via 0ad are
really easily recognizable as such, because the window title carries an
🔴 red dot as prefix then.
2024-02-23 21:15:25 +00:00
Lennart Poettering
db7136ec73 signal-util: imply sentinel -1 in sigprocmask_many() + sigset_add_many() args list 2024-02-23 09:35:12 -08:00
Lennart Poettering
8496b938ab run: set window title
In interactive terminal mode, let's set a window title that reflects our
change of context to the target. Let's prefix it it with red/yellow
emoji dot in case we changed privileges.
2024-02-09 18:27:01 +01:00
Lennart Poettering
3ef072ee26 pretty-print: split out color tinting into a helper of its own 2024-01-23 16:45:37 +01:00
Lennart Poettering
87da1af78f run: the --background= switch expects an argument
We got this right when running as "uid0", but wrong when invoked under
"systemd-run". Let's fix that.
2024-01-20 01:10:46 +00:00
Mike Yuan
e7cbb4a137 run: strjoina is used, not sprintf
Follow-up for 43ead5e3d6
2024-01-17 16:10:37 +00:00
Mike Yuan
5447338b95 run: don't show IP/IO accounting data if 0 2024-01-12 17:35:21 +08:00
Mike Yuan
43ead5e3d6 run: show accounting data of same type in the same line 2024-01-12 16:58:11 +08:00
Antonio Alvarez Feijoo
80a8dbb4ea run: use SPECIAL_USER_SLICE 2024-01-04 15:22:54 +01:00
Lennart Poettering
2118b179ca man: try to improve wording on --slice-inherit docs 2023-12-21 19:15:01 +01:00
Lennart Poettering
2f0b4d578b run/uid0: tint the terminal background color (and add new --background= switch)
This adds a new --background= switch that allows specifiying a
background color for the terminal while the tool runs.

It also teaches the tool when invoked as uid0 to tint the terminal in a
reddish hue when operating as root, and in a yellowish hue when
operating as any other user.

This should highlight nicely when the user is operating with elevated
privileges, or changed privileges.
2023-12-21 19:15:01 +01:00
Lennart Poettering
1072d94731 run: optionally set the "ignore-failure" flag for ExecStart= lines 2023-12-21 19:14:43 +01:00
Lennart Poettering
72eb3081b2 run: when invoked as "uid0", expose some sudo-like behaviour
This turns "systemd-run" into a multi-call binary. When invoked under
the name "uid0", then it behaves a bit more like traditional "sudo".
This mostly means defaults appropriuate for that, for example a PAM
stack, interactivity and more.

Fixes: #29199
2023-12-21 19:14:43 +01:00
Yu Watanabe
760e7a3cb1 Merge pull request #30537 from poettering/run-arg-service-type-fix
run: arg_service_type is not supposed to be allocated from heap
2023-12-20 23:46:11 +09:00
Lennart Poettering
e132be4642 run: underline section titles in --help as we usually do 2023-12-20 12:14:24 +01:00
Lennart Poettering
4b5baf7514 run: arg_service_type is not supposed to be allocated from heap 2023-12-20 12:13:18 +01:00
Matteo Croce
e22ad53d5c dbus-wait-for-jobs: change 'quiet' flag to enum
Change the 'quiet' flag to `bus_wait_for_jobs()` to an enum, so we can
select with more granularity the type of information logged.
2023-12-19 04:52:41 -08:00
Zbigniew Jędrzejewski-Szmek
9ce3440a6f run: fix bad escaping and memory ownership confusion
arg_description was either set to arg_unit (i.e. a const char*), or to
char *description, the result of allocation in run(). But description
was decorated with _cleanup_, so it would be freed when going out of the
function. Nothing bad would happen, because the program would exit after
exiting from run(), but this is just all too messy.

Also, strv_join(" ") + shell_escape() is not a good way to escape command
lines. In particular, one the join has happened, we cannot distinguish
empty arguments, or arguments with whitespace, etc. We have a helper
function to do the escaping properly, so let's use that.

Fixup for 2c29813da3.
2023-12-02 09:47:45 +01:00
Zbigniew Jędrzejewski-Szmek
cffa43255a run: adjust indentation 2023-12-02 09:47:45 +01:00
Yu Watanabe
2c29813da3 run: escape command for description
The command arguments may contain spurious characters, e.g. line-break.
When we use command arguments as a description of a unit, we should
escape them.

Fixes #30187.
2023-11-25 12:52:35 +09:00
Florian Schmaus
aac3384e56 cgroup: add support for memory.swap.peak 2023-11-11 12:14:07 +01:00
Florian Schmaus
324ec6b5d2 run: include peak memory in output
Fixes #28542.

Signed-off-by: Florian Schmaus <flo@geekplace.eu>
2023-11-06 18:10:06 +01:00
Lennart Poettering
49a510eba2 run: pin the unit we invoke continously while we are running
We read properties of the unit, hence it shouldn't be GC'ed as long as
we run. Hence, let's just set AddRef unconditionally for the units we
create.
2023-10-16 14:30:11 +02:00
Lennart Poettering
7693661a84 run: output invocation ID when starting service and scope units 2023-10-16 14:30:11 +02:00
Lennart Poettering
f2c96f0fd3 run: support --scope on old service managers that lack native PIDFD support
Before this we'd fail with a complaint that PIDFDs is not supported by
the service manager. Add some compat support by falling back to classic
numeric PIDs in that case.
2023-10-13 23:16:52 +01:00
Lennart Poettering
7eda208ffe tree-wide: prefer sending pifds over pids when creating scope units 2023-10-05 17:10:00 +02:00
Yu Watanabe
5c707c8cdb meson: move declarations of busctl, stdio-bridge, and run 2023-08-01 21:37:31 +09:00
Luca Boccassi
8167c56bfa run: disable --expand-environment by default for --scope
The intention was to have this option enabled by default everywhere,
but unfortunately at least one case was found where it breaks
compatibility of a program using systemd-run --scopes and expecting
variables not to be expanded:

https://sources.debian.org/src/pbuilder/0.231/pbuilder-checkparams/#L400

Example run:

systemd-run --quiet --scope --description=pbuilder_build_xfce4-notes-plugin_1.10.0-1.dsc '--slice=system-pbuilder-build-xfce4\x2dnotes\x2dplugin_1.10.0\x2d1-449932.slice' chroot /var/cache/pbuilder/build/449932 dpkg-query -W '--showformat=${Version}' apt

Restore backward compatibility and make the option disabled by default
when --scope is used, and enabled by default for other types.

In case --expand-environment is not specified and a '$' character is
detected, print a warning to nudge users toward specifying the
parameter as needed. In the future we can then flip the default.

Follow-up for 2ed7a221fa
2023-07-20 17:37:27 +01:00
Lennart Poettering
f331434d13 env-file: when resolving env vars in command lines, collect list of unset/invalid ones
When resolving environment variables we currently silently resolve unset
and invalid environment variables to empty strings. Let's do this
slightly less silently: log about unset and invalid env vars, but still
resolve them to an empty string.

Fixes: #27036
2023-06-27 13:49:46 +02:00
Zbigniew Jędrzejewski-Szmek
f3f8bd6a38 various: fix error message for bus_wait_for_jobs_new()
As pointed out by Mike Yuan in review of grandparent commit,
bus_wait_for_jobs_new() can also fail for non-oom reasons.
2023-05-26 15:09:03 +02:00
Arthur Shau
9107ef5637 run: change sd_bus_call_method to bus_call_method (#27518)
Migrates the sd_bus_call_method code in run.c to use bus-locator.h api
2023-05-04 09:41:10 +08:00
Zbigniew Jędrzejewski-Szmek
208a59c15f Merge pull request #27113 from keszybz/variable-expansion-rework
Rework serialization of command lines in pid1 and make run not expand variables
2023-04-24 22:03:06 +02:00
Zbigniew Jędrzejewski-Szmek
2ed7a221fa run: expand variables also with --scope
This makes syntax be the same for commands which are started by the manager and
those which are spawned directly (when --scope is used).

Before:
$ systemd-run -q -t echo '$TERM'
xterm-256color

$ systemd-run -q --scope echo '$TERM'
$TERM

Now:
$ systemd-run -q --scope echo '$TERM'
xterm-256color

Previous behaviour can be restored via --expand-environment=no:
$ systemd-run -q --scope --expand-environment=no echo '$TERM'
$TERM

Fixes #22948.

At some level, this is a compat break. Fortunately --scope is not very widely
used, so I think we can get away with this. Having different syntax depending
on whether --scope was used or not was bad UX.

A NEWS entry will be required.
2023-04-24 10:02:30 +02:00
Zbigniew Jędrzejewski-Szmek
f872ddd182 run: add --expand-environment=no to disable server-side envvar expansion
This uses StartExecEx to get the equivalent of ExecStart=:. StartExecEx was
added in b3d593673c, so this will not work with
older systemds.

A hint is emitted if we get an error indicating lack of support. PID1 returns
SD_BUS_ERROR_PROPERTY_READ_ONLY, but I'm checking for
SD_BUS_ERROR_UNKNOWN_PROPERTY too for safety.
2023-04-24 10:02:30 +02:00
Zbigniew Jędrzejewski-Szmek
b58026bddc run: split out creation of unit creation messages
Just refactoring, in preparation for future changes.
(Though I think it'd be reasonable to do anyway, those functions were
awfully long.)

'git diff' displays this badly. The middle part of start_transient_service()
is moved to make_transient_service_unit(), and the middle part of
start_transient_trigger() is moved to make_transient_trigger_unit().
2023-04-24 10:02:30 +02:00
Zbigniew Jędrzejewski-Szmek
ac9a75d05e run: simplify returning of status
start_transient_service() would return two ints: one normally and one via
*retval. We can just return one int and propagate it directly, because we
use DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE().
2023-04-24 10:02:30 +02:00
Yu Watanabe
ef9c12b157 tree-wide: reset optind to 0 when GNU extensions in optstring are used
Otherwise, if getopt() and friends are used before parse_argv(), then
the GNU extensions may be ignored.

This should not change any behavior at least now, as we usually use
getopt_long() only once per invocation. But in the next commit,
getopt_long() will be used for other arrays, hence this change will
become necessary.
2023-03-29 10:34:41 +09:00
Lennart Poettering
4870133bfa basic: add RuntimeScope enum
In various tools and services we have a per-system and per-user concept.
So far we sometimes used a boolean indicating whether we are in system
mode, or a reversed boolean indicating whether we are in user mode, or
the LookupScope enum used by the lookup path logic.

Let's address that, in introduce a common enum for this, we can use all
across the board.

This is mostly just search/replace, no actual code changes.
2023-03-10 09:47:39 +01:00
Yu Watanabe
5bb1d7fbab tree-wide: use -EBADF more 2022-12-21 01:50:33 +09:00