File: bolt-watchdog.c

package info (click to toggle)
bolt 0.9.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,820 kB
  • sloc: ansic: 28,978; python: 2,817; xml: 460; javascript: 269; sh: 173; makefile: 14
file content (238 lines) | stat: -rw-r--r-- 6,096 bytes parent folder | download | duplicates (3)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright © 2019 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

#include "bolt-watchdog.h"

#include "bolt-log.h"
#include "bolt-str.h"
#include "bolt-time.h"
#include "bolt-unix.h"

#include <inttypes.h>

/* prototypes */
static void     watchdog_initable_iface_init (GInitableIface *iface);
static gboolean bolt_watchdog_initialize (GInitable    *initable,
                                          GCancellable *cancellable,
                                          GError      **error);

static gboolean bolt_watchdog_on_pulse (gpointer user_data);
/*  */
struct _BoltWatchdog
{
  GObject object;

  /*  */
  guint64 timeout;   /* the actual timeout (usec) */
  guint64 pulse;     /* the calculated pulse (sec) */
  guint   pulse_id;  /* source id for the pulse */

};

enum {
  PROP_0,

  PROP_TIMEOUT,
  PROP_PULSE,

  PROP_LAST
};

static GParamSpec *props[PROP_LAST] = { NULL, };


G_DEFINE_TYPE_WITH_CODE (BoltWatchdog,
                         bolt_watchdog,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                watchdog_initable_iface_init));

static void
bolt_watchdog_finalize (GObject *object)
{
  BoltWatchdog *dog = BOLT_WATCHDOG (object);

  g_clear_handle_id (&dog->pulse_id, g_source_remove);

  G_OBJECT_CLASS (bolt_watchdog_parent_class)->finalize (object);
}


static void
bolt_watchdog_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  BoltWatchdog *dog = BOLT_WATCHDOG (object);

  switch (prop_id)
    {
    case PROP_TIMEOUT:
      g_value_set_uint64 (value, dog->timeout);
      break;

    case PROP_PULSE:
      g_value_set_uint (value, dog->pulse);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bolt_watchdog_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
  BoltWatchdog *dog = BOLT_WATCHDOG (object);

  switch (prop_id)
    {
    case PROP_TIMEOUT:
      dog->timeout = g_value_get_uint64 (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bolt_watchdog_init (BoltWatchdog *dog)
{
}

static void
bolt_watchdog_class_init (BoltWatchdogClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->get_property = bolt_watchdog_get_property;
  gobject_class->set_property = bolt_watchdog_set_property;
  gobject_class->finalize     = bolt_watchdog_finalize;

  props[PROP_TIMEOUT] =
    g_param_spec_uint64 ("timeout",
                         "Timeout", NULL,
                         0, G_MAXUINT64, 0,
                         G_PARAM_READABLE |
                         G_PARAM_STATIC_STRINGS);

  props[PROP_PULSE] =
    g_param_spec_uint ("pulse",
                       "Pulse", NULL,
                       0, G_MAXUINT - 1, 0,
                       G_PARAM_READABLE |
                       G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (gobject_class,
                                     PROP_LAST,
                                     props);
}

static void
watchdog_initable_iface_init (GInitableIface *iface)
{
  iface->init = bolt_watchdog_initialize;
}

static gboolean
bolt_watchdog_initialize (GInitable    *initable,
                          GCancellable *cancellable,
                          GError      **error)
{
  BoltWatchdog *dog = BOLT_WATCHDOG (initable);
  guint64 quot, rem;
  guint pulse;
  guint tid;
  int r;

  r = bolt_sd_watchdog_enabled (&dog->timeout, error);

  if (r < 0)
    return FALSE;
  else if (r == 0)
    return TRUE;

  quot = dog->timeout / G_USEC_PER_SEC;
  rem = dog->timeout % G_USEC_PER_SEC;

  if (quot < 2 || quot >= G_MAXUINT)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                   "invalid timeout: %" G_GUINT64_FORMAT, dog->timeout);
      return FALSE;
    }

  if (rem != 0)
    bolt_warn (LOG_TOPIC ("watchdog"),
               "sub-second precision timeout: "
               "%" G_GUINT64_FORMAT ". Rounding down.", rem);

  /* we send a pulse/ping right in the middle of the timeout
   * period to ensure we never miss one */
  pulse = quot / 2;

  tid = g_timeout_add_seconds (pulse, bolt_watchdog_on_pulse, dog);
  dog->pulse_id = tid;
  dog->pulse = pulse;

  /* we have an active timeout enabled */
  bolt_info (LOG_TOPIC ("watchdog"), "enabled [pulse: %lus]", pulse);

  return TRUE;
}

/* internal methods */
static gboolean
bolt_watchdog_on_pulse (gpointer user_data)
{
  g_autoptr(GError) err = NULL;
  gboolean ok;
  gboolean sent;

  ok = bolt_sd_notify_literal ("WATCHDOG=1", &sent, &err);

  if (!ok)
    bolt_warn_err (err, LOG_TOPIC ("watchdog"), "failed to send ping");
  else
    bolt_debug (LOG_TOPIC ("watchdog"), "ping [sent: %s]",
                bolt_yesno (sent));

  return G_SOURCE_CONTINUE;
}

/* public methods */
BoltWatchdog  *
bolt_watchdog_new (GError **error)
{
  BoltWatchdog *watchdog;

  watchdog = g_initable_new (BOLT_TYPE_WATCHDOG,
                             NULL, error,
                             NULL);

  return watchdog;
}