mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 09:44:18 +00:00 
			
		
		
		
	Check if the index is in use and fail before creating the child process,
rather than leaving a stray child on failure.
This commit is contained in:
		
							
								
								
									
										20
									
								
								session.c
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								session.c
									
									
									
									
									
								
							@@ -212,10 +212,16 @@ session_new(struct session *s,
 | 
				
			|||||||
    const char *name, const char *cmd, const char *cwd, int idx, char **cause)
 | 
					    const char *name, const char *cmd, const char *cwd, int idx, char **cause)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct window	*w;
 | 
						struct window	*w;
 | 
				
			||||||
 | 
						struct winlink	*wl;
 | 
				
			||||||
	struct environ	 env;
 | 
						struct environ	 env;
 | 
				
			||||||
	const char	*shell;
 | 
						const char	*shell;
 | 
				
			||||||
	u_int		 hlimit;
 | 
						u_int		 hlimit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if ((wl = winlink_add(&s->windows, idx)) == NULL) {
 | 
				
			||||||
 | 
							xasprintf(cause, "index in use: %d", idx);
 | 
				
			||||||
 | 
							return (NULL);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	environ_init(&env);
 | 
						environ_init(&env);
 | 
				
			||||||
	environ_copy(&global_environ, &env);
 | 
						environ_copy(&global_environ, &env);
 | 
				
			||||||
	environ_copy(&s->environ, &env);
 | 
						environ_copy(&s->environ, &env);
 | 
				
			||||||
@@ -229,15 +235,18 @@ session_new(struct session *s,
 | 
				
			|||||||
	w = window_create(
 | 
						w = window_create(
 | 
				
			||||||
	    name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause);
 | 
						    name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause);
 | 
				
			||||||
	if (w == NULL) {
 | 
						if (w == NULL) {
 | 
				
			||||||
 | 
							winlink_remove(&s->windows, wl);
 | 
				
			||||||
		environ_free(&env);
 | 
							environ_free(&env);
 | 
				
			||||||
		return (NULL);
 | 
							return (NULL);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						winlink_set_window(wl, w);
 | 
				
			||||||
	environ_free(&env);
 | 
						environ_free(&env);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (options_get_number(&s->options, "set-remain-on-exit"))
 | 
						if (options_get_number(&s->options, "set-remain-on-exit"))
 | 
				
			||||||
		options_set_number(&w->options, "remain-on-exit", 1);
 | 
							options_set_number(&w->options, "remain-on-exit", 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return (session_attach(s, w, idx, cause));
 | 
						session_group_synchronize_from(s);
 | 
				
			||||||
 | 
						return (wl);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Attach a window to a session. */
 | 
					/* Attach a window to a session. */
 | 
				
			||||||
@@ -246,8 +255,12 @@ session_attach(struct session *s, struct window *w, int idx, char **cause)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	struct winlink	*wl;
 | 
						struct winlink	*wl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ((wl = winlink_add(&s->windows, w, idx)) == NULL)
 | 
						if ((wl = winlink_add(&s->windows, idx)) == NULL) {
 | 
				
			||||||
		xasprintf(cause, "index in use: %d", idx);
 | 
							xasprintf(cause, "index in use: %d", idx);
 | 
				
			||||||
 | 
							return (NULL);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						winlink_set_window(wl, w);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	session_group_synchronize_from(s);
 | 
						session_group_synchronize_from(s);
 | 
				
			||||||
	return (wl);
 | 
						return (wl);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -526,7 +539,8 @@ session_group_synchronize1(struct session *target, struct session *s)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	/* Link all the windows from the target. */
 | 
						/* Link all the windows from the target. */
 | 
				
			||||||
	RB_FOREACH(wl, winlinks, ww) {
 | 
						RB_FOREACH(wl, winlinks, ww) {
 | 
				
			||||||
		wl2 = winlink_add(&s->windows, wl->window, wl->idx);
 | 
							wl2 = winlink_add(&s->windows, wl->idx);
 | 
				
			||||||
 | 
							winlink_set_window(wl2, wl->window);
 | 
				
			||||||
		wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
 | 
							wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										3
									
								
								tmux.h
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								tmux.h
									
									
									
									
									
								
							@@ -1828,7 +1828,8 @@ struct winlink	*winlink_find_by_index(struct winlinks *, int);
 | 
				
			|||||||
struct winlink	*winlink_find_by_window(struct winlinks *, struct window *);
 | 
					struct winlink	*winlink_find_by_window(struct winlinks *, struct window *);
 | 
				
			||||||
int		 winlink_next_index(struct winlinks *, int);
 | 
					int		 winlink_next_index(struct winlinks *, int);
 | 
				
			||||||
u_int		 winlink_count(struct winlinks *);
 | 
					u_int		 winlink_count(struct winlinks *);
 | 
				
			||||||
struct winlink	*winlink_add(struct winlinks *, struct window *, int);
 | 
					struct winlink	*winlink_add(struct winlinks *, int);
 | 
				
			||||||
 | 
					void		 winlink_set_window(struct winlink *, struct window *);
 | 
				
			||||||
void		 winlink_remove(struct winlinks *, struct winlink *);
 | 
					void		 winlink_remove(struct winlinks *, struct winlink *);
 | 
				
			||||||
struct winlink	*winlink_next(struct winlink *);
 | 
					struct winlink	*winlink_next(struct winlink *);
 | 
				
			||||||
struct winlink	*winlink_previous(struct winlink *);
 | 
					struct winlink	*winlink_previous(struct winlink *);
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										14
									
								
								window.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								window.c
									
									
									
									
									
								
							@@ -123,7 +123,7 @@ winlink_count(struct winlinks *wwl)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct winlink *
 | 
					struct winlink *
 | 
				
			||||||
winlink_add(struct winlinks *wwl, struct window *w, int idx)
 | 
					winlink_add(struct winlinks *wwl, int idx)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct winlink	*wl;
 | 
						struct winlink	*wl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -135,14 +135,18 @@ winlink_add(struct winlinks *wwl, struct window *w, int idx)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	wl = xcalloc(1, sizeof *wl);
 | 
						wl = xcalloc(1, sizeof *wl);
 | 
				
			||||||
	wl->idx = idx;
 | 
						wl->idx = idx;
 | 
				
			||||||
	wl->window = w;
 | 
					 | 
				
			||||||
	RB_INSERT(winlinks, wwl, wl);
 | 
						RB_INSERT(winlinks, wwl, wl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	w->references++;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return (wl);
 | 
						return (wl);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					winlink_set_window(struct winlink *wl, struct window *w)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						wl->window = w;
 | 
				
			||||||
 | 
						w->references++;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
winlink_remove(struct winlinks *wwl, struct winlink *wl)
 | 
					winlink_remove(struct winlinks *wwl, struct winlink *wl)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -153,12 +157,14 @@ winlink_remove(struct winlinks *wwl, struct winlink *wl)
 | 
				
			|||||||
		xfree(wl->status_text);
 | 
							xfree(wl->status_text);
 | 
				
			||||||
	xfree(wl);
 | 
						xfree(wl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (w != NULL) {
 | 
				
			||||||
		if (w->references == 0)
 | 
							if (w->references == 0)
 | 
				
			||||||
			fatal("bad reference count");
 | 
								fatal("bad reference count");
 | 
				
			||||||
		w->references--;
 | 
							w->references--;
 | 
				
			||||||
		if (w->references == 0)
 | 
							if (w->references == 0)
 | 
				
			||||||
			window_destroy(w);
 | 
								window_destroy(w);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct winlink *
 | 
					struct winlink *
 | 
				
			||||||
winlink_next(struct winlink *wl)
 | 
					winlink_next(struct winlink *wl)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user