Mercurial > dovecot > core-2.2
annotate src/lib/fdpass.c @ 22664:fea53c2725c0
director: Fix director_max_parallel_moves/kicks type
Should be uint, not time.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 09 Nov 2017 12:24:16 +0200 |
parents | 2e2563132d5f |
children | cb108f786fb4 |
rev | line source |
---|---|
21390
2e2563132d5f
Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
19552
diff
changeset
|
1 /* Copyright (c) 2002-2017 Dovecot authors, see the included COPYING file */ |
1338 | 2 |
1740
219b6e9e82f1
Fix for IRIX (plus comment cleanups)
Timo Sirainen <tss@iki.fi>
parents:
1432
diff
changeset
|
3 /* |
219b6e9e82f1
Fix for IRIX (plus comment cleanups)
Timo Sirainen <tss@iki.fi>
parents:
1432
diff
changeset
|
4 fdpass.c - File descriptor passing between processes via UNIX sockets |
0 | 5 |
1740
219b6e9e82f1
Fix for IRIX (plus comment cleanups)
Timo Sirainen <tss@iki.fi>
parents:
1432
diff
changeset
|
6 This isn't fully portable, but pretty much all UNIXes nowadays should |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
7 support this. If you're having runtime problems with fd_read(), check the |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
8 end of fd_read() and play with the if condition. If you're having problems |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
9 with fd_send(), try defining BUGGY_CMSG_MACROS. |
0 | 10 |
1740
219b6e9e82f1
Fix for IRIX (plus comment cleanups)
Timo Sirainen <tss@iki.fi>
parents:
1432
diff
changeset
|
11 If this file doesn't compile at all, you should check if this is supported |
219b6e9e82f1
Fix for IRIX (plus comment cleanups)
Timo Sirainen <tss@iki.fi>
parents:
1432
diff
changeset
|
12 in your system at all. It may require some extra #define to enable it. |
219b6e9e82f1
Fix for IRIX (plus comment cleanups)
Timo Sirainen <tss@iki.fi>
parents:
1432
diff
changeset
|
13 If not, you're pretty much out of luck. Cygwin didn't last I checked. |
0 | 14 */ |
15 | |
350 | 16 #define _XPG4_2 |
1372 | 17 |
1411
027a528ebf01
Define _XOPEN_SOURCE only for IRIX.
Timo Sirainen <tss@iki.fi>
parents:
1405
diff
changeset
|
18 #if defined(irix) || defined (__irix__) || defined(sgi) || defined (__sgi__) |
1740
219b6e9e82f1
Fix for IRIX (plus comment cleanups)
Timo Sirainen <tss@iki.fi>
parents:
1432
diff
changeset
|
19 # define _XOPEN_SOURCE 4 /* for IRIX */ |
1411
027a528ebf01
Define _XOPEN_SOURCE only for IRIX.
Timo Sirainen <tss@iki.fi>
parents:
1405
diff
changeset
|
20 #endif |
027a528ebf01
Define _XOPEN_SOURCE only for IRIX.
Timo Sirainen <tss@iki.fi>
parents:
1405
diff
changeset
|
21 |
1426
f5b3575194ed
Don't redefine _XOPEN_SOURCE_EXTENDED.
Timo Sirainen <tss@iki.fi>
parents:
1411
diff
changeset
|
22 #if !defined(_AIX) && !defined(_XOPEN_SOURCE_EXTENDED) |
1372 | 23 # define _XOPEN_SOURCE_EXTENDED /* for Tru64, breaks AIX */ |
24 #endif | |
0 | 25 |
2389 | 26 #ifdef HAVE_CONFIG_H |
27 # include "lib.h" | |
28 #else | |
29 # define i_assert(x) | |
30 #endif | |
31 | |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
32 #include <string.h> |
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
33 #include <limits.h> |
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
34 #include <sys/types.h> |
0 | 35 |
467
fd24b81a7f04
include sys/socket.h, not network.h. Required with solaris+ipv6 where
Timo Sirainen <tss@iki.fi>
parents:
350
diff
changeset
|
36 #include <sys/socket.h> |
0 | 37 #include <sys/un.h> |
38 #include <sys/uio.h> | |
39 | |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
40 #include "fdpass.h" |
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
41 |
11838
fd79394f7652
configure wasn't compiling fd pass checking program correctly.
Timo Sirainen <tss@iki.fi>
parents:
10998
diff
changeset
|
42 #ifndef HAVE_CONFIG_H |
fd79394f7652
configure wasn't compiling fd pass checking program correctly.
Timo Sirainen <tss@iki.fi>
parents:
10998
diff
changeset
|
43 struct const_iovec { |
fd79394f7652
configure wasn't compiling fd pass checking program correctly.
Timo Sirainen <tss@iki.fi>
parents:
10998
diff
changeset
|
44 const void *iov_base; |
fd79394f7652
configure wasn't compiling fd pass checking program correctly.
Timo Sirainen <tss@iki.fi>
parents:
10998
diff
changeset
|
45 size_t iov_len; |
fd79394f7652
configure wasn't compiling fd pass checking program correctly.
Timo Sirainen <tss@iki.fi>
parents:
10998
diff
changeset
|
46 }; |
fd79394f7652
configure wasn't compiling fd pass checking program correctly.
Timo Sirainen <tss@iki.fi>
parents:
10998
diff
changeset
|
47 #endif |
fd79394f7652
configure wasn't compiling fd pass checking program correctly.
Timo Sirainen <tss@iki.fi>
parents:
10998
diff
changeset
|
48 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
49 /* RFC 2292 defines CMSG_*() macros, but some operating systems don't have them |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
50 so we'll define our own if they don't exist. |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
51 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
52 CMSG_LEN(data) is used to calculate size of sizeof(struct cmsghdr) + |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
53 sizeof(data) and padding between them. |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
54 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
55 CMSG_SPACE(data) also calculates the padding needed after the data, in case |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
56 multiple objects are sent. |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
57 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
58 cmsghdr contains cmsg_len field and two integers. cmsg_len is sometimes |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
59 defined as sockaddr_t and sometimes size_t, so it can be either 32bit or |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
60 64bit. This padding is added by compiler in sizeof(struct cmsghdr). |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
61 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
62 Padding required by CMSG_DATA() can vary. Usually it wants size_t or 32bit. |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
63 With Solaris it's in _CMSG_DATA_ALIGNMENT (32bit), we assume others want |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
64 size_t. |
2253
509923906599
added comment about solaris and changed the macros once again, although may
Timo Sirainen <tss@iki.fi>
parents:
2185
diff
changeset
|
65 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
66 We don't really need CMSG_SPACE() to be exactly correct, because currently |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
67 we send only one object at a time. But anyway I'm trying to keep that |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
68 correct in case it's sometimes needed.. |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
69 */ |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
70 |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
71 #ifdef BUGGY_CMSG_MACROS |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
72 /* Some OSes have broken CMSG macros in 64bit systems. The macros use 64bit |
7410
b3c900eb7e83
Apparently "alignmentation" isn't a word. Changed to "alignment".
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
73 alignment while kernel uses 32bit alignment. */ |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
74 # undef CMSG_SPACE |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
75 # undef CMSG_LEN |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
76 # undef CMSG_DATA |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
77 # define CMSG_DATA(cmsg) ((char *)((cmsg) + 1)) |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
78 # define _CMSG_DATA_ALIGNMENT 4 |
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
79 # define _CMSG_HDR_ALIGNMENT 4 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
80 #endif |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
81 |
345
9efeeb4ac5a9
Define CMSG_SPACE always if it's undefined, not only if __sun__ is defined.
Timo Sirainen <tss@iki.fi>
parents:
183
diff
changeset
|
82 #ifndef CMSG_SPACE |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
83 # define MY_ALIGN(len, align) \ |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
84 (((len) + align - 1) & ~(align - 1)) |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
85 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
86 /* Alignment between cmsghdr and data */ |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
87 # ifndef _CMSG_DATA_ALIGNMENT |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
88 # define _CMSG_DATA_ALIGNMENT sizeof(size_t) |
2127 | 89 # endif |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
90 /* Alignment between data and next cmsghdr */ |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
91 # ifndef _CMSG_HDR_ALIGNMENT |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
92 # define _CMSG_HDR_ALIGNMENT sizeof(size_t) |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
93 # endif |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
94 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
95 # define CMSG_SPACE(len) \ |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
96 (MY_ALIGN(sizeof(struct cmsghdr), _CMSG_DATA_ALIGNMENT) + \ |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
97 MY_ALIGN(len, _CMSG_HDR_ALIGNMENT)) |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
98 # define CMSG_LEN(len) \ |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
99 (MY_ALIGN(sizeof(struct cmsghdr), _CMSG_DATA_ALIGNMENT) + (len)) |
0 | 100 #endif |
101 | |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
102 #ifdef SCM_RIGHTS |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
103 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
104 ssize_t fd_send(int handle, int send_fd, const void *data, size_t size) |
0 | 105 { |
106 struct msghdr msg; | |
10998
be5b93c18203
Avoid casting away const qualifiers wherever possible.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
107 struct const_iovec iov; |
0 | 108 struct cmsghdr *cmsg; |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
107
diff
changeset
|
109 char buf[CMSG_SPACE(sizeof(int))]; |
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
107
diff
changeset
|
110 |
871 | 111 /* at least one byte is required to be sent with fd passing */ |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
112 i_assert(size > 0 && size < INT_MAX); |
0 | 113 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
114 memset(&msg, 0, sizeof(struct msghdr)); |
0 | 115 |
10998
be5b93c18203
Avoid casting away const qualifiers wherever possible.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
116 iov.iov_base = data; |
0 | 117 iov.iov_len = size; |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
118 |
10998
be5b93c18203
Avoid casting away const qualifiers wherever possible.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
119 msg.msg_iov = (void *)&iov; |
0 | 120 msg.msg_iovlen = 1; |
121 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
122 if (send_fd != -1) { |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
123 /* set the control and controllen before CMSG_FIRSTHDR(). */ |
2185
078da7741971
Fix for the previous Solaris fix. fd_send() was sending two fds.
Timo Sirainen <tss@iki.fi>
parents:
2127
diff
changeset
|
124 memset(buf, 0, sizeof(buf)); |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
125 msg.msg_control = buf; |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
126 msg.msg_controllen = sizeof(buf); |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
127 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
128 cmsg = CMSG_FIRSTHDR(&msg); |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
129 cmsg->cmsg_level = SOL_SOCKET; |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
130 cmsg->cmsg_type = SCM_RIGHTS; |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
131 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
12130
bdf922d26534
fdpass: Use memcpy() to access data in CMSG_DATA().
Timo Sirainen <tss@iki.fi>
parents:
12083
diff
changeset
|
132 memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd)); |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
133 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
134 /* set the real length we want to use. Do it after all is |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
135 set just in case CMSG macros required the extra padding |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
136 in the end. */ |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
137 msg.msg_controllen = cmsg->cmsg_len; |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
138 } |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
139 |
0 | 140 return sendmsg(handle, &msg, 0); |
141 } | |
142 | |
1794
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
143 #ifdef LINUX20 |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
144 /* Linux 2.0.x doesn't set any cmsg fields. Note that this might make some |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
145 attacks possible so don't do it unless you really have to. */ |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
146 # define CHECK_CMSG(cmsg) ((cmsg) != NULL) |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
147 #else |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
148 # define CHECK_CMSG(cmsg) \ |
1896 | 149 ((cmsg) != NULL && \ |
150 (size_t)(cmsg)->cmsg_len >= (size_t)CMSG_LEN(sizeof(int)) && \ | |
1794
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
151 (cmsg)->cmsg_level == SOL_SOCKET && (cmsg)->cmsg_type == SCM_RIGHTS) |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
152 #endif |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
153 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
154 ssize_t fd_read(int handle, void *data, size_t size, int *fd) |
0 | 155 { |
156 struct msghdr msg; | |
157 struct iovec iov; | |
158 struct cmsghdr *cmsg; | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
159 ssize_t ret; |
0 | 160 char buf[CMSG_SPACE(sizeof(int))]; |
161 | |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
162 i_assert(size > 0 && size < INT_MAX); |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
107
diff
changeset
|
163 |
0 | 164 memset(&msg, 0, sizeof (struct msghdr)); |
165 | |
871 | 166 iov.iov_base = data; |
167 iov.iov_len = size; | |
168 | |
0 | 169 msg.msg_iov = &iov; |
170 msg.msg_iovlen = 1; | |
871 | 171 |
1105
02454eee1775
Have to check that msg_controllen is large enough, otherwise cmsg points to
Timo Sirainen <tss@iki.fi>
parents:
915
diff
changeset
|
172 memset(buf, 0, sizeof(buf)); |
871 | 173 msg.msg_control = buf; |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
174 msg.msg_controllen = sizeof(buf); |
0 | 175 |
176 ret = recvmsg(handle, &msg, 0); | |
915
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
177 if (ret <= 0) { |
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
178 *fd = -1; |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
179 return ret; |
915
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
180 } |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
181 |
1338 | 182 /* at least one byte transferred - we should have the fd now. |
183 do extra checks to make sure it really is an fd that is being | |
184 transferred to avoid potential DoS conditions. some systems don't | |
17515
38cd37cea8b1
lib: fd_recv() no longer checks for msghdr.msg_controllen
Timo Sirainen <tss@iki.fi>
parents:
17356
diff
changeset
|
185 set all these values correctly however so CHECK_CMSG() is somewhat |
38cd37cea8b1
lib: fd_recv() no longer checks for msghdr.msg_controllen
Timo Sirainen <tss@iki.fi>
parents:
17356
diff
changeset
|
186 system dependent */ |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
187 cmsg = CMSG_FIRSTHDR(&msg); |
17515
38cd37cea8b1
lib: fd_recv() no longer checks for msghdr.msg_controllen
Timo Sirainen <tss@iki.fi>
parents:
17356
diff
changeset
|
188 if (!CHECK_CMSG(cmsg)) |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
189 *fd = -1; |
12130
bdf922d26534
fdpass: Use memcpy() to access data in CMSG_DATA().
Timo Sirainen <tss@iki.fi>
parents:
12083
diff
changeset
|
190 else |
bdf922d26534
fdpass: Use memcpy() to access data in CMSG_DATA().
Timo Sirainen <tss@iki.fi>
parents:
12083
diff
changeset
|
191 memcpy(fd, CMSG_DATA(cmsg), sizeof(*fd)); |
0 | 192 return ret; |
193 } | |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
194 |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
195 #else |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
196 # ifdef __GNUC__ |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
197 # warning SCM_RIGHTS not supported, privilege separation not possible |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
198 # endif |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
2389
diff
changeset
|
199 ssize_t fd_send(int handle ATTR_UNUSED, int send_fd ATTR_UNUSED, |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
2389
diff
changeset
|
200 const void *data ATTR_UNUSED, size_t size ATTR_UNUSED) |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
201 { |
1432
13c0031b2522
Use ENOSYS rather than EINVAL for fd_send/fd_recv.
Timo Sirainen <tss@iki.fi>
parents:
1428
diff
changeset
|
202 errno = ENOSYS; |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
203 return -1; |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
204 } |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
205 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
2389
diff
changeset
|
206 ssize_t fd_read(int handle ATTR_UNUSED, void *data ATTR_UNUSED, |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
2389
diff
changeset
|
207 size_t size ATTR_UNUSED, int *fd ATTR_UNUSED) |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
208 { |
1432
13c0031b2522
Use ENOSYS rather than EINVAL for fd_send/fd_recv.
Timo Sirainen <tss@iki.fi>
parents:
1428
diff
changeset
|
209 errno = ENOSYS; |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
210 return -1; |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
211 } |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
212 #endif |