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]));
82 table
->size
= max_nb_obj
;
87 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
88 size_t memory_map_size
)
90 int shmfd
, waitfd
[2], ret
, i
, sigblocked
= 0;
91 struct shm_object
*obj
;
93 char tmp_name
[NAME_MAX
] = "/ust-shm-tmp-XXXXXX";
94 sigset_t all_sigs
, orig_sigs
;
96 if (table
->allocated_len
>= table
->size
)
98 obj
= &table
->objects
[table
->allocated_len
];
100 /* wait_fd: create pipe */
106 for (i
= 0; i
< 2; i
++) {
107 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
113 /* The write end of the pipe needs to be non-blocking */
114 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
119 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
121 /* shm_fd: create shm */
124 * Theoretically, we could leak a shm if the application crashes
125 * between open and unlink. Disable signals on this thread for
126 * increased safety against this scenario.
128 sigfillset(&all_sigs
);
129 ret
= pthread_sigmask(SIG_BLOCK
, &all_sigs
, &orig_sigs
);
131 PERROR("pthread_sigmask");
132 goto error_pthread_sigmask
;
137 * Allocate shm, and immediately unlink its shm oject, keeping
138 * only the file descriptor as a reference to the object. If it
139 * already exists (caused by short race window during which the
140 * global object exists in a concurrent shm_open), simply retry.
141 * We specifically do _not_ use the / at the beginning of the
142 * pathname so that some OS implementations can keep it local to
143 * the process (POSIX leaves this implementation-defined).
147 * Using mktemp filename with O_CREAT | O_EXCL open
150 (void) mktemp(tmp_name
);
151 if (tmp_name
[0] == '\0') {
155 shmfd
= shm_open(tmp_name
,
156 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
157 } while (shmfd
< 0 && (errno
== EEXIST
|| errno
== EACCES
));
162 ret
= shm_unlink(tmp_name
);
163 if (ret
< 0 && errno
!= ENOENT
) {
164 PERROR("shm_unlink");
165 goto error_shm_release
;
168 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
170 PERROR("pthread_sigmask");
171 goto error_sigmask_release
;
173 ret
= zero_file(shmfd
, memory_map_size
);
176 goto error_zero_file
;
178 ret
= ftruncate(shmfd
, memory_map_size
);
181 goto error_ftruncate
;
185 /* memory_map: mmap */
186 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
187 MAP_SHARED
, shmfd
, 0);
188 if (memory_map
== MAP_FAILED
) {
192 obj
->type
= SHM_OBJECT_SHM
;
193 obj
->memory_map
= memory_map
;
194 obj
->memory_map_size
= memory_map_size
;
195 obj
->allocated_len
= 0;
196 obj
->index
= table
->allocated_len
++;
204 error_sigmask_release
:
212 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
214 PERROR("pthread_sigmask");
217 error_pthread_sigmask
:
219 for (i
= 0; i
< 2; i
++) {
220 ret
= close(waitfd
[i
]);
231 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
232 size_t memory_map_size
)
234 struct shm_object
*obj
;
236 int waitfd
[2], i
, ret
;
238 if (table
->allocated_len
>= table
->size
)
240 obj
= &table
->objects
[table
->allocated_len
];
242 memory_map
= zmalloc(memory_map_size
);
246 /* wait_fd: create pipe */
252 for (i
= 0; i
< 2; i
++) {
253 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
259 /* The write end of the pipe needs to be non-blocking */
260 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
265 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
270 obj
->type
= SHM_OBJECT_MEM
;
271 obj
->memory_map
= memory_map
;
272 obj
->memory_map_size
= memory_map_size
;
273 obj
->allocated_len
= 0;
274 obj
->index
= table
->allocated_len
++;
279 for (i
= 0; i
< 2; i
++) {
280 ret
= close(waitfd
[i
]);
292 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
293 size_t memory_map_size
,
294 enum shm_object_type type
)
298 return _shm_object_table_alloc_shm(table
, memory_map_size
);
300 return _shm_object_table_alloc_mem(table
, memory_map_size
);
307 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
308 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
309 size_t memory_map_size
)
311 struct shm_object
*obj
;
315 if (table
->allocated_len
>= table
->size
)
317 /* streams _must_ be received in sequential order, else fail. */
318 if (stream_nr
+ 1 != table
->allocated_len
)
321 obj
= &table
->objects
[table
->allocated_len
];
323 /* wait_fd: set write end of the pipe. */
324 obj
->wait_fd
[0] = -1; /* read end is unset */
325 obj
->wait_fd
[1] = wakeup_fd
;
326 obj
->shm_fd
= shm_fd
;
328 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
333 /* The write end of the pipe needs to be non-blocking */
334 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
340 /* memory_map: mmap */
341 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
342 MAP_SHARED
, shm_fd
, 0);
343 if (memory_map
== MAP_FAILED
) {
347 obj
->type
= SHM_OBJECT_SHM
;
348 obj
->memory_map
= memory_map
;
349 obj
->memory_map_size
= memory_map_size
;
350 obj
->allocated_len
= memory_map_size
;
351 obj
->index
= table
->allocated_len
++;
361 * Passing ownership of mem to object.
363 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
364 void *mem
, size_t memory_map_size
, int wakeup_fd
)
366 struct shm_object
*obj
;
369 if (table
->allocated_len
>= table
->size
)
371 obj
= &table
->objects
[table
->allocated_len
];
373 obj
->wait_fd
[0] = -1; /* read end is unset */
374 obj
->wait_fd
[1] = wakeup_fd
;
377 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
382 /* The write end of the pipe needs to be non-blocking */
383 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
389 obj
->type
= SHM_OBJECT_MEM
;
390 obj
->memory_map
= mem
;
391 obj
->memory_map_size
= memory_map_size
;
392 obj
->allocated_len
= memory_map_size
;
393 obj
->index
= table
->allocated_len
++;
402 void shmp_object_destroy(struct shm_object
*obj
)
409 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
414 ret
= close(obj
->shm_fd
);
419 for (i
= 0; i
< 2; i
++) {
420 if (obj
->wait_fd
[i
] < 0)
422 ret
= close(obj
->wait_fd
[i
]);
434 for (i
= 0; i
< 2; i
++) {
435 if (obj
->wait_fd
[i
] < 0)
437 ret
= close(obj
->wait_fd
[i
]);
443 free(obj
->memory_map
);
451 void shm_object_table_destroy(struct shm_object_table
*table
)
455 for (i
= 0; i
< table
->allocated_len
; i
++)
456 shmp_object_destroy(&table
->objects
[i
]);
461 * zalloc_shm - allocate memory within a shm object.
463 * Shared memory is already zeroed by shmget.
464 * *NOT* multithread-safe (should be protected by mutex).
465 * Returns a -1, -1 tuple on error.
467 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
470 struct shm_ref shm_ref_error
= { -1, -1 };
472 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
473 return shm_ref_error
;
474 ref
.index
= obj
->index
;
475 ref
.offset
= obj
->allocated_len
;
476 obj
->allocated_len
+= len
;
480 void align_shm(struct shm_object
*obj
, size_t align
)
482 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
483 obj
->allocated_len
+= offset_len
;
This page took 0.039 seconds and 4 git commands to generate.