diff options
author | Ivan Kruglov <mail@ikruglov.com> | 2024-10-22 15:15:54 +0200 |
---|---|---|
committer | Ivan Kruglov <mail@ikruglov.com> | 2024-11-06 11:37:51 +0100 |
commit | b0eca6dee06feab5a7f89423061a588e84c01efb (patch) | |
tree | 69d7d396bf0c47f1c3eb40675cef55d434c7b262 /src | |
parent | machine: introduce machine_start_getty() and machine_start_shell() helpers (diff) | |
download | systemd-b0eca6dee06feab5a7f89423061a588e84c01efb.tar.xz systemd-b0eca6dee06feab5a7f89423061a588e84c01efb.zip |
machine: machine_default_shell_path() & machine_default_shell_args() helper functions
Diffstat (limited to 'src')
-rw-r--r-- | src/machine/machine-dbus.c | 19 | ||||
-rw-r--r-- | src/machine/machine.c | 30 | ||||
-rw-r--r-- | src/machine/machine.h | 2 |
3 files changed, 34 insertions, 17 deletions
diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index 371441a18e..2ed779d117 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 b1ef7e034b..587c2267b6 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 58250adb0e..bf265143f0 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); |