switch-root: NACK invalid requests instead of false success

initctl switch-root always exits 0, no matter what happens on the
finit side.  do_switch_root_api() sends the ACK before switch_root()
has validated anything, so any failure after that point never reaches
the client -- the error only shows up in the log.  The runlevel guard
in api_cb() has the same problem: it rejects the request but still
sends an ACK.

Split the validation out of switch_root() into switch_root_precheck()
and run it before the ACK.  A failed check now sends a NACK with the
error message, which initctl prints before exiting 1:

    initctl switch-root /mnt
    switch-root: /mnt is not a mount point

The ACK is only sent once the precheck passes, since after that point
we are committed.  On success finit execs the new init and the
connection dies with no reply at all, so initctl treats only an
explicit NACK as failure.

Signed-off-by: Paweł Sobczak <github@fixeq.qzz.io>
This commit is contained in:
Paweł Sobczak authored and Joachim Wiberg committed 2026-08-16 23:17:57 +02:00
1 parent 8e2492fc87
commit 42840f0f36
4 files changed
+130 -69

No files matched your search

+7 -3
View File
@@ -1161,10 +1161,14 @@ int do_switch_root(int argc, char *argv[])
printf(" ...\n");
/*
* On success, finit exec's new init and we lose connection.
* A "failure" to read reply is actually expected on success.
* Unlike do_cmd(), a failing client_send() is expected here: on
* success Finit execs the new init and the connection dies with
* no reply. Only an explicit NACK means the request was denied.
*/
client_send(&rq, sizeof(rq));
if (client_send(&rq, sizeof(rq)) && rq.cmd == INIT_CMD_NACK) {
puts(rq.data);
return 1;
}
return 0;
}