Ensure that only one browser-switchboard is active at any time
[browser-switch] / main.c
diff --git a/main.c b/main.c
index 9adacbe..2dfe439 100644 (file)
--- a/main.c
+++ b/main.c
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
-#include <unistd.h>
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <regex.h>
 #include <dbus/dbus-glib.h>
 
 #include "browser-switchboard.h"
 #include "launcher.h"
 #include "dbus-server-bindings.h"
-
-#define DEFAULT_HOMEDIR "/home/user"
-#define CONFIGFILE_LOC "/.config/browser-switchboard"
-#define CONFIGFILE_LOC_OLD "/.config/browser-proxy"
-#define MAXLINE 1024
+#include "configfile.h"
 
 struct swb_context ctx;
 
@@ -56,114 +50,51 @@ static void waitforzombies(int signalnum) {
 }
 
 static void read_config(int signalnum) {
-       char *homedir, *configfile;
-       size_t len;
-       char buf[MAXLINE];
-       char *key, *value;
-       char *default_browser = NULL;
        FILE *fp;
-       regex_t re_ignore, re_config1, re_config2;
-       regmatch_t substrs[3];
+       int continuous_mode_seen = 0;
+       struct swb_config_line line;
+       char *default_browser = NULL;
 
        set_config_defaults(&ctx);
 
-       /* Put together the path to the config file */
-       if (!(homedir = getenv("HOME")))
-               homedir = DEFAULT_HOMEDIR;
-       len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
-       if (!(configfile = calloc(len, sizeof(char))))
+       if (!(fp = open_config_file()))
                goto out_noopen;
-       snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
-
-       /* Try to open the config file */
-       if (!(fp = fopen(configfile, "r"))) {
-               /* Try the legacy config file location before giving up
-                  XXX we assume here that CONFIGFILE_LOC_OLD is shorter
-                  than CONFIGFILE_LOC! */
-               snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC_OLD);
-               if (!(fp = fopen(configfile, "r")))
-                       goto out_noopen;
-       }
-
-       /* compile regex matching blank lines or comments */
-       if (regcomp(&re_ignore, "^[[:space:]]*(#|$)", REG_EXTENDED|REG_NOSUB))
-               goto out_nore;
-       /* compile regex matching foo = "bar", with arbitrary whitespace at
-          beginning and end of line and surrounding the = */
-       if (regcomp(&re_config1,
-                   "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*\"(.*)\"[[:space:]]*$",
-                   REG_EXTENDED)) {
-               regfree(&re_ignore);
-               goto out_nore;
-       }
-       /* compile regex matching foo = bar, with arbitrary whitespace at
-          beginning of line and surrounding the = */
-       if (regcomp(&re_config2,
-                   "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*(.*)$",
-                   REG_EXTENDED|REG_NEWLINE)) {
-               regfree(&re_ignore);
-               regfree(&re_config1);
-               goto out_nore;
-       }
 
-       /* Read in the config file one line at a time and parse it
-          XXX doesn't deal with lines longer than MAXLINE */
-       while (fgets(buf, MAXLINE, fp)) {
-               /* skip blank lines and comments */
-               if (!regexec(&re_ignore, buf, 0, NULL, 0))
-                       continue;
-
-               /* Find the substrings corresponding to the key and value
-                  If the line doesn't match our idea of a config file entry,
-                  skip it */
-               if (regexec(&re_config1, buf, 3, substrs, 0) &&
-                   regexec(&re_config2, buf, 3, substrs, 0))
-                       continue;
-               if (substrs[1].rm_so == -1 || substrs[2].rm_so == -1)
-                       continue;
-
-               /* copy the config value into a new string */
-               len = substrs[2].rm_eo - substrs[2].rm_so;
-               if (!(value = calloc(len+1, sizeof(char))))
-                       goto out;
-               strncpy(value, buf+substrs[2].rm_so, len);
-               /* calloc() zeroes the memory, so string is automatically
-                  null terminated */
-
-               /* make key point to a null-terminated string holding the 
-                  config key */
-               key = buf + substrs[1].rm_so;
-               buf[substrs[1].rm_eo] = '\0';
-
-               if (!strcmp(key, "continuous_mode")) {
-                       ctx.continuous_mode = atoi(value);
-                       free(value);
-               } else if (!strcmp(key, "default_browser")) {
-                       if (!default_browser)
-                               default_browser = value;
-               } else if (!strcmp(key, "other_browser_cmd")) {
-                       if (!ctx.other_browser_cmd)
-                               ctx.other_browser_cmd = value;
-               } else {
-                       /* Don't need this line's contents */
-                       free(value);
+       /* Parse the config file
+          TODO: should we handle errors differently than EOF? */
+       if (!parse_config_file_begin())
+               goto out;
+       while (!parse_config_file_line(fp, &line)) {
+               if (line.parsed) {
+                       if (!strcmp(line.key, "continuous_mode")) {
+                               if (!continuous_mode_seen) {
+                                       ctx.continuous_mode = atoi(line.value);
+                                       continuous_mode_seen = 1;
+                               }
+                               free(line.value);
+                       } else if (!strcmp(line.key, "default_browser")) {
+                               if (!default_browser)
+                                       default_browser = line.value;
+                       } else if (!strcmp(line.key, "other_browser_cmd")) {
+                               if (!ctx.other_browser_cmd)
+                                       ctx.other_browser_cmd = line.value;
+                       } else {
+                               /* Don't need this line's contents */
+                               free(line.value);
+                       }
                }
-               value = NULL;
+               free(line.key);
        }
+       parse_config_file_end();
 
        printf("continuous_mode: %d\n", ctx.continuous_mode);
        printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
        printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
 
 out:
-       regfree(&re_ignore);
-       regfree(&re_config1);
-       regfree(&re_config2);
-out_nore:
        fclose(fp);
 out_noopen:
        update_default_browser(&ctx, default_browser);
-       free(configfile);
        free(default_browser);
        return;
 }
@@ -172,6 +103,7 @@ int main() {
        OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
        GMainLoop *mainloop;
        GError *error = NULL;
+       int reqname_result;
 
        read_config(0);
 
@@ -215,6 +147,27 @@ int main() {
                return 1;
        }
 
+       /* Get the org.maemo.garage.browser-switchboard name from D-Bus, as
+          a form of locking to ensure that not more than one
+          browser-switchboard process is active at any time.  With
+          DBUS_NAME_FLAG_DO_NOT_QUEUE set and DBUS_NAME_FLAG_REPLACE_EXISTING
+          not set, getting the name succeeds if and only if no other
+          process owns the name. */
+       if (!dbus_g_proxy_call(ctx.dbus_proxy, "RequestName", &error,
+                              G_TYPE_STRING, "org.maemo.garage.browser-switchboard",
+                              G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
+                              G_TYPE_INVALID,
+                              G_TYPE_UINT, &reqname_result,
+                              G_TYPE_INVALID)) {
+               printf("Couldn't acquire browser-switchboard lock: %s\n",
+                      error->message);
+               return 1;
+       }
+       if (reqname_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {  
+               printf("Another browser-switchboard already running\n");
+               return 1;
+       }
+
        dbus_request_osso_browser_name(&ctx);
 
        /* Register ourselves to handle the osso_browser D-Bus methods */