4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/stat.h> /* For mode constants */
26 #include <fcntl.h> /* For O_* constants */
31 #include <lttng/align.h>
37 * Ensure we have the required amount of space available by writing 0
38 * into the entire buffer. Not doing so can trigger SIGBUS when going
39 * beyond the available shm space.
42 int zero_file(int fd
, size_t len
)
50 pagelen
= sysconf(_SC_PAGESIZE
);
53 zeropage
= calloc(pagelen
, 1);
57 while (len
> written
) {
59 retlen
= write(fd
, zeropage
,
60 min_t(size_t, pagelen
, len
- written
));
61 } while (retlen
== -1UL && errno
== EINTR
);
74 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
76 struct shm_object_table
*table
;
78 table
= zmalloc(sizeof(struct shm_object_table
) +
79 max_nb_obj
* sizeof(table
->objects
[0]));
80 table
->size
= max_nb_obj
;
84 struct shm_object
*shm_object_table_append(struct shm_object_table
*table
,
85 size_t memory_map_size
)
87 int shmfd
, waitfd
[2], ret
, i
, sigblocked
= 0;
88 struct shm_object
*obj
;
90 char tmp_name
[NAME_MAX
] = "ust-shm-tmp-XXXXXX";
91 sigset_t all_sigs
, orig_sigs
;
93 if (table
->allocated_len
>= table
->size
)
95 obj
= &table
->objects
[table
->allocated_len
];
97 /* wait_fd: create pipe */
103 for (i
= 0; i
< 2; i
++) {
104 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
110 /* The write end of the pipe needs to be non-blocking */
111 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
116 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
118 /* shm_fd: create shm */
121 * Theoretically, we could leak a shm if the application crashes
122 * between open and unlink. Disable signals on this thread for
123 * increased safety against this scenario.
125 sigfillset(&all_sigs
);
126 ret
= pthread_sigmask(SIG_BLOCK
, &all_sigs
, &orig_sigs
);
128 PERROR("pthread_sigmask");
129 goto error_pthread_sigmask
;
134 * Allocate shm, and immediately unlink its shm oject, keeping
135 * only the file descriptor as a reference to the object. If it
136 * already exists (caused by short race window during which the
137 * global object exists in a concurrent shm_open), simply retry.
138 * We specifically do _not_ use the / at the beginning of the
139 * pathname so that some OS implementations can keep it local to
140 * the process (POSIX leaves this implementation-defined).
144 * Using mktemp filename with O_CREAT | O_EXCL open
148 if (tmp_name
[0] == '\0') {
152 shmfd
= shm_open(tmp_name
,
153 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
154 } while (shmfd
< 0 && (errno
== EEXIST
|| errno
== EACCES
));
159 ret
= shm_unlink(tmp_name
);
160 if (ret
< 0 && errno
!= ENOENT
) {
161 PERROR("shm_unlink");
162 goto error_shm_release
;
165 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
167 PERROR("pthread_sigmask");
168 goto error_sigmask_release
;
170 ret
= zero_file(shmfd
, memory_map_size
);
173 goto error_zero_file
;
175 ret
= ftruncate(shmfd
, memory_map_size
);
178 goto error_ftruncate
;
182 /* memory_map: mmap */
183 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
184 MAP_SHARED
, shmfd
, 0);
185 if (memory_map
== MAP_FAILED
) {
189 obj
->memory_map
= memory_map
;
190 obj
->memory_map_size
= memory_map_size
;
191 obj
->allocated_len
= 0;
192 obj
->index
= table
->allocated_len
++;
200 error_sigmask_release
:
208 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
210 PERROR("pthread_sigmask");
213 error_pthread_sigmask
:
215 for (i
= 0; i
< 2; i
++) {
216 ret
= close(waitfd
[i
]);
227 struct shm_object
*shm_object_table_append_shadow(struct shm_object_table
*table
,
228 int shm_fd
, int wait_fd
, size_t memory_map_size
)
230 struct shm_object
*obj
;
233 if (table
->allocated_len
>= table
->size
)
235 obj
= &table
->objects
[table
->allocated_len
];
237 /* wait_fd: set read end of the pipe. */
238 obj
->wait_fd
[0] = wait_fd
;
239 obj
->wait_fd
[1] = -1; /* write end is unset. */
240 obj
->shm_fd
= shm_fd
;
242 /* memory_map: mmap */
243 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
244 MAP_SHARED
, shm_fd
, 0);
245 if (memory_map
== MAP_FAILED
) {
249 obj
->memory_map
= memory_map
;
250 obj
->memory_map_size
= memory_map_size
;
251 obj
->allocated_len
= memory_map_size
;
252 obj
->index
= table
->allocated_len
++;
261 void shmp_object_destroy(struct shm_object
*obj
)
265 if (!obj
->is_shadow
) {
266 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
272 if (obj
->shm_fd
>= 0) {
273 ret
= close(obj
->shm_fd
);
279 for (i
= 0; i
< 2; i
++) {
280 if (obj
->wait_fd
[i
] < 0)
282 ret
= close(obj
->wait_fd
[i
]);
290 void shm_object_table_destroy(struct shm_object_table
*table
)
294 for (i
= 0; i
< table
->allocated_len
; i
++)
295 shmp_object_destroy(&table
->objects
[i
]);
300 * zalloc_shm - allocate memory within a shm object.
302 * Shared memory is already zeroed by shmget.
303 * *NOT* multithread-safe (should be protected by mutex).
304 * Returns a -1, -1 tuple on error.
306 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
309 struct shm_ref shm_ref_error
= { -1, -1 };
311 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
312 return shm_ref_error
;
313 ref
.index
= obj
->index
;
314 ref
.offset
= obj
->allocated_len
;
315 obj
->allocated_len
+= len
;
319 void align_shm(struct shm_object
*obj
, size_t align
)
321 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
322 obj
->allocated_len
+= offset_len
;
This page took 0.035747 seconds and 4 git commands to generate.