public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Phillip Susi <psusi@ubuntu.com>
To: util-linux@vger.kernel.org
Subject: [PATCH] setsid: don't fork
Date: Thu, 14 Nov 2013 14:23:13 -0500	[thread overview]
Message-ID: <1384456993-3721-1-git-send-email-psusi@ubuntu.com> (raw)

2.24 added a switch to setsid to wait for the child to exit
and return its exit status.  I believe this was the wrong way
to fix the underlying bug since the behavior of the program
still differs depending on whether it is run as the group leader
or not, and requires a switch to get the correct behavior.
Instead of this --wait switch, just make sure the parent
process is the one that execs the new program rather than
the child process.

Signed-off-by: Phillip Susi <psusi@ubuntu.com>
---
 sys-utils/setsid.1 |  3 ---
 sys-utils/setsid.c | 23 +++++++++--------------
 2 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/sys-utils/setsid.1 b/sys-utils/setsid.1
index da8d648..ab1b20c 100644
--- a/sys-utils/setsid.1
+++ b/sys-utils/setsid.1
@@ -16,9 +16,6 @@ runs a program in a new session.
 \fB\-c\fP, \fB\-\-ctty\fP
 Set the controlling terminal to the current one.
 .TP
-\fB\-w\fP, \fB\-\-wait\fP
-Wait the execution of the program to end, and return the exit value of
-the child as return value of the
 .BR setsid .
 .SH "SEE ALSO"
 .BR setsid (2)
diff --git a/sys-utils/setsid.c b/sys-utils/setsid.c
index 782de82..9810138 100644
--- a/sys-utils/setsid.c
+++ b/sys-utils/setsid.c
@@ -47,12 +47,10 @@ int main(int argc, char **argv)
 {
 	int ch;
 	int ctty = 0;
-	pid_t pid;
-	int status = 0;
+	pid_t pid = 0;
 
 	static const struct option longopts[] = {
 		{"ctty", no_argument, NULL, 'c'},
-		{"wait", no_argument, NULL, 'w'},
 		{"version", no_argument, NULL, 'V'},
 		{"help", no_argument, NULL, 'h'},
 		{NULL, 0, NULL, 0}
@@ -71,9 +69,6 @@ int main(int argc, char **argv)
 		case 'c':
 			ctty=1;
 			break;
-		case 'w':
-			status = 1;
-			break;
 		case 'h':
 			usage(stdout);
 		default:
@@ -90,22 +85,22 @@ int main(int argc, char **argv)
 			err(EXIT_FAILURE, _("fork"));
 		case 0:
 			/* child */
+			sleep(5);
 			break;
 		default:
 			/* parent */
-			if (!status)
-				return EXIT_SUCCESS;
-			if (wait(&status) != pid)
-				err(EXIT_FAILURE, "wait");
-			if (WIFEXITED(status))
-				return WEXITSTATUS(status);
-			err(status, _("child %d did not exit normally"), pid);
+			if (setpgid(pid, 0) == -1)
+				err(EXIT_FAILURE, _("setpgid failed"));
+			if (setpgid(0, pid) == -1)
+				err(EXIT_FAILURE, _("setpgid failed"));
+
 		}
 	}
 	if (setsid() < 0)
 		/* cannot happen */
 		err(EXIT_FAILURE, _("setsid failed"));
-
+	if (pid)
+		kill(pid, SIGTERM);
 	if (ctty) {
 		if (ioctl(STDIN_FILENO, TIOCSCTTY, 1))
 			err(EXIT_FAILURE, _("failed to set the controlling terminal"));
-- 
1.8.3.2


             reply	other threads:[~2013-11-14 19:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-14 19:23 Phillip Susi [this message]
2013-11-19 13:24 ` [PATCH] setsid: don't fork Karel Zak
2013-11-19 15:01   ` Phillip Susi
2013-11-19 18:05     ` Karel Zak
2013-11-19 19:49       ` Phillip Susi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1384456993-3721-1-git-send-email-psusi@ubuntu.com \
    --to=psusi@ubuntu.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox