gladish dialog for ladishd settings
[ladish.git] / daemon / main.c
blobd8fab925db7a694022af72aeddf5feac0939d3bf
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
7 * Copyright (C) 2002 Robert Ham <rah@bash.sh>
9 **************************************************************************
10 * This file contains the code that implements main() and other top-level functionality
11 **************************************************************************
13 * LADI Session Handler is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * LADI Session Handler is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
25 * or write to the Free Software Foundation, Inc.,
26 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "common.h"
31 #include <stdlib.h>
32 #include <signal.h>
33 #include <sys/stat.h>
35 #include "version.h" /* git version define */
36 #include "proctitle.h"
37 #include "loader.h"
38 #include "sigsegv.h"
39 #include "control.h"
40 #include "studio.h"
41 #include "../dbus_constants.h"
42 #include "../common/catdup.h"
43 #include "../common/dirhelpers.h"
44 #include "../proxies/a2j_proxy.h"
45 #include "../proxies/jmcore_proxy.h"
46 #include "../proxies/notify_proxy.h"
47 #include "../proxies/conf_proxy.h"
48 #include "conf.h"
50 bool g_quit;
51 const char * g_dbus_unique_name;
52 dbus_object_path g_control_object;
53 char * g_base_dir;
54 static bool g_use_notify = false;
56 #if 0
57 static DBusHandlerResult lashd_client_disconnect_handler(DBusConnection * connection, DBusMessage * message, void * data)
59 /* If the message isn't a signal the object path handler may use it */
60 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
62 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
65 const char *member, *name, *old_name;
66 struct lash_client *client;
68 if (!(member = dbus_message_get_member(message)))
70 log_error("Received JACK signal with NULL member");
71 return DBUS_HANDLER_RESULT_HANDLED;
74 if (strcmp(member, "NameOwnerChanged") != 0)
76 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
79 dbus_error_init(&err);
81 if (!dbus_message_get_args(message, &err,
82 DBUS_TYPE_STRING, &name,
83 DBUS_TYPE_STRING, &old_name,
84 DBUS_TYPE_INVALID))
86 log_error("Cannot get message arguments: %s",
87 err.message);
88 dbus_error_free(&err);
89 return DBUS_HANDLER_RESULT_HANDLED;
92 client = server_find_client_by_dbus_name(old_name);
93 if (client)
95 client_disconnected(client);
96 return DBUS_HANDLER_RESULT_HANDLED;
99 else
101 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
105 dbus_bus_add_match(
106 service->connection,
107 "type='signal'"
108 ",sender='org.freedesktop.DBus'"
109 ",path='/org/freedesktop/DBus'"
110 ",interface='org.freedesktop.DBus'"
111 ",member='NameOwnerChanged'"
112 ",arg2=''",
113 &err);
114 if (dbus_error_is_set(&err))
116 log_error("Failed to add D-Bus match rule: %s", err.message);
117 dbus_error_free(&err);
118 goto fail;
121 if (!dbus_connection_add_filter(service->connection, lashd_client_disconnect_handler, NULL, NULL))
123 log_error("Failed to add D-Bus filter");
124 goto fail;
126 #endif
128 static bool connect_dbus(void)
130 int ret;
132 dbus_error_init(&g_dbus_error);
134 g_dbus_connection = dbus_bus_get(DBUS_BUS_SESSION, &g_dbus_error);
135 if (dbus_error_is_set(&g_dbus_error))
137 log_error("Failed to get bus: %s", g_dbus_error.message);
138 dbus_error_free(&g_dbus_error);
139 goto fail;
142 g_dbus_unique_name = dbus_bus_get_unique_name(g_dbus_connection);
143 if (g_dbus_unique_name == NULL)
145 log_error("Failed to read unique bus name");
146 goto unref_connection;
149 log_info("Connected to local session bus, unique name is \"%s\"", g_dbus_unique_name);
151 ret = dbus_bus_request_name(g_dbus_connection, SERVICE_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &g_dbus_error);
152 if (ret == -1)
154 log_error("Failed to acquire bus name: %s", g_dbus_error.message);
155 dbus_error_free(&g_dbus_error);
156 goto unref_connection;
159 if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
161 log_error("Requested connection name already exists");
162 goto unref_connection;
165 g_control_object = dbus_object_path_new(CONTROL_OBJECT_PATH, &g_lashd_interface_control, NULL, NULL);
166 if (g_control_object == NULL)
168 goto unref_connection;
171 if (!dbus_object_path_register(g_dbus_connection, g_control_object))
173 goto destroy_control_object;
176 return true;
178 destroy_control_object:
179 dbus_object_path_destroy(g_dbus_connection, g_control_object);
180 unref_connection:
181 dbus_connection_unref(g_dbus_connection);
183 fail:
184 return false;
187 static void disconnect_dbus(void)
189 dbus_object_path_destroy(g_dbus_connection, g_control_object);
190 dbus_connection_unref(g_dbus_connection);
193 void term_signal_handler(int signum)
195 log_info("Caught signal %d (%s), terminating", signum, strsignal(signum));
196 g_quit = true;
199 bool install_term_signal_handler(int signum, bool ignore_if_already_ignored)
201 sig_t sigh;
203 sigh = signal(signum, term_signal_handler);
204 if (sigh == SIG_ERR)
206 log_error("signal() failed to install handler function for signal %d.", signum);
207 return false;
210 if (sigh == SIG_IGN && ignore_if_already_ignored)
212 signal(SIGTERM, SIG_IGN);
215 return true;
218 bool init_paths(void)
220 const char * home_dir;
222 home_dir = getenv("HOME");
223 if (home_dir == NULL)
225 log_error("Environment variable HOME not set");
226 return false;
229 g_base_dir = catdup(home_dir, BASE_DIR);
230 if (g_base_dir == NULL)
232 log_error("catdup failed for '%s' and '%s'", home_dir, BASE_DIR);
233 return false;
236 if (!ensure_dir_exist(g_base_dir, 0700))
238 free(g_base_dir);
239 return false;
242 return true;
245 void uninit_paths(void)
247 free(g_base_dir);
250 static void on_conf_notify_changed(void * context, const char * key, const char * value)
252 bool notify_enable;
254 if (value == NULL)
256 notify_enable = LADISH_CONF_KEY_DAEMON_NOTIFY_DEFAULT;
258 else
260 notify_enable = conf_string2bool(value);
263 if (notify_enable)
265 if (!g_use_notify)
267 g_use_notify = ladish_notify_init("LADI Session Handler");
270 else
272 log_info("Sending notifications is disabled");
273 if (g_use_notify)
275 ladish_notify_uninit();
276 g_use_notify = false;
281 int main(int argc, char ** argv, char ** envp)
283 struct stat st;
284 char timestamp_str[26];
285 int ret;
287 st.st_mtime = 0;
288 stat(argv[0], &st);
289 ctime_r(&st.st_mtime, timestamp_str);
290 timestamp_str[24] = 0;
292 lash_init_setproctitle(argc, argv, envp);
294 dbus_threads_init_default();
296 log_info("------------------");
297 log_info("LADI session handler activated. Version %s (%s) built on %s", PACKAGE_VERSION, GIT_VERSION, timestamp_str);
299 ret = EXIT_FAILURE;
301 if (!init_paths())
303 goto exit;
306 loader_init(ladish_studio_on_child_exit);
308 if (!room_templates_init())
310 goto uninit_loader;
313 if (!connect_dbus())
315 log_error("Failed to connecto to D-Bus");
316 goto uninit_room_templates;
319 /* install the signal handlers */
320 install_term_signal_handler(SIGTERM, false);
321 install_term_signal_handler(SIGINT, true);
322 install_term_signal_handler(SIGHUP, true);
323 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
325 log_error("signal(SIGPIPE, SIG_IGN).");
328 /* setup our SIGSEGV magic that prints nice stack in our logfile */
329 setup_sigsegv();
331 if (!conf_proxy_init())
333 goto uninit_dbus;
336 if (!conf_register(LADISH_CONF_KEY_DAEMON_NOTIFY, on_conf_notify_changed, NULL))
338 goto uninit_conf;
341 if (!conf_register(LADISH_CONF_KEY_DAEMON_SHELL, NULL, NULL))
343 goto uninit_conf;
346 if (!conf_register(LADISH_CONF_KEY_DAEMON_TERMINAL, NULL, NULL))
348 goto uninit_conf;
351 if (!conf_register(LADISH_CONF_KEY_DAEMON_STUDIO_AUTOSTART, NULL, NULL))
353 goto uninit_conf;
356 if (!a2j_proxy_init())
358 goto uninit_conf;
361 if (!jmcore_proxy_init())
363 goto uninit_a2j;
366 if (!ladish_studio_init())
368 goto uninit_jmcore;
371 ladish_notify_simple(LADISH_NOTIFY_URGENCY_LOW, "LADI Session Handler daemon activated", NULL);
373 while (!g_quit)
375 dbus_connection_read_write_dispatch(g_dbus_connection, 50);
376 loader_run();
377 ladish_studio_run();
380 emit_clean_exit();
381 ladish_notify_simple(LADISH_NOTIFY_URGENCY_LOW, "LADI Session Handler daemon deactivated", NULL);
383 ret = EXIT_SUCCESS;
385 log_debug("Finished, cleaning up");
387 ladish_studio_uninit();
389 uninit_jmcore:
390 jmcore_proxy_uninit();
392 uninit_a2j:
393 a2j_proxy_uninit();
395 uninit_conf:
396 if (g_use_notify)
398 ladish_notify_uninit();
401 conf_proxy_uninit();
403 uninit_dbus:
404 disconnect_dbus();
406 uninit_room_templates:
407 room_templates_uninit();
409 uninit_loader:
410 loader_uninit();
412 uninit_paths();
414 exit:
415 log_debug("Cleaned up, exiting");
417 log_info("LADI session handler deactivated");
418 log_info("------------------");
420 exit(EXIT_SUCCESS);