Also use freopen() instead of close/dup or dup2 for redirecting standard
io streams to /dev/null.
---
 avserver.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/avserver.c b/avserver.c
index 994edcd0..22119f8 100644
--- a/avserver.c
+++ b/avserver.c
@@ -25,6 +25,7 @@
 #endif
 #include <string.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include "libavformat/avformat.h"
 // FIXME those are internal headers, avserver _really_ shouldn't use them
 #include "libavformat/ffm.h"
@@ -494,7 +495,7 @@ static void start_children(FFStream *feed)
                 /* In child */
                 char pathname[1024];
                 char *slash;
-                int i;
+                int i, retval;
 
                 av_strlcpy(pathname, my_program_name, sizeof(pathname));
 
@@ -514,18 +515,20 @@ static void start_children(FFStream *feed)
                 for (i = 3; i < 256; i++)
                     close(i);
 
-                if (!avserver_debug) {
-                    i = open("/dev/null", O_RDWR);
-                    if (i != -1) {
-                        dup2(i, 0);
-                        dup2(i, 1);
-                        dup2(i, 2);
-                        close(i);
-                    }
-                }
 
                 /* This is needed to make relative pathnames work */
-                chdir(my_program_dir);
+                retval = chdir(my_program_dir);
+                if (retval = -1)
+                    perror("chdir to work directory failed");
+
+                if (!avserver_debug) {
+                    if (!freopen("/dev/null", "r", stdin))
+                        http_log("failed to redirect STDIN to /dev/null\n;");
+                    if (!freopen("/dev/null", "w", stdout))
+                        http_log("failed to redirect STDOUT to /dev/null\n;");
+                    if (!freopen("/dev/null", "w", stderr))
+                        http_log("failed to redirect STDERR to /dev/null\n;");
+                }
 
                 signal(SIGPIPE, SIG_DFL);
 
@@ -4730,14 +4733,14 @@ int main(int argc, char **argv)
         } else {
             /* child */
             setsid();
-            close(0);
-            open("/dev/null", O_RDWR);
+            fclose(stdin);
             if (strcmp(logfilename, "-") != 0) {
-                close(1);
-                dup(0);
+                if (!freopen("/dev/null", "w", stdout))
+                    http_log("failed to redirect STDOUT to /dev/null\n");
             }
-            close(2);
-            dup(0);
+            if (!freopen("/dev/null", "w", stderr))
+                http_log("failed to redirect STDERR to /dev/null\n;");
+
         }
     }
 
@@ -4745,7 +4748,8 @@ int main(int argc, char **argv)
     signal(SIGPIPE, SIG_IGN);
 
     if (avserver_daemon)
-        chdir("/");
+        if (chdir("/"))
+            http_log("failed to chdir to /\n");
 
     if (http_server() < 0) {
         http_log("Could not start server\n");
-- 
1.7.12.4

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to