4 martin 1.2 // Licensed to The Open Group (TOG) under one or more contributor license
5 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
6 // this work for additional information regarding copyright ownership.
7 // Each contributor licenses this file to you under the OpenPegasus Open
8 // Source License; you may not use this file except in compliance with the
9 // License.
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 martin 1.2 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 //////////////////////////////////////////////////////////////////////////
|
32 kumpf 1.1 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include "FileHandle.h"
40
41 /*
42 **==============================================================================
43 **
44 ** RedirectTerminalIO()
45 **
46 ** Redirect stdin, stdout, and stderr to /dev/null.
47 **
48 **==============================================================================
49 */
50
51 void RedirectTerminalIO(void)
52 {
53 kumpf 1.1 /* Close these file descriptors (stdin, stdout, stderr). */
54
55 close(0);
56 close(1);
57 close(2);
58
59 /* Direct standard input, output, and error to /dev/null: */
60
61 open("/dev/null", O_RDONLY);
62 open("/dev/null", O_RDWR);
63 open("/dev/null", O_RDWR);
64 }
65
66 /*
67 **==============================================================================
68 **
69 ** CloseUnusedDescriptors()
70 **
71 ** Close all file descriptors except stdin, stdout, stderr, and the two
72 ** specified file descriptors.
73 **
74 kumpf 1.1 **==============================================================================
75 */
76
77 void CloseUnusedDescriptors(int fd1, int fd2)
78 {
79 struct rlimit rlim;
80
81 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
82 {
83 int i;
84
85 for (i = 3; i < (int)rlim.rlim_cur; i++)
86 {
87 if (i != fd1 && i != fd2)
88 {
89 close(i);
90 }
91 }
92 }
93 }
|