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>
35 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
37 struct shm_object_table
*table
;
39 table
= zmalloc(sizeof(struct shm_object_table
) +
40 max_nb_obj
* sizeof(table
->objects
[0]));
41 table
->size
= max_nb_obj
;
45 struct shm_object
*shm_object_table_append(struct shm_object_table
*table
,
46 size_t memory_map_size
)
48 int shmfd
, waitfd
[2], ret
, i
, sigblocked
= 0;
49 struct shm_object
*obj
;
51 char tmp_name
[NAME_MAX
] = "ust-shm-tmp-XXXXXX";
52 sigset_t all_sigs
, orig_sigs
;
54 if (table
->allocated_len
>= table
->size
)
56 obj
= &table
->objects
[table
->allocated_len
];
58 /* wait_fd: create pipe */
64 for (i
= 0; i
< 2; i
++) {
65 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
71 /* The write end of the pipe needs to be non-blocking */
72 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
77 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
79 /* shm_fd: create shm */
82 * Theoretically, we could leak a shm if the application crashes
83 * between open and unlink. Disable signals on this thread for
84 * increased safety against this scenario.
86 sigfillset(&all_sigs
);
87 ret
= pthread_sigmask(SIG_BLOCK
, &all_sigs
, &orig_sigs
);
89 PERROR("pthread_sigmask");
90 goto error_pthread_sigmask
;
95 * Allocate shm, and immediately unlink its shm oject, keeping
96 * only the file descriptor as a reference to the object. If it
97 * already exists (caused by short race window during which the
98 * global object exists in a concurrent shm_open), simply retry.
99 * We specifically do _not_ use the / at the beginning of the
100 * pathname so that some OS implementations can keep it local to
101 * the process (POSIX leaves this implementation-defined).
105 * Using mktemp filename with O_CREAT | O_EXCL open
109 if (tmp_name
[0] == '\0') {
113 shmfd
= shm_open(tmp_name
,
114 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
115 } while (shmfd
< 0 && (errno
== EEXIST
|| errno
== EACCES
));
120 ret
= shm_unlink(tmp_name
);
121 if (ret
< 0 && errno
!= ENOENT
) {
122 PERROR("shm_unlink");
123 goto error_shm_release
;
126 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
128 PERROR("pthread_sigmask");
129 goto error_sigmask_release
;
131 ret
= ftruncate(shmfd
, memory_map_size
);
134 goto error_ftruncate
;
138 /* memory_map: mmap */
139 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
140 MAP_SHARED
, shmfd
, 0);
141 if (memory_map
== MAP_FAILED
) {
145 obj
->memory_map
= memory_map
;
146 obj
->memory_map_size
= memory_map_size
;
147 obj
->allocated_len
= 0;
148 obj
->index
= table
->allocated_len
++;
155 error_sigmask_release
:
163 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
165 PERROR("pthread_sigmask");
168 error_pthread_sigmask
:
170 for (i
= 0; i
< 2; i
++) {
171 ret
= close(waitfd
[i
]);
182 struct shm_object
*shm_object_table_append_shadow(struct shm_object_table
*table
,
183 int shm_fd
, int wait_fd
, size_t memory_map_size
)
185 struct shm_object
*obj
;
188 if (table
->allocated_len
>= table
->size
)
190 obj
= &table
->objects
[table
->allocated_len
];
192 /* wait_fd: set read end of the pipe. */
193 obj
->wait_fd
[0] = wait_fd
;
194 obj
->wait_fd
[1] = -1; /* write end is unset. */
195 obj
->shm_fd
= shm_fd
;
197 /* memory_map: mmap */
198 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
199 MAP_SHARED
, shm_fd
, 0);
200 if (memory_map
== MAP_FAILED
) {
204 obj
->memory_map
= memory_map
;
205 obj
->memory_map_size
= memory_map_size
;
206 obj
->allocated_len
= memory_map_size
;
207 obj
->index
= table
->allocated_len
++;
216 void shmp_object_destroy(struct shm_object
*obj
)
220 if (!obj
->is_shadow
) {
221 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
227 if (obj
->shm_fd
>= 0) {
228 ret
= close(obj
->shm_fd
);
234 for (i
= 0; i
< 2; i
++) {
235 if (obj
->wait_fd
[i
] < 0)
237 ret
= close(obj
->wait_fd
[i
]);
245 void shm_object_table_destroy(struct shm_object_table
*table
)
249 for (i
= 0; i
< table
->allocated_len
; i
++)
250 shmp_object_destroy(&table
->objects
[i
]);
255 * zalloc_shm - allocate memory within a shm object.
257 * Shared memory is already zeroed by shmget.
258 * *NOT* multithread-safe (should be protected by mutex).
259 * Returns a -1, -1 tuple on error.
261 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
264 struct shm_ref shm_ref_error
= { -1, -1 };
266 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
267 return shm_ref_error
;
268 ref
.index
= obj
->index
;
269 ref
.offset
= obj
->allocated_len
;
270 obj
->allocated_len
+= len
;
274 void align_shm(struct shm_object
*obj
, size_t align
)
276 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
277 obj
->allocated_len
+= offset_len
;
This page took 0.037459 seconds and 4 git commands to generate.