-
Notifications
You must be signed in to change notification settings - Fork 50
/
shm-mmap.c
231 lines (187 loc) · 4.69 KB
/
shm-mmap.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
/* $Id$ */
/*
* Copyright (c) 2006 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "fdm.h"
/*
* This implements shared memory using mmap'd files in TMPDIR.
*/
int shm_expand(struct shm *, size_t);
char shm_block[BUFSIZ];
#ifdef MAP_NOSYNC
#define SHM_FLAGS MAP_SHARED|MAP_NOSYNC
#else
#define SHM_FLAGS MAP_SHARED
#endif
#define SHM_PROT PROT_READ|PROT_WRITE
/* Work out shm path. */
char *
shm_path(struct shm *shm)
{
static char path[MAXPATHLEN];
if (ppath(path, sizeof path, "%s/%s", conf.tmp_dir, shm->name) != 0)
return (NULL);
return (path);
}
/* Expand or reduce shm file to size. */
int
shm_expand(struct shm *shm, size_t size)
{
ssize_t n;
if (size == shm->size)
return (0);
if (size < shm->size)
return (ftruncate(shm->fd, size) != 0);
if (lseek(shm->fd, shm->size, SEEK_SET) == -1)
return (-1);
/*
* Fill the file using write(2) to avoid fragmentation problems on
* FreeBSD and also to detect disk full.
*/
while (size > sizeof shm_block) {
if ((n = write(shm->fd, shm_block, sizeof shm_block)) == -1)
return (-1);
if (n != sizeof shm_block) {
errno = EIO;
return (-1);
}
size -= sizeof shm_block;
}
if (size > 0) {
if ((n = write(shm->fd, shm_block, size)) == -1)
return (-1);
if ((size_t) n != size) {
errno = EIO;
return (-1);
}
}
/*
* Sync the fd, should hopefully fail if disk full.
*/
if (fsync(shm->fd) != 0)
return (-1);
return (0);
}
/* Create an shm file and map it. */
void *
shm_create(struct shm *shm, size_t size)
{
int saved_errno;
char *path;
if (size == 0)
fatalx("zero size");
if (ppath(
shm->name, sizeof shm->name, "%s.XXXXXXXXXX", __progname) != 0)
return (NULL);
if ((path = shm_path(shm)) == NULL)
return (NULL);
if ((shm->fd = mkstemp(path)) == -1)
return (NULL);
strlcpy(shm->name, xbasename(path), sizeof shm->name);
if (shm_expand(shm, size) != 0)
goto error;
shm->data = mmap(NULL, size, SHM_PROT, SHM_FLAGS, shm->fd, 0);
if (shm->data == MAP_FAILED)
goto error;
madvise(shm->data, size, MADV_SEQUENTIAL);
shm->size = size;
return (shm->data);
error:
saved_errno = errno;
unlink(path);
errno = saved_errno;
return (NULL);
}
/* Destroy shm file. */
void
shm_destroy(struct shm *shm)
{
char *path;
if (*shm->name == '\0')
return;
shm_close(shm);
if ((path = shm_path(shm)) == NULL)
fatal("unlink failed");
if (unlink(path) != 0)
fatal("unlink failed");
*shm->name = '\0';
}
/* Close and unmap shm without destroying file. */
void
shm_close(struct shm *shm)
{
if (shm->fd == -1)
return;
if (munmap(shm->data, shm->size) != 0)
fatal("munmap failed");
shm->data = NULL;
close(shm->fd);
shm->fd = -1;
}
/* Reopen and map shm file. */
void *
shm_reopen(struct shm *shm)
{
char *path;
if ((path = shm_path(shm)) == NULL)
return (NULL);
if ((shm->fd = open(path, O_RDWR, 0)) == -1)
return (NULL);
shm->data = mmap(NULL, shm->size, SHM_PROT, SHM_FLAGS, shm->fd, 0);
if (shm->data == MAP_FAILED)
return (NULL);
madvise(shm->data, shm->size, MADV_SEQUENTIAL);
return (shm->data);
}
/* Set ownership of shm file. */
int
shm_owner(struct shm *shm, uid_t uid, gid_t gid)
{
if (fchown(shm->fd, uid, gid) != 0)
return (-1);
return (0);
}
/* Resize an shm file. */
void *
shm_resize(struct shm *shm, size_t nmemb, size_t size)
{
size_t newsize = nmemb * size;
if (size == 0)
fatalx("zero size");
if (SIZE_MAX / nmemb < size)
fatalx("nmemb * size > SIZE_MAX");
#ifndef HAVE_MREMAP
if (munmap(shm->data, shm->size) != 0)
fatal("munmap failed");
shm->data = NULL;
#endif
if (shm_expand(shm, newsize) != 0)
return (NULL);
#ifdef HAVE_MREMAP
shm->data = mremap(shm->data, shm->size, newsize, MREMAP_MAYMOVE);
#else
shm->data = mmap(NULL, newsize, SHM_PROT, SHM_FLAGS, shm->fd, 0);
#endif
if (shm->data == MAP_FAILED)
return (NULL);
madvise(shm->data, newsize, MADV_SEQUENTIAL);
shm->size = newsize;
return (shm->data);
}