summaryrefslogtreecommitdiff
path: root/common/watchdog-client.c
blob: 4ccf15ce5023a8c1351317b3f25952e04eb666ba (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* vi:set et ai sw=2 sts=2 ts=2: */
/* -
 * Copyright (c) 2012 GENIVI.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <systemd/sd-daemon.h>

#include <glib-object.h>
#include <gio/gio.h>

#include <common/watchdog-client.h>



/**
 * SECTION: watchdog-client
 * @title: Watchdog Client
 * @short_description: Notifies the systemd watchdog over a set period.
 * @stability: Internal
 * 
 * The watchdog client notifies systemd's watchdog regularly over a period of seconds
 * defined in watchdog_client_new(). If the appropriate service file has %WatchdogSec set
 * then systemd will restart the service if it has not replied during that period (e.g. it
 * has crashed or is stuck in an infinite loop).
 * 
 */



/* property identifiers */
enum
{
  PROP_0,
  PROP_TIMEOUT,
};



static void     watchdog_client_constructed  (GObject      *object);
static void     watchdog_client_finalize     (GObject      *object);
static void     watchdog_client_get_property (GObject      *object,
                                              guint         prop_id,
                                              GValue       *value,
                                              GParamSpec   *pspec);
static void     watchdog_client_set_property (GObject      *object,
                                              guint         prop_id,
                                              const GValue *value,
                                              GParamSpec   *pspec);
static gboolean watchdog_client_timeout      (gpointer      user_data);



struct _WatchdogClientClass
{
  GObjectClass __parent__;
};

struct _WatchdogClient
{
  GObject          __parent__;

  guint            timeout;
  guint            timeout_id;
};



G_DEFINE_TYPE (WatchdogClient, watchdog_client, G_TYPE_OBJECT);



static void
watchdog_client_class_init (WatchdogClientClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->constructed = watchdog_client_constructed;
  gobject_class->finalize = watchdog_client_finalize;
  gobject_class->get_property = watchdog_client_get_property;
  gobject_class->set_property = watchdog_client_set_property;

  g_object_class_install_property (gobject_class,
                                   PROP_TIMEOUT,
                                   g_param_spec_uint ("timeout",
                                                      "timeout",
                                                      "timeout",
                                                      0, G_MAXUINT, 120,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT_ONLY |
                                                      G_PARAM_STATIC_STRINGS));
}



static void
watchdog_client_init (WatchdogClient *client)
{
}



static void
watchdog_client_constructed (GObject *object)
{
  WatchdogClient *client = WATCHDOG_CLIENT (object);

  /* trigger a systemd watchdog timestap update now */
  watchdog_client_timeout (client);

  /* schedule a regular timeout to update the systemd watchdog timestamp */
  client->timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
                                                   client->timeout,
                                                   watchdog_client_timeout,
                                                   g_object_ref (client),
                                                   (GDestroyNotify) g_object_unref);
}



static void
watchdog_client_finalize (GObject *object)
{
  WatchdogClient *client = WATCHDOG_CLIENT (object);

  /* drop the watchdog timeout */
  if (client->timeout_id > 0)
    g_source_remove (client->timeout_id);

  (*G_OBJECT_CLASS (watchdog_client_parent_class)->finalize) (object);
}



static void
watchdog_client_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  WatchdogClient *client = WATCHDOG_CLIENT (object);

  switch (prop_id)
    {
    case PROP_TIMEOUT:
      g_value_set_uint (value, client->timeout);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
watchdog_client_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  WatchdogClient *client = WATCHDOG_CLIENT (object);

  switch (prop_id)
    {
    case PROP_TIMEOUT:
      client->timeout = g_value_get_uint (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static gboolean
watchdog_client_timeout (gpointer user_data)
{
  sd_notify (0, "WATCHDOG=1");
  return TRUE;
}



/**
 * watchdog_client_new:
 * @timeout: The amount of time to wait in between notifications to systemd's watchdog.
 * 
 * Creates a new watchdog and starts notifying systemd's watchdog every @timeout seconds.
 * 
 * Returns: A new instance of #WatchdogClient.
 */
WatchdogClient *
watchdog_client_new (guint timeout)
{
  return g_object_new (TYPE_WATCHDOG_CLIENT, "timeout", timeout, NULL);
}