xref: /haiku/src/tests/system/kernel/transfer_area_test.cpp (revision 3be9edf8da228afd9fec0390f408c964766122aa)
1 /*
2  * Copyright 2005, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <OS.h>
8 #include <syscalls.h>
9 
10 #include <stdio.h>
11 #include <string.h>
12 
13 
14 static const char *kPortName = "transfer area test";
15 
16 
17 int
18 main(int argc, char **argv)
19 {
20 	port_id port;
21 	area_id area;
22 
23 	if (argc > 1) {
24 		// we're the sender
25 		port = find_port(kPortName);
26 		if (port < B_OK) {
27 			fprintf(stderr, "Area receiver is not yet running.\n");
28 			return -1;
29 		}
30 
31 		port_info info;
32 		get_port_info(port, &info);
33 
34 		char *address;
35 		area = create_area("test transfer area", (void **)&address, B_ANY_ADDRESS, B_PAGE_SIZE,
36 			B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
37 		if (area < B_OK) {
38 			fprintf(stderr, "Could not create area: %s.\n", strerror(area));
39 			return -1;
40 		}
41 
42 		sprintf(address, "Oh my god - it's working! (%s)", argv[1]);
43 
44 		status_t status = _kern_transfer_area(area, (void **)&address, B_ANY_ADDRESS, info.team);
45 		if (status < B_OK) {
46 			fprintf(stderr, "Could not transfer area: %s.\n", strerror(status));
47 			return -1;
48 		}
49 
50 		write_port(port, area, NULL, 0);
51 	} else {
52 		// we're the receiver
53 		port = create_port(1, kPortName);
54 		if (port < B_OK) {
55 			fprintf(stderr, "Could not create port: %s.\n", strerror(area));
56 			return -1;
57 		}
58 
59 		puts("Waiting for an area to be received (start same command again with an argument)...");
60 
61 		ssize_t size;
62 		if ((size = read_port(port, (int32 *)&area, NULL, 0)) < B_OK) {
63 			fprintf(stderr, "Reading from port failed: %s.\n", strerror(size));
64 			return -1;
65 		}
66 
67 		printf("Received Area %ld\n", area);
68 
69 		area_info info;
70 		get_area_info(area, &info);
71 		printf("  name = \"%s\", base = %p, size = %#lx, team = %ld\n",
72 			info.name, info.address, info.size, info.team);
73 		printf("  contents: %s\n", (char *)info.address);
74 
75 		delete_area(area);
76 	}
77 
78 	return 0;
79 }
80