]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemctl/systemctl-whoami.c
Merge pull request #31000 from flatcar-hub/krnowak/mutable-overlays
[thirdparty/systemd.git] / src / systemctl / systemctl-whoami.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "format-util.h"
4 #include "parse-util.h"
5 #include "systemctl.h"
6 #include "systemctl-util.h"
7 #include "systemctl-whoami.h"
8
9 int verb_whoami(int argc, char *argv[], void *userdata) {
10 sd_bus *bus;
11 int r, ret = 0;
12
13 r = acquire_bus(BUS_FULL, &bus);
14 if (r < 0)
15 return r;
16
17 if (argc <= 1) {
18 _cleanup_free_ char *unit = NULL;
19
20 if (arg_transport != BUS_TRANSPORT_LOCAL)
21 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE),
22 "Refusing to look up our local PID on remote host.");
23
24 /* Our own process can never go away while querying, hence no need to go through pidfd. */
25
26 r = get_unit_by_pid(bus, 0, &unit, /* ret_path = */ NULL);
27 if (r < 0)
28 return r;
29
30 puts(unit);
31 return 0;
32 }
33
34 STRV_FOREACH(pidstr, strv_skip(argv, 1)) {
35 _cleanup_free_ char *unit = NULL;
36 pid_t pid;
37
38 r = parse_pid(*pidstr, &pid);
39 if (r < 0)
40 return log_error_errno(r, "Invalid PID specified: %s", *pidstr);
41
42 r = lookup_unit_by_pidref(bus, pid, &unit, /* ret_path = */ NULL);
43 if (r < 0)
44 RET_GATHER(ret, r);
45 else
46 puts(unit);
47 }
48
49 return ret;
50 }