Commit | Line | Data |
---|---|---|
1d498196 MD |
1 | /* |
2 | * libringbuffer/shm.c | |
3 | * | |
4 | * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * Dual LGPL v2.1/GPL v2 license. | |
7 | */ | |
8 | ||
9 | #include "shm.h" | |
10 | #include <unistd.h> | |
11 | #include <fcntl.h> | |
12 | #include <sys/mman.h> | |
13 | #include <sys/stat.h> /* For mode constants */ | |
14 | #include <fcntl.h> /* For O_* constants */ | |
15 | #include <assert.h> | |
16 | #include <ust/align.h> | |
17 | ||
18 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj) | |
19 | { | |
20 | struct shm_object_table *table; | |
21 | ||
22 | table = zmalloc(sizeof(struct shm_object_table) + | |
23 | max_nb_obj * sizeof(table->objects[0])); | |
24 | table->size = max_nb_obj; | |
25 | return table; | |
26 | } | |
27 | ||
28 | struct shm_object *shm_object_table_append(struct shm_object_table *table, | |
29 | size_t memory_map_size) | |
30 | { | |
31 | int shmfd, waitfd[2], ret, i; | |
32 | struct shm_object *obj; | |
33 | char *memory_map; | |
34 | ||
35 | if (table->allocated_len >= table->size) | |
36 | return NULL; | |
7a9c21bd | 37 | obj = &table->objects[table->allocated_len]; |
1d498196 MD |
38 | |
39 | /* wait_fd: create pipe */ | |
40 | ret = pipe(waitfd); | |
41 | if (ret < 0) { | |
42 | PERROR("pipe"); | |
43 | goto error_pipe; | |
44 | } | |
45 | for (i = 0; i < 2; i++) { | |
46 | ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC); | |
47 | if (ret < 0) { | |
48 | PERROR("fcntl"); | |
49 | goto error_fcntl; | |
50 | } | |
51 | } | |
5d61a504 MD |
52 | /* The write end of the pipe needs to be non-blocking */ |
53 | ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK); | |
54 | if (ret < 0) { | |
55 | PERROR("fcntl"); | |
56 | goto error_fcntl; | |
57 | } | |
7a9c21bd | 58 | memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); |
1d498196 MD |
59 | |
60 | /* shm_fd: create shm */ | |
61 | ||
62 | /* | |
63 | * Allocate shm, and immediately unlink its shm oject, keeping | |
64 | * only the file descriptor as a reference to the object. If it | |
65 | * already exists (caused by short race window during which the | |
66 | * global object exists in a concurrent shm_open), simply retry. | |
67 | * We specifically do _not_ use the / at the beginning of the | |
68 | * pathname so that some OS implementations can keep it local to | |
69 | * the process (POSIX leaves this implementation-defined). | |
616d144a MD |
70 | * Ignore the shm_unlink errors, because we handle leaks that |
71 | * could occur by applications crashing between shm_open and | |
72 | * shm_unlink by unlinking the shm before every open. Therefore, | |
73 | * we can only leak one single shm (and only if the application | |
74 | * crashes between shm_open and the following shm_unlink). | |
1d498196 MD |
75 | */ |
76 | do { | |
616d144a | 77 | (void) shm_unlink("ust-shm-tmp"); |
1d498196 MD |
78 | shmfd = shm_open("ust-shm-tmp", |
79 | O_CREAT | O_EXCL | O_RDWR, 0700); | |
80 | } while (shmfd < 0 && errno == EEXIST); | |
81 | if (shmfd < 0) { | |
82 | PERROR("shm_open"); | |
83 | goto error_shm_open; | |
84 | } | |
74d89c32 | 85 | (void) shm_unlink("ust-shm-tmp"); |
1d498196 MD |
86 | ret = ftruncate(shmfd, memory_map_size); |
87 | if (ret) { | |
88 | PERROR("ftruncate"); | |
89 | goto error_ftruncate; | |
90 | } | |
91 | obj->shm_fd = shmfd; | |
92 | ||
93 | /* memory_map: mmap */ | |
94 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, | |
95 | MAP_SHARED, shmfd, 0); | |
96 | if (memory_map == MAP_FAILED) { | |
97 | PERROR("mmap"); | |
98 | goto error_mmap; | |
99 | } | |
100 | obj->memory_map = memory_map; | |
101 | obj->memory_map_size = memory_map_size; | |
102 | obj->allocated_len = 0; | |
dc613eb9 | 103 | obj->index = table->allocated_len++; |
7a9c21bd | 104 | |
1d498196 MD |
105 | return obj; |
106 | ||
107 | error_mmap: | |
108 | error_ftruncate: | |
109 | error_unlink: | |
110 | ret = close(shmfd); | |
111 | if (ret) { | |
112 | PERROR("close"); | |
113 | assert(0); | |
114 | } | |
115 | error_shm_open: | |
116 | error_fcntl: | |
117 | for (i = 0; i < 2; i++) { | |
118 | ret = close(waitfd[i]); | |
119 | if (ret) { | |
120 | PERROR("close"); | |
121 | assert(0); | |
122 | } | |
123 | } | |
124 | error_pipe: | |
1d498196 MD |
125 | return NULL; |
126 | ||
127 | } | |
128 | ||
129 | static | |
130 | void shmp_object_destroy(struct shm_object *obj) | |
131 | { | |
132 | int ret, i; | |
133 | ||
134 | ret = munmap(obj->memory_map, obj->memory_map_size); | |
135 | if (ret) { | |
136 | PERROR("umnmap"); | |
137 | assert(0); | |
138 | } | |
139 | ret = close(obj->shm_fd); | |
140 | if (ret) { | |
141 | PERROR("close"); | |
142 | assert(0); | |
143 | } | |
144 | for (i = 0; i < 2; i++) { | |
145 | ret = close(obj->wait_fd[i]); | |
146 | if (ret) { | |
147 | PERROR("close"); | |
148 | assert(0); | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | void shm_object_table_destroy(struct shm_object_table *table) | |
154 | { | |
155 | int i; | |
156 | ||
157 | for (i = 0; i < table->allocated_len; i++) | |
158 | shmp_object_destroy(&table->objects[i]); | |
159 | free(table); | |
160 | } | |
161 | ||
162 | /* | |
163 | * zalloc_shm - allocate memory within a shm object. | |
164 | * | |
165 | * Shared memory is already zeroed by shmget. | |
166 | * *NOT* multithread-safe (should be protected by mutex). | |
167 | * Returns a -1, -1 tuple on error. | |
168 | */ | |
169 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len) | |
170 | { | |
171 | struct shm_ref ref; | |
172 | struct shm_ref shm_ref_error = { -1, -1 }; | |
173 | ||
174 | if (obj->memory_map_size - obj->allocated_len < len) | |
175 | return shm_ref_error; | |
176 | ref.index = obj->index; | |
177 | ref.offset = obj->allocated_len; | |
178 | obj->allocated_len += len; | |
179 | return ref; | |
180 | } | |
181 | ||
182 | void align_shm(struct shm_object *obj, size_t align) | |
183 | { | |
184 | size_t offset_len = offset_align(obj->allocated_len, align); | |
185 | obj->allocated_len += offset_len; | |
186 | } |