torture3: Add some brlock entries in cleanup2
[Samba.git] / source3 / lib / messages_dgm.c
blob8b897f641a53f6fcdf90e8364b823f205fe6e6f2
1 /*
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/>.
20 #include "includes.h"
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"
25 #include "messages.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;
34 void *tevent_handle;
35 struct unix_msg_ctx *dgm_ctx;
36 char *cache_dir;
37 int lockfile_fd;
40 struct messaging_dgm_hdr {
41 uint32_t msg_version;
42 enum messaging_type msg_type;
43 struct server_id dst;
44 struct server_id src;
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,
53 void *private_data);
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)
61 fstring buf;
62 char *dir;
63 char *lockfile_name;
64 int lockfile_fd;
65 struct flock lck = {};
66 int unique_len, ret;
67 ssize_t written;
68 bool ok;
70 dir = talloc_asprintf(tmp_ctx, "%s/lck", cache_dir);
71 if (dir == NULL) {
72 return ENOMEM;
75 ok = directory_create_or_exist_strict(dir, sec_initial_uid(), 0755);
76 if (!ok) {
77 ret = errno;
78 DEBUG(1, ("%s: Could not create lock directory: %s\n",
79 __func__, strerror(ret)));
80 TALLOC_FREE(dir);
81 return ret;
84 lockfile_name = talloc_asprintf(tmp_ctx, "%s/%u", dir,
85 (unsigned)pid);
86 TALLOC_FREE(dir);
87 if (lockfile_name == NULL) {
88 DEBUG(1, ("%s: talloc_asprintf failed\n", __func__));
89 return ENOMEM;
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) {
96 ret = errno;
97 DEBUG(1, ("%s: open failed: %s\n", __func__, strerror(errno)));
98 goto fail_free;
101 lck.l_type = F_WRLCK;
102 lck.l_whence = SEEK_SET;
103 lck.l_start = 0;
104 lck.l_len = 0;
106 ret = fcntl(lockfile_fd, F_SETLK, &lck);
107 if (ret == -1) {
108 ret = errno;
109 DEBUG(1, ("%s: fcntl failed: %s\n", __func__, strerror(ret)));
110 goto fail_close;
113 unique_len = snprintf(buf, sizeof(buf), "%"PRIu64, unique);
115 /* shorten a potentially preexisting file */
117 ret = ftruncate(lockfile_fd, unique_len);
118 if (ret == -1) {
119 ret = errno;
120 DEBUG(1, ("%s: ftruncate failed: %s\n", __func__,
121 strerror(ret)));
122 goto fail_unlink;
125 written = write(lockfile_fd, buf, unique_len);
126 if (written != unique_len) {
127 ret = errno;
128 DEBUG(1, ("%s: write failed: %s\n", __func__, strerror(ret)));
129 goto fail_unlink;
132 TALLOC_FREE(lockfile_name);
133 *plockfile_fd = lockfile_fd;
134 return 0;
136 fail_unlink:
137 unlink(lockfile_name);
138 fail_close:
139 close(lockfile_fd);
140 fail_free:
141 TALLOC_FREE(lockfile_name);
142 return ret;
145 static int messaging_dgm_lockfile_remove(TALLOC_CTX *tmp_ctx,
146 const char *cache_dir, pid_t pid)
148 char *lockfile_name;
149 int ret;
151 lockfile_name = talloc_asprintf(
152 tmp_ctx, "%s/lck/%u", cache_dir, (unsigned)pid);
153 if (lockfile_name == NULL) {
154 return ENOMEM;
157 ret = unlink(lockfile_name);
158 if (ret == -1) {
159 ret = errno;
160 DEBUG(10, ("%s: unlink(%s) failed: %s\n", __func__,
161 lockfile_name, strerror(ret)));
164 TALLOC_FREE(lockfile_name);
165 return ret;
168 int messaging_dgm_init(struct messaging_context *msg_ctx,
169 TALLOC_CTX *mem_ctx,
170 struct messaging_backend **presult)
172 struct messaging_backend *result;
173 struct messaging_dgm_context *ctx;
174 struct server_id pid = messaging_server_id(msg_ctx);
175 int ret;
176 bool ok;
177 const char *cache_dir;
178 char *socket_dir;
179 struct sockaddr_un socket_address;
180 size_t sockname_len;
181 uint64_t cookie;
183 cache_dir = lp_cache_directory();
184 if (cache_dir == NULL) {
185 return errno;
188 result = talloc(mem_ctx, struct messaging_backend);
189 if (result == NULL) {
190 goto fail_nomem;
192 ctx = talloc_zero(result, struct messaging_dgm_context);
193 if (ctx == NULL) {
194 goto fail_nomem;
197 result->private_data = ctx;
198 result->send_fn = messaging_dgm_send;
199 ctx->msg_ctx = msg_ctx;
201 ctx->cache_dir = talloc_strdup(ctx, cache_dir);
202 if (ctx->cache_dir == NULL) {
203 goto fail_nomem;
205 socket_dir = talloc_asprintf(ctx, "%s/msg", cache_dir);
206 if (socket_dir == NULL) {
207 goto fail_nomem;
210 socket_address = (struct sockaddr_un) { .sun_family = AF_UNIX };
211 sockname_len = snprintf(socket_address.sun_path,
212 sizeof(socket_address.sun_path),
213 "%s/%u", socket_dir, (unsigned)pid.pid);
214 if (sockname_len >= sizeof(socket_address.sun_path)) {
215 TALLOC_FREE(result);
216 return ENAMETOOLONG;
219 sec_init();
221 ret = messaging_dgm_lockfile_create(ctx, cache_dir, pid.pid,
222 &ctx->lockfile_fd, pid.unique_id);
223 if (ret != 0) {
224 DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
225 __func__, strerror(ret)));
226 TALLOC_FREE(result);
227 return ret;
230 ctx->msg_callbacks = poll_funcs_init_tevent(ctx);
231 if (ctx->msg_callbacks == NULL) {
232 goto fail_nomem;
235 ctx->tevent_handle = poll_funcs_tevent_register(
236 ctx, ctx->msg_callbacks,
237 messaging_tevent_context(msg_ctx));
238 if (ctx->tevent_handle == NULL) {
239 goto fail_nomem;
242 ok = directory_create_or_exist_strict(socket_dir, sec_initial_uid(),
243 0700);
244 if (!ok) {
245 DEBUG(1, ("Could not create socket directory\n"));
246 TALLOC_FREE(result);
247 return EACCES;
249 TALLOC_FREE(socket_dir);
251 unlink(socket_address.sun_path);
253 generate_random_buffer((uint8_t *)&cookie, sizeof(cookie));
255 ret = unix_msg_init(&socket_address, ctx->msg_callbacks, 1024, cookie,
256 messaging_dgm_recv, ctx, &ctx->dgm_ctx);
257 if (ret != 0) {
258 DEBUG(1, ("unix_msg_init failed: %s\n", strerror(ret)));
259 TALLOC_FREE(result);
260 return ret;
262 talloc_set_destructor(ctx, messaging_dgm_context_destructor);
264 *presult = result;
265 return 0;
267 fail_nomem:
268 TALLOC_FREE(result);
269 return ENOMEM;
272 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
274 struct server_id pid = messaging_server_id(c->msg_ctx);
277 * First delete the socket to avoid races. The lockfile is the
278 * indicator that we're still around.
280 unix_msg_free(c->dgm_ctx);
282 if (getpid() == pid.pid) {
283 (void)messaging_dgm_lockfile_remove(c, c->cache_dir, pid.pid);
285 close(c->lockfile_fd);
286 return 0;
289 static int messaging_dgm_send(struct server_id src,
290 struct server_id pid, int msg_type,
291 const struct iovec *iov, int iovlen,
292 struct messaging_backend *backend)
294 struct messaging_dgm_context *ctx = talloc_get_type_abort(
295 backend->private_data, struct messaging_dgm_context);
296 struct messaging_dgm_hdr hdr;
297 struct iovec iov2[iovlen + 1];
298 struct server_id_buf idbuf;
299 struct sockaddr_un dst;
300 ssize_t dst_pathlen;
301 int ret;
303 dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
305 dst_pathlen = snprintf(dst.sun_path, sizeof(dst.sun_path),
306 "%s/msg/%u", ctx->cache_dir, (unsigned)pid.pid);
307 if (dst_pathlen >= sizeof(dst.sun_path)) {
308 return ENAMETOOLONG;
311 hdr.msg_version = MESSAGE_VERSION;
312 hdr.msg_type = msg_type & MSG_TYPE_MASK;
313 hdr.dst = pid;
314 hdr.src = src;
316 DEBUG(10, ("%s: Sending message 0x%x to %s\n", __func__,
317 (unsigned)hdr.msg_type,
318 server_id_str_buf(pid, &idbuf)));
320 iov2[0].iov_base = &hdr;
321 iov2[0].iov_len = sizeof(hdr);
322 memcpy(iov2+1, iov, iovlen*sizeof(struct iovec));
324 become_root();
325 ret = unix_msg_send(ctx->dgm_ctx, &dst, iov2, iovlen + 1);
326 unbecome_root();
328 return ret;
331 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
332 uint8_t *msg, size_t msg_len,
333 void *private_data)
335 struct messaging_dgm_context *dgm_ctx = talloc_get_type_abort(
336 private_data, struct messaging_dgm_context);
337 struct messaging_dgm_hdr *hdr;
338 struct messaging_rec rec;
339 struct server_id_buf idbuf;
341 if (msg_len < sizeof(*hdr)) {
342 DEBUG(1, ("message too short: %u\n", (unsigned)msg_len));
343 return;
347 * unix_msg guarantees alignment, so we can cast here
349 hdr = (struct messaging_dgm_hdr *)msg;
351 rec.msg_version = hdr->msg_version;
352 rec.msg_type = hdr->msg_type;
353 rec.dest = hdr->dst;
354 rec.src = hdr->src;
355 rec.buf.data = msg + sizeof(*hdr);
356 rec.buf.length = msg_len - sizeof(*hdr);
358 DEBUG(10, ("%s: Received message 0x%x len %u from %s\n", __func__,
359 (unsigned)hdr->msg_type, (unsigned)rec.buf.length,
360 server_id_str_buf(rec.src, &idbuf)));
362 messaging_dispatch_rec(dgm_ctx->msg_ctx, &rec);
365 int messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid)
367 struct messaging_backend *be = messaging_local_backend(msg_ctx);
368 struct messaging_dgm_context *ctx = talloc_get_type_abort(
369 be->private_data, struct messaging_dgm_context);
370 char *lockfile_name, *socket_name;
371 int fd, ret;
372 struct flock lck = {};
374 lockfile_name = talloc_asprintf(talloc_tos(), "%s/lck/%u",
375 ctx->cache_dir, (unsigned)pid);
376 if (lockfile_name == NULL) {
377 return ENOMEM;
379 socket_name = talloc_asprintf(lockfile_name, "%s/msg/%u",
380 ctx->cache_dir, (unsigned)pid);
381 if (socket_name == NULL) {
382 TALLOC_FREE(lockfile_name);
383 return ENOMEM;
386 fd = open(lockfile_name, O_NONBLOCK|O_WRONLY, 0);
387 if (fd == -1) {
388 ret = errno;
389 DEBUG(10, ("%s: open(%s) failed: %s\n", __func__,
390 lockfile_name, strerror(ret)));
391 return ret;
394 lck.l_type = F_WRLCK;
395 lck.l_whence = SEEK_SET;
396 lck.l_start = 0;
397 lck.l_len = 0;
399 ret = fcntl(fd, F_SETLK, &lck);
400 if (ret != 0) {
401 ret = errno;
402 DEBUG(10, ("%s: Could not get lock: %s\n", __func__,
403 strerror(ret)));
404 TALLOC_FREE(lockfile_name);
405 close(fd);
406 return ret;
409 (void)unlink(socket_name);
410 (void)unlink(lockfile_name);
411 (void)close(fd);
413 TALLOC_FREE(lockfile_name);
414 return 0;
417 int messaging_dgm_wipe(struct messaging_context *msg_ctx)
419 struct messaging_backend *be = messaging_local_backend(msg_ctx);
420 struct messaging_dgm_context *ctx = talloc_get_type_abort(
421 be->private_data, struct messaging_dgm_context);
422 char *msgdir_name;
423 DIR *msgdir;
424 struct dirent *dp;
425 pid_t our_pid = getpid();
426 int ret;
429 * We scan the socket directory and not the lock directory. Otherwise
430 * we would race against messaging_dgm_lockfile_create's open(O_CREAT)
431 * and fcntl(SETLK).
434 msgdir_name = talloc_asprintf(talloc_tos(), "%s/msg", ctx->cache_dir);
435 if (msgdir_name == NULL) {
436 return ENOMEM;
439 msgdir = opendir(msgdir_name);
440 if (msgdir == NULL) {
441 ret = errno;
442 TALLOC_FREE(msgdir_name);
443 return ret;
445 TALLOC_FREE(msgdir_name);
447 while ((dp = readdir(msgdir)) != NULL) {
448 unsigned long pid;
450 pid = strtoul(dp->d_name, NULL, 10);
451 if (pid == 0) {
453 * . and .. and other malformed entries
455 continue;
457 if (pid == our_pid) {
459 * fcntl(F_GETLK) will succeed for ourselves, we hold
460 * that lock ourselves.
462 continue;
465 ret = messaging_dgm_cleanup(msg_ctx, pid);
466 DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
467 pid, ret ? strerror(ret) : "ok"));
469 closedir(msgdir);
471 return 0;
474 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
475 struct messaging_context *msg_ctx,
476 struct tevent_context *ev)
478 struct messaging_backend *be = messaging_local_backend(msg_ctx);
479 struct messaging_dgm_context *ctx = talloc_get_type_abort(
480 be->private_data, struct messaging_dgm_context);
481 return poll_funcs_tevent_register(mem_ctx, ctx->msg_callbacks, ev);