1 /* vi:set et ai sw=2 sts=2 ts=2: */
3 * Copyright (c) 2012 GENIVI.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 #include <systemd/sd-daemon.h>
16 #include <glib-object.h>
19 #include <common/watchdog-client.h>
24 * SECTION: watchdog-client
25 * @title: WatchdogClient
26 * @short_description: Notifies the systemd watchdog in regular intervals.
27 * @stability: Internal
29 * The #WatchdogClient notifies systemd's watchdog in a regular interval that
30 * is specified upon construction. If the unit file associated with the
31 * application has %WatchdogSec set then systemd will restart the application
32 * if it does not update the watchdog timestamp in this interval (e.g. if it
33 * has crashed or is stuck in an infinite loop).
35 * In order to avoid problems with delays it is recommended to notify the
36 * systemd watchdog twice in the %WatchdogSec interval, so usually the
37 * value passed to #watchdog_client_new will be half of %WatchdogSec.
42 /* property identifiers */
51 static void watchdog_client_constructed (GObject *object);
52 static void watchdog_client_finalize (GObject *object);
53 static void watchdog_client_get_property (GObject *object,
57 static void watchdog_client_set_property (GObject *object,
61 static gboolean watchdog_client_timeout (gpointer user_data);
65 struct _WatchdogClientClass
67 GObjectClass __parent__;
70 struct _WatchdogClient
80 G_DEFINE_TYPE (WatchdogClient, watchdog_client, G_TYPE_OBJECT);
85 watchdog_client_class_init (WatchdogClientClass *klass)
87 GObjectClass *gobject_class;
89 gobject_class = G_OBJECT_CLASS (klass);
90 gobject_class->constructed = watchdog_client_constructed;
91 gobject_class->finalize = watchdog_client_finalize;
92 gobject_class->get_property = watchdog_client_get_property;
93 gobject_class->set_property = watchdog_client_set_property;
95 g_object_class_install_property (gobject_class,
97 g_param_spec_uint ("timeout",
102 G_PARAM_CONSTRUCT_ONLY |
103 G_PARAM_STATIC_STRINGS));
109 watchdog_client_init (WatchdogClient *client)
116 watchdog_client_constructed (GObject *object)
118 WatchdogClient *client = WATCHDOG_CLIENT (object);
120 /* trigger a systemd watchdog timestap update now */
121 watchdog_client_timeout (client);
123 /* schedule a regular timeout to update the systemd watchdog timestamp */
124 client->timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
126 watchdog_client_timeout,
127 g_object_ref (client),
128 (GDestroyNotify) g_object_unref);
134 watchdog_client_finalize (GObject *object)
136 WatchdogClient *client = WATCHDOG_CLIENT (object);
138 /* drop the watchdog timeout */
139 if (client->timeout_id > 0)
140 g_source_remove (client->timeout_id);
142 (*G_OBJECT_CLASS (watchdog_client_parent_class)->finalize) (object);
148 watchdog_client_get_property (GObject *object,
153 WatchdogClient *client = WATCHDOG_CLIENT (object);
158 g_value_set_uint (value, client->timeout);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 watchdog_client_set_property (GObject *object,
174 WatchdogClient *client = WATCHDOG_CLIENT (object);
179 client->timeout = g_value_get_uint (value);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 watchdog_client_timeout (gpointer user_data)
192 sd_notify (0, "WATCHDOG=1");
199 * watchdog_client_new:
200 * @timeout: The amount of time to wait in between notifications to systemd's watchdog.
202 * Creates a new watchdog and starts notifying systemd's watchdog every @timeout seconds.
204 * Returns: A new instance of #WatchdogClient.
207 watchdog_client_new (guint timeout)
209 return g_object_new (TYPE_WATCHDOG_CLIENT, "timeout", timeout, NULL);