public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* forking in setsid
@ 2013-06-19 14:53 Damien Wyart
  2013-06-19 16:00 ` Sami Kerola
  0 siblings, 1 reply; 5+ messages in thread
From: Damien Wyart @ 2013-06-19 14:53 UTC (permalink / raw)
  To: util-linux

Hi,

While searching information about setsid and daemonization, I came accros
the following "bug" report in Debian :
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=495881

Unfortunately, it doesn't seem to have been forwarded upstream after
creation, and never received an answer.

I would be interested in getting your opinions about it and the patch
attached to it.

Thanks in advance

Best regards,

Damien

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: forking in setsid
  2013-06-19 14:53 forking in setsid Damien Wyart
@ 2013-06-19 16:00 ` Sami Kerola
  2013-06-19 17:16   ` Damien Wyart
  2013-07-09  9:36   ` Karel Zak
  0 siblings, 2 replies; 5+ messages in thread
From: Sami Kerola @ 2013-06-19 16:00 UTC (permalink / raw)
  To: Damien Wyart; +Cc: util-linux

On 19 June 2013 15:53, Damien Wyart <damien.wyart@gmail.com> wrote:
> While searching information about setsid and daemonization, I came accros
> the following "bug" report in Debian :
> http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=495881
>
> Unfortunately, it doesn't seem to have been forwarded upstream after
> creation, and never received an answer.
>
> I would be interested in getting your opinions about it and the patch
> attached to it.

Hi Damien,

I am not sure if it is good idea to change default behavior of the
software. Perhaps waiting return value of child should be new option,
something like


>From c2deb80ef6feb7039a0554e4b8a8272e59cb1a87 Mon Sep 17 00:00:00 2001
From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Date: Wed, 20 Aug 2008 12:00:00 +0100
Subject: [PATCH] setsid: add an option to wait child return value
Organization: Lastminute.com

This change was sent by Damien Wyart in behalf of Daniel Kahn Gillmor.

Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=495881
Reference: http://permalink.gmane.org/gmane.linux.utilities.util-linux-ng/7736
Reported-by: Damien Wyart <damien.wyart@gmail.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 sys-utils/setsid.1 |  5 +++++
 sys-utils/setsid.c | 28 +++++++++++++++++++++++-----
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/sys-utils/setsid.1 b/sys-utils/setsid.1
index eff7948..da8d648 100644
--- a/sys-utils/setsid.1
+++ b/sys-utils/setsid.1
@@ -15,6 +15,11 @@ runs a program in a new session.
 .TP
 \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)
 .SH AUTHOR
diff --git a/sys-utils/setsid.c b/sys-utils/setsid.c
index 756a520..0e11a1a 100644
--- a/sys-utils/setsid.c
+++ b/sys-utils/setsid.c
@@ -9,6 +9,9 @@
  * 2001-01-18 John Fremlin <vii@penguinpowered.com>
  * - fork in case we are process group leader
  *
+ * 2008-08-20 Daniel Kahn Gillmor <dkg@fifthhorseman.net>
+ * - if forked, wait on child process and emit its return code.
+ *
  */

 #include <getopt.h>
@@ -16,6 +19,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/wait.h>

 #include "c.h"
 #include "nls.h"
@@ -29,8 +34,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
  program_invocation_short_name);

  fputs(USAGE_OPTIONS, out);
- fputs(_(" -c, --ctty     set the controlling terminal to the current one\n"),
- out);
+ fputs(_(" -c, --ctty     set the controlling terminal to the current
one\n"), out);
+ fputs(_(" -w, --wait     wait program to exit, and use the same
return\n"), out);

  fputs(USAGE_HELP, out);
  fputs(USAGE_VERSION, out);
@@ -43,10 +48,13 @@ int main(int argc, char **argv)
 {
  int ch;
  int ctty = 0;
+ pid_t pid;
+ int status = 0;

  static const struct option longopts[] = {
  {"ctty", no_argument, NULL, 'c'},
  {"version", no_argument, NULL, 'V'},
+ {"wait", no_argument, NULL, 'w'},
  {"help", no_argument, NULL, 'h'},
  {NULL, 0, NULL, 0}
  };
@@ -56,7 +64,7 @@ int main(int argc, char **argv)
  textdomain(PACKAGE);
  atexit(close_stdout);

- while ((ch = getopt_long(argc, argv, "+Vhc", longopts, NULL)) != -1)
+ while ((ch = getopt_long(argc, argv, "+Vhcw", longopts, NULL)) != -1)
  switch (ch) {
  case 'V':
  printf(UTIL_LINUX_VERSION);
@@ -64,6 +72,9 @@ int main(int argc, char **argv)
  case 'c':
  ctty=1;
  break;
+ case 'w':
+ status = 1;
+ break;
  case 'h':
  usage(stdout);
  default:
@@ -74,15 +85,22 @@ int main(int argc, char **argv)
  usage(stderr);

  if (getpgrp() == getpid()) {
- switch (fork()) {
+ pid = fork();
+ switch (pid) {
  case -1:
  err(EXIT_FAILURE, _("fork"));
  case 0:
  /* child */
  break;
  default:
+ if (!status)
+ return EXIT_SUCCESS;
  /* parent */
- return 0;
+ if (wait(&status) != pid)
+ err(EXIT_FAILURE, "wait");
+ if (WIFEXITED(status))
+ return WEXITSTATUS(status);
+ err(status, _("child %d did not exit normally"), pid);
  }
  }
  if (setsid() < 0)
--
1.8.3.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: forking in setsid
  2013-06-19 16:00 ` Sami Kerola
@ 2013-06-19 17:16   ` Damien Wyart
  2013-07-09  9:36   ` Karel Zak
  1 sibling, 0 replies; 5+ messages in thread
From: Damien Wyart @ 2013-06-19 17:16 UTC (permalink / raw)
  To: kerolasa; +Cc: util-linux

Thanks for your answer. Let's wait for other comments on it...

Btw, would it make sense to have a "full" daemonization command in
util-linux (like this one http://software.clapper.org/daemonize/) ?
There is one in FreeBSD (daemon), and a daemon function also exists in
glibc.

Thanks,

Damien

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: forking in setsid
  2013-06-19 16:00 ` Sami Kerola
  2013-06-19 17:16   ` Damien Wyart
@ 2013-07-09  9:36   ` Karel Zak
  1 sibling, 0 replies; 5+ messages in thread
From: Karel Zak @ 2013-07-09  9:36 UTC (permalink / raw)
  To: kerolasa; +Cc: Damien Wyart, util-linux

On Wed, Jun 19, 2013 at 05:00:09PM +0100, Sami Kerola wrote:
> From c2deb80ef6feb7039a0554e4b8a8272e59cb1a87 Mon Sep 17 00:00:00 2001
> From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
> Date: Wed, 20 Aug 2008 12:00:00 +0100
> Subject: [PATCH] setsid: add an option to wait child return value
> Organization: Lastminute.com

it would be nice to have usable patch, ideally sent by git-send-email.

    Karel

> - fputs(_(" -c, --ctty     set the controlling terminal to the current one\n"),
> - out);
> + fputs(_(" -c, --ctty     set the controlling terminal to the current
> one\n"), out);
> + fputs(_(" -w, --wait     wait program to exit, and use the same
> return\n"), out);

 invalid patch formatting

> + if (wait(&status) != pid)
> + err(EXIT_FAILURE, "wait");
> + if (WIFEXITED(status))
> + return WEXITSTATUS(status);
> + err(status, _("child %d did not exit normally"), pid);

 useless formatting...

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: forking in setsid
@ 2013-07-09 19:08 Daniel Kahn Gillmor
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Kahn Gillmor @ 2013-07-09 19:08 UTC (permalink / raw)
  To: Damien Wyart, Sami Kerola, Karel Zak, util-linux

[-- Attachment #1: Type: text/plain, Size: 1420 bytes --]

Hi folks--

I'm not subscribed to the util-linux mailing list (it's a firehose i
don't have the capacity to drink from), so please keep me Cc'ed on
replies.

I'm sorry that my original report was to debian only, and was not
forwarded upstream until now, but i appreciate Damien Wyert dusting it
off.

When i originally wrote http://bugs.debian.org/495881, i asked the
following question:

> I don't understand /usr/bin/setsid terribly well: is this difference
> in behavior [between setsid as process group leader or not-leader]
> desirable for some reason?  If so, why?  The shipped documentation
> (setsid(1)) makes no mention of these behavioral inconsistencies.

These questions arose from struggling with setsid in the context of some
work with ssh, in particular the thread starting here:

 http://marc.info/?l=openssh-unix-dev&m=121927018112558&w=2

i ended up resolving that problem without needing to use setsid, and i
think i came away from it (due to the aforementioned behavioral
inconsistencies) with the sense that /usr/bin/setsid wasn't actually a
particularly reliable or predictable tool.

I don't know whether it's more appropriate to try to fix it, to provide
a "--behave-consistently-regardless-of-pg-leader-status" option, or just
to document the weirdnesses, but i would certainly be curious to know if
there are users of setsid that rely on this strange set of behaviors.

Regards,

        --dkg

[-- Attachment #2: Type: application/pgp-signature, Size: 965 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-07-09 19:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-19 14:53 forking in setsid Damien Wyart
2013-06-19 16:00 ` Sami Kerola
2013-06-19 17:16   ` Damien Wyart
2013-07-09  9:36   ` Karel Zak
  -- strict thread matches above, loose matches on Subject: below --
2013-07-09 19:08 Daniel Kahn Gillmor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox