Skip to content

Commit 67538b4

Browse files
committed
drivers/main.{c,h}, docs/man/ups.conf.txt, conf/ups.conf.sample, NEWS.adoc: introduce reconnect_max_tries setting for NUT drivers [#3540]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent 28e3bf0 commit 67538b4

5 files changed

Lines changed: 62 additions & 1 deletion

File tree

NEWS.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ https://github.com/networkupstools/nut/milestone/13
156156
chunks populated only partially with bytes we intended. These buffers are
157157
now more diligently pre-zeroed as their siblings were in other code paths,
158158
to avoid sending host stack garbage to a device. [#3529]
159+
* Introduced a `reconnect_max_tries` setting for NUT drivers, so they can
160+
be configured to exit after the specified number of reconnection attempts
161+
(so that the OS service management facility can completely recycle the
162+
driver program). [#3540]
159163

160164
- NUT client libraries:
161165
* Complete support for actions documented in `docs/net-protocol.txt`

conf/ups.conf.sample

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ maxretry = 3
112112
#
113113
# The default is 75 seconds.
114114
#
115+
# reconnect_max_tries: OPTIONAL. This can be set as a global variable above
116+
# your first UPS definition and it can also be set in a
117+
# UPS section. If a driver loses connection to the device,
118+
# try reconnecting for the specified number of iterations and
119+
# exit (so some OS service management facility or an alerted
120+
# sysadmin can restart the driver program "from scratch").
121+
# A zero value (unreasonable but possible) means to exit as
122+
# soon as the device connection was lost.
123+
# A negative value (default) means to try reconnecting
124+
# indefinitely (e.g. data cable fell out, or the data path
125+
# to a networked UPS was lost for a significant time).
126+
#
115127
# debug_min: OPTIONAL. Specify a minimum debug level for all driver daemons
116128
# and for the upsdrvctl tool (when specified at global level),
117129
# or for this driver daemon (when specified in a driver section),

docs/man/ups.conf.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ its own in `upsd.conf`.
100100
+
101101
Environment variable `NUT_STATEPATH` set by caller can override this setting.
102102

103+
*reconnect_max_tries*::
104+
105+
Optional. If a driver loses connection to the device, try reconnecting for
106+
the specified number of iterations and exit (so some OS service management
107+
facility or an alerted sysadmin can restart the driver program "from scratch").
108+
+
109+
A zero value (unreasonable but possible) means to exit as soon as the device
110+
connection was lost.
111+
+
112+
A negative value (default) means to try reconnecting indefinitely (e.g. data
113+
cable fell out, or the data path to a networked UPS was lost for a significant
114+
time).
115+
103116
*maxstartdelay*::
104117

105118
Optional. Same as the UPS field of the same name, but this is the
@@ -340,6 +353,11 @@ They would not be actively killed by `upsdrvctl` after this timeout expires.
340353
+
341354
The default is 75 seconds.
342355

356+
*reconnect_max_tries*::
357+
Optional. This can be set as a global variable above your first UPS
358+
definition and it can also be set in a UPS section. See explanation
359+
above, in the global section.
360+
343361
*maxretry*::
344362
Optional. This can be set as a global variable above your first UPS
345363
definition and it can also be set in a UPS section. See explanation

drivers/main.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ vartab_t *vartab_h = NULL;
102102
time_t poll_interval = 2;
103103
static char *chroot_path = NULL, *user = NULL, *group = NULL;
104104
static int user_from_cmdline = 0, group_from_cmdline = 0;
105+
/* visible to driver implementations */
106+
int reconnect_max_tries = -1, reconnect_count = 0;
105107

106108
/* signal handling */
107109
int exit_flag = 0;
@@ -1397,6 +1399,18 @@ static int main_arg(char *var, char *val)
13971399
return 1; /* handled */
13981400
}
13991401

1402+
/* Allow per-driver overrides of the global setting
1403+
* and allow to reload this, why not. */
1404+
if (!strcmp(var, "reconnect_max_tries")) {
1405+
int intval = -1;
1406+
if ( str_to_int (val, &intval, 10) ) {
1407+
reconnect_max_tries = intval;
1408+
reconnect_count = 0;
1409+
} else {
1410+
upslogx(LOG_INFO, "WARNING : Invalid reconnect_max_tries value found in ups.conf global settings");
1411+
}
1412+
}
1413+
14001414
/* only for upsdrvctl - ignored here */
14011415
if (!strcmp(var, "sdorder"))
14021416
return 1; /* handled */
@@ -1571,6 +1585,18 @@ static void do_global_args(const char *var, const char *val)
15711585
return;
15721586
}
15731587

1588+
if (!strcmp(var, "reconnect_max_tries")) {
1589+
int intval = -1;
1590+
if ( str_to_int (val, &intval, 10) ) {
1591+
reconnect_max_tries = intval;
1592+
reconnect_count = 0;
1593+
} else {
1594+
upslogx(LOG_INFO, "WARNING : Invalid reconnect_max_tries value found in ups.conf global settings");
1595+
}
1596+
1597+
return;
1598+
}
1599+
15741600
/* Allow to specify its minimal debugging level for all drivers -
15751601
* admins can set more with command-line args, but can't set
15761602
* less without changing config. Should help debug of services.

drivers/main.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
extern const char *upsname, *device_name;
1515
extern char *device_path, *device_sdcommands;
1616
extern int broken_driver, experimental_driver,
17-
do_lock_port, exit_flag, handling_upsdrv_shutdown;
17+
do_lock_port, exit_flag, handling_upsdrv_shutdown,
18+
reconnect_max_tries, reconnect_count;
1819
extern TYPE_FD upsfd, extrafd;
1920
extern time_t poll_interval;
2021

0 commit comments

Comments
 (0)