dfd92ec12f2fed2706834ff284b90db47c566e85
[platform/core/system/tlm.git] / src / daemon / tlm-main.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of tlm (Tizen Login Manager)
5  *
6  * Copyright (C) 2013-2015 Intel Corporation.
7  *
8  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
9  *          Jussi Laako <jussi.laako@linux.intel.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24  * 02110-1301 USA
25  */
26
27 #include <string.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <glib.h>
31 #include <gio/gio.h>
32 #include <glib-unix.h>
33
34 #if HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 #include "tlm-log.h"
38 #include "tlm-manager.h"
39 #include "tlm-seat.h"
40 #include "tlm-config.h"
41 #include "tlm-config-general.h"
42
43
44 static GMainLoop *main_loop = NULL;
45
46
47 static void
48 _on_manager_stopped_cb (TlmManager *manager, gpointer user_data)
49 {
50     DBG ("manager stopped - quit mainloop");
51     g_main_loop_quit (main_loop);
52 }
53
54 static gboolean
55 _on_sigterm_cb (gpointer data)
56 {
57     DBG ("SIGTERM");
58
59     TlmManager *manager = TLM_MANAGER(data);
60
61     g_signal_connect (manager,
62                       "manager-stopped",
63                       G_CALLBACK (_on_manager_stopped_cb),
64                       main_loop);
65     tlm_manager_stop (manager);
66
67     return FALSE;
68 }
69
70 static gboolean
71 _on_sighup_cb (gpointer data)
72 {
73     DBG ("SIGHUP");
74
75     TlmManager *manager = TLM_MANAGER(data);
76     tlm_manager_sighup_received (manager);
77
78     return FALSE;
79 }
80
81 static void
82 _setup_unix_signal_handlers (TlmManager *manager)
83 {
84     if (signal (SIGINT, SIG_IGN) == SIG_ERR)
85         WARN ("failed ignore SIGINT: %s", strerror(errno));
86
87     g_unix_signal_add (SIGTERM, _on_sigterm_cb, (gpointer) manager);
88     g_unix_signal_add (SIGHUP, _on_sighup_cb, (gpointer) manager);
89 }
90
91 int main(int argc, char *argv[])
92 {
93     GError *error = 0;
94     TlmManager *manager = 0;
95
96     gboolean show_version = FALSE;
97     gboolean fatal_warnings = FALSE;
98     gchar *username = NULL;
99
100     GOptionContext *opt_context = NULL;
101     GOptionEntry opt_entries[] = {
102         { "version", 'v', 0, 
103           G_OPTION_ARG_NONE, &show_version, 
104           "Login Manager Version", "Version" },
105         { "fatal-warnings", 0, 0,
106           G_OPTION_ARG_NONE, &fatal_warnings,
107           "Make all warnings fatal", NULL },
108         { "username", 'u', 0,
109           G_OPTION_ARG_STRING, &username,
110           "Username to use", NULL },
111         {NULL }
112     };
113    
114 #if !GLIB_CHECK_VERSION(2,35,0)
115     g_type_init ();
116 #endif
117
118     opt_context = g_option_context_new ("Tizen Login Manager");
119     g_option_context_add_main_entries (opt_context, opt_entries, NULL);
120     g_option_context_parse (opt_context, &argc, &argv, &error);
121     g_option_context_free (opt_context);
122     if (error) {
123         ERR ("Error parsing options: %s", error->message);
124         g_error_free (error);
125         return -1;
126     }
127
128     if (show_version) {
129         INFO("Version: "PACKAGE_VERSION"\n");
130         return 0;
131     }
132
133     if (fatal_warnings) {
134         GLogLevelFlags log_level = g_log_set_always_fatal (G_LOG_FATAL_MASK);
135         log_level |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
136         g_log_set_always_fatal (log_level);
137     }
138
139     tlm_log_init (G_LOG_DOMAIN);
140
141     main_loop = g_main_loop_new (NULL, FALSE);
142
143     manager = tlm_manager_new (username);
144     _setup_unix_signal_handlers (manager);
145     tlm_manager_start (manager);
146
147     g_main_loop_run (main_loop);
148
149     g_object_unref (G_OBJECT(manager));
150
151     DBG ("clean shutdown");
152
153     tlm_log_close (NULL);
154
155     return 0;
156 }
157