@@ -42,6 +42,9 @@ auto main(int argc, char **argv) -> int {
4242 " p,pretty" , " indent json output with <arg> spaces (0 - 255)" , cxxopts::value<uint8_t >());
4343 options.add_options (" settings" )(
4444 " i,interval" , " repeat with given interval (milliseconds)" , cxxopts::value<unsigned >());
45+ options.add_options (" signal" )(" r,register-signal" ,
46+ " register for SIGUSR1 on writing modbus commands. Provide pid of modbus client." ,
47+ cxxopts::value<pid_t >());
4548
4649 options.add_options (" shared memory" )(" shmname" , " name of the shared memory to dump" , cxxopts::value<std::string>());
4750 options.add_options (" shared memory" )(" configfile" , " config file" , cxxopts::value<std::string>());
@@ -243,8 +246,9 @@ auto main(int argc, char **argv) -> int {
243246 << std::endl;
244247 };
245248
246- const bool cyclic = opts[" interval" ].count ();
247- if (cyclic) {
249+ const bool cyclic = opts[" interval" ].count ();
250+ const bool on_signal = opts[" register-signal" ].count ();
251+ if (cyclic || on_signal) {
248252 pid_t shm_owner_pid = 0 ;
249253 if (opts.count (" pid" ) == 0 ) {
250254 std::cerr << " Warning: No SHM owner PID provided.\n "
@@ -257,29 +261,42 @@ auto main(int argc, char **argv) -> int {
257261 shm_owner_pid = opts[" pid" ].as <pid_t >();
258262 }
259263
260- // check interval
261- const auto interval = opts[" interval" ].as <unsigned >();
262- static constexpr unsigned MIN_INTERVAL = 10 ;
263- if (interval < MIN_INTERVAL) {
264- std::cerr << " interval to short. (min: " << MIN_INTERVAL << " )\n " ;
265- return EX_USAGE;
264+ if (opts.count (" register-signal" ) > 0 ) {
265+ const auto modbus_client_pid = opts[" register-signal" ].as <pid_t >();
266+ int tmp = kill (modbus_client_pid, SIGUSR1);
267+ if (tmp == -1 ) {
268+ perror (" failed to send SIGUSR1 to modbus client" );
269+ return EX_OSERR;
270+ }
266271 }
267272
268273 // signal set
269274 sigset_t sleep_sigset;
270275 sigemptyset (&sleep_sigset);
271276 sigaddset (&sleep_sigset, SIGALRM);
277+ sigaddset (&sleep_sigset, SIGUSR1);
272278 sigprocmask (SIG_BLOCK, &sleep_sigset, nullptr );
273279 sigaddset (&sleep_sigset, SIGINT); // add to sigwait, but do not block
274280 sigaddset (&sleep_sigset, SIGTERM); // add to sigwait, but do not block
275281
276- // start timer
277- const struct timeval interval_time {
278- static_cast <__time_t >(interval / 1000 ), // NOLINT
282+ std::unique_ptr<cxxitimer::ITimer_Real> interval_timer;
283+ if (cyclic) {
284+ // check interval
285+ const auto interval = opts[" interval" ].as <unsigned >();
286+ static constexpr unsigned MIN_INTERVAL = 10 ;
287+ if (interval < MIN_INTERVAL) {
288+ std::cerr << " interval to short. (min: " << MIN_INTERVAL << " )\n " ;
289+ return EX_USAGE;
290+ }
291+
292+ // start timer
293+ const struct timeval interval_time {
294+ static_cast <__time_t >(interval / 1000 ), // NOLINT
279295 static_cast <__syscall_slong_t >((interval % 1000 ) * 1000 ) // NOLINT
280- };
281- cxxitimer::ITimer_Real interval_timer (interval_time);
282- interval_timer.start ();
296+ };
297+ interval_timer = std::make_unique<cxxitimer::ITimer_Real>(interval_time);
298+ interval_timer->start ();
299+ }
283300
284301 while (true ) {
285302 // execute
@@ -295,7 +312,7 @@ auto main(int argc, char **argv) -> int {
295312 exit (EX_OSERR);
296313 }
297314
298- if (sig != SIGALRM) break ;
315+ if (sig != SIGALRM && sig != SIGUSR1 ) break ;
299316
300317 // check shm owner pid
301318 if (shm_owner_pid) {
0 commit comments