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 <ust/align.h>
21 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
23 struct shm_object_table
*table
;
25 table
= zmalloc(sizeof(struct shm_object_table
) +
26 max_nb_obj
* sizeof(table
->objects
[0]));
27 table
->size
= max_nb_obj
;
31 struct shm_object
*shm_object_table_append(struct shm_object_table
*table
,
32 size_t memory_map_size
)
34 int shmfd
, waitfd
[2], ret
, i
, sigblocked
= 0;
35 struct shm_object
*obj
;
37 char tmp_name
[NAME_MAX
] = "ust-shm-tmp-XXXXXX";
38 sigset_t all_sigs
, orig_sigs
;
40 if (table
->allocated_len
>= table
->size
)
42 obj
= &table
->objects
[table
->allocated_len
];
44 /* wait_fd: create pipe */
50 for (i
= 0; i
< 2; i
++) {
51 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
57 /* The write end of the pipe needs to be non-blocking */
58 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
63 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
65 /* shm_fd: create shm */
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.
72 sigfillset(&all_sigs
);
73 ret
= pthread_sigmask(SIG_BLOCK
, &all_sigs
, &orig_sigs
);
75 PERROR("pthread_sigmask");
76 goto error_pthread_sigmask
;
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).
91 * Using mktemp filename with O_CREAT | O_EXCL open
95 if (tmp_name
[0] == '\0') {
99 shmfd
= shm_open(tmp_name
,
100 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
101 } while (shmfd
< 0 && (errno
== EEXIST
|| errno
== EACCES
));
106 ret
= shm_unlink(tmp_name
);
107 if (ret
< 0 && errno
!= ENOENT
) {
108 PERROR("shm_unlink");
109 goto error_shm_release
;
112 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
114 PERROR("pthread_sigmask");
115 goto error_sigmask_release
;
117 ret
= ftruncate(shmfd
, memory_map_size
);
120 goto error_ftruncate
;
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
) {
131 obj
->memory_map
= memory_map
;
132 obj
->memory_map_size
= memory_map_size
;
133 obj
->allocated_len
= 0;
134 obj
->index
= table
->allocated_len
++;
141 error_sigmask_release
:
149 ret
= pthread_sigmask(SIG_SETMASK
, &orig_sigs
, NULL
);
151 PERROR("pthread_sigmask");
154 error_pthread_sigmask
:
156 for (i
= 0; i
< 2; i
++) {
157 ret
= close(waitfd
[i
]);
168 struct shm_object
*shm_object_table_append_shadow(struct shm_object_table
*table
,
169 int shm_fd
, int wait_fd
, size_t memory_map_size
)
171 struct shm_object
*obj
;
174 if (table
->allocated_len
>= table
->size
)
176 obj
= &table
->objects
[table
->allocated_len
];
178 /* wait_fd: set read end of the pipe. */
179 obj
->wait_fd
[0] = wait_fd
;
180 obj
->wait_fd
[1] = -1; /* write end is unset. */
181 obj
->shm_fd
= shm_fd
;
183 /* memory_map: mmap */
184 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
185 MAP_SHARED
, shm_fd
, 0);
186 if (memory_map
== MAP_FAILED
) {
190 obj
->memory_map
= memory_map
;
191 obj
->memory_map_size
= memory_map_size
;
192 obj
->allocated_len
= memory_map_size
;
193 obj
->index
= table
->allocated_len
++;
202 void shmp_object_destroy(struct shm_object
*obj
)
206 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
211 ret
= close(obj
->shm_fd
);
216 for (i
= 0; i
< 2; i
++) {
217 if (obj
->wait_fd
[i
] < 0)
219 ret
= close(obj
->wait_fd
[i
]);
227 void shm_object_table_destroy(struct shm_object_table
*table
)
231 for (i
= 0; i
< table
->allocated_len
; i
++)
232 shmp_object_destroy(&table
->objects
[i
]);
237 * zalloc_shm - allocate memory within a shm object.
239 * Shared memory is already zeroed by shmget.
240 * *NOT* multithread-safe (should be protected by mutex).
241 * Returns a -1, -1 tuple on error.
243 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
246 struct shm_ref shm_ref_error
= { -1, -1 };
248 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
249 return shm_ref_error
;
250 ref
.index
= obj
->index
;
251 ref
.offset
= obj
->allocated_len
;
252 obj
->allocated_len
+= len
;
256 void align_shm(struct shm_object
*obj
, size_t align
)
258 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
259 obj
->allocated_len
+= offset_len
;
This page took 0.034268 seconds and 4 git commands to generate.