File: jackdbus.c

package info (click to toggle)
jackd2 1.9.17~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,524 kB
  • sloc: cpp: 48,028; ansic: 30,427; python: 12,448; sh: 270; makefile: 60
file content (976 lines) | stat: -rw-r--r-- 24,267 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/* -*- Mode: C ; c-basic-offset: 4 -*- */
/*
    Copyright (C) 2007,2008,2010 Nedko Arnaudov
    Copyright (C) 2007-2008 Juuso Alasuutari
    Copyright (C) 2008 Marc-Olivier Barre

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <signal.h>
#include <dbus/dbus.h>
#include <pthread.h>
#include <unistd.h>

#include "config.h"

#include "jackdbus.h"
#include "controller.h"
#include "jack/jack.h"
#include "jack/jslist.h"
#include "jack/control.h"
#include "sigsegv.h"
#include "svnversion.h"

static char * g_log_filename;
static ino_t g_log_file_ino;
FILE *g_logfile;
char *g_jackdbus_config_dir;
size_t g_jackdbus_config_dir_len; /* without terminating '\0' char */
char *g_jackdbus_log_dir;
size_t g_jackdbus_log_dir_len; /* without terminating '\0' char */
int g_exit_command;
DBusConnection *g_connection;

void
jack_dbus_send_signal(
    const char *sender_object_path,
    const char *iface,
    const char *signal_name,
    int first_arg_type,
    ...)
{
    DBusMessage *message_ptr;
    va_list ap;

    va_start(ap, first_arg_type);

    message_ptr = dbus_message_new_signal(sender_object_path, iface, signal_name);
    if (message_ptr == NULL)
    {
        jack_error("dbus_message_new_signal() failed.");
        goto exit;
    }

    if (!dbus_message_append_args_valist(message_ptr, first_arg_type, ap))
    {
        jack_error("dbus_message_append_args_valist() failed.");
        goto unref;
    }

    /* Add message to outgoing message queue */
    if (!dbus_connection_send(g_connection, message_ptr, NULL))
    {
        jack_error("dbus_connection_send() failed.");
        goto unref;
    }

unref:
    dbus_message_unref(message_ptr);

exit:
    va_end(ap);
}

/*
 * Send a method return.
 *
 * If call->reply is NULL (i.e. a message construct method failed
 * due to lack of memory) attempt to send a void method return.
 */
static
void
jack_dbus_send_method_return(
    struct jack_dbus_method_call * call)
{
    if (call->message == NULL)
    {
        /* async call */
        return;
    }

    if (call->reply)
    {
    retry_send:
        if (!dbus_connection_send (call->connection, call->reply, NULL))
        {
            jack_error ("Ran out of memory trying to queue method return");
        }

        dbus_connection_flush (call->connection);
        dbus_message_unref (call->reply);
        call->reply = NULL;
    }
    else
    {
        jack_error ("send_method_return() called with a NULL message,"
                    " trying to construct a void return...");

        if ((call->reply = dbus_message_new_method_return (call->message)))
        {
            goto retry_send;
        }
        else
        {
            jack_error ("Failed to construct method return!");
        }
    }
}

#define object_ptr ((struct jack_dbus_object_descriptor *)data)

/*
 * The D-Bus message handler for object path /org/jackaudio/Controller.
 */
DBusHandlerResult
jack_dbus_message_handler(
    DBusConnection *connection,
    DBusMessage *message,
    void *data)
{
    struct jack_dbus_method_call call;
    const char *interface_name;
    struct jack_dbus_interface_descriptor ** interface_ptr_ptr;

    /* Check if the message is a method call. If not, ignore it. */
    if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
    {
        goto handled;
    }

    /* Get the invoked method's name and make sure it's non-NULL. */
    if (!(call.method_name = dbus_message_get_member (message)))
    {
        jack_dbus_error(
            &call,
            JACK_DBUS_ERROR_UNKNOWN_METHOD,
            "Received method call with empty method name");
        goto send_return;
    }

    /* Initialize our data. */
    call.context = object_ptr->context;
    call.connection = connection;
    call.message = message;
    call.reply = NULL;

    /* Check if there's an interface specified for this method call. */
    interface_name = dbus_message_get_interface (message);
    if (interface_name != NULL)
    {
        /* Check if we can match the interface and method.
         * The interface handler functions only return false if the
         * method name was unknown, otherwise they run the specified
         * method and return TRUE.
         */

        interface_ptr_ptr = object_ptr->interfaces;

        while (*interface_ptr_ptr != NULL)
        {
            if (strcmp(interface_name, (*interface_ptr_ptr)->name) == 0)
            {
                if (!(*interface_ptr_ptr)->handler(&call, (*interface_ptr_ptr)->methods))
                {
                    break;
                }

                goto send_return;
            }

            interface_ptr_ptr++;
        }
    }
    else
    {
        /* No interface was specified so we have to try them all. This is
         * dictated by the D-Bus specification which states that method calls
         * omitting the interface must never be rejected.
         */

        interface_ptr_ptr = object_ptr->interfaces;

        while (*interface_ptr_ptr != NULL)
        {
            if ((*interface_ptr_ptr)->handler(&call, (*interface_ptr_ptr)->methods))
            {
                goto send_return;
            }

            interface_ptr_ptr++;
        }
    }

    jack_dbus_error(
        &call,
        JACK_DBUS_ERROR_UNKNOWN_METHOD,
        "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist",
        call.method_name,
        dbus_message_get_signature(message),
        interface_name);

send_return:
    jack_dbus_send_method_return(&call);

handled:
    return DBUS_HANDLER_RESULT_HANDLED;
}

void
jack_dbus_message_handler_unregister(
    DBusConnection *connection,
    void *data)
{
    jack_info ("Message handler was unregistered");
}

#undef object_ptr

/*
 * Check if the supplied method name exists in org.jackaudio.JackConfigure,
 * if it does execute it and return TRUE. Otherwise return FALSE.
 */
bool
jack_dbus_run_method(
    struct jack_dbus_method_call *call,
    const struct jack_dbus_interface_method_descriptor * methods)
{
    const struct jack_dbus_interface_method_descriptor * method_ptr;

    method_ptr = methods;

    while (method_ptr->name != NULL)
    {
        if (strcmp(call->method_name, method_ptr->name) == 0)
        {
            method_ptr->handler(call);
            return TRUE;
        }

        method_ptr++;
    }

    return FALSE;
}

/*
 * Read arguments from a method call.
 * If the operation fails construct an error and return false,
 * otherwise return true.
 */
bool
jack_dbus_get_method_args(
    struct jack_dbus_method_call *call,
    int type,
    ...)
{
    va_list args;
    DBusError error;
    bool retval = true;

    va_start (args, type);
    dbus_error_init (&error);

    if (!dbus_message_get_args_valist (call->message, &error, type, args))
    {
        jack_dbus_error (call, JACK_DBUS_ERROR_INVALID_ARGS,
                         "Invalid arguments to method \"%s\"",
                         call->method_name);
        retval = false;
    }

    dbus_error_free (&error);
    va_end (args);

    return retval;
}

/*
 * Read a string and a variant argument from a method call.
 * If the operation fails construct an error and return false,
 * otherwise return true.
 */
bool
jack_dbus_get_method_args_string_and_variant(
    struct jack_dbus_method_call *call,
    const char **arg1,
    message_arg_t *arg2,
    int *type_ptr)
{
    DBusMessageIter iter, sub_iter;

    /* First we want a string... */
    if (dbus_message_iter_init (call->message, &iter)
        && dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_STRING)
    {
        dbus_message_iter_get_basic (&iter, arg1);
        dbus_message_iter_next (&iter);

        /* ...and then a variant. */
        if (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_VARIANT)
        {
            dbus_message_iter_recurse (&iter, &sub_iter);
            dbus_message_iter_get_basic (&sub_iter, arg2);
            *type_ptr = dbus_message_iter_get_arg_type (&sub_iter);

            /* Got what we wanted. */
            return true;
        }
    }

    jack_dbus_error (call, JACK_DBUS_ERROR_INVALID_ARGS,
                     "Invalid arguments to method \"%s\"",
                     call->method_name);

    return false;
}

/*
 * Read two strings and a variant argument from a method call.
 * If the operation fails construct an error and return false,
 * otherwise return true.
 */
bool
jack_dbus_get_method_args_two_strings_and_variant(
    struct jack_dbus_method_call *call,
    const char **arg1,
    const char **arg2,
    message_arg_t *arg3,
    int *type_ptr)
{
    DBusMessageIter iter, sub_iter;

    /* First we want a string... */
    if (dbus_message_iter_init (call->message, &iter)
        && dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_STRING)
    {
        dbus_message_iter_get_basic (&iter, arg1);
        dbus_message_iter_next (&iter);
        
        /* ...and then a second string. */
        if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
        {
            return false;
        }
        
        /* Got what we wanted. */
        dbus_message_iter_get_basic (&iter, arg2);
        dbus_message_iter_next (&iter);

        /* ...and then a variant. */
        if (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_VARIANT)
        {
            dbus_message_iter_recurse (&iter, &sub_iter);
            dbus_message_iter_get_basic (&sub_iter, arg3);
            *type_ptr = dbus_message_iter_get_arg_type (&sub_iter);

            /* Got what we wanted. */
            return true;
        }
    }

    jack_dbus_error (call, JACK_DBUS_ERROR_INVALID_ARGS,
                     "Invalid arguments to method \"%s\"",
                     call->method_name);

    return false;
}

/*
 * Append a variant type to a D-Bus message.
 * Return false if something fails, true otherwise.
 */
bool
jack_dbus_message_append_variant(
    DBusMessageIter *iter,
    int type,
    const char *signature,
    message_arg_t *arg)
{
    DBusMessageIter sub_iter;

    /* Open a variant container. */
    if (!dbus_message_iter_open_container (iter, DBUS_TYPE_VARIANT, signature, &sub_iter))
    {
        goto fail;
    }

    /* Append the supplied value. */
    if (!dbus_message_iter_append_basic (&sub_iter, type, (const void *) arg))
    {
        dbus_message_iter_close_container (iter, &sub_iter);
        goto fail;
    }

    /* Close the container. */
    if (!dbus_message_iter_close_container (iter, &sub_iter))
    {
        goto fail;
    }

    return true;

fail:
    return false;
}

/*
 * Construct an empty method return message.
 *
 * The operation can only fail due to lack of memory, in which case
 * there's no sense in trying to construct an error return. Instead,
 * call->reply will be set to NULL and handled in send_method_return().
 */
void
jack_dbus_construct_method_return_empty(
    struct jack_dbus_method_call * call)
{
    call->reply = dbus_message_new_method_return (call->message);

    if (call->reply == NULL)
    {
        jack_error ("Ran out of memory trying to construct method return");
    }
}

/*
 * Construct a method return which holds a single argument or, if
 * the type parameter is DBUS_TYPE_INVALID, no arguments at all
 * (a void message).
 *
 * The operation can only fail due to lack of memory, in which case
 * there's no sense in trying to construct an error return. Instead,
 * call->reply will be set to NULL and handled in send_method_return().
 */
void
jack_dbus_construct_method_return_single(
    struct jack_dbus_method_call *call,
    int type,
    message_arg_t arg)
{
    DBusMessageIter iter;
    call->reply = dbus_message_new_method_return (call->message);

    if (call->reply == NULL)
    {
        goto fail_no_mem;
    }

    /* Void method return requested by caller. */
    if (type == DBUS_TYPE_INVALID)
    {
        return;
    }

    /* Prevent crash on NULL input string. */
    else if (type == DBUS_TYPE_STRING && arg.string == NULL)
    {
        arg.string = "";
    }

    dbus_message_iter_init_append (call->reply, &iter);

    if (!dbus_message_iter_append_basic (&iter, type, (const void *) &arg))
    {
        dbus_message_unref (call->reply);
        call->reply = NULL;
        goto fail_no_mem;
    }

    return;

fail_no_mem:
    jack_error ("Ran out of memory trying to construct method return");
}

/*
 * Construct a method return which holds an array of strings.
 *
 * The operation can only fail due to lack of memory, in which case
 * there's no sense in trying to construct an error return. Instead,
 * call->reply will be set to NULL and handled in send_method_return().
 */
void
jack_dbus_construct_method_return_array_of_strings(
    struct jack_dbus_method_call *call,
    unsigned int num_members,
    const char **array)
{
    DBusMessageIter iter, sub_iter;
    unsigned int i;

    call->reply = dbus_message_new_method_return (call->message);
    if (!call->reply)
    {
        goto fail;
    }

    dbus_message_iter_init_append (call->reply, &iter);

    if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &sub_iter))
    {
        goto fail_unref;
    }

    for (i = 0; i < num_members; ++i)
    {
        if (!dbus_message_iter_append_basic (&sub_iter, DBUS_TYPE_STRING, (const void *) &array[i]))
        {
            dbus_message_iter_close_container (&iter, &sub_iter);
            goto fail_unref;
        }
    }

    if (!dbus_message_iter_close_container (&iter, &sub_iter))
    {
        goto fail_unref;
    }

    return;

fail_unref:
    dbus_message_unref (call->reply);
    call->reply = NULL;

fail:
    jack_error ("Ran out of memory trying to construct method return");
}

static bool jack_dbus_log_open(void)
{
    struct stat st;
    int ret;
    int retry;

    if (g_logfile != NULL)
    {
        ret = stat(g_log_filename, &st);
        if (ret != 0 || g_log_file_ino != st.st_ino)
        {
            fclose(g_logfile);
        }
        else
        {
            return true;
        }
    }

    for (retry = 0; retry < 10; retry++)
    {
        g_logfile = fopen(g_log_filename, "a");
        if (g_logfile == NULL)
        {
            fprintf(stderr, "Cannot open jackdbus log file \"%s\": %d (%s)\n", g_log_filename, errno, strerror(errno));
            return false;
        }

        ret = stat(g_log_filename, &st);
        if (ret == 0)
        {
            g_log_file_ino = st.st_ino;
            return true;
        }

        fclose(g_logfile);
        g_logfile = NULL;
    }

    fprintf(stderr, "Cannot stat just opened jackdbus log file \"%s\": %d (%s). %d retries\n", g_log_filename, errno, strerror(errno), retry);
    return false;
}

void 
jack_dbus_info_callback(const char *msg)
{
    time_t timestamp;
    char timestamp_str[26];

    time(&timestamp);
    ctime_r(&timestamp, timestamp_str);
    timestamp_str[24] = 0;

    if (jack_dbus_log_open())
    {
        fprintf(g_logfile, "%s: %s\n", timestamp_str, msg);
        fflush(g_logfile);
    }
}

#define ANSI_BOLD_ON    "\033[1m"
#define ANSI_BOLD_OFF   "\033[22m"
#define ANSI_COLOR_RED  "\033[31m"
#define ANSI_RESET      "\033[0m"

void 
jack_dbus_error_callback(const char *msg)
{
    time_t timestamp;
    char timestamp_str[26];

    time(&timestamp);
    ctime_r(&timestamp, timestamp_str);
    timestamp_str[24] = 0;

    if (jack_dbus_log_open())
    {
        fprintf(g_logfile, "%s: " ANSI_BOLD_ON ANSI_COLOR_RED "ERROR: %s" ANSI_RESET "\n", timestamp_str, msg);
        fflush(g_logfile);
    }
}

bool
ensure_dir_exist(const char *dirname, int mode)
{
    struct stat st;
    if (stat(dirname, &st) != 0)
    {
        if (errno == ENOENT)
        {
            printf("Directory \"%s\" does not exist. Creating...\n", dirname);
            if (mkdir(dirname, mode) != 0)
            {
                fprintf(stderr, "Failed to create \"%s\" directory: %d (%s)\n", dirname, errno, strerror(errno));
                return false;
            }
        }
        else
        {
            fprintf(stderr, "Failed to stat \"%s\": %d (%s)\n", dirname, errno, strerror(errno));
            return false;
        }
    }
    else
    {
        if (!S_ISDIR(st.st_mode))
        {
            fprintf(stderr, "\"%s\" exists but is not directory.\n", dirname);
            return false;
        }
    }
    return true;
}

char *
pathname_cat(const char *pathname_a, const char *pathname_b)
{
    char *pathname;
    int pathname_a_len, pathname_b_len, pathname_len;
    pathname_a_len = strlen(pathname_a);
    pathname_b_len = strlen(pathname_b);
    pathname = malloc(pathname_a_len + pathname_b_len + 1);
    if (pathname == NULL)
    {
        fprintf(stderr, "Out of memory\n");
        return NULL;
    }
    memcpy(pathname, pathname_a, pathname_a_len);
    memcpy(pathname + pathname_a_len, pathname_b, pathname_b_len);
    pathname_len = pathname_a_len + pathname_b_len;
    pathname[pathname_len] = 0;
    return pathname;
}

bool
paths_init()
{
	const char *home_dir, *xdg_config_home, *xdg_log_home;
    
    home_dir = getenv("HOME");
    if (home_dir == NULL)
    {
        fprintf(stderr, "Environment variable HOME not set\n");
        goto fail;
    }

	xdg_config_home = getenv("XDG_CONFIG_HOME");
	if (xdg_config_home == NULL)
	{
	    if (!(xdg_config_home = pathname_cat(home_dir, DEFAULT_XDG_CONFIG))) goto fail;
	}

    if (!(xdg_log_home = pathname_cat(home_dir, DEFAULT_XDG_LOG))) goto fail;

	if (!(g_jackdbus_config_dir = pathname_cat(xdg_config_home, JACKDBUS_DIR))) goto fail;
	if (!(g_jackdbus_log_dir = pathname_cat(xdg_log_home, JACKDBUS_DIR))) goto fail;

    if (!ensure_dir_exist(xdg_config_home, 0700))
    {
        goto fail;
    }
    
    if (!ensure_dir_exist(xdg_log_home, 0700))
    {
        goto fail;
    }

    if (!ensure_dir_exist(g_jackdbus_config_dir, 0700))
    {
        free(g_jackdbus_config_dir);
        goto fail;
    }
    g_jackdbus_config_dir_len = strlen(g_jackdbus_config_dir);
    
    if (!ensure_dir_exist(g_jackdbus_log_dir, 0700))
    {
        free(g_jackdbus_log_dir);
        goto fail;
    }
    g_jackdbus_log_dir_len = strlen(g_jackdbus_log_dir);

    return true;

fail:
    return false;
}

void
paths_uninit()
{
    free(g_jackdbus_config_dir);
    free(g_jackdbus_log_dir);
}

static bool log_init(void)
{
    size_t log_len;

    log_len = strlen(JACKDBUS_LOG);

    g_log_filename = malloc(g_jackdbus_log_dir_len + log_len + 1);
    if (g_log_filename == NULL)
    {
        fprintf(stderr, "Out of memory\n");
        return false;
    }

    memcpy(g_log_filename, g_jackdbus_log_dir, g_jackdbus_log_dir_len);
    memcpy(g_log_filename + g_jackdbus_log_dir_len, JACKDBUS_LOG, log_len);
    g_log_filename[g_jackdbus_log_dir_len + log_len] = 0;

    if (!jack_dbus_log_open())
    {
        return false;
    }

    return true;
}

static void log_uninit(void)
{
    if (g_logfile != NULL)
    {
        fclose(g_logfile);
    }

    free(g_log_filename);
}

void
jack_dbus_error(
    void *dbus_call_context_ptr,
    const char *error_name,
    const char *format,
    ...)
{
    va_list ap;
    char buffer[300];

    va_start(ap, format);

    vsnprintf(buffer, sizeof(buffer), format, ap);

    jack_error_callback(buffer);
    if (dbus_call_context_ptr != NULL)
    {
        if (((struct jack_dbus_method_call *)dbus_call_context_ptr)->reply != NULL)
        {
            dbus_message_unref(((struct jack_dbus_method_call *)dbus_call_context_ptr)->reply);
            ((struct jack_dbus_method_call *)dbus_call_context_ptr)->reply = NULL;
        }

        ((struct jack_dbus_method_call *)dbus_call_context_ptr)->reply = dbus_message_new_error(
            ((struct jack_dbus_method_call *)dbus_call_context_ptr)->message,
            error_name,
            buffer);
    }

    va_end(ap);
}

void
jack_dbus_only_error(
    void *dbus_call_context_ptr,
    const char *error_name,
    const char *format,
    ...)
{
    va_list ap;
    char buffer[300];

    va_start(ap, format);

    vsnprintf(buffer, sizeof(buffer), format, ap);

    if (((struct jack_dbus_method_call *)dbus_call_context_ptr)->reply != NULL)
    {
        dbus_message_unref(((struct jack_dbus_method_call *)dbus_call_context_ptr)->reply);
        ((struct jack_dbus_method_call *)dbus_call_context_ptr)->reply = NULL;
    }

    ((struct jack_dbus_method_call *)dbus_call_context_ptr)->reply = dbus_message_new_error(
        ((struct jack_dbus_method_call *)dbus_call_context_ptr)->message,
        error_name,
        buffer);

    va_end(ap);
}

int
main (int argc, char **argv)
{
    DBusError error;
    int ret;
    void *controller_ptr;
    struct stat st;
    char timestamp_str[26];

    st.st_mtime = 0;
    stat(argv[0], &st);
    ctime_r(&st.st_mtime, timestamp_str);
    timestamp_str[24] = 0;

    if (!jack_controller_settings_init())
    {
        ret = 1;
        goto fail;
    }

    if (argc != 2 || strcmp(argv[1], "auto") != 0)
    {
        ret = 0;
        fprintf(
            stderr,
            "jackdbus should be auto-executed by D-Bus message bus daemon.\n"
            "If you want to run it manually anyway, specify \"auto\" as only parameter\n");
        goto fail_uninit_xml;
    }

    if (!paths_init())
    {
        ret = 1;
        goto fail_uninit_xml;
    }

    if (!log_init())
    {
        ret = 1;
        goto fail_uninit_paths;
    }

#if !defined(DISABLE_SIGNAL_MAGIC)
    jackctl_setup_signals(0);
#endif

    jack_set_error_function(jack_dbus_error_callback);
    jack_set_info_function(jack_dbus_info_callback);

    /* setup our SIGSEGV magic that prints nice stack in our logfile */
    setup_sigsegv();

    jack_info("------------------");
    jack_info("Controller activated. Version %s (%s) built on %s", jack_get_version_string(), SVN_VERSION, timestamp_str);

    if (!dbus_threads_init_default())
    {
        jack_error("dbus_threads_init_default() failed");
        ret = 1;
        goto fail_uninit_log;
    }

    dbus_error_init (&error);
    g_connection = dbus_bus_get (DBUS_BUS_SESSION, &error);
    if (dbus_error_is_set (&error))
    {
        jack_error("Cannot connect to D-Bus session bus: %s", error.message);
        ret = 1;
        goto fail_uninit_log;
    }

    ret = dbus_bus_request_name(
                g_connection,
                "org.jackaudio.service",
                DBUS_NAME_FLAG_DO_NOT_QUEUE,
                &error);
    if (ret == -1)
    {
        jack_error("Cannot request service name: %s", error.message);
                dbus_error_free(&error);
        ret = 1;
        goto fail_unref_connection;
    }
    else if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
    {
        jack_error("Requested D-Bus service name already exists");
        ret = 1;
        goto fail_unref_connection;
    }

    controller_ptr = jack_controller_create(g_connection);

    if (controller_ptr == NULL)
    {
        ret = 1;
        goto fail_unref_connection;
    }

    jack_info("Listening for D-Bus messages");

    g_exit_command = FALSE;
    while (!g_exit_command && dbus_connection_read_write_dispatch (g_connection, 200))
    {
        jack_controller_run(controller_ptr);
    }

    jack_controller_destroy(controller_ptr);

    jack_info("Controller deactivated.");

    ret = 0;

fail_unref_connection:
    dbus_connection_unref(g_connection);

fail_uninit_log:
    log_uninit();

fail_uninit_paths:
    paths_uninit();

fail_uninit_xml:
    jack_controller_settings_uninit();

fail:
    return ret;
}