From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:50258) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RmNY8-0005rt-AW for qemu-devel@nongnu.org; Sun, 15 Jan 2012 05:42:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RmNY7-0003Db-Aq for qemu-devel@nongnu.org; Sun, 15 Jan 2012 05:42:56 -0500 Received: from lo.gmane.org ([80.91.229.12]:36288) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RmNY6-0003DV-VM for qemu-devel@nongnu.org; Sun, 15 Jan 2012 05:42:55 -0500 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RmNY0-0006Tr-CY for qemu-devel@nongnu.org; Sun, 15 Jan 2012 11:42:48 +0100 Received: from 93-34-200-238.ip51.fastwebnet.it ([93.34.200.238]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 15 Jan 2012 11:42:48 +0100 Received: from pbonzini by 93-34-200-238.ip51.fastwebnet.it with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 15 Jan 2012 11:42:48 +0100 From: Paolo Bonzini Date: Sun, 15 Jan 2012 11:42:36 +0100 Message-ID: References: <20120114124210.E43483DE@gandalf.tls.msk.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit In-Reply-To: <20120114124210.E43483DE@gandalf.tls.msk.ru> Subject: Re: [Qemu-devel] [PATCH] rework daemonizing logic in qemu-nbd List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org On 01/14/2012 01:39 PM, Michael Tokarev wrote: > if (pid == 0) { > - close(stderr_fd[0]); > - ret = qemu_daemon(0, 0); > - > - /* Temporarily redirect stderr to the parent's pipe... */ > - dup2(stderr_fd[1], STDERR_FILENO); > - if (ret == -1) { > + int nullfd = open("/dev/null", O_RDWR); > + if (nullfd< 0 || setsid()< 0) { > err(EXIT_FAILURE, "Failed to daemonize"); > } This is forking only once. > - > - /* ... close the descriptor we inherited and go on. */ > - close(stderr_fd[1]); > - } else { > - bool errors = false; > - char *buf; > - > - /* In the parent. Print error messages from the child until > - * it closes the pipe. > + /* redirect stdin from /dev/null, > + * stdout (temporarily) to the pipe to parent, This is a bit of a hack. > + /* now complete the daemonizing procedure. > + */ > + if (device && !verbose) { > + if (chdir("/") < 0) { > + err(EXIT_FAILURE, "unable to chdir to /"); > + } > + /* this redirects stderr to /dev/null */ > + dup2(STDIN_FILENO, STDERR_FILENO); > + /* this redirects stdout to /dev/null too, and closes parent pipe */ > + dup2(STDIN_FILENO, STDOUT_FILENO); > + } > + Half of this is already done in client_thread, and that would be the place where you should add dup2(0, 1). Also, the chdir can be moved earlier, after bdrv_open. Paolo