[systemd-devel] [RFC][PATCH 2/2] systemctl: switch-root - read init= from /proc/cmdline

Lennart Poettering lennart at poettering.net
Wed Dec 19 14:49:44 PST 2012


On Wed, 28.11.12 01:30, Tom Gundersen (teg at jklm.no) wrote:

> If the path to init is not specified as an argumnt to systemctl, but
> init= is given on the kernel commandline, use that.
> 
> This means the initrd does not need glue code to parse the kernel
> commandline before passing on init= to systemctl.

Looks OK.

> -        const char *root, *init;
> +        int r;
> +        const char *root;
> +        char *init = NULL;

Might want to use _cleanup_free_ for this.

>  
>          l = strv_length(args);
>          if (l < 2 || l > 3) {
> @@ -3332,9 +3334,22 @@ static int switch_root(DBusConnection *bus, char **args) {
>          }
>  
>          root = args[1];
> -        init = l >= 3 ? args[2] : "";
>  
> -        return bus_method_call_with_reply (
> +        if (l >= 3) {
> +                init = strdup(args[2]);
> +        } else {

Please don't use {} for single-line if blocks. This isn't PHP ;-)...

> +                parse_env_file("/proc/cmdline", WHITESPACE,
> +                               "init", &init,
> +                               NULL);
> +
> +                if (!init)
> +                        init = strdup("");
> +
> +        }

You should check init for OOM here.

> +
> +        log_debug("switching root - root: %s; init: %s", root, init);
> +
> +        r = bus_method_call_with_reply (
>                          bus,
>                          "org.freedesktop.systemd1",
>                          "/org/freedesktop/systemd1",
> @@ -3345,6 +3360,10 @@ static int switch_root(DBusConnection *bus, char **args) {
>                          DBUS_TYPE_STRING, &root,
>                          DBUS_TYPE_STRING, &init,
>                          DBUS_TYPE_INVALID);
> +
> +        free(init);
> +
> +        return r;
>  }
>  
>  static int set_environment(DBusConnection *bus, char **args) {

Otherwise looks good.

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list