1 /* -*- Mode: C ; c-basic-offset: 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.
35 #include "version.h" /* git version define */
36 #include "proctitle.h"
41 #include "../dbus_constants.h"
42 #include "../catdup.h"
43 #include "dirhelpers.h"
46 const char * g_dbus_unique_name
;
47 object_path_t
* g_control_object
;
51 static DBusHandlerResult
lashd_client_disconnect_handler(DBusConnection
* connection
, DBusMessage
* message
, void * data
)
53 /* If the message isn't a signal the object path handler may use it */
54 if (dbus_message_get_type(message
) != DBUS_MESSAGE_TYPE_SIGNAL
)
56 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
59 const char *member
, *name
, *old_name
;
60 struct lash_client
*client
;
62 if (!(member
= dbus_message_get_member(message
)))
64 lash_error("Received JACK signal with NULL member");
65 return DBUS_HANDLER_RESULT_HANDLED
;
68 if (strcmp(member
, "NameOwnerChanged") != 0)
70 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
73 dbus_error_init(&err
);
75 if (!dbus_message_get_args(message
, &err
,
76 DBUS_TYPE_STRING
, &name
,
77 DBUS_TYPE_STRING
, &old_name
,
80 lash_error("Cannot get message arguments: %s",
82 dbus_error_free(&err
);
83 return DBUS_HANDLER_RESULT_HANDLED
;
86 client
= server_find_client_by_dbus_name(old_name
);
89 client_disconnected(client
);
90 return DBUS_HANDLER_RESULT_HANDLED
;
95 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
102 ",sender='org.freedesktop.DBus'"
103 ",path='/org/freedesktop/DBus'"
104 ",interface='org.freedesktop.DBus'"
105 ",member='NameOwnerChanged'"
108 if (dbus_error_is_set(&err
))
110 lash_error("Failed to add D-Bus match rule: %s", err
.message
);
111 dbus_error_free(&err
);
115 if (!dbus_connection_add_filter(service
->connection
, lashd_client_disconnect_handler
, NULL
, NULL
))
117 lash_error("Failed to add D-Bus filter");
122 static bool connect_dbus(void)
126 g_dbus_connection
= dbus_bus_get(DBUS_BUS_SESSION
, &g_dbus_error
);
127 if (dbus_error_is_set(&g_dbus_error
))
129 lash_error("Failed to get bus: %s", g_dbus_error
.message
);
130 dbus_error_free(&g_dbus_error
);
134 g_dbus_unique_name
= dbus_bus_get_unique_name(g_dbus_connection
);
135 if (g_dbus_unique_name
== NULL
)
137 lash_error("Failed to read unique bus name");
138 goto unref_connection
;
141 lash_info("Connected to local session bus, unique name is \"%s\"", g_dbus_unique_name
);
143 ret
= dbus_bus_request_name(g_dbus_connection
, SERVICE_NAME
, DBUS_NAME_FLAG_DO_NOT_QUEUE
, &g_dbus_error
);
146 lash_error("Failed to acquire bus name: %s", g_dbus_error
.message
);
147 dbus_error_free(&g_dbus_error
);
148 goto unref_connection
;
151 if (ret
== DBUS_REQUEST_NAME_REPLY_EXISTS
)
153 lash_error("Requested connection name already exists");
154 goto unref_connection
;
157 g_control_object
= object_path_new(CONTROL_OBJECT_PATH
, NULL
, 1, &g_lashd_interface_control
);
158 if (g_control_object
== NULL
)
160 goto unref_connection
;
163 if (!object_path_register(g_dbus_connection
, g_control_object
))
165 goto destroy_control_object
;
170 destroy_control_object
:
171 object_path_destroy(g_dbus_connection
, g_control_object
);
173 dbus_connection_unref(g_dbus_connection
);
179 static void disconnect_dbus(void)
181 object_path_destroy(g_dbus_connection
, g_control_object
);
182 dbus_connection_unref(g_dbus_connection
);
185 static void on_child_exit(pid_t pid
)
187 //client_disconnected(server_find_client_by_pid(child_ptr->pid));
190 void term_signal_handler(int signum
)
192 lash_info("Caught signal %d (%s), terminating", signum
, strsignal(signum
));
196 bool install_term_signal_handler(int signum
, bool ignore_if_already_ignored
)
200 sigh
= signal(signum
, term_signal_handler
);
203 lash_error("signal() failed to install handler function for signal %d.", signum
);
207 if (sigh
== SIG_IGN
&& ignore_if_already_ignored
)
209 signal(SIGTERM
, SIG_IGN
);
215 bool init_paths(void)
217 const char * home_dir
;
219 home_dir
= getenv("HOME");
220 if (home_dir
== NULL
)
222 lash_error("Environment variable HOME not set");
226 g_base_dir
= catdup(home_dir
, BASE_DIR
);
227 if (g_base_dir
== NULL
)
229 lash_error("catdup failed for '%s' and '%s'", home_dir
, BASE_DIR
);
233 if (!ensure_dir_exist(g_base_dir
, 0700))
242 void uninit_paths(void)
247 int main(int argc
, char ** argv
, char ** envp
)
250 char timestamp_str
[26];
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 lash_info("------------------");
263 lash_info("LADI session handler activated. Version %s (%s) built on %s", PACKAGE_VERSION
, GIT_VERSION
, timestamp_str
);
272 loader_init(on_child_exit
);
276 lash_error("Failed to connecto to D-Bus");
280 /* install the signal handlers */
281 install_term_signal_handler(SIGTERM
, false);
282 install_term_signal_handler(SIGINT
, true);
283 install_term_signal_handler(SIGHUP
, true);
284 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
286 lash_error("signal(SIGPIPE, SIG_IGN).");
289 /* setup our SIGSEGV magic that prints nice stack in our logfile */
299 dbus_connection_read_write_dispatch(g_dbus_connection
, 50);
306 lash_debug("Finished, cleaning up");
319 lash_debug("Cleaned up, exiting");
321 lash_info("LADI session handler deactivated");
322 lash_info("------------------");