Also register for path / on D-Bus
[browser-switch] / main.c
1 /*
2  * main.c -- config file parsing and main loop for browser-switchboard
3  *
4  * Copyright (C) 2009-2010 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
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.
11  *
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.
16  *
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,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <glib.h>
31 #include <dbus/dbus-glib.h>
32
33 #include "browser-switchboard.h"
34 #include "launcher.h"
35 #include "dbus-server-bindings.h"
36 #include "config.h"
37 #include "log.h"
38
39 struct swb_context ctx;
40
41 static void read_config(void);
42
43 /* Wait for zombies on SIGCHLD */
44 static void waitforzombies(int signalnum) {
45         while (waitpid(-1, NULL, WNOHANG) > 0);
46 }
47
48 /* Handle other signals in event loop by writing signal number to pipe */
49 int eventpipe[2];
50 static void handle_signal(int signalnum) {
51         write(eventpipe[1], &signalnum, sizeof signalnum);
52 }
53
54 /* Callbacks for polling the event pipe in the GLib event loop */
55 static GPollFD fdevents_pfd;
56 /* Called before entering the poll() */
57 static gboolean fdevents_prepare(GSource *source, gint *timeout) {
58         /* No timeout for poll() */
59         *timeout = -1;
60         return FALSE;
61 }
62 /* Check to see whether a handled event has happened */
63 static gboolean fdevents_check(GSource *source) {
64         return !!(fdevents_pfd.revents & G_IO_IN);
65 }
66 /* Read the event from the pipe and handle it */
67 static gboolean fdevents_dispatch(GSource *source,
68                                   GSourceFunc callback, gpointer user_data) {
69         int eventnum;
70
71         if (read(eventpipe[0], &eventnum, sizeof eventnum) == (sizeof eventnum)) {
72                 /* Handle the event passed to us */
73                 switch (eventnum) {
74                   /* SIGHUP received -- reread config file */
75                   case SIGHUP:
76                         read_config();
77                         break;
78                   default:
79                         return FALSE;
80                 }
81                 return TRUE;
82         } else {
83                 return FALSE;
84         }
85 }
86 static GSourceFuncs fdevents_funcs = {
87         .prepare = fdevents_prepare,
88         .check = fdevents_check,
89         .dispatch = fdevents_dispatch,
90         .finalize = NULL,
91 };
92
93
94 static void read_config(void) {
95         struct swb_config cfg;
96
97         swb_config_init(&cfg);
98
99         swb_config_load(&cfg);
100
101         log_config(cfg.logging);
102 #ifdef FREMANTLE
103         /* continuous mode is required on Fremantle */
104         ctx.continuous_mode = 1;
105         if (!cfg.continuous_mode)
106                 log_msg("continuous_mode = 0 operation no longer supported, ignoring config setting\n");
107 #else
108         ctx.continuous_mode = cfg.continuous_mode;
109 #endif
110         free(ctx.other_browser_cmd);
111         if (cfg.other_browser_cmd) {
112                 if (!(ctx.other_browser_cmd = strdup(cfg.other_browser_cmd))) {
113                         log_perror(errno, "Failed to set other_browser_cmd");
114                         exit(1);
115                 }
116         } else
117                 ctx.other_browser_cmd = NULL;
118         update_default_browser(&ctx, cfg.default_browser);
119 #ifdef FREMANTLE
120         ctx.autostart_microb = cfg.autostart_microb;
121 #endif
122
123         log_msg("continuous_mode: %d\n", cfg.continuous_mode);
124         log_msg("default_browser: '%s'\n", cfg.default_browser);
125         log_msg("other_browser_cmd: '%s'\n",
126                 cfg.other_browser_cmd?cfg.other_browser_cmd:"NULL");
127         log_msg("logging: '%s'\n", cfg.logging);
128
129         swb_config_free(&cfg);
130         return;
131 }
132
133 int main() {
134         OssoBrowser *obj_osso_browser, *obj_osso_browser_sys;
135         OssoBrowser *obj_osso_browser_req, *obj_osso_browser_sys_req;
136         OssoBrowser *obj_osso_browser_root, *obj_osso_browser_sys_root;
137         GMainLoop *mainloop;
138         GError *error = NULL;
139         int reqname_result;
140         GSource *fdevents;
141
142         read_config();
143
144         if (ctx.continuous_mode) {
145                 /* Install signal handlers */
146                 struct sigaction act;
147                 act.sa_flags = SA_RESTART;
148                 sigemptyset(&(act.sa_mask));
149
150                 /* SIGCHLD -- clean up after zombies */
151                 act.sa_handler = waitforzombies;
152                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
153                         log_msg("Installing signal handler failed\n");
154                         return 1;
155                 }
156
157                 /* All other signals are handled via the GLib main loop */
158                 if (pipe(eventpipe) == -1) {
159                         log_perror(errno, "Creating event pipe failed");
160                         return 1;
161                 }
162                 act.sa_handler = handle_signal;
163                 if (sigaction(SIGHUP, &act, NULL) == -1) {
164                         log_msg("Installing signal handler failed\n");
165                         return 1;
166                 }
167         }
168
169         g_type_init();
170
171         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
172                         &dbus_glib_osso_browser_object_info);
173
174         /* Get a connection to the D-Bus session bus */
175         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
176         if (!ctx.session_bus) {
177                 log_msg("Couldn't get a D-Bus bus connection\n");
178                 return 1;
179         }
180         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
181                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
182                         "org.freedesktop.DBus");
183         if (!ctx.dbus_proxy) {
184                 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
185                 return 1;
186         }
187
188         /* Get the org.maemo.garage.browser-switchboard name from D-Bus, as
189            a form of locking to ensure that not more than one
190            browser-switchboard process is active at any time.  With
191            DBUS_NAME_FLAG_DO_NOT_QUEUE set and DBUS_NAME_FLAG_REPLACE_EXISTING
192            not set, getting the name succeeds if and only if no other
193            process owns the name. */
194         if (!dbus_g_proxy_call(ctx.dbus_proxy, "RequestName", &error,
195                                G_TYPE_STRING, "org.maemo.garage.browser-switchboard",
196                                G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
197                                G_TYPE_INVALID,
198                                G_TYPE_UINT, &reqname_result,
199                                G_TYPE_INVALID)) {
200                 log_msg("Couldn't acquire browser-switchboard lock: %s\n",
201                         error->message);
202                 return 1;
203         }
204         if (reqname_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
205                 log_msg("Another browser-switchboard already running\n");
206                 return 1;
207         }
208
209         /* Get a connection to the D-Bus system bus */
210         ctx.system_bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
211         if (!ctx.system_bus) {
212                 log_msg("Couldn't get a D-Bus system bus connection\n");
213                 return 1;
214         }
215         ctx.dbus_system_proxy = dbus_g_proxy_new_for_name(ctx.system_bus,
216                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
217                         "org.freedesktop.DBus");
218         if (!ctx.dbus_system_proxy) {
219                 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
220                 return 1;
221         }
222
223         dbus_request_osso_browser_name(&ctx);
224
225         /* Register ourselves to handle the osso_browser D-Bus methods */
226         obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
227         obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
228         obj_osso_browser_root = g_object_new(OSSO_BROWSER_TYPE, NULL);
229         obj_osso_browser_sys = g_object_new(OSSO_BROWSER_TYPE, NULL);
230         obj_osso_browser_sys_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
231         obj_osso_browser_sys_root = g_object_new(OSSO_BROWSER_TYPE, NULL);
232         dbus_g_connection_register_g_object(ctx.session_bus,
233                         "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
234         dbus_g_connection_register_g_object(ctx.session_bus,
235                         "/com/nokia/osso_browser/request",
236                         G_OBJECT(obj_osso_browser_req));
237         dbus_g_connection_register_g_object(ctx.session_bus,
238                         "/", G_OBJECT(obj_osso_browser_root));
239         dbus_g_connection_register_g_object(ctx.system_bus,
240                         "/com/nokia/osso_browser",
241                         G_OBJECT(obj_osso_browser_sys));
242         dbus_g_connection_register_g_object(ctx.system_bus,
243                         "/com/nokia/osso_browser/request",
244                         G_OBJECT(obj_osso_browser_sys_req));
245         dbus_g_connection_register_g_object(ctx.system_bus,
246                         "/", G_OBJECT(obj_osso_browser_sys_root));
247
248         mainloop = g_main_loop_new(NULL, FALSE);
249
250         /* Hook up event pipe to the main loop */
251         if (ctx.continuous_mode) {
252                 fdevents = g_source_new(&fdevents_funcs, sizeof(GSource));
253                 g_source_set_priority(fdevents, G_PRIORITY_HIGH);
254                 fdevents_pfd.fd = eventpipe[0];
255                 fdevents_pfd.events = G_IO_IN;
256                 fdevents_pfd.revents = 0;
257                 g_source_add_poll(fdevents, &fdevents_pfd);
258                 g_source_attach(fdevents, NULL);
259         }
260
261         log_msg("Starting main loop\n");
262         g_main_loop_run(mainloop);
263         log_msg("Main loop completed\n");
264
265         return 0;
266 }