-
Notifications
You must be signed in to change notification settings - Fork 0
/
shmw_sysv.c
161 lines (149 loc) · 3.5 KB
/
shmw_sysv.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
/*
* 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_sysv
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "common.h"
#include "crc.h"
#include "shmw.h"
#ifndef SHM_HUGETLB
# ifdef h_linux
# define SHM_HUGETLB 04000
# endif
#endif
/* Standard attach flags we rely on */
#ifdef SHM_RND
# define AT_FLAGS SHM_RND
#else
# define AT_FLAGS 0
#endif
int shmw_dt(struct shm_s *s)
{
if (!s)
return -1;
if (s->ptrc)
shmdt(s->ptrc);
if (s->ptr)
shmdt(s->ptr);
/*
* 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->id >= 0)
shmctl(s->id, IPC_RMID, NULL);
s->id = -1;
s->name = NULL;
return 0;
}
int shmw_ctor(struct shm_s *s, const char *name, size_t *_siz, size_t huge, int circ, int sems)
{
int flags_g = IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR;
uint8_t *addr = NULL, *addrc = NULL, *addrt;
size_t siz, page;
int id, ia64 = 0;
if (!s || !name)
return -1;
memset(s, 0, sizeof *s);
page = get_page();
#ifndef h_linux
if (huge) {
fputs("warn: SysV shm: cannot use HugeTLBfs (non-linux system)\n", stderr);
return -1;
}
#else
if (huge > page) {
page = huge;
flags_g |= SHM_HUGETLB;
# ifdef __ia64__
addr = (uint8_t *)(0x8000000000000000ULL);
ia64 = 1;
# endif
}
#endif
siz = Y_ALIGN(*_siz, Y_MAX(page, (size_t)SHMLBA));
#if 0
/* this is far from perfect, but ... */
key = (key_t)((crc_str(name) & ~0xFFFFu) | (rand() & 0xFFFFu));
#endif
id = shmget(IPC_PRIVATE, siz, flags_g);
if (id < 0) {
fprintf(stderr, "error: SysV shm '%s': shmget(): %s\n", name, strerror(errno));
return -1;
}
s->id = id;
addr = shmat(id, addr, AT_FLAGS);
if (addr == (void *)-1) {
fprintf(stderr, "error: SysV shm '%s': shmat(): %s\n", name, strerror(errno));
goto outid;
}
s->ptr = addr;
if (!circ)
goto circex;
/* try above */
addrc = shmat(id, addr + siz, AT_FLAGS);
if (addrc != (void *)-1 && (addr + siz == addrc || addrc + siz == addr))
goto circok;
if (addrc != (void *)-1)
shmdt(addrc);
if (ia64)
goto circex;
/* try below */
addrc = shmat(id, addr - siz, AT_FLAGS);
if (addrc != (void *)-1 && (addr + siz == addrc || addrc + siz == addr))
goto circok;
if (addrc != (void *)-1)
shmdt(addrc);
goto circex;
circok:
if (addrc < addr) {
addrt = addr;
addr = addrc;
addrc = addrt;
}
s->ptr = addr;
s->ptrc = addrc;
circex:
*_siz = siz;
s->name = name;
return 0; /* non-shared allocators (e.g. malloc) return 1 */
outid:
shmctl(id, IPC_RMID, NULL);
return -1;
}
#else
/* mostly to quiet gcc */
int has_no_sysv_shm = 1;
#endif