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 the goup ID of the shared memory object is set to the effective
11  * group ID of the process when the shared memory object does not exists and
12  * the O_CREAT flags is set.
13  *
14  * The test use fstat to check the flag.
15  */
16 
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include "posixtest.h"
24 
25 #define SHM_NAME "posixtest_17-1"
26 
main()27 int main(){
28 	int fd, result;
29 	struct stat stat_buf;
30 
31 	result = shm_unlink(SHM_NAME);
32 	if(result != 0 && errno != ENOENT) {
33 		/* The shared memory object exist and shm_unlink can not
34 		   remove it. */
35 		perror("An error occurs when calling shm_unlink()");
36 		return PTS_UNRESOLVED;
37 	}
38 
39 	fd = shm_open(SHM_NAME, O_RDONLY|O_CREAT, S_IRUSR|S_IWUSR);
40 	if(fd == -1) {
41 		perror("An error occurs when calling shm_open()");
42 		return PTS_UNRESOLVED;
43 	}
44 
45 	result = fstat(fd, &stat_buf);
46 	if(result != 0) {
47 		perror("An error occurs when calling fstat()");
48 		shm_unlink(SHM_NAME);
49 		return PTS_UNRESOLVED;
50 	}
51 
52 	shm_unlink(SHM_NAME);
53 
54 	if(stat_buf.st_gid == getegid()){
55 		printf("Test PASSED\n");
56 		return PTS_PASS;
57 	}
58 	printf("shm_open() does not set the user ID to the effective user ID of the process.\n");
59 	return PTS_FAIL;
60 }
61 
62