summaryrefslogtreecommitdiffstats
path: root/src/systemctl/systemctl-whoami.c
blob: 607f2db047956710fbe89164248a7db7c875dd03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "format-util.h"
#include "parse-util.h"
#include "systemctl.h"
#include "systemctl-util.h"
#include "systemctl-whoami.h"

int verb_whoami(int argc, char *argv[], void *userdata) {
        sd_bus *bus;
        int r, ret = 0;

        r = acquire_bus(BUS_FULL, &bus);
        if (r < 0)
                return r;

        if (argc <= 1) {
                _cleanup_free_ char *unit = NULL;

                if (arg_transport != BUS_TRANSPORT_LOCAL)
                        return log_error_errno(SYNTHETIC_ERRNO(EREMOTE),
                                               "Refusing to look up our local PID on remote host.");

                /* Our own process can never go away while querying, hence no need to go through pidfd. */

                r = get_unit_by_pid(bus, 0, &unit, /* ret_path = */ NULL);
                if (r < 0)
                        return r;

                puts(unit);
                return 0;
        }

        STRV_FOREACH(pidstr, strv_skip(argv, 1)) {
                _cleanup_free_ char *unit = NULL;
                pid_t pid;

                r = parse_pid(*pidstr, &pid);
                if (r < 0)
                        return log_error_errno(r, "Invalid PID specified: %s", *pidstr);

                r = lookup_unit_by_pidref(bus, pid, &unit, /* ret_path = */ NULL);
                if (r < 0)
                        RET_GATHER(ret, r);
                else
                        puts(unit);
        }

        return ret;
}