Blame SOURCES/0001-Update-service-files-and-add-sd_notify-support-to-is.patch

3d2430
From 6896af7a1a076302470db07ac9e6e91eee8988df Mon Sep 17 00:00:00 2001
3d2430
From: Lee Duncan <lduncan@suse.com>
3d2430
Date: Tue, 16 Oct 2018 13:53:09 -0700
3d2430
Subject: [PATCH 1/1] Update service files and add sd_notify support to iscsid
3d2430
 and iscsiuio
3d2430
3d2430
Cherry Picks the following changes from upstream:
3d2430
3d2430
  * Added service file for iscsi logins
3d2430
  * Use sd_notify() to tell systemd when iscsid is ready.
3d2430
  * Update systemd unit files for iscsid
3d2430
  * Use pkg-config in Makefiles for newer libraries.
3d2430
  * Improve daemon synchronization, fix err msgs
3d2430
  * Fix pipe notification code
3d2430
  * Add systemd support for iscsiuio
3d2430
  * Make iscsid systemd usage optional
3d2430
  * Handle systemd disablement correctly in iscsiuio
3d2430
---
3d2430
 Makefile                     | 10 +++++-
3d2430
 etc/systemd/iscsi.service    | 21 +++++-------
3d2430
 etc/systemd/iscsid.service   | 12 +++----
3d2430
 etc/systemd/iscsiuio.service |  8 +++--
3d2430
 iscsiuio/configure.ac        |  8 +++++
3d2430
 iscsiuio/src/unix/logger.c   |  2 +-
3d2430
 iscsiuio/src/unix/main.c     | 49 +++++++++++++++++++++-----
3d2430
 usr/Makefile                 | 14 ++++++--
3d2430
 usr/event_poll.c             | 21 ++++++++++--
3d2430
 usr/event_poll.h             |  1 +
3d2430
 usr/iscsi_sysfs.c            | 44 ++++++++++++++++++++++++
3d2430
 usr/iscsi_sysfs.h            |  1 +
3d2430
 usr/iscsid.c                 | 66 +++++++++++++++++++++++++++++-------
3d2430
 13 files changed, 208 insertions(+), 49 deletions(-)
3d2430
3d2430
diff --git a/Makefile b/Makefile
3d2430
index 03175a3..340c457 100644
3d2430
--- a/Makefile
3d2430
+++ b/Makefile
3d2430
@@ -31,6 +31,14 @@ ifneq (,$(CFLAGS))
3d2430
 export CFLAGS
3d2430
 endif
3d2430
 
3d2430
+# export systemd disablement if set
3d2430
+ifneq ($(NO_SYSTEMD),)
3d2430
+export NO_SYSTEMD
3d2430
+WITHOUT_ARG = --without-systemd
3d2430
+else
3d2430
+WITHOUT_ARG =
3d2430
+endif
3d2430
+
3d2430
 # Random comments:
3d2430
 # using '$(MAKE)' instead of just 'make' allows make to run in parallel
3d2430
 # over multiple makefile.
3d2430
@@ -59,7 +67,7 @@ utils/open-isns/Makefile: utils/open-isns/configure utils/open-isns/Makefile.in
3d2430
 	cd utils/open-isns; ./configure --with-security=no
3d2430
 
3d2430
 iscsiuio/Makefile: iscsiuio/configure iscsiuio/Makefile.in
3d2430
-	cd iscsiuio; ./configure
3d2430
+	cd iscsiuio; ./configure $(WITHOUT_ARG)
3d2430
 
3d2430
 iscsiuio/configure iscsiuio/Makefile.in: iscsiuio/configure.ac iscsiuio/Makefile.am
3d2430
 	cd iscsiuio; autoreconf --install
3d2430
diff --git a/etc/systemd/iscsi.service b/etc/systemd/iscsi.service
3d2430
index 2736956..0edcf51 100644
3d2430
--- a/etc/systemd/iscsi.service
3d2430
+++ b/etc/systemd/iscsi.service
3d2430
@@ -1,20 +1,17 @@
3d2430
 [Unit]
3d2430
 Description=Login and scanning of iSCSI devices
3d2430
-Documentation=man:iscsid(8) man:iscsiadm(8)
3d2430
-DefaultDependencies=no
3d2430
-Conflicts=shutdown.target
3d2430
-After=systemd-remount-fs.service network.target iscsid.service iscsiuio.service
3d2430
-Before=remote-fs-pre.target
3d2430
-Wants=remote-fs-pre.target iscsi-shutdown.service
3d2430
-ConditionDirectoryNotEmpty=|/var/lib/iscsi/nodes
3d2430
-ConditionDirectoryNotEmpty=|/sys/class/iscsi_session
3d2430
+Documentation=man:iscsiadm(8) man:iscsid(8)
3d2430
+Before=remote-fs.target
3d2430
+After=network.target network-online.target iscsid.service
3d2430
+ConditionPathExists=/etc/iscsi/initiatorname.iscsi
3d2430
 
3d2430
 [Service]
3d2430
 Type=oneshot
3d2430
-RemainAfterExit=true
3d2430
-ExecStart=-/usr/libexec/iscsi-mark-root-nodes
3d2430
 ExecStart=-/sbin/iscsiadm -m node --loginall=automatic
3d2430
-ExecReload=-/sbin/iscsiadm -m node --loginall=automatic
3d2430
+ExecStop=/sbin/iscsiadm -m node --logoutall=automatic
3d2430
+ExecStop=/sbin/iscsiadm -m node --logoutall=manual
3d2430
+SuccessExitStatus=21
3d2430
+RemainAfterExit=true
3d2430
 
3d2430
 [Install]
3d2430
-WantedBy=sysinit.target
3d2430
+WantedBy=remote-fs.target
3d2430
diff --git a/etc/systemd/iscsid.service b/etc/systemd/iscsid.service
3d2430
index 653dd08..f5e8979 100644
3d2430
--- a/etc/systemd/iscsid.service
3d2430
+++ b/etc/systemd/iscsid.service
3d2430
@@ -1,16 +1,16 @@
3d2430
 [Unit]
3d2430
 Description=Open-iSCSI
3d2430
-Documentation=man:iscsid(8) man:iscsiadm(8)
3d2430
+Documentation=man:iscsid(8) man:iscsiuio(8) man:iscsiadm(8)
3d2430
 DefaultDependencies=no
3d2430
-Conflicts=shutdown.target
3d2430
 After=network.target iscsiuio.service
3d2430
 Before=remote-fs-pre.target
3d2430
 
3d2430
 [Service]
3d2430
-Type=forking
3d2430
-PIDFile=/var/run/iscsid.pid
3d2430
-ExecStart=/usr/sbin/iscsid
3d2430
-ExecStop=/sbin/iscsiadm -k 0 2
3d2430
+Type=notify
3d2430
+NotifyAccess=main
3d2430
+ExecStart=/sbin/iscsid -f
3d2430
+KillMode=mixed
3d2430
 
3d2430
 [Install]
3d2430
 WantedBy=multi-user.target
3d2430
+Also=iscsid.socket
3d2430
diff --git a/etc/systemd/iscsiuio.service b/etc/systemd/iscsiuio.service
3d2430
index f0410b7..e4d9fd0 100644
3d2430
--- a/etc/systemd/iscsiuio.service
3d2430
+++ b/etc/systemd/iscsiuio.service
3d2430
@@ -9,9 +9,11 @@ After=network.target
3d2430
 Before=remote-fs-pre.target iscsid.service
3d2430
 
3d2430
 [Service]
3d2430
-Type=forking
3d2430
-PIDFile=/var/run/iscsiuio.pid
3d2430
-ExecStart=/usr/sbin/iscsiuio
3d2430
+Type=notify
3d2430
+NotifyAccess=main
3d2430
+ExecStart=/sbin/iscsiuio -f
3d2430
+KillMode=mixed
3d2430
+Restart=on-failure
3d2430
 
3d2430
 [Install]
3d2430
 WantedBy=multi-user.target
3d2430
diff --git a/iscsiuio/configure.ac b/iscsiuio/configure.ac
3d2430
index 7d53323..b41df0e 100644
3d2430
--- a/iscsiuio/configure.ac
3d2430
+++ b/iscsiuio/configure.ac
3d2430
@@ -63,6 +63,14 @@ AC_ARG_ENABLE(debug,
3d2430
         CFLAGS="${CFLAGS} -g -O0"
3d2430
     fi])
3d2430
 AM_CONDITIONAL([DEBUG], [test x$debug = xtrue])
3d2430
+## check for systemd support, default on
3d2430
+AC_ARG_WITH([systemd],
3d2430
+	    AS_HELP_STRING([--without-systemd], [Build without systemd]),
3d2430
+    [case "${withval}" in
3d2430
+     yes) LDFLAGS="`pkg-config --libs libsystemd`" ;;
3d2430
+     no)  CFLAGS="${CFLAGS} -DNO_SYSTEMD" ;;
3d2430
+     *)   AC_MSG_ERROR([bad value $withval for --with-systemd]) ;;
3d2430
+     esac],[LDFLAGS="`pkg-config --libs libsystemd`"])
3d2430
 
3d2430
 AC_CONFIG_COMMANDS([default],[[
3d2430
     if [ -n "$SOURCE_DATE_EPOCH" ] ; then
3d2430
diff --git a/iscsiuio/src/unix/logger.c b/iscsiuio/src/unix/logger.c
3d2430
index d41f9e8..87e16cd 100644
3d2430
--- a/iscsiuio/src/unix/logger.c
3d2430
+++ b/iscsiuio/src/unix/logger.c
3d2430
@@ -135,7 +135,7 @@ int init_logger(char *filename)
3d2430
 	}
3d2430
 	main_log.fp = fopen(filename, "a");
3d2430
 	if (main_log.fp == NULL) {
3d2430
-		printf("Could not create log file: %s <%s>\n",
3d2430
+		fprintf(stderr, "WARN: Could not create log file: %s <%s>\n",
3d2430
 		       filename, strerror(errno));
3d2430
 		rc = -EIO;
3d2430
 	}
3d2430
diff --git a/iscsiuio/src/unix/main.c b/iscsiuio/src/unix/main.c
3d2430
index 39d5339..188b96e 100644
3d2430
--- a/iscsiuio/src/unix/main.c
3d2430
+++ b/iscsiuio/src/unix/main.c
3d2430
@@ -48,6 +48,9 @@
3d2430
 #include <net/ethernet.h>
3d2430
 #include <arpa/inet.h>
3d2430
 #include <sys/mman.h>
3d2430
+#ifndef	NO_SYSTEMD
3d2430
+#include <systemd/sd-daemon.h>
3d2430
+#endif
3d2430
 
3d2430
 #include "uip.h"
3d2430
 #include "uip_arp.h"
3d2430
@@ -146,7 +149,7 @@ signal_wait:
3d2430
 		fini_logger(SHUTDOWN_LOGGER);
3d2430
 		rc = init_logger(main_log.log_file);
3d2430
 		if (rc != 0)
3d2430
-			printf("Could not initialize the logger in "
3d2430
+			fprintf(stderr, "WARN: Could not initialize the logger in "
3d2430
 			       "signal!\n");
3d2430
 		goto signal_wait;
3d2430
 	default:
3d2430
@@ -239,6 +242,7 @@ int main(int argc, char *argv[])
3d2430
 	int foreground = 0;
3d2430
 	pid_t pid;
3d2430
 	pthread_attr_t attr;
3d2430
+	int pipefds[2];
3d2430
 
3d2430
 	/*  Record the start time for the user space daemon */
3d2430
 	opt.start_time = time(NULL);
3d2430
@@ -281,7 +285,7 @@ int main(int argc, char *argv[])
3d2430
 		/*  initialize the logger */
3d2430
 		rc = init_logger(main_log.log_file);
3d2430
 		if (rc != 0 && opt.debug == DEBUG_ON)
3d2430
-			printf("WARN: Could not initialize the logger\n");
3d2430
+			fprintf(stderr, "WARN: Could not initialize the logger\n");
3d2430
 	}
3d2430
 
3d2430
 	LOG_INFO("Started iSCSI uio stack: Ver " PACKAGE_VERSION);
3d2430
@@ -316,38 +320,53 @@ int main(int argc, char *argv[])
3d2430
 
3d2430
 		fd = open(pid_file, O_WRONLY | O_CREAT, 0644);
3d2430
 		if (fd < 0) {
3d2430
-			printf("Unable to create pid file: %s", pid_file);
3d2430
+			fprintf(stderr, "ERR: Unable to create pid file: %s\n",
3d2430
+				pid_file);
3d2430
+			exit(1);
3d2430
+		}
3d2430
+
3d2430
+		if (pipe(pipefds) < 0) {
3d2430
+			fprintf(stderr, "ERR: Unable to create a PIPE: %s\n",
3d2430
+				strerror(errno));
3d2430
 			exit(1);
3d2430
 		}
3d2430
 
3d2430
 		pid = fork();
3d2430
 		if (pid < 0) {
3d2430
-			printf("Starting daemon failed");
3d2430
+			fprintf(stderr, "ERR: Starting daemon failed\n");
3d2430
 			exit(1);
3d2430
 		} else if (pid) {
3d2430
+			char msgbuf[4];
3d2430
+
3d2430
+			/* parent: wait for child msg then exit */
3d2430
+			close(pipefds[1]);
3d2430
+			read(pipefds[0], msgbuf, sizeof(msgbuf));
3d2430
 			exit(0);
3d2430
 		}
3d2430
 
3d2430
+		/* the child */
3d2430
 		rc = chdir("/");
3d2430
 		if (rc == -1)
3d2430
-			printf("Unable to chdir(\") [%s]", strerror(errno));
3d2430
+			fprintf(stderr, "WARN: Unable to chdir(\") [%s]\n", strerror(errno));
3d2430
 
3d2430
 		if (lockf(fd, F_TLOCK, 0) < 0) {
3d2430
-			printf("Unable to lock pid file: %s [%s]",
3d2430
+			fprintf(stderr, "ERR: Unable to lock pid file: %s [%s]\n",
3d2430
 			       pid_file, strerror(errno));
3d2430
 			exit(1);
3d2430
 		}
3d2430
 
3d2430
 		rc = ftruncate(fd, 0);
3d2430
 		if (rc == -1)
3d2430
-			printf("ftruncate(%d, 0) failed [%s]",
3d2430
+			fprintf(stderr, "WARN: ftruncate(%d, 0) failed [%s]\n",
3d2430
 			       fd, strerror(errno));
3d2430
 
3d2430
 		sprintf(buf, "%d\n", getpid());
3d2430
 		written_bytes = write(fd, buf, strlen(buf));
3d2430
-		if (written_bytes == -1)
3d2430
-			printf("Could not write pid file [%s]",
3d2430
+		if (written_bytes == -1) {
3d2430
+			fprintf(stderr, "ERR: Could not write pid file [%s]\n",
3d2430
 			       strerror(errno));
3d2430
+			exit(1);
3d2430
+		}
3d2430
 		close(fd);
3d2430
 
3d2430
 		daemon_init();
3d2430
@@ -393,6 +412,18 @@ int main(int argc, char *argv[])
3d2430
 	if (rc != 0)
3d2430
 		goto error;
3d2430
 
3d2430
+	if (!foreground) {
3d2430
+		/* signal parent they can go away now */
3d2430
+		close(pipefds[0]);
3d2430
+		write(pipefds[1], "ok\n", 3);
3d2430
+		close(pipefds[1]);
3d2430
+	}
3d2430
+
3d2430
+#ifndef	NO_SYSTEMD
3d2430
+	sd_notify(0, "READY=1\n"
3d2430
+		     "STATUS=Ready to process requests\n");
3d2430
+#endif
3d2430
+
3d2430
 	/*  NetLink connection to listen to NETLINK_ISCSI private messages */
3d2430
 	if (nic_nl_open() != 0)
3d2430
 		goto error;
3d2430
diff --git a/usr/Makefile b/usr/Makefile
3d2430
index 865d001..742a0fb 100644
3d2430
--- a/usr/Makefile
3d2430
+++ b/usr/Makefile
3d2430
@@ -28,10 +28,18 @@ IPC_OBJ=ioctl.o
3d2430
 endif
3d2430
 endif
3d2430
 
3d2430
+PKG_CONFIG = /usr/bin/pkg-config
3d2430
+
3d2430
 CFLAGS ?= -O2 -g
3d2430
 WARNFLAGS ?= -Wall -Wstrict-prototypes
3d2430
 CFLAGS += $(WARNFLAGS) -I../include -I. -I../utils/open-isns \
3d2430
 				-D$(OSNAME) $(IPC_CFLAGS) -DISNS_ENABLE
3d2430
+CFLAGS += $(shell $(PKG_CONFIG) --cflags libkmod)
3d2430
+CFLAGS += $(shell $(PKG_CONFIG) --cflags libsystemd)
3d2430
+LDFLAGS += $(shell $(PKG_CONFIG) --libs libkmod)
3d2430
+ifeq ($(NO_SYSTEMD),)
3d2430
+LDFLAGS += $(shell $(PKG_CONFIG) --libs libsystemd)
3d2430
+endif
3d2430
 PROGRAMS = iscsid iscsiadm iscsistart
3d2430
 
3d2430
 # libc compat files
3d2430
@@ -55,14 +63,14 @@ all: $(PROGRAMS)
3d2430
 
3d2430
 iscsid: $(ISCSI_LIB_SRCS) $(INITIATOR_SRCS) $(DISCOVERY_SRCS) \
3d2430
 	iscsid.o session_mgmt.o discoveryd.o mntcheck.o
3d2430
-	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ -L../utils/open-isns -lisns -lrt -lmount
3d2430
+	$(CC) $(CFLAGS) $^ -o $@ -L../utils/open-isns -lisns -lrt -lmount $(LDFLAGS)
3d2430
 
3d2430
 iscsiadm: $(ISCSI_LIB_SRCS) $(DISCOVERY_SRCS) iscsiadm.o session_mgmt.o mntcheck.o
3d2430
-	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ -L../utils/open-isns -lisns -lmount
3d2430
+	$(CC) $(CFLAGS) $^ -o $@ -L../utils/open-isns -lisns -lmount $(LDFLAGS)
3d2430
 
3d2430
 iscsistart: $(ISCSI_LIB_SRCS) $(INITIATOR_SRCS) $(FW_BOOT_SRCS) \
3d2430
 		iscsistart.o statics.o
3d2430
-	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ -lrt
3d2430
+	$(CC) $(CFLAGS) $^ -o $@ -lrt $(LDFLAGS)
3d2430
 clean:
3d2430
 	rm -f *.o $(PROGRAMS) .depend $(LIBSYS)
3d2430
 
3d2430
diff --git a/usr/event_poll.c b/usr/event_poll.c
3d2430
index ac25044..4cf4ce2 100644
3d2430
--- a/usr/event_poll.c
3d2430
+++ b/usr/event_poll.c
3d2430
@@ -41,6 +41,10 @@
3d2430
 
3d2430
 static unsigned int reap_count;
3d2430
 
3d2430
+/* track pid of reload fork, while running */
3d2430
+static pid_t reload_pid = 0;
3d2430
+static void (*reload_callback)(void);
3d2430
+
3d2430
 #define REAP_WAKEUP 1000 /* in millisecs */
3d2430
 
3d2430
 void reap_inc(void)
3d2430
@@ -48,9 +52,18 @@ void reap_inc(void)
3d2430
 	reap_count++;
3d2430
 }
3d2430
 
3d2430
+/* track the reload process to be reaped, when done */
3d2430
+void reap_track_reload_process(pid_t reload_proc_pid, void (*reload_done_callback)(void))
3d2430
+{
3d2430
+	reload_pid = reload_proc_pid;
3d2430
+	reload_callback = reload_done_callback;
3d2430
+	reap_inc();
3d2430
+}
3d2430
+
3d2430
 void reap_proc(void)
3d2430
 {
3d2430
-	int rc, i, max_reaps;
3d2430
+	int i, max_reaps;
3d2430
+	pid_t rc;
3d2430
 
3d2430
 	/*
3d2430
 	 * We don't really need reap_count, but calling wait() all the
3d2430
@@ -60,9 +73,13 @@ void reap_proc(void)
3d2430
 	for (i = 0; i < max_reaps; i++) {
3d2430
 		rc = waitpid(0, NULL, WNOHANG);
3d2430
 		if (rc > 0) {
3d2430
+			if (rc == reload_pid) {
3d2430
+				log_debug(6, "reaped reload process");
3d2430
+				reload_callback();
3d2430
+			}
3d2430
 			reap_count--;
3d2430
 			log_debug(6, "reaped pid %d, reap_count now %d",
3d2430
-				  rc, reap_count);
3d2430
+				  (int)rc, reap_count);
3d2430
 		}
3d2430
 	}
3d2430
 }
3d2430
diff --git a/usr/event_poll.h b/usr/event_poll.h
3d2430
index c0eed5c..f23132b 100644
3d2430
--- a/usr/event_poll.h
3d2430
+++ b/usr/event_poll.h
3d2430
@@ -25,6 +25,7 @@ struct queue_task;
3d2430
 int shutdown_callback(pid_t pid);
3d2430
 void reap_proc(void);
3d2430
 void reap_inc(void);
3d2430
+void reap_track_reload_process(pid_t realod_proc_pid, void (*reload_done_callback)(void));
3d2430
 void event_loop(struct iscsi_ipc *ipc, int control_fd, int mgmt_ipc_fd);
3d2430
 void event_loop_exit(struct queue_task *qtask);
3d2430
 
3d2430
diff --git a/usr/iscsi_sysfs.c b/usr/iscsi_sysfs.c
3d2430
index 3395590..b9d5a84 100644
3d2430
--- a/usr/iscsi_sysfs.c
3d2430
+++ b/usr/iscsi_sysfs.c
3d2430
@@ -1512,6 +1512,50 @@ free_info:
3d2430
 	return rc;
3d2430
 }
3d2430
 
3d2430
+/*
3d2430
+ * count the number of sessions -- a much-simplified
3d2430
+ * version of iscsi_sysfs_for_each_session
3d2430
+ *
3d2430
+ * TODO: return an array of the session info we find, for use
3d2430
+ * by iscsi_sysfs_for_each_session(), so it doesn't have to
3d2430
+ * do it all over again
3d2430
+ */
3d2430
+int iscsi_sysfs_count_sessions(void)
3d2430
+{
3d2430
+	struct dirent **namelist = NULL;
3d2430
+	int n, i;
3d2430
+	struct session_info *info;
3d2430
+
3d2430
+
3d2430
+	info = calloc(1, sizeof(*info));
3d2430
+	if (!info)
3d2430
+		/* no sessions found */
3d2430
+		return 0;
3d2430
+
3d2430
+	n = scandir(ISCSI_SESSION_DIR, &namelist, trans_filter, alphasort);
3d2430
+	if (n <= 0)
3d2430
+		/* no sessions found */
3d2430
+		goto free_info;
3d2430
+
3d2430
+	/*
3d2430
+	 * try to get session info for each session found, but ignore
3d2430
+	 * errors if any since it may be a race condition
3d2430
+	 */
3d2430
+	for (i = 0; i < n; i++)
3d2430
+		if (iscsi_sysfs_get_sessioninfo_by_id(info,
3d2430
+					namelist[i]->d_name) != 0)
3d2430
+			log_warning("could not find session info for %s",
3d2430
+					namelist[i]->d_name);
3d2430
+
3d2430
+	for (i = 0; i < n; i++)
3d2430
+		free(namelist[i]);
3d2430
+	free(namelist);
3d2430
+
3d2430
+free_info:
3d2430
+	free(info);
3d2430
+	return n;
3d2430
+}
3d2430
+
3d2430
 int iscsi_sysfs_get_session_state(char *state, int sid)
3d2430
 {
3d2430
 	char id[NAME_SIZE];
3d2430
diff --git a/usr/iscsi_sysfs.h b/usr/iscsi_sysfs.h
3d2430
index cdcefa6..1d0377f 100644
3d2430
--- a/usr/iscsi_sysfs.h
3d2430
+++ b/usr/iscsi_sysfs.h
3d2430
@@ -53,6 +53,7 @@ extern int iscsi_sysfs_for_each_iface_on_host(void *data, uint32_t host_no,
3d2430
 extern int iscsi_sysfs_for_each_session(void *data, int *nr_found,
3d2430
 					iscsi_sysfs_session_op_fn *fn,
3d2430
 					int in_parallel);
3d2430
+extern int iscsi_sysfs_count_sessions(void);
3d2430
 extern int iscsi_sysfs_for_each_host(void *data, int *nr_found,
3d2430
 				     iscsi_sysfs_host_op_fn *fn);
3d2430
 extern uint32_t iscsi_sysfs_get_host_no_from_sid(uint32_t sid, int *err);
3d2430
diff --git a/usr/iscsid.c b/usr/iscsid.c
3d2430
index 01b90c9..b0629f4 100644
3d2430
--- a/usr/iscsid.c
3d2430
+++ b/usr/iscsid.c
3d2430
@@ -34,6 +34,9 @@
3d2430
 #include <sys/wait.h>
3d2430
 #include <sys/types.h>
3d2430
 #include <sys/stat.h>
3d2430
+#ifndef	NO_SYSTEMD
3d2430
+#include <systemd/sd-daemon.h>
3d2430
+#endif
3d2430
 
3d2430
 #include "iscsid.h"
3d2430
 #include "mgmt_ipc.h"
3d2430
@@ -62,6 +65,7 @@ static pid_t log_pid;
3d2430
 static gid_t gid;
3d2430
 static int daemonize = 1;
3d2430
 static int mgmt_ipc_fd;
3d2430
+static int sessions_to_recover = 0;
3d2430
 
3d2430
 static struct option const long_options[] = {
3d2430
 	{"config", required_argument, NULL, 'c'},
3d2430
@@ -334,6 +338,30 @@ static void missing_iname_warn(char *initiatorname_file)
3d2430
 		  "ignored.", initiatorname_file, initiatorname_file);
3d2430
 }
3d2430
 
3d2430
+/* called right before we enter the event loop */
3d2430
+static void set_state_to_ready(void)
3d2430
+{
3d2430
+#ifndef	NO_SYSTEMD
3d2430
+	if (sessions_to_recover)
3d2430
+		sd_notify(0, "READY=1\n"
3d2430
+				"RELOADING=1\n"
3d2430
+				"STATUS=Syncing existing session(s)\n");
3d2430
+	else
3d2430
+		sd_notify(0, "READY=1\n"
3d2430
+				"STATUS=Ready to process requests\n");
3d2430
+#endif
3d2430
+}
3d2430
+
3d2430
+/* called when recovery process has been reaped */
3d2430
+static void set_state_done_reloading(void)
3d2430
+{
3d2430
+#ifndef	NO_SYSTEMD
3d2430
+	sessions_to_recover = 0;
3d2430
+	sd_notifyf(0, "READY=1\n"
3d2430
+			"STATUS=Ready to process requests\n");
3d2430
+#endif
3d2430
+}
3d2430
+
3d2430
 int main(int argc, char *argv[])
3d2430
 {
3d2430
 	struct utsname host_info; /* will use to compound initiator alias */
3d2430
@@ -526,18 +554,31 @@ int main(int argc, char *argv[])
3d2430
 		daemon_config.safe_logout = 1;
3d2430
 	free(safe_logout);
3d2430
 
3d2430
-	pid = fork();
3d2430
-	if (pid == 0) {
3d2430
-		int nr_found = 0;
3d2430
-		/* child */
3d2430
-		/* TODO - test with async support enabled */
3d2430
-		iscsi_sysfs_for_each_session(NULL, &nr_found, sync_session, 0);
3d2430
-		exit(0);
3d2430
-	} else if (pid < 0) {
3d2430
-		log_error("Fork failed error %d: existing sessions"
3d2430
-			  " will not be synced", errno);
3d2430
-	} else
3d2430
-		reap_inc();
3d2430
+	/* see if we have any stale sessions to recover */
3d2430
+	sessions_to_recover = iscsi_sysfs_count_sessions();
3d2430
+	if (sessions_to_recover) {
3d2430
+
3d2430
+		/*
3d2430
+		 * recover stale sessions in the background
3d2430
+		 */
3d2430
+
3d2430
+		pid = fork();
3d2430
+		if (pid == 0) {
3d2430
+			int nr_found; /* not used */
3d2430
+			/* child */
3d2430
+			/* TODO - test with async support enabled */
3d2430
+			iscsi_sysfs_for_each_session(NULL, &nr_found, sync_session, 0);
3d2430
+			exit(0);
3d2430
+		} else if (pid < 0) {
3d2430
+			log_error("Fork failed error %d: existing sessions"
3d2430
+				  " will not be synced", errno);
3d2430
+		} else {
3d2430
+			/* parent */
3d2430
+			log_debug(8, "forked child (pid=%d) to recover %d session(s)",
3d2430
+					(int)pid, sessions_to_recover);
3d2430
+			reap_track_reload_process(pid, set_state_done_reloading);
3d2430
+		}
3d2430
+	}
3d2430
 
3d2430
 	iscsi_initiator_init();
3d2430
 	increase_max_files();
3d2430
@@ -554,6 +595,7 @@ int main(int argc, char *argv[])
3d2430
 		exit(ISCSI_ERR);
3d2430
 	}
3d2430
 
3d2430
+	set_state_to_ready();
3d2430
 	event_loop(ipc, control_fd, mgmt_ipc_fd);
3d2430
 
3d2430
 	idbm_terminate();
3d2430
-- 
3d2430
2.21.0
3d2430