1 /* $Id: loan1.c,v 1.5 2006/05/10 19:07:22 mrg Exp $ */
4 * Copyright (c)2004 YAMAMOTO Takashi,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/socket.h>
39 #define BUFSIZE (32 * 1024) /* enough size to trigger sosend_loan */
43 void testloan(void *, void *, char, int);
44 int main(int, char *[]);
47 testloan(void *vp
, void *vp2
, char pat
, int docheck
)
58 memcpy(backup
, vp
, BUFSIZE
);
60 if (socketpair(AF_LOCAL
, SOCK_STREAM
, PF_UNSPEC
, fds
))
61 err(EXIT_FAILURE
, "socketpair");
64 if (setsockopt(fds
[1], SOL_SOCKET
, SO_RCVBUF
, &val
, sizeof(val
)))
65 err(EXIT_FAILURE
, "SO_RCVBUF");
68 if (setsockopt(fds
[0], SOL_SOCKET
, SO_SNDBUF
, &val
, sizeof(val
)))
69 err(EXIT_FAILURE
, "SO_SNDBUF");
71 if (fcntl(fds
[0], F_SETFL
, O_NONBLOCK
))
72 err(EXIT_FAILURE
, "fcntl");
74 nwritten
= write(fds
[0], vp
+ pgsize
, BUFSIZE
- pgsize
);
75 if (nwritten
== (ssize_t
)-1)
76 err(EXIT_FAILURE
, "write");
79 memset(vp2
, pat
, BUFSIZE
);
81 nread
= read(fds
[1], buf
+ pgsize
, BUFSIZE
- pgsize
);
82 if (nread
== (ssize_t
)-1)
83 err(EXIT_FAILURE
, "read");
85 if (nread
!= nwritten
)
86 errx(EXIT_FAILURE
, "short read");
88 if (docheck
&& memcmp(backup
, buf
+ pgsize
, nread
))
89 errx(EXIT_FAILURE
, "data mismatch");
91 if (close(fds
[0]) || close(fds
[1]))
92 err(EXIT_FAILURE
, "close");
96 main(int argc
, char *argv
[])
102 pgsize
= sysconf(_SC_PAGESIZE
);
104 err(EXIT_FAILURE
, "_SC_PAGESIZE");
106 fd
= open(argv
[1], O_RDWR
);
108 err(EXIT_FAILURE
, "open");
110 vp
= mmap(NULL
, BUFSIZE
, PROT_READ
|PROT_WRITE
, MAP_FILE
|MAP_PRIVATE
,
112 if (vp
== MAP_FAILED
)
113 err(EXIT_FAILURE
, "mmap");
116 testloan(vp
, vp2
, 'A', 0);
117 testloan(vp
, vp2
, 'B', 1);
119 if (munmap(vp
, BUFSIZE
))
120 err(EXIT_FAILURE
, "munmap");
122 vp
= mmap(NULL
, BUFSIZE
, PROT_READ
|PROT_WRITE
, MAP_FILE
|MAP_SHARED
,
124 if (vp
== MAP_FAILED
)
125 err(EXIT_FAILURE
, "mmap");
128 testloan(vp
, vp2
, 'C', 0);
129 testloan(vp
, vp2
, 'D', 1);
131 if (munmap(vp
, BUFSIZE
))
132 err(EXIT_FAILURE
, "munmap");
134 vp
= mmap(NULL
, BUFSIZE
, PROT_READ
|PROT_WRITE
, MAP_FILE
|MAP_SHARED
,
136 if (vp
== MAP_FAILED
)
137 err(EXIT_FAILURE
, "mmap");
138 vp2
= mmap(NULL
, BUFSIZE
, PROT_READ
|PROT_WRITE
, MAP_FILE
|MAP_SHARED
,
140 if (vp2
== MAP_FAILED
)
141 err(EXIT_FAILURE
, "mmap");
143 testloan(vp
, vp2
, 'E', 1);
145 if (munmap(vp
, BUFSIZE
))
146 err(EXIT_FAILURE
, "munmap");