summaryrefslogtreecommitdiff
path: root/src/sessiond/main.c
blob: d0e4db61074dd4181f93098d142102cfc0f447a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of tlm
 *
 * Copyright (C) 2014-2015 Intel Corporation.
 *
 * Contact: Imran Zaman <imran.zaman@intel.com>
 *
 * 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <config.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <glib-unix.h>
#include <glib.h>
#include <gio/gio.h>
#include <sys/prctl.h>

#include "common/tlm-log.h"
#include "tlm-session-daemon.h"

static TlmSessionDaemon *_daemon = NULL;
static guint _sig_source_id[2];

static void
_on_daemon_closed (gpointer data, GObject *server)
{
    _daemon = NULL;
    DBG ("Daemon closed");
    if (data) g_main_loop_quit ((GMainLoop *)data);
}

static gboolean
_handle_quit_signal (gpointer user_data)
{
    GMainLoop *ml = (GMainLoop *) user_data;

    g_return_val_if_fail (ml != NULL, FALSE);
    DBG ("Received quit signal");
    if (ml) g_main_loop_quit (ml);

    return FALSE;
}

static void
_install_sighandlers (GMainLoop *main_loop)
{
    GSource *source = NULL;
    GMainContext *ctx = g_main_loop_get_context (main_loop);

    if (signal (SIGINT, SIG_IGN) == SIG_ERR)
    {
        gchar strerr_buf[MAX_STRERROR_LEN] = {0,};
        WARN ("failed to ignore SIGINT: %s", strerror_r(errno, strerr_buf, MAX_STRERROR_LEN));
    }

    source = g_unix_signal_source_new (SIGTERM);
    g_source_set_callback (source,
                           _handle_quit_signal,
                           main_loop,
                           NULL);
    _sig_source_id[0] = g_source_attach (source, ctx);

    source = g_unix_signal_source_new (SIGHUP);
    g_source_set_callback (source,
                           _handle_quit_signal,
                           main_loop,
                           NULL);
    _sig_source_id[1] = g_source_attach (source, ctx);

    if (prctl(PR_SET_PDEATHSIG, SIGHUP))
        WARN ("failed to set parent death signal");
}

int main (int argc, char **argv)
{
    GMainLoop *main_loop = NULL;
    gint in_fd = 0, out_fd = 1;
    gchar strerr_buf[MAX_STRERROR_LEN] = {0,};

    /* Duplicates stdin and stdout descriptors and point the descriptors
     * to /dev/null to avoid anyone writing to descriptors
     * */
    in_fd = dup(0);
    if (in_fd == -1) {
        WARN ("Failed to dup stdin : %s(%d)", strerror_r(errno, strerr_buf, MAX_STRERROR_LEN), errno);
        in_fd = 0;
    }
    if (!freopen("/dev/null", "r+", stdin)) {
        WARN ("Unable to redirect stdin to /dev/null");
    }

    out_fd = dup(1);
    if (out_fd == -1) {
        WARN ("Failed to dup stdout : %s(%d)", strerror_r(errno, strerr_buf, MAX_STRERROR_LEN), errno);
        out_fd = 1;
    }

    if (!freopen("/dev/null", "w+", stdout)) {
        WARN ("Unable to redirect stdout to /dev/null");
    }

    /* Reattach stdout to stderr */
    //dup2 (2, 1);

#if !GLIB_CHECK_VERSION (2, 36, 0)
    g_type_init ();
#endif

    DBG ("old pgid=%u", getpgrp ());

    _daemon = tlm_session_daemon_new (in_fd, out_fd);
    if (_daemon == NULL) {
        return -1;
    }

    main_loop = g_main_loop_new (NULL, FALSE);
    g_object_weak_ref (G_OBJECT (_daemon), _on_daemon_closed, main_loop);
    _install_sighandlers (main_loop);

    tlm_log_init(G_LOG_DOMAIN);

    DBG ("Entering main event loop");

    g_main_loop_run (main_loop);

    if(_daemon) {
        g_object_unref (_daemon);
    }

    if (main_loop) {
        g_main_loop_unref (main_loop);
    }
    tlm_log_close (NULL);
    return 0;
}