dtprintinfo: Coverity 89561
[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 libraries 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     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 #if defined(SYSV) || defined (SVR4) || defined(__linux__)
81     setpgrp ();
82 #else
83     setpgrp (0, getpid());
84 #endif
85
86     close (0); 
87     close (1);
88     close (2);
89
90     if ((i = open ("/dev/tty", O_RDWR)) >= 0) { /* did open succeed? */
91 #if defined(SYSV) && defined(TIOCTTY)
92         int zero = 0;
93         (void) ioctl (i, TIOCTTY, &zero);
94 #else
95         (void) ioctl (i, TIOCNOTTY, (char *) 0);    /* detach, BSD style */
96 #endif
97         (void) close (i);
98     }
99
100     /*
101      * Set up the standard file descriptors.
102      */
103     (void) open ("/", O_RDONLY);        /* root inode already in core */
104     (void) dup2 (0, 1);
105     (void) dup2 (0, 2);
106 }