xref: /haiku/src/tests/system/kernel/transfer_area_test.cpp (revision c90684742e7361651849be4116d0e5de3a817194)
1 /*
2  * Copyright 2005-2009, 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,
36 			B_ANY_ADDRESS, B_PAGE_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
37 		if (area < 0) {
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 		area_id targetArea = _kern_transfer_area(area, (void **)&address,
45 			B_ANY_ADDRESS, info.team);
46 		if (targetArea < 0) {
47 			fprintf(stderr, "Could not transfer area: %s.\n",
48 				strerror(targetArea));
49 			return 1;
50 		}
51 
52 		write_port(port, targetArea, NULL, 0);
53 	} else {
54 		// we're the receiver
55 		port = create_port(1, kPortName);
56 		if (port < 0) {
57 			fprintf(stderr, "Could not create port: %s.\n", strerror(area));
58 			return 1;
59 		}
60 
61 		puts("Waiting for an area to be received (start command again with an "
62 			"argument)...");
63 
64 		ssize_t size;
65 		if ((size = read_port(port, (int32 *)&area, NULL, 0)) < B_OK) {
66 			fprintf(stderr, "Reading from port failed: %s.\n", strerror(size));
67 			return 1;
68 		}
69 
70 		printf("Received Area %ld\n", area);
71 
72 		area_info info;
73 		if (get_area_info(area, &info) != B_OK) {
74 			fprintf(stderr, "Could not get area info.\n");
75 			return 1;
76 		}
77 		printf("  name = \"%s\", base = %p, size = %#lx, team = %ld\n",
78 			info.name, info.address, info.size, info.team);
79 		printf("  contents: %s\n", (char *)info.address);
80 
81 		delete_area(area);
82 	}
83 
84 	return 0;
85 }
86