Use C++ linker
[oweals/cde.git] / cde / programs / dtlogin / daemon.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*                                                                      *
24  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
25  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
26  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
27  * (c) Copyright 1993, 1994 Novell, Inc.                                *
28  */
29 /*
30  * xdm - display manager daemon
31  *
32  * $TOG: daemon.c /main/5 1998/04/06 13:21:16 mgreess $
33  *
34  * Copyright 1988 Massachusetts Institute of Technology
35  *
36  * Permission to use, copy, modify, and distribute this software and its
37  * documentation for any purpose and without fee is hereby granted, provided
38  * that the above copyright notice appear in all copies and that both that
39  * copyright notice and this permission notice appear in supporting
40  * documentation, and that the name of M.I.T. not be used in advertising or
41  * publicity pertaining to distribution of the software without specific,
42  * written prior permission.  M.I.T. makes no representations about the
43  * suitability of this software for any purpose.  It is provided "as is"
44  * without express or implied warranty.
45  *
46  * Author:  Keith Packard, MIT X Consortium
47  */
48
49 #include <X11/Xos.h>
50 #include <sys/ioctl.h>
51 #ifdef hpux
52 #include <sys/ptyio.h>
53 #endif
54
55 #ifdef SVR4
56 #include <termio.h>
57 #endif
58
59 extern void exit ();
60
61
62 void
63 BecomeDaemon( void )
64 {
65     register int i;
66
67     /*
68      * fork so that the process goes into the background automatically. Also
69      * has a nice side effect of having the child process get inherited by
70      * init (pid 1).
71      */
72
73     if (fork ())                        /* if parent */
74       exit (0);                         /* then no more work to do */
75
76     /*
77      * Close standard file descriptors and get rid of controlling tty
78      */
79
80 #ifdef __osf__
81 /* use setsid() instead of setpgrp() */
82     setsid();
83 #else
84 #if defined(SYSV) || defined (SVR4) || defined(linux)
85     setpgrp ();
86 #else
87     setpgrp (0, getpid());
88 #endif
89 #endif /* __osf__ */
90
91     close (0); 
92     close (1);
93     close (2);
94
95     if ((i = open ("/dev/tty", O_RDWR)) >= 0) { /* did open succeed? */
96 #if defined(SYSV) && defined(TIOCTTY)
97         int zero = 0;
98         (void) ioctl (i, TIOCTTY, &zero);
99 #else
100         (void) ioctl (i, TIOCNOTTY, (char *) 0);    /* detach, BSD style */
101 #endif
102         (void) close (i);
103     }
104
105     /*
106      * Set up the standard file descriptors.
107      */
108     (void) open ("/", O_RDONLY);        /* root inode already in core */
109     (void) dup2 (0, 1);
110     (void) dup2 (0, 2);
111 }