c40e03
From e696afd2d810fef403c6e5d35a44cc0f22128310 Mon Sep 17 00:00:00 2001
c40e03
From: Gary Lockyer <gary@catalyst.net.nz>
c40e03
Date: Mon, 21 Aug 2017 15:12:04 +1200
c40e03
Subject: [PATCH 1/4] s4/smbd: set the process group.
c40e03
MIME-Version: 1.0
c40e03
Content-Type: text/plain; charset=UTF-8
c40e03
Content-Transfer-Encoding: 8bit
c40e03
c40e03
Set the process group in the samba daemon, the --no-process-group option
c40e03
allows this to be disabled.  The no-process-group option needs to be
c40e03
disabled in self test.
c40e03
c40e03
Signed-off-by: Gary Lockyer <gary@catalyst.net.nz>
c40e03
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
c40e03
Reviewed-by: Ralph Boehme <slow@samba.org>
c40e03
c40e03
Autobuild-User(master): Ralph Böhme <slow@samba.org>
c40e03
Autobuild-Date(master): Mon Sep 18 04:39:50 CEST 2017 on sn-devel-144
c40e03
---
c40e03
 selftest/target/Samba4.pm |  2 +-
c40e03
 source4/smbd/server.c     | 18 +++++++++++++++++-
c40e03
 2 files changed, 18 insertions(+), 2 deletions(-)
c40e03
c40e03
diff --git a/selftest/target/Samba4.pm b/selftest/target/Samba4.pm
c40e03
index 772f982cb9d..6a1856ef642 100755
c40e03
--- a/selftest/target/Samba4.pm
c40e03
+++ b/selftest/target/Samba4.pm
c40e03
@@ -158,7 +158,7 @@ sub check_or_start($$$)
c40e03
 		close($env_vars->{STDIN_PIPE});
c40e03
 		open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
c40e03
 
c40e03
-		exec(@preargs, Samba::bindir_path($self, "samba"), "-M", $process_model, "-i", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
c40e03
+		exec(@preargs, Samba::bindir_path($self, "samba"), "-M", $process_model, "-i", "--no-process-group", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
c40e03
 	}
c40e03
 	$env_vars->{SAMBA_PID} = $pid;
c40e03
 	print "DONE ($pid)\n";
c40e03
diff --git a/source4/smbd/server.c b/source4/smbd/server.c
c40e03
index a8bad06bed3..ba520e0a8f5 100644
c40e03
--- a/source4/smbd/server.c
c40e03
+++ b/source4/smbd/server.c
c40e03
@@ -341,6 +341,7 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 {
c40e03
 	bool opt_daemon = false;
c40e03
 	bool opt_interactive = false;
c40e03
+	bool opt_no_process_group = false;
c40e03
 	int opt;
c40e03
 	poptContext pc;
c40e03
 #define _MODULE_PROTO(init) extern NTSTATUS init(TALLOC_CTX *);
c40e03
@@ -356,7 +357,8 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 		OPT_DAEMON = 1000,
c40e03
 		OPT_INTERACTIVE,
c40e03
 		OPT_PROCESS_MODEL,
c40e03
-		OPT_SHOW_BUILD
c40e03
+		OPT_SHOW_BUILD,
c40e03
+		OPT_NO_PROCESS_GROUP,
c40e03
 	};
c40e03
 	struct poptOption long_options[] = {
c40e03
 		POPT_AUTOHELP
c40e03
@@ -371,6 +373,8 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 			"till autotermination", "seconds"},
c40e03
 		{"show-build", 'b', POPT_ARG_NONE, NULL, OPT_SHOW_BUILD,
c40e03
 			"show build info", NULL },
c40e03
+		{"no-process-group", '\0', POPT_ARG_NONE, NULL,
c40e03
+		  OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
c40e03
 		POPT_COMMON_SAMBA
c40e03
 		POPT_COMMON_VERSION
c40e03
 		{ NULL }
c40e03
@@ -393,6 +397,9 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 		case OPT_SHOW_BUILD:
c40e03
 			show_build();
c40e03
 			break;
c40e03
+		case OPT_NO_PROCESS_GROUP:
c40e03
+			opt_no_process_group = true;
c40e03
+			break;
c40e03
 		default:
c40e03
 			fprintf(stderr, "\nInvalid option %s: %s\n\n",
c40e03
 				  poptBadOption(pc, 0), poptStrerror(opt));
c40e03
@@ -508,6 +515,15 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 		stdin_event_flags = 0;
c40e03
 	}
c40e03
 
c40e03
+#if HAVE_SETPGID
c40e03
+	/*
c40e03
+	 * If we're interactive we want to set our own process group for
c40e03
+	 * signal management, unless --no-process-group specified.
c40e03
+	 */
c40e03
+	if (opt_interactive && !opt_no_process_group)
c40e03
+		setpgid((pid_t)0, (pid_t)0);
c40e03
+#endif
c40e03
+
c40e03
 	/* catch EOF on stdin */
c40e03
 #ifdef SIGTTIN
c40e03
 	signal(SIGTTIN, SIG_IGN);
c40e03
-- 
c40e03
2.15.0
c40e03
c40e03
c40e03
From 1e3f38e58d52c7424831855c8db63c391e0b4b75 Mon Sep 17 00:00:00 2001
c40e03
From: Andreas Schneider <asn@samba.org>
c40e03
Date: Wed, 15 Nov 2017 10:00:52 +0100
c40e03
Subject: [PATCH 2/4] s4:samba: Do not segfault if we run into issues
c40e03
c40e03
Signed-off-by: Andreas Schneider <asn@samba.org>
c40e03
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
c40e03
(cherry picked from commit bfafabfb942668328401a3c89fc55b50dc56c209)
c40e03
---
c40e03
 source4/smbd/server.c | 8 ++++++++
c40e03
 1 file changed, 8 insertions(+)
c40e03
c40e03
diff --git a/source4/smbd/server.c b/source4/smbd/server.c
c40e03
index ba520e0a8f5..406f79593b9 100644
c40e03
--- a/source4/smbd/server.c
c40e03
+++ b/source4/smbd/server.c
c40e03
@@ -100,8 +100,16 @@ static void cleanup_tmp_files(struct loadparm_context *lp_ctx)
c40e03
 {
c40e03
 	char *path;
c40e03
 	TALLOC_CTX *mem_ctx = talloc_new(NULL);
c40e03
+	if (mem_ctx == NULL) {
c40e03
+		exit_daemon("Failed to create memory context",
c40e03
+			    ENOMEM);
c40e03
+	}
c40e03
 
c40e03
 	path = smbd_tmp_path(mem_ctx, lp_ctx, NULL);
c40e03
+	if (path == NULL) {
c40e03
+		exit_daemon("Failed to cleanup temporary files",
c40e03
+			    EINVAL);
c40e03
+	}
c40e03
 
c40e03
 	recursive_delete(path);
c40e03
 	talloc_free(mem_ctx);
c40e03
-- 
c40e03
2.15.0
c40e03
c40e03
c40e03
From b7d08eda158ba540dc7ca8755a6a8fdf34e52501 Mon Sep 17 00:00:00 2001
c40e03
From: Andreas Schneider <asn@samba.org>
c40e03
Date: Fri, 10 Nov 2017 09:18:18 +0100
c40e03
Subject: [PATCH 3/4] s4:samba: Allow samba daemon to run in foreground
c40e03
c40e03
We are passing the no_process_group to become_daemon() that setsid() is
c40e03
not called. In case we are double forking, we run in SysV daemon mode,
c40e03
setsid() should be called!
c40e03
c40e03
See:
c40e03
https://www.freedesktop.org/software/systemd/man/daemon.html
c40e03
c40e03
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13129
c40e03
c40e03
Signed-off-by: Andreas Schneider <asn@samba.org>
c40e03
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
c40e03
c40e03
(cherry picked from commit 8736013dc42c5755b75bbb2e843a290bcd545909)
c40e03
---
c40e03
 source3/smbd/server.c |  2 +-
c40e03
 source4/smbd/server.c | 13 ++++++++++---
c40e03
 2 files changed, 11 insertions(+), 4 deletions(-)
c40e03
c40e03
diff --git a/source3/smbd/server.c b/source3/smbd/server.c
c40e03
index 181bcd1e123..252b43190d7 100644
c40e03
--- a/source3/smbd/server.c
c40e03
+++ b/source3/smbd/server.c
c40e03
@@ -1592,7 +1592,7 @@ extern void build_options(bool screen);
c40e03
 	struct poptOption long_options[] = {
c40e03
 	POPT_AUTOHELP
c40e03
 	{"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
c40e03
-	{"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon)"},
c40e03
+	{"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon) and log to stdout"},
c40e03
 	{"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools, etc.)" },
c40e03
 	{"no-process-group", '\0', POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
c40e03
 	{"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
c40e03
diff --git a/source4/smbd/server.c b/source4/smbd/server.c
c40e03
index 406f79593b9..2349d5c7fa0 100644
c40e03
--- a/source4/smbd/server.c
c40e03
+++ b/source4/smbd/server.c
c40e03
@@ -348,6 +348,7 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 				const char *argv[])
c40e03
 {
c40e03
 	bool opt_daemon = false;
c40e03
+	bool opt_fork = true;
c40e03
 	bool opt_interactive = false;
c40e03
 	bool opt_no_process_group = false;
c40e03
 	int opt;
c40e03
@@ -363,6 +364,7 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 	struct stat st;
c40e03
 	enum {
c40e03
 		OPT_DAEMON = 1000,
c40e03
+		OPT_FOREGROUND,
c40e03
 		OPT_INTERACTIVE,
c40e03
 		OPT_PROCESS_MODEL,
c40e03
 		OPT_SHOW_BUILD,
c40e03
@@ -372,6 +374,8 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 		POPT_AUTOHELP
c40e03
 		{"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON,
c40e03
 		 "Become a daemon (default)", NULL },
c40e03
+		{"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FOREGROUND,
c40e03
+		 "Run the daemon in foreground", NULL },
c40e03
 		{"interactive",	'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE,
c40e03
 		 "Run interactive (not a daemon)", NULL},
c40e03
 		{"model", 'M', POPT_ARG_STRING,	NULL, OPT_PROCESS_MODEL,
c40e03
@@ -396,6 +400,9 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 		case OPT_DAEMON:
c40e03
 			opt_daemon = true;
c40e03
 			break;
c40e03
+		case OPT_FOREGROUND:
c40e03
+			opt_fork = false;
c40e03
+			break;
c40e03
 		case OPT_INTERACTIVE:
c40e03
 			opt_interactive = true;
c40e03
 			break;
c40e03
@@ -422,7 +429,7 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 			"not allowed together with -D|--daemon\n\n");
c40e03
 		poptPrintUsage(pc, stderr, 0);
c40e03
 		return 1;
c40e03
-	} else if (!opt_interactive) {
c40e03
+	} else if (!opt_interactive && !opt_fork) {
c40e03
 		/* default is --daemon */
c40e03
 		opt_daemon = true;
c40e03
 	}
c40e03
@@ -458,8 +465,8 @@ static int binary_smbd_main(const char *binary_name,
c40e03
 	}
c40e03
 
c40e03
 	if (opt_daemon) {
c40e03
-		DEBUG(3,("Becoming a daemon.\n"));
c40e03
-		become_daemon(true, false, false);
c40e03
+		DBG_NOTICE("Becoming a daemon.\n");
c40e03
+		become_daemon(opt_fork, opt_no_process_group, false);
c40e03
 	}
c40e03
 
c40e03
 	/* Create the memory context to hang everything off. */
c40e03
-- 
c40e03
2.15.0
c40e03
c40e03
c40e03
From 90588e8d08dcf38d97249eb39d87c5eb36f1fcd3 Mon Sep 17 00:00:00 2001
c40e03
From: Andreas Schneider <asn@samba.org>
c40e03
Date: Fri, 10 Nov 2017 09:32:27 +0100
c40e03
Subject: [PATCH 4/4] systemd: Start processes in forground and without a
c40e03
 process group
c40e03
c40e03
We should not double fork in notify mode or systemd think something
c40e03
during startup will be wrong and send SIGTERM to the process. So
c40e03
sometimes the daemon will not start up correctly.
c40e03
c40e03
systemd will also handle the process group.
c40e03
c40e03
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13129
c40e03
c40e03
Signed-off-by: Andreas Schneider <asn@samba.org>
c40e03
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
c40e03
c40e03
(cherry picked from commit 8b6f58194da7e849cdb9d20712dff49b17a93a77)
c40e03
---
c40e03
 packaging/systemd/nmb.service     | 2 +-
c40e03
 packaging/systemd/samba.service   | 2 +-
c40e03
 packaging/systemd/smb.service     | 2 +-
c40e03
 packaging/systemd/winbind.service | 2 +-
c40e03
 4 files changed, 4 insertions(+), 4 deletions(-)
c40e03
c40e03
diff --git a/packaging/systemd/nmb.service b/packaging/systemd/nmb.service
c40e03
index 992c0cd9d2b..71c93d6088b 100644
c40e03
--- a/packaging/systemd/nmb.service
c40e03
+++ b/packaging/systemd/nmb.service
c40e03
@@ -7,7 +7,7 @@ Type=notify
c40e03
 NotifyAccess=all
c40e03
 PIDFile=/run/nmbd.pid
c40e03
 EnvironmentFile=-/etc/sysconfig/samba
c40e03
-ExecStart=/usr/sbin/nmbd $NMBDOPTIONS
c40e03
+ExecStart=/usr/sbin/nmbd --foreground --no-process-group $NMBDOPTIONS
c40e03
 ExecReload=/usr/bin/kill -HUP $MAINPID
c40e03
 LimitCORE=infinity
c40e03
 
c40e03
diff --git a/packaging/systemd/samba.service b/packaging/systemd/samba.service
c40e03
index 824f89c2030..1b64c3b779d 100644
c40e03
--- a/packaging/systemd/samba.service
c40e03
+++ b/packaging/systemd/samba.service
c40e03
@@ -8,7 +8,7 @@ NotifyAccess=all
c40e03
 PIDFile=/run/samba.pid
c40e03
 LimitNOFILE=16384
c40e03
 EnvironmentFile=-/etc/sysconfig/samba
c40e03
-ExecStart=/usr/sbin/samba $SAMBAOPTIONS
c40e03
+ExecStart=/usr/sbin/samba --foreground --no-process-group $SAMBAOPTIONS
c40e03
 ExecReload=/usr/bin/kill -HUP $MAINPID
c40e03
 
c40e03
 [Install]
c40e03
diff --git a/packaging/systemd/smb.service b/packaging/systemd/smb.service
c40e03
index 6053a5caaa5..adf6684c7d9 100644
c40e03
--- a/packaging/systemd/smb.service
c40e03
+++ b/packaging/systemd/smb.service
c40e03
@@ -8,7 +8,7 @@ NotifyAccess=all
c40e03
 PIDFile=/run/smbd.pid
c40e03
 LimitNOFILE=16384
c40e03
 EnvironmentFile=-/etc/sysconfig/samba
c40e03
-ExecStart=/usr/sbin/smbd $SMBDOPTIONS
c40e03
+ExecStart=/usr/sbin/smbd --foreground --no-process-group $SMBDOPTIONS
c40e03
 ExecReload=/usr/bin/kill -HUP $MAINPID
c40e03
 LimitCORE=infinity
c40e03
 
c40e03
diff --git a/packaging/systemd/winbind.service b/packaging/systemd/winbind.service
c40e03
index c511488166e..46b3797251d 100644
c40e03
--- a/packaging/systemd/winbind.service
c40e03
+++ b/packaging/systemd/winbind.service
c40e03
@@ -7,7 +7,7 @@ Type=notify
c40e03
 NotifyAccess=all
c40e03
 PIDFile=/run/winbindd.pid
c40e03
 EnvironmentFile=-/etc/sysconfig/samba
c40e03
-ExecStart=/usr/sbin/winbindd "$WINBINDOPTIONS"
c40e03
+ExecStart=/usr/sbin/winbindd --foreground --no-process-group "$WINBINDOPTIONS"
c40e03
 ExecReload=/usr/bin/kill -HUP $MAINPID
c40e03
 LimitCORE=infinity
c40e03
 
c40e03
-- 
c40e03
2.15.0
c40e03