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