]> git.sesse.net Git - vlc/commitdiff
screensaver: convert to POSIX spawn
authorRémi Denis-Courmont <remi@remlab.net>
Tue, 29 Dec 2009 18:19:34 +0000 (20:19 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Tue, 29 Dec 2009 18:19:34 +0000 (20:19 +0200)
modules/misc/screensaver.c

index 057bfe048a2365a6ff58d951c942f12a9af2f63b..92fee3853245676fae8d2059a071c78f88e576cc 100644 (file)
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_inhibit.h>
+#include <vlc_charset.h>
 
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <unistd.h>
+#include <fcntl.h>
 #include <signal.h>
+#include <spawn.h>
 
 /*****************************************************************************
  * 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);
 }