Remove double fork for spawn and kill status with SIGTERM

This commit is contained in:
Philippe Pepiot 2010-12-03 16:59:07 +01:00
parent d0058146dd
commit c8a74878a1
5 changed files with 25 additions and 45 deletions

View File

@ -695,7 +695,5 @@ getevent(XEvent ev)
break;
}
wait((int[]){0});
return;
}

View File

@ -55,12 +55,24 @@ const func_name_list_t layout_list[] =
{ NULL, NULL }
};
static void sigchld(int);
static void
sigchld(int sig)
{
(void)sig;
if (signal(SIGCHLD, sigchld) == SIG_ERR)
warn("signal(SIGCHLD)");
while (waitpid(-1, NULL, WNOHANG) > 0);
}
/** Init WMFS
*/
void
init(void)
{
/* First init */
sigchld(0);
ewmh_init_hints();
init_conf();
init_gc();

View File

@ -30,6 +30,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include "wmfs.h"
/** malloc with error support and size_t overflow protection
@ -252,15 +253,14 @@ alias_to_str(char *conf_choice)
* \param cmd Command
* \return child pid
*/
int
pid_t
spawn(const char *format, ...)
{
char *sh = NULL;
char cmd[512];
va_list ap;
pid_t pid, ret;
int p[2];
size_t len;
pid_t pid;
va_start(ap, format);
len = vsnprintf(cmd, sizeof(cmd), format, ap);
@ -275,48 +275,18 @@ spawn(const char *format, ...)
if(!(sh = getenv("SHELL")))
sh = "/bin/sh";
if (pipe(p) == -1)
{
warn("pipe");
return -1;
}
if((pid = fork()) == 0)
{
close(p[0]);
if((pid = fork()) == 0)
{
if(dpy)
close(ConnectionNumber(dpy));
setsid();
execl(sh, sh, "-c", cmd, (char*)NULL);
exit(EXIT_FAILURE);
}
if (sizeof(pid_t) != write(p[1], &pid, sizeof(pid_t)))
warn("write");
close(p[1]);
exit(EXIT_SUCCESS);
}
else if (pid != -1)
{
close(p[1]);
if (sizeof(pid_t) != read(p[0], &ret, sizeof(pid_t)))
{
warn("read");
ret = -1;
}
close(p[0]);
waitpid(pid, NULL, 0);
}
else
{
warn("fork");
ret = -1;
if(dpy)
close(ConnectionNumber(dpy));
setsid();
execl(sh, sh, "-c", cmd, (char*)NULL);
err(1, "execl(%s)", cmd);
}
if (pid == -1)
warn("fork()");
return ret;
return pid;
}
/** Swap two pointer.

View File

@ -134,7 +134,7 @@ quit(void)
/* kill status script */
if (conf.status_pid != (pid_t)-1)
kill(conf.status_pid, SIGQUIT);
kill(conf.status_pid, SIGTERM);
return;
}

View File

@ -309,7 +309,7 @@ Layout layout_name_to_struct(Layout lt[], char *name, int n, const func_name_lis
char* alias_to_str(char *conf_choice);
/* }}} */
char *char_to_str(const char c);
int spawn(const char *str, ...);
pid_t spawn(const char *str, ...);
void swap_ptr(void **x, void **y);
void uicb_spawn(uicb_t);
char *clean_value(char *str);