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; | |
37 | obj = &table->objects[table->allocated_len++]; | |
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 | } | |
1d498196 MD |
58 | *obj->wait_fd = *waitfd; |
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). | |
70 | */ | |
71 | do { | |
72 | shmfd = shm_open("ust-shm-tmp", | |
73 | O_CREAT | O_EXCL | O_RDWR, 0700); | |
74 | } while (shmfd < 0 && errno == EEXIST); | |
75 | if (shmfd < 0) { | |
76 | PERROR("shm_open"); | |
77 | goto error_shm_open; | |
78 | } | |
79 | ret = shm_unlink("ust-shm-tmp"); | |
80 | if (ret) { | |
81 | PERROR("shm_unlink"); | |
82 | goto error_unlink; | |
83 | } | |
84 | ret = ftruncate(shmfd, memory_map_size); | |
85 | if (ret) { | |
86 | PERROR("ftruncate"); | |
87 | goto error_ftruncate; | |
88 | } | |
89 | obj->shm_fd = shmfd; | |
90 | ||
91 | /* memory_map: mmap */ | |
92 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, | |
93 | MAP_SHARED, shmfd, 0); | |
94 | if (memory_map == MAP_FAILED) { | |
95 | PERROR("mmap"); | |
96 | goto error_mmap; | |
97 | } | |
98 | obj->memory_map = memory_map; | |
99 | obj->memory_map_size = memory_map_size; | |
100 | obj->allocated_len = 0; | |
101 | return obj; | |
102 | ||
103 | error_mmap: | |
104 | error_ftruncate: | |
105 | error_unlink: | |
106 | ret = close(shmfd); | |
107 | if (ret) { | |
108 | PERROR("close"); | |
109 | assert(0); | |
110 | } | |
111 | error_shm_open: | |
112 | error_fcntl: | |
113 | for (i = 0; i < 2; i++) { | |
114 | ret = close(waitfd[i]); | |
115 | if (ret) { | |
116 | PERROR("close"); | |
117 | assert(0); | |
118 | } | |
119 | } | |
120 | error_pipe: | |
121 | free(obj); | |
122 | return NULL; | |
123 | ||
124 | } | |
125 | ||
126 | static | |
127 | void shmp_object_destroy(struct shm_object *obj) | |
128 | { | |
129 | int ret, i; | |
130 | ||
131 | ret = munmap(obj->memory_map, obj->memory_map_size); | |
132 | if (ret) { | |
133 | PERROR("umnmap"); | |
134 | assert(0); | |
135 | } | |
136 | ret = close(obj->shm_fd); | |
137 | if (ret) { | |
138 | PERROR("close"); | |
139 | assert(0); | |
140 | } | |
141 | for (i = 0; i < 2; i++) { | |
142 | ret = close(obj->wait_fd[i]); | |
143 | if (ret) { | |
144 | PERROR("close"); | |
145 | assert(0); | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | void shm_object_table_destroy(struct shm_object_table *table) | |
151 | { | |
152 | int i; | |
153 | ||
154 | for (i = 0; i < table->allocated_len; i++) | |
155 | shmp_object_destroy(&table->objects[i]); | |
156 | free(table); | |
157 | } | |
158 | ||
159 | /* | |
160 | * zalloc_shm - allocate memory within a shm object. | |
161 | * | |
162 | * Shared memory is already zeroed by shmget. | |
163 | * *NOT* multithread-safe (should be protected by mutex). | |
164 | * Returns a -1, -1 tuple on error. | |
165 | */ | |
166 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len) | |
167 | { | |
168 | struct shm_ref ref; | |
169 | struct shm_ref shm_ref_error = { -1, -1 }; | |
170 | ||
171 | if (obj->memory_map_size - obj->allocated_len < len) | |
172 | return shm_ref_error; | |
173 | ref.index = obj->index; | |
174 | ref.offset = obj->allocated_len; | |
175 | obj->allocated_len += len; | |
176 | return ref; | |
177 | } | |
178 | ||
179 | void align_shm(struct shm_object *obj, size_t align) | |
180 | { | |
181 | size_t offset_len = offset_align(obj->allocated_len, align); | |
182 | obj->allocated_len += offset_len; | |
183 | } |