Add first draft of new D-Bus API
[framework/connectivity/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <getopt.h>
32 #include <sys/stat.h>
33 #include <net/if.h>
34
35 #include <gdbus.h>
36
37 #include "connman.h"
38
39 static GMainLoop *main_loop = NULL;
40
41 static void sig_term(int sig)
42 {
43         g_main_loop_quit(main_loop);
44 }
45
46 static gchar *option_interface = NULL;
47 static gboolean option_detach = TRUE;
48 static gboolean option_compat = FALSE;
49 static gboolean option_debug = FALSE;
50
51 static GOptionEntry options[] = {
52         { "interface", 'i', 0, G_OPTION_ARG_STRING, &option_interface,
53                                 "Specify network interface", "IFACE" },
54         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
55                                 G_OPTION_ARG_NONE, &option_detach,
56                                 "Don't fork daemon to background" },
57         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
58                                 "Enable Network Manager compatibility" },
59         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
60                                 "Enable debug information output" },
61         { NULL },
62 };
63
64 int main(int argc, char *argv[])
65 {
66         GOptionContext *context;
67         GError *error = NULL;
68         DBusConnection *conn;
69         DBusError err;
70         struct sigaction sa;
71
72         if (g_thread_supported() == FALSE)
73                 g_thread_init(NULL);
74
75         context = g_option_context_new(NULL);
76         g_option_context_add_main_entries(context, options, NULL);
77
78         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
79                 if (error != NULL) {
80                         g_printerr("%s\n", error->message);
81                         g_error_free(error);
82                 } else
83                         g_printerr("An unknown error occurred\n");
84                 exit(1);
85         }
86
87         g_option_context_free(context);
88
89         if (option_detach == TRUE) {
90                 if (daemon(0, 0)) {
91                         perror("Can't start daemon");
92                         exit(1);
93                 }
94         }
95
96         mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
97                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
98
99         mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
100                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
101
102         main_loop = g_main_loop_new(NULL, FALSE);
103
104         if (dbus_threads_init_default() == FALSE) {
105                 fprintf(stderr, "Can't init usage of threads\n");
106                 exit(1);
107         }
108
109         dbus_error_init(&err);
110
111         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
112         if (conn == NULL) {
113                 if (dbus_error_is_set(&err) == TRUE) {
114                         fprintf(stderr, "%s\n", err.message);
115                         dbus_error_free(&err);
116                 } else
117                         fprintf(stderr, "Can't register with system bus\n");
118                 exit(1);
119         }
120
121         if (option_compat == TRUE) {
122                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
123                         fprintf(stderr, "Can't register compat service\n");
124                         option_compat = FALSE;
125                 }
126         }
127
128         __connman_log_init(option_detach, option_debug);
129
130         __connman_element_init(conn);
131
132         __connman_agent_init(conn);
133
134         __connman_manager_init(conn, option_compat);
135
136         __connman_plugin_init();
137
138         g_free(option_interface);
139
140         memset(&sa, 0, sizeof(sa));
141         sa.sa_handler = sig_term;
142         sigaction(SIGINT, &sa, NULL);
143         sigaction(SIGTERM, &sa, NULL);
144
145         g_main_loop_run(main_loop);
146
147         __connman_plugin_cleanup();
148
149         __connman_manager_cleanup();
150
151         __connman_agent_cleanup();
152
153         __connman_element_cleanup();
154
155         __connman_log_cleanup();
156
157         g_dbus_cleanup_connection(conn);
158
159         g_main_loop_unref(main_loop);
160
161         rmdir(STORAGEDIR);
162
163         rmdir(STATEDIR);
164
165         return 0;
166 }