From 2251f476170b393eaa3c5bd3be19cb759a1adb8d Mon Sep 17 00:00:00 2001 From: David Zeuthen Date: Fri, 8 Oct 2010 17:20:12 -0400 Subject: Add enough infrastructure to make it possible to trace memory leaks # G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --leak-check=full --show-reachable=yes .libs/lt-stcd Signed-off-by: David Zeuthen --- stc/Makefile.am | 1 + stc/gposixsignal.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ stc/gposixsignal.h | 43 ++++++++++++++++++ stc/stcd.c | 21 +++++++++ 4 files changed, 194 insertions(+) create mode 100644 stc/gposixsignal.c create mode 100644 stc/gposixsignal.h diff --git a/stc/Makefile.am b/stc/Makefile.am index 1f95389..0fdc0df 100644 --- a/stc/Makefile.am +++ b/stc/Makefile.am @@ -108,6 +108,7 @@ daemon_PROGRAMS = stcd stcd_SOURCES = \ stcd.c \ + gposixsignal.h gposixsignal.c \ $(NULL) stcd_CPPFLAGS = \ diff --git a/stc/gposixsignal.c b/stc/gposixsignal.c new file mode 100644 index 0000000..2b99d88 --- /dev/null +++ b/stc/gposixsignal.c @@ -0,0 +1,129 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* GDBus - GLib D-Bus Library + * + * Copyright (C) 2010 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#include "config.h" + +#include +#include +#include + +#include "gposixsignal.h" + +typedef struct +{ + GSource source; + GPollFD pollfd; + gint signum; +} _GPosixSignalSource; + +static gboolean +_g_posix_signal_source_prepare (GSource *_source, + gint *timeout) +{ + *timeout = -1; + return FALSE; +} + +static gboolean +_g_posix_signal_source_check (GSource *_source) +{ + _GPosixSignalSource *source = (_GPosixSignalSource *) _source; + return source->pollfd.revents != 0; +} + +static gboolean +_g_posix_signal_source_dispatch (GSource *_source, + GSourceFunc callback, + gpointer user_data) + +{ + _GPosixSignalWatchFunc func = (_GPosixSignalWatchFunc) callback; + g_warn_if_fail (func != NULL); + return (*func) (user_data); +} + +static void +_g_posix_signal_source_finalize (GSource *_source) +{ + _GPosixSignalSource *source = (_GPosixSignalSource *) _source; + close (source->pollfd.fd); +} + +static GSourceFuncs _g_posix_signal_source_funcs = +{ + _g_posix_signal_source_prepare, + _g_posix_signal_source_check, + _g_posix_signal_source_dispatch, + _g_posix_signal_source_finalize +}; + +GSource * +_g_posix_signal_source_new (gint signum) +{ + sigset_t sigset; + gint fd; + GSource *_source; + _GPosixSignalSource *source; + + _source = NULL; + + sigemptyset (&sigset); + sigaddset (&sigset, signum); + + if (sigprocmask (SIG_BLOCK, &sigset, NULL) == -1) + g_assert_not_reached (); + + fd = signalfd (-1, &sigset, SFD_NONBLOCK | SFD_CLOEXEC); + + _source = g_source_new (&_g_posix_signal_source_funcs, sizeof (_GPosixSignalSource)); + source = (_GPosixSignalSource *) _source; + + source->pollfd.fd = fd; + source->pollfd.events = G_IO_IN; + g_source_add_poll (_source, &source->pollfd); + + source->signum = signum; + return _source; +} + +guint +_g_posix_signal_watch_add (gint signum, + gint priority, + _GPosixSignalWatchFunc function, + gpointer user_data, + GDestroyNotify notify) +{ + GSource *source; + guint id; + + g_return_val_if_fail (function != NULL, 0); + + source = _g_posix_signal_source_new (signum); + if (priority != G_PRIORITY_DEFAULT_IDLE) + g_source_set_priority (source, priority); + g_source_set_callback (source, (GSourceFunc) function, user_data, notify); + id = g_source_attach (source, NULL); + g_source_unref (source); + + return id; +} diff --git a/stc/gposixsignal.h b/stc/gposixsignal.h new file mode 100644 index 0000000..7c6d6e6 --- /dev/null +++ b/stc/gposixsignal.h @@ -0,0 +1,43 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* GDBus - GLib D-Bus Library + * + * Copyright (C) 2010 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifndef ___G_POSIX_SIGNAL_H__ +#define ___G_POSIX_SIGNAL_H__ + +#include + +G_BEGIN_DECLS + +typedef gboolean (*_GPosixSignalWatchFunc) (gpointer user_data); + +GSource *_g_posix_signal_source_new (gint signum); + +guint _g_posix_signal_watch_add (gint signum, + gint priority, + _GPosixSignalWatchFunc function, + gpointer user_data, + GDestroyNotify notify); + +G_END_DECLS + +#endif /* ___G_POSIX_SIGNAL_H__ */ diff --git a/stc/stcd.c b/stc/stcd.c index d7b26be..28ad6a6 100644 --- a/stc/stcd.c +++ b/stc/stcd.c @@ -25,6 +25,7 @@ #include #include "stc.h" +#include "gposixsignal.h" /* ---------------------------------------------------------------------------------------------------- */ @@ -827,11 +828,21 @@ on_item_changed (StcMonitor *monitor, /* ---------------------------------------------------------------------------------------------------- */ +static gboolean +on_sigint (gpointer user_data) +{ + GMainLoop *loop = user_data; + g_print ("Handling SIGINT\n"); + g_main_loop_quit (loop); + return FALSE; +} + int main (int argc, char *argv[]) { GMainLoop *loop; _StcDaemon *daemon; + guint sigint_id; g_type_init (); @@ -843,8 +854,18 @@ main (int argc, char *argv[]) loop = g_main_loop_new (NULL, FALSE); daemon = _stc_daemon_new (); + sigint_id = _g_posix_signal_watch_add (SIGINT, + G_PRIORITY_DEFAULT, + on_sigint, + loop, + NULL); + g_main_loop_run (loop); + g_print ("Shutting down\n"); + + if (sigint_id > 0) + g_source_remove (sigint_id); g_object_unref (daemon); g_main_loop_unref (loop); -- cgit v1.2.3