diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index 371441a18e3..2ed779d117f 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -359,25 +359,10 @@ int bus_machine_method_open_shell(sd_bus_message *message, void *userdata, sd_bu if (r < 0) return r; if (isempty(path)) { - path = "/bin/sh"; - - args = new0(char*, 3 + 1); + path = machine_default_shell_path(); + args = machine_default_shell_args(user); if (!args) return -ENOMEM; - args[0] = strdup("sh"); - if (!args[0]) - return -ENOMEM; - args[1] = strdup("-c"); - if (!args[1]) - return -ENOMEM; - r = asprintf(&args[2], - "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\ - "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */ - user); - if (r < 0) { - args[2] = NULL; - return -ENOMEM; - } } else { if (!path_is_absolute(path)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified path '%s' is not absolute", path); diff --git a/src/machine/machine.c b/src/machine/machine.c index b1ef7e034b9..587c2267b69 100644 --- a/src/machine/machine.c +++ b/src/machine/machine.c @@ -943,6 +943,36 @@ int machine_start_shell( return 0; } +char** machine_default_shell_args(const char *user) { + _cleanup_strv_free_ char **args = NULL; + int r; + + assert(user); + + args = new0(char*, 3 + 1); + if (!args) + return NULL; + + args[0] = strdup("sh"); + if (!args[0]) + return NULL; + + args[1] = strdup("-c"); + if (!args[1]) + return NULL; + + r = asprintf(&args[2], + "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\ + "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */ + user); + if (r < 0) { + args[2] = NULL; + return NULL; + } + + return TAKE_PTR(args); +} + void machine_release_unit(Machine *m) { assert(m); diff --git a/src/machine/machine.h b/src/machine/machine.h index 58250adb0ee..bf265143f03 100644 --- a/src/machine/machine.h +++ b/src/machine/machine.h @@ -104,6 +104,8 @@ int machine_openpt(Machine *m, int flags, char **ret_slave); int machine_open_terminal(Machine *m, const char *path, int mode); int machine_start_getty(Machine *m, const char *ptmx_name, sd_bus_error *error); int machine_start_shell(Machine *m, int ptmx_fd, const char *ptmx_name, const char *user, const char *path, char **args, char **env, sd_bus_error *error); +#define machine_default_shell_path() ("/bin/sh") +char** machine_default_shell_args(const char *user); int machine_get_uid_shift(Machine *m, uid_t *ret);