@@ -2955,18 +2955,70 @@ static int show_one(const char *verb, DBusConnection *bus, const char *path, boo
29552955 return r ;
29562956}
29572957
2958- static int show ( DBusConnection * bus , char * * args ) {
2958+ static int show_one_by_pid ( const char * verb , DBusConnection * bus , uint32_t pid , bool * new_line ) {
29592959 DBusMessage * m = NULL , * reply = NULL ;
2960- int r , ret = 0 ;
2960+ const char * path = NULL ;
29612961 DBusError error ;
2962+ int r ;
2963+
2964+ dbus_error_init (& error );
2965+
2966+ m = dbus_message_new_method_call (
2967+ "org.freedesktop.systemd1" ,
2968+ "/org/freedesktop/systemd1" ,
2969+ "org.freedesktop.systemd1.Manager" ,
2970+ "GetUnitByPID" );
2971+ if (!m ) {
2972+ log_error ("Could not allocate message." );
2973+ r = - ENOMEM ;
2974+ goto finish ;
2975+ }
2976+
2977+ if (!dbus_message_append_args (m ,
2978+ DBUS_TYPE_UINT32 , & pid ,
2979+ DBUS_TYPE_INVALID )) {
2980+ log_error ("Could not append arguments to message." );
2981+ r = - ENOMEM ;
2982+ goto finish ;
2983+ }
2984+
2985+ reply = dbus_connection_send_with_reply_and_block (bus , m , -1 , & error );
2986+ if (!reply ) {
2987+ log_error ("Failed to issue method call: %s" , bus_error_message (& error ));
2988+ r = - EIO ;
2989+ goto finish ;
2990+ }
2991+
2992+ if (!dbus_message_get_args (reply , & error ,
2993+ DBUS_TYPE_OBJECT_PATH , & path ,
2994+ DBUS_TYPE_INVALID )) {
2995+ log_error ("Failed to parse reply: %s" , bus_error_message (& error ));
2996+ r = - EIO ;
2997+ goto finish ;
2998+ }
2999+
3000+ r = show_one (verb , bus , path , false, new_line );
3001+
3002+ finish :
3003+ if (m )
3004+ dbus_message_unref (m );
3005+
3006+ if (reply )
3007+ dbus_message_unref (reply );
3008+
3009+ dbus_error_free (& error );
3010+
3011+ return r ;
3012+ }
3013+
3014+ static int show (DBusConnection * bus , char * * args ) {
3015+ int r , ret = 0 ;
29623016 bool show_properties , new_line = false;
29633017 char * * name ;
29643018
29653019 assert (bus );
29663020 assert (args );
29673021
2968- dbus_error_init (& error );
2969-
29703022 show_properties = !streq (args [0 ], "status" );
29713023
29723024 if (show_properties )
@@ -2976,157 +3028,55 @@ static int show(DBusConnection *bus, char **args) {
29763028 /* If not argument is specified inspect the manager
29773029 * itself */
29783030
2979- ret = show_one (args [0 ], bus , "/org/freedesktop/systemd1" , show_properties , & new_line );
2980- goto finish ;
3031+ return show_one (args [0 ], bus , "/org/freedesktop/systemd1" , show_properties , & new_line );
29813032 }
29823033
29833034 STRV_FOREACH (name , args + 1 ) {
2984- const char * path = NULL ;
29853035 uint32_t id ;
29863036
29873037 if (safe_atou32 (* name , & id ) < 0 ) {
29883038
29893039 /* Interpret as unit name */
29903040
2991- if (!(m = dbus_message_new_method_call (
2992- "org.freedesktop.systemd1" ,
2993- "/org/freedesktop/systemd1" ,
2994- "org.freedesktop.systemd1.Manager" ,
2995- "LoadUnit" ))) {
2996- log_error ("Could not allocate message." );
2997- ret = - ENOMEM ;
2998- goto finish ;
2999- }
3000-
3001- if (!dbus_message_append_args (m ,
3002- DBUS_TYPE_STRING , name ,
3003- DBUS_TYPE_INVALID )) {
3004- log_error ("Could not append arguments to message." );
3005- ret = - ENOMEM ;
3006- goto finish ;
3007- }
3008-
3009- if (!(reply = dbus_connection_send_with_reply_and_block (bus , m , -1 , & error ))) {
3010-
3011- if (!dbus_error_has_name (& error , DBUS_ERROR_ACCESS_DENIED )) {
3012- log_error ("Failed to issue method call: %s" , bus_error_message (& error ));
3013- ret = - EIO ;
3014- goto finish ;
3015- }
3016-
3017- dbus_error_free (& error );
3018-
3019- dbus_message_unref (m );
3020- if (!(m = dbus_message_new_method_call (
3021- "org.freedesktop.systemd1" ,
3022- "/org/freedesktop/systemd1" ,
3023- "org.freedesktop.systemd1.Manager" ,
3024- "GetUnit" ))) {
3025- log_error ("Could not allocate message." );
3026- ret = - ENOMEM ;
3027- goto finish ;
3028- }
3041+ char * e , * p ;
3042+ e = bus_path_escape (* name );
3043+ if (!e )
3044+ return - ENOMEM ;
3045+ p = strappend ("/org/freedesktop/systemd1/unit/" , e );
3046+ free (e );
3047+ if (!p )
3048+ return - ENOMEM ;
30293049
3030- if (!dbus_message_append_args (m ,
3031- DBUS_TYPE_STRING , name ,
3032- DBUS_TYPE_INVALID )) {
3033- log_error ("Could not append arguments to message." );
3034- ret = - ENOMEM ;
3035- goto finish ;
3036- }
3050+ r = show_one (args [0 ], bus , p , show_properties , & new_line );
3051+ free (p );
30373052
3038- if (!(reply = dbus_connection_send_with_reply_and_block (bus , m , -1 , & error ))) {
3039- log_error ("Failed to issue method call: %s" , bus_error_message (& error ));
3040-
3041- if (dbus_error_has_name (& error , BUS_ERROR_NO_SUCH_UNIT ))
3042- ret = 4 ; /* According to LSB: "program or service status is unknown" */
3043- else
3044- ret = - EIO ;
3045- goto finish ;
3046- }
3047- }
3053+ if (r != 0 )
3054+ ret = r ;
30483055
30493056 } else if (show_properties ) {
30503057
30513058 /* Interpret as job id */
30523059
3053- if (!(m = dbus_message_new_method_call (
3054- "org.freedesktop.systemd1" ,
3055- "/org/freedesktop/systemd1" ,
3056- "org.freedesktop.systemd1.Manager" ,
3057- "GetJob" ))) {
3058- log_error ("Could not allocate message." );
3059- ret = - ENOMEM ;
3060- goto finish ;
3061- }
3060+ char * p ;
3061+ if (asprintf (& p , "/org/freedesktop/systemd1/job/%u" , id ) < 0 )
3062+ return - ENOMEM ;
30623063
3063- if (!dbus_message_append_args (m ,
3064- DBUS_TYPE_UINT32 , & id ,
3065- DBUS_TYPE_INVALID )) {
3066- log_error ("Could not append arguments to message." );
3067- ret = - ENOMEM ;
3068- goto finish ;
3069- }
3064+ r = show_one (args [0 ], bus , p , show_properties , & new_line );
3065+ free (p );
3066+
3067+ if (r != 0 )
3068+ ret = r ;
30703069
3071- if (!(reply = dbus_connection_send_with_reply_and_block (bus , m , -1 , & error ))) {
3072- log_error ("Failed to issue method call: %s" , bus_error_message (& error ));
3073- ret = - EIO ;
3074- goto finish ;
3075- }
30763070 } else {
30773071
30783072 /* Interpret as PID */
30793073
3080- if (!(m = dbus_message_new_method_call (
3081- "org.freedesktop.systemd1" ,
3082- "/org/freedesktop/systemd1" ,
3083- "org.freedesktop.systemd1.Manager" ,
3084- "GetUnitByPID" ))) {
3085- log_error ("Could not allocate message." );
3086- ret = - ENOMEM ;
3087- goto finish ;
3088- }
3089-
3090- if (!dbus_message_append_args (m ,
3091- DBUS_TYPE_UINT32 , & id ,
3092- DBUS_TYPE_INVALID )) {
3093- log_error ("Could not append arguments to message." );
3094- ret = - ENOMEM ;
3095- goto finish ;
3096- }
3097-
3098- if (!(reply = dbus_connection_send_with_reply_and_block (bus , m , -1 , & error ))) {
3099- log_error ("Failed to issue method call: %s" , bus_error_message (& error ));
3100- ret = - EIO ;
3101- goto finish ;
3102- }
3074+ r = show_one_by_pid (args [0 ], bus , id , & new_line );
3075+ if (r != 0 )
3076+ ret = r ;
31033077 }
3104-
3105- if (!dbus_message_get_args (reply , & error ,
3106- DBUS_TYPE_OBJECT_PATH , & path ,
3107- DBUS_TYPE_INVALID )) {
3108- log_error ("Failed to parse reply: %s" , bus_error_message (& error ));
3109- ret = - EIO ;
3110- goto finish ;
3111- }
3112-
3113- if ((r = show_one (args [0 ], bus , path , show_properties , & new_line )) != 0 )
3114- ret = r ;
3115-
3116- dbus_message_unref (m );
3117- dbus_message_unref (reply );
3118- m = reply = NULL ;
31193078 }
31203079
3121- finish :
3122- if (m )
3123- dbus_message_unref (m );
3124-
3125- if (reply )
3126- dbus_message_unref (reply );
3127-
3128- dbus_error_free (& error );
3129-
31303080 return ret ;
31313081}
31323082
0 commit comments