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 "system/network.h"
22 #include "system/filesys.h"
24 #include "lib/util/data_blob.h"
25 #include "lib/util/debug.h"
26 #include "lib/unix_msg/unix_msg.h"
27 #include "system/filesys.h"
28 #include "lib/messages_dgm.h"
29 #include "lib/param/param.h"
30 #include "poll_funcs/poll_funcs_tevent.h"
31 #include "unix_msg/unix_msg.h"
35 * This will carry enough for a socket path
37 char buf[sizeof(struct sockaddr_un)];
40 struct messaging_dgm_context {
42 struct poll_funcs *msg_callbacks;
44 struct unix_msg_ctx *dgm_ctx;
45 struct sun_path_buf socket_dir;
46 struct sun_path_buf lockfile_dir;
49 void (*recv_cb)(const uint8_t *msg,
54 void *recv_cb_private_data;
56 bool *have_dgm_context;
59 static struct messaging_dgm_context *global_dgm_context;
61 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
62 uint8_t *msg, size_t msg_len,
63 int *fds, size_t num_fds,
66 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c);
68 static int messaging_dgm_lockfile_create(struct messaging_dgm_context *ctx,
69 pid_t pid, int *plockfile_fd,
74 struct sun_path_buf lockfile_name;
79 ret = snprintf(lockfile_name.buf, sizeof(lockfile_name.buf),
80 "%s/%u", ctx->lockfile_dir.buf, (unsigned)pid);
81 if (ret >= sizeof(lockfile_name.buf)) {
85 /* no O_EXCL, existence check is via the fcntl lock */
87 lockfile_fd = open(lockfile_name.buf, O_NONBLOCK|O_CREAT|O_RDWR,
89 if (lockfile_fd == -1) {
91 DEBUG(1, ("%s: open failed: %s\n", __func__, strerror(errno)));
95 lck = (struct flock) {
100 ret = fcntl(lockfile_fd, F_SETLK, &lck);
103 DEBUG(1, ("%s: fcntl failed: %s\n", __func__, strerror(ret)));
107 unique_len = snprintf(buf, sizeof(buf), "%ju\n", (uintmax_t)unique);
109 /* shorten a potentially preexisting file */
111 ret = ftruncate(lockfile_fd, unique_len);
114 DEBUG(1, ("%s: ftruncate failed: %s\n", __func__,
119 written = write(lockfile_fd, buf, unique_len);
120 if (written != unique_len) {
122 DEBUG(1, ("%s: write failed: %s\n", __func__, strerror(ret)));
126 *plockfile_fd = lockfile_fd;
130 unlink(lockfile_name.buf);
136 int messaging_dgm_init(struct tevent_context *ev,
138 const char *socket_dir,
139 const char *lockfile_dir,
140 void (*recv_cb)(const uint8_t *msg,
145 void *recv_cb_private_data)
147 struct messaging_dgm_context *ctx;
149 struct sockaddr_un socket_address;
151 static bool have_dgm_context = false;
153 if (have_dgm_context) {
157 ctx = talloc_zero(NULL, struct messaging_dgm_context);
162 ctx->recv_cb = recv_cb;
163 ctx->recv_cb_private_data = recv_cb_private_data;
165 len = strlcpy(ctx->lockfile_dir.buf, lockfile_dir,
166 sizeof(ctx->lockfile_dir.buf));
167 if (len >= sizeof(ctx->lockfile_dir.buf)) {
172 len = strlcpy(ctx->socket_dir.buf, socket_dir,
173 sizeof(ctx->socket_dir.buf));
174 if (len >= sizeof(ctx->socket_dir.buf)) {
179 socket_address = (struct sockaddr_un) { .sun_family = AF_UNIX };
180 len = snprintf(socket_address.sun_path,
181 sizeof(socket_address.sun_path),
182 "%s/%u", socket_dir, (unsigned)ctx->pid);
183 if (len >= sizeof(socket_address.sun_path)) {
188 ret = messaging_dgm_lockfile_create(ctx, ctx->pid, &ctx->lockfile_fd,
191 DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
192 __func__, strerror(ret)));
197 ctx->msg_callbacks = poll_funcs_init_tevent(ctx);
198 if (ctx->msg_callbacks == NULL) {
202 ctx->tevent_handle = poll_funcs_tevent_register(
203 ctx, ctx->msg_callbacks, ev);
204 if (ctx->tevent_handle == NULL) {
208 unlink(socket_address.sun_path);
210 ret = unix_msg_init(&socket_address, ctx->msg_callbacks, 1024,
211 messaging_dgm_recv, ctx, &ctx->dgm_ctx);
213 DEBUG(1, ("unix_msg_init failed: %s\n", strerror(ret)));
217 talloc_set_destructor(ctx, messaging_dgm_context_destructor);
219 ctx->have_dgm_context = &have_dgm_context;
221 global_dgm_context = ctx;
229 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
232 * First delete the socket to avoid races. The lockfile is the
233 * indicator that we're still around.
235 unix_msg_free(c->dgm_ctx);
237 if (getpid() == c->pid) {
238 struct sun_path_buf name;
241 ret = snprintf(name.buf, sizeof(name.buf), "%s/%u",
242 c->lockfile_dir.buf, (unsigned)c->pid);
243 if (ret >= sizeof(name.buf)) {
245 * We've checked the length when creating, so this
246 * should never happen
252 close(c->lockfile_fd);
254 if (c->have_dgm_context != NULL) {
255 *c->have_dgm_context = false;
261 void messaging_dgm_destroy(void)
263 TALLOC_FREE(global_dgm_context);
266 int messaging_dgm_send(pid_t pid,
267 const struct iovec *iov, int iovlen,
268 const int *fds, size_t num_fds)
270 struct messaging_dgm_context *ctx = global_dgm_context;
271 struct sockaddr_un dst;
279 dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
281 dst_pathlen = snprintf(dst.sun_path, sizeof(dst.sun_path),
282 "%s/%u", ctx->socket_dir.buf, (unsigned)pid);
283 if (dst_pathlen >= sizeof(dst.sun_path)) {
287 DEBUG(10, ("%s: Sending message to %u\n", __func__, (unsigned)pid));
289 ret = unix_msg_send(ctx->dgm_ctx, &dst, iov, iovlen, fds, num_fds);
294 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
295 uint8_t *msg, size_t msg_len,
296 int *fds, size_t num_fds,
299 struct messaging_dgm_context *dgm_ctx = talloc_get_type_abort(
300 private_data, struct messaging_dgm_context);
302 dgm_ctx->recv_cb(msg, msg_len, fds, num_fds,
303 dgm_ctx->recv_cb_private_data);
306 static int messaging_dgm_read_unique(int fd, uint64_t *punique)
310 unsigned long long unique;
313 rw_ret = pread(fd, buf, sizeof(buf)-1, 0);
319 unique = strtoull(buf, &endptr, 10);
320 if ((unique == 0) && (errno == EINVAL)) {
323 if ((unique == ULLONG_MAX) && (errno == ERANGE)) {
326 if (endptr[0] != '\n') {
333 int messaging_dgm_get_unique(pid_t pid, uint64_t *unique)
335 struct messaging_dgm_context *ctx = global_dgm_context;
336 struct sun_path_buf lockfile_name;
343 if (pid == getpid()) {
345 * Protect against losing our own lock
347 return messaging_dgm_read_unique(ctx->lockfile_fd, unique);
350 ret = snprintf(lockfile_name.buf, sizeof(lockfile_name.buf),
351 "%s/%u", ctx->lockfile_dir.buf, (int)pid);
352 if (ret >= sizeof(lockfile_name.buf)) {
356 fd = open(lockfile_name.buf, O_NONBLOCK|O_RDONLY, 0);
361 ret = messaging_dgm_read_unique(fd, unique);
366 int messaging_dgm_cleanup(pid_t pid)
368 struct messaging_dgm_context *ctx = global_dgm_context;
369 struct sun_path_buf lockfile_name, socket_name;
371 struct flock lck = {};
377 len = snprintf(socket_name.buf, sizeof(socket_name.buf), "%s/%u",
378 ctx->socket_dir.buf, (unsigned)pid);
379 if (len >= sizeof(socket_name.buf)) {
383 len = snprintf(lockfile_name.buf, sizeof(lockfile_name.buf), "%s/%u",
384 ctx->lockfile_dir.buf, (unsigned)pid);
385 if (len >= sizeof(lockfile_name.buf)) {
389 fd = open(lockfile_name.buf, O_NONBLOCK|O_WRONLY, 0);
393 DEBUG(10, ("%s: open(%s) failed: %s\n", __func__,
394 lockfile_name.buf, strerror(ret)));
399 lck.l_type = F_WRLCK;
400 lck.l_whence = SEEK_SET;
404 ret = fcntl(fd, F_SETLK, &lck);
407 if ((ret != EACCES) && (ret != EAGAIN)) {
408 DEBUG(10, ("%s: Could not get lock: %s\n", __func__,
415 DEBUG(10, ("%s: Cleaning up : %s\n", __func__, strerror(ret)));
417 (void)unlink(socket_name.buf);
418 (void)unlink(lockfile_name.buf);
423 int messaging_dgm_wipe(void)
425 struct messaging_dgm_context *ctx = global_dgm_context;
428 pid_t our_pid = getpid();
436 * We scan the socket directory and not the lock directory. Otherwise
437 * we would race against messaging_dgm_lockfile_create's open(O_CREAT)
441 msgdir = opendir(ctx->socket_dir.buf);
442 if (msgdir == NULL) {
446 while ((dp = readdir(msgdir)) != NULL) {
449 pid = strtoul(dp->d_name, NULL, 10);
452 * . and .. and other malformed entries
456 if (pid == our_pid) {
458 * fcntl(F_GETLK) will succeed for ourselves, we hold
459 * that lock ourselves.
464 ret = messaging_dgm_cleanup(pid);
465 DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
466 pid, ret ? strerror(ret) : "ok"));
473 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
474 struct tevent_context *ev)
476 struct messaging_dgm_context *ctx = global_dgm_context;
481 return poll_funcs_tevent_register(mem_ctx, ctx->msg_callbacks, ev);