Debian Codegraph @

tests/shm-perms.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include <errno.h>
19 #include <memory.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <sys/shm.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <hugetlbfs.h>
27 #include "hugetests.h"
28
29 #define P "shm-perms"
30 #define DESC \
31 "* Test shared memory behavior when multiple threads are attached *\n"\
32 "* to a segment with different permissions. A segment is created *\n"\
33 "* and children attach read-only to check reservation accounting. *"
34
35 #define SEGMENT_SIZE ((size_t)0x4000000)
36 #define SEGMENT_KEY 0x82ba15ff
37 #define STRIDE 0x200000
38
39 static int global_shmid = -1;
40 void *shm_addr = NULL;
41
42 void cleanup(void)
43 {
44 remove_shmid(global_shmid);
45 }
46
47 int attach_segment(size_t segsize, int shmflags, int shmperms)
48 {
49 int shmid;
50
51 /* Create/get large segment */
52 shmid = shmget(SEGMENT_KEY, segsize, shmflags);
53 if (shmid == -1) {
54 perror("shmget(SEGMENT)");
55 cleanup();
56 exit(EXIT_FAILURE);
57 }
58
59 /* Attach large segment */
60 if ( (shm_addr = shmat(shmid, shm_addr, shmperms)) == (void *)-1) {
61 perror("shmat(SEGMENT)");
62 cleanup();
63 exit(EXIT_FAILURE);
64 }
65
66 global_shmid = shmid;
67 return shmid;
68 }
69
70 int main(int argc, char **argv)
71 {
72 char *p;
73 pid_t *wait_list;
74 int i, iterations;
75 long hpage_size = check_hugepagesize();
76 long total_hpages = get_huge_page_counter(hpage_size, HUGEPAGES_TOTAL);
77
78 /* Setup */
79 test_init(argc, argv);
80 check_hugetlb_shm_group();
81 if (hpage_size > SEGMENT_SIZE)
82 CONFIG("Page size is too large for configured SEGMENT_SIZE\n");
83 check_free_huge_pages(SEGMENT_SIZE / hpage_size);
84
85 iterations = (total_hpages * hpage_size) / SEGMENT_SIZE + 1;
86 verbose_printf("iterations = %d\n", iterations);
87
88 wait_list = malloc(sizeof(pid_t) * iterations);
89 if (wait_list == NULL)
90 FAIL("Failed to allocate wait_list");
91
92 /* Create, attach and part init segment */
93 attach_segment(SEGMENT_SIZE, IPC_CREAT|SHM_HUGETLB|0640, 0);
94 p = (char *)shm_addr;
95 for (i = 0; i < 4; i++, p += STRIDE)
96 memset(p, 0x55, STRIDE);
97
98 /* Detach segment */
99 if (shmdt(shm_addr) != 0)
100 FAIL("shmdt(SEGMENT)");
101
102 /* Create children to reattach read-only */
103 for (i = 0; i < iterations; i++) {
104 pid_t pid;
105 pid = fork();
106 if (pid == -1)
107 FAIL("fork");
108
109 if (pid) {
110 wait_list[i] = pid;
111 } else {
112 attach_segment(0, 0, SHM_RDONLY);
113 if (shmdt(shm_addr) != 0) {
114 perror("shmdt(SEGMENT)");
115 exit(EXIT_FAILURE);
116 }
117 exit(EXIT_SUCCESS);
118 }
119 }
120
121 /* Wait for all children to exit */
122 for (i = 0; i < iterations; i++) {
123 int status;
124 if (waitpid(wait_list[i], &status, 0) == -1)
125 FAIL("waitpid");
126 if (status != EXIT_SUCCESS)
127 FAIL("Child exited with failure");
128 }
129
130 PASS();
131 }