summaryrefslogtreecommitdiff
path: root/src/lib/evil
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2017-04-03 06:16:56 +0200
committerStefan Schmidt <stefan@osg.samsung.com>2017-04-03 11:24:34 +0200
commit58d52a23097416c0b935edf070723aafe6c2239c (patch)
tree1d3b7c4c5a405cd4ce4e59d959f4b90f88d505ca /src/lib/evil
parent87a25fd353ec245e3ff434ad964b223538092471 (diff)
Evil: fix fcntl() with sockets
On Windows, fd and sockets are different. Sockets are actually HANDLES, hence _get_osfhandle() should not be used with sockets, and GetHandleInformation() and SetHandleInformation() can be used directly with sockets. This is fixed by adding a function to test if the 'fd passed to fcntl() is a socket or not.
Diffstat (limited to 'src/lib/evil')
-rw-r--r--src/lib/evil/evil_fcntl.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/lib/evil/evil_fcntl.c b/src/lib/evil/evil_fcntl.c
index 188e476..df79c36 100644
--- a/src/lib/evil/evil_fcntl.c
+++ b/src/lib/evil/evil_fcntl.c
@@ -11,6 +11,21 @@
11#include "evil_macro.h" 11#include "evil_macro.h"
12#include "evil_fcntl.h" 12#include "evil_fcntl.h"
13 13
14/* SOCKET is defined as a uintptr_t, so passing a fd (int) is not a problem */
15static int
16_is_socket(SOCKET s)
17{
18 fd_set rfds;
19 struct timeval tv;
20
21 tv.tv_sec = 0.00000001;
22 tv.tv_usec = 0;
23 FD_ZERO(&rfds);
24 FD_SET(s, &rfds);
25
26 return select(1, &rfds, NULL, NULL, &tv) != SOCKET_ERROR;
27}
28
14 29
15/* 30/*
16 * port of fcntl function 31 * port of fcntl function
@@ -29,17 +44,19 @@ int fcntl(int fd, int cmd, ...)
29 HANDLE h; 44 HANDLE h;
30 DWORD flag; 45 DWORD flag;
31 46
32 h = (HANDLE)_get_osfhandle(fd); 47 h = _is_socket(fd) ? (HANDLE)fd : (HANDLE)_get_osfhandle(fd);
33 if (h == INVALID_HANDLE_VALUE) 48 if (h == INVALID_HANDLE_VALUE)
34 return -1; 49 return -1;
35 50
36 if (!GetHandleInformation(h, &flag)) 51 if (GetHandleInformation(h, &flag))
37 { 52 {
38 /* FIXME: should we close h ? MSDN seems to say that */ 53 if (flag == HANDLE_FLAG_INHERIT)
39 return -1; 54 return FD_CLOEXEC;
55
56 return 0;
40 } 57 }
41 58
42 res = 0; 59 return -1;
43 } 60 }
44 61
45 if (cmd == F_SETFD) 62 if (cmd == F_SETFD)
@@ -47,7 +64,7 @@ int fcntl(int fd, int cmd, ...)
47 HANDLE h; 64 HANDLE h;
48 long flag; 65 long flag;
49 66
50 h = (HANDLE)_get_osfhandle(fd); 67 h = _is_socket(fd) ? (HANDLE)fd : (HANDLE)_get_osfhandle(fd);
51 if (h == INVALID_HANDLE_VALUE) 68 if (h == INVALID_HANDLE_VALUE)
52 return -1; 69 return -1;
53 70
@@ -55,7 +72,7 @@ int fcntl(int fd, int cmd, ...)
55 if (flag == FD_CLOEXEC) 72 if (flag == FD_CLOEXEC)
56 { 73 {
57 if (SetHandleInformation(h, HANDLE_FLAG_INHERIT, 0)) 74 if (SetHandleInformation(h, HANDLE_FLAG_INHERIT, 0))
58 res = 0; 75 return 0;
59 } 76 }
60 } 77 }
61 else if (cmd == F_GETFL) 78 else if (cmd == F_GETFL)