@@ -229,25 +229,34 @@ static int exec_context_tty_size(const ExecContext *context, unsigned *ret_rows,
229229}
230230
231231static void exec_context_tty_reset (const ExecContext * context , const ExecParameters * p ) {
232- const char * path ;
232+ _cleanup_close_ int fd = - EBADF ;
233+ const char * path = exec_context_tty_path (ASSERT_PTR (context ));
233234
234- assert (context );
235+ /* Take a lock around the device for the duration of the setup that we do here.
236+ * systemd-vconsole-setup.service also takes the lock to avoid being interrupted.
237+ * We open a new fd that will be closed automatically, and operate on it for convenience.
238+ */
235239
236- path = exec_context_tty_path (context );
240+ if (p && p -> stdin_fd >= 0 ) {
241+ fd = xopenat_lock (p -> stdin_fd , NULL ,
242+ O_RDONLY |O_CLOEXEC |O_NONBLOCK |O_NOCTTY , 0 , 0 , LOCK_BSD , LOCK_EX );
243+ if (fd < 0 )
244+ return ;
245+ } else if (path ) {
246+ fd = open_terminal (path , O_RDWR |O_NOCTTY |O_CLOEXEC |O_NONBLOCK );
247+ if (fd < 0 )
248+ return ;
237249
238- if (context -> tty_vhangup ) {
239- if (p && p -> stdin_fd >= 0 )
240- (void ) terminal_vhangup_fd (p -> stdin_fd );
241- else if (path )
242- (void ) terminal_vhangup (path );
243- }
250+ if (lock_generic (fd , LOCK_BSD , LOCK_EX ) < 0 )
251+ return ;
252+ } else
253+ return ; /* nothing to do */
244254
245- if (context -> tty_reset ) {
246- if (p && p -> stdin_fd >= 0 )
247- (void ) reset_terminal_fd (p -> stdin_fd , true);
248- else if (path )
249- (void ) reset_terminal (path );
250- }
255+ if (context -> tty_vhangup )
256+ (void ) terminal_vhangup_fd (fd );
257+
258+ if (context -> tty_reset )
259+ (void ) reset_terminal_fd (fd , true);
251260
252261 if (p && p -> stdin_fd >= 0 ) {
253262 unsigned rows = context -> tty_rows , cols = context -> tty_cols ;
@@ -7180,16 +7189,6 @@ bool exec_context_has_encrypted_credentials(ExecContext *c) {
71807189 return false;
71817190}
71827191
7183- int exec_context_add_default_dependencies (Unit * u , const ExecContext * c ) {
7184- assert (u );
7185- assert (u -> default_dependencies );
7186-
7187- if (c && exec_context_needs_term (c ))
7188- return unit_add_dependency_by_name (u , UNIT_AFTER , SPECIAL_VCONSOLE_SETUP_SERVICE ,
7189- /* add_reference= */ true, UNIT_DEPENDENCY_DEFAULT );
7190- return 0 ;
7191- }
7192-
71937192void exec_status_start (ExecStatus * s , pid_t pid ) {
71947193 assert (s );
71957194
0 commit comments