5 * D-BUS Service Utilities
7 * Provides MINIMAL utilities for construction of D-BUS "Services".
9 * Copyright(C) Jason Vas Dias, Red Hat Inc., 2005
10 * Modified by Adam Tkac, Red Hat Inc., 2007
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation at
15 * http://www.fsf.org/licensing/licenses/gpl.txt
16 * and included in this software distribution as the "LICENSE" file.
18 * This program 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.
25 #include <sys/types.h>
27 #include <linux/limits.h>
29 #include <sys/socket.h>
30 #include <sys/select.h>
32 #include <sys/ioctl.h>
38 extern size_t strnlen(const char *s
, size_t maxlen
);
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
45 typedef void (*__free_fn_t
) (void *__nodep
);
46 extern void tdestroy (void *__root
, __free_fn_t __freefct
);
52 #define DBUS_API_SUBJECT_TO_CHANGE "Very Annoying and Silly!"
53 #include <dbus/dbus.h>
55 #include <named/dbus_service.h>
56 #include <isc/result.h>
60 DBusConnection
*connection
;
61 DBusDispatchStatus dispatchStatus
;
63 dbus_svc_WatchHandler wh
;
65 const char * unique_name
;
66 dbus_svc_MessageHandler mh
;
68 dbus_svc_MessageHandler mf
;
70 dbus_svc_ShutdownHandler sh
;
72 dbus_svc_ErrorHandler eh
;
73 dbus_svc_ErrorHandler dh
;
87 DBusMessage
*currentMessage
;
89 } DBusConnectionState
;
96 dbus_svc_MessageHandler mh
;
104 dbus_svc_MessageHandler mh
;
106 } MessageHandlerNode
;
110 DBusConnectionState
*cs
;
111 dbus_svc_MessageHandler mf
;
117 typedef struct dbto_s
120 DBusConnectionState
*cs
;
122 } DBusConnectionTimeout
;
124 static void no_free( void *p
){ p
=0; }
126 static int ptr_key_comparator( const void *p1
, const void *p2
)
138 static DBusHandlerResult
139 default_message_filter
140 ( DBusConnection
*connection
,
141 DBusMessage
*message
,
145 DBusConnectionState
*cs
= p
;
146 uint32_t type
=dbus_message_get_type( message
),
147 serial
=dbus_message_get_serial( message
);
148 uint8_t reply
=dbus_message_get_no_reply( message
)==0;
150 *path
= dbus_message_get_path( message
),
151 *dest
= dbus_message_get_destination( message
),
152 *member
= dbus_message_get_member( message
),
153 *interface
=dbus_message_get_interface( message
),
154 *sender
=dbus_message_get_sender( message
),
155 *signature
=dbus_message_get_signature( message
);
156 connection
= connection
;
159 (*(cs
->mf
))( cs
, type
, reply
, serial
, dest
, path
, member
, interface
, 0L,
160 sender
, signature
, message
, 0L, 0L, 0L, cs
->def_mf_obj
167 ( DBusConnectionState
*cs
, dbus_svc_MessageHandler mh
, void *obj
, int n_matches
, ... )
173 va_start(va
, n_matches
);
176 cs
->def_mf_obj
= obj
;
178 if ( ! dbus_connection_add_filter (cs
->connection
, default_message_filter
, cs
, NULL
))
180 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_add_filter: dbus_connection_add_filter failed");
187 memset(&error
,'\0',sizeof(DBusError
));
188 dbus_error_init(&error
);
191 m
= va_arg(va
, char* ) ;
193 dbus_bus_add_match(cs
->connection
, m
, &error
);
195 if( dbus_error_is_set(&error
))
197 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_add_filter: dbus_bus_add_match failed for %s: %s %s",
198 m
, error
.name
, error
.message
211 dbus_svc_get_args_va(DBusConnectionState
*cs
, DBusMessage
* msg
, dbus_svc_DataType firstType
, va_list va
)
214 memset(&error
,'\0',sizeof(DBusError
));
215 dbus_error_init(&error
);
216 if( (!dbus_message_get_args_valist(msg
, &error
, firstType
, va
)) || dbus_error_is_set(&error
) )
218 if( dbus_error_is_set(&error
) )
220 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_get_args failed: %s %s",error
.name
, error
.message
);
221 dbus_error_free(&error
);
223 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_get_args failed: dbus_message_get_args_valist failed");
230 dbus_svc_get_args(DBusConnectionState
*cs
, DBusMessage
* msg
, dbus_svc_DataType firstType
, ...)
234 va_start(va
, firstType
);
235 r
= dbus_svc_get_args_va( cs
, msg
, firstType
, va
);
242 ( DBusConnectionState
*cs
,
243 dbus_svc_MessageType type
,
244 int32_t reply_serial
,
245 uint32_t *new_serial
,
246 const char *destination
,
248 const char *interface
,
250 dbus_svc_DataType firstType
,
254 DBusMessageIter iter
;
269 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: dbus_svc_new_message failed");
273 if( type
!= DBUS_MESSAGE_TYPE_ERROR
)
275 if( !dbus_message_append_args_valist( msg
, firstType
, va
) )
277 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: dbus_message_append_args_valist failed");
282 if( firstType
== DBUS_TYPE_STRING
)
285 e
= va_arg( va
, char* );
286 if( (e
== 0L) || !dbus_message_set_error_name( msg
, e
) )
288 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: dbus_message_set_error_name failed");
291 firstType
= va_arg(va
, int);
292 if( firstType
== DBUS_TYPE_STRING
)
295 e
= va_arg( va
, char* );
298 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: NULL error message");
301 dbus_message_iter_init_append (msg
, &iter
);
302 if( !dbus_message_iter_append_basic
303 (&iter
, DBUS_TYPE_STRING
, &e
)
306 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: dbus_message_iter_append_basic failed");
312 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: unhandled type for error name: %c", firstType
);
317 if( !dbus_connection_send(cs
->connection
, msg
, new_serial
) )
319 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: dbus_message_send failed");
322 if( cs
->dh
!= 0L ) (*(cs
->dh
))("Sending message");
323 dbus_connection_flush(cs
->connection
);
329 ( DBusConnectionState
*cs
,
330 dbus_svc_MessageType type
,
331 int32_t reply_serial
,
332 uint32_t *new_serial
,
333 const char *destination
,
335 const char *interface
,
337 dbus_svc_DataType firstType
,
343 va_start(va
, firstType
);
344 r
= dbus_svc_send_va(cs
, type
, reply_serial
, new_serial
, destination
, path
,interface
,member
,firstType
,va
);
349 dbus_svc_MessageHandle
351 ( DBusConnectionState
* cs
,
352 dbus_svc_MessageType type
,
353 int32_t reply_serial
,
354 const char *destination
,
356 const char *interface
,
360 DBusMessage
*msg
= dbus_message_new(type
);
364 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_new_message: dbus_message_set_reply_serial failed");
368 if( reply_serial
!= -1 )
370 if( !dbus_message_set_reply_serial(msg
,reply_serial
) )
372 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_new_message: dbus_message_set_reply_serial failed");
377 if( (destination
!=0L) && !dbus_message_set_destination(msg
, destination
) )
379 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_new_message: dbus_message_set_destination failed");
383 if( !dbus_message_set_path(msg
, path
) )
385 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_new_message: dbus_message_set_path failed");
389 if( ! dbus_message_set_interface(msg
,interface
) )
391 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_new_message: dbus_message_set_interface failed - %s", interface
);
395 if( !dbus_message_set_member(msg
,member
) )
397 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_new_message: dbus_message_set_member failed");
405 dbus_svc_send_message
407 DBusConnectionState
*cs
,
408 dbus_svc_MessageHandle msg
,
412 if( !dbus_connection_send(cs
->connection
, msg
, new_serial
) )
414 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: dbus_message_send failed");
417 if( cs
->dh
!= 0L ) (*(cs
->dh
))("Sending message");
418 dbus_connection_flush(cs
->connection
);
423 dbus_svc_message_append_args(DBusConnectionState
*cs
, dbus_svc_MessageHandle msg
, dbus_svc_DataType firstType
, ...)
426 va_start(va
, firstType
);
427 if( !dbus_message_append_args_valist( msg
, firstType
, va
) )
429 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_send: dbus_message_append_args failed");
437 dbus_svc_MessageHandle
439 ( DBusConnectionState
*cs
,
440 const char *destination
,
443 const char *interface
,
444 dbus_svc_DataType firstType
,
448 DBusMessage
*message
=0L, *reply
=0L;
451 int reply_timeout
=20000;
453 va_start(va
, firstType
);
455 memset(&error
,'\0',sizeof(DBusError
));
456 dbus_error_init(&error
);
459 dbus_message_new_method_call
468 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_call: dbus_message_new_method_call failed");
473 if( !dbus_message_append_args_valist( message
, firstType
, va
) )
475 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_call: dbus_message_append_args_valist failed");
481 dbus_connection_send_with_reply_and_block
483 message
, reply_timeout
,
489 if( cs
->eh
!= 0L ) (*(cs
->eh
))("dbus_svc_call: dbus_message_send_with_reply_and_block failed: %s %s",
490 error
.name
, error
.message
499 dbus_svc_MessageIterator
500 dbus_svc_message_iterator_new( DBusConnectionState
*cs
, DBusMessage
*msg
)
502 DBusMessageIter
*iter
= malloc( sizeof(DBusMessageIter
) );
507 if( !dbus_message_iter_init( msg
, iter
))
517 dbus_svc_message_next_arg_type( DBusConnectionState
*cs
, dbus_svc_MessageIterator iter
)
521 return dbus_message_iter_get_arg_type( iter
);
525 dbus_svc_message_next_arg( DBusConnectionState
*cs
, dbus_svc_MessageIterator iter
, void *value
)
529 dbus_message_iter_get_basic( iter
, value
);
530 dbus_message_iter_next( iter
);
534 dbus_svc_message_element_type(DBusConnectionState
*cs
, dbus_svc_MessageIterator iter
)
538 return dbus_message_iter_get_element_type(iter
);
542 dbus_svc_message_get_elements( DBusConnectionState
*cs
, dbus_svc_MessageIterator iter
, uint32_t *n
, void *array
)
546 dbus_message_iter_get_fixed_array( iter
, n
, array
);
549 void dbus_svc_message_iterator_free( DBusConnectionState
*cs
, dbus_svc_MessageIterator iter
)
556 uint8_t dbus_svc_message_type( DBusMessage
*msg
)
558 return dbus_message_get_type( msg
);
561 static DBusConnectionState
*
562 dbcs_new( DBusConnection
*connection
)
564 DBusConnectionState
*dbcs
= (DBusConnectionState
*) malloc( sizeof(DBusConnectionState
) );
567 memset( dbcs
, '\0', sizeof( DBusConnectionState
));
568 dbcs
->connection
= connection
;
573 static DBusConnectionTimeout
*
574 timeout_new( DBusTimeout
*timeout
)
576 DBusConnectionTimeout
*to
= (DBusConnectionTimeout
*) malloc ( sizeof(DBusConnectionTimeout
) );
580 dbus_timeout_set_data(timeout
, to
, 0L);
581 if( dbus_timeout_get_enabled(timeout
) )
582 gettimeofday(&(to
->tv
),0L);
593 add_timeout( DBusTimeout
*timeout
, void *csp
)
595 DBusConnectionState
*cs
= csp
;
596 DBusConnectionTimeout
*to
= timeout_new(timeout
);
597 if( cs
->dh
!= 0L ) (*(cs
->dh
))("add_timeout: %d", dbus_timeout_get_interval(timeout
));
601 if( tsearch((void*)to
, &(cs
->timeouts
), ptr_key_comparator
) != 0L )
604 if( cs
->eh
!= 0L ) (*(cs
->eh
))("add_timeout: out of memory");
609 remove_timeout( DBusTimeout
*timeout
, void *csp
)
611 DBusConnectionState
*cs
= csp
;
612 DBusConnectionTimeout
*to
= dbus_timeout_get_data(timeout
);
613 if( (to
!= 0L) && (to
->to
== timeout
) )
615 if( cs
->dh
!= 0L ) (*(cs
->dh
))("remove_timeout: %d", dbus_timeout_get_interval(to
->to
));
616 if( tdelete((const void*)to
, &(cs
->timeouts
), ptr_key_comparator
) != 0L )
620 if( cs
->eh
!= 0L ) (*(cs
->eh
))("remove_timeout: can't happen?!?: timeout data %p not found", to
);
622 if( cs
->eh
!= 0L ) (*(cs
->eh
))("remove_timeout: can't happen?!?: timeout %p did not record data %p %p",
623 timeout
, to
, ((to
!= 0L) ? to
->to
: 0L)
628 toggle_timeout( DBusTimeout
*timeout
, void *csp
)
630 DBusConnectionState
*cs
= csp
;
631 DBusConnectionTimeout
**top
= tfind( (const void*) dbus_timeout_get_data(timeout
),
636 if( (top
!= 0L) && ((to
=*top
) != 0L) && (to
->to
== timeout
) )
638 if( cs
->dh
!= 0L ) (*(cs
->dh
))("toggle_timeout: %d", dbus_timeout_get_interval(to
->to
));
639 if( dbus_timeout_get_enabled(timeout
) )
640 gettimeofday(&(to
->tv
),0L);
647 if( cs
->eh
!= 0L ) (*(cs
->eh
))("toggle_timeout: can't happen?!?: timeout %p %s %p %p", timeout
,
648 ((to
==0L) ? "did not record data" : "not found"),
649 to
, ((to
!= 0L) ? to
->to
: 0L)
654 process_timeout( const void *p
, const VISIT which
, const int level
)
656 DBusConnectionState
*cs
;
657 void * const *tpp
= p
;
658 DBusConnectionTimeout
*to
;
660 float now
, then
, interval
;
661 int l
= level
? 1 : 0;
664 gettimeofday(&tv
,0L);
666 if( (tpp
!= 0L) && (*tpp
!= 0L) && ((which
== postorder
) || (which
== leaf
)) )
668 to
= (DBusConnectionTimeout
*)*tpp
;
670 if ( !dbus_timeout_get_enabled(to
->to
) )
672 cs
= dbus_timeout_get_data(to
->to
);
673 then
= ((float)to
->tv
.tv_sec
) + (((float)to
->tv
.tv_usec
) / 1000000.0);
676 interval
= ((float)dbus_timeout_get_interval(to
->to
)) / 1000.0;
677 now
= ((float)tv
.tv_sec
) + (( (float)tv
.tv_usec
) / 1000000.0);
678 if( (now
- then
) >= interval
)
680 if( cs
->dh
!= 0L ) (*(cs
->dh
))("handle_timeout: %d - %f %f %f", dbus_timeout_get_interval(to
->to
), then
, now
, interval
);
681 dbus_timeout_handle( to
->to
);
692 process_timeouts ( DBusConnectionState
*cs
)
694 twalk( cs
->timeouts
, process_timeout
);
698 set_watch_fds( DBusWatch
*watch
, DBusConnectionState
*cs
)
700 uint8_t flags
= dbus_watch_get_flags(watch
);
701 int fd
= dbus_watch_get_fd(watch
);
706 if ( dbus_watch_get_enabled(watch
) )
708 if ( flags
& DBUS_WATCH_READABLE
)
710 FD_SET(fd
, &(cs
->r_fds
));
712 (*(cs
->wh
))( fd
, WATCH_ENABLE
| WATCH_READ
, cs
->wh_arg
);
715 FD_CLR(fd
, &(cs
->r_fds
));
717 (*(cs
->wh
))( fd
, WATCH_READ
, cs
->wh_arg
);
720 if ( flags
& DBUS_WATCH_WRITABLE
)
722 FD_SET(fd
, &(cs
->w_fds
));
724 (*(cs
->wh
))( fd
, WATCH_ENABLE
| WATCH_WRITE
, cs
->wh_arg
);
727 FD_CLR(fd
, &(cs
->w_fds
));
729 (*(cs
->wh
))( fd
, WATCH_WRITE
, cs
->wh_arg
);
731 if ( flags
& DBUS_WATCH_ERROR
)
733 FD_SET(fd
, &(cs
->e_fds
));
735 (*(cs
->wh
))( fd
, WATCH_ENABLE
| WATCH_ERROR
, cs
->wh_arg
);
738 FD_CLR(fd
, &(cs
->e_fds
));
740 (*(cs
->wh
))( fd
, WATCH_ERROR
, cs
->wh_arg
);
744 if( FD_ISSET( fd
, &(cs
->r_fds
)) )
746 (*(cs
->wh
))( fd
, WATCH_READ
, cs
->wh_arg
);
747 FD_CLR(fd
, &(cs
->r_fds
));
749 if( FD_ISSET( fd
, &(cs
->w_fds
)) )
751 (*(cs
->wh
))( fd
, WATCH_WRITE
, cs
->wh_arg
);
752 FD_CLR(fd
, &(cs
->w_fds
));
754 if( FD_ISSET( fd
, &(cs
->e_fds
)) )
756 (*(cs
->wh
))( fd
, WATCH_ERROR
, cs
->wh_arg
);
757 FD_CLR(fd
, &(cs
->e_fds
));
762 add_watch ( DBusWatch
*watch
, void *csp
)
764 DBusConnectionState
*cs
= csp
;
766 dbus_watch_set_data(watch
, cs
, no_free
);
767 if( cs
->dh
!= 0L ) (*(cs
->dh
))("add_watch: %d", dbus_watch_get_fd(watch
));
768 if( tsearch((const void*)watch
,&(cs
->watches
),ptr_key_comparator
) == 0L )
770 if( cs
->eh
!= 0L ) (*(cs
->eh
))("add_watch: out of memory");
773 set_watch_fds(watch
,cs
);
778 remove_watch ( DBusWatch
*watch
, void *csp
)
780 DBusConnectionState
*cs
= csp
;
781 int fd
= dbus_watch_get_fd(watch
);
782 if( tdelete((const void*)watch
, &(cs
->watches
), ptr_key_comparator
) == 0L )
783 if( cs
->eh
!= 0L ) (*(cs
->eh
))("remove_watch: can't happen?!?: watch not found");
784 if( cs
->dh
!= 0L ) (*(cs
->dh
))("remove_watch: %d", dbus_watch_get_fd(watch
));
785 FD_CLR(fd
, &(cs
->r_fds
));
786 FD_CLR(fd
, &(cs
->w_fds
));
787 FD_CLR(fd
, &(cs
->e_fds
));
789 (*(cs
->wh
))(dbus_watch_get_fd(watch
), WATCH_READ
| WATCH_WRITE
| WATCH_ERROR
, cs
->wh_arg
);
793 toggle_watch ( DBusWatch
*watch
, void *csp
)
795 DBusConnectionState
*cs
= csp
;
796 if( cs
->dh
!= 0L ) (*(cs
->dh
))("toggle_watch: %d", dbus_watch_get_fd(watch
));
797 set_watch_fds(watch
,cs
);
801 process_watch( const void *p
, const VISIT which
, const int level
)
807 DBusConnectionState
*cs
;
808 int l
= level
? 1 : 0;
811 if((wpp
!= 0L) && (*wpp
!= 0L) && ( (which
== postorder
) || (which
== leaf
) ) )
813 w
= (DBusWatch
*)*wpp
;
814 cs
= dbus_watch_get_data( w
);
817 if( ! dbus_watch_get_enabled(w
) )
819 fd
= dbus_watch_get_fd( w
);
820 flags
= dbus_watch_get_flags( w
);
821 if( cs
->dh
!= 0L ) (*(cs
->dh
))("handle_watch: %d", dbus_watch_get_fd( w
));
822 if ( (flags
& DBUS_WATCH_READABLE
) && (FD_ISSET(fd
, &(cs
->s_r_fds
))) )
823 dbus_watch_handle(w
, DBUS_WATCH_READABLE
);
824 if ( (flags
& DBUS_WATCH_WRITABLE
) && (FD_ISSET(fd
, &(cs
->s_w_fds
))) )
825 dbus_watch_handle(w
, DBUS_WATCH_READABLE
);
826 if ( (flags
& DBUS_WATCH_ERROR
) && (FD_ISSET(fd
, &(cs
->s_e_fds
))) )
827 dbus_watch_handle(w
, DBUS_WATCH_ERROR
);
832 process_watches ( DBusConnectionState
*cs
)
834 twalk( cs
->watches
, process_watch
);
837 void dbus_svc_handle_watch( DBusConnectionState
*cs
, int fd
, dbus_svc_WatchFlags action
)
842 FD_SET(fd
, &(cs
->s_r_fds
));
846 FD_SET(fd
, &(cs
->s_w_fds
));
850 FD_SET(fd
, &(cs
->s_e_fds
));
857 ( DBusConnection
*connection
,
858 DBusDispatchStatus new_status
,
862 connection
=connection
;
863 DBusConnectionState
*cs
= csp
;
864 cs
->dispatchStatus
= new_status
;
868 dbus_svc_main_loop( DBusConnectionState
*cs
, void (*idle_handler
)(DBusConnectionState
*) )
870 struct timeval timeout
={0,200000};
873 while( cs
->status
!= SHUTDOWN
)
875 cs
->s_r_fds
= cs
->r_fds
;
876 cs
->s_w_fds
= cs
->w_fds
;
877 cs
->s_e_fds
= cs
->e_fds
;
880 timeout
.tv_usec
= 200000;
882 if ( (n_fds
= select(cs
->n
, &(cs
->s_r_fds
), &(cs
->s_w_fds
), &(cs
->s_e_fds
), &timeout
)) < 0 )
886 if( cs
->eh
!= 0L ) (*(cs
->eh
))( "select failed: %d : %s", errno
, strerror(errno
));
894 process_timeouts(cs
);
896 if ( cs
->dispatchStatus
!= DBUS_DISPATCH_COMPLETE
)
897 dbus_connection_dispatch( cs
->connection
);
899 if( idle_handler
!= 0L )
904 void dbus_svc_dispatch(DBusConnectionState
*cs
)
908 FD_ZERO(&(cs
->s_r_fds
));
909 FD_ZERO(&(cs
->s_w_fds
));
910 FD_ZERO(&(cs
->s_e_fds
));
912 process_timeouts(cs
);
914 while ( cs
->dispatchStatus
!= DBUS_DISPATCH_COMPLETE
)
915 dbus_connection_dispatch( cs
->connection
);
919 dbus_svc_quit( DBusConnectionState
*cs
)
921 cs
->status
= SHUTDOWN
;
926 ( DBusConnection
*connection
,
928 dbus_svc_WatchHandler wh
,
929 dbus_svc_ErrorHandler eh
,
930 dbus_svc_ErrorHandler dh
,
934 *dbus
= dbcs_new( connection
);
938 if(eh
)(*(eh
))("connection_setup: out of memory");
942 (*dbus
)->wh_arg
= wh_arg
;
946 if (!dbus_connection_set_watch_functions
947 ( (*dbus
)->connection
,
956 if( (*dbus
)->eh
!= 0L ) (*((*dbus
)->eh
))("connection_setup: dbus_connection_set_watch_functions failed");
960 if (!dbus_connection_set_timeout_functions
970 if( (*dbus
)->eh
!= 0L ) (*((*dbus
)->eh
))("connection_setup: dbus_connection_set_timeout_functions failed");
974 dbus_connection_set_dispatch_status_function
981 if (dbus_connection_get_dispatch_status (connection
) != DBUS_DISPATCH_COMPLETE
)
982 dbus_connection_ref(connection
);
984 return ISC_R_SUCCESS
;
990 dbus_connection_set_dispatch_status_function (connection
, NULL
, NULL
, NULL
);
991 dbus_connection_set_watch_functions (connection
, NULL
, NULL
, NULL
, NULL
, NULL
);
992 dbus_connection_set_timeout_functions (connection
, NULL
, NULL
, NULL
, NULL
, NULL
);
994 return ISC_R_FAILURE
;
1000 dbus_svc_DBUS_TYPE bus
,
1003 dbus_svc_WatchHandler wh
,
1004 dbus_svc_ErrorHandler eh
,
1005 dbus_svc_ErrorHandler dh
,
1009 DBusConnection
*connection
;
1011 char *session_bus_address
=0L;
1013 memset(&error
,'\0',sizeof(DBusError
));
1015 dbus_error_init(&error
);
1019 /* DBUS_PRIVATE_* bus types are the only type which allow reconnection if the dbus-daemon is restarted
1021 case DBUS_PRIVATE_SYSTEM
:
1023 if ( (connection
= dbus_connection_open_private("unix:path=/var/run/dbus/system_bus_socket", &error
)) == 0L )
1025 if(eh
)(*eh
)("dbus_svc_init failed: %s %s",error
.name
, error
.message
);
1026 return ISC_R_FAILURE
;
1029 if ( ! dbus_bus_register(connection
,&error
) )
1031 if(eh
)(*eh
)("dbus_bus_register failed: %s %s", error
.name
, error
.message
);
1032 dbus_connection_close(connection
);
1034 return ISC_R_FAILURE
;
1038 case DBUS_PRIVATE_SESSION
:
1040 session_bus_address
= getenv("DBUS_SESSION_BUS_ADDRESS");
1041 if ( session_bus_address
== 0L )
1043 if(eh
)(*eh
)("dbus_svc_init failed: DBUS_SESSION_BUS_ADDRESS environment variable not set");
1044 return ISC_R_FAILURE
;
1047 if ( (connection
= dbus_connection_open_private(session_bus_address
, &error
)) == 0L )
1049 if(eh
)(*eh
)("dbus_svc_init failed: %s %s",error
.name
, error
.message
);
1050 return ISC_R_FAILURE
;
1053 if ( ! dbus_bus_register(connection
,&error
) )
1055 if(eh
)(*eh
)("dbus_bus_register failed: %s %s", error
.name
, error
.message
);
1056 dbus_connection_close(connection
);
1058 return ISC_R_FAILURE
;
1065 if ( (connection
= dbus_bus_get (bus
, &error
)) == 0L )
1067 if(eh
)(*eh
)("dbus_svc_init failed: %s %s",error
.name
, error
.message
);
1068 return ISC_R_FAILURE
;
1073 if(eh
)(*eh
)("dbus_svc_init failed: unknown bus type %d", bus
);
1074 return ISC_R_FAILURE
;
1077 dbus_connection_set_exit_on_disconnect(connection
, FALSE
);
1079 if ( (connection_setup(connection
, dbus
, wh
, eh
, dh
, wh_arg
)) != ISC_R_SUCCESS
)
1081 if(eh
)(*eh
)("dbus_svc_init failed: connection_setup failed");
1082 return ISC_R_FAILURE
;
1086 return ISC_R_SUCCESS
;
1088 (*dbus
)->unique_name
= dbus_bus_get_unique_name(connection
);
1091 ( dbus_bus_request_name
1093 #ifdef DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT
1094 DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT
,
1102 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
:
1104 case DBUS_REQUEST_NAME_REPLY_EXISTS
:
1105 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE
:
1106 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
:
1107 if(eh
)(*eh
)("dbus_svc_init: dbus_bus_request_name failed: Name already registered");
1110 if(eh
)(*eh
)("dbus_svc_init: dbus_bus_request_name failed: %s %s", error
.name
, error
.message
);
1113 return ISC_R_SUCCESS
;
1116 dbus_connection_close( connection
);
1117 dbus_connection_unref( connection
);
1120 dbus_connection_set_dispatch_status_function (connection
, NULL
, NULL
, NULL
);
1121 dbus_connection_set_watch_functions (connection
, NULL
, NULL
, NULL
, NULL
, NULL
);
1122 dbus_connection_set_timeout_functions (connection
, NULL
, NULL
, NULL
, NULL
, NULL
);
1125 return ISC_R_FAILURE
;
1128 const char *dbus_svc_unique_name(DBusConnectionState
*cs
)
1130 return cs
->unique_name
;
1134 dbus_svc_shutdown ( DBusConnectionState
*cs
)
1136 if (!dbus_connection_set_watch_functions
1138 NULL
, NULL
, NULL
, NULL
, NULL
1140 ) if( cs
->eh
!= 0L ) (*(cs
->eh
))("connection_shutdown: dbus_connection_set_watch_functions: No Memory."
1141 "Setting watch functions to NULL failed."
1144 if (!dbus_connection_set_timeout_functions
1146 NULL
, NULL
, NULL
, NULL
, NULL
1148 ) if( cs
->eh
!= 0L ) (*(cs
->eh
))("connection_shutdown: dbus_connection_set_timeout_functions: No Memory."
1149 "Setting timeout functions to NULL failed."
1152 dbus_connection_set_dispatch_status_function (cs
->connection
, NULL
, NULL
, NULL
);
1154 tdestroy( cs
->timeouts
, free
);
1156 tdestroy( cs
->watches
, no_free
);
1159 dbus_connection_close( cs
->connection
);
1160 dbus_connection_unref( cs
->connection
);