5 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <sys/signalfd.h>
42 static GMainLoop *event_loop = NULL;
44 static unsigned int __terminated = 0;
46 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
49 struct signalfd_siginfo si;
53 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
56 fd = g_io_channel_unix_get_fd(channel);
58 result = read(fd, &si, sizeof(si));
59 if (result != sizeof(si))
62 switch (si.ssi_signo) {
65 if (__terminated == 0) {
67 g_main_loop_quit(event_loop);
73 __obex_log_enable_debug();
83 static guint setup_signalfd(void)
91 sigaddset(&mask, SIGINT);
92 sigaddset(&mask, SIGTERM);
93 sigaddset(&mask, SIGUSR2);
94 sigaddset(&mask, SIGPIPE);
96 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
97 perror("Failed to set signal mask");
101 fd = signalfd(-1, &mask, 0);
103 perror("Failed to create signal descriptor");
107 channel = g_io_channel_unix_new(fd);
109 g_io_channel_set_close_on_unref(channel, TRUE);
110 g_io_channel_set_encoding(channel, NULL, NULL);
111 g_io_channel_set_buffered(channel, FALSE);
113 source = g_io_add_watch(channel,
114 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
115 signal_handler, NULL);
117 g_io_channel_unref(channel);
122 static char *option_debug = NULL;
123 static gboolean option_stderr = FALSE;
125 static gboolean parse_debug(const char *key, const char *value,
126 gpointer user_data, GError **error)
129 option_debug = g_strdup(value);
131 option_debug = g_strdup("*");
136 static GOptionEntry options[] = {
137 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
138 G_OPTION_ARG_CALLBACK, parse_debug,
139 "Enable debug information output", "DEBUG" },
140 { "stderr", 's', 0, G_OPTION_ARG_NONE, &option_stderr,
141 "Write log information to stderr" },
146 int main(int argc, char *argv[])
148 GOptionContext *context;
152 context = g_option_context_new(NULL);
153 g_option_context_add_main_entries(context, options, NULL);
155 g_option_context_parse(context, &argc, &argv, &gerr);
157 g_printerr("%s\n", gerr->message);
162 g_option_context_free(context);
164 event_loop = g_main_loop_new(NULL, FALSE);
166 signal = setup_signalfd();
168 __obex_log_init("obex-client", option_debug, !option_stderr);
170 if (manager_init() < 0)
173 DBG("Entering main loop");
175 g_main_loop_run(event_loop);
177 g_source_remove(signal);
181 g_main_loop_unref(event_loop);
183 __obex_log_cleanup();