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 <signal.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <dbus/dbus-glib.h>
30
31 #include "browser-switchboard.h"
32 #include "launcher.h"
33 #include "dbus-server-bindings.h"
34 #include "config.h"
35 #include "log.h"
36
37 struct swb_context ctx;
38
39 static void waitforzombies(int signalnum) {
40         while (waitpid(-1, NULL, WNOHANG) > 0)
41                 log_msg("Waited for a zombie\n");
42 }
43
44 static void read_config(int signalnum) {
45         struct swb_config cfg;
46
47         swb_config_init(&cfg);
48
49         swb_config_load(&cfg);
50
51         log_config(cfg.logging);
52 #ifdef FREMANTLE
53         /* continuous mode is required on Fremantle */
54         ctx.continuous_mode = 1;
55         if (!cfg.continuous_mode)
56                 log_msg("continuous_mode = 0 operation no longer supported, ignoring config setting\n");
57 #else
58         ctx.continuous_mode = cfg.continuous_mode;
59 #endif
60         free(ctx.other_browser_cmd);
61         if (cfg.other_browser_cmd) {
62                 if (!(ctx.other_browser_cmd = strdup(cfg.other_browser_cmd))) {
63                         log_perror(errno, "Failed to set other_browser_cmd");
64                         exit(1);
65                 }
66         } else
67                 ctx.other_browser_cmd = NULL;
68         update_default_browser(&ctx, cfg.default_browser);
69 #ifdef FREMANTLE
70         ctx.autostart_microb = cfg.autostart_microb;
71 #endif
72
73         log_msg("continuous_mode: %d\n", cfg.continuous_mode);
74         log_msg("default_browser: '%s'\n", cfg.default_browser);
75         log_msg("other_browser_cmd: '%s'\n",
76                 cfg.other_browser_cmd?cfg.other_browser_cmd:"NULL");
77         log_msg("logging: '%s'\n", cfg.logging);
78
79         swb_config_free(&cfg);
80         return;
81 }
82
83 int main() {
84         OssoBrowser *obj_osso_browser, *obj_osso_browser_sys;
85         OssoBrowser *obj_osso_browser_req, *obj_osso_browser_sys_req;
86         OssoBrowser *obj_osso_browser_root, *obj_osso_browser_sys_root;
87         GMainLoop *mainloop;
88         GError *error = NULL;
89         int reqname_result;
90
91         read_config(0);
92
93         if (ctx.continuous_mode) {
94                 /* Install signal handlers */
95                 struct sigaction act;
96                 act.sa_flags = SA_RESTART;
97                 sigemptyset(&(act.sa_mask));
98
99                 /* SIGCHLD -- clean up after zombies */
100                 act.sa_handler = waitforzombies;
101                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
102                         log_msg("Installing signal handler failed\n");
103                         return 1;
104                 }
105
106                 /* SIGHUP -- reread config file */
107                 act.sa_handler = read_config;
108                 if (sigaction(SIGHUP, &act, NULL) == -1) {
109                         log_msg("Installing signal handler failed\n");
110                         return 1;
111                 }
112         }
113
114         g_type_init();
115
116         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
117                         &dbus_glib_osso_browser_object_info);
118
119         /* Get a connection to the D-Bus session bus */
120         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
121         if (!ctx.session_bus) {
122                 log_msg("Couldn't get a D-Bus bus connection\n");
123                 return 1;
124         }
125         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
126                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
127                         "org.freedesktop.DBus");
128         if (!ctx.dbus_proxy) {
129                 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
130                 return 1;
131         }
132
133         /* Get the org.maemo.garage.browser-switchboard name from D-Bus, as
134            a form of locking to ensure that not more than one
135            browser-switchboard process is active at any time.  With
136            DBUS_NAME_FLAG_DO_NOT_QUEUE set and DBUS_NAME_FLAG_REPLACE_EXISTING
137            not set, getting the name succeeds if and only if no other
138            process owns the name. */
139         if (!dbus_g_proxy_call(ctx.dbus_proxy, "RequestName", &error,
140                                G_TYPE_STRING, "org.maemo.garage.browser-switchboard",
141                                G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
142                                G_TYPE_INVALID,
143                                G_TYPE_UINT, &reqname_result,
144                                G_TYPE_INVALID)) {
145                 log_msg("Couldn't acquire browser-switchboard lock: %s\n",
146                         error->message);
147                 return 1;
148         }
149         if (reqname_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
150                 log_msg("Another browser-switchboard already running\n");
151                 return 1;
152         }
153
154         /* Get a connection to the D-Bus system bus */
155         ctx.system_bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
156         if (!ctx.system_bus) {
157                 log_msg("Couldn't get a D-Bus system bus connection\n");
158                 return 1;
159         }
160         ctx.dbus_system_proxy = dbus_g_proxy_new_for_name(ctx.system_bus,
161                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
162                         "org.freedesktop.DBus");
163         if (!ctx.dbus_system_proxy) {
164                 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
165                 return 1;
166         }
167
168         dbus_request_osso_browser_name(&ctx);
169
170         /* Register ourselves to handle the osso_browser D-Bus methods */
171         obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
172         obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
173         obj_osso_browser_root = g_object_new(OSSO_BROWSER_TYPE, NULL);
174         obj_osso_browser_sys = g_object_new(OSSO_BROWSER_TYPE, NULL);
175         obj_osso_browser_sys_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
176         obj_osso_browser_sys_root = g_object_new(OSSO_BROWSER_TYPE, NULL);
177         dbus_g_connection_register_g_object(ctx.session_bus,
178                         "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
179         dbus_g_connection_register_g_object(ctx.session_bus,
180                         "/com/nokia/osso_browser/request",
181                         G_OBJECT(obj_osso_browser_req));
182         dbus_g_connection_register_g_object(ctx.session_bus,
183                         "/", G_OBJECT(obj_osso_browser_root));
184         dbus_g_connection_register_g_object(ctx.system_bus,
185                         "/com/nokia/osso_browser",
186                         G_OBJECT(obj_osso_browser_sys));
187         dbus_g_connection_register_g_object(ctx.system_bus,
188                         "/com/nokia/osso_browser/request",
189                         G_OBJECT(obj_osso_browser_sys_req));
190         dbus_g_connection_register_g_object(ctx.system_bus,
191                         "/", G_OBJECT(obj_osso_browser_sys_root));
192
193         mainloop = g_main_loop_new(NULL, FALSE);
194         log_msg("Starting main loop\n");
195         g_main_loop_run(mainloop);
196         log_msg("Main loop completed\n");
197
198         return 0;
199 }