-
Notifications
You must be signed in to change notification settings - Fork 1
/
shm.c
56 lines (51 loc) · 1.26 KB
/
shm.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
#include <string.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "./shm.h"
#define BUFFER_SIZE 256
void randname(char *buf) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
long r = ts.tv_nsec;
for (size_t i = 0; i < strlen(buf); ++i) {
if (buf[i] != 'X') continue;
buf[i] = 'A'+(r&15)+(r&16)*2;
r >>= 5;
}
}
int create_shm_file(const char* filename) {
int retries = 100;
char name[BUFFER_SIZE];
size_t len = strlen(filename);
memcpy(name, filename, len);
const char* randomstr = "-XXXXXX";
do {
memcpy(&name[len], randomstr, strlen(randomstr));
randname(name);
--retries;
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0) {
shm_unlink(name);
return fd;
}
} while (retries > 0 && errno == EEXIST);
return -1;
}
int allocate_shm_file(const char* filename, size_t size) {
int fd = create_shm_file(filename);
if (fd < 0)
return -1;
int ret;
do {
ret = ftruncate(fd, size);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
close(fd);
return -1;
}
return fd;
}