Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/fdpass.c @ 1332:f2f3fe0916c1 HEAD
Compile fix for tru64
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 04 Apr 2003 15:09:27 +0300 |
parents | 00dd1db9e312 |
children | 04ed7a2536cc |
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 |
1332 | 27 #define _XOPEN_SOURCE_EXTENDED /* for Tru64 */ |
0 | 28 |
29 #include "lib.h" | |
30 #include "fdpass.h" | |
31 | |
467
fd24b81a7f04
include sys/socket.h, not network.h. Required with solaris+ipv6 where
Timo Sirainen <tss@iki.fi>
parents:
350
diff
changeset
|
32 #include <sys/socket.h> |
0 | 33 #include <sys/un.h> |
34 #include <sys/uio.h> | |
35 | |
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
|
36 #ifndef CMSG_SPACE |
0 | 37 # define CMSG_ALIGN(len) \ |
38 (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) | |
39 # define CMSG_SPACE(len) \ | |
40 (CMSG_ALIGN(len) + CMSG_ALIGN(sizeof(struct cmsghdr))) | |
41 # define CMSG_LEN(len) \ | |
42 (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) | |
43 #endif | |
44 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
45 ssize_t fd_send(int handle, int send_fd, const void *data, size_t size) |
0 | 46 { |
47 struct msghdr msg; | |
48 struct iovec iov; | |
49 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
|
50 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
|
51 |
871 | 52 /* 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
|
53 i_assert(size > 0 && size < SSIZE_T_MAX); |
0 | 54 |
55 memset(&msg, 0, sizeof (struct msghdr)); | |
56 | |
57 iov.iov_base = (void *) data; | |
58 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
|
59 |
0 | 60 msg.msg_iov = &iov; |
61 msg.msg_iovlen = 1; | |
62 | |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
63 if (send_fd != -1) { |
871 | 64 /* 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
|
65 msg.msg_control = buf; |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
66 msg.msg_controllen = sizeof(buf); |
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
67 |
612
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 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
|
72 *((int *) CMSG_DATA(cmsg)) = send_fd; |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
73 |
871 | 74 /* set the real length we want to use. it's different than |
75 sizeof(buf) in 64bit systems. */ | |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
76 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
|
77 } |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
78 |
0 | 79 return sendmsg(handle, &msg, 0); |
80 } | |
81 | |
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 fd_read(int handle, void *data, size_t size, int *fd) |
0 | 83 { |
84 struct msghdr msg; | |
85 struct iovec iov; | |
86 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
|
87 ssize_t ret; |
0 | 88 char buf[CMSG_SPACE(sizeof(int))]; |
89 | |
870
971947c0034f
Fixed to work with 64bit BSD systems.
Timo Sirainen <tss@iki.fi>
parents:
612
diff
changeset
|
90 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
|
91 |
0 | 92 memset(&msg, 0, sizeof (struct msghdr)); |
93 | |
871 | 94 iov.iov_base = data; |
95 iov.iov_len = size; | |
96 | |
0 | 97 msg.msg_iov = &iov; |
98 msg.msg_iovlen = 1; | |
871 | 99 |
1105
02454eee1775
Have to check that msg_controllen is large enough, otherwise cmsg points to
Timo Sirainen <tss@iki.fi>
parents:
915
diff
changeset
|
100 memset(buf, 0, sizeof(buf)); |
871 | 101 msg.msg_control = buf; |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
102 msg.msg_controllen = sizeof(buf); |
0 | 103 |
104 ret = recvmsg(handle, &msg, 0); | |
915
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
105 if (ret <= 0) { |
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
106 *fd = -1; |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
107 return ret; |
915
f33a139029db
Still some fixes, I broke Solaris 8.
Timo Sirainen <tss@iki.fi>
parents:
910
diff
changeset
|
108 } |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
109 |
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
110 /* at least one byte transferred - we should have the fd now */ |
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
111 cmsg = CMSG_FIRSTHDR(&msg); |
1105
02454eee1775
Have to check that msg_controllen is large enough, otherwise cmsg points to
Timo Sirainen <tss@iki.fi>
parents:
915
diff
changeset
|
112 if (msg.msg_controllen < CMSG_SPACE(sizeof(int)) || |
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
|
113 cmsg == NULL || cmsg->cmsg_len < CMSG_LEN(sizeof(int)) || |
1174 | 114 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
|
115 *fd = -1; |
7c91f579ebed
Some fixes, cleanups and possibility to send/receive data without actually
Timo Sirainen <tss@iki.fi>
parents:
467
diff
changeset
|
116 else |
910
e757c5fab691
fd_read() fixes, hopefully works better now.
Timo Sirainen <tss@iki.fi>
parents:
871
diff
changeset
|
117 *fd = *((int *) CMSG_DATA(cmsg)); |
0 | 118 return ret; |
119 } |