-
Notifications
You must be signed in to change notification settings - Fork 0
/
shmw_posix.c
240 lines (219 loc) · 5.1 KB
/
shmw_posix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2012+ Michal Soltys <[email protected]>
*
* This file is part of Yancat.
*
* Yancat is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Yancat is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Yancat. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#ifdef has_shm_posix
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <mntent.h>
#include "common.h"
#include "shmw.h"
int shmw_dt(struct shm_s *s)
{
if (!s)
return -1;
if (s->ptrc)
munmap(s->ptrc, s->siz);
if (s->ptr)
munmap(s->ptr, s->siz);
/*
* we may not NULLize above pointers here, as the area being detached
* itself might be in shared area; dtor() is otow final, so clean it up
* there
*/
return 0;
}
int shmw_dtor(struct shm_s *s)
{
if (shmw_dt(s) < 0)
return -1;
s->ptrc = NULL;
s->ptr = NULL;
if (s->name) {
if (s->ishuge)
unlink(s->name);
else
shm_unlink(s->name);
free(s->name);
s->name = NULL;
}
return 0;
}
/* find hugetlbfs mount, randomize, use if path length is sane */
static char* get_hmnt(const char* f)
{
size_t pathmax, len;
char *point = NULL;
struct mntent *m;
FILE *fs = setmntent("/etc/mtab", "r");
if (!fs)
return NULL;
while ((m = getmntent(fs))) {
if (strcmp(m->mnt_type, "hugetlbfs"))
continue;
fprintf(stderr, "info: HugeTLBfs found at %s\n", m->mnt_dir);
pathmax = get_pathmax(m->mnt_dir);
len = strlen(m->mnt_dir) + strlen(f) + 9;
if (len >= pathmax) {
ERRG("insane path!");
break;
}
if (!(point = (char *)malloc(len + 1))) {
ERRG("malloc()");
break;
}
strcpy(point, m->mnt_dir);
strcat(point, f);
point[len - 9] = '-';
get_strrnd(point + len - 8, 8);
point[len] = '\0';
break;
}
if (!m)
fputs("error: HugeTLBfs mount no found\n", stderr);
endmntent(fs);
return point;
}
static char* get_smnt(const char* f)
{
size_t pathmax, len;
char *point = NULL;
/* mandated by shm_open portability (supposedly ?) */
pathmax = 255;
len = strlen(f) + 9;
if (len >= pathmax) {
ERRG("insane path!");
goto out;
}
if (!(point = (char *)malloc(len + 1))) {
ERRG("malloc()");
goto out;
}
strcpy(point, f);
point[len - 9] = '-';
get_strrnd(point + len - 8, 8);
point[len] = '\0';
out:
return point;
}
#define PROT (PROT_READ | PROT_WRITE)
#ifndef MAP_HASSEMAPHORE
# define MAP_HASSEMAPHORE 0
#endif
#ifndef MAP_NOSYNC
# define MAP_NOSYNC 0
#endif
int shmw_ctor(struct shm_s *s, const char *name, size_t *_siz, size_t huge, int circ, int sems)
{
uint8_t *addr = NULL, *addrc = NULL, *addrt;
int flags_m = MAP_SHARED | MAP_NOSYNC | (sems ? MAP_HASSEMAPHORE : 0);
size_t siz, page;
int fd = -1, iaflag = 0;
if (!s || !name)
return -1;
memset(s, 0, sizeof *s);
page = get_page();
#ifndef h_linux
if (huge) {
fputs("warn: POSIX shm: cannot use HugeTLBfs (non-linux system)\n", stderr);
return -1;
}
#endif
if (huge > page) {
page = huge;
#ifdef __ia64__
flags_m |= MAP_FIXED;
addr = (uint8_t *)(0x8000000000000000ULL);
iaflag = 1;
#endif
s->ishuge = 1;
s->name = get_hmnt(name);
} else {
s->name = get_smnt(name);
}
if (!s->name)
return -1;
siz = Y_ALIGN(*_siz, page);
s->siz = siz;
if (s->ishuge)
fd = open(s->name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
else
fd = shm_open(s->name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
fprintf(stderr, "error: POSIX shm '%s': open(): %s\n", name, strerror(errno));
goto outstr;
}
if (ftruncate(fd, (off_t)siz) < 0) {
fprintf(stderr, "error: POSIX shm '%s': ftruncate(): %s\n", name, strerror(errno));
goto outfd;
}
addr = mmap(addr, siz, PROT, flags_m, fd, 0);
if (addr == MAP_FAILED) {
fprintf(stderr, "error: POSIX shm '%s': mmap(): %s\n", name, strerror(errno));
goto outfd;
}
s->ptr = addr;
if (!circ)
goto circex;
/* try above */
addrc = mmap(addr + siz, siz, PROT, flags_m, fd, 0);
if (addrc != MAP_FAILED && (addr + siz == addrc || addrc + siz == addr))
goto circok;
if (addrc != MAP_FAILED)
munmap(addrc, siz);
if (iaflag)
goto circex;
/* try below */
addrc = mmap(addr - siz, siz, PROT, flags_m, fd, 0);
if (addrc != MAP_FAILED && (addr + siz == addrc || addrc + siz == addr))
goto circok;
if (addrc != MAP_FAILED)
munmap(addrc, siz);
goto circex;
circok:
if (addrc < addr) {
addrt = addr;
addr = addrc;
addrc = addrt;
}
s->ptr = addr;
s->ptrc = addrc;
circex:
close(fd);
*_siz = siz;
return 0; /* non-shared allocators (e.g. malloc) return 1 */
outfd:
if (s->ishuge)
unlink(s->name);
else
shm_unlink(s->name);
close(fd);
outstr:
free(s->name);
return -1;
}
#else
/* mostly to quiet gcc */
int has_no_posix_shm = 1;
#endif