Postfix3.3.1
dup2_pass_on_exec.c
[詳解]
1 /*++
2 /* NAME
3 /* dup2_pass_on_exec 1
4 /* SUMMARY
5 /* dup2 close-on-exec behaviour test program
6 /* SYNOPSIS
7 /* dup2_pass_on_exec
8 /* DESCRIPTION
9 /* dup2_pass_on_exec sets the close-on-exec flag on its
10 /* standard input and then dup2() to duplicate it.
11 /* Posix-1003.1 specifies in section 6.2.1.2 that dup2(o,n) should behave
12 /* as: close(n); n = fcntl(o, F_DUPFD, n); as long as o is a valid
13 /* file-descriptor, n!=o, and 0<=n<=[OPEN_MAX].
14 /* Section 6.5.2.2 states that the close-on-exec flag of the result of a
15 /* successful fcntl(o, F_DUPFD, n) is cleared.
16 /*
17 /* At least Ultrix4.3a does not clear the close-on-exec flag of n on
18 /* dup2(o, n).
19 /* DIAGNOSTICS
20 /* Problems are reported to the standard error stream.
21 /* LICENSE
22 /* .ad
23 /* .fi
24 /* The Secure Mailer license must be distributed with this software.
25 /* AUTHOR(S)
26 /* Christian von Roques <roques@pond.sub.org>
27 /* Forststrasse 71
28 /* 76131 Karlsruhe, GERMANY
29 /*--*/
30 
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 
36 #define DO(s) if (s < 0) { perror(#s); exit(1); }
37 
38 int main(int unused_argc, char **unused_argv)
39 {
40  int res;
41 
42  printf("Setting the close-on-exec flag of file-descriptor 0.\n");
43  DO(fcntl(0, F_SETFD, 1));
44 
45  printf("Duplicating file-descriptor 0 to 3.\n");
46  DO(dup2(0, 3));
47 
48  printf("Testing if the close-on-exec flag of file-descriptor 3 is set.\n");
49  DO((res = fcntl(3, F_GETFD, 0)));
50  if (res & 1)
51  printf(
52 "Yes, a newly dup2()ed file-descriptor has the close-on-exec \
53 flag cloned.\n\
54 THIS VIOLATES Posix1003.1 section 6.2.1.2 or 6.5.2.2!\n\
55 You should #define DUP2_DUPS_CLOSE_ON_EXEC in sys_defs.h \
56 for your OS.\n");
57  else
58  printf(
59 "No, a newly dup2()ed file-descriptor has the close-on-exec \
60 flag cleared.\n\
61 This complies with Posix1003.1 section 6.2.1.2 and 6.5.2.2!\n");
62 
63  return 0;
64 }
#define DO(s)
int main(int unused_argc, char **unused_argv)