<html><head></head><body><div style="color:#000; background-color:#fff; font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div src="mid://AH_NimIAADERWmHdKQi2eCdos8I/2" id="yui_3_16_0_ym19_1_1516313013807_35523"><div id="yui_3_16_0_ym19_1_1516313013807_35524"><div id="yui_3_16_0_ym19_1_1516313013807_35525"><div style="color:#000;background-color:#fff;font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;" id="yui_3_16_0_ym19_1_1516313013807_35526"><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_35527">I
am trying to use "sd_watchdog_enabled". If I run my service without
forking, the sd_watchdog_enabled function works as expected:<br id="yui_3_16_0_ym19_1_1516313013807_35528"><br id="yui_3_16_0_ym19_1_1516313013807_35529">Jan 18 15:05:29 thinkpad systemd[1]: Starting WaitonlyServer...<br id="yui_3_16_0_ym19_1_1516313013807_35530">Jan 18 15:05:30 thinkpad waitonly[11172]: PID before fork = 11172<br id="yui_3_16_0_ym19_1_1516313013807_35531">Jan 18 15:05:30 thinkpad waitonly[11172]: Return from lockme = 0<br id="yui_3_16_0_ym19_1_1516313013807_35532">Jan 18 15:05:30 thinkpad waitonly[11172]: PID to compare with watchdog_pid: 11172<br id="yui_3_16_0_ym19_1_1516313013807_35533">Jan 18 15:05:30 thinkpad waitonly[11172]: Return from watchdog = 1<br id="yui_3_16_0_ym19_1_1516313013807_35534"><br id="yui_3_16_0_ym19_1_1516313013807_35535">however if i run the forkme function (to put process in the background) it behaves as follows:<br id="yui_3_16_0_ym19_1_1516313013807_35536"><br id="yui_3_16_0_ym19_1_1516313013807_35537">Jan 18 15:06:25 thinkpad waitonly[11228]: Return from forkme = 11228<br id="yui_3_16_0_ym19_1_1516313013807_35538">Jan 18 15:06:25 thinkpad waitonly[11228]: Return from lockme = 0<br id="yui_3_16_0_ym19_1_1516313013807_35539">Jan 18 15:06:25 thinkpad waitonly[11228]: PID to compare with watchdog_pid: 11228<br id="yui_3_16_0_ym19_1_1516313013807_35540">Jan 18 15:06:25 thinkpad waitonly[11228]: systemd watchdog not enabled - not sending watchdog keepalives!<br id="yui_3_16_0_ym19_1_1516313013807_35541">Jan 18 15:06:25 thinkpad waitonly[11228]: systemd watchdog pid = -1905553534<br id="yui_3_16_0_ym19_1_1516313013807_35542">Jan 18 15:06:25 thinkpad waitonly[11228]: Return from watchdog = 0<br id="yui_3_16_0_ym19_1_1516313013807_35543"><br id="yui_3_16_0_ym19_1_1516313013807_35544"><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_35545">where am I going wrong? any help with this is greatly appreciated.<br id="yui_3_16_0_ym19_1_1516313013807_35546"></div><br id="yui_3_16_0_ym19_1_1516313013807_35547">thanks!<br id="yui_3_16_0_ym19_1_1516313013807_35548"><br id="yui_3_16_0_ym19_1_1516313013807_35549">main.c -------------------------------------------------------------------<br id="yui_3_16_0_ym19_1_1516313013807_35550"><br id="yui_3_16_0_ym19_1_1516313013807_35551">pid_t pid;<br id="yui_3_16_0_ym19_1_1516313013807_35552">int watchdogInfo = 0;<br id="yui_3_16_0_ym19_1_1516313013807_35553">int ret;<br id="yui_3_16_0_ym19_1_1516313013807_35554"><br id="yui_3_16_0_ym19_1_1516313013807_35555">int main() {<br id="yui_3_16_0_ym19_1_1516313013807_35556"><br id="yui_3_16_0_ym19_1_1516313013807_35557"> setlogmask (LOG_UPTO (LOG_INFO));<br id="yui_3_16_0_ym19_1_1516313013807_35558"> openlog (NULL, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);<br id="yui_3_16_0_ym19_1_1516313013807_35559"><br id="yui_3_16_0_ym19_1_1516313013807_35560"> pid = getpid();<br id="yui_3_16_0_ym19_1_1516313013807_35561"> syslog (LOG_NOTICE, "PID before fork = %d", pid);</div><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_35562"><br id="yui_3_16_0_ym19_1_1516313013807_35563"></div> ret = forkme();<br id="yui_3_16_0_ym19_1_1516313013807_35564"> syslog (LOG_NOTICE, "Return from forkme = %d", ret);<br id="yui_3_16_0_ym19_1_1516313013807_35565"><br id="yui_3_16_0_ym19_1_1516313013807_35566"> ret = lockme();<br id="yui_3_16_0_ym19_1_1516313013807_35567"> syslog (LOG_NOTICE, "Return from lockme = %d", ret);<br id="yui_3_16_0_ym19_1_1516313013807_35568"><br id="yui_3_16_0_ym19_1_1516313013807_35569"> pid = getpid();<br id="yui_3_16_0_ym19_1_1516313013807_35570"> syslog (LOG_NOTICE, "PID after fork = %d", pid);<br id="yui_3_16_0_ym19_1_1516313013807_35571"><br id="yui_3_16_0_ym19_1_1516313013807_35572"> watchdogInfo = systemd_get_watchdog_time();<br id="yui_3_16_0_ym19_1_1516313013807_35573"> syslog (LOG_NOTICE, "Return from watchdog = %d", watchdogInfo);<br id="yui_3_16_0_ym19_1_1516313013807_35574"><br id="yui_3_16_0_ym19_1_1516313013807_35575"> for (;;) { // Run forever<br id="yui_3_16_0_ym19_1_1516313013807_35576"> sleep(10);<br id="yui_3_16_0_ym19_1_1516313013807_35577"> syslog (LOG_NOTICE, "Service Running...");<br id="yui_3_16_0_ym19_1_1516313013807_35578">}<br id="yui_3_16_0_ym19_1_1516313013807_35579">}<br id="yui_3_16_0_ym19_1_1516313013807_35580"><br id="yui_3_16_0_ym19_1_1516313013807_35581">forkme.c -------------------------------------------------------------------<br id="yui_3_16_0_ym19_1_1516313013807_35582"><div id="yui_3_16_0_ym19_1_1516313013807_35583"><br id="yui_3_16_0_ym19_1_1516313013807_35584"></div><div id="yui_3_16_0_ym19_1_1516313013807_35585">pid_t pid;</div><br id="yui_3_16_0_ym19_1_1516313013807_35586">int forkme(void) {<br id="yui_3_16_0_ym19_1_1516313013807_35587"> if ((pid = fork()) < 0)<br id="yui_3_16_0_ym19_1_1516313013807_35588"> exit(1);<br id="yui_3_16_0_ym19_1_1516313013807_35589"> else if(pid != 0) /* parent */<br id="yui_3_16_0_ym19_1_1516313013807_35590"> exit(0);<br id="yui_3_16_0_ym19_1_1516313013807_35591"> setsid();<br id="yui_3_16_0_ym19_1_1516313013807_35592">}<br id="yui_3_16_0_ym19_1_1516313013807_35593"><div id="yui_3_16_0_ym19_1_1516313013807_35594"><br id="yui_3_16_0_ym19_1_1516313013807_35595"></div><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_35596">lockme.c -------------------------------------------------------------------<br id="yui_3_16_0_ym19_1_1516313013807_35597"></div><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_35598"><br id="yui_3_16_0_ym19_1_1516313013807_35599">#define LOCKFILE "/var/run/waitonly.pid"<br id="yui_3_16_0_ym19_1_1516313013807_35600">#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)<br id="yui_3_16_0_ym19_1_1516313013807_35601"><br id="yui_3_16_0_ym19_1_1516313013807_35602">int lockme(void)<br id="yui_3_16_0_ym19_1_1516313013807_35603">{<br id="yui_3_16_0_ym19_1_1516313013807_35604"> int fd;<br id="yui_3_16_0_ym19_1_1516313013807_35605"> char buf[16];<br id="yui_3_16_0_ym19_1_1516313013807_35606"> fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);<br id="yui_3_16_0_ym19_1_1516313013807_35607"> if (fd < 0) {<br id="yui_3_16_0_ym19_1_1516313013807_35608"> syslog(LOG_ERR, "can’t open %s: %s", LOCKFILE, strerror(errno));<br id="yui_3_16_0_ym19_1_1516313013807_35609"> exit(1);<br id="yui_3_16_0_ym19_1_1516313013807_35610"> }<br id="yui_3_16_0_ym19_1_1516313013807_35611"> ftruncate(fd, 0);<br id="yui_3_16_0_ym19_1_1516313013807_35612"> sprintf(buf, "%ld", (long)getpid());<br id="yui_3_16_0_ym19_1_1516313013807_35613"> write(fd, buf, strlen(buf)+1);<br id="yui_3_16_0_ym19_1_1516313013807_35614"> return(0);<br id="yui_3_16_0_ym19_1_1516313013807_35615">}<br id="yui_3_16_0_ym19_1_1516313013807_35616"><br id="yui_3_16_0_ym19_1_1516313013807_35617"></div>systemd.c -------------------------------------------------------------------<br id="yui_3_16_0_ym19_1_1516313013807_35618"><br id="yui_3_16_0_ym19_1_1516313013807_35619">// return watchdog - 0 means that watchdog is not enabled<br id="yui_3_16_0_ym19_1_1516313013807_35620">int systemd_get_watchdog_time(void)<br id="yui_3_16_0_ym19_1_1516313013807_35621">{<br id="yui_3_16_0_ym19_1_1516313013807_35622">uint64_t usec;<br id="yui_3_16_0_ym19_1_1516313013807_35623">char *watchdog = NULL;<br id="yui_3_16_0_ym19_1_1516313013807_35624">char *watchdog_pid = NULL;<br id="yui_3_16_0_ym19_1_1516313013807_35625">int ret;<br id="yui_3_16_0_ym19_1_1516313013807_35626"><br id="yui_3_16_0_ym19_1_1516313013807_35627">ret = sd_watchdog_enabled(0, &usec);<br id="yui_3_16_0_ym19_1_1516313013807_35628"><br id="yui_3_16_0_ym19_1_1516313013807_35629">if (ret == 0) {<br id="yui_3_16_0_ym19_1_1516313013807_35630"> syslog (LOG_NOTICE, "systemd watchdog not enabled - not sending watchdog keepalives!");<br id="yui_3_16_0_ym19_1_1516313013807_35631"> watchdog_pid = getenv("WATCHDOG_PID");<br id="yui_3_16_0_ym19_1_1516313013807_35632"> syslog (LOG_NOTICE, "systemd watchdog pid = %d", watchdog_pid);<br id="yui_3_16_0_ym19_1_1516313013807_35633">}<br id="yui_3_16_0_ym19_1_1516313013807_35634">if (ret < 0) {<br id="yui_3_16_0_ym19_1_1516313013807_35635"> syslog (LOG_NOTICE, "systemd watchdog returned error %d - not sending watchdog keepalives", ret);<br id="yui_3_16_0_ym19_1_1516313013807_35636">}<br id="yui_3_16_0_ym19_1_1516313013807_35637"><div id="yui_3_16_0_ym19_1_1516313013807_35638">}</div><div id="yui_3_16_0_ym19_1_1516313013807_35639"><br id="yui_3_16_0_ym19_1_1516313013807_35640"></div><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_35641">waitonly.service -------------------------------------------------------------------</div><div id="yui_3_16_0_ym19_1_1516313013807_35642"><br id="yui_3_16_0_ym19_1_1516313013807_35643"></div><div id="yui_3_16_0_ym19_1_1516313013807_35644">[Unit]</div>Description=WaitonlyServer <br id="yui_3_16_0_ym19_1_1516313013807_35645">After=syslog.target networking.service<br id="yui_3_16_0_ym19_1_1516313013807_35646">OnFailure=heartbeat-failed@%n.service<br id="yui_3_16_0_ym19_1_1516313013807_35647"><br id="yui_3_16_0_ym19_1_1516313013807_35648">[Service]<br id="yui_3_16_0_ym19_1_1516313013807_35649">Nice=-5<br id="yui_3_16_0_ym19_1_1516313013807_35650">Type=forking<br id="yui_3_16_0_ym19_1_1516313013807_35651">NotifyAccess=all<br id="yui_3_16_0_ym19_1_1516313013807_35652">StartLimitInterval=3m<br id="yui_3_16_0_ym19_1_1516313013807_35653">StartLimitBurst=3<br id="yui_3_16_0_ym19_1_1516313013807_35654">TimeoutSec=1m<br id="yui_3_16_0_ym19_1_1516313013807_35655">WatchdogSec=60s<br id="yui_3_16_0_ym19_1_1516313013807_35656">PIDFile=/var/run/waitonly.pid<br id="yui_3_16_0_ym19_1_1516313013807_35657">RestartSec=5<br id="yui_3_16_0_ym19_1_1516313013807_35658">Restart=on-abnormal<br id="yui_3_16_0_ym19_1_1516313013807_35659">LimitNOFILE=1024<br id="yui_3_16_0_ym19_1_1516313013807_35660">ExecStart=/usr/bin/waitonly<br id="yui_3_16_0_ym19_1_1516313013807_35661">ExecStop=/usr/bin/kill $MAINPID<br id="yui_3_16_0_ym19_1_1516313013807_35662">ExecReload=/usr/bin/kill -HUP $MAINPID<br id="yui_3_16_0_ym19_1_1516313013807_35663"><br id="yui_3_16_0_ym19_1_1516313013807_35664"><br id="yui_3_16_0_ym19_1_1516313013807_35665">[Install]<br id="yui_3_16_0_ym19_1_1516313013807_35666">WantedBy=multi-user.target<br id="yui_3_16_0_ym19_1_1516313013807_35667"></div></div></div></div> <span id="yui_3_16_0_ym19_1_1516313013807_35668"> <a tabindex="0" href="https://mail.yahoo.com/?soc_src=mail&soc_trk=ma" data-action="reply_sender" id="yui_3_16_0_ym19_1_1516313013807_35669"> </a></span></div></body></html>