~ubuntu-branches/ubuntu/trusty/hud/trusty-updates

« back to all changes in this revision

Viewing changes to src/watchdog.c

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-01-20 19:43:59 UTC
  • mfrom: (1.1.26)
  • Revision ID: package-import@ubuntu.com-20140120194359-jxxxqtd4ql9elvpf
Tags: 13.10.1+14.04.20140120-0ubuntu1
* New rebuild forced
* Automatic snapshot from revision 362

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2013 Canonical Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 *
16
 
 */
17
 
 
18
 
#ifdef HAVE_CONFIG_H
19
 
#include "config.h"
20
 
#endif
21
 
 
22
 
#include <stdlib.h>
23
 
#include "watchdog.h"
24
 
 
25
 
#define DEFAULT_TIMEOUT  600 /* seconds, or 10 minutes */
26
 
 
27
 
typedef struct _HudWatchdogPrivate HudWatchdogPrivate;
28
 
struct _HudWatchdogPrivate {
29
 
        guint timeout;
30
 
        gulong timer;
31
 
        GMainLoop * loop;
32
 
};
33
 
 
34
 
#define HUD_WATCHDOG_GET_PRIVATE(o) \
35
 
(G_TYPE_INSTANCE_GET_PRIVATE ((o), HUD_WATCHDOG_TYPE, HudWatchdogPrivate))
36
 
 
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);
42
 
 
43
 
G_DEFINE_TYPE (HudWatchdog, hud_watchdog, G_TYPE_OBJECT);
44
 
 
45
 
static void
46
 
hud_watchdog_class_init (HudWatchdogClass *klass)
47
 
{
48
 
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
49
 
 
50
 
        g_type_class_add_private (klass, sizeof (HudWatchdogPrivate));
51
 
 
52
 
        object_class->dispose = hud_watchdog_dispose;
53
 
        object_class->finalize = hud_watchdog_finalize;
54
 
 
55
 
        return;
56
 
}
57
 
 
58
 
static void
59
 
hud_watchdog_init (HudWatchdog *self)
60
 
{
61
 
        self->priv = HUD_WATCHDOG_GET_PRIVATE(self);
62
 
 
63
 
        const gchar * envvar = g_getenv("HUD_SERVICE_TIMEOUT");
64
 
        if (envvar == NULL) {
65
 
                self->priv->timeout = DEFAULT_TIMEOUT;
66
 
        } else {
67
 
                self->priv->timeout = atoi(envvar);
68
 
        }
69
 
 
70
 
        if (self->priv->timeout != 0) {
71
 
                self->priv->timer = g_timeout_add_seconds(self->priv->timeout, fire_watchdog, self);
72
 
        }
73
 
 
74
 
        return;
75
 
}
76
 
 
77
 
static void
78
 
hud_watchdog_dispose (GObject *object)
79
 
{
80
 
        HudWatchdog * self = HUD_WATCHDOG(object);
81
 
 
82
 
        if (self->priv->timer != 0) {
83
 
                g_source_remove(self->priv->timer);
84
 
                self->priv->timer = 0;
85
 
        }
86
 
 
87
 
        G_OBJECT_CLASS (hud_watchdog_parent_class)->dispose (object);
88
 
        return;
89
 
}
90
 
 
91
 
static void
92
 
hud_watchdog_finalize (GObject *object)
93
 
{
94
 
 
95
 
        G_OBJECT_CLASS (hud_watchdog_parent_class)->finalize (object);
96
 
        return;
97
 
}
98
 
 
99
 
/* Oh noes!  It's our time to go!  Do this!  */
100
 
static gboolean
101
 
fire_watchdog (gpointer user_data)
102
 
{
103
 
        g_return_val_if_fail(IS_HUD_WATCHDOG(user_data), TRUE);
104
 
        HudWatchdog * self = HUD_WATCHDOG(user_data);
105
 
 
106
 
        g_debug("Firing Watchdog");
107
 
 
108
 
        if (self->priv->loop != NULL) {
109
 
                g_main_loop_quit(self->priv->loop);
110
 
        }
111
 
 
112
 
        return FALSE;
113
 
}
114
 
 
115
 
/**
116
 
 * hud_watchdog_new:
117
 
 * @loop: Mainloop to quit if we timeout
118
 
 *
119
 
 * Sets up a watchdog that will quit on the main loop if it
120
 
 * doesn't get enough attention.  Reminds of an girlfriend
121
 
 * I had once...
122
 
 *
123
 
 * Return value: (transfer full): A new #HudWatchdog object
124
 
 */
125
 
HudWatchdog *
126
 
hud_watchdog_new (GMainLoop * loop)
127
 
{
128
 
        HudWatchdog * watchdog = g_object_new(HUD_WATCHDOG_TYPE, NULL);
129
 
 
130
 
        watchdog->priv->loop = loop;
131
 
 
132
 
        return watchdog;
133
 
}
134
 
 
135
 
/**
136
 
 * hud_watchdog_ping:
137
 
 * @watchdog: Watchdog to give attention to
138
 
 *
139
 
 * Makes sure to startover and not timeout.
140
 
 */
141
 
void
142
 
hud_watchdog_ping (HudWatchdog * watchdog)
143
 
{
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) {
148
 
                return;
149
 
        }
150
 
 
151
 
        g_return_if_fail(IS_HUD_WATCHDOG(watchdog));
152
 
 
153
 
        if (watchdog->priv->timer != 0) {
154
 
                g_source_remove(watchdog->priv->timer);
155
 
                watchdog->priv->timer = 0;
156
 
        }
157
 
 
158
 
        if (watchdog->priv->timeout != 0) {
159
 
                watchdog->priv->timer = g_timeout_add_seconds(watchdog->priv->timeout, fire_watchdog, watchdog);
160
 
        }
161
 
        return;
162
 
}
163
 
 
164
 
/**
165
 
 * hud_watchdog_get_timeout:
166
 
 * @watchdog: Watchdog to interegate
167
 
 *
168
 
 * Get the timeout of this watchdog.
169
 
 *
170
 
 * Return value: The number of seconds before it goes off
171
 
 */
172
 
guint
173
 
hud_watchdog_get_timeout (HudWatchdog * watchdog)
174
 
{
175
 
        g_return_val_if_fail(IS_HUD_WATCHDOG(watchdog), 0);
176
 
 
177
 
        return watchdog->priv->timeout;
178
 
}