[vlc-devel] commit: screensaver: convert to POSIX spawn ( Rémi Denis-Courmont )

git version control git at videolan.org
Tue Dec 29 19:20:01 CET 2009


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Dec 29 20:19:34 2009 +0200| [860a707fa6b05eb59ee15da833af7d655176ff65] | committer: Rémi Denis-Courmont 

screensaver: convert to POSIX spawn

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=860a707fa6b05eb59ee15da833af7d655176ff65
---

 modules/misc/screensaver.c |   64 ++++++++++++++++++++++++--------------------
 1 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/modules/misc/screensaver.c b/modules/misc/screensaver.c
index 057bfe0..92fee38 100644
--- a/modules/misc/screensaver.c
+++ b/modules/misc/screensaver.c
@@ -33,11 +33,13 @@
 #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);
 }




More information about the vlc-devel mailing list