2
* Copyright © 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
25
#define DEFAULT_TIMEOUT 600 /* seconds, or 10 minutes */
27
typedef struct _HudWatchdogPrivate HudWatchdogPrivate;
28
struct _HudWatchdogPrivate {
34
#define HUD_WATCHDOG_GET_PRIVATE(o) \
35
(G_TYPE_INSTANCE_GET_PRIVATE ((o), HUD_WATCHDOG_TYPE, HudWatchdogPrivate))
37
static void hud_watchdog_class_init (HudWatchdogClass *klass);
38
static void hud_watchdog_init (HudWatchdog *self);
39
static void hud_watchdog_dispose (GObject *object);
40
static void hud_watchdog_finalize (GObject *object);
41
static gboolean fire_watchdog (gpointer user_data);
43
G_DEFINE_TYPE (HudWatchdog, hud_watchdog, G_TYPE_OBJECT);
46
hud_watchdog_class_init (HudWatchdogClass *klass)
48
GObjectClass *object_class = G_OBJECT_CLASS (klass);
50
g_type_class_add_private (klass, sizeof (HudWatchdogPrivate));
52
object_class->dispose = hud_watchdog_dispose;
53
object_class->finalize = hud_watchdog_finalize;
59
hud_watchdog_init (HudWatchdog *self)
61
self->priv = HUD_WATCHDOG_GET_PRIVATE(self);
63
const gchar * envvar = g_getenv("HUD_SERVICE_TIMEOUT");
65
self->priv->timeout = DEFAULT_TIMEOUT;
67
self->priv->timeout = atoi(envvar);
70
if (self->priv->timeout != 0) {
71
self->priv->timer = g_timeout_add_seconds(self->priv->timeout, fire_watchdog, self);
78
hud_watchdog_dispose (GObject *object)
80
HudWatchdog * self = HUD_WATCHDOG(object);
82
if (self->priv->timer != 0) {
83
g_source_remove(self->priv->timer);
84
self->priv->timer = 0;
87
G_OBJECT_CLASS (hud_watchdog_parent_class)->dispose (object);
92
hud_watchdog_finalize (GObject *object)
95
G_OBJECT_CLASS (hud_watchdog_parent_class)->finalize (object);
99
/* Oh noes! It's our time to go! Do this! */
101
fire_watchdog (gpointer user_data)
103
g_return_val_if_fail(IS_HUD_WATCHDOG(user_data), TRUE);
104
HudWatchdog * self = HUD_WATCHDOG(user_data);
106
g_debug("Firing Watchdog");
108
if (self->priv->loop != NULL) {
109
g_main_loop_quit(self->priv->loop);
117
* @loop: Mainloop to quit if we timeout
119
* Sets up a watchdog that will quit on the main loop if it
120
* doesn't get enough attention. Reminds of an girlfriend
123
* Return value: (transfer full): A new #HudWatchdog object
126
hud_watchdog_new (GMainLoop * loop)
128
HudWatchdog * watchdog = g_object_new(HUD_WATCHDOG_TYPE, NULL);
130
watchdog->priv->loop = loop;
137
* @watchdog: Watchdog to give attention to
139
* Makes sure to startover and not timeout.
142
hud_watchdog_ping (HudWatchdog * watchdog)
144
/* Doing a silent fail on NULL so that our tests can not worry about
145
setting up dummy watchdogs when just testing the query. They have
146
their own timeouts */
147
if (watchdog == NULL) {
151
g_return_if_fail(IS_HUD_WATCHDOG(watchdog));
153
if (watchdog->priv->timer != 0) {
154
g_source_remove(watchdog->priv->timer);
155
watchdog->priv->timer = 0;
158
if (watchdog->priv->timeout != 0) {
159
watchdog->priv->timer = g_timeout_add_seconds(watchdog->priv->timeout, fire_watchdog, watchdog);
165
* hud_watchdog_get_timeout:
166
* @watchdog: Watchdog to interegate
168
* Get the timeout of this watchdog.
170
* Return value: The number of seconds before it goes off
173
hud_watchdog_get_timeout (HudWatchdog * watchdog)
175
g_return_val_if_fail(IS_HUD_WATCHDOG(watchdog), 0);
177
return watchdog->priv->timeout;