Mercurial > hg > octave-shane > gnulib-hg
annotate lib/accept4.c @ 16326:067aa8df247a
accept4, fcntl, socket modules: Avoid warnings on x86_64 mingw64.
* lib/accept4.c (accept4): Use intptr_t to convert handle pointer to
an integer.
* lib/fcntl.c (dupfd): Likewise.
* lib/w32sock.h (SOCKET_TO_FD): Likewise.
author | Marc-André Lureau <marcandre.lureau@redhat.com> |
---|---|
date | Sat, 28 Jan 2012 13:23:31 +0100 |
parents | 59c686e5b2df |
children | bb182ee4a09d |
rev | line source |
---|---|
11898 | 1 /* Accept a connection on a socket, with specific opening flags. |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
15752
diff
changeset
|
2 Copyright (C) 2009-2012 Free Software Foundation, Inc. |
11898 | 3 |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2, or (at your option) | |
7 any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License along | |
15 with this program; if not, write to the Free Software Foundation, | |
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | |
17 | |
18 #include <config.h> | |
19 | |
20 /* Specification. */ | |
21 #include <sys/socket.h> | |
22 | |
23 #include <errno.h> | |
24 #include <fcntl.h> | |
25 #include "binary-io.h" | |
15752
b86e9061a6d0
New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
26 #include "msvc-nothrow.h" |
11898 | 27 |
28 #ifndef SOCK_CLOEXEC | |
29 # define SOCK_CLOEXEC 0 | |
30 #endif | |
31 | |
32 int | |
33 accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) | |
34 { | |
35 int fd; | |
36 | |
11899
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
37 #if HAVE_ACCEPT4 |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
38 # undef accept4 |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
39 /* Try the system call first, if it exists. (We may be running with a glibc |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
40 that has the function but with an older kernel that lacks it.) */ |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
41 { |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
42 /* Cache the information whether the system call really exists. */ |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
43 static int have_accept4_really; /* 0 = unknown, 1 = yes, -1 = no */ |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
44 if (have_accept4_really >= 0) |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
45 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
46 int result = accept4 (sockfd, addr, addrlen, flags); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
47 if (!(result < 0 && errno == ENOSYS)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
48 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
49 have_accept4_really = 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
50 return result; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
51 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
52 have_accept4_really = -1; |
11899
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
53 } |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
54 } |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
55 #endif |
f0c8cf1802a2
Tolerate declared but missing accept4 syscall.
Bruno Haible <bruno@clisp.org>
parents:
11898
diff
changeset
|
56 |
11898 | 57 /* Check the supported flags. */ |
58 if ((flags & ~(SOCK_CLOEXEC | O_TEXT | O_BINARY)) != 0) | |
59 { | |
60 errno = EINVAL; | |
61 return -1; | |
62 } | |
63 | |
64 fd = accept (sockfd, addr, addrlen); | |
65 if (fd < 0) | |
66 return -1; | |
67 | |
68 #if SOCK_CLOEXEC | |
69 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
16242
59c686e5b2df
Talk about "native Windows API", not "Woe32".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
70 /* Native Windows API. */ |
11898 | 71 if (flags & SOCK_CLOEXEC) |
72 { | |
73 HANDLE curr_process = GetCurrentProcess (); | |
74 HANDLE old_handle = (HANDLE) _get_osfhandle (fd); | |
75 HANDLE new_handle; | |
76 int nfd; | |
77 | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
78 if (!DuplicateHandle (curr_process, /* SourceProcessHandle */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
79 old_handle, /* SourceHandle */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
80 curr_process, /* TargetProcessHandle */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
81 (PHANDLE) &new_handle, /* TargetHandle */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
82 (DWORD) 0, /* DesiredAccess */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
83 FALSE, /* InheritHandle */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
84 DUPLICATE_SAME_ACCESS)) /* Options */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
85 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
86 close (fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
87 errno = EBADF; /* arbitrary */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
88 return -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
89 } |
11898 | 90 |
91 /* Closing fd before allocating the new fd ensures that the new fd will | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
92 have the minimum possible value. */ |
11898 | 93 close (fd); |
16326
067aa8df247a
accept4, fcntl, socket modules: Avoid warnings on x86_64 mingw64.
Marc-André Lureau <marcandre.lureau@redhat.com>
parents:
16242
diff
changeset
|
94 nfd = _open_osfhandle ((intptr_t) new_handle, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
95 O_NOINHERIT | (flags & (O_TEXT | O_BINARY))); |
11898 | 96 if (nfd < 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
97 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
98 CloseHandle (new_handle); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
99 return -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
100 } |
11898 | 101 return nfd; |
102 } | |
103 # else | |
104 /* Unix API. */ | |
105 if (flags & SOCK_CLOEXEC) | |
106 { | |
107 int fcntl_flags; | |
108 | |
109 if ((fcntl_flags = fcntl (fd, F_GETFD, 0)) < 0 | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
110 || fcntl (fd, F_SETFD, fcntl_flags | FD_CLOEXEC) == -1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
111 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
112 int saved_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
113 close (fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
114 errno = saved_errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
115 return -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11899
diff
changeset
|
116 } |
11898 | 117 } |
118 # endif | |
119 #endif | |
120 | |
121 #if O_BINARY | |
122 if (flags & O_BINARY) | |
123 setmode (fd, O_BINARY); | |
124 else if (flags & O_TEXT) | |
125 setmode (fd, O_TEXT); | |
126 #endif | |
127 | |
128 return fd; | |
129 } |