lib: Move lp_ctdbd_socket() to cluster_support.c
[kai/samba-autobuild/.git] / source3 / lib / messages_dgm.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Samba internal messaging functions
4  * Copyright (C) 2013 by Volker Lendecke
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
23 #include <dirent.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"
32
33 struct sun_path_buf {
34         /*
35          * This will carry enough for a socket path
36          */
37         char buf[sizeof(struct sockaddr_un)];
38 };
39
40 struct messaging_dgm_context {
41         pid_t pid;
42         struct poll_funcs *msg_callbacks;
43         void *tevent_handle;
44         struct unix_msg_ctx *dgm_ctx;
45         struct sun_path_buf socket_dir;
46         struct sun_path_buf lockfile_dir;
47         int lockfile_fd;
48
49         void (*recv_cb)(const uint8_t *msg,
50                         size_t msg_len,
51                         int *fds,
52                         size_t num_fds,
53                         void *private_data);
54         void *recv_cb_private_data;
55
56         bool *have_dgm_context;
57 };
58
59 static struct messaging_dgm_context *global_dgm_context;
60
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,
64                                void *private_data);
65
66 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c);
67
68 static int messaging_dgm_lockfile_create(struct messaging_dgm_context *ctx,
69                                          pid_t pid, int *plockfile_fd,
70                                          uint64_t unique)
71 {
72         char buf[64];
73         int lockfile_fd;
74         struct sun_path_buf lockfile_name;
75         struct flock lck;
76         int unique_len, ret;
77         ssize_t written;
78
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)) {
82                 return ENAMETOOLONG;
83         }
84
85         /* no O_EXCL, existence check is via the fcntl lock */
86
87         lockfile_fd = open(lockfile_name.buf, O_NONBLOCK|O_CREAT|O_RDWR,
88                            0644);
89         if (lockfile_fd == -1) {
90                 ret = errno;
91                 DEBUG(1, ("%s: open failed: %s\n", __func__, strerror(errno)));
92                 return ret;
93         }
94
95         lck = (struct flock) {
96                 .l_type = F_WRLCK,
97                 .l_whence = SEEK_SET
98         };
99
100         ret = fcntl(lockfile_fd, F_SETLK, &lck);
101         if (ret == -1) {
102                 ret = errno;
103                 DEBUG(1, ("%s: fcntl failed: %s\n", __func__, strerror(ret)));
104                 goto fail_close;
105         }
106
107         unique_len = snprintf(buf, sizeof(buf), "%ju\n", (uintmax_t)unique);
108
109         /* shorten a potentially preexisting file */
110
111         ret = ftruncate(lockfile_fd, unique_len);
112         if (ret == -1) {
113                 ret = errno;
114                 DEBUG(1, ("%s: ftruncate failed: %s\n", __func__,
115                           strerror(ret)));
116                 goto fail_unlink;
117         }
118
119         written = write(lockfile_fd, buf, unique_len);
120         if (written != unique_len) {
121                 ret = errno;
122                 DEBUG(1, ("%s: write failed: %s\n", __func__, strerror(ret)));
123                 goto fail_unlink;
124         }
125
126         *plockfile_fd = lockfile_fd;
127         return 0;
128
129 fail_unlink:
130         unlink(lockfile_name.buf);
131 fail_close:
132         close(lockfile_fd);
133         return ret;
134 }
135
136 int messaging_dgm_init(struct tevent_context *ev,
137                        uint64_t unique,
138                        const char *socket_dir,
139                        const char *lockfile_dir,
140                        void (*recv_cb)(const uint8_t *msg,
141                                        size_t msg_len,
142                                        int *fds,
143                                        size_t num_fds,
144                                        void *private_data),
145                        void *recv_cb_private_data)
146 {
147         struct messaging_dgm_context *ctx;
148         int ret;
149         struct sockaddr_un socket_address;
150         size_t len;
151         static bool have_dgm_context = false;
152
153         if (have_dgm_context) {
154                 return EEXIST;
155         }
156
157         ctx = talloc_zero(NULL, struct messaging_dgm_context);
158         if (ctx == NULL) {
159                 goto fail_nomem;
160         }
161         ctx->pid = getpid();
162         ctx->recv_cb = recv_cb;
163         ctx->recv_cb_private_data = recv_cb_private_data;
164
165         len = strlcpy(ctx->lockfile_dir.buf, lockfile_dir,
166                       sizeof(ctx->lockfile_dir.buf));
167         if (len >= sizeof(ctx->lockfile_dir.buf)) {
168                 TALLOC_FREE(ctx);
169                 return ENAMETOOLONG;
170         }
171
172         len = strlcpy(ctx->socket_dir.buf, socket_dir,
173                       sizeof(ctx->socket_dir.buf));
174         if (len >= sizeof(ctx->socket_dir.buf)) {
175                 TALLOC_FREE(ctx);
176                 return ENAMETOOLONG;
177         }
178
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)) {
184                 TALLOC_FREE(ctx);
185                 return ENAMETOOLONG;
186         }
187
188         ret = messaging_dgm_lockfile_create(ctx, ctx->pid, &ctx->lockfile_fd,
189                                             unique);
190         if (ret != 0) {
191                 DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
192                           __func__, strerror(ret)));
193                 TALLOC_FREE(ctx);
194                 return ret;
195         }
196
197         ctx->msg_callbacks = poll_funcs_init_tevent(ctx);
198         if (ctx->msg_callbacks == NULL) {
199                 goto fail_nomem;
200         }
201
202         ctx->tevent_handle = poll_funcs_tevent_register(
203                 ctx, ctx->msg_callbacks, ev);
204         if (ctx->tevent_handle == NULL) {
205                 goto fail_nomem;
206         }
207
208         unlink(socket_address.sun_path);
209
210         ret = unix_msg_init(&socket_address, ctx->msg_callbacks, 1024,
211                             messaging_dgm_recv, ctx, &ctx->dgm_ctx);
212         if (ret != 0) {
213                 DEBUG(1, ("unix_msg_init failed: %s\n", strerror(ret)));
214                 TALLOC_FREE(ctx);
215                 return ret;
216         }
217         talloc_set_destructor(ctx, messaging_dgm_context_destructor);
218
219         ctx->have_dgm_context = &have_dgm_context;
220
221         global_dgm_context = ctx;
222         return 0;
223
224 fail_nomem:
225         TALLOC_FREE(ctx);
226         return ENOMEM;
227 }
228
229 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
230 {
231         /*
232          * First delete the socket to avoid races. The lockfile is the
233          * indicator that we're still around.
234          */
235         unix_msg_free(c->dgm_ctx);
236
237         if (getpid() == c->pid) {
238                 struct sun_path_buf name;
239                 int ret;
240
241                 ret = snprintf(name.buf, sizeof(name.buf), "%s/%u",
242                                c->lockfile_dir.buf, (unsigned)c->pid);
243                 if (ret >= sizeof(name.buf)) {
244                         /*
245                          * We've checked the length when creating, so this
246                          * should never happen
247                          */
248                         abort();
249                 }
250                 unlink(name.buf);
251         }
252         close(c->lockfile_fd);
253
254         if (c->have_dgm_context != NULL) {
255                 *c->have_dgm_context = false;
256         }
257
258         return 0;
259 }
260
261 void messaging_dgm_destroy(void)
262 {
263         TALLOC_FREE(global_dgm_context);
264 }
265
266 int messaging_dgm_send(pid_t pid,
267                        const struct iovec *iov, int iovlen,
268                        const int *fds, size_t num_fds)
269 {
270         struct messaging_dgm_context *ctx = global_dgm_context;
271         struct sockaddr_un dst;
272         ssize_t dst_pathlen;
273         int ret;
274
275         if (ctx == NULL) {
276                 return ENOTCONN;
277         }
278
279         dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
280
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)) {
284                 return ENAMETOOLONG;
285         }
286
287         DEBUG(10, ("%s: Sending message to %u\n", __func__, (unsigned)pid));
288
289         ret = unix_msg_send(ctx->dgm_ctx, &dst, iov, iovlen, fds, num_fds);
290
291         return ret;
292 }
293
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,
297                                void *private_data)
298 {
299         struct messaging_dgm_context *dgm_ctx = talloc_get_type_abort(
300                 private_data, struct messaging_dgm_context);
301
302         dgm_ctx->recv_cb(msg, msg_len, fds, num_fds,
303                          dgm_ctx->recv_cb_private_data);
304 }
305
306 static int messaging_dgm_read_unique(int fd, uint64_t *punique)
307 {
308         char buf[25];
309         ssize_t rw_ret;
310         unsigned long long unique;
311         char *endptr;
312
313         rw_ret = pread(fd, buf, sizeof(buf)-1, 0);
314         if (rw_ret == -1) {
315                 return errno;
316         }
317         buf[rw_ret] = '\0';
318
319         unique = strtoull(buf, &endptr, 10);
320         if ((unique == 0) && (errno == EINVAL)) {
321                 return EINVAL;
322         }
323         if ((unique == ULLONG_MAX) && (errno == ERANGE)) {
324                 return ERANGE;
325         }
326         if (endptr[0] != '\n') {
327                 return EINVAL;
328         }
329         *punique = unique;
330         return 0;
331 }
332
333 int messaging_dgm_get_unique(pid_t pid, uint64_t *unique)
334 {
335         struct messaging_dgm_context *ctx = global_dgm_context;
336         struct sun_path_buf lockfile_name;
337         int ret, fd;
338
339         if (ctx == NULL) {
340                 return EBADF;
341         }
342
343         if (pid == getpid()) {
344                 /*
345                  * Protect against losing our own lock
346                  */
347                 return messaging_dgm_read_unique(ctx->lockfile_fd, unique);
348         }
349
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)) {
353                 return ENAMETOOLONG;
354         }
355
356         fd = open(lockfile_name.buf, O_NONBLOCK|O_RDONLY, 0);
357         if (fd == -1) {
358                 return errno;
359         }
360
361         ret = messaging_dgm_read_unique(fd, unique);
362         close(fd);
363         return ret;
364 }
365
366 int messaging_dgm_cleanup(pid_t pid)
367 {
368         struct messaging_dgm_context *ctx = global_dgm_context;
369         struct sun_path_buf lockfile_name, socket_name;
370         int fd, len, ret;
371         struct flock lck = {};
372
373         if (ctx == NULL) {
374                 return ENOTCONN;
375         }
376
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)) {
380                 return ENAMETOOLONG;
381         }
382
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)) {
386                 return ENAMETOOLONG;
387         }
388
389         fd = open(lockfile_name.buf, O_NONBLOCK|O_WRONLY, 0);
390         if (fd == -1) {
391                 ret = errno;
392                 if (ret != ENOENT) {
393                         DEBUG(10, ("%s: open(%s) failed: %s\n", __func__,
394                                    lockfile_name.buf, strerror(ret)));
395                 }
396                 return ret;
397         }
398
399         lck.l_type = F_WRLCK;
400         lck.l_whence = SEEK_SET;
401         lck.l_start = 0;
402         lck.l_len = 0;
403
404         ret = fcntl(fd, F_SETLK, &lck);
405         if (ret != 0) {
406                 ret = errno;
407                 if ((ret != EACCES) && (ret != EAGAIN)) {
408                         DEBUG(10, ("%s: Could not get lock: %s\n", __func__,
409                                    strerror(ret)));
410                 }
411                 close(fd);
412                 return ret;
413         }
414
415         DEBUG(10, ("%s: Cleaning up : %s\n", __func__, strerror(ret)));
416
417         (void)unlink(socket_name.buf);
418         (void)unlink(lockfile_name.buf);
419         (void)close(fd);
420         return 0;
421 }
422
423 int messaging_dgm_wipe(void)
424 {
425         struct messaging_dgm_context *ctx = global_dgm_context;
426         DIR *msgdir;
427         struct dirent *dp;
428         pid_t our_pid = getpid();
429         int ret;
430
431         if (ctx == NULL) {
432                 return ENOTCONN;
433         }
434
435         /*
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)
438          * and fcntl(SETLK).
439          */
440
441         msgdir = opendir(ctx->socket_dir.buf);
442         if (msgdir == NULL) {
443                 return errno;
444         }
445
446         while ((dp = readdir(msgdir)) != NULL) {
447                 unsigned long pid;
448
449                 pid = strtoul(dp->d_name, NULL, 10);
450                 if (pid == 0) {
451                         /*
452                          * . and .. and other malformed entries
453                          */
454                         continue;
455                 }
456                 if (pid == our_pid) {
457                         /*
458                          * fcntl(F_GETLK) will succeed for ourselves, we hold
459                          * that lock ourselves.
460                          */
461                         continue;
462                 }
463
464                 ret = messaging_dgm_cleanup(pid);
465                 DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
466                            pid, ret ? strerror(ret) : "ok"));
467         }
468         closedir(msgdir);
469
470         return 0;
471 }
472
473 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
474                                             struct tevent_context *ev)
475 {
476         struct messaging_dgm_context *ctx = global_dgm_context;
477
478         if (ctx == NULL) {
479                 return NULL;
480         }
481         return poll_funcs_tevent_register(mem_ctx, ctx->msg_callbacks, ev);
482 }