Make signal handlers async-signal-safe
[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_req;
135         OssoBrowser *obj_osso_browser_sys, *obj_osso_browser_sys_req;
136         GMainLoop *mainloop;
137         GError *error = NULL;
138         int reqname_result;
139         GSource *fdevents;
140
141         read_config();
142
143         if (ctx.continuous_mode) {
144                 /* Install signal handlers */
145                 struct sigaction act;
146                 act.sa_flags = SA_RESTART;
147                 sigemptyset(&(act.sa_mask));
148
149                 /* SIGCHLD -- clean up after zombies */
150                 act.sa_handler = waitforzombies;
151                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
152                         log_msg("Installing signal handler failed\n");
153                         return 1;
154                 }
155
156                 /* All other signals are handled via the GLib main loop */
157                 if (pipe(eventpipe) == -1) {
158                         log_perror(errno, "Creating event pipe failed");
159                         return 1;
160                 }
161                 act.sa_handler = handle_signal;
162                 if (sigaction(SIGHUP, &act, NULL) == -1) {
163                         log_msg("Installing signal handler failed\n");
164                         return 1;
165                 }
166         }
167
168         g_type_init();
169
170         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
171                         &dbus_glib_osso_browser_object_info);
172
173         /* Get a connection to the D-Bus session bus */
174         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
175         if (!ctx.session_bus) {
176                 log_msg("Couldn't get a D-Bus bus connection\n");
177                 return 1;
178         }
179         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
180                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
181                         "org.freedesktop.DBus");
182         if (!ctx.dbus_proxy) {
183                 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
184                 return 1;
185         }
186
187         /* Get the org.maemo.garage.browser-switchboard name from D-Bus, as
188            a form of locking to ensure that not more than one
189            browser-switchboard process is active at any time.  With
190            DBUS_NAME_FLAG_DO_NOT_QUEUE set and DBUS_NAME_FLAG_REPLACE_EXISTING
191            not set, getting the name succeeds if and only if no other
192            process owns the name. */
193         if (!dbus_g_proxy_call(ctx.dbus_proxy, "RequestName", &error,
194                                G_TYPE_STRING, "org.maemo.garage.browser-switchboard",
195                                G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
196                                G_TYPE_INVALID,
197                                G_TYPE_UINT, &reqname_result,
198                                G_TYPE_INVALID)) {
199                 log_msg("Couldn't acquire browser-switchboard lock: %s\n",
200                         error->message);
201                 return 1;
202         }
203         if (reqname_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
204                 log_msg("Another browser-switchboard already running\n");
205                 return 1;
206         }
207
208         /* Get a connection to the D-Bus system bus */
209         ctx.system_bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
210         if (!ctx.system_bus) {
211                 log_msg("Couldn't get a D-Bus system bus connection\n");
212                 return 1;
213         }
214         ctx.dbus_system_proxy = dbus_g_proxy_new_for_name(ctx.system_bus,
215                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
216                         "org.freedesktop.DBus");
217         if (!ctx.dbus_system_proxy) {
218                 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
219                 return 1;
220         }
221
222         dbus_request_osso_browser_name(&ctx);
223
224         /* Register ourselves to handle the osso_browser D-Bus methods */
225         obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
226         obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
227         obj_osso_browser_sys = g_object_new(OSSO_BROWSER_TYPE, NULL);
228         obj_osso_browser_sys_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
229         dbus_g_connection_register_g_object(ctx.session_bus,
230                         "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
231         dbus_g_connection_register_g_object(ctx.session_bus,
232                         "/com/nokia/osso_browser/request",
233                         G_OBJECT(obj_osso_browser_req));
234         dbus_g_connection_register_g_object(ctx.system_bus,
235                         "/com/nokia/osso_browser",
236                         G_OBJECT(obj_osso_browser_sys));
237         dbus_g_connection_register_g_object(ctx.system_bus,
238                         "/com/nokia/osso_browser/request",
239                         G_OBJECT(obj_osso_browser_sys_req));
240
241         mainloop = g_main_loop_new(NULL, FALSE);
242
243         /* Hook up event pipe to the main loop */
244         if (ctx.continuous_mode) {
245                 fdevents = g_source_new(&fdevents_funcs, sizeof(GSource));
246                 g_source_set_priority(fdevents, G_PRIORITY_HIGH);
247                 fdevents_pfd.fd = eventpipe[0];
248                 fdevents_pfd.events = G_IO_IN;
249                 fdevents_pfd.revents = 0;
250                 g_source_add_poll(fdevents, &fdevents_pfd);
251                 g_source_attach(fdevents, NULL);
252         }
253
254         log_msg("Starting main loop\n");
255         g_main_loop_run(mainloop);
256         log_msg("Main loop completed\n");
257
258         return 0;
259 }