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