1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of tlm
6 * Copyright (C) 2014-2015 Intel Corporation.
8 * Contact: Imran Zaman <imran.zaman@intel.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
32 #include <glib-unix.h>
35 #include <sys/prctl.h>
37 #include "common/tlm-log.h"
38 #include "tlm-session-daemon.h"
40 static TlmSessionDaemon *_daemon = NULL;
41 static guint _sig_source_id[2];
44 _on_daemon_closed (gpointer data, GObject *server)
47 DBG ("Daemon closed");
48 if (data) g_main_loop_quit ((GMainLoop *)data);
52 _handle_quit_signal (gpointer user_data)
54 GMainLoop *ml = (GMainLoop *) user_data;
56 g_return_val_if_fail (ml != NULL, FALSE);
57 DBG ("Received quit signal");
58 if (ml) g_main_loop_quit (ml);
64 _install_sighandlers (GMainLoop *main_loop)
66 GSource *source = NULL;
67 GMainContext *ctx = g_main_loop_get_context (main_loop);
69 if (signal (SIGINT, SIG_IGN) == SIG_ERR)
70 WARN ("failed to ignore SIGINT: %s", strerror(errno));
72 source = g_unix_signal_source_new (SIGTERM);
73 g_source_set_callback (source,
77 _sig_source_id[0] = g_source_attach (source, ctx);
79 source = g_unix_signal_source_new (SIGHUP);
80 g_source_set_callback (source,
84 _sig_source_id[1] = g_source_attach (source, ctx);
86 if (prctl(PR_SET_PDEATHSIG, SIGHUP))
87 WARN ("failed to set parent death signal");
90 int main (int argc, char **argv)
92 GMainLoop *main_loop = NULL;
93 gint in_fd = 0, out_fd = 1;
95 /* Duplicates stdin and stdout descriptors and point the descriptors
96 * to /dev/null to avoid anyone writing to descriptors
100 WARN ("Failed to dup stdin : %s(%d)", strerror(errno), errno);
103 if (!freopen("/dev/null", "r+", stdin)) {
104 WARN ("Unable to redirect stdin to /dev/null");
109 WARN ("Failed to dup stdout : %s(%d)", strerror(errno), errno);
113 if (!freopen("/dev/null", "w+", stdout)) {
114 WARN ("Unable to redirect stdout to /dev/null");
117 /* Reattach stdout to stderr */
120 #if !GLIB_CHECK_VERSION (2, 36, 0)
124 DBG ("old pgid=%u", getpgrp ());
126 _daemon = tlm_session_daemon_new (in_fd, out_fd);
127 if (_daemon == NULL) {
131 main_loop = g_main_loop_new (NULL, FALSE);
132 g_object_weak_ref (G_OBJECT (_daemon), _on_daemon_closed, main_loop);
133 _install_sighandlers (main_loop);
135 tlm_log_init(G_LOG_DOMAIN);
137 DBG ("Entering main event loop");
139 g_main_loop_run (main_loop);
142 g_object_unref (_daemon);
146 g_main_loop_unref (main_loop);
148 tlm_log_close (NULL);