daemon: fix incorrect identifiers
[ladish.git] / daemon / main.c
blob869e350aa4c3625331bc0e0c16fe3653dc81872d
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 "../catdup.h"
43 #include "dirhelpers.h"
44 #include "../proxies/a2j_proxy.h"
45 #include "../proxies/jmcore_proxy.h"
46 #include "../proxies/notify_proxy.h"
48 bool g_quit;
49 const char * g_dbus_unique_name;
50 dbus_object_path g_control_object;
51 char * g_base_dir;
53 #if 0
54 static DBusHandlerResult lashd_client_disconnect_handler(DBusConnection * connection, DBusMessage * message, void * data)
56 /* If the message isn't a signal the object path handler may use it */
57 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
59 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
62 const char *member, *name, *old_name;
63 struct lash_client *client;
65 if (!(member = dbus_message_get_member(message)))
67 log_error("Received JACK signal with NULL member");
68 return DBUS_HANDLER_RESULT_HANDLED;
71 if (strcmp(member, "NameOwnerChanged") != 0)
73 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
76 dbus_error_init(&err);
78 if (!dbus_message_get_args(message, &err,
79 DBUS_TYPE_STRING, &name,
80 DBUS_TYPE_STRING, &old_name,
81 DBUS_TYPE_INVALID))
83 log_error("Cannot get message arguments: %s",
84 err.message);
85 dbus_error_free(&err);
86 return DBUS_HANDLER_RESULT_HANDLED;
89 client = server_find_client_by_dbus_name(old_name);
90 if (client)
92 client_disconnected(client);
93 return DBUS_HANDLER_RESULT_HANDLED;
96 else
98 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
102 dbus_bus_add_match(
103 service->connection,
104 "type='signal'"
105 ",sender='org.freedesktop.DBus'"
106 ",path='/org/freedesktop/DBus'"
107 ",interface='org.freedesktop.DBus'"
108 ",member='NameOwnerChanged'"
109 ",arg2=''",
110 &err);
111 if (dbus_error_is_set(&err))
113 log_error("Failed to add D-Bus match rule: %s", err.message);
114 dbus_error_free(&err);
115 goto fail;
118 if (!dbus_connection_add_filter(service->connection, lashd_client_disconnect_handler, NULL, NULL))
120 log_error("Failed to add D-Bus filter");
121 goto fail;
123 #endif
125 static bool connect_dbus(void)
127 int ret;
129 dbus_error_init(&g_dbus_error);
131 g_dbus_connection = dbus_bus_get(DBUS_BUS_SESSION, &g_dbus_error);
132 if (dbus_error_is_set(&g_dbus_error))
134 log_error("Failed to get bus: %s", g_dbus_error.message);
135 dbus_error_free(&g_dbus_error);
136 goto fail;
139 g_dbus_unique_name = dbus_bus_get_unique_name(g_dbus_connection);
140 if (g_dbus_unique_name == NULL)
142 log_error("Failed to read unique bus name");
143 goto unref_connection;
146 log_info("Connected to local session bus, unique name is \"%s\"", g_dbus_unique_name);
148 ret = dbus_bus_request_name(g_dbus_connection, SERVICE_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &g_dbus_error);
149 if (ret == -1)
151 log_error("Failed to acquire bus name: %s", g_dbus_error.message);
152 dbus_error_free(&g_dbus_error);
153 goto unref_connection;
156 if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
158 log_error("Requested connection name already exists");
159 goto unref_connection;
162 g_control_object = dbus_object_path_new(CONTROL_OBJECT_PATH, &g_lashd_interface_control, NULL, NULL);
163 if (g_control_object == NULL)
165 goto unref_connection;
168 if (!dbus_object_path_register(g_dbus_connection, g_control_object))
170 goto destroy_control_object;
173 return true;
175 destroy_control_object:
176 dbus_object_path_destroy(g_dbus_connection, g_control_object);
177 unref_connection:
178 dbus_connection_unref(g_dbus_connection);
180 fail:
181 return false;
184 static void disconnect_dbus(void)
186 dbus_object_path_destroy(g_dbus_connection, g_control_object);
187 dbus_connection_unref(g_dbus_connection);
190 void term_signal_handler(int signum)
192 log_info("Caught signal %d (%s), terminating", signum, strsignal(signum));
193 g_quit = true;
196 bool install_term_signal_handler(int signum, bool ignore_if_already_ignored)
198 sig_t sigh;
200 sigh = signal(signum, term_signal_handler);
201 if (sigh == SIG_ERR)
203 log_error("signal() failed to install handler function for signal %d.", signum);
204 return false;
207 if (sigh == SIG_IGN && ignore_if_already_ignored)
209 signal(SIGTERM, SIG_IGN);
212 return true;
215 bool init_paths(void)
217 const char * home_dir;
219 home_dir = getenv("HOME");
220 if (home_dir == NULL)
222 log_error("Environment variable HOME not set");
223 return false;
226 g_base_dir = catdup(home_dir, BASE_DIR);
227 if (g_base_dir == NULL)
229 log_error("catdup failed for '%s' and '%s'", home_dir, BASE_DIR);
230 return false;
233 if (!ensure_dir_exist(g_base_dir, 0700))
235 free(g_base_dir);
236 return false;
239 return true;
242 void uninit_paths(void)
244 free(g_base_dir);
247 int main(int argc, char ** argv, char ** envp)
249 struct stat st;
250 char timestamp_str[26];
251 int ret;
253 st.st_mtime = 0;
254 stat(argv[0], &st);
255 ctime_r(&st.st_mtime, timestamp_str);
256 timestamp_str[24] = 0;
258 lash_init_setproctitle(argc, argv, envp);
260 dbus_threads_init_default();
262 log_info("------------------");
263 log_info("LADI session handler activated. Version %s (%s) built on %s", PACKAGE_VERSION, GIT_VERSION, timestamp_str);
265 ret = EXIT_FAILURE;
267 if (!init_paths())
269 goto exit;
272 loader_init(ladish_studio_on_child_exit);
274 if (!room_templates_init())
276 goto uninit_loader;
279 if (!connect_dbus())
281 log_error("Failed to connecto to D-Bus");
282 goto uninit_room_templates;
285 /* install the signal handlers */
286 install_term_signal_handler(SIGTERM, false);
287 install_term_signal_handler(SIGINT, true);
288 install_term_signal_handler(SIGHUP, true);
289 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
291 log_error("signal(SIGPIPE, SIG_IGN).");
294 /* setup our SIGSEGV magic that prints nice stack in our logfile */
295 setup_sigsegv();
297 if (!ladish_notify_init("LADI Session Handler"))
299 goto uninit_dbus;
302 if (!a2j_proxy_init())
304 goto uninit_notify;
307 if (!jmcore_proxy_init())
309 goto uninit_a2j;
312 if (!ladish_studio_init())
314 goto uninit_jmcore;
317 ladish_notify_simple(LADISH_NOTIFY_URGENCY_LOW, "LADI Session Handler daemon activated", NULL);
319 while (!g_quit)
321 dbus_connection_read_write_dispatch(g_dbus_connection, 50);
322 loader_run();
323 ladish_studio_run();
326 emit_clean_exit();
327 ladish_notify_simple(LADISH_NOTIFY_URGENCY_LOW, "LADI Session Handler daemon deactivated", NULL);
329 ret = EXIT_SUCCESS;
331 log_debug("Finished, cleaning up");
333 ladish_studio_uninit();
335 uninit_jmcore:
336 jmcore_proxy_uninit();
338 uninit_a2j:
339 a2j_proxy_uninit();
341 uninit_notify:
342 ladish_notify_uninit();
344 uninit_dbus:
345 disconnect_dbus();
347 uninit_room_templates:
348 room_templates_uninit();
350 uninit_loader:
351 loader_uninit();
353 uninit_paths();
355 exit:
356 log_debug("Cleaned up, exiting");
358 log_info("LADI session handler deactivated");
359 log_info("------------------");
361 exit(EXIT_SUCCESS);