Sync usage with man page.
[netbsd-mini2440.git] / regress / sys / uvm / loan1 / loan1.c
blobabfce25564a00ffd5267cdda55f2352bfc214129
1 /* $Id: loan1.c,v 1.5 2006/05/10 19:07:22 mrg Exp $ */
3 /*-
4 * Copyright (c)2004 YAMAMOTO Takashi,
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
29 #include <sys/mman.h>
30 #include <sys/socket.h>
32 #include <err.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #define BUFSIZE (32 * 1024) /* enough size to trigger sosend_loan */
41 int pgsize;
43 void testloan(void *, void *, char, int);
44 int main(int, char *[]);
46 void
47 testloan(void *vp, void *vp2, char pat, int docheck)
49 char buf[BUFSIZE];
50 char backup[BUFSIZE];
52 int fds[2];
53 int val;
54 ssize_t nwritten;
55 ssize_t nread;
57 if (docheck)
58 memcpy(backup, vp, BUFSIZE);
60 if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, fds))
61 err(EXIT_FAILURE, "socketpair");
63 val = BUFSIZE;
64 if (setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)))
65 err(EXIT_FAILURE, "SO_RCVBUF");
67 val = BUFSIZE;
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");
78 /* break loan */
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");
95 int
96 main(int argc, char *argv[])
98 void *vp;
99 void *vp2;
100 int fd;
102 pgsize = sysconf(_SC_PAGESIZE);
103 if (pgsize == -1)
104 err(EXIT_FAILURE, "_SC_PAGESIZE");
106 fd = open(argv[1], O_RDWR);
107 if (fd == -1)
108 err(EXIT_FAILURE, "open");
110 vp = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE,
111 fd, 0);
112 if (vp == MAP_FAILED)
113 err(EXIT_FAILURE, "mmap");
114 vp2 = vp;
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,
123 fd, 0);
124 if (vp == MAP_FAILED)
125 err(EXIT_FAILURE, "mmap");
126 vp2 = vp;
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,
135 fd, 0);
136 if (vp == MAP_FAILED)
137 err(EXIT_FAILURE, "mmap");
138 vp2 = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
139 fd, 0);
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");
148 exit(EXIT_SUCCESS);