if ((fd = acquire_terminal(
tty_path(context),
i == EXEC_INPUT_TTY_FAIL,
- i == EXEC_INPUT_TTY_FORCE)) < 0)
+ i == EXEC_INPUT_TTY_FORCE,
+ false)) < 0)
return fd;
if (fd != STDIN_FILENO) {
if ((fd = acquire_terminal(
tty_path(context),
context->std_input == EXEC_INPUT_TTY_FAIL,
- context->std_input == EXEC_INPUT_TTY_FORCE)) < 0) {
+ context->std_input == EXEC_INPUT_TTY_FORCE,
+ false)) < 0) {
r = EXIT_STDIN;
goto fail;
}
else if (pid == 0) {
int fd, r;
- if ((fd = acquire_terminal("/dev/console", false, true)) < 0)
+ if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
log_error("Failed to acquire terminal: %s", strerror(-fd));
else if ((r = make_stdio(fd)) < 0)
log_error("Failed to duplicate terminal fd: %s", strerror(-r));
}
}
-int acquire_terminal(const char *name, bool fail, bool force) {
+int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) {
int fd = -1, notify = -1, r, wd = -1;
assert(name);
return -errno;
/* First, try to get the tty */
- if ((r = ioctl(fd, TIOCSCTTY, force)) < 0 &&
- (force || fail || errno != EPERM)) {
+ r = ioctl(fd, TIOCSCTTY, force);
+
+ /* Sometimes it makes sense to ignore TIOCSCTTY
+ * returning EPERM, i.e. when very likely we already
+ * are have this controlling terminal. */
+ if (r < 0 && errno == EPERM && ignore_tiocstty_eperm)
+ r = 0;
+
+ if (r < 0 && (force || fail || errno != EPERM)) {
r = -errno;
goto fail;
}
int reset_terminal(int fd);
int open_terminal(const char *name, int mode);
-int acquire_terminal(const char *name, bool fail, bool force);
+int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);
int release_terminal(void);
int flush_fd(int fd);