3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-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
43 static GMainLoop *event_loop;
45 static void sig_term(int sig)
47 g_main_loop_quit(event_loop);
50 static gboolean option_detach = TRUE;
51 static gboolean option_debug = FALSE;
53 static GOptionEntry options[] = {
54 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
55 G_OPTION_ARG_NONE, &option_detach,
56 "Don't run as daemon in background" },
57 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
58 "Enable debug information output" },
62 static void debug(const char *format, ...)
71 vsyslog(LOG_DEBUG, format, ap);
76 static void sig_debug(int sig)
78 option_debug = !option_debug;
81 int main(int argc, char *argv[])
83 GOptionContext *context;
88 /* Drop capabilities */
89 capng_clear(CAPNG_SELECT_BOTH);
90 capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
91 CAP_NET_BIND_SERVICE, CAP_NET_ADMIN,
92 CAP_NET_RAW, CAP_IPC_LOCK, -1);
93 capng_apply(CAPNG_SELECT_BOTH);
96 context = g_option_context_new(NULL);
97 g_option_context_add_main_entries(context, options, NULL);
99 if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
101 g_printerr("%s\n", err->message);
104 g_printerr("An unknown error occurred\n");
108 g_option_context_free(context);
110 if (option_detach == TRUE) {
112 perror("Can't start daemon");
119 openlog("hcitrace", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
121 syslog(LOG_INFO, "HCI trace deamon %s", VERSION);
123 memset(&sa, 0, sizeof(sa));
124 sa.sa_flags = SA_NOCLDSTOP;
125 sa.sa_handler = sig_term;
126 sigaction(SIGTERM, &sa, NULL);
127 sigaction(SIGINT, &sa, NULL);
129 sa.sa_handler = sig_debug;
130 sigaction(SIGUSR2, &sa, NULL);
132 sa.sa_handler = SIG_IGN;
133 sigaction(SIGPIPE, &sa, NULL);
135 if (option_debug == TRUE) {
136 syslog(LOG_INFO, "Enabling debug information");
139 event_loop = g_main_loop_new(NULL, FALSE);
141 debug("Entering main loop");
143 g_main_loop_run(event_loop);
145 g_main_loop_unref(event_loop);
147 syslog(LOG_INFO, "Exit");