resource-asm: close all file descriptors when forking.
authorIsmo Puustinen <ismo.puustinen@intel.com>
Tue, 11 Dec 2012 12:54:51 +0000 (14:54 +0200)
committerKrisztian Litkey <krisztian.litkey@intel.com>
Thu, 8 Jan 2015 16:37:09 +0000 (18:37 +0200)
src/plugins/plugin-resource-asm.c
src/plugins/resource-asm/asm-bridge.c

index 306a30a..8a489f4 100644 (file)
@@ -38,6 +38,7 @@
 #include <sys/stat.h>
 #include <signal.h>
 #include <wait.h>
+#include <fcntl.h>
 
 #include <audio-session-manager.h>
 
@@ -966,6 +967,43 @@ static void htbl_free_client(void *key, void *object)
     mrp_free(client);
 }
 
+static int close_fds()
+{
+    int maxfd;
+    int i;
+    int newin, newout, newerr;
+
+    /* Closing all file descriptors in a protable way is tricky, so improve
+       this function as we go. */
+
+    maxfd = sysconf(_SC_OPEN_MAX);
+
+    for (i = 0; i < maxfd; i++) {
+        if (i != fileno(stdin) && i != fileno(stdout) && i != fileno(stderr))
+            close(i);
+    }
+
+    /* redirect the streams to /dev/null */
+
+    newin = open("/dev/null", O_RDONLY);
+    newout = open("/dev/null", O_WRONLY);
+    newerr = open("/dev/null", O_WRONLY);
+
+    if (newin < 0 || newout < 0 || newerr < 0)
+        return -1;
+
+    if (dup2(newin, fileno(stdin)) < 0 ||
+        dup2(newout, fileno(stdout)) < 0 ||
+        dup2(newerr, fileno(stderr)) < 0)
+        return -1;
+
+    close(newin);
+    close(newout);
+    close(newerr);
+
+    return 0;
+}
+
 static int asm_init(mrp_plugin_t *plugin)
 {
     mrp_plugin_arg_t *args = plugin->args;
@@ -1054,6 +1092,10 @@ static int asm_init(mrp_plugin_t *plugin)
     }
     else if (pid == 0) {
         /* child */
+        if (close_fds() < 0) {
+            mrp_log_error("close_fds() failed");
+            exit(1);
+        }
         execl(ctx->binary, ctx->binary, ctx->address, NULL);
         exit(1);
     }
index 62d6983..16d8e23 100644 (file)
@@ -65,6 +65,7 @@ struct watched_file {
     ctx_t *ctx;
 };
 
+
 static void *wait_queue (void *arg) {
     ASM_msg_lib_to_asm_t msg;
 
@@ -424,13 +425,6 @@ static int connect_to_murphy(char *address, ctx_t *ctx)
         goto error;
     }
 
-#if 0
-    if (!mrp_transport_bind(ctx->mt, &addr, alen)) {
-        mrp_log_error("Failed to bind the transport to address '%s'", address);
-        goto error;
-    }
-#endif
-
     if (!mrp_transport_connect(ctx->mt, &addr, alen)) {
         mrp_log_error("Failed to connect the transport");
         goto error;