2023-07-03 14:36:14 +02:00
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2023-11-25 22:10:16 +08:00
# include "format-util.h"
# include "parse-util.h"
2023-07-03 14:36:14 +02:00
# include "systemctl.h"
# include "systemctl-util.h"
# include "systemctl-whoami.h"
int verb_whoami ( int argc , char * argv [ ] , void * userdata ) {
sd_bus * bus ;
2024-02-24 09:01:22 +08:00
int r , ret = 0 ;
2023-07-03 14:36:14 +02:00
r = acquire_bus ( BUS_FULL , & bus ) ;
if ( r < 0 )
return r ;
2023-11-25 22:10:16 +08:00
if ( argc < = 1 ) {
_cleanup_free_ char * unit = NULL ;
2023-07-03 14:36:14 +02:00
if ( arg_transport ! = BUS_TRANSPORT_LOCAL )
2024-02-24 09:01:22 +08:00
return log_error_errno ( SYNTHETIC_ERRNO ( EREMOTE ) ,
" Refusing to look up our local PID on remote host. " ) ;
2023-07-03 14:36:14 +02:00
2024-02-24 09:01:22 +08:00
/* Our own process can never go away while querying, hence no need to go through pidfd. */
2023-11-25 22:10:16 +08:00
2024-02-24 09:01:22 +08:00
r = get_unit_by_pid ( bus , 0 , & unit , /* ret_path = */ NULL ) ;
2023-11-25 22:10:16 +08:00
if ( r < 0 )
return r ;
2023-07-03 14:36:14 +02:00
2023-11-25 22:10:16 +08:00
puts ( unit ) ;
return 0 ;
2023-07-03 14:36:14 +02:00
}
2023-11-25 22:10:16 +08:00
2024-02-24 09:01:22 +08:00
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 ) ;
2023-11-25 22:10:16 +08:00
2024-02-24 09:01:22 +08:00
r = lookup_unit_by_pidref ( bus , pid , & unit , /* ret_path = */ NULL ) ;
if ( r < 0 )
RET_GATHER ( ret , r ) ;
else
puts ( unit ) ;
}
2023-11-25 22:10:16 +08:00
2024-02-24 09:01:22 +08:00
return ret ;
2023-07-03 14:36:14 +02:00
}