Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/fdpass.c @ 870:971947c0034f HEAD
Fixed to work with 64bit BSD systems.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sat, 28 Dec 2002 21:20:37 +0200 |
parents | 7c91f579ebed |
children | 28212c00d147 |
rev | line source |
---|---|
0 | 1 /* |
28 | 2 fdpass.c - FD passing |
0 | 3 |
4 Copyright (c) 2002 Timo Sirainen | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining | |
7 a copy of this software and associated documentation files (the | |
8 "Software"), to deal in the Software without restriction, including | |
9 without limitation the rights to use, copy, modify, merge, publish, | |
10 distribute, sublicense, and/or sell copies of the Software, and to | |
11 permit persons to whom the Software is furnished to do so, subject to | |
12 the following conditions: | |
13 | |
14 The above copyright notice and this permission notice shall be | |
15 included in all copies or substantial portions of the Software. | |
16 | |
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 */ | |
25 | |
350 | 26 #define _XPG4_2 |
0 | 27 |
28 #include "lib.h" | |
29 #include "fdpass.h" | |
30 | |
467
fd24b81a7f04
include sys/socket.h, not network.h. Required with solaris+ipv6 where
Timo Sirainen <tss@iki.fi>
parents:
350
diff
changeset
|
31 #include <sys/socket.h> |
0 | 32 #include <sys/un.h> |
33 #include <sys/uio.h> | |
34 | |
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
|
35 #ifndef CMSG_SPACE |
0 | 36 # define CMSG_ALIGN(len) \ |
37 (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) | |
38 # define CMSG_SPACE(len) \ | |
39 (CMSG_ALIGN(len) + CMSG_ALIGN(sizeof(struct cmsghdr))) | |
40 # define CMSG_LEN(len) \ | |
41 (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) | |
42 #endif | |
43 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
44 ssize_t fd_send(int handle, int send_fd, const void *data, size_t size) |
0 | 45 { |
46 struct msghdr msg; | |
47 struct iovec iov; | |
48 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
|
49 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
|
50 |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
51 i_assert(size > 0 && size < SSIZE_T_MAX); |
0 | 52 |
53 memset(&msg, 0, sizeof (struct msghdr)); | |
54 | |
55 iov.iov_base = (void *) data; | |
56 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
|
57 |
0 | 58 msg.msg_iov = &iov; |
59 msg.msg_iovlen = 1; | |
60 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
61 if (send_fd != -1) { |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
62 msg.msg_control = buf; |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
63 msg.msg_controllen = sizeof(buf); |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
64 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 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
|
69 *((int *) CMSG_DATA(cmsg)) = send_fd; |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
70 |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
71 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
|
72 } |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
73 |
0 | 74 return sendmsg(handle, &msg, 0); |
75 } | |
76 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
77 ssize_t fd_read(int handle, void *data, size_t size, int *fd) |
0 | 78 { |
79 struct msghdr msg; | |
80 struct iovec iov; | |
81 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
|
82 ssize_t ret; |
0 | 83 char buf[CMSG_SPACE(sizeof(int))]; |
84 | |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
85 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
|
86 |
0 | 87 memset(&msg, 0, sizeof (struct msghdr)); |
88 | |
89 msg.msg_control = buf; | |
90 msg.msg_controllen = sizeof(buf); | |
91 msg.msg_iov = &iov; | |
92 msg.msg_iovlen = 1; | |
93 cmsg = CMSG_FIRSTHDR(&msg); | |
94 cmsg->cmsg_level = SOL_SOCKET; | |
95 cmsg->cmsg_type = SCM_RIGHTS; | |
107 | 96 iov.iov_base = data; |
0 | 97 iov.iov_len = size; |
98 | |
99 ret = recvmsg(handle, &msg, 0); | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
100 if (msg.msg_controllen != sizeof(buf)) |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
101 *fd = -1; |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
102 else |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
103 *fd = *(int *) CMSG_DATA(cmsg); |
0 | 104 return ret; |
105 } |