@@ -1210,7 +1210,23 @@ exec_pty(struct command_details *details,
12101210 * enabled, use a pipe to interpose ourselves instead of using the
12111211 * pty fd. We always use a pipe for stdin when in background mode.
12121212 */
1213- if (!fd_matches_pgrp (STDIN_FILENO , ppgrp , & sb )) {
1213+ if (ISSET (details -> flags , CD_BACKGROUND )) {
1214+ /*
1215+ * Running in background (sudo -b), no access to terminal input.
1216+ * In non-pty mode, the command runs in an orphaned process
1217+ * group and reads from the controlling terminal fail with EIO.
1218+ * We cannot do the same while running in a pty but if we set
1219+ * stdin to a half-closed pipe, reads from it will get EOF.
1220+ */
1221+ sudo_debug_printf (SUDO_DEBUG_INFO ,
1222+ "terminal input not available, creating empty pipe" );
1223+ SET (details -> flags , CD_EXEC_BG );
1224+ if (pipe2 (io_pipe [STDIN_FILENO ], O_CLOEXEC ) != 0 )
1225+ sudo_fatal ("%s" , U_ ("unable to create pipe" ));
1226+ io_fds [SFD_STDIN ] = io_pipe [STDIN_FILENO ][0 ];
1227+ close (io_pipe [STDIN_FILENO ][1 ]);
1228+ io_pipe [STDIN_FILENO ][1 ] = -1 ;
1229+ } else if (!fd_matches_pgrp (STDIN_FILENO , ppgrp , & sb )) {
12141230 if (!interpose [STDIN_FILENO ]) {
12151231 /* Not logging stdin, do not interpose. */
12161232 sudo_debug_printf (SUDO_DEBUG_INFO ,
@@ -1240,22 +1256,6 @@ exec_pty(struct command_details *details,
12401256 */
12411257 SET (details -> flags , CD_EXEC_BG );
12421258 }
1243- } else if (ISSET (details -> flags , CD_BACKGROUND )) {
1244- /*
1245- * Running in background (sudo -b), no access to terminal input.
1246- * In non-pty mode, the command runs in an orphaned process
1247- * group and reads from the controlling terminal fail with EIO.
1248- * We cannot do the same while running in a pty but if we set
1249- * stdin to a half-closed pipe, reads from it will get EOF.
1250- */
1251- sudo_debug_printf (SUDO_DEBUG_INFO ,
1252- "terminal input not available, creating empty pipe" );
1253- SET (details -> flags , CD_EXEC_BG );
1254- if (pipe2 (io_pipe [STDIN_FILENO ], O_CLOEXEC ) != 0 )
1255- sudo_fatal ("%s" , U_ ("unable to create pipe" ));
1256- io_fds [SFD_STDIN ] = io_pipe [STDIN_FILENO ][0 ];
1257- close (io_pipe [STDIN_FILENO ][1 ]);
1258- io_pipe [STDIN_FILENO ][1 ] = -1 ;
12591259 }
12601260 if (!fd_matches_pgrp (STDOUT_FILENO , ppgrp , & sb )) {
12611261 if (!interpose [STDOUT_FILENO ]) {
0 commit comments