1 /* -*- Mode: C ; c-basic-offset: 4 -*- */
3 Copyright (C) 2007,2008,2010 Nedko Arnaudov
4 Copyright (C) 2007-2008 Juuso Alasuutari
5 Copyright (C) 2008 Marc-Olivier Barre
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #if defined(HAVE_CONFIG_H)
33 #include <dbus/dbus.h>
40 #include "controller.h"
41 #include "jack/jack.h"
42 #include "jack/jslist.h"
43 #include "jack/control.h"
45 #include "svnversion.h"
47 static char * g_log_filename
;
48 static ino_t g_log_file_ino
;
50 char *g_jackdbus_config_dir
;
51 size_t g_jackdbus_config_dir_len
; /* without terminating '\0' char */
52 char *g_jackdbus_log_dir
;
53 size_t g_jackdbus_log_dir_len
; /* without terminating '\0' char */
55 DBusConnection
*g_connection
;
58 jack_dbus_send_signal(
59 const char *sender_object_path
,
61 const char *signal_name
,
65 DBusMessage
*message_ptr
;
68 va_start(ap
, first_arg_type
);
70 message_ptr
= dbus_message_new_signal(sender_object_path
, iface
, signal_name
);
71 if (message_ptr
== NULL
)
73 jack_error("dbus_message_new_signal() failed.");
77 if (!dbus_message_append_args_valist(message_ptr
, first_arg_type
, ap
))
79 jack_error("dbus_message_append_args_valist() failed.");
83 /* Add message to outgoing message queue */
84 if (!dbus_connection_send(g_connection
, message_ptr
, NULL
))
86 jack_error("dbus_connection_send() failed.");
91 dbus_message_unref(message_ptr
);
98 * Send a method return.
100 * If call->reply is NULL (i.e. a message construct method failed
101 * due to lack of memory) attempt to send a void method return.
105 jack_dbus_send_method_return(
106 struct jack_dbus_method_call
* call
)
108 if (call
->message
== NULL
)
117 if (!dbus_connection_send (call
->connection
, call
->reply
, NULL
))
119 jack_error ("Ran out of memory trying to queue method return");
122 dbus_connection_flush (call
->connection
);
123 dbus_message_unref (call
->reply
);
128 jack_error ("send_method_return() called with a NULL message,"
129 " trying to construct a void return...");
131 if ((call
->reply
= dbus_message_new_method_return (call
->message
)))
137 jack_error ("Failed to construct method return!");
142 #define object_ptr ((struct jack_dbus_object_descriptor *)data)
145 * The D-Bus message handler for object path /org/jackaudio/Controller.
148 jack_dbus_message_handler(
149 DBusConnection
*connection
,
150 DBusMessage
*message
,
153 struct jack_dbus_method_call call
;
154 const char *interface_name
;
155 struct jack_dbus_interface_descriptor
** interface_ptr_ptr
;
157 /* Check if the message is a method call. If not, ignore it. */
158 if (dbus_message_get_type (message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
163 /* Get the invoked method's name and make sure it's non-NULL. */
164 if (!(call
.method_name
= dbus_message_get_member (message
)))
168 JACK_DBUS_ERROR_UNKNOWN_METHOD
,
169 "Received method call with empty method name");
173 /* Initialize our data. */
174 call
.context
= object_ptr
->context
;
175 call
.connection
= connection
;
176 call
.message
= message
;
179 /* Check if there's an interface specified for this method call. */
180 interface_name
= dbus_message_get_interface (message
);
181 if (interface_name
!= NULL
)
183 /* Check if we can match the interface and method.
184 * The inteface handler functions only return false if the
185 * method name was unknown, otherwise they run the specified
186 * method and return TRUE.
189 interface_ptr_ptr
= object_ptr
->interfaces
;
191 while (*interface_ptr_ptr
!= NULL
)
193 if (strcmp(interface_name
, (*interface_ptr_ptr
)->name
) == 0)
195 if (!(*interface_ptr_ptr
)->handler(&call
, (*interface_ptr_ptr
)->methods
))
208 /* No interface was specified so we have to try them all. This is
209 * dictated by the D-Bus specification which states that method calls
210 * omitting the interface must never be rejected.
213 interface_ptr_ptr
= object_ptr
->interfaces
;
215 while (*interface_ptr_ptr
!= NULL
)
217 if ((*interface_ptr_ptr
)->handler(&call
, (*interface_ptr_ptr
)->methods
))
228 JACK_DBUS_ERROR_UNKNOWN_METHOD
,
229 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist",
231 dbus_message_get_signature(message
),
235 jack_dbus_send_method_return(&call
);
238 return DBUS_HANDLER_RESULT_HANDLED
;
242 jack_dbus_message_handler_unregister(
243 DBusConnection
*connection
,
246 jack_info ("Message handler was unregistered");
252 * Check if the supplied method name exists in org.jackaudio.JackConfigure,
253 * if it does execute it and return TRUE. Otherwise return FALSE.
256 jack_dbus_run_method(
257 struct jack_dbus_method_call
*call
,
258 const struct jack_dbus_interface_method_descriptor
* methods
)
260 const struct jack_dbus_interface_method_descriptor
* method_ptr
;
262 method_ptr
= methods
;
264 while (method_ptr
->name
!= NULL
)
266 if (strcmp(call
->method_name
, method_ptr
->name
) == 0)
268 method_ptr
->handler(call
);
279 * Read arguments from a method call.
280 * If the operation fails construct an error and return false,
281 * otherwise return true.
284 jack_dbus_get_method_args(
285 struct jack_dbus_method_call
*call
,
293 va_start (args
, type
);
294 dbus_error_init (&error
);
296 if (!dbus_message_get_args_valist (call
->message
, &error
, type
, args
))
298 jack_dbus_error (call
, JACK_DBUS_ERROR_INVALID_ARGS
,
299 "Invalid arguments to method \"%s\"",
304 dbus_error_free (&error
);
311 * Read a string and a variant argument from a method call.
312 * If the operation fails construct an error and return false,
313 * otherwise return true.
316 jack_dbus_get_method_args_string_and_variant(
317 struct jack_dbus_method_call
*call
,
322 DBusMessageIter iter
, sub_iter
;
324 /* First we want a string... */
325 if (dbus_message_iter_init (call
->message
, &iter
)
326 && dbus_message_iter_get_arg_type (&iter
) == DBUS_TYPE_STRING
)
328 dbus_message_iter_get_basic (&iter
, arg1
);
329 dbus_message_iter_next (&iter
);
331 /* ...and then a variant. */
332 if (dbus_message_iter_get_arg_type (&iter
) == DBUS_TYPE_VARIANT
)
334 dbus_message_iter_recurse (&iter
, &sub_iter
);
335 dbus_message_iter_get_basic (&sub_iter
, arg2
);
336 *type_ptr
= dbus_message_iter_get_arg_type (&sub_iter
);
338 /* Got what we wanted. */
343 jack_dbus_error (call
, JACK_DBUS_ERROR_INVALID_ARGS
,
344 "Invalid arguments to method \"%s\"",
351 * Read two strings and a variant argument from a method call.
352 * If the operation fails construct an error and return false,
353 * otherwise return true.
356 jack_dbus_get_method_args_two_strings_and_variant(
357 struct jack_dbus_method_call
*call
,
363 DBusMessageIter iter
, sub_iter
;
365 /* First we want a string... */
366 if (dbus_message_iter_init (call
->message
, &iter
)
367 && dbus_message_iter_get_arg_type (&iter
) == DBUS_TYPE_STRING
)
369 dbus_message_iter_get_basic (&iter
, arg1
);
370 dbus_message_iter_next (&iter
);
372 /* ...and then a second string. */
373 if (dbus_message_iter_get_arg_type (&iter
) != DBUS_TYPE_STRING
)
378 /* Got what we wanted. */
379 dbus_message_iter_get_basic (&iter
, arg2
);
380 dbus_message_iter_next (&iter
);
382 /* ...and then a variant. */
383 if (dbus_message_iter_get_arg_type (&iter
) == DBUS_TYPE_VARIANT
)
385 dbus_message_iter_recurse (&iter
, &sub_iter
);
386 dbus_message_iter_get_basic (&sub_iter
, arg3
);
387 *type_ptr
= dbus_message_iter_get_arg_type (&sub_iter
);
389 /* Got what we wanted. */
394 jack_dbus_error (call
, JACK_DBUS_ERROR_INVALID_ARGS
,
395 "Invalid arguments to method \"%s\"",
402 * Append a variant type to a D-Bus message.
403 * Return false if something fails, true otherwise.
406 jack_dbus_message_append_variant(
407 DBusMessageIter
*iter
,
409 const char *signature
,
412 DBusMessageIter sub_iter
;
414 /* Open a variant container. */
415 if (!dbus_message_iter_open_container (iter
, DBUS_TYPE_VARIANT
, signature
, &sub_iter
))
420 /* Append the supplied value. */
421 if (!dbus_message_iter_append_basic (&sub_iter
, type
, (const void *) arg
))
423 dbus_message_iter_close_container (iter
, &sub_iter
);
427 /* Close the container. */
428 if (!dbus_message_iter_close_container (iter
, &sub_iter
))
440 * Construct an empty method return message.
442 * The operation can only fail due to lack of memory, in which case
443 * there's no sense in trying to construct an error return. Instead,
444 * call->reply will be set to NULL and handled in send_method_return().
447 jack_dbus_construct_method_return_empty(
448 struct jack_dbus_method_call
* call
)
450 call
->reply
= dbus_message_new_method_return (call
->message
);
452 if (call
->reply
== NULL
)
454 jack_error ("Ran out of memory trying to construct method return");
459 * Construct a method return which holds a single argument or, if
460 * the type parameter is DBUS_TYPE_INVALID, no arguments at all
463 * The operation can only fail due to lack of memory, in which case
464 * there's no sense in trying to construct an error return. Instead,
465 * call->reply will be set to NULL and handled in send_method_return().
468 jack_dbus_construct_method_return_single(
469 struct jack_dbus_method_call
*call
,
473 DBusMessageIter iter
;
474 call
->reply
= dbus_message_new_method_return (call
->message
);
476 if (call
->reply
== NULL
)
481 /* Void method return requested by caller. */
482 if (type
== DBUS_TYPE_INVALID
)
487 /* Prevent crash on NULL input string. */
488 else if (type
== DBUS_TYPE_STRING
&& arg
.string
== NULL
)
493 dbus_message_iter_init_append (call
->reply
, &iter
);
495 if (!dbus_message_iter_append_basic (&iter
, type
, (const void *) &arg
))
497 dbus_message_unref (call
->reply
);
505 jack_error ("Ran out of memory trying to construct method return");
509 * Construct a method return which holds an array of strings.
511 * The operation can only fail due to lack of memory, in which case
512 * there's no sense in trying to construct an error return. Instead,
513 * call->reply will be set to NULL and handled in send_method_return().
516 jack_dbus_construct_method_return_array_of_strings(
517 struct jack_dbus_method_call
*call
,
518 unsigned int num_members
,
521 DBusMessageIter iter
, sub_iter
;
524 call
->reply
= dbus_message_new_method_return (call
->message
);
530 dbus_message_iter_init_append (call
->reply
, &iter
);
532 if (!dbus_message_iter_open_container (&iter
, DBUS_TYPE_ARRAY
, "s", &sub_iter
))
537 for (i
= 0; i
< num_members
; ++i
)
539 if (!dbus_message_iter_append_basic (&sub_iter
, DBUS_TYPE_STRING
, (const void *) &array
[i
]))
541 dbus_message_iter_close_container (&iter
, &sub_iter
);
546 if (!dbus_message_iter_close_container (&iter
, &sub_iter
))
554 dbus_message_unref (call
->reply
);
558 jack_error ("Ran out of memory trying to construct method return");
561 static bool jack_dbus_log_open(void)
567 if (g_logfile
!= NULL
)
569 ret
= stat(g_log_filename
, &st
);
570 if (ret
!= 0 || g_log_file_ino
!= st
.st_ino
)
580 for (retry
= 0; retry
< 10; retry
++)
582 g_logfile
= fopen(g_log_filename
, "a");
583 if (g_logfile
== NULL
)
585 fprintf(stderr
, "Cannot open jackdbus log file \"%s\": %d (%s)\n", g_log_filename
, errno
, strerror(errno
));
589 ret
= stat(g_log_filename
, &st
);
592 g_log_file_ino
= st
.st_ino
;
600 fprintf(stderr
, "Cannot stat just opened jackdbus log file \"%s\": %d (%s). %d retries\n", g_log_filename
, errno
, strerror(errno
), retry
);
605 jack_dbus_info_callback(const char *msg
)
608 char timestamp_str
[26];
611 ctime_r(×tamp
, timestamp_str
);
612 timestamp_str
[24] = 0;
614 if (jack_dbus_log_open())
616 fprintf(g_logfile
, "%s: %s\n", timestamp_str
, msg
);
621 #define ANSI_BOLD_ON "\033[1m"
622 #define ANSI_BOLD_OFF "\033[22m"
623 #define ANSI_COLOR_RED "\033[31m"
624 #define ANSI_RESET "\033[0m"
627 jack_dbus_error_callback(const char *msg
)
630 char timestamp_str
[26];
633 ctime_r(×tamp
, timestamp_str
);
634 timestamp_str
[24] = 0;
636 if (jack_dbus_log_open())
638 fprintf(g_logfile
, "%s: " ANSI_BOLD_ON ANSI_COLOR_RED
"ERROR: %s" ANSI_RESET
"\n", timestamp_str
, msg
);
644 ensure_dir_exist(const char *dirname
, int mode
)
647 if (stat(dirname
, &st
) != 0)
651 printf("Directory \"%s\" does not exist. Creating...\n", dirname
);
652 if (mkdir(dirname
, mode
) != 0)
654 fprintf(stderr
, "Failed to create \"%s\" directory: %d (%s)\n", dirname
, errno
, strerror(errno
));
660 fprintf(stderr
, "Failed to stat \"%s\": %d (%s)\n", dirname
, errno
, strerror(errno
));
666 if (!S_ISDIR(st
.st_mode
))
668 fprintf(stderr
, "\"%s\" exists but is not directory.\n", dirname
);
676 pathname_cat(const char *pathname_a
, const char *pathname_b
)
679 int pathname_a_len
, pathname_b_len
, pathname_len
;
680 pathname_a_len
= strlen(pathname_a
);
681 pathname_b_len
= strlen(pathname_b
);
682 pathname
= malloc(pathname_a_len
+ pathname_b_len
+ 1);
683 if (pathname
== NULL
)
685 fprintf(stderr
, "Out of memory\n");
688 memcpy(pathname
, pathname_a
, pathname_a_len
);
689 memcpy(pathname
+ pathname_a_len
, pathname_b
, pathname_b_len
);
690 pathname_len
= pathname_a_len
+ pathname_b_len
;
691 pathname
[pathname_len
] = 0;
698 const char *home_dir
, *xdg_config_home
, *xdg_log_home
;
700 home_dir
= getenv("HOME");
701 if (home_dir
== NULL
)
703 fprintf(stderr
, "Environment variable HOME not set\n");
707 xdg_config_home
= getenv("XDG_CONFIG_HOME");
708 if (xdg_config_home
== NULL
)
710 if (!(xdg_config_home
= pathname_cat(home_dir
, DEFAULT_XDG_CONFIG
))) goto fail
;
713 if (!(xdg_log_home
= pathname_cat(home_dir
, DEFAULT_XDG_LOG
))) goto fail
;
715 if (!(g_jackdbus_config_dir
= pathname_cat(xdg_config_home
, JACKDBUS_DIR
))) goto fail
;
716 if (!(g_jackdbus_log_dir
= pathname_cat(xdg_log_home
, JACKDBUS_DIR
))) goto fail
;
718 if (!ensure_dir_exist(xdg_config_home
, 0700))
723 if (!ensure_dir_exist(xdg_log_home
, 0700))
728 if (!ensure_dir_exist(g_jackdbus_config_dir
, 0700))
730 free(g_jackdbus_config_dir
);
733 g_jackdbus_config_dir_len
= strlen(g_jackdbus_config_dir
);
735 if (!ensure_dir_exist(g_jackdbus_log_dir
, 0700))
737 free(g_jackdbus_log_dir
);
740 g_jackdbus_log_dir_len
= strlen(g_jackdbus_log_dir
);
751 free(g_jackdbus_config_dir
);
752 free(g_jackdbus_log_dir
);
755 static bool log_init(void)
759 log_len
= strlen(JACKDBUS_LOG
);
761 g_log_filename
= malloc(g_jackdbus_log_dir_len
+ log_len
+ 1);
762 if (g_log_filename
== NULL
)
764 fprintf(stderr
, "Out of memory\n");
768 memcpy(g_log_filename
, g_jackdbus_log_dir
, g_jackdbus_log_dir_len
);
769 memcpy(g_log_filename
+ g_jackdbus_log_dir_len
, JACKDBUS_LOG
, log_len
);
770 g_log_filename
[g_jackdbus_log_dir_len
+ log_len
] = 0;
772 if (!jack_dbus_log_open())
780 static void log_uninit(void)
782 if (g_logfile
!= NULL
)
787 free(g_log_filename
);
792 void *dbus_call_context_ptr
,
793 const char *error_name
,
800 va_start(ap
, format
);
802 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
804 jack_error_callback(buffer
);
805 if (dbus_call_context_ptr
!= NULL
)
807 if (((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
!= NULL
)
809 dbus_message_unref(((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
);
810 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
= NULL
;
813 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
= dbus_message_new_error(
814 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->message
,
823 jack_dbus_only_error(
824 void *dbus_call_context_ptr
,
825 const char *error_name
,
832 va_start(ap
, format
);
834 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
836 if (((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
!= NULL
)
838 dbus_message_unref(((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
);
839 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
= NULL
;
842 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
= dbus_message_new_error(
843 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->message
,
851 main (int argc
, char **argv
)
855 void *controller_ptr
;
857 char timestamp_str
[26];
861 ctime_r(&st
.st_mtime
, timestamp_str
);
862 timestamp_str
[24] = 0;
864 if (!jack_controller_settings_init())
870 if (argc
!= 2 || strcmp(argv
[1], "auto") != 0)
875 "jackdbus should be auto-executed by D-Bus message bus daemon.\n"
876 "If you want to run it manually anyway, specify \"auto\" as only parameter\n");
877 goto fail_uninit_xml
;
883 goto fail_uninit_xml
;
889 goto fail_uninit_paths
;
892 #if !defined(DISABLE_SIGNAL_MAGIC)
893 jackctl_setup_signals(0);
896 jack_set_error_function(jack_dbus_error_callback
);
897 jack_set_info_function(jack_dbus_info_callback
);
899 /* setup our SIGSEGV magic that prints nice stack in our logfile */
902 jack_info("------------------");
903 jack_info("Controller activated. Version %s (%s) built on %s", jack_get_version_string(), SVN_VERSION
, timestamp_str
);
905 if (!dbus_threads_init_default())
907 jack_error("dbus_threads_init_default() failed");
909 goto fail_uninit_log
;
912 dbus_error_init (&error
);
913 g_connection
= dbus_bus_get (DBUS_BUS_SESSION
, &error
);
914 if (dbus_error_is_set (&error
))
916 jack_error("Cannot connect to D-Bus session bus: %s", error
.message
);
918 goto fail_uninit_log
;
921 ret
= dbus_bus_request_name(
923 "org.jackaudio.service",
924 DBUS_NAME_FLAG_DO_NOT_QUEUE
,
928 jack_error("Cannot request service name: %s", error
.message
);
929 dbus_error_free(&error
);
931 goto fail_unref_connection
;
933 else if (ret
== DBUS_REQUEST_NAME_REPLY_EXISTS
)
935 jack_error("Requested D-Bus service name already exists");
937 goto fail_unref_connection
;
940 controller_ptr
= jack_controller_create(g_connection
);
942 if (controller_ptr
== NULL
)
945 goto fail_unref_connection
;
948 jack_info("Listening for D-Bus messages");
950 g_exit_command
= FALSE
;
951 while (!g_exit_command
&& dbus_connection_read_write_dispatch (g_connection
, 200))
953 jack_controller_run(controller_ptr
);
956 jack_controller_destroy(controller_ptr
);
958 jack_info("Controller deactivated.");
962 fail_unref_connection
:
963 dbus_connection_unref(g_connection
);
972 jack_controller_settings_uninit();