Home | History | Annotate | Download | only in mlockall
      1 /*
      2  *  This program is free software; you can redistribute it and/or modify
      3  *  it under the terms of the GNU General Public License version 2.
      4  *
      5  *  This program is distributed in the hope that it will be useful,
      6  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      7  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      8  *  GNU General Public License for more details.
      9  *
     10  * Test that mlockall lock the mapped files pages currently mapped into the
     11  * address space of the process when MCL_CURRENT is set.
     12  *
     13  * This test use msync to check that the page is locked.
     14  */
     15 #include <sys/mman.h>
     16 #include <stdio.h>
     17 #include <unistd.h>
     18 #include <errno.h>
     19 #include <fcntl.h>
     20 #include <stdlib.h>
     21 #include "posixtest.h"
     22 
     23 int main(void)
     24 {
     25 	void *page_ptr;
     26 	size_t page_size;
     27 	int result, fd;
     28 	void *foo;
     29 	char filename[] = "/tmp/mlockall_3-7-XXXXXX";
     30 
     31 	page_size = sysconf(_SC_PAGESIZE);
     32 	if (errno) {
     33 		perror("An error occurs when calling sysconf()");
     34 		return PTS_UNRESOLVED;
     35 	}
     36 
     37 	fd = mkstemp(filename);
     38 	if (fd == -1) {
     39 		perror("An error occurs when calling open()");
     40 		return PTS_UNRESOLVED;
     41 	}
     42 	unlink(filename);
     43 
     44 	foo = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
     45 	if (foo == MAP_FAILED) {
     46 		perror("An error occurs when calling mmap()");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	if (mlockall(MCL_CURRENT) == -1) {
     51 		if (errno == EPERM) {
     52 			printf
     53 			    ("You don't have permission to lock your address space.\nTry to rerun this test as root.\n");
     54 		} else {
     55 			perror("An error occurs when calling mlockall()");
     56 		}
     57 		return PTS_UNRESOLVED;
     58 	}
     59 
     60 	page_ptr = (void *)((long)foo - ((long)foo % page_size));
     61 
     62 	result = msync(page_ptr, page_size, MS_SYNC | MS_INVALIDATE);
     63 	if (result == -1 && errno == EBUSY) {
     64 		printf("Test PASSED\n");
     65 		return PTS_PASS;
     66 	} else if (result == 0) {
     67 		printf
     68 		    ("The mapped files pages of the process are not locked.\n");
     69 		return PTS_FAIL;
     70 	}
     71 	perror("Unexpected error");
     72 	return PTS_UNRESOLVED;
     73 }
     74