~indicator-applet-developers/dbus-test-runner/trunk.16.10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "watchdog.h"

typedef struct {
	GPid watchdog;
} DbusTestWatchdogPrivate;

static void dbus_test_watchdog_class_init (DbusTestWatchdogClass *klass);
static void dbus_test_watchdog_init       (DbusTestWatchdog *self);
static void dbus_test_watchdog_finalize   (GObject *object);

G_DEFINE_TYPE_WITH_PRIVATE (DbusTestWatchdog, dbus_test_watchdog, G_TYPE_OBJECT);

/* Initialize class */
static void
dbus_test_watchdog_class_init (DbusTestWatchdogClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = dbus_test_watchdog_finalize;

	return;
}

/* Initialize instance data */
static void
dbus_test_watchdog_init (G_GNUC_UNUSED DbusTestWatchdog *self)
{
	DbusTestWatchdogPrivate *priv = dbus_test_watchdog_get_instance_private (self);

	priv->watchdog = 0;

	return;
}

/* clean up memory */
static void
dbus_test_watchdog_finalize (GObject *object)
{
	DbusTestWatchdog * watchdog = DBUS_TEST_WATCHDOG(object);
	DbusTestWatchdogPrivate *priv = dbus_test_watchdog_get_instance_private (watchdog);

	if (priv->watchdog != 0) {
		kill(priv->watchdog, SIGTERM);
	}

	G_OBJECT_CLASS (dbus_test_watchdog_parent_class)->finalize (object);
	return;
}

/**
 * dbus_test_watchdog_add_pid:
 * @watchdog: Instance of #DbusTestWatchdog
 * @pid: PID to kill
 *
 * Adds a PID for the watchdog to watch.
 */
void
dbus_test_watchdog_add_pid (DbusTestWatchdog * watchdog, GPid pid)
{
	g_return_if_fail(DBUS_TEST_IS_WATCHDOG(watchdog));
	g_return_if_fail(pid != 0);
	DbusTestWatchdogPrivate *priv = dbus_test_watchdog_get_instance_private (watchdog);
	g_return_if_fail(priv->watchdog == 0);

	/* Setting up argument vector */
	gchar * strpid = g_strdup_printf("%d", pid);
	gchar * argv[3];
	argv[0] = WATCHDOG;
	argv[1] = strpid;
	argv[2] = NULL;
	
	GError * error = NULL;

	/* Spawn the watchdog, we now have 60 secs */
	g_spawn_async (NULL, /* cwd */
	               argv,
	               NULL, /* env */
	               0, /* flags */
	               NULL, NULL, /* Setup function */
	               &priv->watchdog,
	               &error);

	g_free(strpid);

	if (error != NULL) {
		g_warning("Unable to start watchdog");
		priv->watchdog = 0;
		g_error_free(error);
		return;
	}

	return;
}

/**
 * dbus_test_watchdog_add_pid:
 * @watchdog: Instance of #DbusTestWatchdog
 *
 * Tell the watchdog not to kill.  For now.
 */
void
dbus_test_watchdog_ping (DbusTestWatchdog * watchdog)
{
	g_return_if_fail(DBUS_TEST_IS_WATCHDOG(watchdog));
	DbusTestWatchdogPrivate *priv = dbus_test_watchdog_get_instance_private (watchdog);

	if (priv->watchdog != 0) {
		kill(priv->watchdog, SIGHUP);
	}

	return;
}