libust ABI: export streams
[lttng-ust.git] / libringbuffer / shm.c
CommitLineData
1d498196
MD
1/*
2 * libringbuffer/shm.c
3 *
4 * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Dual LGPL v2.1/GPL v2 license.
7 */
8
9#include "shm.h"
10#include <unistd.h>
11#include <fcntl.h>
12#include <sys/mman.h>
13#include <sys/stat.h> /* For mode constants */
14#include <fcntl.h> /* For O_* constants */
15#include <assert.h>
8da6cd6d
MD
16#include <stdio.h>
17#include <signal.h>
18#include <dirent.h>
1d498196
MD
19#include <ust/align.h>
20
21struct shm_object_table *shm_object_table_create(size_t max_nb_obj)
22{
23 struct shm_object_table *table;
24
25 table = zmalloc(sizeof(struct shm_object_table) +
26 max_nb_obj * sizeof(table->objects[0]));
27 table->size = max_nb_obj;
28 return table;
29}
30
31struct shm_object *shm_object_table_append(struct shm_object_table *table,
32 size_t memory_map_size)
33{
5a61337d 34 int shmfd, waitfd[2], ret, i, sigblocked = 0;
1d498196
MD
35 struct shm_object *obj;
36 char *memory_map;
8da6cd6d
MD
37 char tmp_name[NAME_MAX] = "ust-shm-tmp-XXXXXX";
38 sigset_t all_sigs, orig_sigs;
1d498196
MD
39
40 if (table->allocated_len >= table->size)
41 return NULL;
7a9c21bd 42 obj = &table->objects[table->allocated_len];
1d498196
MD
43
44 /* wait_fd: create pipe */
45 ret = pipe(waitfd);
46 if (ret < 0) {
47 PERROR("pipe");
48 goto error_pipe;
49 }
50 for (i = 0; i < 2; i++) {
51 ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC);
52 if (ret < 0) {
53 PERROR("fcntl");
54 goto error_fcntl;
55 }
56 }
5d61a504
MD
57 /* The write end of the pipe needs to be non-blocking */
58 ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK);
59 if (ret < 0) {
60 PERROR("fcntl");
61 goto error_fcntl;
62 }
7a9c21bd 63 memcpy(obj->wait_fd, waitfd, sizeof(waitfd));
1d498196
MD
64
65 /* shm_fd: create shm */
66
8da6cd6d
MD
67 /*
68 * Theoretically, we could leak a shm if the application crashes
69 * between open and unlink. Disable signals on this thread for
70 * increased safety against this scenario.
71 */
72 sigfillset(&all_sigs);
73 ret = pthread_sigmask(SIG_BLOCK, &all_sigs, &orig_sigs);
74 if (ret == -1) {
75 PERROR("pthread_sigmask");
76 goto error_pthread_sigmask;
77 }
5a61337d 78 sigblocked = 1;
8da6cd6d 79
1d498196
MD
80 /*
81 * Allocate shm, and immediately unlink its shm oject, keeping
82 * only the file descriptor as a reference to the object. If it
83 * already exists (caused by short race window during which the
84 * global object exists in a concurrent shm_open), simply retry.
85 * We specifically do _not_ use the / at the beginning of the
86 * pathname so that some OS implementations can keep it local to
87 * the process (POSIX leaves this implementation-defined).
88 */
89 do {
8da6cd6d
MD
90 /*
91 * Using mktemp filename with O_CREAT | O_EXCL open
92 * flags.
93 */
94 mktemp(tmp_name);
95 if (tmp_name[0] == '\0') {
96 PERROR("mktemp");
97 goto error_shm_open;
a8803897 98 }
8da6cd6d 99 shmfd = shm_open(tmp_name,
1d498196 100 O_CREAT | O_EXCL | O_RDWR, 0700);
8da6cd6d 101 } while (shmfd < 0 && (errno == EEXIST || errno == EACCES));
1d498196
MD
102 if (shmfd < 0) {
103 PERROR("shm_open");
104 goto error_shm_open;
105 }
8da6cd6d 106 ret = shm_unlink(tmp_name);
a8803897
MD
107 if (ret < 0 && errno != ENOENT) {
108 PERROR("shm_unlink");
109 goto error_shm_release;
110 }
5a61337d 111 sigblocked = 0;
8da6cd6d
MD
112 ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL);
113 if (ret == -1) {
114 PERROR("pthread_sigmask");
5a61337d 115 goto error_sigmask_release;
8da6cd6d 116 }
1d498196
MD
117 ret = ftruncate(shmfd, memory_map_size);
118 if (ret) {
119 PERROR("ftruncate");
120 goto error_ftruncate;
121 }
122 obj->shm_fd = shmfd;
123
124 /* memory_map: mmap */
125 memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE,
126 MAP_SHARED, shmfd, 0);
127 if (memory_map == MAP_FAILED) {
128 PERROR("mmap");
129 goto error_mmap;
130 }
131 obj->memory_map = memory_map;
132 obj->memory_map_size = memory_map_size;
133 obj->allocated_len = 0;
dc613eb9 134 obj->index = table->allocated_len++;
7a9c21bd 135
1d498196
MD
136 return obj;
137
138error_mmap:
139error_ftruncate:
a8803897 140error_shm_release:
5a61337d 141error_sigmask_release:
1d498196
MD
142 ret = close(shmfd);
143 if (ret) {
144 PERROR("close");
145 assert(0);
146 }
147error_shm_open:
5a61337d
MD
148 if (sigblocked) {
149 ret = pthread_sigmask(SIG_SETMASK, &orig_sigs, NULL);
150 if (ret == -1) {
151 PERROR("pthread_sigmask");
152 }
153 }
8da6cd6d 154error_pthread_sigmask:
1d498196
MD
155error_fcntl:
156 for (i = 0; i < 2; i++) {
157 ret = close(waitfd[i]);
158 if (ret) {
159 PERROR("close");
160 assert(0);
161 }
162 }
163error_pipe:
1d498196
MD
164 return NULL;
165
166}
167
168static
169void shmp_object_destroy(struct shm_object *obj)
170{
171 int ret, i;
172
173 ret = munmap(obj->memory_map, obj->memory_map_size);
174 if (ret) {
175 PERROR("umnmap");
176 assert(0);
177 }
178 ret = close(obj->shm_fd);
179 if (ret) {
180 PERROR("close");
181 assert(0);
182 }
183 for (i = 0; i < 2; i++) {
184 ret = close(obj->wait_fd[i]);
185 if (ret) {
186 PERROR("close");
187 assert(0);
188 }
189 }
190}
191
192void shm_object_table_destroy(struct shm_object_table *table)
193{
194 int i;
195
196 for (i = 0; i < table->allocated_len; i++)
197 shmp_object_destroy(&table->objects[i]);
198 free(table);
199}
200
201/*
202 * zalloc_shm - allocate memory within a shm object.
203 *
204 * Shared memory is already zeroed by shmget.
205 * *NOT* multithread-safe (should be protected by mutex).
206 * Returns a -1, -1 tuple on error.
207 */
208struct shm_ref zalloc_shm(struct shm_object *obj, size_t len)
209{
210 struct shm_ref ref;
211 struct shm_ref shm_ref_error = { -1, -1 };
212
213 if (obj->memory_map_size - obj->allocated_len < len)
214 return shm_ref_error;
215 ref.index = obj->index;
216 ref.offset = obj->allocated_len;
217 obj->allocated_len += len;
218 return ref;
219}
220
221void align_shm(struct shm_object *obj, size_t align)
222{
223 size_t offset_len = offset_align(obj->allocated_len, align);
224 obj->allocated_len += offset_len;
225}
This page took 0.03153 seconds and 4 git commands to generate.