summaryrefslogtreecommitdiff
path: root/src/bin/evas/evas_cserve2_shm.c
diff options
context:
space:
mode:
authorChris Michael <cp.michael@samsung.com>2018-08-30 13:53:13 +0900
committerHermet Park <hermetpark@gmail.com>2018-08-30 13:53:13 +0900
commit3846059288a06f83de941b0fc7b180b706ab4647 (patch)
treed9d08624aab153b9ec2df35ee2ea02650e793593 /src/bin/evas/evas_cserve2_shm.c
parentb4e17905e9ad05179368d5f43cd88abb08d9b125 (diff)
evas: Remove binary cserve2
Summary: ref T7226 Depends on D6939 Reviewers: raster, cedric, zmike, Hermet Reviewed By: Hermet Subscribers: #reviewers, #committers Tags: #efl Maniphest Tasks: T7226 Differential Revision: https://phab.enlightenment.org/D6940
Diffstat (limited to 'src/bin/evas/evas_cserve2_shm.c')
-rw-r--r--src/bin/evas/evas_cserve2_shm.c336
1 files changed, 0 insertions, 336 deletions
diff --git a/src/bin/evas/evas_cserve2_shm.c b/src/bin/evas/evas_cserve2_shm.c
deleted file mode 100644
index d88d12a59f..0000000000
--- a/src/bin/evas/evas_cserve2_shm.c
+++ /dev/null
@@ -1,336 +0,0 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include "evas_cserve2.h"
6
7#include <errno.h>
8#include <sys/mman.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11#include <unistd.h>
12
13struct _Shm_Mapping
14{
15 const char *name;
16 size_t length;
17 Eina_Inlist *segments;
18};
19
20typedef struct _Shm_Mapping Shm_Mapping;
21
22struct _Shm_Handle
23{
24 EINA_INLIST;
25 Shm_Mapping *mapping;
26 off_t map_offset;
27 off_t image_offset;
28 size_t map_size;
29 size_t image_size;
30 int refcount;
31 int shmid;
32 void *data;
33};
34
35static int id = 0;
36
37size_t
38cserve2_shm_size_normalize(size_t size, size_t align)
39{
40 long pagesize;
41 size_t normalized;
42
43 pagesize = eina_cpu_page_size();
44
45 if (align)
46 align = ((align + pagesize - 1) / pagesize) * pagesize;
47 else
48 align = pagesize;
49 normalized = ((size + align - 1) / align) * align;
50
51 return normalized;
52}
53
54Shm_Handle *
55cserve2_shm_request(const char *infix, size_t size)
56{
57 Shm_Mapping *map;
58 Shm_Handle *shm;
59 char shmname[NAME_MAX];
60 size_t map_size;
61 int fd;
62
63 map = calloc(1, sizeof(Shm_Mapping));
64 if (!map)
65 {
66 ERR("Failed to allocate mapping handler.");
67 return NULL;
68 }
69
70 shm = calloc(1, sizeof(Shm_Handle));
71 if (!shm)
72 {
73 ERR("Failed to allocate shared memory handler.");
74 free(map);
75 return NULL;
76 }
77
78 do {
79 snprintf(shmname, sizeof(shmname), "/evas-shm-%05d-%05d-%s-%08x",
80 (int) getuid(), (int) getpid(), infix, ++id);
81 fd = shm_open(shmname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
82 if (fd == -1 && errno != EEXIST)
83 {
84 ERR("Failed to create shared memory object '%s': %m", shmname);
85 free(map);
86 free(shm);
87 return NULL;
88 }
89 } while (fd == -1);
90
91 map_size = cserve2_shm_size_normalize(size, 0);
92
93 if (ftruncate(fd, map_size) == -1)
94 {
95 ERR("Failed to set size of shared file: %m");
96 close(fd);
97 free(map);
98 free(shm);
99 return NULL;
100 }
101 close(fd);
102
103 map->name = eina_stringshare_add(shmname);
104 map->length = map_size;
105
106 map->segments = eina_inlist_append(map->segments, EINA_INLIST_GET(shm));
107 shm->mapping = map;
108 shm->map_offset = 0;
109 shm->image_offset = 0;
110
111 shm->image_size = size;
112 shm->map_size = map_size;
113 shm->shmid = id;
114
115 return shm;
116}
117
118Shm_Handle *
119cserve2_shm_segment_request(Shm_Handle *shm, size_t size)
120{
121 Shm_Handle *segment;
122 size_t map_size;
123 Shm_Mapping *map = shm->mapping;
124 int fd;
125
126 segment = calloc(1, sizeof (Shm_Handle));
127 if (!segment) return NULL;
128
129 fd = shm_open(map->name, O_RDWR, S_IRUSR | S_IWUSR);
130 if (fd == -1)
131 {
132 ERR("Could not reopen shm handle: %m");
133 free(segment);
134 return NULL;
135 }
136
137 map_size = cserve2_shm_size_normalize(size, 0);
138 map_size += map->length;
139
140 if (ftruncate(fd, map_size) == -1)
141 {
142 ERR("Could not set the size of the shm: %m");
143 close(fd);
144 free(segment);
145 return NULL;
146 }
147 close(fd);
148
149 segment->mapping = map;
150 segment->map_offset = map->length;
151 segment->map_size = map_size - map->length;
152 segment->image_size = size;
153 segment->image_offset = segment->map_offset;
154 map->length = map_size;
155 map->segments = eina_inlist_append(map->segments, EINA_INLIST_GET(segment));
156
157 return segment;
158}
159
160Shm_Handle *
161cserve2_shm_resize(Shm_Handle *shm, size_t newsize)
162{
163 size_t map_size;
164 int fd;
165
166 if (!shm)
167 return NULL;
168
169 if (shm->map_offset || shm->image_offset)
170 {
171 CRI("Can not resize shm with non-zero offset");
172 return NULL;
173 }
174
175 if (eina_inlist_count(shm->mapping->segments) > 1)
176 {
177 CRI("Can not resize shm with more than one segment");
178 return NULL;
179 }
180
181 fd = shm_open(shm->mapping->name, O_RDWR, S_IRUSR | S_IWUSR);
182 if (fd == -1)
183 {
184 ERR("Could not reopen shm handle: %m");
185 return NULL;
186 }
187
188 map_size = cserve2_shm_size_normalize(newsize, 0);
189 if (ftruncate(fd, map_size))
190 {
191 ERR("Could not set the size of the shm: %m");
192 close(fd);
193 return NULL;
194 }
195
196 if (shm->data)
197 {
198 munmap(shm->data, shm->image_size);
199 shm->data = mmap(NULL, shm->image_size, PROT_WRITE, MAP_SHARED,
200 fd, shm->image_offset);
201 }
202 close(fd);
203
204 shm->map_size = map_size;
205 shm->image_size = newsize;
206 shm->mapping->length = map_size;
207
208 return shm;
209}
210
211void
212cserve2_shm_unref(Shm_Handle *shm)
213{
214 Shm_Mapping *map = shm->mapping;
215
216 map->segments = eina_inlist_remove(map->segments, EINA_INLIST_GET(shm));
217
218 if (shm->data)
219 munmap(shm->data, shm->image_size);
220 free(shm);
221
222 if (map->segments)
223 return;
224
225 shm_unlink(map->name);
226 eina_stringshare_del(map->name);
227 free(map);
228}
229
230const char *
231cserve2_shm_name_get(const Shm_Handle *shm)
232{
233 return shm->mapping->name;
234}
235
236int
237cserve2_shm_id_get(const Shm_Handle *shm)
238{
239 return shm->shmid;
240}
241
242off_t
243cserve2_shm_map_offset_get(const Shm_Handle *shm)
244{
245 return shm->map_offset;
246}
247
248off_t
249cserve2_shm_offset_get(const Shm_Handle *shm)
250{
251 return shm->image_offset;
252}
253
254size_t
255cserve2_shm_map_size_get(const Shm_Handle *shm)
256{
257 return shm->map_size;
258}
259
260size_t
261cserve2_shm_size_get(const Shm_Handle *shm)
262{
263 return shm->image_size;
264}
265
266void *
267cserve2_shm_map(Shm_Handle *shm)
268{
269 int fd;
270 const char *name;
271
272 if (shm->refcount++)
273 return shm->data;
274
275 name = cserve2_shm_name_get(shm);
276
277 fd = shm_open(name, O_RDWR, S_IWUSR);
278 if (fd == -1)
279 return MAP_FAILED;
280
281 shm->data = mmap(NULL, shm->image_size, PROT_WRITE, MAP_SHARED,
282 fd, shm->image_offset);
283
284 close(fd);
285
286 return shm->data;
287}
288
289void
290cserve2_shm_unmap(Shm_Handle *shm)
291{
292 if (--shm->refcount)
293 return;
294
295 munmap(shm->data, shm->image_size);
296 shm->data = NULL;
297}
298
299static void
300_cserve2_shm_cleanup()
301{
302 Eina_Iterator *iter;
303 const Eina_File_Direct_Info *f_info;
304 char pattern[NAME_MAX];
305
306 sprintf(pattern, "evas-shm-%05d-", (int) getuid());
307 iter = eina_file_direct_ls("/dev/shm");
308 EINA_ITERATOR_FOREACH(iter, f_info)
309 {
310 if (strstr(f_info->path, pattern))
311 {
312 const char *shmname = strrchr(f_info->path, '/');
313 if (!shmname) continue;
314
315 if (shm_unlink(shmname) == -1)
316 ERR("Failed to remove shm entry at %s: %d %s", shmname, errno, strerror(errno));
317 else
318 DBG("cserve2 cleanup: removed %s", shmname);
319 }
320 else
321 DBG("cserve2 cleanup: ignoring %s", f_info->path);
322 }
323 eina_iterator_free(iter);
324}
325
326void
327cserve2_shm_init()
328{
329 _cserve2_shm_cleanup();
330}
331
332void
333cserve2_shm_shutdown()
334{
335 _cserve2_shm_cleanup();
336}