2 #define DBUS_API_SUBJECT_TO_CHANGE
9 #include <dbus/dbus-glib.h>
10 #include <dbus/dbus-glib-lowlevel.h>
13 #define RETURN_ERROR 10
14 #define RETURN_FAIL 20
16 static const char* exec_cmd(char* const argv
[]) {
35 if (dup2(fd
[1], STDOUT_FILENO
) < 0 ||
36 dup2(fd
[1], STDERR_FILENO
) < 0) {
44 if (execvp(argv
[0], argv
) < 0) {
55 int rd
= read(fd
[0], res
, 65535);
72 static DBusHandlerResult
filter_disconnect(DBusConnection
* connection
,
75 GMainLoop
* loop
= (GMainLoop
*) user_data
;
77 if (!dbus_message_is_signal(message
, DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL
,
79 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
82 dbus_connection_disconnect(connection
);
83 g_main_loop_quit(loop
);
85 return DBUS_HANDLER_RESULT_HANDLED
;
88 static void path_unregistered(DBusConnection
* connection
,
90 printf("path_unregistered\n");
91 // Connection was finalized
94 static DBusHandlerResult
path_message(DBusConnection
* connection
,
97 if (dbus_message_is_method_call(message
,
100 DBusMessageIter iter
;
102 const char* res
= NULL
;
106 dbus_message_iter_init(message
, &iter
);
108 while (dbus_message_iter_get_arg_type(&iter
) == DBUS_TYPE_STRING
) {
111 if (!dbus_message_iter_next(&iter
)) {
116 argv
= malloc(sizeof (char*) * (argc
+ 1));
122 dbus_message_iter_init(message
, &iter
);
124 for (i
= 1; i
< argc
; ++i
) {
125 argv
[i
] = dbus_message_iter_get_string(&iter
);
126 dbus_message_iter_next(&iter
);
131 res
= exec_cmd(argv
);
134 fprintf(stderr
, "Unable to create dcop argument vector\n");
138 reply
= dbus_message_new_method_return(message
);
141 if (dbus_message_append_args(reply
,
142 DBUS_TYPE_STRING
, res
,
143 DBUS_TYPE_INVALID
)) {
144 if (dbus_connection_send(connection
, reply
, NULL
)) {
148 fprintf(stderr
, "Unable to send reply\n");
152 fprintf(stderr
, "Unable to build reply\n");
155 dbus_message_unref (reply
);
158 fprintf(stderr
, "Unable to create reply\n");
163 return DBUS_HANDLER_RESULT_HANDLED
;
166 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
171 static DBusObjectPathVTable dcop_vtable
= {
178 int main(int argc
, char **argv
)
181 DBusConnection
*connection
;
186 fprintf(stderr
, "%s: No arguments allowed.\n", argv
[0]);
190 dbus_error_init(&error
);
192 connection
= dbus_bus_get(DBUS_BUS_ACTIVATION
, &error
);
194 if (connection
!= NULL
) {
195 loop
= g_main_loop_new(NULL
, FALSE
);
198 dbus_connection_setup_with_g_main(connection
, NULL
);
200 if (dbus_connection_add_filter(connection
, filter_disconnect
, loop
, NULL
)) {
201 if (dbus_connection_register_object_path(connection
,
202 "/org/aros/dbus/dbus_dcop",
205 int result
= dbus_bus_acquire_service(connection
, "org.aros.dbus.DCOP",
208 if (!dbus_error_is_set(&error
)) {
209 g_main_loop_run(loop
);
212 fprintf(stderr
, "ailed to acquire service: %s\n", error
.message
);
217 fprintf(stderr
, "Failed to register object path\n");
221 dbus_connection_remove_filter(connection
, filter_disconnect
, NULL
);
224 fprintf(stderr
, "Failed to create connection filter\n");
230 fprintf(stderr
, "Failed to create mail loop\n");
234 dbus_connection_unref(connection
);
237 fprintf(stderr
, "Failed to open connection to activating message bus: %s\n", error
.message
);
241 dbus_error_free(&error
);