-
Notifications
You must be signed in to change notification settings - Fork 1
/
shmfile.c
358 lines (264 loc) · 7.38 KB
/
shmfile.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* Copyright (c) 2017, 2018 Timothy Savannah
* All Rights Reserved, Licensed under terms of Lesser GNU Public License version 2.1
*
* shmfile.c - Definitions and private attributes for shmfile
*
*/
#define _GNU_SOURCE
#include "shmfile.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef __GNUC__
#define __visibility_hidden __attribute__( (visibility ("hidden")) )
#define likely(x) __builtin_expect(!!(x),1)
#define unlikely(x) __builtin_expect(!!(x),0)
#else
#define __visibility_hidden
#define likely(x) ( !!(x) )
#define unlikely(x) ( !!(x) )
#endif
#ifndef FSHM_QUIET
#define printerr(_x, ...) fprintf(stderr, _x)
#else
#define printerr(_x, ...)
#endif
#define FSHM_COOKIE(_cookie) ( (struct fshm_cookie*) _cookie)
#define IS_FSHM_OWNER(_cookie) !!( FSHM_COOKIE(_cookie)->fshm_flags & FSHM_OWNER )
#define IS_FSHM_GUEST(_cookie) !!( FSHM_COOKIE(_cookie)->fshm_flags & FSHM_GUEST )
#define IS_FSHM_PERSISTENT(_cookie) !!( FSHM_COOKIE(_cookie)->fshm_flags & FSHM_PERSIST )
#define IS_FSHM_FLAGS_OWNER(_fshm_flags) !!( _fshm_flags & FSHM_OWNER )
#define IS_FSHM_FLAGS_GUEST(_fshm_flags) !!( _fshm_flags & FSHM_GUEST )
#define IS_FSHM_FLAGS_PERSIST(_fshm_flags) !!( _fshm_flags & FSHM_PERSIST )
static volatile const char *copyright = "Copyright (c) 2017, 2018 Timothy Savannah All Rights Reserved, licensed under terms of Lesser GNU Public License version 2.1";
static volatile const char *product_name = "shmfile";
static volatile const char *version = "Version 1.1.0";
typedef struct {
unsigned char major;
unsigned char minor;
unsigned char patchlevel;
const char *extra;
}_shmfile_version_info_t;
static volatile _shmfile_version_info_t _shmfile_version_info = { (unsigned char)1, (unsigned char)1, (unsigned char)0, "" };
void shmfile_get_version(unsigned char *major, unsigned char *minor, unsigned char *patchlevel, const char **extra)
{
*major = _shmfile_version_info.major;
*minor = _shmfile_version_info.minor;
*patchlevel = _shmfile_version_info.patchlevel;
*extra = _shmfile_version_info.extra;
}
void _fshm_printCopyrightVersion(void)
{
printf("%s %s\n%s\n\n", product_name, version, copyright);
}
__visibility_hidden struct fshm_cookie {
int fd;
char *name;
int fshm_flags;
};
__visibility_hidden ssize_t _fshm_read(void *cookie, char *buf, size_t size)
{
struct fshm_cookie *fshmCookie;
ssize_t ret;
fshmCookie = FSHM_COOKIE(cookie);
ret = read(fshmCookie->fd, buf, size);
return ret;
}
__visibility_hidden ssize_t _fshm_write(void *cookie, const char *buf, size_t size)
{
struct fshm_cookie *fshmCookie;
ssize_t ret;
fshmCookie = FSHM_COOKIE(cookie);
ret = write(fshmCookie->fd, buf, size);
return ret;
}
__visibility_hidden int _fshm_seek(void *cookie, off_t *offset, int whence)
{
struct fshm_cookie *fshmCookie;
int ret;
off_t newOffset;
fshmCookie = FSHM_COOKIE(cookie);
newOffset = lseek(fshmCookie->fd, *offset, whence);
if ( likely( newOffset >= 0 ) )
{
*offset = newOffset;
ret = 0;
}
else
{
ret = -1;
}
return ret;
}
__visibility_hidden int _fshm_close(void *cookie)
{
struct fshm_cookie *fshmCookie;
int ret = 0;
fshmCookie = FSHM_COOKIE(cookie);
if ( IS_FSHM_OWNER(fshmCookie) && !IS_FSHM_PERSISTENT(cookie) )
{
ret = shm_unlink(fshmCookie->name);
}
free(fshmCookie->name);
free(fshmCookie);
return ret;
}
FILE* fshm_open(const char *name, mode_t mode, int fshm_flags)
{
int fd;
int oflag;
char *nameCpy;
size_t nameLen;
const char *modeStr = NULL;
struct fshm_cookie *cookie;
cookie_io_functions_t fshmIoFuncs;
FILE *fObj;
if ( unlikely( ! ( fshm_flags & (FSHM_OWNER | FSHM_GUEST) ) ) )
{
printerr("Error: fshm_open called but flags specified neither FSHM_OWNER nor FSHM_GUEST.\n");
errno = EINVAL;
return NULL;
}
if ( unlikely( !name || name[0] == '\0' ) )
{
printerr("Error: fshm_open called with NULL/empty \"name\" parameter.\n");
errno = EINVAL;
return NULL;
}
oflag = O_RDWR;
if ( fshm_flags & FSHM_OWNER )
{
if ( unlikely( fshm_flags & FSHM_GUEST ) )
{
printerr("Error: fshm_open called with both FSHM_OWNER and FSHM_GUEST\n");
errno = EINVAL;
return NULL;
}
oflag |= O_CREAT;
oflag |= O_EXCL;
}
fd = shm_open(name, oflag, mode);
if ( unlikely( fd < 0 ) )
{
if ( fshm_flags & FSHM_GUEST && errno == EACCES)
{
/*
* Guest read and write denied,
* Try read only
* */
oflag = O_RDONLY;
fd = shm_open(name, oflag, mode);
if ( fd >= 0 )
{
modeStr = "r";
goto _created_ok;
}
}
return NULL;
}
_created_ok:
cookie = malloc( sizeof(struct fshm_cookie) );
if ( unlikely( !cookie ) )
{
errno = ENOMEM;
return NULL;
}
nameLen = strlen(name);
nameCpy = malloc(nameLen + 1);
strcpy(nameCpy, name);
cookie->fshm_flags = fshm_flags;
cookie->fd = fd;
cookie->name = nameCpy;
fshmIoFuncs.read = _fshm_read;
fshmIoFuncs.write = _fshm_write;
fshmIoFuncs.seek = _fshm_seek;
fshmIoFuncs.close = _fshm_close;
if ( fshm_flags & FSHM_OWNER )
{
modeStr = "w+";
}
else if ( likely(modeStr == NULL) )
{
modeStr = "r+";
}
fObj = fopencookie(cookie, modeStr, fshmIoFuncs);
if ( unlikely( ! fObj ) )
{
_fshm_close(cookie);
return NULL;
}
/* Set the _fileno field so fileno() and then non-f* functions work */
fObj->_fileno = fd;
if ( fshm_flags & FSHM_OWNER )
{
/* Set the mode permissions on this shmfile */
fchmod(fd, mode);
}
return fObj;
}
FILE* fshm_create(const char *name, mode_t mode)
{
return fshm_open(name, mode, FSHM_OWNER);
}
FILE *fshm_guest_open(const char *name)
{
return fshm_open(name, 0, FSHM_GUEST);
}
int fshm_chgrp(FILE *fshm_file, gid_t group)
{
int fd;
uid_t ownerUid;
struct stat statBuf;
fd = fileno( fshm_file );
/* Check if fshm_file was a valid stream */
if ( unlikely( fd < 0 ) )
{
return -1;
}
/* Gather current uid on file */
if ( unlikely( fstat( fd, &statBuf ) != 0 ) )
{
return -1;
}
ownerUid = statBuf.st_uid;
/* Check if we are trying to change group to the current group (NOP) */
if ( unlikely ( statBuf.st_gid == group ) )
{
return 0;
}
/* Perform the actual chown call. */
if ( unlikely( fchown(fd, ownerUid, group) != 0 ) )
{
return -1;
}
return 0;
}
int fshm_chmod(FILE *fshm_file, mode_t mode)
{
int fd;
fd = fileno( fshm_file );
/* Check if fshm_file was a valid stream */
if ( unlikely( fd < 0 ) )
{
errno = ENOENT;
return -1;
}
/* Perform the actual chmod call */
if ( unlikely( fchmod(fd, mode) != 0 ) )
{
return -1;
}
return 0;
}
int fshm_force_destroy(const char *name)
{
return shm_unlink(name);
}
/* vim: set ts=4 sw=4 st=4 expandtab : */