main: Remove handsfree_audio_manager init/cleanup
[platform/upstream/ofono.git] / src / main.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  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 <sys/signalfd.h>
32
33 #include <gdbus.h>
34
35 #include "ofono.h"
36
37 #define SHUTDOWN_GRACE_SECONDS 10
38
39 static GMainLoop *event_loop;
40
41 void __ofono_exit(void)
42 {
43         g_main_loop_quit(event_loop);
44 }
45
46 static gboolean quit_eventloop(gpointer user_data)
47 {
48         __ofono_exit();
49         return FALSE;
50 }
51
52 static unsigned int __terminated = 0;
53
54 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
55                                                         gpointer user_data)
56 {
57         struct signalfd_siginfo si;
58         ssize_t result;
59         int fd;
60
61         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
62                 return FALSE;
63
64         fd = g_io_channel_unix_get_fd(channel);
65
66         result = read(fd, &si, sizeof(si));
67         if (result != sizeof(si))
68                 return FALSE;
69
70         switch (si.ssi_signo) {
71         case SIGINT:
72         case SIGTERM:
73                 if (__terminated == 0) {
74                         ofono_info("Terminating");
75                         g_timeout_add_seconds(SHUTDOWN_GRACE_SECONDS,
76                                                 quit_eventloop, NULL);
77                         __ofono_modem_shutdown();
78                 }
79
80                 __terminated = 1;
81                 break;
82         }
83
84         return TRUE;
85 }
86
87 static guint setup_signalfd(void)
88 {
89         GIOChannel *channel;
90         guint source;
91         sigset_t mask;
92         int fd;
93
94         sigemptyset(&mask);
95         sigaddset(&mask, SIGINT);
96         sigaddset(&mask, SIGTERM);
97
98         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
99                 perror("Failed to set signal mask");
100                 return 0;
101         }
102
103         fd = signalfd(-1, &mask, 0);
104         if (fd < 0) {
105                 perror("Failed to create signal descriptor");
106                 return 0;
107         }
108
109         channel = g_io_channel_unix_new(fd);
110
111         g_io_channel_set_close_on_unref(channel, TRUE);
112         g_io_channel_set_encoding(channel, NULL, NULL);
113         g_io_channel_set_buffered(channel, FALSE);
114
115         source = g_io_add_watch(channel,
116                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
117                                 signal_handler, NULL);
118
119         g_io_channel_unref(channel);
120
121         return source;
122 }
123
124 static void system_bus_disconnected(DBusConnection *conn, void *user_data)
125 {
126         ofono_error("System bus has disconnected!");
127
128         g_main_loop_quit(event_loop);
129 }
130
131 static gchar *option_debug = NULL;
132 static gchar *option_plugin = NULL;
133 static gchar *option_noplugin = NULL;
134 static gboolean option_detach = TRUE;
135 static gboolean option_version = FALSE;
136
137 static gboolean parse_debug(const char *key, const char *value,
138                                         gpointer user_data, GError **error)
139 {
140         if (value)
141                 option_debug = g_strdup(value);
142         else
143                 option_debug = g_strdup("*");
144
145         return TRUE;
146 }
147
148 static GOptionEntry options[] = {
149         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
150                                 G_OPTION_ARG_CALLBACK, parse_debug,
151                                 "Specify debug options to enable", "DEBUG" },
152         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
153                                 "Specify plugins to load", "NAME,..," },
154         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
155                                 "Specify plugins not to load", "NAME,..." },
156         { "nodetach", 'n', G_OPTION_FLAG_REVERSE,
157                                 G_OPTION_ARG_NONE, &option_detach,
158                                 "Don't run as daemon in background" },
159         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
160                                 "Show version information and exit" },
161         { NULL },
162 };
163
164 int main(int argc, char **argv)
165 {
166         GOptionContext *context;
167         GError *err = NULL;
168         DBusConnection *conn;
169         DBusError error;
170         guint signal;
171
172 #ifdef NEED_THREADS
173         if (g_thread_supported() == FALSE)
174                 g_thread_init(NULL);
175 #endif
176
177         context = g_option_context_new(NULL);
178         g_option_context_add_main_entries(context, options, NULL);
179
180         if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
181                 if (err != NULL) {
182                         g_printerr("%s\n", err->message);
183                         g_error_free(err);
184                         return 1;
185                 }
186
187                 g_printerr("An unknown error occurred\n");
188                 return 1;
189         }
190
191         g_option_context_free(context);
192
193         if (option_version == TRUE) {
194                 printf("%s\n", VERSION);
195                 exit(0);
196         }
197
198         if (option_detach == TRUE) {
199                 if (daemon(0, 0)) {
200                         perror("Can't start daemon");
201                         return 1;
202                 }
203         }
204
205         event_loop = g_main_loop_new(NULL, FALSE);
206
207 #ifdef NEED_THREADS
208         if (dbus_threads_init_default() == FALSE) {
209                 fprintf(stderr, "Can't init usage of threads\n");
210                 exit(1);
211         }
212 #endif
213
214         signal = setup_signalfd();
215
216         __ofono_log_init(argv[0], option_debug, option_detach);
217
218         dbus_error_init(&error);
219
220         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, OFONO_SERVICE, &error);
221         if (conn == NULL) {
222                 if (dbus_error_is_set(&error) == TRUE) {
223                         ofono_error("Unable to hop onto D-Bus: %s",
224                                         error.message);
225                         dbus_error_free(&error);
226                 } else {
227                         ofono_error("Unable to hop onto D-Bus");
228                 }
229
230                 goto cleanup;
231         }
232
233         g_dbus_set_disconnect_function(conn, system_bus_disconnected,
234                                         NULL, NULL);
235
236         __ofono_dbus_init(conn);
237
238         __ofono_modemwatch_init();
239
240         __ofono_manager_init();
241
242         __ofono_plugin_init(option_plugin, option_noplugin);
243
244         g_free(option_plugin);
245         g_free(option_noplugin);
246
247         g_main_loop_run(event_loop);
248
249         __ofono_plugin_cleanup();
250
251         __ofono_manager_cleanup();
252
253         __ofono_modemwatch_cleanup();
254
255         __ofono_dbus_cleanup();
256         dbus_connection_unref(conn);
257
258 cleanup:
259         g_source_remove(signal);
260
261         g_main_loop_unref(event_loop);
262
263         __ofono_log_cleanup();
264
265         return 0;
266 }