Use daemon(3) instead of forking manually

Message ID 87mzlk9vin.fsf@kosh.bigo.ensc.de
State New
Headers

Commit Message

Enrico Scholz Oct. 8, 2005, 10:02 a.m. UTC
  Hello,

the attached patch uses daemon(3) instead of forking manually. This
executes setsid(2) (which is missing currently) and gives better FDs 0-2
(it is a little bit unclean to use these standard fds for other things).

daemon(3) is called with a set 'nochdir' flag to allow core-file
generation in writable directories.



Enrico
  

Comments

Matthias Schwarzott Oct. 31, 2005, 5:41 p.m. UTC | #1
On Saturday 08 October 2005 12:02, Enrico Scholz wrote:
> Hello,
>
> the attached patch uses daemon(3) instead of forking manually. This
> executes setsid(2) (which is missing currently) and gives better FDs 0-2
> (it is a little bit unclean to use these standard fds for other things).
>
> daemon(3) is called with a set 'nochdir' flag to allow core-file
> generation in writable directories.
>
>

I tested this patch and would recommend to include it in vdr.

Matthias
  

Patch

2005-09-17 17:52  Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
	- vdr.c: use daemon(3) instead of forking manually; this
	  executes 'setsid()' and creates better FDs 0-2

--- vdr-1.3.32/vdr.c.daemon	2005-09-17 13:40:57.000000000 +0200
+++ vdr-1.3.32/vdr.c	2005-09-17 13:46:41.000000000 +0200
@@ -508,17 +508,11 @@ 
   // Daemon mode:
 
   if (DaemonMode) {
-     pid_t pid = fork();
-     if (pid < 0) {
-        fprintf(stderr, "%m\n");
+     if (daemon(1, 0)==-1) {
         esyslog("ERROR: %m");
+        perror("daemon()");
         return 2;
-        }
-     if (pid != 0)
-        return 0; // initial program immediately returns
-     fclose(stdin);
-     fclose(stdout);
-     fclose(stderr);
+       }
      }
   else if (Terminal) {
      // Claim new controlling terminal