not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kdm / backend / daemon.c
blob7d6b543f5f7d4e133094c38083ee05631c64f735
1 /*
3 Copyright 1988, 1998 The Open Group
4 Copyright 2000,2001,2003 Oswald Buddenhagen <ossi@kde.org>
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of a copyright holder shall
24 not be used in advertising or otherwise to promote the sale, use or
25 other dealings in this Software without prior written authorization
26 from the copyright holder.
31 * xdm - display manager daemon
32 * Author: Keith Packard, MIT X Consortium
35 #include "dm.h"
36 #include "dm_error.h"
38 void
39 becomeDaemon( void )
41 int pfd[2];
44 * fork so that the process goes into the background automatically. Also
45 * has a nice side effect of having the child process get inherited by
46 * init (pid 1).
47 * Create a pipe and block on it, so the parent knows when the child is
48 * done with detaching. This eliminates the possibility that the child
49 * might get killed when the init script that's running xdm exits.
52 if (pipe( pfd ))
53 pfd[0] = pfd[1] = -1; /* so what ...? */
54 switch (fork ()) {
55 case 0:
56 /* child */
57 break;
58 case -1:
59 /* error */
60 logError( "Daemon fork failed: %m\n" );
61 break;
63 default:
64 /* parent */
65 close( pfd[1] );
66 read( pfd[0], &pfd[1] /* dummy */, 1 );
67 exit( 0 );
70 /* don't use daemon() - it doesn't buy us anything but an additional fork */
72 setsid();
74 close( pfd[0] );
75 close( pfd[1] ); /* tell parent that we're done with detaching */
77 chdir( "/" );