X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?p=xscreensaver;a=blobdiff_plain;f=utils%2Fgrabclient.c;h=7b5298dffd156f653653a550fae454f332de4dab;hp=0568d9b911557479c89c795d5fe4399e42efc1d0;hb=e4fa2ac140f7bc56571373a7b7eb585fa4500e38;hpb=96a411663168b0ba5432b407a83be55f3df0c802 diff --git a/utils/grabclient.c b/utils/grabclient.c index 0568d9b9..7b5298df 100644 --- a/utils/grabclient.c +++ b/utils/grabclient.c @@ -26,6 +26,14 @@ #include "vroot.h" #include +#ifdef HAVE_UNISTD_H +# include +#endif +#ifdef HAVE_SYS_WAIT_H +# include /* for waitpid() and associated macros */ +#endif + + extern char *progname; @@ -176,6 +184,56 @@ hack_subproc_environment (Display *dpy) } +/* Spawn a program, and wait for it to finish. + If we just use system() for this, then sometimes the subprocess + doesn't die when *this* process is sent a TERM signal. Perhaps + this is due to the intermediate /bin/sh that system() uses to + parse arguments? I'm not sure. But using fork() and execvp() + here seems to close the race. + */ + +static void +exec_simple_command (const char *command) +{ + char *av[1024]; + int ac = 0; + char *token = strtok (strdup(command), " \t"); + while (token) + { + av[ac++] = token; + token = strtok(0, " \t"); + } + av[ac] = 0; + + execvp (av[0], av); /* shouldn't return. */ +} + +static void +fork_exec_wait (const char *command) +{ + char buf [255]; + pid_t forked; + int status; + + switch ((int) (forked = fork ())) + { + case -1: + sprintf (buf, "%s: couldn't fork", progname); + perror (buf); + return; + + case 0: + exec_simple_command (command); + exit (1); /* exits child fork */ + break; + + default: + waitpid (forked, &status, 0); + break; + } +} + + /* Loads an image into the Drawable. When grabbing desktop images, the Window will be unmapped first. */ @@ -216,7 +274,7 @@ load_random_image (Screen *screen, Window window, Drawable drawable, XSync (dpy, True); hack_subproc_environment (dpy); - system (cmd); + fork_exec_wait (cmd); free (cmd); XSync (dpy, True);