| 1 |
/* Systemd service wd notification for OC-2G BTS management daemon */ |
| 2 |
|
| 3 |
/* Copyright (C) 2015 by Yves Godin <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Affero General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Affero General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Affero General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "misc/oc2gbts_mgr.h" |
| 21 |
#include "misc/oc2gbts_swd.h" |
| 22 |
#include <osmocom/core/logging.h> |
| 23 |
|
| 24 |
/* Needed for service watchdog notification */ |
| 25 |
#include <systemd/sd-daemon.h> |
| 26 |
|
| 27 |
/* This is the period used to verify if all events have been registered to be allowed |
| 28 |
to notify the systemd service watchdog |
| 29 |
*/ |
| 30 |
#define SWD_PERIOD 30 |
| 31 |
|
| 32 |
static void swd_start(struct oc2gbts_mgr_instance *mgr); |
| 33 |
static void swd_process(struct oc2gbts_mgr_instance *mgr); |
| 34 |
static void swd_close(struct oc2gbts_mgr_instance *mgr); |
| 35 |
static void swd_state_reset(struct oc2gbts_mgr_instance *mgr, int reason); |
| 36 |
static int swd_run(struct oc2gbts_mgr_instance *mgr, int from_loop); |
| 37 |
static void swd_loop_run(void *_data); |
| 38 |
|
| 39 |
enum swd_state { |
| 40 |
SWD_INITIAL, |
| 41 |
SWD_IN_PROGRESS, |
| 42 |
}; |
| 43 |
|
| 44 |
enum swd_result { |
| 45 |
SWD_FAIL_START, |
| 46 |
SWD_FAIL_NOTIFY, |
| 47 |
SWD_SUCCESS, |
| 48 |
}; |
| 49 |
|
| 50 |
static void swd_start(struct oc2gbts_mgr_instance *mgr) |
| 51 |
{ |
| 52 |
swd_process(mgr); |
| 53 |
} |
| 54 |
|
| 55 |
static void swd_process(struct oc2gbts_mgr_instance *mgr) |
| 56 |
{ |
| 57 |
int rc = 0, notify = 0; |
| 58 |
|
| 59 |
/* Did we get all needed conditions ? */ |
| 60 |
if (mgr->swd.swd_eventmasks == mgr->swd.swd_events) { |
| 61 |
/* Ping systemd service wd if enabled */ |
| 62 |
rc = sd_notify(0, "WATCHDOG=1"); |
| 63 |
LOGP(DSWD, LOGL_INFO, "Watchdog notification attempt\n"); |
| 64 |
notify = 1; |
| 65 |
} |
| 66 |
else { |
| 67 |
LOGP(DSWD, LOGL_INFO, "Missing watchdog events: e:0x%016llx,m:0x%016llx\n",mgr->swd.swd_events,mgr->swd.swd_eventmasks); |
| 68 |
} |
| 69 |
|
| 70 |
if (rc < 0) { |
| 71 |
LOGP(DSWD, LOGL_ERROR, |
| 72 |
"Failed to notify system service watchdog: %d\n", rc); |
| 73 |
swd_state_reset(mgr, SWD_FAIL_NOTIFY); |
| 74 |
return; |
| 75 |
} |
| 76 |
else { |
| 77 |
/* Did we notified the watchdog? */ |
| 78 |
if (notify) { |
| 79 |
mgr->swd.swd_events = 0; |
| 80 |
/* Makes sure we really cleared it in case any event was notified at this same moment (it would be lost) */ |
| 81 |
if (mgr->swd.swd_events != 0) |
| 82 |
mgr->swd.swd_events = 0; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
swd_state_reset(mgr, SWD_SUCCESS); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
static void swd_close(struct oc2gbts_mgr_instance *mgr) |
| 91 |
{ |
| 92 |
} |
| 93 |
|
| 94 |
static void swd_state_reset(struct oc2gbts_mgr_instance *mgr, int outcome) |
| 95 |
{ |
| 96 |
if (mgr->swd.swd_from_loop) { |
| 97 |
mgr->swd.swd_timeout.data = mgr; |
| 98 |
mgr->swd.swd_timeout.cb = swd_loop_run; |
| 99 |
osmo_timer_schedule(&mgr->swd.swd_timeout, SWD_PERIOD, 0); |
| 100 |
} |
| 101 |
|
| 102 |
mgr->swd.state = SWD_INITIAL; |
| 103 |
swd_close(mgr); |
| 104 |
} |
| 105 |
|
| 106 |
static int swd_run(struct oc2gbts_mgr_instance *mgr, int from_loop) |
| 107 |
{ |
| 108 |
if (mgr->swd.state != SWD_INITIAL) { |
| 109 |
LOGP(DSWD, LOGL_ERROR, "Swd is already in progress.\n"); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
mgr->swd.swd_from_loop = from_loop; |
| 114 |
|
| 115 |
/* From now on everything will be handled from the failure */ |
| 116 |
mgr->swd.state = SWD_IN_PROGRESS; |
| 117 |
swd_start(mgr); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static void swd_loop_run(void *_data) |
| 122 |
{ |
| 123 |
int rc; |
| 124 |
struct oc2gbts_mgr_instance *mgr = _data; |
| 125 |
|
| 126 |
LOGP(DSWD, LOGL_INFO, "Going to check for watchdog notification.\n"); |
| 127 |
rc = swd_run(mgr, 1); |
| 128 |
if (rc != 0) { |
| 129 |
swd_state_reset(mgr, SWD_FAIL_START); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/* 'swd_num_events' configures the number of events to be monitored before notifying the |
| 134 |
systemd service watchdog. It must be in the range of [1,64]. Events are notified |
| 135 |
through the function 'oc2gbts_swd_event' |
| 136 |
*/ |
| 137 |
int oc2gbts_swd_init(struct oc2gbts_mgr_instance *mgr, int swd_num_events) |
| 138 |
{ |
| 139 |
/* Checks for a valid number of events to validate */ |
| 140 |
if (swd_num_events < 1 || swd_num_events > 64) |
| 141 |
return(-1); |
| 142 |
|
| 143 |
mgr->swd.state = SWD_INITIAL; |
| 144 |
mgr->swd.swd_timeout.data = mgr; |
| 145 |
mgr->swd.swd_timeout.cb = swd_loop_run; |
| 146 |
osmo_timer_schedule(&mgr->swd.swd_timeout, 0, 0); |
| 147 |
|
| 148 |
if (swd_num_events == 64){ |
| 149 |
mgr->swd.swd_eventmasks = 0xffffffffffffffffULL; |
| 150 |
} |
| 151 |
else { |
| 152 |
mgr->swd.swd_eventmasks = ((1ULL << swd_num_events) - 1); |
| 153 |
} |
| 154 |
mgr->swd.swd_events = 0; |
| 155 |
mgr->swd.num_events = swd_num_events; |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
/* Notifies that the specified event 'swd_event' happened correctly; |
| 161 |
the value must be in the range of [0,'swd_num_events'[ (see oc2gbts_swd_init). |
| 162 |
For example, if 'swd_num_events' was 64, 'swd_event' events are numbered 0 to 63. |
| 163 |
WARNING: if this function can be used from multiple threads at the same time, |
| 164 |
it must be protected with a kind of mutex to avoid losing event notification. |
| 165 |
*/ |
| 166 |
int oc2gbts_swd_event(struct oc2gbts_mgr_instance *mgr, enum mgr_swd_events swd_event) |
| 167 |
{ |
| 168 |
/* Checks for a valid specified event (smaller than max possible) */ |
| 169 |
if ((int)(swd_event) < 0 || (int)(swd_event) >= mgr->swd.num_events) |
| 170 |
return(-1); |
| 171 |
|
| 172 |
mgr->swd.swd_events = mgr->swd.swd_events | ((unsigned long long int)(1) << (int)(swd_event)); |
| 173 |
|
| 174 |
/* !!! Uncomment following line to debug events notification */ |
| 175 |
LOGP(DSWD, LOGL_DEBUG,"Swd event notified: %d\n", (int)(swd_event)); |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |