hal/hald-runner main.c, 1.2, 1.3 runner.c, 1.3, 1.4 runner.h, 1.1, 1.2 utils.c, 1.1, 1.2

Kay Sievers kay at freedesktop.org
Mon Jan 23 06:54:50 PST 2006


Update of /cvs/hal/hal/hald-runner
In directory gabe:/tmp/cvs-serv21853/hald-runner

Modified Files:
	main.c runner.c runner.h utils.c 
Log Message:
2006-01-23  Kay Sievers  <kay.sievers at vrfy.org>

        Adopt hald-runner to usual HAL coding style.

        * hald-runner/main.c: (parse_first_part), (handle_run),
        (handle_start), (handle_kill), (filter), (main):
        * hald-runner/runner.c: (del_run_data), (new_run_request),
        (del_run_request), (send_reply), (remove_from_hash_table),
        (run_exited), (run_timedout), (find_program), (run_request_run),
        (kill_rd), (do_kill_udi), (run_kill_udi), (hash_kill_udi),
        (run_kill_all), (run_init):
        * hald-runner/runner.h:
        * hald-runner/utils.c: (get_string_array),
        (get_string_array_from_fd), (free_string_array):


Index: main.c
===================================================================
RCS file: /cvs/hal/hal/hald-runner/main.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- main.c	21 Jan 2006 16:54:04 -0000	1.2
+++ main.c	23 Jan 2006 14:54:48 -0000	1.3
@@ -32,191 +32,186 @@
 #include "runner.h"
 
 static gboolean
-parse_first_part(run_request *r, DBusMessage *msg, DBusMessageIter *iter) {
-  DBusMessageIter sub_iter;
-  char *tmpstr;
+parse_first_part(run_request *r, DBusMessage *msg, DBusMessageIter *iter)
+{
+	DBusMessageIter sub_iter;
+	char *tmpstr;
 
-  /* First should be the device UDI */
-  if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) 
-    goto malformed;
-  dbus_message_iter_get_basic(iter, &tmpstr);
-  r->udi = g_strdup(tmpstr);
+	/* First should be the device UDI */
+	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) 
+		goto malformed;
+	dbus_message_iter_get_basic(iter, &tmpstr);
+	r->udi = g_strdup(tmpstr);
 
-  /* Then the environment array */
-  if (!dbus_message_iter_next(iter) ||
-      dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) 
-    goto malformed;
-  dbus_message_iter_recurse(iter, &sub_iter);
-  r->environment = get_string_array(&sub_iter);
+	/* Then the environment array */
+	if (!dbus_message_iter_next(iter) || dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
+		goto malformed;
+	dbus_message_iter_recurse(iter, &sub_iter);
+	r->environment = get_string_array(&sub_iter);
 
-  /* Then argv */
-  if (!dbus_message_iter_next(iter) ||
-      dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) 
-    goto malformed;
-  dbus_message_iter_recurse(iter, &sub_iter);
-  r->argv = get_string_array(&sub_iter);
+	/* Then argv */
+	if (!dbus_message_iter_next(iter) || dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) 
+		goto malformed;
+	dbus_message_iter_recurse(iter, &sub_iter);
+	r->argv = get_string_array(&sub_iter);
 
-  return TRUE;
+	return TRUE;
 
 malformed:
-  return FALSE;
+	return FALSE;
 }
 
 static void
-handle_run(DBusConnection *con, DBusMessage *msg) {
-  DBusMessage *reply;
-  DBusMessageIter iter;
-  run_request *r;
-  char *tmpstr;
+handle_run(DBusConnection *con, DBusMessage *msg)
+{
+	DBusMessage *reply;
+	DBusMessageIter iter;
+	run_request *r;
+	char *tmpstr;
 
-  r = new_run_request();
-  g_assert(dbus_message_iter_init(msg, &iter));
+	r = new_run_request();
+	g_assert(dbus_message_iter_init(msg, &iter));
 
-  if (!parse_first_part(r, msg, &iter)) 
-    goto malformed;
+	if (!parse_first_part(r, msg, &iter)) 
+		goto malformed;
 
-  /* Next a string of what should be written to stdin */
-  if (!dbus_message_iter_next(&iter) ||
-      dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) 
-    goto malformed;
-  dbus_message_iter_get_basic(&iter, &tmpstr);
-  r->input = g_strdup(tmpstr);
+	/* Next a string of what should be written to stdin */
+	if (!dbus_message_iter_next(&iter) || dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
+		goto malformed;
+	dbus_message_iter_get_basic(&iter, &tmpstr);
+	r->input = g_strdup(tmpstr);
 
-  /* Then an bool to indicate if we should grab stderr */
-  if (!dbus_message_iter_next(&iter) ||
-      dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) 
-    goto malformed;
-  dbus_message_iter_get_basic(&iter, &(r->error_on_stderr));
+	/* Then an bool to indicate if we should grab stderr */
+	if (!dbus_message_iter_next(&iter) || dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) 
+		goto malformed;
+	dbus_message_iter_get_basic(&iter, &(r->error_on_stderr));
 
-  /* Then an uint32 timeout for it */
-  if (!dbus_message_iter_next(&iter) ||
-      dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) 
-    goto malformed;
-  dbus_message_iter_get_basic(&iter, &(r->timeout));
+	/* Then an uint32 timeout for it */
+	if (!dbus_message_iter_next(&iter) || dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) 
+		goto malformed;
+	dbus_message_iter_get_basic(&iter, &(r->timeout));
+
+	/* let run_request_run handle the reply */
+	run_request_run(r, con, msg);
+	return;
 
-  /* let run_request_run handle the reply */
-  run_request_run(r, con, msg);
-  return ;
 malformed:
-  del_run_request(r);
-  reply = dbus_message_new_error(msg, "org.freedesktop.HalRunner.Malformed",
-                                 "Malformed run request");
-  dbus_connection_send(con, reply, NULL);
-  dbus_message_unref(reply);
+	del_run_request(r);
+	reply = dbus_message_new_error(msg, "org.freedesktop.HalRunner.Malformed",
+				       "Malformed run request");
+	dbus_connection_send(con, reply, NULL);
+	dbus_message_unref(reply);
 }
 
 static void
-handle_start(DBusConnection *con, DBusMessage *msg) {
-  DBusMessage *reply;
-  DBusMessageIter iter;
-  run_request *r;
+handle_start(DBusConnection *con, DBusMessage *msg)
+{
+	DBusMessage *reply;
+	DBusMessageIter iter;
+	run_request *r;
 
-  r = new_run_request();
-  g_assert(dbus_message_iter_init(msg, &iter));
+	r = new_run_request();
+	g_assert(dbus_message_iter_init(msg, &iter));
 
-  if (!dbus_message_iter_init(msg, &iter) || !parse_first_part(r, msg, &iter)) 
-    goto malformed;
+	if (!dbus_message_iter_init(msg, &iter) || !parse_first_part(r, msg, &iter))
+		goto malformed;
 
-  if (run_request_run(r, NULL, NULL)) {
-    reply = dbus_message_new_method_return(msg);
-  } else {
-    reply = dbus_message_new_error(msg, "org.freedesktop.HalRunner.Failed",
-                                    "Start request failed");
-  }
-  dbus_connection_send(con, reply, NULL);
-  dbus_message_unref(reply);
-  return ;
+	if (run_request_run(r, NULL, NULL)) {
+		reply = dbus_message_new_method_return(msg);
+	} else {
+		reply = dbus_message_new_error(msg, "org.freedesktop.HalRunner.Failed",
+					       "Start request failed");
+	}
+	dbus_connection_send(con, reply, NULL);
+	dbus_message_unref(reply);
+	return ;
 malformed:
-  del_run_request(r);
-  reply = dbus_message_new_error(msg, "org.freedesktop.HalRunner.Malformed",
-                                 "Malformed start request");
-  dbus_connection_send(con, reply, NULL);
-  dbus_message_unref(reply);
+	del_run_request(r);
+	reply = dbus_message_new_error(msg, "org.freedesktop.HalRunner.Malformed",
+				       "Malformed start request");
+	dbus_connection_send(con, reply, NULL);
+	dbus_message_unref(reply);
 }
 
 static void
-handle_kill(DBusConnection *con, DBusMessage *msg) {
-  DBusError error;
-  DBusMessage *reply = NULL;
-  char *udi;
+handle_kill(DBusConnection *con, DBusMessage *msg)
+{
+	DBusError error;
+	DBusMessage *reply = NULL;
+	char *udi;
 
-  dbus_error_init (&error);
-  if (!dbus_message_get_args(msg, &error,
-                             DBUS_TYPE_STRING, &udi,
-                             DBUS_TYPE_INVALID)) {
-    reply = dbus_message_new_error (msg, 
-                                     "org.freedesktop.HalRunner.Malformed", 
-                                     "Malformed kill message");
-    g_assert(reply);
-    dbus_connection_send (con, reply, NULL);
-    dbus_message_unref(reply);
-    return;
-  }
-  run_kill_udi(udi);
-  /* always successfull */
-  reply = dbus_message_new_method_return(msg);
-  dbus_connection_send(con, reply, NULL);
-  dbus_message_unref(reply);
+	dbus_error_init (&error);
+	if (!dbus_message_get_args(msg, &error,
+				   DBUS_TYPE_STRING, &udi,
+				   DBUS_TYPE_INVALID)) {
+		reply = dbus_message_new_error (msg, "org.freedesktop.HalRunner.Malformed", 
+						"Malformed kill message");
+		g_assert(reply);
+		dbus_connection_send (con, reply, NULL);
+		dbus_message_unref(reply);
+		return;
+	}
+	run_kill_udi(udi);
+
+	/* always successfull */
+	reply = dbus_message_new_method_return(msg);
+	dbus_connection_send(con, reply, NULL);
+	dbus_message_unref(reply);
 }
 
 static DBusHandlerResult
-filter(DBusConnection *con, DBusMessage *msg, void *user_data) {
-  DBusMessage *reply;
+filter(DBusConnection *con, DBusMessage *msg, void *user_data)
+{
+	DBusMessage *reply;
 
-  if (dbus_message_is_method_call(msg,
-        "org.freedesktop.HalRunner", "Run")) {
-    handle_run(con, msg);
-    return DBUS_HANDLER_RESULT_HANDLED;
-  } else if (dbus_message_is_method_call(msg,
-              "org.freedesktop.HalRunner", "Start")) {
-    handle_start(con, msg);
-    return DBUS_HANDLER_RESULT_HANDLED;
-  } else if (dbus_message_is_method_call(msg,
-              "org.freedesktop.HalRunner", "Kill")) {
-    handle_kill(con, msg);
-    return DBUS_HANDLER_RESULT_HANDLED;
-  } else if (dbus_message_is_method_call(msg,
-              "org.freedesktop.HalRunner", "KillAll")) {
-    run_kill_all();
-    /* alwasy successfull */
-    reply = dbus_message_new_method_return(msg);
-    dbus_connection_send(con, reply, NULL);
-    dbus_message_unref(reply);
-    return DBUS_HANDLER_RESULT_HANDLED;
-  }
-  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	if (dbus_message_is_method_call(msg, "org.freedesktop.HalRunner", "Run")) {
+		handle_run(con, msg);
+		return DBUS_HANDLER_RESULT_HANDLED;
+	} else if (dbus_message_is_method_call(msg, "org.freedesktop.HalRunner", "Start")) {
+		handle_start(con, msg);
+		return DBUS_HANDLER_RESULT_HANDLED;
+	} else if (dbus_message_is_method_call(msg, "org.freedesktop.HalRunner", "Kill")) {
+		handle_kill(con, msg);
+		return DBUS_HANDLER_RESULT_HANDLED;
+	} else if (dbus_message_is_method_call(msg, "org.freedesktop.HalRunner", "KillAll")) {
+		run_kill_all();
+		/* alwasy successfull */
+		reply = dbus_message_new_method_return(msg);
+		dbus_connection_send(con, reply, NULL);
+		dbus_message_unref(reply);
+		return DBUS_HANDLER_RESULT_HANDLED;
+	}
+	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
 int
 main(int argc, char **argv)
 {
-  DBusConnection *c;
-  DBusError error;
-  GMainLoop *loop;
-  char *dbus_address;
+	DBusConnection *c;
+	DBusError error;
+	GMainLoop *loop;
+	char *dbus_address;
 
-  run_init();
-  dbus_error_init(&error);
-  dbus_address = getenv("HALD_RUNNER_DBUS_ADDRESS");
-  g_assert(dbus_address != NULL);
+	run_init();
+	dbus_error_init(&error);
+	dbus_address = getenv("HALD_RUNNER_DBUS_ADDRESS");
+	g_assert(dbus_address != NULL);
 
-  fprintf(stderr, "Runner started - allowed paths are '%s'\n", getenv("PATH"));
+	fprintf(stderr, "Runner started - allowed paths are '%s'\n", getenv("PATH"));
 
-  c = dbus_connection_open(dbus_address, &error);
-  if (c == NULL) 
-    goto error;
+	c = dbus_connection_open(dbus_address, &error);
+	if (c == NULL)
+		goto error;
 
-  loop = g_main_loop_new(NULL, FALSE);
+	loop = g_main_loop_new(NULL, FALSE);
 
-  dbus_connection_setup_with_g_main(c, NULL);
-  dbus_connection_set_exit_on_disconnect(c, TRUE);
-  dbus_connection_add_filter(c, filter, NULL, NULL);
+	dbus_connection_setup_with_g_main(c, NULL);
+	dbus_connection_set_exit_on_disconnect(c, TRUE);
+	dbus_connection_add_filter(c, filter, NULL, NULL);
 
-  g_main_loop_run(loop);
+	g_main_loop_run(loop);
 
-  fprintf(stderr, "Should not be reached\n");
-  
 error:
- fprintf(stderr,"An error has occured: %s\n", error.message);
- return -1;
+	fprintf(stderr,"An error has occured: %s\n", error.message);
+	return -1;
 }

Index: runner.c
===================================================================
RCS file: /cvs/hal/hal/hald-runner/runner.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- runner.c	21 Jan 2006 16:54:04 -0000	1.3
+++ runner.c	23 Jan 2006 14:54:48 -0000	1.4
@@ -50,280 +50,289 @@
 GHashTable *udi_hash = NULL;
 
 typedef struct {
-  run_request *r;
-  DBusMessage *msg;
-  DBusConnection *con;
-  GPid pid;
-  gint stderr_v;
-  guint watch;
-  guint timeout;
-  gboolean sent_kill;
+	run_request *r;
+	DBusMessage *msg;
+	DBusConnection *con;
+	GPid pid;
+	gint stderr_v;
+	guint watch;
+	guint timeout;
+	gboolean sent_kill;
 } run_data;
 
 static void
-del_run_data(run_data *rd) {
-  if (rd == NULL) 
-    return;
+del_run_data(run_data *rd)
+{
+	if (rd == NULL)
+		return;
 
-  del_run_request(rd->r);
-  if (rd->msg) {
-    dbus_message_unref(rd->msg);
-  }
-  g_spawn_close_pid(rd->pid);
+	del_run_request(rd->r);
+	if (rd->msg)
+		dbus_message_unref(rd->msg);
 
-  if (rd->stderr_v >= 0) {
-    close(rd->stderr_v);
-  }
+	g_spawn_close_pid(rd->pid);
 
-  if (rd->timeout != 0) {
-    g_source_remove(rd->timeout);
-  }
-  g_free(rd);
+	if (rd->stderr_v >= 0)
+		close(rd->stderr_v);
+
+	if (rd->timeout != 0)
+		g_source_remove(rd->timeout);
+
+	g_free(rd);
 }
 
 run_request *
-new_run_request(void) {
-  run_request *result;
-  result = g_new0(run_request, 1);
-  g_assert(result != NULL);
-  return result;
+new_run_request(void)
+{
+	run_request *result;
+	result = g_new0(run_request, 1);
+	g_assert(result != NULL);
+	return result;
 }
 
 void
-del_run_request(run_request *r) {
-  if (r == NULL) 
-    return;
-  g_free(r->udi);
-  free_string_array(r->environment);
-  free_string_array(r->argv);
-  g_free(r->input);
-  g_free(r);
+del_run_request(run_request *r)
+{
+	if (r == NULL)
+		return;
+	g_free(r->udi);
+	free_string_array(r->environment);
+	free_string_array(r->argv);
+	g_free(r->input);
+	g_free(r);
 }
 
 static void
-send_reply(DBusConnection *con, DBusMessage *msg, 
-           guint32 exit_type, gint32 return_code, gchar **error) {
-  DBusMessage *reply;
-  DBusMessageIter iter;
-  int i;
+send_reply(DBusConnection *con, DBusMessage *msg, guint32 exit_type, gint32 return_code, gchar **error)
+{
+	DBusMessage *reply;
+	DBusMessageIter iter;
+	int i;
 
-  if (con == NULL || msg == NULL) 
-    return;
+	if (con == NULL || msg == NULL) 
+		return;
 
-  reply = dbus_message_new_method_return(msg);
-  g_assert(reply != NULL);
+	reply = dbus_message_new_method_return(msg);
+	g_assert(reply != NULL);
 
-  dbus_message_iter_init_append(reply, &iter);
-  dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &exit_type);
-  dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &return_code);
-  if (error != NULL) for (i = 0; error[i] != NULL; i++) {
-    dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &error[i]);
-  }
+	dbus_message_iter_init_append(reply, &iter);
+	dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &exit_type);
+	dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &return_code);
+	if (error != NULL) for (i = 0; error[i] != NULL; i++) {
+		dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &error[i]);
+	}
 
-  dbus_connection_send(con, reply, NULL);
-  dbus_message_unref(reply);
+	dbus_connection_send(con, reply, NULL);
+	dbus_message_unref(reply);
 }
 
 static void
-remove_from_hash_table(run_data *rd) {
-  GList *list;
-  /* Remove to the hashtable */
-  list = (GList *)g_hash_table_lookup(udi_hash, rd->r->udi);
-  list = g_list_remove(list, rd);
-  /* The hash table will take care to not leak the dupped string */
-  g_hash_table_insert(udi_hash, g_strdup(rd->r->udi), list);
+remove_from_hash_table(run_data *rd)
+{
+	GList *list;
+
+	/* Remove to the hashtable */
+	list = (GList *)g_hash_table_lookup(udi_hash, rd->r->udi);
+	list = g_list_remove(list, rd);
+	/* The hash table will take care to not leak the dupped string */
+	g_hash_table_insert(udi_hash, g_strdup(rd->r->udi), list);
 }
 
 static void
-run_exited(GPid pid, gint status, gpointer data) {
-  run_data *rd = (run_data *)data;
-  char **error = NULL;
-
-  printf("%s exited\n", rd->r->argv[0]);
-  rd->watch = 0;
-  if (rd->sent_kill == TRUE) {
-    /* We send it a kill, so ignore */
-    del_run_data(rd);
-    return;
-  }
-  /* Check if it was a normal exit */
-  if (!WIFEXITED(status)) {
-    /* No not normal termination ? crash ? */
-    send_reply(rd->con, rd->msg, HALD_RUN_FAILED, 0, NULL);
-    remove_from_hash_table(rd);
-    del_run_data(rd);
-    return;
-  }
-  /* normal exit */
-  if (rd->stderr_v >= 0) {
-    /* Need to read stderr */
-    error = get_string_array_from_fd(rd->stderr_v);
-    rd->stderr_v = -1;
-  }
-  if (rd->msg != NULL) {
-    send_reply(rd->con, rd->msg, HALD_RUN_SUCCESS, WEXITSTATUS(status), error);
-  }
-  free_string_array(error);
+run_exited(GPid pid, gint status, gpointer data)
+{
+	run_data *rd = (run_data *)data;
+	char **error = NULL;
 
-  remove_from_hash_table(rd);
-  del_run_data(rd);
+	printf("%s exited\n", rd->r->argv[0]);
+	rd->watch = 0;
+	if (rd->sent_kill == TRUE) {
+		/* We send it a kill, so ignore */
+		del_run_data(rd);
+		return;
+	}
+	/* Check if it was a normal exit */
+	if (!WIFEXITED(status)) {
+		/* No not normal termination ? crash ? */
+		send_reply(rd->con, rd->msg, HALD_RUN_FAILED, 0, NULL);
+		remove_from_hash_table(rd);
+		del_run_data(rd);
+		return;
+	}
+	/* normal exit */
+	if (rd->stderr_v >= 0) {
+		/* Need to read stderr */
+		error = get_string_array_from_fd(rd->stderr_v);
+		rd->stderr_v = -1;
+	}
+	if (rd->msg != NULL)
+		send_reply(rd->con, rd->msg, HALD_RUN_SUCCESS, WEXITSTATUS(status), error);
+	free_string_array(error);
+	remove_from_hash_table(rd);
+	del_run_data(rd);
 }
 
 static gboolean
 run_timedout(gpointer data) {
-  run_data *rd = (run_data *)data;
-  /* Time is up, kill the process, send reply that it was killed! 
-   * Don't wait for exit, because it could hang in state D
-   */ 
-  kill(rd->pid, SIGTERM);
-  /* Ensure the timeout is not removed in the delete */
-  rd->timeout = 0;
-  /* So the exit watch will know it's killed  in case it runs*/
-  rd->sent_kill = TRUE;
+	run_data *rd = (run_data *)data;
+	/* Time is up, kill the process, send reply that it was killed! 
+	 * Don't wait for exit, because it could hang in state D
+	 */ 
+	kill(rd->pid, SIGTERM);
+	/* Ensure the timeout is not removed in the delete */
+	rd->timeout = 0;
+	/* So the exit watch will know it's killed  in case it runs*/
+	rd->sent_kill = TRUE;
 
-  send_reply(rd->con, rd->msg, HALD_RUN_TIMEOUT, 0, NULL);
-  remove_from_hash_table(rd);
-  return FALSE;
+	send_reply(rd->con, rd->msg, HALD_RUN_TIMEOUT, 0, NULL);
+	remove_from_hash_table(rd);
+	return FALSE;
 }
 
-static
-gboolean find_program(char **argv) 
+static gboolean
+find_program(char **argv)
 {
-  /* Search for the program in the dirs where it's allowed to be */
-  char *program;
-  char *path = NULL;
+	/* Search for the program in the dirs where it's allowed to be */
+	char *program;
+	char *path = NULL;
 
-  if (argv[0] == NULL) 
-    return FALSE;
+	if (argv[0] == NULL) 
+		return FALSE;
 
-  program = g_path_get_basename(argv[0]);
+	program = g_path_get_basename(argv[0]);
 
-  /* first search $PATH to make e.g. run-hald.sh work */
-  path = g_find_program_in_path (program);
-  g_free(program);
-  if (path == NULL) 
-    return FALSE;
-  else {
-    /* Replace program in argv[0] with the full path */
-    g_free(argv[0]);
-    argv[0] = path;
-  }
-  return TRUE;
+	/* first search $PATH to make e.g. run-hald.sh work */
+	path = g_find_program_in_path (program);
+	g_free(program);
+	if (path == NULL)
+		return FALSE;
+	else {
+		/* Replace program in argv[0] with the full path */
+		g_free(argv[0]);
+		argv[0] = path;
+	}
+	return TRUE;
 }
 
-
 /* Run the given request and reply it's result on msg */
-gboolean 
-run_request_run(run_request *r, DBusConnection *con, DBusMessage *msg) {
-  GPid pid;
-  GError *error = NULL;
-  gint *stdin_p = NULL;
-  gint *stderr_p = NULL;
-  gint stdin_v;
-  gint stderr_v = -1;
-  run_data *rd = NULL;
-  GList *list;
+gboolean
+run_request_run(run_request *r, DBusConnection *con, DBusMessage *msg)
+{
+	GPid pid;
+	GError *error = NULL;
+	gint *stdin_p = NULL;
+	gint *stderr_p = NULL;
+	gint stdin_v;
+	gint stderr_v = -1;
+	run_data *rd = NULL;
+	GList *list;
 
-  printf("Run started %s (%d) (%d) \n!", r->argv[0], r->timeout, 
-      r->error_on_stderr);
-  if (r->input != NULL) {
-    stdin_p = &stdin_v; 
-  }
-  if (r->error_on_stderr) {
-    stderr_p = &stderr_v;
-  }
+	printf("Run started %s (%d) (%d) \n!", r->argv[0], r->timeout,
+		r->error_on_stderr);
+	if (r->input != NULL) {
+		stdin_p = &stdin_v; 
+	}
+	if (r->error_on_stderr) {
+		stderr_p = &stderr_v;
+	}
 
-  if (!find_program(r->argv) ||
-      !g_spawn_async_with_pipes(NULL, r->argv, r->environment, 
-                                G_SPAWN_DO_NOT_REAP_CHILD,
-                                NULL, NULL, &pid, 
-                                stdin_p, NULL, stderr_p, &error)) {
-    del_run_request(r);
-    if (con && msg) {
-      send_reply(con, msg, HALD_RUN_FAILED, 0, NULL);
-    }
-    return FALSE;
-  }
+	if (!find_program(r->argv) ||
+		!g_spawn_async_with_pipes(NULL, r->argv, r->environment,
+		                          G_SPAWN_DO_NOT_REAP_CHILD,
+		                          NULL, NULL, &pid,
+		                          stdin_p, NULL, stderr_p, &error)) {
+		del_run_request(r);
+		if (con && msg)
+			send_reply(con, msg, HALD_RUN_FAILED, 0, NULL);
+		return FALSE;
+	}
 
-  if (r->input) {
-    write(stdin_v, r->input, strlen(r->input));
-    close(stdin_v);
-  }
+	if (r->input) {
+		write(stdin_v, r->input, strlen(r->input));
+		close(stdin_v);
+	}
 
-  rd = g_new0(run_data,1);
-  g_assert(rd != NULL);
-  rd->r = r;
-  rd->msg = msg;
-  if (msg != NULL) {
-    dbus_message_ref(msg);
-  }
-  rd->con = con;
-  rd->pid = pid;
-  rd->stderr_v = stderr_v;
-  rd->sent_kill = FALSE;
+	rd = g_new0(run_data,1);
+	g_assert(rd != NULL);
+	rd->r = r;
+	rd->msg = msg;
+	if (msg != NULL)
+		dbus_message_ref(msg);
 
-  /* Add watch for exit of the program */
-  rd->watch = g_child_watch_add(pid, run_exited, rd);
-  /* Add timeout if needed */
-  if (r->timeout > 0) {
-    rd->timeout = g_timeout_add(r->timeout, run_timedout, rd);
-  } else {
-    rd->timeout = 0;
-  }
-  /* Add to the hashtable */
-  list = (GList *)g_hash_table_lookup(udi_hash, r->udi);
-  list = g_list_prepend(list, rd);
-  /* The hash table will take care to not leak the dupped string */
-  g_hash_table_insert(udi_hash, g_strdup(r->udi), list);
-  return TRUE;
+	rd->con = con;
+	rd->pid = pid;
+	rd->stderr_v = stderr_v;
+	rd->sent_kill = FALSE;
+
+	/* Add watch for exit of the program */
+	rd->watch = g_child_watch_add(pid, run_exited, rd);
+
+	/* Add timeout if needed */
+	if (r->timeout > 0)
+		rd->timeout = g_timeout_add(r->timeout, run_timedout, rd);
+	else
+		rd->timeout = 0;
+
+	/* Add to the hashtable */
+	list = (GList *)g_hash_table_lookup(udi_hash, r->udi);
+	list = g_list_prepend(list, rd);
+
+	/* The hash table will take care to not leak the dupped string */
+	g_hash_table_insert(udi_hash, g_strdup(r->udi), list);
+	return TRUE;
 }
 
 static void
-kill_rd(gpointer data, gpointer user_data) {
-  run_data *rd = (run_data *)data;
+kill_rd(gpointer data, gpointer user_data)
+{
+	run_data *rd = (run_data *)data;
 
-  kill(rd->pid, SIGTERM);
-  printf("Sent kill to %d\n", rd->pid);
-  if (rd->timeout != 0) {
-    /* Remove the timeout watch */
-    g_source_remove(rd->timeout);
-    rd->timeout = 0;
-  }
-  /* So the exit watch will know it's killed  in case it runs*/
-  rd->sent_kill = TRUE;
+	kill(rd->pid, SIGTERM);
+	printf("Sent kill to %d\n", rd->pid);
+	if (rd->timeout != 0) {
+		/* Remove the timeout watch */
+		g_source_remove(rd->timeout);
+		rd->timeout = 0;
+	}
 
-  if (rd->msg != NULL) {
-    send_reply(rd->con, rd->msg, HALD_RUN_KILLED, 0, NULL);
-  }
+	/* So the exit watch will know it's killed  in case it runs */
+	rd->sent_kill = TRUE;
+
+	if (rd->msg != NULL)
+		send_reply(rd->con, rd->msg, HALD_RUN_KILLED, 0, NULL);
 }
 
 static void
-do_kill_udi(gchar *udi) {
-  GList *list;
-  list = (GList *)g_hash_table_lookup(udi_hash, udi);
-  g_list_foreach(list, kill_rd, NULL);
-  g_list_free(list);
+do_kill_udi(gchar *udi)
+{
+	GList *list;
+	list = (GList *)g_hash_table_lookup(udi_hash, udi);
+	g_list_foreach(list, kill_rd, NULL);
+	g_list_free(list);
 }
 
 /* Kill all running request for a udi */
 void 
-run_kill_udi(gchar *udi) {
-  do_kill_udi(udi);
-  g_hash_table_remove(udi_hash, udi);
+run_kill_udi(gchar *udi)
+{
+	do_kill_udi(udi);
+	g_hash_table_remove(udi_hash, udi);
 }
 
 static void
 hash_kill_udi(gpointer key, gpointer value, gpointer user_data) {
-  do_kill_udi(key);
+	do_kill_udi(key);
 }
 
 /* Kill all running request*/
 void 
-run_kill_all() {
-  g_hash_table_foreach_remove(udi_hash, hash_kill_udi, NULL);
+run_kill_all()
+{
+	g_hash_table_foreach_remove(udi_hash, hash_kill_udi, NULL);
 }
 
 void
-run_init() {
-  udi_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+run_init()
+{
+	udi_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
 }

Index: runner.h
===================================================================
RCS file: /cvs/hal/hal/hald-runner/runner.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- runner.h	21 Jan 2006 02:45:27 -0000	1.1
+++ runner.h	23 Jan 2006 14:54:48 -0000	1.2
@@ -31,12 +31,12 @@
 #include <glib.h>
 
 typedef struct {
-  gchar *udi;
-  gchar **environment;
-  gchar **argv;
-  gchar *input;
-  gboolean error_on_stderr;
-  guint32 timeout;
+	gchar *udi;
+	gchar **environment;
+	gchar **argv;
+	gchar *input;
+	gboolean error_on_stderr;
+	guint32 timeout;
 } run_request;
 
 run_request *new_run_request(void);
@@ -53,4 +53,5 @@
 
 /* initialise the actual runner data */
 void run_init();
+
 #endif /*  RUNNER_H */

Index: utils.c
===================================================================
RCS file: /cvs/hal/hal/hald-runner/utils.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- utils.c	21 Jan 2006 02:45:27 -0000	1.1
+++ utils.c	23 Jan 2006 14:54:48 -0000	1.2
@@ -31,56 +31,59 @@
 #include "utils.h"
 
 char **
-get_string_array(DBusMessageIter *iter) {
-  GArray *array;
-  char **result;
-  array = g_array_new(TRUE, FALSE, sizeof(char *));
+get_string_array(DBusMessageIter *iter)
+{
+	GArray *array;
+	char **result;
+	array = g_array_new(TRUE, FALSE, sizeof(char *));
 
-  while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
-    const char *value;
-    char *t;
-    dbus_message_iter_get_basic(iter, &value);
-    t = g_strdup(value);
-    g_array_append_vals(array, &t, 1);
-    dbus_message_iter_next(iter);
-  }
-  result = (char **) array->data;
-  g_array_free(array, FALSE);
-  return result;
+	while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
+		const char *value;
+		char *t;
+		dbus_message_iter_get_basic(iter, &value);
+		t = g_strdup(value);
+		g_array_append_vals(array, &t, 1);
+		dbus_message_iter_next(iter);
+	}
+	result = (char **) array->data;
+	g_array_free(array, FALSE);
+	return result;
 }
 
 char **
-get_string_array_from_fd(int fd) {
-  GArray *array;
-  char **result;
-  GString *str;
-  gsize pos;
-  GIOChannel *io;
-  int i = 0;
+get_string_array_from_fd(int fd)
+{
+	GArray *array;
+	char **result;
+	GString *str;
+	gsize pos;
+	GIOChannel *io;
+	int i = 0;
 
-  array = g_array_new(TRUE, FALSE, sizeof(char *));
-  str = g_string_new("");
-  io = g_io_channel_unix_new(fd);
-  while (g_io_channel_read_line_string(io, str, &pos, NULL) 
-          == G_IO_STATUS_NORMAL && (i++ < 128)) {
-    char *t;
-    /* Remove the terminting char aka \n*/
-    g_string_erase(str, pos, 1);
-    t = g_strdup(str->str);
-    g_array_append_vals(array, &t, 1);
-  }
-  g_string_free(str, TRUE);
-  g_io_channel_unref(io);
-  result = (char **) array->data;
-  g_array_free(array, FALSE);
-  return result;
+	array = g_array_new(TRUE, FALSE, sizeof(char *));
+	str = g_string_new("");
+	io = g_io_channel_unix_new(fd);
+	while (g_io_channel_read_line_string(io, str, &pos, NULL) == G_IO_STATUS_NORMAL && (i++ < 128)) {
+		char *t;
+
+		/* Remove the terminting char aka \n */
+		g_string_erase(str, pos, 1);
+		t = g_strdup(str->str);
+		g_array_append_vals(array, &t, 1);
+	}
+	g_string_free(str, TRUE);
+	g_io_channel_unref(io);
+	result = (char **) array->data;
+	g_array_free(array, FALSE);
+	return result;
 }
 
 void 
-free_string_array(char **array) {
-  char **p;
-  for (p = array; p != NULL && *p != NULL; p++) {
-    g_free(*p);
-  }
-  g_free(array);
+free_string_array(char **array)
+{
+	char **p;
+
+	for (p = array; p != NULL && *p != NULL; p++)
+		g_free(*p);
+	g_free(array);
 }




More information about the hal-commit mailing list