return 0;
}
-static int transient_service_set_properties(sd_bus_message *m, const char *pty_path) {
+static int transient_service_set_properties(sd_bus_message *m, const char *pty_path, int pty_fd) {
bool send_term = false;
int r;
bool use_ex_prop = arg_expand_environment == 0;
assert(m);
+ assert(pty_path || pty_fd < 0);
r = transient_unit_set_properties(m, UNIT_SERVICE, arg_property);
if (r < 0)
}
if (pty_path) {
- _cleanup_close_ int pty_slave = -EBADF;
-
- pty_slave = open_terminal(pty_path, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (pty_slave < 0)
- return pty_slave;
+ r = sd_bus_message_append(m, "(sv)", "TTYPath", "s", pty_path);
+ if (r < 0)
+ return bus_log_create_error(r);
- r = sd_bus_message_append(m,
- "(sv)(sv)(sv)(sv)",
- "StandardInputFileDescriptor", "h", pty_slave,
- "StandardOutputFileDescriptor", "h", pty_slave,
- "StandardErrorFileDescriptor", "h", pty_slave,
- "TTYPath", "s", pty_path);
+ if (pty_fd >= 0)
+ r = sd_bus_message_append(m,
+ "(sv)(sv)(sv)",
+ "StandardInputFileDescriptor", "h", pty_fd,
+ "StandardOutputFileDescriptor", "h", pty_fd,
+ "StandardErrorFileDescriptor", "h", pty_fd);
+ else
+ r = sd_bus_message_append(m,
+ "(sv)(sv)(sv)",
+ "StandardInput", "s", "tty",
+ "StandardOutput", "s", "tty",
+ "StandardError", "s", "tty");
if (r < 0)
return bus_log_create_error(r);
sd_bus *bus,
sd_bus_message **message,
const char *service,
- const char *pty_path) {
+ const char *pty_path,
+ int pty_fd) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
int r;
if (r < 0)
return bus_log_create_error(r);
- r = transient_service_set_properties(m, pty_path);
+ r = transient_service_set_properties(m, pty_path, pty_fd);
if (r < 0)
return r;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
_cleanup_free_ char *service = NULL, *pty_path = NULL;
- _cleanup_close_ int master = -EBADF;
+ _cleanup_close_ int master = -EBADF, slave = -EBADF;
int r;
assert(bus);
if (unlockpt(master) < 0)
return log_error_errno(errno, "Failed to unlock tty: %m");
+ slave = open_terminal(pty_path, O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (slave < 0)
+ return log_error_errno(slave, "Failed to open pty slave: %m");
+
} else if (arg_transport == BUS_TRANSPORT_MACHINE) {
_cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *pty_reply = NULL;
pty_path = strdup(s);
if (!pty_path)
return log_oom();
+
+ // FIXME: Introduce OpenMachinePTYEx() that accepts ownership/permission as param
+ // and additionally returns the pty fd, for #33216 and #32999
} else
assert_not_reached();
}
return r;
}
- r = make_transient_service_unit(bus, &m, service, pty_path);
+ r = make_transient_service_unit(bus, &m, service, pty_path, slave);
if (r < 0)
return r;
+ slave = safe_close(slave);
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
if (r < 0)
return bus_log_create_error(r);
- r = transient_service_set_properties(m, NULL);
+ r = transient_service_set_properties(m, /* pty_path = */ NULL, /* pty_fd = */ -EBADF);
if (r < 0)
return r;