Add logging infrastructure
[connman] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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
34 #include <gdbus.h>
35
36 #include "connman.h"
37
38 static GMainLoop *main_loop = NULL;
39
40 static void sig_term(int sig)
41 {
42         g_main_loop_quit(main_loop);
43 }
44
45 static void usage(void)
46 {
47         printf("Connection Manager version %s\n\n", VERSION);
48
49         printf("Usage:\n"
50                 "\tconnmand [options]\n"
51                 "\n");
52
53         printf("Options:\n"
54                 "\t-c, --compat         Enable Network Manager compatibility\n"
55                 "\t-n, --nodaemon       Don't fork daemon to background\n"
56                 "\t-h, --help           Display help\n"
57                 "\n");
58 }
59
60 static struct option options[] = {
61         { "nodaemon", 0, 0, 'n' },
62         { "compat",   0, 0, 'c' },
63         { "debug",    0, 0, 'd' },
64         { "help",     0, 0, 'h' },
65         { }
66 };
67
68 int main(int argc, char *argv[])
69 {
70         DBusConnection *conn;
71         DBusError err;
72         struct sigaction sa;
73         int opt, detach = 1, compat = 0, debug = 0;
74
75         while ((opt = getopt_long(argc, argv, "+ncdh", options, NULL)) != EOF) {
76                 switch (opt) {
77                 case 'n':
78                         detach = 0;
79                         break;
80                 case 'c':
81                         compat = 1;
82                         break;
83                 case 'd':
84                         debug = 1;
85                         break;
86                 case 'h':
87                 default:
88                         usage();
89                         exit(0);
90                 }
91         }
92
93         argc -= optind;
94         argv += optind;
95         optind = 0;
96
97         if (detach) {
98                 if (daemon(0, 0)) {
99                         perror("Can't start daemon");
100                         exit(1);
101                 }
102         }
103
104         mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
105                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
106
107         mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
108                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
109
110         main_loop = g_main_loop_new(NULL, FALSE);
111
112         dbus_error_init(&err);
113
114         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
115         if (conn == NULL) {
116                 if (dbus_error_is_set(&err) == TRUE) {
117                         fprintf(stderr, "%s\n", err.message);
118                         dbus_error_free(&err);
119                 } else
120                         fprintf(stderr, "Can't register with system bus\n");
121                 exit(1);
122         }
123
124         if (compat) {
125                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE)
126                         compat = 0;
127         }
128
129         __connman_log_init(detach, debug);
130
131         __connman_agent_init(conn);
132
133         __connman_manager_init(conn, compat);
134
135         __connman_plugin_init();
136
137         __connman_rtnl_init();
138
139         __connman_iface_init(conn);
140
141         memset(&sa, 0, sizeof(sa));
142         sa.sa_handler = sig_term;
143         sigaction(SIGINT, &sa, NULL);
144         sigaction(SIGTERM, &sa, NULL);
145
146         g_main_loop_run(main_loop);
147
148         __connman_iface_cleanup();
149
150         __connman_rtnl_cleanup();
151
152         __connman_plugin_cleanup();
153
154         __connman_manager_cleanup();
155
156         __connman_agent_cleanup();
157
158         __connman_log_cleanup();
159
160         g_dbus_cleanup_connection(conn);
161
162         g_main_loop_unref(main_loop);
163
164         rmdir(STORAGEDIR);
165
166         rmdir(STATEDIR);
167
168         return 0;
169 }