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
26 #include <sys/stat.h> /* For mode constants */
27 #include <fcntl.h> /* For O_* constants */
32 #include <lttng/align.h>
36 /* FIXME: Include UUID the proper way, e.g. config.h... */
37 #include <uuid/uuid.h>
40 * Ensure we have the required amount of space available by writing 0
41 * into the entire buffer. Not doing so can trigger SIGBUS when going
42 * beyond the available shm space.
45 int zero_file(int fd
, size_t len
)
53 pagelen
= sysconf(_SC_PAGESIZE
);
56 zeropage
= calloc(pagelen
, 1);
60 while (len
> written
) {
62 retlen
= write(fd
, zeropage
,
63 min_t(size_t, pagelen
, len
- written
));
64 } while (retlen
== -1UL && errno
== EINTR
);
77 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
79 struct shm_object_table
*table
;
81 table
= zmalloc(sizeof(struct shm_object_table
) +
82 max_nb_obj
* sizeof(table
->objects
[0]));
83 table
->size
= max_nb_obj
;
88 * Generate a unique name with the desired prefix.
89 * Pattern is as follow: prefix-pid-uuid.
90 * Caller is responsible of freeing the resulting string.
93 char *gen_unique_name(const char *prefix
)
99 char tmp_name
[NAME_MAX
];
108 uuid_unparse(uuid
, uuid_str
);
110 written
= snprintf(tmp_name
, NAME_MAX
,
111 "%s-%d-%s", prefix
, pid
, uuid_str
);
113 if (written
< 0 || written
>= NAME_MAX
)
116 name
= zmalloc(written
+ 1);
121 return strncpy(name
, tmp_name
, written
);
124 struct shm_object
*shm_object_table_append(struct shm_object_table
*table
,
125 size_t memory_map_size
)
127 int shmfd
, ret
, sigblocked
= 0;
128 struct shm_object
*obj
;
131 const char *base_shm
= "/dev/shm/";
132 const char *base_path
= "/tmp/lttng-fds/";
133 const char *waitfd_prefix
= "ust-wait";
134 const char *shm_prefix
= "ust-shm";
136 char *wait_pipe_path
, *wait_pipe_file
;
137 char *shm_path
, *shm_symlink_path
, *shm_file
;
139 char tmp_name
[NAME_MAX
] = "ust-shm-tmp-XXXXXX";
141 sigset_t all_sigs
, orig_sigs
;
143 if (table
->allocated_len
>= table
->size
)
145 obj
= &table
->objects
[table
->allocated_len
];
147 wait_pipe_file
= gen_unique_name(waitfd_prefix
);
149 if (!wait_pipe_file
) {
150 goto error_gen_unique_wait
;
153 wait_pipe_path
= zmalloc(strlen(base_path
)
154 + strlen(wait_pipe_file
) + 1);
156 if (!wait_pipe_path
) {
157 free(wait_pipe_file
);
158 goto error_wait_alloc
;
161 strncat(wait_pipe_path
, base_path
, strlen(base_path
));
162 strncat(wait_pipe_path
, wait_pipe_file
, strlen(wait_pipe_file
));
164 free(wait_pipe_file
);
166 /* wait_fd: create named pipe */
167 ret
= mkfifo(wait_pipe_path
, 0777);
173 obj
->wait_fd
[0] = -1;
174 obj
->wait_fd
[1] = -1;
175 obj
->wait_pipe_path
= wait_pipe_path
;
177 /* shm_fd: create shm */
180 * Theoretically, we could leak a shm if the application crashes
181 * between open and unlink. Disable signals on this thread for
182 * increased safety against this scenario.
184 sigfillset(&all_sigs
);
185 ret
= pthread_sigmask(SIG_BLOCK
, &all_sigs
, &orig_sigs
);
187 PERROR("pthread_sigmask");
188 goto error_pthread_sigmask
;
193 * We specifically do _not_ use the / at the beginning of the
194 * pathname so that some OS implementations can keep it local to
195 * the process (POSIX leaves this implementation-defined).
199 * Using mktemp filename with O_CREAT | O_EXCL open
203 if (tmp_name
[0] == '\0') {
207 shmfd
= shm_open(tmp_name
,
208 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
209 } while (shmfd
< 0 && (errno
== EEXIST
|| errno
== EACCES
));
216 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
218 PERROR("pthread_sigmask");
219 goto error_sigmask_release
;
222 /* Create unique symlink to shm */
223 shm_path
= zmalloc(strlen(base_shm
) + strlen(tmp_name
) + 1);
226 goto error_shm_alloc
;
229 strncat(shm_path
, base_shm
, strlen(base_shm
));
230 strncat(shm_path
, tmp_name
, strlen(tmp_name
));
232 shm_file
= gen_unique_name(shm_prefix
);
236 goto error_gen_unique_shm
;
239 shm_symlink_path
= zmalloc(strlen(base_path
) + strlen(shm_file
) + 1);
241 if (!shm_symlink_path
) {
244 goto error_symlink_alloc
;
247 strncat(shm_symlink_path
, base_path
, strlen(base_path
));
248 strncat(shm_symlink_path
, shm_file
, strlen(shm_file
));
252 ret
= symlink(shm_path
, shm_symlink_path
);
256 free(shm_symlink_path
);
257 goto error_symlink_shm
;
262 ret
= zero_file(shmfd
, memory_map_size
);
265 goto error_zero_file
;
267 ret
= ftruncate(shmfd
, memory_map_size
);
270 goto error_ftruncate
;
273 obj
->shm_path
= shm_symlink_path
;
275 /* memory_map: mmap */
276 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
277 MAP_SHARED
, shmfd
, 0);
278 if (memory_map
== MAP_FAILED
) {
282 obj
->memory_map
= memory_map
;
283 obj
->memory_map_size
= memory_map_size
;
284 obj
->allocated_len
= 0;
285 obj
->index
= table
->allocated_len
++;
292 free(shm_symlink_path
);
295 error_gen_unique_shm
:
297 error_sigmask_release
:
305 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
307 PERROR("pthread_sigmask");
310 error_pthread_sigmask
:
312 free(wait_pipe_path
);
314 error_gen_unique_wait
:
318 struct shm_object
*shm_object_table_append_shadow(struct shm_object_table
*table
,
319 int shm_fd
, int wait_fd
, size_t memory_map_size
)
321 struct shm_object
*obj
;
324 if (table
->allocated_len
>= table
->size
)
326 obj
= &table
->objects
[table
->allocated_len
];
328 /* wait_fd: set read end of the pipe. */
329 obj
->wait_fd
[0] = wait_fd
;
330 obj
->wait_fd
[1] = -1; /* write end is unset. */
331 obj
->shm_fd
= shm_fd
;
333 /* memory_map: mmap */
334 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
335 MAP_SHARED
, shm_fd
, 0);
336 if (memory_map
== MAP_FAILED
) {
340 obj
->memory_map
= memory_map
;
341 obj
->memory_map_size
= memory_map_size
;
342 obj
->allocated_len
= memory_map_size
;
343 obj
->index
= table
->allocated_len
++;
352 void shmp_object_destroy(struct shm_object
*obj
)
356 if (!obj
->is_shadow
) {
357 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
363 if (obj
->shm_fd
>= 0) {
364 ret
= close(obj
->shm_fd
);
375 for (i
= 0; i
< 2; i
++) {
376 if (obj
->wait_fd
[i
] < 0)
378 ret
= close(obj
->wait_fd
[i
]);
385 if (obj
->wait_pipe_path
) {
386 free(obj
->wait_pipe_path
);
390 void shm_object_table_destroy(struct shm_object_table
*table
)
394 for (i
= 0; i
< table
->allocated_len
; i
++)
395 shmp_object_destroy(&table
->objects
[i
]);
400 * zalloc_shm - allocate memory within a shm object.
402 * Shared memory is already zeroed by shmget.
403 * *NOT* multithread-safe (should be protected by mutex).
404 * Returns a -1, -1 tuple on error.
406 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
409 struct shm_ref shm_ref_error
= { -1, -1 };
411 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
412 return shm_ref_error
;
413 ref
.index
= obj
->index
;
414 ref
.offset
= obj
->allocated_len
;
415 obj
->allocated_len
+= len
;
419 void align_shm(struct shm_object
*obj
, size_t align
)
421 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
422 obj
->allocated_len
+= offset_len
;
This page took 0.036997 seconds and 4 git commands to generate.