From: RĂ©mi Denis-Courmont Date: Tue, 29 Dec 2009 18:19:34 +0000 (+0200) Subject: screensaver: convert to POSIX spawn X-Git-Tag: 1.1.0-ff~1628 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=860a707fa6b05eb59ee15da833af7d655176ff65;p=vlc screensaver: convert to POSIX spawn --- diff --git a/modules/misc/screensaver.c b/modules/misc/screensaver.c index 057bfe048a..92fee38532 100644 --- a/modules/misc/screensaver.c +++ b/modules/misc/screensaver.c @@ -33,11 +33,13 @@ #include #include #include +#include #include #include -#include +#include #include +#include /***************************************************************************** * Local prototypes @@ -51,8 +53,12 @@ static void Inhibit( vlc_inhibit_t *, bool ); struct vlc_inhibit_sys { vlc_timer_t timer; + posix_spawn_file_actions_t actions; + posix_spawnattr_t attr; + int nullfd; }; +extern char **environ; /***************************************************************************** * Module descriptor @@ -82,6 +88,21 @@ static int Activate( vlc_object_t *p_this ) } p_ih->inhibit = Inhibit; + int fd = utf8_open ("/dev/null", O_WRONLY); + posix_spawn_file_actions_init (&p_sys->actions); + if (fd != -1) + { + posix_spawn_file_actions_adddup2 (&p_sys->actions, fd, 1); + posix_spawn_file_actions_adddup2 (&p_sys->actions, fd, 2); + posix_spawn_file_actions_addclose (&p_sys->actions, fd); + } + p_sys->nullfd = fd; + + sigset_t set; + posix_spawnattr_init (&p_sys->attr); + sigemptyset (&set); + posix_spawnattr_setsigmask (&p_sys->attr, &set); + return VLC_SUCCESS; } @@ -94,7 +115,10 @@ static void Deactivate( vlc_object_t *p_this ) vlc_inhibit_sys_t *p_sys = p_ih->p_sys; vlc_timer_destroy( p_sys->timer ); - + if (p_sys->nullfd != -1) + close (p_sys->nullfd); + posix_spawnattr_destroy (&p_sys->attr); + posix_spawn_file_actions_destroy (&p_sys->actions); free( p_sys ); } @@ -107,33 +131,15 @@ static void Inhibit( vlc_inhibit_t *p_ih, bool suspend ) /***************************************************************************** * Execute: Spawns a process using execv() *****************************************************************************/ -static void Execute( vlc_object_t *p_this, const char *const *ppsz_args ) +static void Execute (vlc_inhibit_t *p_ih, const char *const *argv) { - pid_t pid = fork(); - switch( pid ) + vlc_inhibit_sys_t *p_sys = p_ih->p_sys; + pid_t pid; + + if (posix_spawn (&pid, argv[0], &p_sys->actions, &p_sys->attr, + (char **)argv, environ) == 0) { - case 0: /* we're the child */ - { - sigset_t set; - sigemptyset (&set); - pthread_sigmask (SIG_SETMASK, &set, NULL); - - /* We don't want output */ - if( ( freopen( "/dev/null", "w", stdout ) != NULL ) - && ( freopen( "/dev/null", "w", stderr ) != NULL ) ) - execv( ppsz_args[0] , (char *const *)ppsz_args ); - /* If the file we want to execute doesn't exist we exit() */ - exit( EXIT_FAILURE ); - } - case -1: /* we're the error */ - msg_Dbg( p_this, "Couldn't fork() while launching %s", - ppsz_args[0] ); - break; - default: /* we're the parent */ - /* Wait for the child to exit. - * We will not deadlock because we ran "/bin/sh &" */ - while( waitpid( pid, NULL, 0 ) != pid); - break; + while (waitpid (pid, NULL, 0) != pid); } } @@ -151,9 +157,9 @@ static void Timer( void *data ) /* http://www.jwz.org/xscreensaver/faq.html#dvd */ const char *const ppsz_xsargs[] = { "/bin/sh", "-c", "xscreensaver-command -deactivate &", (char*)NULL }; - Execute( VLC_OBJECT(p_ih), ppsz_xsargs ); + Execute (p_ih, ppsz_xsargs); const char *const ppsz_gsargs[] = { "/bin/sh", "-c", "gnome-screensaver-command --poke &", (char*)NULL }; - Execute( VLC_OBJECT(p_ih), ppsz_gsargs ); + Execute (p_ih, ppsz_gsargs); }