1 /***************************************************************************
4 * main.c - Main dbus interface of the hald runner
6 * Copyright (C) 2006 Sjoerd Simons, <sjoerd@luon.net>
7 * Copyright (C) 2007 Codethink Ltd. Author Rob Taylor <rob.taylor@codethink.co.uk>
9 * Licensed under the Academic Free License version 2.1
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 **************************************************************************/
28 #define DBUS_API_SUBJECT_TO_CHANGE
29 #include <dbus/dbus-glib-lowlevel.h>
36 #define __attribute__(x)
40 parse_udi (run_request
*r
, DBusMessage
*msg
, DBusMessageIter
*iter
)
44 /* Should be the device UDI */
45 if (dbus_message_iter_get_arg_type(iter
) != DBUS_TYPE_STRING
)
47 dbus_message_iter_get_basic(iter
, &tmpstr
);
48 r
->udi
= g_strdup(tmpstr
);
50 if (!dbus_message_iter_next(iter
))
60 parse_environment(run_request
*r
, DBusMessage
*msg
, DBusMessageIter
*iter
)
62 DBusMessageIter sub_iter
;
65 /* The environment array */
66 if (dbus_message_iter_get_arg_type(iter
) != DBUS_TYPE_ARRAY
)
68 dbus_message_iter_recurse(iter
, &sub_iter
);
69 /* Add default path for the programs we start */
70 #if defined(__FreeBSD__)
71 tmpstr
= g_strdup_printf("PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/X11R6/sbin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:%s", getenv("PATH"));
73 tmpstr
= g_strdup_printf("PATH=/sbin:/usr/sbin:/bin:/usr/bin:%s", getenv("PATH"));
75 r
->environment
= get_string_array(&sub_iter
, tmpstr
);
78 if (!dbus_message_iter_next(iter
) || dbus_message_iter_get_arg_type(iter
) != DBUS_TYPE_ARRAY
)
80 dbus_message_iter_recurse(iter
, &sub_iter
);
81 r
->argv
= get_string_array(&sub_iter
, NULL
);
90 handle_run(DBusConnection
*con
, DBusMessage
*msg
)
97 r
= new_run_request();
98 g_assert(dbus_message_iter_init(msg
, &iter
));
100 if (!parse_udi(r
, msg
, &iter
))
103 if (!parse_environment(r
, msg
, &iter
))
106 /* Next a string of what should be written to stdin */
107 if (!dbus_message_iter_next(&iter
) || dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
)
109 dbus_message_iter_get_basic(&iter
, &tmpstr
);
110 r
->input
= g_strdup(tmpstr
);
112 /* Then an bool to indicate if we should grab stderr */
113 if (!dbus_message_iter_next(&iter
) || dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_BOOLEAN
)
115 dbus_message_iter_get_basic(&iter
, &(r
->error_on_stderr
));
117 /* Then an uint32 timeout for it */
118 if (!dbus_message_iter_next(&iter
) || dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_UINT32
)
120 dbus_message_iter_get_basic(&iter
, &(r
->timeout
));
122 /* let run_request_run handle the reply */
123 run_request_run(r
, con
, msg
, NULL
);
128 reply
= dbus_message_new_error(msg
, "org.freedesktop.HalRunner.Malformed",
129 "Malformed run request");
130 dbus_connection_send(con
, reply
, NULL
);
131 dbus_message_unref(reply
);
135 handle_start(DBusConnection
*con
, DBusMessage
*msg
, gboolean is_singleton
)
138 DBusMessageIter iter
;
142 r
= new_run_request();
143 r
->is_singleton
= is_singleton
;
145 g_assert(dbus_message_iter_init(msg
, &iter
));
147 if (!dbus_message_iter_init(msg
, &iter
))
150 if (!is_singleton
&& !parse_udi(r
, msg
, &iter
)) {
151 fprintf(stderr
, "error parsing udi");
155 if (!parse_environment(r
, msg
, &iter
)) {
156 fprintf(stderr
, "error parsing environment");
160 if (run_request_run(r
, con
, NULL
, &pid
)) {
162 reply
= dbus_message_new_method_return(msg
);
163 dbus_message_append_args (reply
,
164 DBUS_TYPE_INT64
, &ppid
,
168 reply
= dbus_message_new_error(msg
, "org.freedesktop.HalRunner.Failed",
169 "Start request failed");
171 dbus_connection_send(con
, reply
, NULL
);
172 dbus_message_unref(reply
);
176 reply
= dbus_message_new_error(msg
, "org.freedesktop.HalRunner.Malformed",
177 "Malformed start request");
178 dbus_connection_send(con
, reply
, NULL
);
179 dbus_message_unref(reply
);
183 handle_kill(DBusConnection
*con
, DBusMessage
*msg
)
186 DBusMessage
*reply
= NULL
;
189 dbus_error_init (&error
);
190 if (!dbus_message_get_args(msg
, &error
,
191 DBUS_TYPE_STRING
, &udi
,
192 DBUS_TYPE_INVALID
)) {
193 reply
= dbus_message_new_error (msg
, "org.freedesktop.HalRunner.Malformed",
194 "Malformed kill message");
196 dbus_connection_send (con
, reply
, NULL
);
197 dbus_message_unref(reply
);
202 /* always successfull */
203 reply
= dbus_message_new_method_return(msg
);
204 dbus_connection_send(con
, reply
, NULL
);
205 dbus_message_unref(reply
);
208 static DBusHandlerResult
209 filter(DBusConnection
*con
, DBusMessage
*msg
, void *user_data
)
213 if (dbus_message_is_method_call(msg
, "org.freedesktop.HalRunner", "Run")) {
214 handle_run(con
, msg
);
215 return DBUS_HANDLER_RESULT_HANDLED
;
216 } else if (dbus_message_is_method_call(msg
, "org.freedesktop.HalRunner", "Start")) {
217 handle_start(con
, msg
, FALSE
);
218 return DBUS_HANDLER_RESULT_HANDLED
;
219 } else if (dbus_message_is_method_call(msg
, "org.freedesktop.HalRunner", "StartSingleton")) {
220 handle_start(con
, msg
, TRUE
);
221 return DBUS_HANDLER_RESULT_HANDLED
;
222 } else if (dbus_message_is_method_call(msg
, "org.freedesktop.HalRunner", "Kill")) {
223 handle_kill(con
, msg
);
224 return DBUS_HANDLER_RESULT_HANDLED
;
225 } else if (dbus_message_is_method_call(msg
, "org.freedesktop.HalRunner", "Shutdown")) {
228 return DBUS_HANDLER_RESULT_HANDLED
;
229 } else if (dbus_message_is_method_call(msg
, "org.freedesktop.HalRunner", "KillAll")) {
231 /* alwasy successfull */
232 reply
= dbus_message_new_method_return(msg
);
233 dbus_connection_send(con
, reply
, NULL
);
234 dbus_message_unref(reply
);
235 return DBUS_HANDLER_RESULT_HANDLED
;
237 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
241 main(int argc
, char **argv
)
249 dbus_error_init(&error
);
250 dbus_address
= getenv("HALD_RUNNER_DBUS_ADDRESS");
251 g_assert(dbus_address
!= NULL
);
253 fprintf(stderr
, "Runner started - allowed paths are '%s'\n", getenv("PATH"));
255 c
= dbus_connection_open(dbus_address
, &error
);
259 loop
= g_main_loop_new(NULL
, FALSE
);
261 dbus_connection_setup_with_g_main(c
, NULL
);
262 dbus_connection_set_exit_on_disconnect(c
, TRUE
);
263 dbus_connection_add_filter(c
, filter
, NULL
, NULL
);
265 g_main_loop_run(loop
);
268 fprintf(stderr
,"An error has occured: %s\n", error
.message
);