summaryrefslogtreecommitdiff
path: root/regress/sys/nfs/mmap-sysctl-copyin.c
blob: 9d333920d1281f7b5441da0f95d186d6b96f03c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*	$OpenBSD: mmap-sysctl-copyin.c,v 1.2 2017/01/13 01:33:14 bluhm Exp $	*/

#include <sys/param.h>
#include <sys/mman.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/sysctl.h>

#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>

#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define FILE	"sysctl-net.inet.tcp.always_keepalive"
#define CLIENT	"/mnt/regress-nfs-client"
#define SERVER	"/mnt/regress-nfs-server"

int
main(void)
{
	char *p, path[PATH_MAX];
	int mib[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_ALWAYS_KEEPALIVE };
	u_int miblen = sizeof(mib) / sizeof(mib[0]);
	int fd, val;
	size_t len;

	/*
	 * Get current value of sysctl net.inet.tcp.always_keepalive
	 * and write it into a file on the NFS server.
	 */
	snprintf(path, sizeof(path), "%s/%s", SERVER, FILE);
	if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0777)) == -1)
		err(1, "open write '%s'", path);
	len = sizeof(int);
	if (sysctl(mib, miblen, &val, &len, NULL, 0) == -1)
		err(1, "sysctl get keepalive");
	if (len != sizeof(int))
		errx(1, "len is not %zu: %zu", sizeof(int), len);
	if (write(fd, &val, len) == -1)
		err(1, "write");
	if (close(fd) == -1)
		err(1, "close write");

	/*
	 * Map file on NFS client and read value to
	 * sysctl net.inet.tcp.always_keepalive.
	 */
	snprintf(path, sizeof(path), "%s/%s", CLIENT, FILE);
	if ((fd = open(path, O_RDWR)) == -1)
		err(1, "open mmap '%s'", path);
	p = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	if (p == MAP_FAILED)
		err(1, "mmap");
	if (sysctl(mib, miblen, NULL, 0, p, sizeof(int)) == -1)
		err(1, "sysctl set keepalive");
	if (close(fd) == -1)
		err(1, "close mmap");

	return (0);
}