<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 dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_43624">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_43625"><br id="yui_3_16_0_ym19_1_1516313013807_43626">Jan 18 15:05:29 thinkpad systemd[1]: Starting WaitonlyServer...<br id="yui_3_16_0_ym19_1_1516313013807_43627">Jan 18 15:05:30 thinkpad waitonly[11172]: PID before fork = 11172<br id="yui_3_16_0_ym19_1_1516313013807_43628">Jan 18 15:05:30 thinkpad waitonly[11172]: Return from lockme = 0<br id="yui_3_16_0_ym19_1_1516313013807_43629">Jan 18 15:05:30 thinkpad waitonly[11172]: PID to compare with watchdog_pid: 11172<br id="yui_3_16_0_ym19_1_1516313013807_43630">Jan 18 15:05:30 thinkpad waitonly[11172]: Return from watchdog = 1<br id="yui_3_16_0_ym19_1_1516313013807_43631"><br id="yui_3_16_0_ym19_1_1516313013807_43632">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_43633"><br id="yui_3_16_0_ym19_1_1516313013807_43634">Jan 18 15:06:25 thinkpad waitonly[11228]: Return from forkme = 11228<br id="yui_3_16_0_ym19_1_1516313013807_43635">Jan 18 15:06:25 thinkpad waitonly[11228]: Return from lockme = 0<br id="yui_3_16_0_ym19_1_1516313013807_43636">Jan 18 15:06:25 thinkpad waitonly[11228]: PID to compare with watchdog_pid: 11228<br id="yui_3_16_0_ym19_1_1516313013807_43637">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_43638">Jan 18 15:06:25 thinkpad waitonly[11228]: systemd watchdog pid = -1905553534<br id="yui_3_16_0_ym19_1_1516313013807_43639">Jan 18 15:06:25 thinkpad waitonly[11228]: Return from watchdog = 0<br id="yui_3_16_0_ym19_1_1516313013807_43640"><br id="yui_3_16_0_ym19_1_1516313013807_43641"><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_43642">where am I going wrong? any help with this is greatly appreciated.<br id="yui_3_16_0_ym19_1_1516313013807_43643"></div><br id="yui_3_16_0_ym19_1_1516313013807_43644">thanks!<br id="yui_3_16_0_ym19_1_1516313013807_43645"><br id="yui_3_16_0_ym19_1_1516313013807_43646">main.c -------------------------------------------------------------------<br id="yui_3_16_0_ym19_1_1516313013807_43647"><br id="yui_3_16_0_ym19_1_1516313013807_43648">pid_t pid;<br id="yui_3_16_0_ym19_1_1516313013807_43649">int watchdogInfo = 0;<br id="yui_3_16_0_ym19_1_1516313013807_43650">int ret;<br id="yui_3_16_0_ym19_1_1516313013807_43651"><br id="yui_3_16_0_ym19_1_1516313013807_43652">int main() {<br id="yui_3_16_0_ym19_1_1516313013807_43653"><br id="yui_3_16_0_ym19_1_1516313013807_43654"> setlogmask (LOG_UPTO (LOG_INFO));<br id="yui_3_16_0_ym19_1_1516313013807_43655"> openlog (NULL, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);<br id="yui_3_16_0_ym19_1_1516313013807_43656"><br id="yui_3_16_0_ym19_1_1516313013807_43657"> pid = getpid();<br id="yui_3_16_0_ym19_1_1516313013807_43658"> syslog (LOG_NOTICE, "PID before fork = %d", pid);</div><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_43659"><br id="yui_3_16_0_ym19_1_1516313013807_43660"></div> ret = forkme();<br id="yui_3_16_0_ym19_1_1516313013807_43661"> syslog (LOG_NOTICE, "Return from forkme = %d", ret);<br id="yui_3_16_0_ym19_1_1516313013807_43662"><br id="yui_3_16_0_ym19_1_1516313013807_43663"> ret = lockme();<br id="yui_3_16_0_ym19_1_1516313013807_43664"> syslog (LOG_NOTICE, "Return from lockme = %d", ret);<br id="yui_3_16_0_ym19_1_1516313013807_43665"><br id="yui_3_16_0_ym19_1_1516313013807_43666"> pid = getpid();<br id="yui_3_16_0_ym19_1_1516313013807_43667"> syslog (LOG_NOTICE, "PID after fork = %d", pid);<br id="yui_3_16_0_ym19_1_1516313013807_43668"><br id="yui_3_16_0_ym19_1_1516313013807_43669"> watchdogInfo = systemd_get_watchdog_time();<br id="yui_3_16_0_ym19_1_1516313013807_43670"> syslog (LOG_NOTICE, "Return from watchdog = %d", watchdogInfo);<br id="yui_3_16_0_ym19_1_1516313013807_43671"><br id="yui_3_16_0_ym19_1_1516313013807_43672"> for (;;) { // Run forever<br id="yui_3_16_0_ym19_1_1516313013807_43673"> sleep(10);<br id="yui_3_16_0_ym19_1_1516313013807_43674"> syslog (LOG_NOTICE, "Service Running...");<br id="yui_3_16_0_ym19_1_1516313013807_43675">}<br id="yui_3_16_0_ym19_1_1516313013807_43676">}<br id="yui_3_16_0_ym19_1_1516313013807_43677"><br id="yui_3_16_0_ym19_1_1516313013807_43678">forkme.c -------------------------------------------------------------------<br id="yui_3_16_0_ym19_1_1516313013807_43679"><div id="yui_3_16_0_ym19_1_1516313013807_43680"><br id="yui_3_16_0_ym19_1_1516313013807_43681"></div><div id="yui_3_16_0_ym19_1_1516313013807_43682">pid_t pid;</div><br id="yui_3_16_0_ym19_1_1516313013807_43683">int forkme(void) {<br id="yui_3_16_0_ym19_1_1516313013807_43684"> if ((pid = fork()) < 0)<br id="yui_3_16_0_ym19_1_1516313013807_43685"> exit(1);<br id="yui_3_16_0_ym19_1_1516313013807_43686"> else if(pid != 0) /* parent */<br id="yui_3_16_0_ym19_1_1516313013807_43687"> exit(0);<br id="yui_3_16_0_ym19_1_1516313013807_43688"> setsid();<br id="yui_3_16_0_ym19_1_1516313013807_43689">}<br id="yui_3_16_0_ym19_1_1516313013807_43690"><div id="yui_3_16_0_ym19_1_1516313013807_43691"><br id="yui_3_16_0_ym19_1_1516313013807_43692"></div><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_43693">lockme.c -------------------------------------------------------------------<br id="yui_3_16_0_ym19_1_1516313013807_43694"></div><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_43695"><br id="yui_3_16_0_ym19_1_1516313013807_43696">#define LOCKFILE "/var/run/waitonly.pid"<br id="yui_3_16_0_ym19_1_1516313013807_43697">#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)<br id="yui_3_16_0_ym19_1_1516313013807_43698"><br id="yui_3_16_0_ym19_1_1516313013807_43699">int lockme(void)<br id="yui_3_16_0_ym19_1_1516313013807_43700">{<br id="yui_3_16_0_ym19_1_1516313013807_43701"> int fd;<br id="yui_3_16_0_ym19_1_1516313013807_43702"> char buf[16];<br id="yui_3_16_0_ym19_1_1516313013807_43703"> fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);<br id="yui_3_16_0_ym19_1_1516313013807_43704"> if (fd < 0) {<br id="yui_3_16_0_ym19_1_1516313013807_43705"> syslog(LOG_ERR, "can’t open %s: %s", LOCKFILE, strerror(errno));<br id="yui_3_16_0_ym19_1_1516313013807_43706"> exit(1);<br id="yui_3_16_0_ym19_1_1516313013807_43707"> }<br id="yui_3_16_0_ym19_1_1516313013807_43708"> ftruncate(fd, 0);<br id="yui_3_16_0_ym19_1_1516313013807_43709"> sprintf(buf, "%ld", (long)getpid());<br id="yui_3_16_0_ym19_1_1516313013807_43710"> write(fd, buf, strlen(buf)+1);<br id="yui_3_16_0_ym19_1_1516313013807_43711"> return(0);<br id="yui_3_16_0_ym19_1_1516313013807_43712">}<br id="yui_3_16_0_ym19_1_1516313013807_43713"><br id="yui_3_16_0_ym19_1_1516313013807_43714"></div>systemd.c -------------------------------------------------------------------<br id="yui_3_16_0_ym19_1_1516313013807_43715"><br id="yui_3_16_0_ym19_1_1516313013807_43716">// return watchdog - 0 means that watchdog is not enabled<br id="yui_3_16_0_ym19_1_1516313013807_43717">int systemd_get_watchdog_time(void)<br id="yui_3_16_0_ym19_1_1516313013807_43718">{<br id="yui_3_16_0_ym19_1_1516313013807_43719">uint64_t usec;<br id="yui_3_16_0_ym19_1_1516313013807_43720">char *watchdog = NULL;<br id="yui_3_16_0_ym19_1_1516313013807_43721">char *watchdog_pid = NULL;<br id="yui_3_16_0_ym19_1_1516313013807_43722">int ret;<br id="yui_3_16_0_ym19_1_1516313013807_43723"><br id="yui_3_16_0_ym19_1_1516313013807_43724">ret = sd_watchdog_enabled(0, &usec);<br id="yui_3_16_0_ym19_1_1516313013807_43725"><br id="yui_3_16_0_ym19_1_1516313013807_43726">if (ret == 0) {<br id="yui_3_16_0_ym19_1_1516313013807_43727"> syslog (LOG_NOTICE, "systemd watchdog not enabled - not sending watchdog keepalives!");<br id="yui_3_16_0_ym19_1_1516313013807_43728"> watchdog_pid = getenv("WATCHDOG_PID");<br id="yui_3_16_0_ym19_1_1516313013807_43729"> syslog (LOG_NOTICE, "systemd watchdog pid = %d", watchdog_pid);<br id="yui_3_16_0_ym19_1_1516313013807_43730">}<br id="yui_3_16_0_ym19_1_1516313013807_43731">if (ret < 0) {<br id="yui_3_16_0_ym19_1_1516313013807_43732"> syslog (LOG_NOTICE, "systemd watchdog returned error %d - not sending watchdog keepalives", ret);<br id="yui_3_16_0_ym19_1_1516313013807_43733">}<br id="yui_3_16_0_ym19_1_1516313013807_43734"><div id="yui_3_16_0_ym19_1_1516313013807_43735">}</div><div id="yui_3_16_0_ym19_1_1516313013807_43736"><br id="yui_3_16_0_ym19_1_1516313013807_43737"></div><div dir="ltr" id="yui_3_16_0_ym19_1_1516313013807_43738">waitonly.service -------------------------------------------------------------------</div><div id="yui_3_16_0_ym19_1_1516313013807_43739"><br id="yui_3_16_0_ym19_1_1516313013807_43740"></div><div id="yui_3_16_0_ym19_1_1516313013807_43741">[Unit]</div>Description=WaitonlyServer <br id="yui_3_16_0_ym19_1_1516313013807_43742">After=syslog.target networking.service<br id="yui_3_16_0_ym19_1_1516313013807_43743">OnFailure=heartbeat-failed@%n.service<br id="yui_3_16_0_ym19_1_1516313013807_43744"><br id="yui_3_16_0_ym19_1_1516313013807_43745">[Service]<br id="yui_3_16_0_ym19_1_1516313013807_43746">Nice=-5<br id="yui_3_16_0_ym19_1_1516313013807_43747">Type=forking<br id="yui_3_16_0_ym19_1_1516313013807_43748">NotifyAccess=all<br id="yui_3_16_0_ym19_1_1516313013807_43749">StartLimitInterval=3m<br id="yui_3_16_0_ym19_1_1516313013807_43750">StartLimitBurst=3<br id="yui_3_16_0_ym19_1_1516313013807_43751">TimeoutSec=1m<br id="yui_3_16_0_ym19_1_1516313013807_43752">WatchdogSec=60s<br id="yui_3_16_0_ym19_1_1516313013807_43753">PIDFile=/var/run/waitonly.pid<br id="yui_3_16_0_ym19_1_1516313013807_43754">RestartSec=5<br id="yui_3_16_0_ym19_1_1516313013807_43755">Restart=on-abnormal<br id="yui_3_16_0_ym19_1_1516313013807_43756">LimitNOFILE=1024<br id="yui_3_16_0_ym19_1_1516313013807_43757">ExecStart=/usr/bin/waitonly<br id="yui_3_16_0_ym19_1_1516313013807_43758">ExecStop=/usr/bin/kill $MAINPID<br id="yui_3_16_0_ym19_1_1516313013807_43759">ExecReload=/usr/bin/kill -HUP $MAINPID<br id="yui_3_16_0_ym19_1_1516313013807_43760"><br id="yui_3_16_0_ym19_1_1516313013807_43761"><br id="yui_3_16_0_ym19_1_1516313013807_43762">[Install]<br id="yui_3_16_0_ym19_1_1516313013807_43763">WantedBy=multi-user.target</div></body></html>