+ /*
+ * If no signals are set, do not start the heartbeat because
+ * it gives a constant stream of TIMEOUT_SIGNAL signals that
+ * make debugging impossible.
+ */
+ if (!no_signals) {
+ jcr->hb_bsock = NULL;
+ jcr->hb_started = false;
+ jcr->hb_dir_bsock = NULL;
+ pthread_create(&jcr->heartbeat_id, NULL, sd_heartbeat_thread, (void *)jcr);
+ }
+}
+
+/* Terminate the heartbeat thread. Used for both SD and DIR */
+void stop_heartbeat_monitor(JCR *jcr)
+{
+ int cnt = 0;
+ if (no_signals) {
+ return;
+ }
+ /* Wait max 10 secs for heartbeat thread to start */
+ while (!jcr->hb_started && cnt++ < 200) {
+ bmicrosleep(0, 50000); /* wait for start */
+ }
+
+ if (jcr->hb_started) {
+ jcr->hb_bsock->set_timed_out(); /* set timed_out to terminate read */
+ jcr->hb_bsock->set_terminated(); /* set to terminate read */
+ }
+ if (jcr->hb_dir_bsock) {
+ jcr->hb_dir_bsock->set_timed_out(); /* set timed_out to terminate read */
+ jcr->hb_dir_bsock->set_terminated(); /* set to terminate read */
+ }
+ if (jcr->hb_started) {
+ Dmsg0(100, "Send kill to heartbeat id\n");
+ pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL); /* make heartbeat thread go away */
+ bmicrosleep(0, 50000);
+ }
+ cnt = 0;
+ /* Wait max 100 secs for heartbeat thread to stop */
+ while (jcr->hb_started && cnt++ < 200) {
+ pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL); /* make heartbeat thread go away */
+ bmicrosleep(0, 500000);
+ }
+}
+
+/*
+ * Thread for sending heartbeats to the Director when there
+ * is no SD monitoring needed -- e.g. restore and verify Vol
+ * both do their own read() on the SD socket.
+ */
+extern "C" void *dir_heartbeat_thread(void *arg)
+{
+ JCR *jcr = (JCR *)arg;
+ BSOCK *dir;
+ time_t last_heartbeat = time(NULL);
+
+ pthread_detach(pthread_self());
+
+ /* Get our own local copy */
+ dir = dup_bsock(jcr->dir_bsock);
+
+ jcr->hb_bsock = dir;
+ jcr->hb_started = true;
+
+ for ( ; !is_bnet_stop(dir); ) {
+ time_t now, next;
+
+ now = time(NULL);
+ next = now - last_heartbeat;
+ if (next >= me->heartbeat_interval) {
+ dir->signal(BNET_HEARTBEAT);
+ last_heartbeat = now;
+ }
+ bmicrosleep(next, 0);
+ }
+ dir->close();
+ jcr->hb_bsock = NULL;
+ jcr->hb_started = false;
+ return NULL;