public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Simon Richter <Simon.Richter@hogyros.de>
Cc: linux-kernel@vger.kernel.org
Subject: Re: RFC: implement daemon() in the kernel
Date: Mon, 20 Nov 2006 16:38:41 -0800	[thread overview]
Message-ID: <45624A91.3010604@zytor.com> (raw)
In-Reply-To: <4561ABB4.6090700@hogyros.de>

Simon Richter wrote:
> 
>  - daemonize a process
> 
> There is a function called daemon() that does this; its behaviour is
> roughly defined by (modulo error handling)
> 
> int daemon(int nochdir, int noclose)
> {
> 	if(!nochdir)
> 		chdir("/");
> 
> 	if(!noclose)
> 	{
> 		int fd = open("/dev/null", O_RDWR);
> 		dup2(fd, 0);
> 		dup2(fd, 1);
> 		dup2(fd, 2);
> 		close(fd);
> 	}
> 
> 	if(fork() > 0)

... that should be if (fork() == 0) ...

> 		_exit(0);

	setsid();
> }
> 


> Since it calls _exit() right after fork() returns (so daemon() never
> returns to the calling process except in case of an error) it would be
> possible to implement this on MMUless machines if the last two lines
> could happen in the kernel.
> 

You could do this quite easily with clone() and a small assembly wrapper.

The assembly wrapper needs to do the last two lines without touching the 
stack in the parent.  That is usually quite trivial, even on 
register-starved architectures; for example, on i386 it would look like 
(ignoring vsyscalls for the moment, which are only an optimization anyway).

__detach_from_parent:
	pushl	%ebx
	movl	$__NR_clone, %eax
	movl	$CLONE_VM|SIGCHLD, %ebx
	xorl	%ecx, %ecx
	int	$0x80
	cmpl	$-4096, %eax
	ja	1f
	andl	%eax, %eax
	je	2f
	# Parent process, must _exit(0)
	xorl	%ebx, %ebx
	movl	$__NR_exit, %eax
	int	$0x80
	# _exit() should never return
	hlt
1:	# Error on fork(), set errno and return -1
	negl	%eax
	movl	%eax, errno		# Or TLS equivalent
	orl	$-1, %eax
2:	# Child process jumps here with %eax == 0 already
	popl	%ebx
	ret


  parent reply	other threads:[~2006-11-21  0:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-20 13:20 RFC: implement daemon() in the kernel Simon Richter
2006-11-20 15:38 ` Mark Rustad
2006-11-20 17:42   ` Simon Richter
2006-11-20 20:48     ` Mark Rustad
2006-11-20 20:38 ` Jan Engelhardt
2006-11-21  0:38 ` H. Peter Anvin [this message]
2006-11-21  9:30   ` Michal Schmidt
2006-11-21 17:15     ` H. Peter Anvin

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=45624A91.3010604@zytor.com \
    --to=hpa@zytor.com \
    --cc=Simon.Richter@hogyros.de \
    --cc=linux-kernel@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