Add preparation for Network Manager compatibility
[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 <syslog.h>
31 #include <signal.h>
32 #include <getopt.h>
33 #include <sys/stat.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 [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         { "nodaemon", 0, 0, 'n' },
63         { "compat",   0, 0, 'c' },
64         { "help",     0, 0, 'h' },
65         { }
66 };
67
68 int main(int argc, char *argv[])
69 {
70         DBusConnection *conn;
71         struct sigaction sa;
72         int log_option = LOG_NDELAY | LOG_PID;
73         int opt, detach = 1, compat = 0;
74
75         while ((opt = getopt_long(argc, argv, "+nch", options, NULL)) != EOF) {
76                 switch(opt) {
77                 case 'n':
78                         detach = 0;
79                         break;
80                 case 'c':
81                         compat = 1;
82                         break;
83                 case 'h':
84                 default:
85                         usage();
86                         exit(0);
87                 }
88         }
89
90         argc -= optind;
91         argv += optind;
92         optind = 0;
93
94         if (detach) {
95                 if (daemon(0, 0)) {
96                         perror("Can't start daemon");
97                         exit(1);
98                 }
99         } else
100                 log_option |= LOG_PERROR;
101
102         openlog("connmand", log_option, LOG_DAEMON);
103
104         mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
105                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
106
107         main_loop = g_main_loop_new(NULL, FALSE);
108
109         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE);
110         if (conn == NULL) {
111                 fprintf(stderr, "Can't register with system bus\n");
112                 exit(1);
113         }
114
115         if (compat) {
116                 if (g_dbus_request_name(conn, NM_SERVICE) == FALSE)
117                         compat = 0;
118         }
119
120         __connman_manager_init(conn, compat);
121
122         __connman_plugin_init();
123
124         __connman_rtnl_init();
125
126         __connman_iface_init(conn);
127
128         memset(&sa, 0, sizeof(sa));
129         sa.sa_handler = sig_term;
130         sigaction(SIGINT, &sa, NULL);
131         sigaction(SIGTERM, &sa, NULL);
132
133         g_main_loop_run(main_loop);
134
135         __connman_iface_cleanup();
136
137         __connman_rtnl_cleanup();
138
139         __connman_plugin_cleanup();
140
141         __connman_manager_cleanup();
142
143         g_dbus_cleanup_connection(conn);
144
145         g_main_loop_unref(main_loop);
146
147         rmdir(STATEDIR);
148
149         closelog();
150
151         return 0;
152 }