Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/fdpass.c @ 9266:cd29b745c8dd HEAD
configure: clock_gettime()'s -lrt adding dropped everything else from $LIBS.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 27 Jul 2009 06:32:42 -0400 |
parents | b9faf4db2a9f |
children | 00cd9aacd03c |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
7410
diff
changeset
|
1 /* Copyright (c) 2002-2009 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 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
42 /* 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
|
43 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
|
44 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
45 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
|
46 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
|
47 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
48 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
|
49 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
|
50 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 */ |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
63 |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
64 #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
|
65 /* 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
|
66 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
|
67 # undef CMSG_SPACE |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
68 # undef CMSG_LEN |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
69 # undef CMSG_DATA |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
70 # define CMSG_DATA(cmsg) ((char *)((cmsg) + 1)) |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
71 # define _CMSG_DATA_ALIGNMENT 4 |
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
72 # 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
|
73 #endif |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
74 |
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
|
75 #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
|
76 # 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
|
77 (((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
|
78 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
79 /* 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
|
80 # 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
|
81 # define _CMSG_DATA_ALIGNMENT sizeof(size_t) |
2127 | 82 # endif |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
83 /* 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
|
84 # 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
|
85 # 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
|
86 # endif |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
87 |
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
88 # 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
|
89 (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
|
90 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
|
91 # 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
|
92 (MY_ALIGN(sizeof(struct cmsghdr), _CMSG_DATA_ALIGNMENT) + (len)) |
0 | 93 #endif |
94 | |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
95 #ifdef SCM_RIGHTS |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
96 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
97 ssize_t fd_send(int handle, int send_fd, const void *data, size_t size) |
0 | 98 { |
99 struct msghdr msg; | |
100 struct iovec iov; | |
101 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
|
102 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
|
103 |
871 | 104 /* 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
|
105 i_assert(size > 0 && size < INT_MAX); |
0 | 106 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
107 memset(&msg, 0, sizeof(struct msghdr)); |
0 | 108 |
109 iov.iov_base = (void *) data; | |
110 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
|
111 |
0 | 112 msg.msg_iov = &iov; |
113 msg.msg_iovlen = 1; | |
114 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
115 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
|
116 /* 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
|
117 memset(buf, 0, sizeof(buf)); |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
118 msg.msg_control = buf; |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
119 msg.msg_controllen = sizeof(buf); |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
120 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
121 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
|
122 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
|
123 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
|
124 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
125 *((int *) CMSG_DATA(cmsg)) = send_fd; |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
126 |
2348
65433665256d
Added BUGGY_CMSG_HEADERS #define to workaround some OS bugs. Updated
Timo Sirainen <tss@iki.fi>
parents:
2253
diff
changeset
|
127 /* 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
|
128 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
|
129 in the end. */ |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
130 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
|
131 } |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
132 |
0 | 133 return sendmsg(handle, &msg, 0); |
134 } | |
135 | |
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
|
136 #ifdef __osf__ |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
137 # define CHECK_MSG(msg) TRUE /* Tru64 */ |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
138 #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
|
139 # define CHECK_MSG(msg) (msg).msg_controllen >= CMSG_SPACE(sizeof(int)) |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
140 #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
|
141 |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
142 #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
|
143 /* 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
|
144 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
|
145 # 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
|
146 #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
|
147 # define CHECK_CMSG(cmsg) \ |
1896 | 148 ((cmsg) != NULL && \ |
149 (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
|
150 (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
|
151 #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
|
152 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
153 ssize_t fd_read(int handle, void *data, size_t size, int *fd) |
0 | 154 { |
155 struct msghdr msg; | |
156 struct iovec iov; | |
157 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
|
158 ssize_t ret; |
0 | 159 char buf[CMSG_SPACE(sizeof(int))]; |
160 | |
2349
b65456de1b49
Check BUGGY_CMSG_MACROS in configure.
Timo Sirainen <tss@iki.fi>
parents:
2348
diff
changeset
|
161 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
|
162 |
0 | 163 memset(&msg, 0, sizeof (struct msghdr)); |
164 | |
871 | 165 iov.iov_base = data; |
166 iov.iov_len = size; | |
167 | |
0 | 168 msg.msg_iov = &iov; |
169 msg.msg_iovlen = 1; | |
871 | 170 |
1105
02454eee1775
Have to check that msg_controllen is large enough, otherwise cmsg points to
Timo Sirainen <tss@iki.fi>
parents:
915
diff
changeset
|
171 memset(buf, 0, sizeof(buf)); |
871 | 172 msg.msg_control = buf; |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
173 msg.msg_controllen = sizeof(buf); |
0 | 174 |
175 ret = recvmsg(handle, &msg, 0); | |
915
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
176 if (ret <= 0) { |
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
177 *fd = -1; |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
178 return ret; |
915
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
179 } |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
180 |
1338 | 181 /* at least one byte transferred - we should have the fd now. |
182 do extra checks to make sure it really is an fd that is being | |
183 transferred to avoid potential DoS conditions. some systems don't | |
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
|
184 set all these values correctly however so CHECK_MSG() and |
e1a7e38c17ed
Put the msg/cmsg checks in CHECK_MSG() and CHECK_CMSG() macros. Add support
Timo Sirainen <tss@iki.fi>
parents:
1740
diff
changeset
|
185 CHECK_CMSG() are somewhat system dependent */ |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
186 cmsg = CMSG_FIRSTHDR(&msg); |
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
|
187 if (!CHECK_MSG(msg) || !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
|
188 *fd = -1; |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
189 else |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
190 *fd = *((int *) CMSG_DATA(cmsg)); |
0 | 191 return ret; |
192 } | |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
193 |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
194 #else |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
195 # ifdef __GNUC__ |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
196 # 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
|
197 # endif |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
2389
diff
changeset
|
198 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
|
199 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
|
200 { |
1432
13c0031b2522
Use ENOSYS rather than EINVAL for fd_send/fd_recv.
Timo Sirainen <tss@iki.fi>
parents:
1428
diff
changeset
|
201 errno = ENOSYS; |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
202 return -1; |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
203 } |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
204 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
2389
diff
changeset
|
205 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
|
206 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
|
207 { |
1432
13c0031b2522
Use ENOSYS rather than EINVAL for fd_send/fd_recv.
Timo Sirainen <tss@iki.fi>
parents:
1428
diff
changeset
|
208 errno = ENOSYS; |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
209 return -1; |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
210 } |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
211 #endif |