blob: 3eabfdac074eaf949ee2596680c858d8948917dc [file] [log] [blame]
/*
*
* OBEX Client
*
* Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/signalfd.h>
#include <glib.h>
#include <gdbus.h>
#include "log.h"
#include "manager.h"
static GMainLoop *event_loop = NULL;
static unsigned int __terminated = 0;
static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
gpointer user_data)
{
struct signalfd_siginfo si;
ssize_t result;
int fd;
if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
return FALSE;
fd = g_io_channel_unix_get_fd(channel);
result = read(fd, &si, sizeof(si));
if (result != sizeof(si))
return FALSE;
switch (si.ssi_signo) {
case SIGINT:
case SIGTERM:
if (__terminated == 0) {
info("Terminating");
g_main_loop_quit(event_loop);
}
__terminated = 1;
break;
case SIGUSR2:
__obex_log_enable_debug();
break;
case SIGPIPE:
/* ignore */
break;
}
return TRUE;
}
static guint setup_signalfd(void)
{
GIOChannel *channel;
guint source;
sigset_t mask;
int fd;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGUSR2);
sigaddset(&mask, SIGPIPE);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
perror("Failed to set signal mask");
return 0;
}
fd = signalfd(-1, &mask, 0);
if (fd < 0) {
perror("Failed to create signal descriptor");
return 0;
}
channel = g_io_channel_unix_new(fd);
g_io_channel_set_close_on_unref(channel, TRUE);
g_io_channel_set_encoding(channel, NULL, NULL);
g_io_channel_set_buffered(channel, FALSE);
source = g_io_add_watch(channel,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
signal_handler, NULL);
g_io_channel_unref(channel);
return source;
}
static char *option_debug = NULL;
static gboolean option_stderr = FALSE;
static gboolean parse_debug(const char *key, const char *value,
gpointer user_data, GError **error)
{
if (value)
option_debug = g_strdup(value);
else
option_debug = g_strdup("*");
return TRUE;
}
static GOptionEntry options[] = {
{ "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
G_OPTION_ARG_CALLBACK, parse_debug,
"Enable debug information output", "DEBUG" },
{ "stderr", 's', 0, G_OPTION_ARG_NONE, &option_stderr,
"Write log information to stderr" },
{ NULL },
};
int main(int argc, char *argv[])
{
GOptionContext *context;
GError *gerr = NULL;
guint signal;
context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, options, NULL);
g_option_context_parse(context, &argc, &argv, &gerr);
if (gerr != NULL) {
g_printerr("%s\n", gerr->message);
g_error_free(gerr);
exit(EXIT_FAILURE);
}
g_option_context_free(context);
event_loop = g_main_loop_new(NULL, FALSE);
signal = setup_signalfd();
__obex_log_init("obex-client", option_debug, !option_stderr);
if (manager_init() < 0)
exit(EXIT_FAILURE);
DBG("Entering main loop");
g_main_loop_run(event_loop);
g_source_remove(signal);
manager_exit();
g_main_loop_unref(event_loop);
__obex_log_cleanup();
return 0;
}