Revert "Kill MicroB browser UI processes instead of using exit_browser"
[browser-switch] / launcher.c
index 2667623..6759813 100644 (file)
@@ -26,6 +26,8 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <dbus/dbus-glib.h>
 
 #ifdef FREMANTLE
 #include <dbus/dbus-glib.h>
 
 #ifdef FREMANTLE
@@ -103,6 +105,20 @@ static DBusHandlerResult check_microb_finished(DBusConnection *connection,
 }
 #endif
 
 }
 #endif
 
+/* Close stdin/stdout/stderr and replace with /dev/null */
+static int close_stdio(void) {
+       int fd;
+
+       if ((fd = open("/dev/null", O_RDWR)) == -1)
+               return -1;
+
+       if (dup2(fd, 0) == -1 || dup2(fd, 1) == -1 || dup2(fd, 2) == -1)
+               return -1;
+
+       close(fd);
+       return 0;
+}
+
 static void launch_tear(struct swb_context *ctx, char *uri) {
        int status;
        static DBusGProxy *tear_proxy = NULL;
 static void launch_tear(struct swb_context *ctx, char *uri) {
        int status;
        static DBusGProxy *tear_proxy = NULL;
@@ -139,6 +155,7 @@ static void launch_tear(struct swb_context *ctx, char *uri) {
                        }
                        /* Child process */
                        setsid();
                        }
                        /* Child process */
                        setsid();
+                       close_stdio();
                }
                execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
        }
                }
                execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
        }
@@ -162,13 +179,13 @@ void launch_microb(struct swb_context *ctx, char *uri) {
        printf("launch_microb with uri '%s'\n", uri);
 
        /* Launch browserd if it's not running */
        printf("launch_microb with uri '%s'\n", uri);
 
        /* Launch browserd if it's not running */
-       status = system("pidof /usr/sbin/browserd > /dev/null");
+       status = system("pidof browserd > /dev/null");
        if (WIFEXITED(status) && WEXITSTATUS(status)) {
                kill_browserd = 1;
 #ifdef FREMANTLE
        if (WIFEXITED(status) && WEXITSTATUS(status)) {
                kill_browserd = 1;
 #ifdef FREMANTLE
-               system("/usr/sbin/browserd -d -b");
+               system("/usr/sbin/browserd -d -b > /dev/null 2>&1");
 #else
 #else
-               system("/usr/sbin/browserd -d");
+               system("/usr/sbin/browserd -d > /dev/null 2>&1");
 #endif
        }
 
 #endif
        }
 
@@ -358,6 +375,8 @@ void launch_microb(struct swb_context *ctx, char *uri) {
                g_object_unref(g_proxy);
        } else {
                /* Child process */
                g_object_unref(g_proxy);
        } else {
                /* Child process */
+               close_stdio();
+
                /* exec maemo-invoker directly instead of relying on the
                   /usr/bin/browser symlink, since /usr/bin/browser may have
                   been replaced with a shell script calling us via D-Bus */
                /* exec maemo-invoker directly instead of relying on the
                   /usr/bin/browser symlink, since /usr/bin/browser may have
                   been replaced with a shell script calling us via D-Bus */
@@ -372,6 +391,8 @@ void launch_microb(struct swb_context *ctx, char *uri) {
                waitpid(pid, &status, 0);
        } else {
                /* Child process */
                waitpid(pid, &status, 0);
        } else {
                /* Child process */
+               close_stdio();
+
                /* exec maemo-invoker directly instead of relying on the
                   /usr/bin/browser symlink, since /usr/bin/browser may have
                   been replaced with a shell script calling us via D-Bus */
                /* exec maemo-invoker directly instead of relying on the
                   /usr/bin/browser symlink, since /usr/bin/browser may have
                   been replaced with a shell script calling us via D-Bus */
@@ -387,7 +408,7 @@ void launch_microb(struct swb_context *ctx, char *uri) {
 
        /* Kill off browserd if we started it */
        if (kill_browserd)
 
        /* Kill off browserd if we started it */
        if (kill_browserd)
-               system("kill `pidof /usr/sbin/browserd`");
+               system("kill `pidof browserd`");
 
        if (!ctx || !ctx->continuous_mode) 
                exit(0);
 
        if (!ctx || !ctx->continuous_mode) 
                exit(0);
@@ -467,6 +488,7 @@ static void launch_other_browser(struct swb_context *ctx, char *uri) {
                }
                /* Child process */
                setsid();
                }
                /* Child process */
                setsid();
+               close_stdio();
        }
        execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
 }
        }
        execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
 }