Skip to content

Commit f5faf4e

Browse files
poetteringbluca
authored andcommitted
varlinkctl: when operating in --more mode, fail correcly on Varlink method error
In varlink.c we generally do not make failing callback functions fatal, since that should be up to the app. Hence, in case of varlinkctl (where we want failures to be fatal), make sure to propagate the error back explicitly. Before this change a failing call to "varlinkctl --more call …" would result in a zero exit code. With this it will correctly exit with a non-zero exit code.
1 parent a925620 commit f5faf4e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/varlinkctl/varlinkctl.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,15 @@ static int reply_callback(
350350
VarlinkReplyFlags flags,
351351
void *userdata) {
352352

353-
int r;
353+
int *ret = ASSERT_PTR(userdata), r;
354354

355355
assert(link);
356356

357357
if (error) {
358358
/* Propagate the error we received via sd_notify() */
359359
(void) sd_notifyf(/* unset_environment= */ false, "VARLINKERROR=%s", error);
360360

361-
r = log_error_errno(SYNTHETIC_ERRNO(EBADE), "Method call failed: %s", error);
361+
r = *ret = log_error_errno(SYNTHETIC_ERRNO(EBADE), "Method call failed: %s", error);
362362
} else
363363
r = 0;
364364

@@ -432,7 +432,8 @@ static int verb_call(int argc, char *argv[], void *userdata) {
432432

433433
} else if (arg_method_flags & VARLINK_METHOD_MORE) {
434434

435-
varlink_set_userdata(vl, (void*) method);
435+
int ret = 0;
436+
varlink_set_userdata(vl, &ret);
436437

437438
r = varlink_bind_reply(vl, reply_callback);
438439
if (r < 0)
@@ -459,6 +460,8 @@ static int verb_call(int argc, char *argv[], void *userdata) {
459460
if (r < 0)
460461
return log_error_errno(r, "Failed to wait for varlink connection events: %m");
461462
}
463+
464+
return ret;
462465
} else {
463466
JsonVariant *reply = NULL;
464467
const char *error = NULL;

test/units/TEST-74-AUX-UTILS.varlinkctl.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ if command -v userdbctl >/dev/null; then
3232
systemctl start systemd-userdbd
3333
varlinkctl call /run/systemd/userdb/io.systemd.Multiplexer io.systemd.UserDatabase.GetUserRecord '{ "userName" : "testuser", "service" : "io.systemd.Multiplexer" }'
3434
varlinkctl call -j /run/systemd/userdb/io.systemd.Multiplexer io.systemd.UserDatabase.GetUserRecord '{ "userName" : "testuser", "service" : "io.systemd.Multiplexer" }' | jq .
35-
varlinkctl call --more /run/systemd/userdb/io.systemd.Multiplexer io.systemd.UserDatabase.GetMemberships '{ "service" : "io.systemd.Multiplexer" }'
36-
varlinkctl call --more -j /run/systemd/userdb/io.systemd.Multiplexer io.systemd.UserDatabase.GetMemberships '{ "service" : "io.systemd.Multiplexer" }' | jq --seq .
35+
# We ignore the return value of the following two calls, since if no memberships are defined at all this will return a NotFound error, which is OK
36+
(varlinkctl call --more /run/systemd/userdb/io.systemd.Multiplexer io.systemd.UserDatabase.GetMemberships '{ "service" : "io.systemd.Multiplexer" }' ||:)
37+
(varlinkctl call --more -j /run/systemd/userdb/io.systemd.Multiplexer io.systemd.UserDatabase.GetMemberships '{ "service" : "io.systemd.Multiplexer" }' ||:) | jq --seq .
3738
varlinkctl call --oneway /run/systemd/userdb/io.systemd.Multiplexer io.systemd.UserDatabase.GetMemberships '{ "service" : "io.systemd.Multiplexer" }'
3839
(! varlinkctl call --oneway /run/systemd/userdb/io.systemd.Multiplexer io.systemd.UserDatabase.GetMemberships '{ "service" : "io.systemd.Multiplexer" }' | grep .)
3940
fi

0 commit comments

Comments
 (0)