1010#include "iovec-util.h"
1111#include "log.h"
1212#include "main-func.h"
13+ #include "path-lookup.h"
1314#include "socket-util.h"
1415#include "string-util.h"
1516#include "strv.h"
16- #include "varlink-util.h"
1717
1818static int process_vsock_cid (unsigned cid , const char * port ) {
1919 int r ;
@@ -135,23 +135,57 @@ static int process_vsock_mux(const char *path, const char *port) {
135135 return 0 ;
136136}
137137
138- static int process_machine (const char * machine , const char * port ) {
139- _cleanup_ (sd_varlink_unrefp ) sd_varlink * vl = NULL ;
138+ static int fetch_machine (const char * machine , RuntimeScope scope , sd_json_variant * * ret ) {
140139 int r ;
141140
142141 assert (machine );
143- assert (port );
142+ assert (ret );
144143
145- r = sd_varlink_connect_address (& vl , "/run/systemd/machine/io.systemd.Machine" );
144+ _cleanup_free_ char * addr = NULL ;
145+ r = runtime_directory_generic (scope , "machine/io.systemd.Machine" , & addr );
146146 if (r < 0 )
147- return log_error_errno (r , "Failed to connect to machined on /run/systemd/machine/io.systemd.Machine: %m" );
147+ return r ;
148+
149+ _cleanup_ (sd_varlink_unrefp ) sd_varlink * vl = NULL ;
150+ r = sd_varlink_connect_address (& vl , addr );
151+ if (r < 0 )
152+ return log_error_errno (r , "Failed to connect to machined on %s: %m" , addr );
148153
149154 _cleanup_ (sd_json_variant_unrefp ) sd_json_variant * result = NULL ;
150- r = varlink_callbo_and_log (
155+ const char * error_id ;
156+ r = sd_varlink_callbo (
151157 vl ,
152158 "io.systemd.Machine.List" ,
153159 & result ,
160+ & error_id ,
154161 SD_JSON_BUILD_PAIR ("name" , SD_JSON_BUILD_STRING (machine )));
162+ if (r < 0 )
163+ return log_error_errno (r , "Failed to issue io.systemd.Machine.List() varlink call: %m" );
164+ if (error_id ) {
165+ if (streq (error_id , "io.systemd.Machine.NoSuchMachine" ))
166+ return - ESRCH ;
167+
168+ r = sd_varlink_error_to_errno (error_id , result ); /* If this is a system errno style error, output it with %m */
169+ if (r != - EBADR )
170+ return log_error_errno (r , "Failed to issue io.systemd.Machine.List() varlink call: %m" );
171+
172+ return log_error_errno (r , "Failed to issue io.systemd.Machine.List() varlink call: %s" , error_id );
173+ }
174+
175+ * ret = TAKE_PTR (result );
176+ return 0 ;
177+ }
178+
179+ static int process_machine (const char * machine , const char * port ) {
180+ int r ;
181+
182+ assert (machine );
183+ assert (port );
184+
185+ _cleanup_ (sd_json_variant_unrefp ) sd_json_variant * result = NULL ;
186+ r = fetch_machine (machine , RUNTIME_SCOPE_USER , & result );
187+ if (r == - ESRCH )
188+ r = fetch_machine (machine , RUNTIME_SCOPE_SYSTEM , & result );
155189 if (r < 0 )
156190 return r ;
157191
@@ -167,7 +201,7 @@ static int process_machine(const char *machine, const char *port) {
167201 return log_error_errno (r , "Failed to parse Varlink reply: %m" );
168202
169203 if (cid == VMADDR_CID_ANY )
170- return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "Machine has no AF_VSOCK CID assigned." );
204+ return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "Machine %s has no AF_VSOCK CID assigned." , machine );
171205
172206 return process_vsock_cid (cid , port );
173207}
0 commit comments