2 * Unix SMB/CIFS implementation.
3 * Samba internal messaging functions
4 * Copyright (C) 2013 by Volker Lendecke
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "lib/util/data_blob.h"
22 #include "lib/util/debug.h"
23 #include "lib/unix_msg/unix_msg.h"
24 #include "system/filesys.h"
26 #include "lib/param/param.h"
27 #include "poll_funcs/poll_funcs_tevent.h"
28 #include "unix_msg/unix_msg.h"
29 #include "librpc/gen_ndr/messaging.h"
31 struct messaging_dgm_context
{
32 struct messaging_context
*msg_ctx
;
33 struct poll_funcs
*msg_callbacks
;
35 struct unix_msg_ctx
*dgm_ctx
;
40 struct messaging_dgm_hdr
{
42 enum messaging_type msg_type
;
47 static int messaging_dgm_send(struct server_id src
,
48 struct server_id pid
, int msg_type
,
49 const struct iovec
*iov
, int iovlen
,
50 struct messaging_backend
*backend
);
51 static void messaging_dgm_recv(struct unix_msg_ctx
*ctx
,
52 uint8_t *msg
, size_t msg_len
,
55 static int messaging_dgm_context_destructor(struct messaging_dgm_context
*c
);
57 static int messaging_dgm_lockfile_create(TALLOC_CTX
*tmp_ctx
,
58 const char *cache_dir
, pid_t pid
,
59 int *plockfile_fd
, uint64_t unique
)
65 struct flock lck
= {};
70 dir
= talloc_asprintf(tmp_ctx
, "%s/lck", cache_dir
);
75 ok
= directory_create_or_exist_strict(dir
, sec_initial_uid(), 0755);
78 DEBUG(1, ("%s: Could not create lock directory: %s\n",
79 __func__
, strerror(ret
)));
84 lockfile_name
= talloc_asprintf(tmp_ctx
, "%s/%u", dir
,
87 if (lockfile_name
== NULL
) {
88 DEBUG(1, ("%s: talloc_asprintf failed\n", __func__
));
92 /* no O_EXCL, existence check is via the fcntl lock */
94 lockfile_fd
= open(lockfile_name
, O_NONBLOCK
|O_CREAT
|O_WRONLY
, 0644);
95 if (lockfile_fd
== -1) {
97 DEBUG(1, ("%s: open failed: %s\n", __func__
, strerror(errno
)));
101 lck
.l_type
= F_WRLCK
;
102 lck
.l_whence
= SEEK_SET
;
106 ret
= fcntl(lockfile_fd
, F_SETLK
, &lck
);
109 DEBUG(1, ("%s: fcntl failed: %s\n", __func__
, strerror(ret
)));
113 unique_len
= snprintf(buf
, sizeof(buf
), "%"PRIu64
, unique
);
115 /* shorten a potentially preexisting file */
117 ret
= ftruncate(lockfile_fd
, unique_len
);
120 DEBUG(1, ("%s: ftruncate failed: %s\n", __func__
,
125 written
= write(lockfile_fd
, buf
, unique_len
);
126 if (written
!= unique_len
) {
128 DEBUG(1, ("%s: write failed: %s\n", __func__
, strerror(ret
)));
132 TALLOC_FREE(lockfile_name
);
133 *plockfile_fd
= lockfile_fd
;
137 unlink(lockfile_name
);
141 TALLOC_FREE(lockfile_name
);
145 static int messaging_dgm_lockfile_remove(const char *cache_dir
, pid_t pid
)
149 char *lockfile_name
, *to_free
;
153 fstr_sprintf(fname
, "lck/%u", (unsigned)pid
);
155 len
= full_path_tos(cache_dir
, fname
, buf
, sizeof(buf
),
156 &lockfile_name
, &to_free
);
161 ret
= unlink(lockfile_name
);
164 DEBUG(10, ("%s: unlink failed: %s\n", __func__
,
167 TALLOC_FREE(to_free
);
171 NTSTATUS
messaging_dgm_init(struct messaging_context
*msg_ctx
,
173 struct messaging_backend
**presult
)
175 struct messaging_backend
*result
;
176 struct messaging_dgm_context
*ctx
;
177 struct server_id pid
= messaging_server_id(msg_ctx
);
180 const char *cache_dir
;
182 struct sockaddr_un socket_address
;
186 cache_dir
= lp_cache_directory();
187 if (cache_dir
== NULL
) {
188 NTSTATUS status
= map_nt_error_from_unix(errno
);
192 result
= talloc(mem_ctx
, struct messaging_backend
);
193 if (result
== NULL
) {
196 ctx
= talloc_zero(result
, struct messaging_dgm_context
);
201 result
->private_data
= ctx
;
202 result
->send_fn
= messaging_dgm_send
;
203 ctx
->msg_ctx
= msg_ctx
;
205 ctx
->cache_dir
= talloc_strdup(ctx
, cache_dir
);
206 if (ctx
->cache_dir
== NULL
) {
209 socket_dir
= talloc_asprintf(ctx
, "%s/msg", cache_dir
);
210 if (socket_dir
== NULL
) {
214 socket_address
= (struct sockaddr_un
) { .sun_family
= AF_UNIX
};
215 sockname_len
= snprintf(socket_address
.sun_path
,
216 sizeof(socket_address
.sun_path
),
217 "%s/%u", socket_dir
, (unsigned)pid
.pid
);
218 if (sockname_len
>= sizeof(socket_address
.sun_path
)) {
220 return NT_STATUS_NAME_TOO_LONG
;
225 ret
= messaging_dgm_lockfile_create(ctx
, cache_dir
, pid
.pid
,
226 &ctx
->lockfile_fd
, pid
.unique_id
);
228 DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
229 __func__
, strerror(ret
)));
231 return map_nt_error_from_unix(ret
);
234 ctx
->msg_callbacks
= poll_funcs_init_tevent(ctx
);
235 if (ctx
->msg_callbacks
== NULL
) {
237 return NT_STATUS_NO_MEMORY
;
240 ctx
->tevent_handle
= poll_funcs_tevent_register(
241 ctx
, ctx
->msg_callbacks
,
242 messaging_tevent_context(msg_ctx
));
243 if (ctx
->tevent_handle
== NULL
) {
245 return NT_STATUS_NO_MEMORY
;
248 ok
= directory_create_or_exist_strict(socket_dir
, sec_initial_uid(),
251 DEBUG(1, ("Could not create socket directory\n"));
253 return NT_STATUS_ACCESS_DENIED
;
255 TALLOC_FREE(socket_dir
);
257 unlink(socket_address
.sun_path
);
259 generate_random_buffer((uint8_t *)&cookie
, sizeof(cookie
));
261 ret
= unix_msg_init(&socket_address
, ctx
->msg_callbacks
, 1024, cookie
,
262 messaging_dgm_recv
, ctx
, &ctx
->dgm_ctx
);
264 DEBUG(1, ("unix_msg_init failed: %s\n", strerror(ret
)));
266 return map_nt_error_from_unix(ret
);
268 talloc_set_destructor(ctx
, messaging_dgm_context_destructor
);
275 return NT_STATUS_NO_MEMORY
;
278 static int messaging_dgm_context_destructor(struct messaging_dgm_context
*c
)
280 struct server_id pid
= messaging_server_id(c
->msg_ctx
);
283 * First delete the socket to avoid races. The lockfile is the
284 * indicator that we're still around.
286 unix_msg_free(c
->dgm_ctx
);
288 if (getpid() == pid
.pid
) {
289 (void)messaging_dgm_lockfile_remove(c
->cache_dir
, pid
.pid
);
291 close(c
->lockfile_fd
);
295 static int messaging_dgm_send(struct server_id src
,
296 struct server_id pid
, int msg_type
,
297 const struct iovec
*iov
, int iovlen
,
298 struct messaging_backend
*backend
)
300 struct messaging_dgm_context
*ctx
= talloc_get_type_abort(
301 backend
->private_data
, struct messaging_dgm_context
);
302 struct messaging_dgm_hdr hdr
;
303 struct iovec iov2
[iovlen
+ 1];
304 struct server_id_buf idbuf
;
305 struct sockaddr_un dst
;
309 dst
= (struct sockaddr_un
) { .sun_family
= AF_UNIX
};
311 dst_pathlen
= snprintf(dst
.sun_path
, sizeof(dst
.sun_path
),
312 "%s/msg/%u", ctx
->cache_dir
, (unsigned)pid
.pid
);
313 if (dst_pathlen
>= sizeof(dst
.sun_path
)) {
317 hdr
.msg_version
= MESSAGE_VERSION
;
318 hdr
.msg_type
= msg_type
& MSG_TYPE_MASK
;
322 DEBUG(10, ("%s: Sending message 0x%x to %s\n", __func__
,
323 (unsigned)hdr
.msg_type
,
324 server_id_str_buf(pid
, &idbuf
)));
326 iov2
[0].iov_base
= &hdr
;
327 iov2
[0].iov_len
= sizeof(hdr
);
328 memcpy(iov2
+1, iov
, iovlen
*sizeof(struct iovec
));
331 ret
= unix_msg_send(ctx
->dgm_ctx
, &dst
, iov2
, iovlen
+ 1);
337 static void messaging_dgm_recv(struct unix_msg_ctx
*ctx
,
338 uint8_t *msg
, size_t msg_len
,
341 struct messaging_dgm_context
*dgm_ctx
= talloc_get_type_abort(
342 private_data
, struct messaging_dgm_context
);
343 struct messaging_dgm_hdr
*hdr
;
344 struct messaging_rec rec
;
345 struct server_id_buf idbuf
;
347 if (msg_len
< sizeof(*hdr
)) {
348 DEBUG(1, ("message too short: %u\n", (unsigned)msg_len
));
353 * unix_msg guarantees alignment, so we can cast here
355 hdr
= (struct messaging_dgm_hdr
*)msg
;
357 rec
.msg_version
= hdr
->msg_version
;
358 rec
.msg_type
= hdr
->msg_type
;
361 rec
.buf
.data
= msg
+ sizeof(*hdr
);
362 rec
.buf
.length
= msg_len
- sizeof(*hdr
);
364 DEBUG(10, ("%s: Received message 0x%x len %u from %s\n", __func__
,
365 (unsigned)hdr
->msg_type
, (unsigned)rec
.buf
.length
,
366 server_id_str_buf(rec
.src
, &idbuf
)));
368 messaging_dispatch_rec(dgm_ctx
->msg_ctx
, &rec
);
371 int messaging_dgm_cleanup(struct messaging_context
*msg_ctx
, pid_t pid
)
373 struct messaging_backend
*be
= messaging_local_backend(msg_ctx
);
374 struct messaging_dgm_context
*ctx
= talloc_get_type_abort(
375 be
->private_data
, struct messaging_dgm_context
);
376 char *lockfile_name
, *socket_name
;
378 struct flock lck
= {};
380 lockfile_name
= talloc_asprintf(talloc_tos(), "%s/lck/%u",
381 ctx
->cache_dir
, (unsigned)pid
);
382 if (lockfile_name
== NULL
) {
385 socket_name
= talloc_asprintf(lockfile_name
, "%s/msg/%u",
386 ctx
->cache_dir
, (unsigned)pid
);
387 if (socket_name
== NULL
) {
388 TALLOC_FREE(lockfile_name
);
392 fd
= open(lockfile_name
, O_NONBLOCK
|O_WRONLY
, 0);
395 DEBUG(10, ("%s: open(%s) failed: %s\n", __func__
,
396 lockfile_name
, strerror(ret
)));
400 lck
.l_type
= F_WRLCK
;
401 lck
.l_whence
= SEEK_SET
;
405 ret
= fcntl(fd
, F_SETLK
, &lck
);
408 DEBUG(10, ("%s: Could not get lock: %s\n", __func__
,
410 TALLOC_FREE(lockfile_name
);
415 (void)unlink(socket_name
);
416 (void)unlink(lockfile_name
);
419 TALLOC_FREE(lockfile_name
);
423 NTSTATUS
messaging_dgm_wipe(struct messaging_context
*msg_ctx
)
425 struct messaging_backend
*be
= messaging_local_backend(msg_ctx
);
426 struct messaging_dgm_context
*ctx
= talloc_get_type_abort(
427 be
->private_data
, struct messaging_dgm_context
);
431 pid_t our_pid
= getpid();
434 * We scan the socket directory and not the lock directory. Otherwise
435 * we would race against messaging_dgm_lockfile_create's open(O_CREAT)
439 msgdir_name
= talloc_asprintf(talloc_tos(), "%s/msg", ctx
->cache_dir
);
440 if (msgdir_name
== NULL
) {
441 return NT_STATUS_NO_MEMORY
;
444 msgdir
= opendir(msgdir_name
);
445 TALLOC_FREE(msgdir_name
);
446 if (msgdir
== NULL
) {
447 return map_nt_error_from_unix(errno
);
450 while ((dp
= readdir(msgdir
)) != NULL
) {
453 pid
= strtoul(dp
->d_name
, NULL
, 10);
456 * . and .. and other malformed entries
460 if (pid
== our_pid
) {
462 * fcntl(F_GETLK) will succeed for ourselves, we hold
463 * that lock ourselves.
468 ret
= messaging_dgm_cleanup(msg_ctx
, pid
);
469 DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
470 pid
, ret
? strerror(ret
) : "ok"));
477 void *messaging_dgm_register_tevent_context(TALLOC_CTX
*mem_ctx
,
478 struct messaging_context
*msg_ctx
,
479 struct tevent_context
*ev
)
481 struct messaging_backend
*be
= messaging_local_backend(msg_ctx
);
482 struct messaging_dgm_context
*ctx
= talloc_get_type_abort(
483 be
->private_data
, struct messaging_dgm_context
);
484 return poll_funcs_tevent_register(mem_ctx
, ctx
->msg_callbacks
, ev
);