Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/fdpass.c @ 1432:13c0031b2522 HEAD
Use ENOSYS rather than EINVAL for fd_send/fd_recv.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 07 May 2003 15:23:42 +0300 |
parents | 5693bddad736 |
children | 219b6e9e82f1 |
rev | line source |
---|---|
0 | 1 /* |
1338 | 2 fdpass.c - File descriptor passing between processes via UNIX sockets |
3 | |
4 This isn't fully portable, but pretty much all UNIXes nowadays should | |
5 support this. If you're having runtime problems, check the end of fd_read() | |
6 and play with the if condition. | |
0 | 7 |
1338 | 8 If this file doesn't compile at all, you should check if this is supported |
9 in your system at all. It may require some extra #define to enable it. | |
10 If not, you're pretty much out of luck. Cygwin didn't last I checked. | |
11 | |
12 Copyright (c) 2002-2003 Timo Sirainen | |
0 | 13 |
14 Permission is hereby granted, free of charge, to any person obtaining | |
15 a copy of this software and associated documentation files (the | |
16 "Software"), to deal in the Software without restriction, including | |
17 without limitation the rights to use, copy, modify, merge, publish, | |
18 distribute, sublicense, and/or sell copies of the Software, and to | |
19 permit persons to whom the Software is furnished to do so, subject to | |
20 the following conditions: | |
21 | |
22 The above copyright notice and this permission notice shall be | |
23 included in all copies or substantial portions of the Software. | |
24 | |
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
26 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
27 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
28 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
29 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
30 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
31 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
32 */ | |
33 | |
350 | 34 #define _XPG4_2 |
1372 | 35 |
1411
027a528ebf01
Define _XOPEN_SOURCE only for IRIX.
Timo Sirainen <tss@iki.fi>
parents:
1405
diff
changeset
|
36 #if defined(irix) || defined (__irix__) || defined(sgi) || defined (__sgi__) |
027a528ebf01
Define _XOPEN_SOURCE only for IRIX.
Timo Sirainen <tss@iki.fi>
parents:
1405
diff
changeset
|
37 # define _XOPEN_SOURCE /* for IRIX */ |
027a528ebf01
Define _XOPEN_SOURCE only for IRIX.
Timo Sirainen <tss@iki.fi>
parents:
1405
diff
changeset
|
38 #endif |
027a528ebf01
Define _XOPEN_SOURCE only for IRIX.
Timo Sirainen <tss@iki.fi>
parents:
1405
diff
changeset
|
39 |
1426
f5b3575194ed
Don't redefine _XOPEN_SOURCE_EXTENDED.
Timo Sirainen <tss@iki.fi>
parents:
1411
diff
changeset
|
40 #if !defined(_AIX) && !defined(_XOPEN_SOURCE_EXTENDED) |
1372 | 41 # define _XOPEN_SOURCE_EXTENDED /* for Tru64, breaks AIX */ |
42 #endif | |
0 | 43 |
44 #include "lib.h" | |
45 #include "fdpass.h" | |
46 | |
467
fd24b81a7f04
include sys/socket.h, not network.h. Required with solaris+ipv6 where
Timo Sirainen <tss@iki.fi>
parents:
350
diff
changeset
|
47 #include <sys/socket.h> |
0 | 48 #include <sys/un.h> |
49 #include <sys/uio.h> | |
50 | |
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
|
51 #ifndef CMSG_SPACE |
0 | 52 # define CMSG_ALIGN(len) \ |
53 (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) | |
54 # define CMSG_SPACE(len) \ | |
55 (CMSG_ALIGN(len) + CMSG_ALIGN(sizeof(struct cmsghdr))) | |
56 # define CMSG_LEN(len) \ | |
57 (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) | |
58 #endif | |
59 | |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
60 #ifdef SCM_RIGHTS |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
61 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
62 ssize_t fd_send(int handle, int send_fd, const void *data, size_t size) |
0 | 63 { |
64 struct msghdr msg; | |
65 struct iovec iov; | |
66 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
|
67 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
|
68 |
871 | 69 /* at least one byte is required to be sent with fd passing */ |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
70 i_assert(size > 0 && size < SSIZE_T_MAX); |
0 | 71 |
72 memset(&msg, 0, sizeof (struct msghdr)); | |
73 | |
74 iov.iov_base = (void *) data; | |
75 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
|
76 |
0 | 77 msg.msg_iov = &iov; |
78 msg.msg_iovlen = 1; | |
79 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
80 if (send_fd != -1) { |
871 | 81 /* set the control and controllen before CMSG_FIRSTHDR() */ |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
82 msg.msg_control = buf; |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
83 msg.msg_controllen = sizeof(buf); |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
84 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 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
|
89 *((int *) CMSG_DATA(cmsg)) = send_fd; |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
90 |
871 | 91 /* set the real length we want to use. it's different than |
92 sizeof(buf) in 64bit systems. */ | |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
93 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
|
94 } |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
95 |
0 | 96 return sendmsg(handle, &msg, 0); |
97 } | |
98 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
99 ssize_t fd_read(int handle, void *data, size_t size, int *fd) |
0 | 100 { |
101 struct msghdr msg; | |
102 struct iovec iov; | |
103 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
|
104 ssize_t ret; |
0 | 105 char buf[CMSG_SPACE(sizeof(int))]; |
106 | |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
107 i_assert(size > 0 && size < SSIZE_T_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
|
108 |
0 | 109 memset(&msg, 0, sizeof (struct msghdr)); |
110 | |
871 | 111 iov.iov_base = data; |
112 iov.iov_len = size; | |
113 | |
0 | 114 msg.msg_iov = &iov; |
115 msg.msg_iovlen = 1; | |
871 | 116 |
1105
02454eee1775
Have to check that msg_controllen is large enough, otherwise cmsg points to
Timo Sirainen <tss@iki.fi>
parents:
915
diff
changeset
|
117 memset(buf, 0, sizeof(buf)); |
871 | 118 msg.msg_control = buf; |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
119 msg.msg_controllen = sizeof(buf); |
0 | 120 |
121 ret = recvmsg(handle, &msg, 0); | |
915
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
122 if (ret <= 0) { |
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
123 *fd = -1; |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
124 return ret; |
915
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
125 } |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
126 |
1338 | 127 /* at least one byte transferred - we should have the fd now. |
128 do extra checks to make sure it really is an fd that is being | |
129 transferred to avoid potential DoS conditions. some systems don't | |
130 set all these values correctly however: | |
131 | |
132 Linux 2.0.x - cmsg_len, cmsg_level, cmsg_type are not set | |
133 Tru64 - msg_controllen isn't set */ | |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
134 cmsg = CMSG_FIRSTHDR(&msg); |
1338 | 135 if ( |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
136 #ifndef __osf__ /* Tru64 */ |
1338 | 137 msg.msg_controllen < CMSG_SPACE(sizeof(int)) || |
138 #endif | |
1173
07c8944fdd28
fd_read(): make sure it's SCM_RIGHTS that was transferred so that login
Timo Sirainen <tss@iki.fi>
parents:
1105
diff
changeset
|
139 cmsg == NULL || cmsg->cmsg_len < CMSG_LEN(sizeof(int)) || |
1174 | 140 cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
141 *fd = -1; |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
142 else |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
143 *fd = *((int *) CMSG_DATA(cmsg)); |
0 | 144 return ret; |
145 } | |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
146 |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
147 #else |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
148 # ifdef __GNUC__ |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
149 # 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
|
150 # endif |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
151 ssize_t fd_send(int handle __attr_unused__, int send_fd __attr_unused__, |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
152 const void *data __attr_unused__, size_t size __attr_unused__) |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
153 { |
1432
13c0031b2522
Use ENOSYS rather than EINVAL for fd_send/fd_recv.
Timo Sirainen <tss@iki.fi>
parents:
1428
diff
changeset
|
154 errno = ENOSYS; |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
155 return -1; |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
156 } |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
157 |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
158 ssize_t fd_read(int handle __attr_unused__, void *data __attr_unused__, |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
159 size_t size __attr_unused__, int *fd __attr_unused__) |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
160 { |
1432
13c0031b2522
Use ENOSYS rather than EINVAL for fd_send/fd_recv.
Timo Sirainen <tss@iki.fi>
parents:
1428
diff
changeset
|
161 errno = ENOSYS; |
1428
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
162 return -1; |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
163 } |
5693bddad736
Compile even if SCM_RIGHTS isn't supported.
Timo Sirainen <tss@iki.fi>
parents:
1426
diff
changeset
|
164 #endif |