CTRL-C caused the weston process to exit
[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 <glib.h>
28 #include <gio/gio.h>
29 #include <glib-unix.h>
30
31 #if HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 #include "tlm-log.h"
35 #include "tlm-manager.h"
36 #include "tlm-seat.h"
37 #include "tlm-config.h"
38 #include "tlm-config-general.h"
39
40
41 static GMainLoop *main_loop = NULL;
42
43
44 static void
45 _on_manager_stopped_cb (TlmManager *manager, gpointer user_data)
46 {
47     DBG ("manager stopped - quit mainloop");
48     g_main_loop_quit (main_loop);
49 }
50
51 static gboolean
52 _on_sigterm_cb (gpointer data)
53 {
54     DBG ("SIGTERM/SIGINT");
55
56     TlmManager *manager = TLM_MANAGER(data);
57
58     g_signal_connect (manager,
59                       "manager-stopped",
60                       G_CALLBACK (_on_manager_stopped_cb),
61                       main_loop);
62     tlm_manager_stop (manager);
63
64     return FALSE;
65 }
66
67 static gboolean
68 _on_sighup_cb (gpointer data)
69 {
70     DBG ("SIGHUP");
71
72     TlmManager *manager = TLM_MANAGER(data);
73     tlm_manager_sighup_received (manager);
74
75     return FALSE;
76 }
77
78 static void
79 _setup_unix_signal_handlers (TlmManager *manager)
80 {
81     g_unix_signal_add (SIGTERM, _on_sigterm_cb, (gpointer) manager);
82     g_unix_signal_add (SIGINT, _on_sigterm_cb, (gpointer) manager);
83     g_unix_signal_add (SIGHUP, _on_sighup_cb, (gpointer) manager);
84 }
85
86 int main(int argc, char *argv[])
87 {
88     GError *error = 0;
89     TlmManager *manager = 0;
90
91     gboolean show_version = FALSE;
92     gboolean fatal_warnings = FALSE;
93     gchar *username = NULL;
94
95     GOptionContext *opt_context = NULL;
96     GOptionEntry opt_entries[] = {
97         { "version", 'v', 0, 
98           G_OPTION_ARG_NONE, &show_version, 
99           "Login Manager Version", "Version" },
100         { "fatal-warnings", 0, 0,
101           G_OPTION_ARG_NONE, &fatal_warnings,
102           "Make all warnings fatal", NULL },
103         { "username", 'u', 0,
104           G_OPTION_ARG_STRING, &username,
105           "Username to use", NULL },
106         {NULL }
107     };
108    
109 #if !GLIB_CHECK_VERSION(2,35,0)
110     g_type_init ();
111 #endif
112
113     opt_context = g_option_context_new ("Tizen Login Manager");
114     g_option_context_add_main_entries (opt_context, opt_entries, NULL);
115     g_option_context_parse (opt_context, &argc, &argv, &error);
116     g_option_context_free (opt_context);
117     if (error) {
118         ERR ("Error parsing options: %s", error->message);
119         g_error_free (error);
120         return -1;
121     }
122
123     if (show_version) {
124         INFO("Version: "PACKAGE_VERSION"\n");
125         return 0;
126     }
127
128     if (fatal_warnings) {
129         GLogLevelFlags log_level = g_log_set_always_fatal (G_LOG_FATAL_MASK);
130         log_level |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
131         g_log_set_always_fatal (log_level);
132     }
133
134     tlm_log_init (G_LOG_DOMAIN);
135
136     main_loop = g_main_loop_new (NULL, FALSE);
137
138     manager = tlm_manager_new (username);
139     _setup_unix_signal_handlers (manager);
140     tlm_manager_start (manager);
141
142     g_main_loop_run (main_loop);
143
144     g_object_unref (G_OBJECT(manager));
145
146     DBG ("clean shutdown");
147
148     tlm_log_close (NULL);
149
150     return 0;
151 }
152