chiark / gitweb /
main: ignore EPERM in TIOCSTTY when opening terminal for crash shell
authorLennart Poettering <lennart@poettering.net>
Tue, 18 May 2010 01:40:19 +0000 (03:40 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 18 May 2010 01:40:19 +0000 (03:40 +0200)
src/execute.c
src/main.c
src/util.c
src/util.h

index 12f514504cabac8261fa3b39e863bf9841615718..107683470244a5ae1b8e90e938b5c783ef7ef3e1 100644 (file)
@@ -274,7 +274,8 @@ static int setup_input(const ExecContext *context, int socket_fd) {
                 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) {
@@ -429,7 +430,8 @@ static int setup_confirm_stdio(const ExecContext *context,
         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;
         }
index e2d2ab5e647ad83bf8cf2442f4982cd6a1eff5bd..311ece6b57141cdf8be5cc1d4f1cbceea5f76342 100644 (file)
@@ -138,7 +138,7 @@ _noreturn static void crash(int sig) {
                 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));
index f7d538aaacf87496367661ba3822bc2f44cb6070..5c1e16ab6ed49efb9c05980ea489955c04b7a608 100644 (file)
@@ -1598,7 +1598,7 @@ int flush_fd(int fd) {
         }
 }
 
-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);
@@ -1640,8 +1640,15 @@ int acquire_terminal(const char *name, bool fail, bool force) {
                         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;
                 }
index a77a952ed6d053436421b14b644f8f9a450604c7..84dd2bc8d130a8f3e7c26383084ffa38c7f77371 100644 (file)
@@ -218,7 +218,7 @@ int ask(char *ret, const char *replies, const char *text, ...);
 
 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);