Skip to content

Commit a9f9f50

Browse files
systemd_watchdog: systemd watchdog object and thread_factory cleanups
Static code analysis fix (preparation) _004 (Issue Avnu#40) Make SystemdWatchdogHandler object to be created in main() for proper cleanup during exit. watchodg_setup() changed to a public member of SystemdWatchdogHandler class.
1 parent 0baef8a commit a9f9f50

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

linux/src/daemon_cl.cpp

+14-31
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#include <string.h>
5959

6060
#ifdef SYSTEMD_WATCHDOG
61-
#include <watchdog.hpp>
61+
#include "watchdog.hpp"
6262
#endif
6363

6464
#define PHY_DELAY_GB_TX_I20 184 //1G delay
@@ -100,34 +100,6 @@ void print_usage( char *arg0 ) {
100100
);
101101
}
102102

103-
int watchdog_setup(OSThreadFactory *thread_factory)
104-
{
105-
#ifdef SYSTEMD_WATCHDOG
106-
SystemdWatchdogHandler *watchdog = new SystemdWatchdogHandler();
107-
OSThread *watchdog_thread = thread_factory->createThread();
108-
int watchdog_result;
109-
long unsigned int watchdog_interval;
110-
watchdog_interval = watchdog->getSystemdWatchdogInterval(&watchdog_result);
111-
if (watchdog_result) {
112-
GPTP_LOG_INFO("Watchtog interval read from service file: %lu us", watchdog_interval);
113-
watchdog->update_interval = watchdog_interval / 2;
114-
GPTP_LOG_STATUS("Starting watchdog handler (Update every: %lu us)", watchdog->update_interval);
115-
watchdog_thread->start(watchdogUpdateThreadFunction, watchdog);
116-
return 0;
117-
} else if (watchdog_result < 0) {
118-
GPTP_LOG_ERROR("Watchdog settings read error.");
119-
delete watchdog;
120-
return -1;
121-
} else {
122-
GPTP_LOG_STATUS("Watchdog disabled");
123-
delete watchdog;
124-
return 0;
125-
}
126-
#else
127-
return 0;
128-
#endif
129-
}
130-
131103
static IEEE1588Clock *pClock = NULL;
132104
static EtherPort *pPort = NULL;
133105

@@ -156,7 +128,6 @@ int main(int argc, char **argv)
156128
memset(config_file_path, 0, 512);
157129

158130
GPTPPersist *pGPTPPersist = NULL;
159-
LinuxThreadFactory *thread_factory = new LinuxThreadFactory();
160131

161132
// Block SIGUSR1
162133
{
@@ -171,10 +142,22 @@ int main(int argc, char **argv)
171142

172143
GPTP_LOG_REGISTER();
173144
GPTP_LOG_INFO("gPTP starting");
174-
if (watchdog_setup(thread_factory) != 0) {
145+
146+
LinuxThreadFactory *thread_factory = new LinuxThreadFactory();
147+
148+
#ifdef SYSTEMD_WATCHDOG
149+
SystemdWatchdogHandler *watchdog = new SystemdWatchdogHandler();
150+
if (watchdog->watchdog_setup(thread_factory) != 0) {
175151
GPTP_LOG_ERROR("Watchdog handler setup error");
152+
delete watchdog;
153+
delete thread_factory;
176154
return -1;
177155
}
156+
#else
157+
// dummy pointer for CLEANUP macro
158+
int *watchdog = NULL;
159+
#endif
160+
178161
phy_delay_map_t ether_phy_delay;
179162
bool input_delay=false;
180163

linux/src/watchdog.cpp

+38-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "watchdog.hpp"
2-
#include "avbts_osthread.hpp"
32
#include "gptp_log.hpp"
43
#include <systemd/sd-daemon.h>
54

@@ -11,17 +10,23 @@ OSThreadExitCode watchdogUpdateThreadFunction(void *arg)
1110
return osthread_ok;
1211
}
1312

13+
bool SystemdWatchdogHandler::startSystemdWatchdogThread(OSThreadFactory *thread_factory)
14+
{
15+
watchdog_thread = thread_factory->createThread();
16+
return watchdog_thread->start(watchdogUpdateThreadFunction, this);
17+
}
1418

1519
SystemdWatchdogHandler::SystemdWatchdogHandler()
1620
{
1721
GPTP_LOG_INFO("Creating Systemd watchdog handler.");
1822
LinuxTimerFactory timer_factory = LinuxTimerFactory();
19-
timer = timer_factory.createTimer();
23+
watchdog_timer = timer_factory.createTimer();
2024
}
2125

2226
SystemdWatchdogHandler::~SystemdWatchdogHandler()
2327
{
24-
//Do nothing
28+
delete watchdog_timer;
29+
delete watchdog_thread;
2530
}
2631

2732
long unsigned int
@@ -39,8 +44,37 @@ void SystemdWatchdogHandler::run_update()
3944
GPTP_LOG_DEBUG("NOTIFYING WATCHDOG.");
4045
sd_notify(0, "WATCHDOG=1");
4146
GPTP_LOG_DEBUG("GOING TO SLEEP %lld", update_interval);
42-
timer->sleep(update_interval);
47+
watchdog_timer->sleep(update_interval);
4348
GPTP_LOG_DEBUG("WATCHDOG WAKE UP");
4449
}
4550
}
4651

52+
int SystemdWatchdogHandler::watchdog_setup(OSThreadFactory *thread_factory)
53+
{
54+
int watchdog_result;
55+
long unsigned int watchdog_interval;
56+
57+
if (!thread_factory) {
58+
GPTP_LOG_ERROR("Watchog setup invalid argument (thread_factory)");
59+
return -1;
60+
}
61+
62+
watchdog_interval = getSystemdWatchdogInterval(&watchdog_result);
63+
if (watchdog_result) {
64+
GPTP_LOG_INFO("Watchdog interval read from service file: %lu us", watchdog_interval);
65+
update_interval = watchdog_interval / 2;
66+
GPTP_LOG_STATUS("Starting watchdog handler (Update every: %lu us)", update_interval);
67+
if (startSystemdWatchdogThread(thread_factory)) {
68+
return 0;
69+
} else {
70+
GPTP_LOG_ERROR("Starting watchdog thread failed");
71+
return -1;
72+
}
73+
} else if (watchdog_result < 0) {
74+
GPTP_LOG_ERROR("Watchdog settings read error.");
75+
return -1;
76+
} else {
77+
GPTP_LOG_STATUS("Watchdog disabled");
78+
return 0;
79+
}
80+
}

linux/src/watchdog.hpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
#define SYSTEMDWATCHDOGHANDLER_H
33
#include <linux_hal_common.hpp>
44
#include <avbts_ostimer.hpp>
5-
5+
#include <avbts_osthread.hpp>
66

77
OSThreadExitCode watchdogUpdateThreadFunction(void *arg);
88

99
class SystemdWatchdogHandler
1010
{
1111
public:
12-
long unsigned int update_interval;
13-
long unsigned int getSystemdWatchdogInterval(int *result);
12+
int watchdog_setup(OSThreadFactory *thread_factory);
1413
void run_update();
1514
SystemdWatchdogHandler();
1615
virtual ~SystemdWatchdogHandler();
1716
private:
18-
OSTimer *timer;
17+
OSTimer *watchdog_timer;
18+
OSThread *watchdog_thread;
19+
long unsigned int update_interval;
20+
long unsigned int getSystemdWatchdogInterval(int *result);
21+
bool startSystemdWatchdogThread(OSThreadFactory *thread_factory);
1922
};
2023

2124
#endif // SYSTEMDWATCHDOGHANDLER_H

0 commit comments

Comments
 (0)