mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	#187, let init handle child processes
This commit is contained in:
		
							parent
							
								
									d2680ac8fe
								
							
						
					
					
						commit
						18f4905e62
					
				
					 2 changed files with 12 additions and 13 deletions
				
			
		| 
						 | 
					@ -9,6 +9,7 @@
 | 
				
			||||||
#include <unistd.h>
 | 
					#include <unistd.h>
 | 
				
			||||||
#include <ctype.h>
 | 
					#include <ctype.h>
 | 
				
			||||||
#include <sys/types.h>
 | 
					#include <sys/types.h>
 | 
				
			||||||
 | 
					#include <sys/wait.h>
 | 
				
			||||||
#include "stringop.h"
 | 
					#include "stringop.h"
 | 
				
			||||||
#include "layout.h"
 | 
					#include "layout.h"
 | 
				
			||||||
#include "focus.h"
 | 
					#include "focus.h"
 | 
				
			||||||
| 
						 | 
					@ -190,18 +191,25 @@ static enum cmd_status cmd_exec_always(int argc, char **argv) {
 | 
				
			||||||
	char cmd[4096];
 | 
						char cmd[4096];
 | 
				
			||||||
	strcpy(cmd, tmp);
 | 
						strcpy(cmd, tmp);
 | 
				
			||||||
	free(tmp);
 | 
						free(tmp);
 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	char *args[] = {"sh", "-c", cmd, 0 };
 | 
					 | 
				
			||||||
	sway_log(L_DEBUG, "Executing %s", cmd);
 | 
						sway_log(L_DEBUG, "Executing %s", cmd);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pid_t pid;
 | 
						pid_t pid;
 | 
				
			||||||
 | 
						// Fork process
 | 
				
			||||||
	if ((pid = fork()) == 0) {
 | 
						if ((pid = fork()) == 0) {
 | 
				
			||||||
		execv("/bin/sh", args);
 | 
							// Fork child process again
 | 
				
			||||||
		_exit(-1);
 | 
							setsid();
 | 
				
			||||||
 | 
							if (fork() == 0) {
 | 
				
			||||||
 | 
								execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL);
 | 
				
			||||||
 | 
								/* Not reached */
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							// Close child process
 | 
				
			||||||
 | 
							_exit(0);
 | 
				
			||||||
	} else if (pid < 0) {
 | 
						} else if (pid < 0) {
 | 
				
			||||||
		sway_log(L_ERROR, "exec command failed, sway could not fork");
 | 
							sway_log(L_ERROR, "exec command failed, sway could not fork");
 | 
				
			||||||
		return CMD_FAILURE;
 | 
							return CMD_FAILURE;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						// cleanup child process
 | 
				
			||||||
 | 
						wait(0);
 | 
				
			||||||
	return CMD_SUCCESS;
 | 
						return CMD_SUCCESS;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,8 +22,6 @@ void sway_terminate(void) {
 | 
				
			||||||
	wlc_terminate();
 | 
						wlc_terminate();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void sigchld_handle(int signal);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static void wlc_log_handler(enum wlc_log_type type, const char *str) {
 | 
					static void wlc_log_handler(enum wlc_log_type type, const char *str) {
 | 
				
			||||||
	if (type == WLC_LOG_ERROR) {
 | 
						if (type == WLC_LOG_ERROR) {
 | 
				
			||||||
		sway_log(L_ERROR, "[wlc] %s", str);
 | 
							sway_log(L_ERROR, "[wlc] %s", str);
 | 
				
			||||||
| 
						 | 
					@ -64,9 +62,6 @@ int main(int argc, char **argv) {
 | 
				
			||||||
		{0, 0, 0, 0}
 | 
							{0, 0, 0, 0}
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Signal handling */
 | 
					 | 
				
			||||||
	signal(SIGCHLD, sigchld_handle);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	setenv("WLC_DIM", "0", 0);
 | 
						setenv("WLC_DIM", "0", 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	wlc_log_set_handler(wlc_log_handler);
 | 
						wlc_log_set_handler(wlc_log_handler);
 | 
				
			||||||
| 
						 | 
					@ -153,7 +148,3 @@ int main(int argc, char **argv) {
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sigchld_handle(int signal) {
 | 
					 | 
				
			||||||
	(void) signal;
 | 
					 | 
				
			||||||
	while (waitpid((pid_t)-1, 0, WNOHANG) > 0);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue