From 6040c15370d703cdadf7fa6c98caced1b9e32e30 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 15 Jan 2007 19:49:02 +0000 Subject: Add startup timeout for spawned services --- hcid/dbus-service.c | 28 ++++++++++++++++++++++++++++ hcid/dbus-service.h | 2 ++ 2 files changed, 30 insertions(+) diff --git a/hcid/dbus-service.c b/hcid/dbus-service.c index e9d851b4..727ae5b3 100644 --- a/hcid/dbus-service.c +++ b/hcid/dbus-service.c @@ -45,6 +45,8 @@ #define SERVICE_INTERFACE "org.bluez.Service" +#define STARTUP_TIMEOUT (10 * 1000) /* 10 seconds */ + #define SERVICE_SUFFIX ".service" #define SERVICE_GROUP "Bluetooth Service" @@ -324,6 +326,11 @@ static void service_died(GPid pid, gint status, gpointer data) debug("%s (%s) exited with status %d", service->exec, service->name, status); + if (service->startup_timer) { + g_timeout_remove(service->startup_timer); + service->startup_timer = 0; + } + g_spawn_close_pid(pid); service->pid = 0; @@ -372,9 +379,26 @@ static DBusHandlerResult service_filter(DBusConnection *conn, if (signal) send_message_and_unref(conn, signal); + g_timeout_remove(service->startup_timer); + service->startup_timer = 0; + + name_listener_add(conn, new, service_exit, conn); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } +static gboolean service_startup_timeout(gpointer data) +{ + struct service *service = data; + + debug("Killing %s because it did not connect to D-Bus in time", + service->exec); + + kill(service->pid, SIGTERM); + + return FALSE; +} + static DBusHandlerResult start(DBusConnection *conn, DBusMessage *msg, void *data) { @@ -416,6 +440,10 @@ static DBusHandlerResult start(DBusConnection *conn, dbus_bus_add_match(conn, NAME_MATCH, NULL); + service->startup_timer = g_timeout_add(STARTUP_TIMEOUT, + service_startup_timeout, + service); + reply = dbus_message_new_method_return(msg); if (reply) send_message_and_unref(conn, reply); diff --git a/hcid/dbus-service.h b/hcid/dbus-service.h index 043795f3..2099c2d5 100644 --- a/hcid/dbus-service.h +++ b/hcid/dbus-service.h @@ -29,6 +29,8 @@ struct service { char *object_path; + guint startup_timer; + /* These are set when the service is running */ GPid pid; /* Process id */ char *bus_name; /* D-Bus unique name */ -- cgit