2 * launcher.c -- functions for launching web browsers for browser-switchboard
4 * Copyright (C) 2009-2010 Steven Luo
5 * Derived from a Python implementation by Jason Simpson and Steven Luo
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #include <sys/types.h>
31 #include <dbus/dbus-glib.h>
34 #include <dbus/dbus.h>
37 #include <sys/ptrace.h>
38 #include <sys/inotify.h>
40 #define DEFAULT_HOMEDIR "/home/user"
41 #define MICROB_PROFILE_DIR "/.mozilla/microb"
42 #define MICROB_LOCKFILE "lock"
45 #include "browser-switchboard.h"
47 #include "dbus-server-bindings.h"
49 #define LAUNCH_DEFAULT_BROWSER launch_microb
52 static int microb_started = 0;
54 /* Check to see whether MicroB is ready to handle D-Bus requests yet
55 See the comments in launch_microb to understand how this works. */
56 static DBusHandlerResult check_microb_started(DBusConnection *connection,
60 char *name, *old, *new;
62 printf("Checking to see if MicroB is ready\n");
63 dbus_error_init(&error);
64 if (!dbus_message_get_args(message, &error,
65 DBUS_TYPE_STRING, &name,
66 DBUS_TYPE_STRING, &old,
67 DBUS_TYPE_STRING, &new,
69 printf("%s\n", error.message);
70 dbus_error_free(&error);
71 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
73 /* If old is an empty string, then the name has been acquired, and
74 MicroB should be ready to handle our request */
75 if (strlen(old) == 0) {
76 printf("MicroB ready\n");
80 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
83 /* Get a browserd PID from the corresponding Mozilla profile lockfile */
84 static pid_t get_browserd_pid(const char *lockfile) {
87 /* The lockfile is a symlink pointing to "[ipaddr]:+[pid]", so read in
88 the target of the symlink and parse it that way */
89 memset(buf, '\0', 256);
90 if (readlink(lockfile, buf, 255) == -1)
92 if (!(tmp = strstr(buf, ":+")))
94 tmp += 2; /* Skip over the ":+" */
100 /* Close stdin/stdout/stderr and replace with /dev/null */
101 static int close_stdio(void) {
104 if ((fd = open("/dev/null", O_RDWR)) == -1)
107 if (dup2(fd, 0) == -1 || dup2(fd, 1) == -1 || dup2(fd, 2) == -1)
114 static void launch_tear(struct swb_context *ctx, char *uri) {
116 static DBusGProxy *tear_proxy = NULL;
117 GError *error = NULL;
123 printf("launch_tear with uri '%s'\n", uri);
125 /* We should be able to just call the D-Bus service to open Tear ...
126 but if Tear's not open, that cuases D-Bus to start Tear and then
127 pass it the OpenAddress call, which results in two browser windows.
128 Properly fixing this probably requires Tear to provide a D-Bus
129 method that opens an address in an existing window, but for now work
130 around by just invoking Tear with exec() if it's not running. */
131 status = system("pidof tear > /dev/null");
132 if (WIFEXITED(status) && !WEXITSTATUS(status)) {
134 if (!(tear_proxy = dbus_g_proxy_new_for_name(
138 "com.nokia.Tear"))) {
139 printf("Failed to create proxy for com.nokia.Tear D-Bus interface\n");
144 if (!dbus_g_proxy_call(tear_proxy, "OpenAddress", &error,
145 G_TYPE_STRING, uri, G_TYPE_INVALID,
147 printf("Opening window failed: %s\n", error->message);
150 if (!ctx->continuous_mode)
153 if (ctx->continuous_mode) {
154 if ((pid = fork()) != 0) {
155 /* Parent process or error in fork() */
156 printf("child: %d\n", (int)pid);
163 execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
167 void launch_microb(struct swb_context *ctx, char *uri) {
168 int kill_browserd = 0;
172 char *homedir, *microb_profile_dir, *microb_lockfile;
175 DBusConnection *raw_connection;
176 DBusError dbus_error;
177 DBusHandleMessageFunction filter_func;
179 GError *gerror = NULL;
182 struct inotify_event *event;
183 pid_t browserd_pid, waited_pid;
184 struct sigaction act, oldact;
191 printf("launch_microb with uri '%s'\n", uri);
193 /* Launch browserd if it's not running */
194 status = system("pidof browserd > /dev/null");
195 if (WIFEXITED(status) && WEXITSTATUS(status)) {
198 system("/usr/sbin/browserd -d -b > /dev/null 2>&1");
200 system("/usr/sbin/browserd -d > /dev/null 2>&1");
204 /* Release the osso_browser D-Bus name so that MicroB can take it */
205 dbus_release_osso_browser_name(ctx);
207 if ((pid = fork()) == -1) {
212 /* Put together the path to the MicroB browserd lockfile */
213 if (!(homedir = getenv("HOME")))
214 homedir = DEFAULT_HOMEDIR;
215 len = strlen(homedir) + strlen(MICROB_PROFILE_DIR) + 1;
216 if (!(microb_profile_dir = calloc(len, sizeof(char)))) {
217 printf("calloc() failed\n");
220 snprintf(microb_profile_dir, len, "%s%s",
221 homedir, MICROB_PROFILE_DIR);
222 len = strlen(homedir) + strlen(MICROB_PROFILE_DIR) +
223 strlen("/") + strlen(MICROB_LOCKFILE) + 1;
224 if (!(microb_lockfile = calloc(len, sizeof(char)))) {
225 printf("calloc() failed\n");
228 snprintf(microb_lockfile, len, "%s%s/%s",
229 homedir, MICROB_PROFILE_DIR, MICROB_LOCKFILE);
231 /* Watch for the creation of a MicroB browserd lockfile
232 NB: The watch has to be set up here, before the browser
233 is launched, to make sure there's no race between browserd
234 starting and us creating the watch */
235 if ((fd = inotify_init()) == -1) {
236 perror("inotify_init");
239 if ((inot_wd = inotify_add_watch(fd, microb_profile_dir,
241 perror("inotify_add_watch");
244 free(microb_profile_dir);
248 /* Wait for our child to start the browser UI process and
249 for it to acquire the com.nokia.osso_browser D-Bus name,
250 then make the appropriate method call to open the browser
253 Ideas for how to do this monitoring derived from the
254 dbus-monitor code (tools/dbus-monitor.c in the D-Bus
257 dbus_error_init(&dbus_error);
259 raw_connection = dbus_bus_get_private(DBUS_BUS_SESSION,
261 if (!raw_connection) {
263 "Failed to open connection to session bus: %s\n",
265 dbus_error_free(&dbus_error);
269 dbus_bus_add_match(raw_connection,
270 "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='com.nokia.osso_browser'",
272 if (dbus_error_is_set(&dbus_error)) {
274 "Failed to set up watch for browser UI start: %s\n",
276 dbus_error_free(&dbus_error);
279 filter_func = check_microb_started;
280 if (!dbus_connection_add_filter(raw_connection,
281 filter_func, NULL, NULL)) {
282 fprintf(stderr, "Failed to set up watch filter!\n");
285 printf("Waiting for MicroB to start\n");
286 while (!microb_started &&
287 dbus_connection_read_write_dispatch(raw_connection,
289 dbus_connection_remove_filter(raw_connection,
291 dbus_bus_remove_match(raw_connection,
292 "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='com.nokia.osso_browser'",
294 if (dbus_error_is_set(&dbus_error))
295 /* Don't really care -- about to disconnect from the
297 dbus_error_free(&dbus_error);
298 dbus_connection_close(raw_connection);
299 dbus_connection_unref(raw_connection);
301 /* Browser UI's started, send it the request for a new window
303 g_proxy = dbus_g_proxy_new_for_name(ctx->session_bus,
304 "com.nokia.osso_browser",
305 "/com/nokia/osso_browser/request",
306 "com.nokia.osso_browser");
308 printf("Couldn't get a com.nokia.osso_browser proxy\n");
311 if (!strcmp(uri, "new_window")) {
312 #if 0 /* Since we can't detect when the bookmark window closes, we'd have a
313 corner case where, if the user just closes the bookmark window
314 without opening any browser windows, we don't kill off MicroB or
315 resume handling com.nokia.osso_browser */
316 if (!dbus_g_proxy_call(g_proxy, "top_application",
317 &gerror, G_TYPE_INVALID,
319 printf("Opening window failed: %s\n",
324 if (!dbus_g_proxy_call(g_proxy, "load_url",
326 G_TYPE_STRING, "about:blank",
329 printf("Opening window failed: %s\n",
334 if (!dbus_g_proxy_call(g_proxy, "load_url",
339 printf("Opening window failed: %s\n",
344 g_object_unref(g_proxy);
346 /* Workaround: the browser process we started is going to want
347 to hang around forever, hogging the com.nokia.osso_browser
348 D-Bus interface while at it. To fix this, we notice that
349 when the last browser window closes, the browser UI restarts
350 its attached browserd process. Get the browserd process's
351 PID and use ptrace() to watch for process termination.
353 This has the problem of not being able to detect whether
354 the bookmark window is open and/or in use, but it's the best
355 that I can think of. Better suggestions would be greatly
358 /* Wait for the MicroB browserd lockfile to be created */
359 printf("Waiting for browserd lockfile to be created\n");
360 memset(buf, '\0', 256);
361 /* read() blocks until there are events to be read */
362 while ((bytes_read = read(fd, buf, 255)) > 0) {
364 /* Loop until we see the event we're looking for
365 or until all the events are processed */
366 while (pos && (pos-buf) < bytes_read) {
367 event = (struct inotify_event *)pos;
368 len = sizeof(struct inotify_event)
370 if (!strcmp(MICROB_LOCKFILE,
372 /* Lockfile created */
375 } else if ((pos-buf) + len < bytes_read)
376 /* More events to process */
379 /* All events processed */
380 pos = buf + bytes_read;
383 /* Event found, stop looking */
385 memset(buf, '\0', 256);
387 inotify_rm_watch(fd, inot_wd);
390 /* Get the PID of the browserd from the lockfile */
391 if ((browserd_pid = get_browserd_pid(microb_lockfile)) <= 0) {
392 if (browserd_pid == 0)
393 printf("Profile lockfile link lacks PID\n");
395 printf("readlink() on lockfile failed: %s\n",
396 strerror(-browserd_pid));
399 free(microb_lockfile);
401 /* Wait for the browserd to close */
402 printf("Waiting for MicroB (browserd pid %d) to finish\n",
404 /* Clear any existing SIGCHLD handler to prevent interference
406 act.sa_handler = SIG_DFL;
408 sigemptyset(&(act.sa_mask));
409 if (sigaction(SIGCHLD, &act, &oldact) == -1) {
410 perror("clearing SIGCHLD handler failed");
414 /* Trace the browserd to get a close notification */
416 if (ptrace(PTRACE_ATTACH, browserd_pid, NULL, NULL) == -1) {
417 perror("PTRACE_ATTACH");
420 ptrace(PTRACE_CONT, browserd_pid, NULL, NULL);
421 while ((waited_pid = wait(&status)) > 0) {
422 if (waited_pid != browserd_pid)
423 /* Not interested in other processes */
425 if (WIFEXITED(status) || WIFSIGNALED(status))
426 /* browserd exited */
428 else if (WIFSTOPPED(status)) {
429 /* browserd was sent a signal
430 We're responsible for making sure this
431 signal gets delivered */
432 if (ignore_sigstop &&
433 WSTOPSIG(status) == SIGSTOP) {
434 /* Ignore the first SIGSTOP received
435 This is raised for some reason
436 immediately after we start tracing
437 the process, and won't be followed
438 by a SIGCONT at any point */
439 printf("Ignoring first SIGSTOP\n");
440 ptrace(PTRACE_CONT, browserd_pid,
445 printf("Forwarding signal %d to browserd\n",
447 ptrace(PTRACE_CONT, browserd_pid,
448 NULL, WSTOPSIG(status));
452 /* Kill off browser UI
453 XXX: There is a race here with the restarting of the closed
454 browserd; if that happens before we kill the browser UI, the
455 newly started browserd may not close with the UI
456 XXX: Hope we don't cause data loss here! */
457 printf("Killing MicroB\n");
459 waitpid(pid, &status, 0);
461 /* Restore old SIGCHLD handler */
462 if (sigaction(SIGCHLD, &oldact, NULL) == -1) {
463 perror("restoring old SIGCHLD handler failed");
471 /* exec maemo-invoker directly instead of relying on the
472 /usr/bin/browser symlink, since /usr/bin/browser may have
473 been replaced with a shell script calling us via D-Bus */
474 /* Launch the browser in the background -- our parent will
475 wait for it to claim the D-Bus name and then display the
476 window using D-Bus */
477 execl("/usr/bin/maemo-invoker", "browser", (char *)NULL);
479 #else /* !FREMANTLE */
482 waitpid(pid, &status, 0);
487 /* exec maemo-invoker directly instead of relying on the
488 /usr/bin/browser symlink, since /usr/bin/browser may have
489 been replaced with a shell script calling us via D-Bus */
490 if (!strcmp(uri, "new_window")) {
491 execl("/usr/bin/maemo-invoker",
492 "browser", (char *)NULL);
494 execl("/usr/bin/maemo-invoker",
495 "browser", "--url", uri, (char *)NULL);
498 #endif /* FREMANTLE */
500 /* Kill off browserd if we started it */
502 system("kill `pidof browserd`");
504 if (!ctx || !ctx->continuous_mode)
507 dbus_request_osso_browser_name(ctx);
510 static void launch_other_browser(struct swb_context *ctx, char *uri) {
512 char *quoted_uri, *quote;
514 size_t cmdlen, urilen;
515 size_t quoted_uri_size;
518 if (!uri || !strcmp(uri, "new_window"))
521 printf("launch_other_browser with uri '%s'\n", uri);
523 if ((urilen = strlen(uri)) > 0) {
524 /* Quote the URI to prevent the shell from interpreting it */
525 /* urilen+3 = length of URI + 2x \' + \0 */
526 if (!(quoted_uri = calloc(urilen+3, sizeof(char))))
528 snprintf(quoted_uri, urilen+3, "'%s'", uri);
530 /* If there are any 's in the original URI, URL-escape them
531 (replace them with %27) */
532 quoted_uri_size = urilen + 3;
533 quote = quoted_uri + 1;
534 while ((quote = strchr(quote, '\'')) &&
535 (offset = quote-quoted_uri) < strlen(quoted_uri)-1) {
536 /* Check to make sure we don't shrink the memory area
537 as a result of integer overflow */
538 if (quoted_uri_size+2 <= quoted_uri_size)
541 /* Grow the memory area;
542 2 = strlen("%27")-strlen("'") */
543 if (!(quoted_uri = realloc(quoted_uri,
546 quoted_uri_size = quoted_uri_size + 2;
548 /* Recalculate the location of the ' character --
549 realloc() may have moved the string in memory */
550 quote = quoted_uri + offset;
552 /* Move the string after the ', including the \0,
554 memmove(quote+3, quote+1, strlen(quote));
555 memcpy(quote, "%27", 3);
558 urilen = strlen(quoted_uri);
562 cmdlen = strlen(ctx->other_browser_cmd);
564 /* cmdlen+urilen+1 is normally two bytes longer than we need (uri will
565 replace "%s"), but is needed in the case other_browser_cmd has no %s
567 if (!(command = calloc(cmdlen+urilen+1, sizeof(char))))
569 snprintf(command, cmdlen+urilen+1, ctx->other_browser_cmd, quoted_uri);
570 printf("command: '%s'\n", command);
572 if (ctx->continuous_mode) {
574 /* Parent process or error in fork() */
584 execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
587 /* Use launch_other_browser as the default browser launcher, with the string
588 passed in as the other_browser_cmd
589 Resulting other_browser_cmd is always safe to free(), even if a pointer
590 to a string constant is passed in */
591 static void use_other_browser_cmd(struct swb_context *ctx, char *cmd) {
592 size_t len = strlen(cmd);
594 free(ctx->other_browser_cmd);
595 ctx->other_browser_cmd = calloc(len+1, sizeof(char));
596 if (!ctx->other_browser_cmd) {
597 printf("malloc failed!\n");
598 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
600 ctx->other_browser_cmd = strncpy(ctx->other_browser_cmd,
602 ctx->default_browser_launcher = launch_other_browser;
606 void update_default_browser(struct swb_context *ctx, char *default_browser) {
610 if (!default_browser) {
611 /* No default_browser configured -- use built-in default */
612 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
616 if (!strcmp(default_browser, "tear"))
617 ctx->default_browser_launcher = launch_tear;
618 else if (!strcmp(default_browser, "microb"))
619 ctx->default_browser_launcher = launch_microb;
620 else if (!strcmp(default_browser, "fennec"))
621 /* Cheat and reuse launch_other_browser, since we don't appear
622 to need to do anything special */
623 use_other_browser_cmd(ctx, "fennec %s");
624 else if (!strcmp(default_browser, "midori"))
625 use_other_browser_cmd(ctx, "midori %s");
626 else if (!strcmp(default_browser, "other")) {
627 if (ctx->other_browser_cmd)
628 ctx->default_browser_launcher = launch_other_browser;
630 printf("default_browser is 'other', but no other_browser_cmd set -- using default\n");
631 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
634 printf("Unknown default_browser %s, using default", default_browser);
635 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
639 void launch_browser(struct swb_context *ctx, char *uri) {
640 if (ctx && ctx->default_browser_launcher)
641 ctx->default_browser_launcher(ctx, uri);