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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/* 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 gsignond
*
* Copyright (C) 2012 Intel Corporation.
*
* Contact: Amarnath Valluri <amarnath.valluri@linux.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 <grp.h>
#include <glib-unix.h>
#include <glib.h>
#include <gio/gio.h>
#include "gsignond/gsignond-log.h"
#include "daemon/dbus/gsignond-dbus.h"
#include "daemon/dbus/gsignond-dbus-server.h"
static GSignondDbusServer *_server = NULL;
static guint _sig_source_id[3];
static void
_on_server_closed (gpointer data, GObject *server)
{
_server = NULL;
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 gboolean
_handle_reload_signal (gpointer user_data)
{
GMainLoop *ml = (GMainLoop *) user_data;
DBG ("Received reload signal");
g_return_val_if_fail (ml != NULL, FALSE);
if (_server) {
g_object_weak_unref (G_OBJECT(_server), _on_server_closed, ml);
g_object_unref (_server);
DBG ("Restarting daemon ....");
_server = gsignond_dbus_server_new ();
g_object_weak_ref (G_OBJECT(_server), _on_server_closed, ml);
}
return TRUE;
}
static void
_install_sighandlers (GMainLoop *main_loop)
{
GSource *source = NULL;
GMainContext *ctx = g_main_loop_get_context (main_loop);
source = g_unix_signal_source_new (SIGHUP);
g_source_set_callback (source,
_handle_reload_signal,
main_loop,
NULL);
_sig_source_id[0] = g_source_attach (source, ctx);
source = g_unix_signal_source_new (SIGTERM);
g_source_set_callback (source,
_handle_quit_signal,
main_loop,
NULL);
_sig_source_id[1] = g_source_attach (source, ctx);
source = g_unix_signal_source_new (SIGINT);
g_source_set_callback (source,
_handle_quit_signal,
main_loop,
NULL);
_sig_source_id[2] = g_source_attach (source, ctx);
}
int main (int argc, char **argv)
{
GError *error = NULL;
GMainLoop *main_loop = NULL;
GOptionContext *opt_context = NULL;
GOptionEntry opt_entries[] = {
{NULL }
};
gid_t daemon_gid;
struct group *daemon_group;
DBG ("before: real gid %d effective gid %d", getgid (), getegid ());
daemon_gid = getgid ();
daemon_group = getgrnam ("gsignond");
if (daemon_group)
daemon_gid = daemon_group->gr_gid;
if (setegid (daemon_gid))
WARN ("setegid() failed");
DBG ("after: real gid %d effective gid %d", getgid (), getegid ());
DBG ("before: real uid %d effective uid %d", getuid (), geteuid ());
if (seteuid (getuid ()))
WARN ("seteuid() failed");
DBG ("after: real uid %d effective uid %d", getuid (), geteuid ());
#if !GLIB_CHECK_VERSION (2, 36, 0)
g_type_init ();
#endif
#ifdef ENABLE_DEBUG
g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
#endif
opt_context = g_option_context_new ("SSO daemon");
g_option_context_add_main_entries (opt_context, opt_entries, NULL);
if (!g_option_context_parse (opt_context, &argc, &argv, &error)) {
ERR ("Error parsing options: %s", error->message);
g_error_free (error);
return -1;
}
main_loop = g_main_loop_new (NULL, FALSE);
_server = gsignond_dbus_server_new ();
g_option_context_free (opt_context);
if (!_server) {
return -1;
}
g_object_weak_ref (G_OBJECT (_server), _on_server_closed, main_loop);
_install_sighandlers(main_loop);
#ifdef USE_P2P
INFO ("server started at : %s", gsignond_dbus_server_get_address (_server));
#endif
INFO ("Entering main event loop");
g_main_loop_run (main_loop);
if(_server) g_object_unref (_server);
if (main_loop) g_main_loop_unref (main_loop);
DBG("Clean shutdown");
return 0;
}
|