Debian Codegraph @

runtime/dyninst/shm.c

raw
1 /* -*- linux-c -*-
2 * Shared Memory Functions
3 * Copyright (C) 2012 Red Hat Inc.
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 */
10
11 #ifndef _STAPDYN_SHM_H_
12 #define _STAPDYN_SHM_H_
13
14 #include <fcntl.h>
15 #include <limits.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/mman.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 static char _stp_shm_name[NAME_MAX] = { '\0' };
24 static int _stp_shm_fd = -1;
25 static int _stp_shm_page_size = -1;
26 static off_t _stp_shm_size = 0;
27 static off_t _stp_shm_allocated = 0;
28 static void *_stp_shm_base = NULL;
29
30 static const char *_stp_shm_init(void);
31 static int _stp_shm_connect(const char *name);
32 static void *_stp_shm_alloc(size_t size);
33 static void *_stp_shm_zalloc(size_t size);
34 static void _stp_shm_free(void *ptr);
35 static void _stp_shm_finalize(void);
36 static void _stp_shm_destroy(void);
37
38
39 #ifdef DEBUG_SHM
40 #define shm_dbug(fmt, args...) _stp_dbug(__FUNCTION__, __LINE__, fmt, ##args)
41 #else
42 #define shm_dbug(fmt, args...)
43 #endif
44
45
46 // Create and initialize the shared memory for this module.
47 static const char *_stp_shm_init(void)
48 {
49 char name[NAME_MAX];
50 long page_size;
51 int fd;
52 void *base;
53
54 // If we already have shared memory, carry on...
55 if (_stp_shm_base)
56 return _stp_shm_name;
57
58 // Find the increment to use in growing our memory size. Since this is
59 // used in mmap, only multiples of the page size make sense.
60 page_size = sysconf(_SC_PAGESIZE);
61 if (page_size < 1)
62 return NULL;
63
64 // Create a unique name for our shared memory. It need not be anything
65 // secret because shm_open's flags will ensure security.
66 (void) snprintf(name, NAME_MAX, "/stapdyn.%d", getpid());
67
68 // Create the actual share memory. The O_EXCL saves us from the
69 // possible mktemp race. Only USR file mode bits are added, because
70 // that's all that ptrace will allow to attach anyway. (Unless you're
71 // root, but that deserves much larger consideration.)
72 fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
73 if (fd < 0)
74 return NULL;
75
76 // Start the shared memory sized with just one unit.
77 if (ftruncate(fd, page_size) < 0)
78 goto err_fd;
79
80 // Now finally map it into this process.
81 base = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
82 MAP_SHARED, fd, 0);
83 if (base == MAP_FAILED)
84 goto err_fd;
85
86 // We're done; set globals and go home!
87 strlcpy(_stp_shm_name, name, sizeof(_stp_shm_name));
88 _stp_shm_fd = fd;
89 _stp_shm_page_size = page_size;
90 _stp_shm_size = page_size;
91 _stp_shm_allocated = 0;
92 _stp_shm_base = base;
93 shm_dbug("initialized %s @ %p", _stp_shm_name, _stp_shm_base);
94 return _stp_shm_name;
95
96 err_fd:
97 close(fd);
98 shm_unlink(_stp_shm_name);
99 return NULL;
100 }
101
102
103 static int _stp_shm_connect(const char *name)
104 {
105 int rc = 0, fd = -1;
106 struct stat st;
107 void *base;
108
109 // NB: since we're just connecting, we won't set _stp_shm_name,
110 // so _stp_shm_destroy won't try to unlink it from this process.
111
112 // Open the pre-existing shared memory
113 fd = shm_open(name, O_RDWR, 0);
114 if (fd < 0)
115 rc = fd;
116
117 // Find out the shared memory's size.
118 // (This implies that the main process is done allocating.)
119 if (rc == 0)
120 rc = fstat(fd, &st);
121
122 // Map it into this process.
123 if (rc == 0) {
124 base = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE,
125 MAP_SHARED, fd, 0);
126 if (base == MAP_FAILED)
127 rc = -1;
128 else
129 _stp_shm_base = base;
130 }
131
132 if (fd >= 0)
133 close(fd);
134
135 if (rc == 0)
136 shm_dbug("connected pid %d to %s @ %p", getpid(), name, _stp_shm_base);
137 return rc;
138 }
139
140
141 // Allocate space from shared memory
142 static void *_stp_shm_alloc(size_t size)
143 {
144 void *alloc;
145
146 // We're not initialized or already finalized.
147 // Either way, we're not taking new requests.
148 if (_stp_shm_fd < 0)
149 return NULL;
150
151 // Round up to 8-byte aligned sizes, just to be a little safer
152 size = (size + 7) & ~7;
153 if (size <= 0)
154 return NULL; // either 0 requested or overflow
155
156 // Check if more memory is needed.
157 if (_stp_shm_size - _stp_shm_allocated < size) {
158 void *new_base;
159
160 // Round up to the nearest page size
161 off_t new_size = _stp_shm_allocated + size;
162 new_size += _stp_shm_page_size - 1;
163 new_size /= _stp_shm_page_size;
164 new_size *= _stp_shm_page_size;
165 if (new_size < _stp_shm_allocated ||
166 new_size - _stp_shm_allocated < size)
167 return NULL; // math overflow?
168
169 // Try to resize the underlying file.
170 if (ftruncate(_stp_shm_fd, new_size) < 0)
171 return NULL;
172
173 // Try to remap the address in memory.
174 new_base = mremap(_stp_shm_base, _stp_shm_size,
175 new_size, MREMAP_MAYMOVE);
176 if (new_base == MAP_FAILED)
177 return NULL;
178
179 // Update globals
180 _stp_shm_size = new_size;
181 _stp_shm_base = new_base;
182 }
183
184 // Finally return some memory.
185 alloc = _stp_shm_base + _stp_shm_allocated;
186 _stp_shm_allocated += size;
187 return alloc;
188 }
189
190
191 // Allocate zeroed space from shared memory
192 static void *_stp_shm_zalloc(size_t size)
193 {
194 void *ptr = _stp_shm_alloc(size);
195 if (ptr)
196 memset(ptr, 0, size);
197 return ptr;
198 }
199
200
201 static void _stp_shm_free(void *ptr __attribute__((unused)))
202 {
203 // NO-OP; we don't try to reclaim individual pieces.
204 }
205
206
207 // Signal that we're done allocating in shared memory.
208 // It won't be allowed to grow or move from here on out.
209 static void _stp_shm_finalize(void)
210 {
211 if (_stp_shm_fd >= 0) {
212 close(_stp_shm_fd);
213 _stp_shm_fd = -1;
214 }
215 shm_dbug("mapped %" PRIi64 " bytes @ %p, used %" PRIi64, _stp_shm_size,
216 _stp_shm_base, _stp_shm_allocated);
217 }
218
219
220 // Tear down everything we created... *sniff*
221 // NB: Make sure not to reference any memory within after this!
222 // (Other processes may still have their own mmap reference though.)
223 // Don't destroy things in shm that may still be used by other processes!
224 static void _stp_shm_destroy(void)
225 {
226 if (_stp_shm_base) {
227 munmap(_stp_shm_base, _stp_shm_size);
228 _stp_shm_base = NULL;
229 _stp_shm_size = 0;
230 _stp_shm_allocated = 0;
231 }
232
233 if (_stp_shm_fd >= 0) {
234 close(_stp_shm_fd);
235 _stp_shm_fd = -1;
236 }
237
238 if (_stp_shm_name[0]) {
239 shm_unlink(_stp_shm_name);
240 _stp_shm_name[0] = '\0';
241 }
242 }
243
244
245 #endif /* _STAPDYN_SHM_H_ */