83c5f2e
To: vim_dev@googlegroups.com
83c5f2e
Subject: Patch 7.3.315
83c5f2e
Fcc: outbox
83c5f2e
From: Bram Moolenaar <Bram@moolenaar.net>
83c5f2e
Mime-Version: 1.0
83c5f2e
Content-Type: text/plain; charset=UTF-8
83c5f2e
Content-Transfer-Encoding: 8bit
83c5f2e
------------
83c5f2e
83c5f2e
Patch 7.3.315
83c5f2e
Problem:    Opening a window before forking causes problems for GTK.
83c5f2e
Solution:   Fork first, create the window in the child and report back to the
83c5f2e
	    parent process whether it worked.  If successful the parent exits,
83c5f2e
	    if unsuccessful the child exits and the parent continues in the
83c5f2e
	    terminal. (Tim Starling)
83c5f2e
Files:	    src/gui.c
83c5f2e
83c5f2e
83c5f2e
*** ../vim-7.3.314/src/gui.c	2011-08-10 17:44:41.000000000 +0200
83c5f2e
--- src/gui.c	2011-09-14 17:34:30.000000000 +0200
83c5f2e
***************
83c5f2e
*** 37,42 ****
83c5f2e
--- 37,60 ----
83c5f2e
  static void gui_set_bg_color __ARGS((char_u *name));
83c5f2e
  static win_T *xy2win __ARGS((int x, int y));
83c5f2e
  
83c5f2e
+ #if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X) \
83c5f2e
+ 	&& !defined(__APPLE__)
83c5f2e
+ # define MAY_FORK
83c5f2e
+ static void gui_do_fork __ARGS((void));
83c5f2e
+ 
83c5f2e
+ static int gui_read_child_pipe __ARGS((int fd));
83c5f2e
+ 
83c5f2e
+ /* Return values for gui_read_child_pipe */
83c5f2e
+ enum {
83c5f2e
+     GUI_CHILD_IO_ERROR,
83c5f2e
+     GUI_CHILD_OK,
83c5f2e
+     GUI_CHILD_FAILED
83c5f2e
+ };
83c5f2e
+ 
83c5f2e
+ #endif /* MAY_FORK */
83c5f2e
+ 
83c5f2e
+ static void gui_attempt_start __ARGS((void));
83c5f2e
+ 
83c5f2e
  static int can_update_cursor = TRUE; /* can display the cursor */
83c5f2e
  
83c5f2e
  /*
83c5f2e
***************
83c5f2e
*** 59,105 ****
83c5f2e
  gui_start()
83c5f2e
  {
83c5f2e
      char_u	*old_term;
83c5f2e
- #if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X) \
83c5f2e
- 	&& !defined(__APPLE__)
83c5f2e
- # define MAY_FORK
83c5f2e
-     int		dofork = TRUE;
83c5f2e
- #endif
83c5f2e
      static int	recursive = 0;
83c5f2e
  
83c5f2e
      old_term = vim_strsave(T_NAME);
83c5f2e
  
83c5f2e
-     /*
83c5f2e
-      * Set_termname() will call gui_init() to start the GUI.
83c5f2e
-      * Set the "starting" flag, to indicate that the GUI will start.
83c5f2e
-      *
83c5f2e
-      * We don't want to open the GUI shell until after we've read .gvimrc,
83c5f2e
-      * otherwise we don't know what font we will use, and hence we don't know
83c5f2e
-      * what size the shell should be.  So if there are errors in the .gvimrc
83c5f2e
-      * file, they will have to go to the terminal: Set full_screen to FALSE.
83c5f2e
-      * full_screen will be set to TRUE again by a successful termcapinit().
83c5f2e
-      */
83c5f2e
      settmode(TMODE_COOK);		/* stop RAW mode */
83c5f2e
      if (full_screen)
83c5f2e
  	cursor_on();			/* needed for ":gui" in .vimrc */
83c5f2e
-     gui.starting = TRUE;
83c5f2e
      full_screen = FALSE;
83c5f2e
  
83c5f2e
! #ifdef FEAT_GUI_GTK
83c5f2e
!     gui.event_time = GDK_CURRENT_TIME;
83c5f2e
! #endif
83c5f2e
  
83c5f2e
  #ifdef MAY_FORK
83c5f2e
!     if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
83c5f2e
! 	dofork = FALSE;
83c5f2e
  #endif
83c5f2e
!     ++recursive;
83c5f2e
! 
83c5f2e
!     termcapinit((char_u *)"builtin_gui");
83c5f2e
!     gui.starting = recursive - 1;
83c5f2e
  
83c5f2e
      if (!gui.in_use)			/* failed to start GUI */
83c5f2e
      {
83c5f2e
! 	termcapinit(old_term);		/* back to old term settings */
83c5f2e
  	settmode(TMODE_RAW);		/* restart RAW mode */
83c5f2e
  #ifdef FEAT_TITLE
83c5f2e
  	set_title_defaults();		/* set 'title' and 'icon' again */
83c5f2e
--- 77,123 ----
83c5f2e
  gui_start()
83c5f2e
  {
83c5f2e
      char_u	*old_term;
83c5f2e
      static int	recursive = 0;
83c5f2e
  
83c5f2e
      old_term = vim_strsave(T_NAME);
83c5f2e
  
83c5f2e
      settmode(TMODE_COOK);		/* stop RAW mode */
83c5f2e
      if (full_screen)
83c5f2e
  	cursor_on();			/* needed for ":gui" in .vimrc */
83c5f2e
      full_screen = FALSE;
83c5f2e
  
83c5f2e
!     ++recursive;
83c5f2e
  
83c5f2e
  #ifdef MAY_FORK
83c5f2e
!     /*
83c5f2e
!      * Quit the current process and continue in the child.
83c5f2e
!      * Makes "gvim file" disconnect from the shell it was started in.
83c5f2e
!      * Don't do this when Vim was started with "-f" or the 'f' flag is present
83c5f2e
!      * in 'guioptions'.
83c5f2e
!      */
83c5f2e
!     if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1)
83c5f2e
!     {
83c5f2e
! 	gui_do_fork();
83c5f2e
!     }
83c5f2e
!     else
83c5f2e
  #endif
83c5f2e
!     {
83c5f2e
! 	gui_attempt_start();
83c5f2e
!     }
83c5f2e
  
83c5f2e
      if (!gui.in_use)			/* failed to start GUI */
83c5f2e
      {
83c5f2e
! 	/* Back to old term settings
83c5f2e
! 	 *
83c5f2e
! 	 * FIXME: If we got here because a child process failed and flagged to
83c5f2e
! 	 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
83c5f2e
! 	 * hit an X11 I/O error and do a longjmp(), leaving recursive
83c5f2e
! 	 * permanently set to 1. This is probably not as big a problem as it
83c5f2e
! 	 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
83c5f2e
! 	 * return "OK" unconditionally, so it would be very difficult to
83c5f2e
! 	 * actually hit this case.
83c5f2e
! 	 */
83c5f2e
! 	termcapinit(old_term);
83c5f2e
  	settmode(TMODE_RAW);		/* restart RAW mode */
83c5f2e
  #ifdef FEAT_TITLE
83c5f2e
  	set_title_defaults();		/* set 'title' and 'icon' again */
83c5f2e
***************
83c5f2e
*** 108,113 ****
83c5f2e
--- 126,166 ----
83c5f2e
  
83c5f2e
      vim_free(old_term);
83c5f2e
  
83c5f2e
+ #ifdef FEAT_AUTOCMD
83c5f2e
+     /* If the GUI started successfully, trigger the GUIEnter event, otherwise
83c5f2e
+      * the GUIFailed event. */
83c5f2e
+     gui_mch_update();
83c5f2e
+     apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
83c5f2e
+ 						   NULL, NULL, FALSE, curbuf);
83c5f2e
+ #endif
83c5f2e
+     --recursive;
83c5f2e
+ }
83c5f2e
+ 
83c5f2e
+ /*
83c5f2e
+  * Set_termname() will call gui_init() to start the GUI.
83c5f2e
+  * Set the "starting" flag, to indicate that the GUI will start.
83c5f2e
+  *
83c5f2e
+  * We don't want to open the GUI shell until after we've read .gvimrc,
83c5f2e
+  * otherwise we don't know what font we will use, and hence we don't know
83c5f2e
+  * what size the shell should be.  So if there are errors in the .gvimrc
83c5f2e
+  * file, they will have to go to the terminal: Set full_screen to FALSE.
83c5f2e
+  * full_screen will be set to TRUE again by a successful termcapinit().
83c5f2e
+  */
83c5f2e
+     static void
83c5f2e
+ gui_attempt_start()
83c5f2e
+ {
83c5f2e
+     static int recursive = 0;
83c5f2e
+ 
83c5f2e
+     ++recursive;
83c5f2e
+     gui.starting = TRUE;
83c5f2e
+ 
83c5f2e
+ #ifdef FEAT_GUI_GTK
83c5f2e
+     gui.event_time = GDK_CURRENT_TIME;
83c5f2e
+ #endif
83c5f2e
+ 
83c5f2e
+     termcapinit((char_u *)"builtin_gui");
83c5f2e
+     gui.starting = recursive - 1;
83c5f2e
+ 
83c5f2e
  #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
83c5f2e
      if (gui.in_use)
83c5f2e
      {
83c5f2e
***************
83c5f2e
*** 123,218 ****
83c5f2e
  	display_errors();
83c5f2e
      }
83c5f2e
  #endif
83c5f2e
  
83c5f2e
! #if defined(MAY_FORK) && !defined(__QNXNTO__)
83c5f2e
!     /*
83c5f2e
!      * Quit the current process and continue in the child.
83c5f2e
!      * Makes "gvim file" disconnect from the shell it was started in.
83c5f2e
!      * Don't do this when Vim was started with "-f" or the 'f' flag is present
83c5f2e
!      * in 'guioptions'.
83c5f2e
!      */
83c5f2e
!     if (gui.in_use && dofork)
83c5f2e
      {
83c5f2e
! 	int	pipefd[2];	/* pipe between parent and child */
83c5f2e
! 	int	pipe_error;
83c5f2e
! 	char	dummy;
83c5f2e
! 	pid_t	pid = -1;
83c5f2e
! 
83c5f2e
! 	/* Setup a pipe between the child and the parent, so that the parent
83c5f2e
! 	 * knows when the child has done the setsid() call and is allowed to
83c5f2e
! 	 * exit. */
83c5f2e
! 	pipe_error = (pipe(pipefd) < 0);
83c5f2e
! 	pid = fork();
83c5f2e
! 	if (pid > 0)	    /* Parent */
83c5f2e
  	{
83c5f2e
! 	    /* Give the child some time to do the setsid(), otherwise the
83c5f2e
! 	     * exit() may kill the child too (when starting gvim from inside a
83c5f2e
! 	     * gvim). */
83c5f2e
! 	    if (pipe_error)
83c5f2e
! 		ui_delay(300L, TRUE);
83c5f2e
! 	    else
83c5f2e
  	    {
83c5f2e
! 		/* The read returns when the child closes the pipe (or when
83c5f2e
! 		 * the child dies for some reason). */
83c5f2e
! 		close(pipefd[1]);
83c5f2e
! 		ignored = (int)read(pipefd[0], &dummy, (size_t)1);
83c5f2e
! 		close(pipefd[0]);
83c5f2e
  	    }
83c5f2e
! 
83c5f2e
! 	    /* When swapping screens we may need to go to the next line, e.g.,
83c5f2e
! 	     * after a hit-enter prompt and using ":gui". */
83c5f2e
! 	    if (newline_on_exit)
83c5f2e
! 		mch_errmsg("\r\n");
83c5f2e
! 
83c5f2e
! 	    /*
83c5f2e
! 	     * The parent must skip the normal exit() processing, the child
83c5f2e
! 	     * will do it.  For example, GTK messes up signals when exiting.
83c5f2e
! 	     */
83c5f2e
! 	    _exit(0);
83c5f2e
  	}
83c5f2e
  
83c5f2e
! # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
83c5f2e
  	/*
83c5f2e
! 	 * Change our process group.  On some systems/shells a CTRL-C in the
83c5f2e
! 	 * shell where Vim was started would otherwise kill gvim!
83c5f2e
  	 */
83c5f2e
! 	if (pid == 0)	    /* child */
83c5f2e
  #  if defined(HAVE_SETSID)
83c5f2e
! 	    (void)setsid();
83c5f2e
  #  else
83c5f2e
! 	    (void)setpgid(0, 0);
83c5f2e
  #  endif
83c5f2e
  # endif
83c5f2e
! 	if (!pipe_error)
83c5f2e
! 	{
83c5f2e
! 	    close(pipefd[0]);
83c5f2e
! 	    close(pipefd[1]);
83c5f2e
! 	}
83c5f2e
  
83c5f2e
  # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
83c5f2e
! 	/* Tell the session manager our new PID */
83c5f2e
! 	gui_mch_forked();
83c5f2e
  # endif
83c5f2e
      }
83c5f2e
- #else
83c5f2e
- # if defined(__QNXNTO__)
83c5f2e
-     if (gui.in_use && dofork)
83c5f2e
- 	procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
83c5f2e
- 		PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
83c5f2e
- # endif
83c5f2e
- #endif
83c5f2e
  
83c5f2e
! #ifdef FEAT_AUTOCMD
83c5f2e
!     /* If the GUI started successfully, trigger the GUIEnter event, otherwise
83c5f2e
!      * the GUIFailed event. */
83c5f2e
!     gui_mch_update();
83c5f2e
!     apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
83c5f2e
! 						   NULL, NULL, FALSE, curbuf);
83c5f2e
  #endif
83c5f2e
  
83c5f2e
!     --recursive;
83c5f2e
  }
83c5f2e
  
83c5f2e
  /*
83c5f2e
   * Call this when vim starts up, whether or not the GUI is started
83c5f2e
   */
83c5f2e
--- 176,346 ----
83c5f2e
  	display_errors();
83c5f2e
      }
83c5f2e
  #endif
83c5f2e
+     --recursive;
83c5f2e
+ }
83c5f2e
  
83c5f2e
! #ifdef MAY_FORK
83c5f2e
! 
83c5f2e
! /* for waitpid() */
83c5f2e
! # if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
83c5f2e
! #  include <sys/wait.h>
83c5f2e
! # endif
83c5f2e
! 
83c5f2e
! /*
83c5f2e
!  * Create a new process, by forking. In the child, start the GUI, and in
83c5f2e
!  * the parent, exit.
83c5f2e
!  *
83c5f2e
!  * If something goes wrong, this will return with gui.in_use still set
83c5f2e
!  * to FALSE, in which case the caller should continue execution without
83c5f2e
!  * the GUI.
83c5f2e
!  *
83c5f2e
!  * If the child fails to start the GUI, then the child will exit and the
83c5f2e
!  * parent will return. If the child succeeds, then the parent will exit
83c5f2e
!  * and the child will return.
83c5f2e
!  */
83c5f2e
!     static void
83c5f2e
! gui_do_fork()
83c5f2e
! {
83c5f2e
! #ifdef __QNXNTO__
83c5f2e
!     procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
83c5f2e
! 	    PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
83c5f2e
!     gui_attempt_start();
83c5f2e
!     return;
83c5f2e
! #else
83c5f2e
!     int		pipefd[2];	/* pipe between parent and child */
83c5f2e
!     int		pipe_error;
83c5f2e
!     int		status;
83c5f2e
!     int		exit_status;
83c5f2e
!     pid_t	pid = -1;
83c5f2e
!     FILE	*parent_file;
83c5f2e
! 
83c5f2e
!     /* Setup a pipe between the child and the parent, so that the parent
83c5f2e
!      * knows when the child has done the setsid() call and is allowed to
83c5f2e
!      * exit. */
83c5f2e
!     pipe_error = (pipe(pipefd) < 0);
83c5f2e
!     pid = fork();
83c5f2e
!     if (pid < 0)	    /* Fork error */
83c5f2e
      {
83c5f2e
! 	EMSG(_("E851: Failed to create a new process for the GUI"));
83c5f2e
! 	return;
83c5f2e
!     }
83c5f2e
!     else if (pid > 0)	    /* Parent */
83c5f2e
!     {
83c5f2e
! 	/* Give the child some time to do the setsid(), otherwise the
83c5f2e
! 	 * exit() may kill the child too (when starting gvim from inside a
83c5f2e
! 	 * gvim). */
83c5f2e
! 	if (!pipe_error)
83c5f2e
  	{
83c5f2e
! 	    /* The read returns when the child closes the pipe (or when
83c5f2e
! 	     * the child dies for some reason). */
83c5f2e
! 	    close(pipefd[1]);
83c5f2e
! 	    status = gui_read_child_pipe(pipefd[0]);
83c5f2e
! 	    if (status == GUI_CHILD_FAILED)
83c5f2e
  	    {
83c5f2e
! 		/* The child failed to start the GUI, so the caller must
83c5f2e
! 		 * continue. There may be more error information written
83c5f2e
! 		 * to stderr by the child. */
83c5f2e
! # ifdef __NeXT__
83c5f2e
! 		wait4(pid, &exit_status, 0, (struct rusage *)0);
83c5f2e
! # else
83c5f2e
! 		waitpid(pid, &exit_status, 0);
83c5f2e
! # endif
83c5f2e
! 		EMSG(_("E852: The child process failed to start the GUI"));
83c5f2e
! 		return;
83c5f2e
  	    }
83c5f2e
! 	    else if (status == GUI_CHILD_IO_ERROR)
83c5f2e
! 	    {
83c5f2e
! 		pipe_error = TRUE;
83c5f2e
! 	    }
83c5f2e
! 	    /* else GUI_CHILD_OK: parent exit */
83c5f2e
  	}
83c5f2e
  
83c5f2e
! 	if (pipe_error)
83c5f2e
! 	    ui_delay(300L, TRUE);
83c5f2e
! 
83c5f2e
! 	/* When swapping screens we may need to go to the next line, e.g.,
83c5f2e
! 	 * after a hit-enter prompt and using ":gui". */
83c5f2e
! 	if (newline_on_exit)
83c5f2e
! 	    mch_errmsg("\r\n");
83c5f2e
! 
83c5f2e
  	/*
83c5f2e
! 	 * The parent must skip the normal exit() processing, the child
83c5f2e
! 	 * will do it.  For example, GTK messes up signals when exiting.
83c5f2e
  	 */
83c5f2e
! 	_exit(0);
83c5f2e
!     }
83c5f2e
!     /* Child */
83c5f2e
! 
83c5f2e
! # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
83c5f2e
!     /*
83c5f2e
!      * Change our process group.  On some systems/shells a CTRL-C in the
83c5f2e
!      * shell where Vim was started would otherwise kill gvim!
83c5f2e
!      */
83c5f2e
  #  if defined(HAVE_SETSID)
83c5f2e
!     (void)setsid();
83c5f2e
  #  else
83c5f2e
!     (void)setpgid(0, 0);
83c5f2e
  #  endif
83c5f2e
  # endif
83c5f2e
!     if (!pipe_error)
83c5f2e
! 	close(pipefd[0]);
83c5f2e
  
83c5f2e
  # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
83c5f2e
!     /* Tell the session manager our new PID */
83c5f2e
!     gui_mch_forked();
83c5f2e
  # endif
83c5f2e
+ 
83c5f2e
+     if (!pipe_error)
83c5f2e
+ 	parent_file = fdopen(pipefd[1], "w");
83c5f2e
+     else
83c5f2e
+ 	parent_file = NULL;
83c5f2e
+ 
83c5f2e
+     /* Try to start the GUI */
83c5f2e
+     gui_attempt_start();
83c5f2e
+ 
83c5f2e
+     /* Notify the parent */
83c5f2e
+     if (parent_file != NULL)
83c5f2e
+     {
83c5f2e
+ 	fputs(gui.in_use ? "ok" : "fail", parent_file);
83c5f2e
+ 	fclose(parent_file);
83c5f2e
      }
83c5f2e
  
83c5f2e
!     /* If we failed to start the GUI, exit now. */
83c5f2e
!     if (!gui.in_use)
83c5f2e
! 	exit(1);
83c5f2e
  #endif
83c5f2e
+ }
83c5f2e
  
83c5f2e
! /*
83c5f2e
!  * Read from a pipe assumed to be connected to the child process (this
83c5f2e
!  * function is called from the parent).
83c5f2e
!  * Return GUI_CHILD_OK if the child successfully started the GUI,
83c5f2e
!  * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
83c5f2e
!  * some other error.
83c5f2e
!  *
83c5f2e
!  * The file descriptor will be closed before the function returns.
83c5f2e
!  */
83c5f2e
!     static int
83c5f2e
! gui_read_child_pipe(int fd)
83c5f2e
! {
83c5f2e
!     size_t	bytes_read;
83c5f2e
!     FILE	*file;
83c5f2e
!     char	buffer[10];
83c5f2e
! 
83c5f2e
!     file = fdopen(fd, "r");
83c5f2e
!     if (!file)
83c5f2e
! 	return GUI_CHILD_IO_ERROR;
83c5f2e
! 
83c5f2e
!     bytes_read = fread(buffer, sizeof(char), sizeof(buffer)-1, file);
83c5f2e
!     buffer[bytes_read] = '\0';
83c5f2e
!     fclose(file);
83c5f2e
!     if (strcmp(buffer, "ok") == 0)
83c5f2e
! 	return GUI_CHILD_OK;
83c5f2e
!     return GUI_CHILD_FAILED;
83c5f2e
  }
83c5f2e
  
83c5f2e
+ #endif /* MAY_FORK */
83c5f2e
+ 
83c5f2e
  /*
83c5f2e
   * Call this when vim starts up, whether or not the GUI is started
83c5f2e
   */
83c5f2e
*** ../vim-7.3.314/src/version.c	2011-09-14 19:01:38.000000000 +0200
83c5f2e
--- src/version.c	2011-09-14 19:02:45.000000000 +0200
83c5f2e
***************
83c5f2e
*** 711,712 ****
83c5f2e
--- 711,714 ----
83c5f2e
  {   /* Add new patch number below this line */
83c5f2e
+ /**/
83c5f2e
+     315,
83c5f2e
  /**/
83c5f2e
83c5f2e
-- 
83c5f2e
A)bort, R)etry, B)ang it with a large hammer
83c5f2e
83c5f2e
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
83c5f2e
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
83c5f2e
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
83c5f2e
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///