Little test for set_area_protection()'s B_READ_AREA -> B_WRITE_AREA behavior.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22123 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2007-08-30 20:39:53 +00:00
parent 7ebe0d01cf
commit 68f2081497
2 changed files with 120 additions and 0 deletions
+4
View File
@@ -44,6 +44,10 @@ SetSupportedPlatformsForTarget sigint_bug113_test
: $(HAIKU_BEOS_COMPATIBLE_PLATFORMS) ;
SimpleTest sigint_bug113_test : sigint_bug113_test.cpp ;
SetSupportedPlatformsForTarget set_area_protection_test1
: $(HAIKU_BEOS_COMPATIBLE_PLATFORMS) ;
SimpleTest set_area_protection_test1 : set_area_protection_test1.cpp ;
SubInclude HAIKU_TOP src tests system kernel cache ;
#SubInclude HAIKU_TOP src tests system kernel disk_device_manager ;
@@ -0,0 +1,116 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <OS.h>
static area_id
create_test_area(const char* name, int** address, uint32 protection)
{
area_id area = create_area(name, (void**)address, B_ANY_ADDRESS,
B_PAGE_SIZE, B_NO_LOCK, protection);
if (area < 0) {
fprintf(stderr, "Error: Failed to create area %s: %s\n", name,
strerror(area));
exit(1);
}
return area;
}
static area_id
clone_test_area(const char* name, int** address, uint32 protection,
area_id source)
{
area_id area = clone_area(name, (void**)address, B_ANY_ADDRESS,
protection, source);
if (area < 0) {
fprintf(stderr, "Error: Failed to clone area %s: %s\n", name,
strerror(area));
exit(1);
}
return area;
}
int
main()
{
// allocate read-only areas
const int kAreaCount = 3;
area_id areas[kAreaCount];
int* areaAddresses[kAreaCount];
areas[0] = create_test_area("area0", &areaAddresses[0], B_READ_AREA);
areas[1] = create_test_area("area1", &areaAddresses[1], B_READ_AREA);
areas[2] = create_test_area("area2", &areaAddresses[2], B_READ_AREA);
int* area2CloneAddress;
/*area_id area2Clone =*/ clone_test_area("area2clone", &area2CloneAddress,
B_READ_AREA | B_WRITE_AREA, areas[2]);
for (int i = 0; i < kAreaCount; i++)
printf("parent: areas[%d]: %ld, %p\n", i, areas[i], areaAddresses[i]);
// fork()
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "Error: Failed to fork(): %s\n", strerror(errno));
exit(1);
}
if (pid == 0) {
// child
// get the IDs of the copied areas
area_id parentAreas[kAreaCount];
for (int i = 0; i < kAreaCount; i++) {
parentAreas[i] = areas[i];
areas[i] = area_for(areaAddresses[i]);
}
for (int i = 0; i < kAreaCount; i++) {
printf("child: areas[%d]: %ld, %p\n", i, areas[i],
areaAddresses[i]);
}
// clone area 1
delete_area(areas[1]);
areas[1] = clone_test_area("child:area1", &areaAddresses[1],
B_READ_AREA, parentAreas[1]);
// clone area 2
delete_area(areas[2]);
areas[2] = clone_test_area("child:area2", &areaAddresses[2],
B_READ_AREA, parentAreas[2]);
snooze(400000);
for (int i = 0; i < kAreaCount; i++)
printf("child: area[%d] contains: %d\n", i, *areaAddresses[i]);
} else {
// parent
snooze(200000);
for (int i = 0; i < kAreaCount; i++) {
status_t error = set_area_protection(areas[i],
B_READ_AREA | B_WRITE_AREA);
if (error == B_OK) {
*areaAddresses[i] = pid;
} else {
fprintf(stderr, "parent: Error: set_area_protection(areas[%d]) "
"failed: %s\n", i, strerror(error));
}
}
status_t result;
wait_for_thread(pid, &result);
}
return 0;
}