[systemd-devel] [PATCH 2/2] systemctl: for switch-root check, if we switch to a systemd init

Lennart Poettering lennart at poettering.net
Wed Mar 5 10:22:43 PST 2014


On Wed, 05.03.14 14:37, harald at redhat.com (harald at redhat.com) wrote:

> From: Harald Hoyer <harald at redhat.com>
> 
> If "systemctl switch-root" is called with a specific "INIT" or
> /proc/cmdline contains "init=", then systemd would not serialize
> itsself.
> 
> Let systemctl check, if the new init is in the standard systemd
> installation path and if so, clear the INIT parameter,
> to let systemd serialize itsself.
> ---
>  src/systemctl/systemctl.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
> index f395265..18dbcc0 100644
> --- a/src/systemctl/systemctl.c
> +++ b/src/systemctl/systemctl.c
> @@ -4283,6 +4283,7 @@ static int show_environment(sd_bus *bus, char **args) {
>  static int switch_root(sd_bus *bus, char **args) {
>          _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
>          _cleanup_free_ char *init = NULL;
> +        _cleanup_free_ char *root_systemd_path = NULL;
>          const char *root;
>          unsigned l;
>          int r;
> @@ -4309,6 +4310,16 @@ static int switch_root(sd_bus *bus, char **args) {
>          if (!init)
>                  return log_oom();
>  
> +        root_systemd_path = strjoin(root, "/", SYSTEMD_BINARY_PATH, NULL);
> +        if (!root_systemd_path)
> +                return log_oom();

We can simplify this and allocate it on the stack:

    char *root_systemd_path;

    root_systemd_path = strappaneda(root, "/" SYSTEMD_BINARY_PATH);

And then we don't have to do the OOM check anymore...

> +
> +        if (files_same(init, root_systemd_path) > 0) {
> +                char *t = init;
> +                init = strdup("");
> +                free(t);

This is actually unnecessary, the sd-bus libraries will automatically
serialize "" when they encounter a NULL string. Actually, switch_root()
could be simplified a bit making use of this...

Just use:
     
     free(init);
     init = NULL;

or so...

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list