1N/A/***************************************************************************
1N/A * CVSID: $Id$
1N/A *
1N/A * runner.c - Process running code
1N/A *
1N/A * Copyright (C) 2006 Sjoerd Simons, <sjoerd@luon.net>
1N/A *
1N/A * Licensed under the Academic Free License version 2.1
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1N/A *
1N/A **************************************************************************/
1N/A#include <stdio.h>
1N/A#include <unistd.h>
1N/A#include <stdlib.h>
1N/A#include <sys/types.h>
1N/A#include <sys/stat.h>
1N/A#include <sys/wait.h>
1N/A#include <signal.h>
1N/A#include <string.h>
1N/A
1N/A#define DBUS_API_SUBJECT_TO_CHANGE
1N/A#include <dbus/dbus-glib-lowlevel.h>
1N/A
1N/A#include <glib.h>
1N/A#include "utils.h"
1N/A#include "runner.h"
1N/A
1N/A/* Successful run of the program */
1N/A#define HALD_RUN_SUCCESS 0x0
1N/A/* Process was killed because of running too long */
1N/A#define HALD_RUN_TIMEOUT 0x1
1N/A/* Failed to start for some reason */
1N/A#define HALD_RUN_FAILED 0x2
1N/A/* Killed on purpose, e.g. hal_util_kill_device_helpers */
1N/A#define HALD_RUN_KILLED 0x4
1N/A
1N/AGHashTable *udi_hash = NULL;
1N/A
1N/Atypedef struct {
1N/A run_request *r;
1N/A DBusMessage *msg;
1N/A DBusConnection *con;
1N/A GPid pid;
1N/A gint stderr_v;
1N/A guint watch;
1N/A guint timeout;
1N/A gboolean sent_kill;
1N/A gboolean emit_pid_exited;
1N/A} run_data;
1N/A
1N/Astatic void
1N/Adel_run_data(run_data *rd)
1N/A{
1N/A if (rd == NULL)
1N/A return;
1N/A
1N/A del_run_request(rd->r);
1N/A if (rd->msg)
1N/A dbus_message_unref(rd->msg);
1N/A
1N/A g_spawn_close_pid(rd->pid);
1N/A
1N/A if (rd->stderr_v >= 0)
1N/A close(rd->stderr_v);
1N/A
1N/A if (rd->timeout != 0)
1N/A g_source_remove(rd->timeout);
1N/A
1N/A g_free(rd);
1N/A}
1N/A
1N/Arun_request *
1N/Anew_run_request(void)
1N/A{
1N/A run_request *result;
1N/A result = g_new0(run_request, 1);
1N/A g_assert(result != NULL);
1N/A return result;
1N/A}
1N/A
1N/Avoid
1N/Adel_run_request(run_request *r)
1N/A{
1N/A if (r == NULL)
1N/A return;
1N/A g_free(r->udi);
1N/A free_string_array(r->environment);
1N/A free_string_array(r->argv);
1N/A g_free(r->input);
1N/A g_free(r);
1N/A}
1N/A
1N/Astatic void
1N/Asend_reply(DBusConnection *con, DBusMessage *msg, guint32 exit_type, gint32 return_code, gchar **error)
1N/A{
1N/A DBusMessage *reply;
1N/A DBusMessageIter iter;
1N/A int i;
1N/A
1N/A if (con == NULL || msg == NULL)
1N/A return;
1N/A
1N/A reply = dbus_message_new_method_return(msg);
1N/A g_assert(reply != NULL);
1N/A
1N/A dbus_message_iter_init_append(reply, &iter);
1N/A dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &exit_type);
1N/A dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &return_code);
1N/A if (error != NULL) for (i = 0; error[i] != NULL; i++) {
1N/A dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &error[i]);
1N/A }
1N/A
1N/A dbus_connection_send(con, reply, NULL);
1N/A dbus_message_unref(reply);
1N/A}
1N/A
1N/Astatic void
1N/Aremove_from_hash_table(run_data *rd)
1N/A{
1N/A GList *list;
1N/A
1N/A /* Remove to the hashtable */
1N/A list = (GList *)g_hash_table_lookup(udi_hash, rd->r->udi);
1N/A list = g_list_remove(list, rd);
1N/A /* The hash table will take care to not leak the dupped string */
1N/A g_hash_table_insert(udi_hash, g_strdup(rd->r->udi), list);
1N/A}
1N/A
1N/Astatic void
1N/Arun_exited(GPid pid, gint status, gpointer data)
1N/A{
1N/A run_data *rd = (run_data *)data;
1N/A char **error = NULL;
1N/A
1N/A printf("%s exited\n", rd->r->argv[0]);
1N/A rd->watch = 0;
1N/A if (rd->sent_kill == TRUE) {
1N/A /* We send it a kill, so ignore */
1N/A del_run_data(rd);
1N/A return;
1N/A }
1N/A /* Check if it was a normal exit */
1N/A if (!WIFEXITED(status)) {
1N/A /* No not normal termination ? crash ? */
1N/A send_reply(rd->con, rd->msg, HALD_RUN_FAILED, 0, NULL);
1N/A goto out;
1N/A }
1N/A /* normal exit */
1N/A if (rd->stderr_v >= 0) {
1N/A /* Need to read stderr */
1N/A error = get_string_array_from_fd(rd->stderr_v);
1N/A close(rd->stderr_v);
1N/A rd->stderr_v = -1;
1N/A }
1N/A if (rd->msg != NULL)
1N/A send_reply(rd->con, rd->msg, HALD_RUN_SUCCESS, WEXITSTATUS(status), error);
1N/A free_string_array(error);
1N/A
1N/Aout:
1N/A remove_from_hash_table(rd);
1N/A
1N/A /* emit a signal that this PID exited */
1N/A if(rd->con != NULL && rd->emit_pid_exited) {
1N/A DBusMessage *signal;
1N/A dbus_int64_t pid64;
1N/A signal = dbus_message_new_signal ("/org/freedesktop/HalRunner",
1N/A "org.freedesktop.HalRunner",
1N/A "StartedProcessExited");
1N/A pid64 = rd->pid;
1N/A dbus_message_append_args (signal,
1N/A DBUS_TYPE_INT64, &pid64,
1N/A DBUS_TYPE_INVALID);
1N/A dbus_connection_send(rd->con, signal, NULL);
1N/A }
1N/A
1N/A del_run_data(rd);
1N/A}
1N/A
1N/Astatic gboolean
1N/Arun_timedout(gpointer data) {
1N/A run_data *rd = (run_data *)data;
1N/A /* Time is up, kill the process, send reply that it was killed!
1N/A * Don't wait for exit, because it could hang in state D
1N/A */
1N/A kill(rd->pid, SIGTERM);
1N/A /* Ensure the timeout is not removed in the delete */
1N/A rd->timeout = 0;
1N/A /* So the exit watch will know it's killed in case it runs*/
1N/A rd->sent_kill = TRUE;
1N/A
1N/A send_reply(rd->con, rd->msg, HALD_RUN_TIMEOUT, 0, NULL);
1N/A remove_from_hash_table(rd);
1N/A return FALSE;
1N/A}
1N/A
1N/Astatic gboolean
1N/Afind_program(char **argv)
1N/A{
1N/A /* Search for the program in the dirs where it's allowed to be */
1N/A char *program;
1N/A char *path = NULL;
1N/A
1N/A if (argv[0] == NULL)
1N/A return FALSE;
1N/A
1N/A program = g_path_get_basename(argv[0]);
1N/A
1N/A /* first search $PATH to make e.g. run-hald.sh work */
1N/A path = g_find_program_in_path (program);
1N/A g_free(program);
1N/A if (path == NULL)
1N/A return FALSE;
1N/A else {
1N/A /* Replace program in argv[0] with the full path */
1N/A g_free(argv[0]);
1N/A argv[0] = path;
1N/A }
1N/A return TRUE;
1N/A}
1N/A
1N/A/* Run the given request and reply it's result on msg */
1N/Agboolean
1N/Arun_request_run (run_request *r, DBusConnection *con, DBusMessage *msg, GPid *out_pid)
1N/A{
1N/A GPid pid;
1N/A GError *error = NULL;
1N/A gint *stdin_p = NULL;
1N/A gint *stderr_p = NULL;
1N/A gint stdin_v;
1N/A gint stderr_v = -1;
1N/A run_data *rd = NULL;
1N/A gboolean program_exists = FALSE;
1N/A char *program_dir = NULL;
1N/A GList *list;
1N/A
1N/A printf("Run started %s (%d) (%d) \n!", r->argv[0], r->timeout,
1N/A r->error_on_stderr);
1N/A if (r->input != NULL) {
1N/A stdin_p = &stdin_v;
1N/A }
1N/A if (r->error_on_stderr) {
1N/A stderr_p = &stderr_v;
1N/A }
1N/A
1N/A program_exists = find_program(r->argv);
1N/A
1N/A if (program_exists) {
1N/A program_dir = g_path_get_dirname (r->argv[0]);
1N/A printf(" full path is '%s', program_dir is '%s'\n", r->argv[0], program_dir);
1N/A }
1N/A
1N/A if (!program_exists ||
1N/A !g_spawn_async_with_pipes(program_dir, r->argv, r->environment,
1N/A G_SPAWN_DO_NOT_REAP_CHILD,
1N/A NULL, NULL, &pid,
1N/A stdin_p, NULL, stderr_p, &error)) {
1N/A g_free (program_dir);
1N/A del_run_request(r);
1N/A if (con && msg)
1N/A send_reply(con, msg, HALD_RUN_FAILED, 0, NULL);
1N/A return FALSE;
1N/A }
1N/A g_free (program_dir);
1N/A
1N/A if (r->input) {
1N/A if (write(stdin_v, r->input, strlen(r->input)) != (ssize_t) strlen(r->input))
1N/A printf("Warning: Error while wite r->input (%s) to stdin_v.\n", r->input);
1N/A close(stdin_v);
1N/A }
1N/A
1N/A rd = g_new0(run_data,1);
1N/A g_assert(rd != NULL);
1N/A rd->r = r;
1N/A rd->msg = msg;
1N/A if (msg != NULL)
1N/A dbus_message_ref(msg);
1N/A
1N/A rd->con = con;
1N/A rd->pid = pid;
1N/A rd->stderr_v = stderr_v;
1N/A rd->sent_kill = FALSE;
1N/A
1N/A /* Add watch for exit of the program */
1N/A rd->watch = g_child_watch_add(pid, run_exited, rd);
1N/A
1N/A /* Add timeout if needed */
1N/A if (r->timeout > 0)
1N/A rd->timeout = g_timeout_add(r->timeout, run_timedout, rd);
1N/A else
1N/A rd->timeout = 0;
1N/A
1N/A /* Add to the hashtable */
1N/A list = (GList *)g_hash_table_lookup(udi_hash, r->udi);
1N/A list = g_list_prepend(list, rd);
1N/A
1N/A /* The hash table will take care to not leak the dupped string */
1N/A g_hash_table_insert(udi_hash, g_strdup(r->udi), list);
1N/A
1N/A /* send back PID if requested.. and only emit StartedProcessExited in this case */
1N/A if (out_pid != NULL) {
1N/A *out_pid = pid;
1N/A rd->emit_pid_exited = TRUE;
1N/A }
1N/A return TRUE;
1N/A}
1N/A
1N/Astatic void
1N/Akill_rd(gpointer data, gpointer user_data)
1N/A{
1N/A run_data *rd = (run_data *)data;
1N/A
1N/A kill(rd->pid, SIGTERM);
1N/A printf("Sent kill to %d\n", rd->pid);
1N/A if (rd->timeout != 0) {
1N/A /* Remove the timeout watch */
1N/A g_source_remove(rd->timeout);
1N/A rd->timeout = 0;
1N/A }
1N/A
1N/A /* So the exit watch will know it's killed in case it runs */
1N/A rd->sent_kill = TRUE;
1N/A
1N/A if (rd->msg != NULL)
1N/A send_reply(rd->con, rd->msg, HALD_RUN_KILLED, 0, NULL);
1N/A}
1N/A
1N/Astatic void
1N/Ado_kill_udi(gchar *udi)
1N/A{
1N/A GList *list;
1N/A list = (GList *)g_hash_table_lookup(udi_hash, udi);
1N/A g_list_foreach(list, kill_rd, NULL);
1N/A g_list_free(list);
1N/A}
1N/A
1N/A/* Kill all running request for a udi */
1N/Avoid
1N/Arun_kill_udi(gchar *udi)
1N/A{
1N/A do_kill_udi(udi);
1N/A g_hash_table_remove(udi_hash, udi);
1N/A}
1N/A
1N/Astatic gboolean
1N/Ahash_kill_udi(gpointer key, gpointer value, gpointer user_data) {
1N/A do_kill_udi(key);
1N/A return TRUE;
1N/A}
1N/A
1N/A/* Kill all running request*/
1N/Avoid
1N/Arun_kill_all()
1N/A{
1N/A g_hash_table_foreach_remove(udi_hash, hash_kill_udi, NULL);
1N/A}
1N/A
1N/Avoid
1N/Arun_init()
1N/A{
1N/A udi_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1N/A}