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