Add logging infrastructure
[connman] / src / main.c
index 21a0513..a4962b2 100644 (file)
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <signal.h>
+#include <getopt.h>
 #include <sys/stat.h>
 
 #include <gdbus.h>
@@ -41,30 +42,102 @@ static void sig_term(int sig)
        g_main_loop_quit(main_loop);
 }
 
+static void usage(void)
+{
+       printf("Connection Manager version %s\n\n", VERSION);
+
+       printf("Usage:\n"
+               "\tconnmand [options]\n"
+               "\n");
+
+       printf("Options:\n"
+               "\t-c, --compat         Enable Network Manager compatibility\n"
+               "\t-n, --nodaemon       Don't fork daemon to background\n"
+               "\t-h, --help           Display help\n"
+               "\n");
+}
+
+static struct option options[] = {
+       { "nodaemon", 0, 0, 'n' },
+       { "compat",   0, 0, 'c' },
+       { "debug",    0, 0, 'd' },
+       { "help",     0, 0, 'h' },
+       { }
+};
+
 int main(int argc, char *argv[])
 {
        DBusConnection *conn;
+       DBusError err;
        struct sigaction sa;
+       int opt, detach = 1, compat = 0, debug = 0;
+
+       while ((opt = getopt_long(argc, argv, "+ncdh", options, NULL)) != EOF) {
+               switch (opt) {
+               case 'n':
+                       detach = 0;
+                       break;
+               case 'c':
+                       compat = 1;
+                       break;
+               case 'd':
+                       debug = 1;
+                       break;
+               case 'h':
+               default:
+                       usage();
+                       exit(0);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+       optind = 0;
+
+       if (detach) {
+               if (daemon(0, 0)) {
+                       perror("Can't start daemon");
+                       exit(1);
+               }
+       }
 
        mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
                        S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
 
+       mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
+                       S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+
        main_loop = g_main_loop_new(NULL, FALSE);
 
-       conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE);
+       dbus_error_init(&err);
+
+       conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
        if (conn == NULL) {
-               fprintf(stderr, "Can't register with system bus\n");
+               if (dbus_error_is_set(&err) == TRUE) {
+                       fprintf(stderr, "%s\n", err.message);
+                       dbus_error_free(&err);
+               } else
+                       fprintf(stderr, "Can't register with system bus\n");
                exit(1);
        }
 
-       __connman_manager_init(conn);
+       if (compat) {
+               if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE)
+                       compat = 0;
+       }
+
+       __connman_log_init(detach, debug);
 
-       __connman_plugin_init();
+       __connman_agent_init(conn);
 
-       __connman_iface_init(conn);
+       __connman_manager_init(conn, compat);
+
+       __connman_plugin_init();
 
        __connman_rtnl_init();
 
+       __connman_iface_init(conn);
+
        memset(&sa, 0, sizeof(sa));
        sa.sa_handler = sig_term;
        sigaction(SIGINT, &sa, NULL);
@@ -72,18 +145,24 @@ int main(int argc, char *argv[])
 
        g_main_loop_run(main_loop);
 
-       __connman_rtnl_cleanup();
-
        __connman_iface_cleanup();
 
+       __connman_rtnl_cleanup();
+
        __connman_plugin_cleanup();
 
        __connman_manager_cleanup();
 
+       __connman_agent_cleanup();
+
+       __connman_log_cleanup();
+
        g_dbus_cleanup_connection(conn);
 
        g_main_loop_unref(main_loop);
 
+       rmdir(STORAGEDIR);
+
        rmdir(STATEDIR);
 
        return 0;