Check for SYS/GL during library init. Reason is that
[AROS.git] / rom / dbus / test / dbus-dcop-bridge.c
blob4d51782f4d553b11833bdd0ed5f236c8c65aa241
2 #define DBUS_API_SUBJECT_TO_CHANGE
3 #include <dbus/dbus.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
8 #include <glib.h>
9 #include <dbus/dbus-glib.h>
10 #include <dbus/dbus-glib-lowlevel.h>
12 #define RETURN_OK 0
13 #define RETURN_ERROR 10
14 #define RETURN_FAIL 20
16 static const char* exec_cmd(char* const argv[]) {
17 int s;
18 int fd[2];
19 char* res = NULL;
21 if (pipe(fd) < 0) {
22 perror("pipe");
23 return NULL;
26 s = fork();
28 if (s < 0) {
29 perror("fork");
30 close(fd[0]);
31 close(fd[1]);
32 return NULL;
34 else if (s == 0) {
35 if (dup2(fd[1], STDOUT_FILENO) < 0 ||
36 dup2(fd[1], STDERR_FILENO) < 0) {
37 perror("dup2");
38 exit(RETURN_ERROR);
41 close(fd[0]);
42 close(fd[1]);
44 if (execvp(argv[0], argv) < 0) {
45 perror("execvp");
46 exit(RETURN_FAIL);
49 else {
50 close(fd[1]);
52 res = malloc(65536);
54 if (res != NULL) {
55 int rd = read(fd[0], res, 65535);
57 if (rd >= 0) {
58 res[rd] = 0;
61 else {
62 perror("malloc");
65 close(fd[0]);
68 return res;
72 static DBusHandlerResult filter_disconnect(DBusConnection* connection,
73 DBusMessage* message,
74 void* user_data) {
75 GMainLoop* loop = (GMainLoop*) user_data;
77 if (!dbus_message_is_signal(message, DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
78 "Disconnected")) {
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,
89 void* user_data) {
90 printf("path_unregistered\n");
91 // Connection was finalized
94 static DBusHandlerResult path_message(DBusConnection* connection,
95 DBusMessage* message,
96 void* user_data) {
97 if (dbus_message_is_method_call(message,
98 "org.aros.dbus.DCOP",
99 "Exec")) {
100 DBusMessageIter iter;
101 DBusMessage* reply;
102 const char* res = NULL;
103 int argc = 1;
104 char** argv;
106 dbus_message_iter_init(message, &iter);
108 while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) {
109 ++argc;
111 if (!dbus_message_iter_next(&iter)) {
112 break;
116 argv = malloc(sizeof (char*) * (argc + 1));
118 if (argv != NULL) {
119 int i;
120 argv[0] = "dcop";
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);
129 argv[i] = NULL;
131 res = exec_cmd(argv);
133 else {
134 fprintf(stderr, "Unable to create dcop argument vector\n");
138 reply = dbus_message_new_method_return(message);
140 if (reply != NULL) {
141 if (dbus_message_append_args(reply,
142 DBUS_TYPE_STRING, res,
143 DBUS_TYPE_INVALID)) {
144 if (dbus_connection_send(connection, reply, NULL)) {
145 // Alles gut
147 else {
148 fprintf(stderr, "Unable to send reply\n");
151 else {
152 fprintf(stderr, "Unable to build reply\n");
155 dbus_message_unref (reply);
157 else {
158 fprintf(stderr, "Unable to create reply\n");
161 free(res);
163 return DBUS_HANDLER_RESULT_HANDLED;
165 else {
166 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
171 static DBusObjectPathVTable dcop_vtable = {
172 path_unregistered,
173 path_message,
174 NULL,
175 0, 0, 0
178 int main(int argc, char **argv)
180 GMainLoop *loop;
181 DBusConnection *connection;
182 DBusError error;
183 int rc = RETURN_OK;
185 if (argc != 1) {
186 fprintf(stderr, "%s: No arguments allowed.\n", argv[0]);
187 return RETURN_FAIL;
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);
197 if (loop != NULL) {
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",
203 &dcop_vtable,
204 NULL)) {
205 int result = dbus_bus_acquire_service(connection, "org.aros.dbus.DCOP",
206 0, &error);
208 if (!dbus_error_is_set(&error)) {
209 g_main_loop_run(loop);
211 else {
212 fprintf(stderr, "ailed to acquire service: %s\n", error.message);
213 rc = RETURN_FAIL;
216 else {
217 fprintf(stderr, "Failed to register object path\n");
218 rc = RETURN_FAIL;
221 dbus_connection_remove_filter(connection, filter_disconnect, NULL);
223 else {
224 fprintf(stderr, "Failed to create connection filter\n");
225 rc = RETURN_FAIL;
229 else {
230 fprintf(stderr, "Failed to create mail loop\n");
231 rc = RETURN_FAIL;
234 dbus_connection_unref(connection);
236 else {
237 fprintf(stderr, "Failed to open connection to activating message bus: %s\n", error.message);
238 rc = RETURN_FAIL;
241 dbus_error_free(&error);
242 dbus_shutdown();
243 return rc;