runner.c revision 7544909da5f7d5b467625910225a72e142c4b6b7
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/***************************************************************************
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * CVSID: $Id$
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * runner.c - Process running code
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * Copyright (C) 2006 Sjoerd Simons, <sjoerd@luon.net>
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * Licensed under the Academic Free License version 2.1
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * This program is free software; you can redistribute it and/or modify
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * it under the terms of the GNU General Public License as published by
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * the Free Software Foundation; either version 2 of the License, or
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * (at your option) any later version.
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * This program is distributed in the hope that it will be useful,
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * but WITHOUT ANY WARRANTY; without even the implied warranty of
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * GNU General Public License for more details.
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * You should have received a copy of the GNU General Public License
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * along with this program; if not, write to the Free Software
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18c2aff776a775d34a4c9893a4c72e0434d68e36artem **************************************************************************/
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/* Successful run of the program */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/* Process was killed because of running too long */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/* Failed to start for some reason */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/* Killed on purpose, e.g. hal_util_kill_device_helpers */
18c2aff776a775d34a4c9893a4c72e0434d68e36artemtypedef struct {
18c2aff776a775d34a4c9893a4c72e0434d68e36artemsend_reply(DBusConnection *con, DBusMessage *msg, guint32 exit_type, gint32 return_code, gchar **error)
18c2aff776a775d34a4c9893a4c72e0434d68e36artem dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &exit_type);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &return_code);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem if (error != NULL) for (i = 0; error[i] != NULL; i++) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &error[i]);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Remove to the hashtable */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem list = (GList *)g_hash_table_lookup(udi_hash, rd->r->udi);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* The hash table will take care to not leak the dupped string */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem g_hash_table_insert(udi_hash, g_strdup(rd->r->udi), list);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* We send it a kill, so ignore */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Check if it was a normal exit */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* No not normal termination ? crash ? */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem send_reply(rd->con, rd->msg, HALD_RUN_FAILED, 0, NULL);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* normal exit */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Need to read stderr */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem send_reply(rd->con, rd->msg, HALD_RUN_SUCCESS, WEXITSTATUS(status), error);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* emit a signal that this PID exited */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem signal = dbus_message_new_signal ("/org/freedesktop/HalRunner",
18c2aff776a775d34a4c9893a4c72e0434d68e36artem "org.freedesktop.HalRunner",
18c2aff776a775d34a4c9893a4c72e0434d68e36artem "StartedProcessExited");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Time is up, kill the process, send reply that it was killed!
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * Don't wait for exit, because it could hang in state D
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Ensure the timeout is not removed in the delete */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* So the exit watch will know it's killed in case it runs*/
18c2aff776a775d34a4c9893a4c72e0434d68e36artem send_reply(rd->con, rd->msg, HALD_RUN_TIMEOUT, 0, NULL);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Search for the program in the dirs where it's allowed to be */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* first search $PATH to make e.g. run-hald.sh work */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Replace program in argv[0] with the full path */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/* Run the given request and reply it's result on msg */
18c2aff776a775d34a4c9893a4c72e0434d68e36artemrun_request_run (run_request *r, DBusConnection *con, DBusMessage *msg, GPid *out_pid)
18c2aff776a775d34a4c9893a4c72e0434d68e36artem printf("Run started %s (%d) (%d) \n!", r->argv[0], r->timeout,
7544909da5f7d5b467625910225a72e142c4b6b7artem printf(" full path is '%s', program_dir is '%s'\n", r->argv[0], program_dir);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem !g_spawn_async_with_pipes(program_dir, r->argv, r->environment,
18c2aff776a775d34a4c9893a4c72e0434d68e36artem if (write(stdin_v, r->input, strlen(r->input)) != (ssize_t) strlen(r->input));
18c2aff776a775d34a4c9893a4c72e0434d68e36artem printf("Warning: Error while wite r->input (%s) to stdin_v.\n", r->input);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Add watch for exit of the program */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Add timeout if needed */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem if (r->timeout > 0)
18c2aff776a775d34a4c9893a4c72e0434d68e36artem rd->timeout = g_timeout_add(r->timeout, run_timedout, rd);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Add to the hashtable */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem list = (GList *)g_hash_table_lookup(udi_hash, r->udi);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* The hash table will take care to not leak the dupped string */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem g_hash_table_insert(udi_hash, g_strdup(r->udi), list);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* send back PID if requested.. and only emit StartedProcessExited in this case */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* Remove the timeout watch */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* So the exit watch will know it's killed in case it runs */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem send_reply(rd->con, rd->msg, HALD_RUN_KILLED, 0, NULL);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/* Kill all running request for a udi */
18c2aff776a775d34a4c9893a4c72e0434d68e36artemhash_kill_udi(gpointer key, gpointer value, gpointer user_data) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/* Kill all running request*/
18c2aff776a775d34a4c9893a4c72e0434d68e36artem g_hash_table_foreach_remove(udi_hash, hash_kill_udi, NULL);