gui: remove unused and duplicate dbus helper code
[ladish.git] / daemon / main.c
blob90425960d2aed8ad0682b40a74cf57f58f849e7e
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 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"
46 bool g_quit;
47 const char * g_dbus_unique_name;
48 dbus_object_path g_control_object;
49 char * g_base_dir;
51 #if 0
52 static DBusHandlerResult lashd_client_disconnect_handler(DBusConnection * connection, DBusMessage * message, void * data)
54 /* If the message isn't a signal the object path handler may use it */
55 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
57 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
60 const char *member, *name, *old_name;
61 struct lash_client *client;
63 if (!(member = dbus_message_get_member(message)))
65 log_error("Received JACK signal with NULL member");
66 return DBUS_HANDLER_RESULT_HANDLED;
69 if (strcmp(member, "NameOwnerChanged") != 0)
71 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
74 dbus_error_init(&err);
76 if (!dbus_message_get_args(message, &err,
77 DBUS_TYPE_STRING, &name,
78 DBUS_TYPE_STRING, &old_name,
79 DBUS_TYPE_INVALID))
81 log_error("Cannot get message arguments: %s",
82 err.message);
83 dbus_error_free(&err);
84 return DBUS_HANDLER_RESULT_HANDLED;
87 client = server_find_client_by_dbus_name(old_name);
88 if (client)
90 client_disconnected(client);
91 return DBUS_HANDLER_RESULT_HANDLED;
94 else
96 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
100 dbus_bus_add_match(
101 service->connection,
102 "type='signal'"
103 ",sender='org.freedesktop.DBus'"
104 ",path='/org/freedesktop/DBus'"
105 ",interface='org.freedesktop.DBus'"
106 ",member='NameOwnerChanged'"
107 ",arg2=''",
108 &err);
109 if (dbus_error_is_set(&err))
111 log_error("Failed to add D-Bus match rule: %s", err.message);
112 dbus_error_free(&err);
113 goto fail;
116 if (!dbus_connection_add_filter(service->connection, lashd_client_disconnect_handler, NULL, NULL))
118 log_error("Failed to add D-Bus filter");
119 goto fail;
121 #endif
123 static bool connect_dbus(void)
125 int ret;
127 g_dbus_connection = dbus_bus_get(DBUS_BUS_SESSION, &g_dbus_error);
128 if (dbus_error_is_set(&g_dbus_error))
130 log_error("Failed to get bus: %s", g_dbus_error.message);
131 dbus_error_free(&g_dbus_error);
132 goto fail;
135 g_dbus_unique_name = dbus_bus_get_unique_name(g_dbus_connection);
136 if (g_dbus_unique_name == NULL)
138 log_error("Failed to read unique bus name");
139 goto unref_connection;
142 log_info("Connected to local session bus, unique name is \"%s\"", g_dbus_unique_name);
144 ret = dbus_bus_request_name(g_dbus_connection, SERVICE_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &g_dbus_error);
145 if (ret == -1)
147 log_error("Failed to acquire bus name: %s", g_dbus_error.message);
148 dbus_error_free(&g_dbus_error);
149 goto unref_connection;
152 if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
154 log_error("Requested connection name already exists");
155 goto unref_connection;
158 g_control_object = dbus_object_path_new(CONTROL_OBJECT_PATH, &g_lashd_interface_control, NULL, NULL);
159 if (g_control_object == NULL)
161 goto unref_connection;
164 if (!dbus_object_path_register(g_dbus_connection, g_control_object))
166 goto destroy_control_object;
169 return true;
171 destroy_control_object:
172 dbus_object_path_destroy(g_dbus_connection, g_control_object);
173 unref_connection:
174 dbus_connection_unref(g_dbus_connection);
176 fail:
177 return false;
180 static void disconnect_dbus(void)
182 dbus_object_path_destroy(g_dbus_connection, g_control_object);
183 dbus_connection_unref(g_dbus_connection);
186 void term_signal_handler(int signum)
188 log_info("Caught signal %d (%s), terminating", signum, strsignal(signum));
189 g_quit = true;
192 bool install_term_signal_handler(int signum, bool ignore_if_already_ignored)
194 sig_t sigh;
196 sigh = signal(signum, term_signal_handler);
197 if (sigh == SIG_ERR)
199 log_error("signal() failed to install handler function for signal %d.", signum);
200 return false;
203 if (sigh == SIG_IGN && ignore_if_already_ignored)
205 signal(SIGTERM, SIG_IGN);
208 return true;
211 bool init_paths(void)
213 const char * home_dir;
215 home_dir = getenv("HOME");
216 if (home_dir == NULL)
218 log_error("Environment variable HOME not set");
219 return false;
222 g_base_dir = catdup(home_dir, BASE_DIR);
223 if (g_base_dir == NULL)
225 log_error("catdup failed for '%s' and '%s'", home_dir, BASE_DIR);
226 return false;
229 if (!ensure_dir_exist(g_base_dir, 0700))
231 free(g_base_dir);
232 return false;
235 return true;
238 void uninit_paths(void)
240 free(g_base_dir);
243 int main(int argc, char ** argv, char ** envp)
245 struct stat st;
246 char timestamp_str[26];
247 int ret;
249 st.st_mtime = 0;
250 stat(argv[0], &st);
251 ctime_r(&st.st_mtime, timestamp_str);
252 timestamp_str[24] = 0;
254 lash_init_setproctitle(argc, argv, envp);
256 dbus_threads_init_default();
258 log_info("------------------");
259 log_info("LADI session handler activated. Version %s (%s) built on %s", PACKAGE_VERSION, GIT_VERSION, timestamp_str);
261 ret = EXIT_FAILURE;
263 if (!init_paths())
265 goto exit;
268 loader_init(studio_on_child_exit);
270 if (!connect_dbus())
272 log_error("Failed to connecto to D-Bus");
273 goto uninit_loader;
276 /* install the signal handlers */
277 install_term_signal_handler(SIGTERM, false);
278 install_term_signal_handler(SIGINT, true);
279 install_term_signal_handler(SIGHUP, true);
280 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
282 log_error("signal(SIGPIPE, SIG_IGN).");
285 /* setup our SIGSEGV magic that prints nice stack in our logfile */
286 setup_sigsegv();
288 a2j_proxy_init();
290 if (!studio_init())
292 goto uninit_dbus;
295 while (!g_quit)
297 dbus_connection_read_write_dispatch(g_dbus_connection, 50);
298 loader_run();
299 studio_run();
302 ret = EXIT_SUCCESS;
304 log_debug("Finished, cleaning up");
306 studio_uninit();
308 a2j_proxy_uninit();
310 uninit_dbus:
311 disconnect_dbus();
313 uninit_loader:
314 loader_uninit();
316 uninit_paths();
318 exit:
319 log_debug("Cleaned up, exiting");
321 log_info("LADI session handler deactivated");
322 log_info("------------------");
324 exit(EXIT_SUCCESS);