4 * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Dual LGPL v2.1/GPL v2 license.
13 #include <sys/stat.h> /* For mode constants */
14 #include <fcntl.h> /* For O_* constants */
19 #include <lttng/align.h>
23 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
25 struct shm_object_table
*table
;
27 table
= zmalloc(sizeof(struct shm_object_table
) +
28 max_nb_obj
* sizeof(table
->objects
[0]));
29 table
->size
= max_nb_obj
;
33 struct shm_object
*shm_object_table_append(struct shm_object_table
*table
,
34 size_t memory_map_size
)
36 int shmfd
, waitfd
[2], ret
, i
, sigblocked
= 0;
37 struct shm_object
*obj
;
39 char tmp_name
[NAME_MAX
] = "ust-shm-tmp-XXXXXX";
40 sigset_t all_sigs
, orig_sigs
;
42 if (table
->allocated_len
>= table
->size
)
44 obj
= &table
->objects
[table
->allocated_len
];
46 /* wait_fd: create pipe */
52 for (i
= 0; i
< 2; i
++) {
53 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
59 /* The write end of the pipe needs to be non-blocking */
60 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
65 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
67 /* shm_fd: create shm */
70 * Theoretically, we could leak a shm if the application crashes
71 * between open and unlink. Disable signals on this thread for
72 * increased safety against this scenario.
74 sigfillset(&all_sigs
);
75 ret
= pthread_sigmask(SIG_BLOCK
, &all_sigs
, &orig_sigs
);
77 PERROR("pthread_sigmask");
78 goto error_pthread_sigmask
;
83 * Allocate shm, and immediately unlink its shm oject, keeping
84 * only the file descriptor as a reference to the object. If it
85 * already exists (caused by short race window during which the
86 * global object exists in a concurrent shm_open), simply retry.
87 * We specifically do _not_ use the / at the beginning of the
88 * pathname so that some OS implementations can keep it local to
89 * the process (POSIX leaves this implementation-defined).
93 * Using mktemp filename with O_CREAT | O_EXCL open
97 if (tmp_name
[0] == '\0') {
101 shmfd
= shm_open(tmp_name
,
102 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
103 } while (shmfd
< 0 && (errno
== EEXIST
|| errno
== EACCES
));
108 ret
= shm_unlink(tmp_name
);
109 if (ret
< 0 && errno
!= ENOENT
) {
110 PERROR("shm_unlink");
111 goto error_shm_release
;
114 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
116 PERROR("pthread_sigmask");
117 goto error_sigmask_release
;
119 ret
= ftruncate(shmfd
, memory_map_size
);
122 goto error_ftruncate
;
126 /* memory_map: mmap */
127 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
128 MAP_SHARED
, shmfd
, 0);
129 if (memory_map
== MAP_FAILED
) {
133 obj
->memory_map
= memory_map
;
134 obj
->memory_map_size
= memory_map_size
;
135 obj
->allocated_len
= 0;
136 obj
->index
= table
->allocated_len
++;
143 error_sigmask_release
:
151 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
153 PERROR("pthread_sigmask");
156 error_pthread_sigmask
:
158 for (i
= 0; i
< 2; i
++) {
159 ret
= close(waitfd
[i
]);
170 struct shm_object
*shm_object_table_append_shadow(struct shm_object_table
*table
,
171 int shm_fd
, int wait_fd
, size_t memory_map_size
)
173 struct shm_object
*obj
;
176 if (table
->allocated_len
>= table
->size
)
178 obj
= &table
->objects
[table
->allocated_len
];
180 /* wait_fd: set read end of the pipe. */
181 obj
->wait_fd
[0] = wait_fd
;
182 obj
->wait_fd
[1] = -1; /* write end is unset. */
183 obj
->shm_fd
= shm_fd
;
185 /* memory_map: mmap */
186 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
187 MAP_SHARED
, shm_fd
, 0);
188 if (memory_map
== MAP_FAILED
) {
192 obj
->memory_map
= memory_map
;
193 obj
->memory_map_size
= memory_map_size
;
194 obj
->allocated_len
= memory_map_size
;
195 obj
->index
= table
->allocated_len
++;
204 void shmp_object_destroy(struct shm_object
*obj
)
208 if (!obj
->is_shadow
) {
209 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
215 if (obj
->shm_fd
>= 0) {
216 ret
= close(obj
->shm_fd
);
222 for (i
= 0; i
< 2; i
++) {
223 if (obj
->wait_fd
[i
] < 0)
225 ret
= close(obj
->wait_fd
[i
]);
233 void shm_object_table_destroy(struct shm_object_table
*table
)
237 for (i
= 0; i
< table
->allocated_len
; i
++)
238 shmp_object_destroy(&table
->objects
[i
]);
243 * zalloc_shm - allocate memory within a shm object.
245 * Shared memory is already zeroed by shmget.
246 * *NOT* multithread-safe (should be protected by mutex).
247 * Returns a -1, -1 tuple on error.
249 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
252 struct shm_ref shm_ref_error
= { -1, -1 };
254 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
255 return shm_ref_error
;
256 ref
.index
= obj
->index
;
257 ref
.offset
= obj
->allocated_len
;
258 obj
->allocated_len
+= len
;
262 void align_shm(struct shm_object
*obj
, size_t align
)
264 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
265 obj
->allocated_len
+= offset_len
;
This page took 0.037631 seconds and 4 git commands to generate.