messaging_dgm: messaging_dgm_lockfile_remove does not use tmp_ctx anymore
[sfrench/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 "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 "lib/messages_dgm.h"
26 #include "lib/param/param.h"
27 #include "poll_funcs/poll_funcs_tevent.h"
28 #include "unix_msg/unix_msg.h"
29
30 struct sun_path_buf {
31         /*
32          * This will carry enough for a socket path
33          */
34         char buf[sizeof(struct sockaddr_un)];
35 };
36
37 struct messaging_dgm_context {
38         pid_t pid;
39         struct poll_funcs *msg_callbacks;
40         void *tevent_handle;
41         struct unix_msg_ctx *dgm_ctx;
42         char *cache_dir;
43         int lockfile_fd;
44
45         void (*recv_cb)(const uint8_t *msg,
46                         size_t msg_len,
47                         void *private_data);
48         void *recv_cb_private_data;
49
50         bool *have_dgm_context;
51 };
52
53 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
54                                uint8_t *msg, size_t msg_len,
55                                void *private_data);
56
57 static int messaging_dgm_lockfile_name(struct sun_path_buf *buf,
58                                        const char *cache_dir,
59                                        pid_t pid)
60 {
61         int ret;
62
63         ret = snprintf(buf->buf, sizeof(buf->buf), "%s/lck/%u", cache_dir,
64                        (unsigned)pid);
65         if (ret >= sizeof(buf->buf)) {
66                 return ENAMETOOLONG;
67         }
68         return 0;
69 }
70
71 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c);
72
73 static int messaging_dgm_lockfile_create(const char *cache_dir,
74                                          uid_t dir_owner, pid_t pid,
75                                          int *plockfile_fd, uint64_t unique)
76 {
77         fstring buf;
78         struct sun_path_buf dir;
79         struct sun_path_buf lockfile_name;
80         int lockfile_fd;
81         struct flock lck;
82         int unique_len, ret;
83         ssize_t written;
84         bool ok;
85
86         ret = messaging_dgm_lockfile_name(&lockfile_name, cache_dir, pid);
87         if (ret != 0) {
88                 return ret;
89         }
90
91         /* shorter than lockfile_name, can't overflow */
92         snprintf(dir.buf, sizeof(dir.buf), "%s/lck", cache_dir);
93
94         ok = directory_create_or_exist_strict(dir.buf, dir_owner, 0755);
95         if (!ok) {
96                 ret = errno;
97                 DEBUG(1, ("%s: Could not create lock directory: %s\n",
98                           __func__, strerror(ret)));
99                 return ret;
100         }
101
102         /* no O_EXCL, existence check is via the fcntl lock */
103
104         lockfile_fd = open(lockfile_name.buf, O_NONBLOCK|O_CREAT|O_WRONLY,
105                            0644);
106         if (lockfile_fd == -1) {
107                 ret = errno;
108                 DEBUG(1, ("%s: open failed: %s\n", __func__, strerror(errno)));
109                 return ret;
110         }
111
112         lck = (struct flock) {
113                 .l_type = F_WRLCK,
114                 .l_whence = SEEK_SET
115         };
116
117         ret = fcntl(lockfile_fd, F_SETLK, &lck);
118         if (ret == -1) {
119                 ret = errno;
120                 DEBUG(1, ("%s: fcntl failed: %s\n", __func__, strerror(ret)));
121                 goto fail_close;
122         }
123
124         unique_len = snprintf(buf, sizeof(buf), "%ju\n", (uintmax_t)unique);
125
126         /* shorten a potentially preexisting file */
127
128         ret = ftruncate(lockfile_fd, unique_len);
129         if (ret == -1) {
130                 ret = errno;
131                 DEBUG(1, ("%s: ftruncate failed: %s\n", __func__,
132                           strerror(ret)));
133                 goto fail_unlink;
134         }
135
136         written = write(lockfile_fd, buf, unique_len);
137         if (written != unique_len) {
138                 ret = errno;
139                 DEBUG(1, ("%s: write failed: %s\n", __func__, strerror(ret)));
140                 goto fail_unlink;
141         }
142
143         *plockfile_fd = lockfile_fd;
144         return 0;
145
146 fail_unlink:
147         unlink(lockfile_name.buf);
148 fail_close:
149         close(lockfile_fd);
150         return ret;
151 }
152
153 static int messaging_dgm_lockfile_remove(const char *cache_dir, pid_t pid)
154 {
155         struct sun_path_buf lockfile_name;
156         int ret;
157
158         ret = messaging_dgm_lockfile_name(&lockfile_name, cache_dir, pid);
159         if (ret != 0) {
160                 return ret;
161         }
162
163         ret = unlink(lockfile_name.buf);
164         if (ret == -1) {
165                 ret = errno;
166                 DEBUG(10, ("%s: unlink(%s) failed: %s\n", __func__,
167                            lockfile_name.buf, strerror(ret)));
168         }
169
170         return ret;
171 }
172
173 int messaging_dgm_init(TALLOC_CTX *mem_ctx,
174                        struct tevent_context *ev,
175                        struct server_id pid,
176                        const char *cache_dir,
177                        uid_t dir_owner,
178                        void (*recv_cb)(const uint8_t *msg,
179                                        size_t msg_len,
180                                        void *private_data),
181                        void *recv_cb_private_data,
182                        struct messaging_dgm_context **pctx)
183 {
184         struct messaging_dgm_context *ctx;
185         int ret;
186         bool ok;
187         char *socket_dir;
188         struct sockaddr_un socket_address;
189         size_t sockname_len;
190         uint64_t cookie;
191         static bool have_dgm_context = false;
192
193         if (have_dgm_context) {
194                 return EEXIST;
195         }
196
197         ctx = talloc_zero(mem_ctx, struct messaging_dgm_context);
198         if (ctx == NULL) {
199                 goto fail_nomem;
200         }
201         ctx->pid = pid.pid;
202         ctx->recv_cb = recv_cb;
203         ctx->recv_cb_private_data = recv_cb_private_data;
204
205         ctx->cache_dir = talloc_strdup(ctx, cache_dir);
206         if (ctx->cache_dir == NULL) {
207                 goto fail_nomem;
208         }
209         socket_dir = talloc_asprintf(ctx, "%s/msg", cache_dir);
210         if (socket_dir == NULL) {
211                 goto fail_nomem;
212         }
213
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)) {
219                 TALLOC_FREE(ctx);
220                 return ENAMETOOLONG;
221         }
222
223         ret = messaging_dgm_lockfile_create(cache_dir, dir_owner, pid.pid,
224                                             &ctx->lockfile_fd, pid.unique_id);
225         if (ret != 0) {
226                 DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
227                           __func__, strerror(ret)));
228                 TALLOC_FREE(ctx);
229                 return ret;
230         }
231
232         ctx->msg_callbacks = poll_funcs_init_tevent(ctx);
233         if (ctx->msg_callbacks == NULL) {
234                 goto fail_nomem;
235         }
236
237         ctx->tevent_handle = poll_funcs_tevent_register(
238                 ctx, ctx->msg_callbacks, ev);
239         if (ctx->tevent_handle == NULL) {
240                 goto fail_nomem;
241         }
242
243         ok = directory_create_or_exist_strict(socket_dir, dir_owner, 0700);
244         if (!ok) {
245                 DEBUG(1, ("Could not create socket directory\n"));
246                 TALLOC_FREE(ctx);
247                 return EACCES;
248         }
249         TALLOC_FREE(socket_dir);
250
251         unlink(socket_address.sun_path);
252
253         generate_random_buffer((uint8_t *)&cookie, sizeof(cookie));
254
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(ctx);
260                 return ret;
261         }
262         talloc_set_destructor(ctx, messaging_dgm_context_destructor);
263
264         ctx->have_dgm_context = &have_dgm_context;
265
266         *pctx = ctx;
267         return 0;
268
269 fail_nomem:
270         TALLOC_FREE(ctx);
271         return ENOMEM;
272 }
273
274 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
275 {
276         /*
277          * First delete the socket to avoid races. The lockfile is the
278          * indicator that we're still around.
279          */
280         unix_msg_free(c->dgm_ctx);
281
282         if (getpid() == c->pid) {
283                 (void)messaging_dgm_lockfile_remove(c->cache_dir, c->pid);
284         }
285         close(c->lockfile_fd);
286
287         if (c->have_dgm_context != NULL) {
288                 *c->have_dgm_context = false;
289         }
290
291         return 0;
292 }
293
294 int messaging_dgm_send(struct messaging_dgm_context *ctx, pid_t pid,
295                        const struct iovec *iov, int iovlen)
296 {
297         struct sockaddr_un dst;
298         ssize_t dst_pathlen;
299         int ret;
300
301         dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
302
303         dst_pathlen = snprintf(dst.sun_path, sizeof(dst.sun_path),
304                                "%s/msg/%u", ctx->cache_dir, (unsigned)pid);
305         if (dst_pathlen >= sizeof(dst.sun_path)) {
306                 return ENAMETOOLONG;
307         }
308
309         DEBUG(10, ("%s: Sending message to %u\n", __func__, (unsigned)pid));
310
311         ret = unix_msg_send(ctx->dgm_ctx, &dst, iov, iovlen);
312
313         return ret;
314 }
315
316 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
317                                uint8_t *msg, size_t msg_len,
318                                void *private_data)
319 {
320         struct messaging_dgm_context *dgm_ctx = talloc_get_type_abort(
321                 private_data, struct messaging_dgm_context);
322
323         dgm_ctx->recv_cb(msg, msg_len, dgm_ctx->recv_cb_private_data);
324 }
325
326 int messaging_dgm_cleanup(struct messaging_dgm_context *ctx, pid_t pid)
327 {
328         struct sun_path_buf lockfile_name, socket_name;
329         int fd, ret;
330         struct flock lck = {};
331
332         ret = messaging_dgm_lockfile_name(&lockfile_name, ctx->cache_dir, pid);
333         if (ret != 0) {
334                 return ret;
335         }
336
337         /* same length as lockfile_name, can't overflow */
338         snprintf(socket_name.buf, sizeof(socket_name.buf), "%s/msg/%u",
339                  ctx->cache_dir, (unsigned)pid);
340
341         fd = open(lockfile_name.buf, O_NONBLOCK|O_WRONLY, 0);
342         if (fd == -1) {
343                 ret = errno;
344                 if (ret != ENOENT) {
345                         DEBUG(10, ("%s: open(%s) failed: %s\n", __func__,
346                                    lockfile_name.buf, strerror(ret)));
347                 }
348                 return ret;
349         }
350
351         lck.l_type = F_WRLCK;
352         lck.l_whence = SEEK_SET;
353         lck.l_start = 0;
354         lck.l_len = 0;
355
356         ret = fcntl(fd, F_SETLK, &lck);
357         if (ret != 0) {
358                 ret = errno;
359                 DEBUG(10, ("%s: Could not get lock: %s\n", __func__,
360                            strerror(ret)));
361                 close(fd);
362                 return ret;
363         }
364
365         (void)unlink(socket_name.buf);
366         (void)unlink(lockfile_name.buf);
367         (void)close(fd);
368         return 0;
369 }
370
371 int messaging_dgm_wipe(struct messaging_dgm_context *ctx)
372 {
373         struct sun_path_buf msgdir_name;
374         DIR *msgdir;
375         struct dirent *dp;
376         pid_t our_pid = getpid();
377         int ret;
378
379         /*
380          * We scan the socket directory and not the lock directory. Otherwise
381          * we would race against messaging_dgm_lockfile_create's open(O_CREAT)
382          * and fcntl(SETLK).
383          */
384
385         ret = snprintf(msgdir_name.buf, sizeof(msgdir_name.buf),
386                        "%s/msg", ctx->cache_dir);
387         if (ret >= sizeof(msgdir_name.buf)) {
388                 return ENAMETOOLONG;
389         }
390
391         msgdir = opendir(msgdir_name.buf);
392         if (msgdir == NULL) {
393                 ret = errno;
394                 return ret;
395         }
396
397         while ((dp = readdir(msgdir)) != NULL) {
398                 unsigned long pid;
399
400                 pid = strtoul(dp->d_name, NULL, 10);
401                 if (pid == 0) {
402                         /*
403                          * . and .. and other malformed entries
404                          */
405                         continue;
406                 }
407                 if (pid == our_pid) {
408                         /*
409                          * fcntl(F_GETLK) will succeed for ourselves, we hold
410                          * that lock ourselves.
411                          */
412                         continue;
413                 }
414
415                 ret = messaging_dgm_cleanup(ctx, pid);
416                 DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
417                            pid, ret ? strerror(ret) : "ok"));
418         }
419         closedir(msgdir);
420
421         return 0;
422 }
423
424 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
425                                             struct messaging_dgm_context *ctx,
426                                             struct tevent_context *ev)
427 {
428         return poll_funcs_tevent_register(mem_ctx, ctx->msg_callbacks, ev);
429 }