b64b2b0a752c11b0992b1dc94d7f3a620534c2a7
[mat/samba.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         struct sun_path_buf socket_dir;
43         struct sun_path_buf lockfile_dir;
44         int lockfile_fd;
45
46         void (*recv_cb)(const uint8_t *msg,
47                         size_t msg_len,
48                         int *fds,
49                         size_t num_fds,
50                         void *private_data);
51         void *recv_cb_private_data;
52
53         bool *have_dgm_context;
54 };
55
56 static struct messaging_dgm_context *global_dgm_context;
57
58 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
59                                uint8_t *msg, size_t msg_len,
60                                int *fds, size_t num_fds,
61                                void *private_data);
62
63 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c);
64
65 static int messaging_dgm_lockfile_create(struct messaging_dgm_context *ctx,
66                                          pid_t pid, int *plockfile_fd,
67                                          uint64_t unique)
68 {
69         fstring buf;
70         int lockfile_fd;
71         struct sun_path_buf lockfile_name;
72         struct flock lck;
73         int unique_len, ret;
74         ssize_t written;
75
76         ret = snprintf(lockfile_name.buf, sizeof(lockfile_name.buf),
77                        "%s/%u", ctx->lockfile_dir.buf, (int)pid);
78         if (ret >= sizeof(lockfile_name.buf)) {
79                 return ENAMETOOLONG;
80         }
81
82         /* no O_EXCL, existence check is via the fcntl lock */
83
84         lockfile_fd = open(lockfile_name.buf, O_NONBLOCK|O_CREAT|O_WRONLY,
85                            0644);
86         if (lockfile_fd == -1) {
87                 ret = errno;
88                 DEBUG(1, ("%s: open failed: %s\n", __func__, strerror(errno)));
89                 return ret;
90         }
91
92         lck = (struct flock) {
93                 .l_type = F_WRLCK,
94                 .l_whence = SEEK_SET
95         };
96
97         ret = fcntl(lockfile_fd, F_SETLK, &lck);
98         if (ret == -1) {
99                 ret = errno;
100                 DEBUG(1, ("%s: fcntl failed: %s\n", __func__, strerror(ret)));
101                 goto fail_close;
102         }
103
104         unique_len = snprintf(buf, sizeof(buf), "%ju\n", (uintmax_t)unique);
105
106         /* shorten a potentially preexisting file */
107
108         ret = ftruncate(lockfile_fd, unique_len);
109         if (ret == -1) {
110                 ret = errno;
111                 DEBUG(1, ("%s: ftruncate failed: %s\n", __func__,
112                           strerror(ret)));
113                 goto fail_unlink;
114         }
115
116         written = write(lockfile_fd, buf, unique_len);
117         if (written != unique_len) {
118                 ret = errno;
119                 DEBUG(1, ("%s: write failed: %s\n", __func__, strerror(ret)));
120                 goto fail_unlink;
121         }
122
123         *plockfile_fd = lockfile_fd;
124         return 0;
125
126 fail_unlink:
127         unlink(lockfile_name.buf);
128 fail_close:
129         close(lockfile_fd);
130         return ret;
131 }
132
133 int messaging_dgm_init(struct tevent_context *ev,
134                        uint64_t unique,
135                        const char *socket_dir,
136                        const char *lockfile_dir,
137                        void (*recv_cb)(const uint8_t *msg,
138                                        size_t msg_len,
139                                        int *fds,
140                                        size_t num_fds,
141                                        void *private_data),
142                        void *recv_cb_private_data)
143 {
144         struct messaging_dgm_context *ctx;
145         int ret;
146         struct sockaddr_un socket_address;
147         uint64_t cookie;
148         size_t len;
149         static bool have_dgm_context = false;
150
151         if (have_dgm_context) {
152                 return EEXIST;
153         }
154
155         ctx = talloc_zero(NULL, struct messaging_dgm_context);
156         if (ctx == NULL) {
157                 goto fail_nomem;
158         }
159         ctx->pid = getpid();
160         ctx->recv_cb = recv_cb;
161         ctx->recv_cb_private_data = recv_cb_private_data;
162
163         len = strlcpy(ctx->lockfile_dir.buf, lockfile_dir,
164                       sizeof(ctx->lockfile_dir.buf));
165         if (len >= sizeof(ctx->lockfile_dir.buf)) {
166                 TALLOC_FREE(ctx);
167                 return ENAMETOOLONG;
168         }
169
170         len = strlcpy(ctx->socket_dir.buf, socket_dir,
171                       sizeof(ctx->socket_dir.buf));
172         if (len >= sizeof(ctx->socket_dir.buf)) {
173                 TALLOC_FREE(ctx);
174                 return ENAMETOOLONG;
175         }
176
177         socket_address = (struct sockaddr_un) { .sun_family = AF_UNIX };
178         len = snprintf(socket_address.sun_path,
179                        sizeof(socket_address.sun_path),
180                        "%s/%u", socket_dir, (unsigned)ctx->pid);
181         if (len >= sizeof(socket_address.sun_path)) {
182                 TALLOC_FREE(ctx);
183                 return ENAMETOOLONG;
184         }
185
186         ret = messaging_dgm_lockfile_create(ctx, ctx->pid, &ctx->lockfile_fd,
187                                             unique);
188         if (ret != 0) {
189                 DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
190                           __func__, strerror(ret)));
191                 TALLOC_FREE(ctx);
192                 return ret;
193         }
194
195         ctx->msg_callbacks = poll_funcs_init_tevent(ctx);
196         if (ctx->msg_callbacks == NULL) {
197                 goto fail_nomem;
198         }
199
200         ctx->tevent_handle = poll_funcs_tevent_register(
201                 ctx, ctx->msg_callbacks, ev);
202         if (ctx->tevent_handle == NULL) {
203                 goto fail_nomem;
204         }
205
206         unlink(socket_address.sun_path);
207
208         generate_random_buffer((uint8_t *)&cookie, sizeof(cookie));
209
210         ret = unix_msg_init(&socket_address, ctx->msg_callbacks, 1024, cookie,
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 int messaging_dgm_cleanup(pid_t pid)
307 {
308         struct messaging_dgm_context *ctx = global_dgm_context;
309         struct sun_path_buf lockfile_name, socket_name;
310         int fd, len, ret;
311         struct flock lck = {};
312
313         if (ctx == NULL) {
314                 return ENOTCONN;
315         }
316
317         len = snprintf(socket_name.buf, sizeof(socket_name.buf), "%s/%u",
318                        ctx->socket_dir.buf, (unsigned)pid);
319         if (len >= sizeof(socket_name.buf)) {
320                 return ENAMETOOLONG;
321         }
322
323         len = snprintf(lockfile_name.buf, sizeof(lockfile_name.buf), "%s/%u",
324                        ctx->lockfile_dir.buf, (unsigned)pid);
325         if (len >= sizeof(lockfile_name.buf)) {
326                 return ENAMETOOLONG;
327         }
328
329         fd = open(lockfile_name.buf, O_NONBLOCK|O_WRONLY, 0);
330         if (fd == -1) {
331                 ret = errno;
332                 if (ret != ENOENT) {
333                         DEBUG(10, ("%s: open(%s) failed: %s\n", __func__,
334                                    lockfile_name.buf, strerror(ret)));
335                 }
336                 return ret;
337         }
338
339         lck.l_type = F_WRLCK;
340         lck.l_whence = SEEK_SET;
341         lck.l_start = 0;
342         lck.l_len = 0;
343
344         ret = fcntl(fd, F_SETLK, &lck);
345         if (ret != 0) {
346                 ret = errno;
347                 if ((ret != EACCES) && (ret != EAGAIN)) {
348                         DEBUG(10, ("%s: Could not get lock: %s\n", __func__,
349                                    strerror(ret)));
350                 }
351                 close(fd);
352                 return ret;
353         }
354
355         DEBUG(10, ("%s: Cleaning up : %s\n", __func__, strerror(ret)));
356
357         (void)unlink(socket_name.buf);
358         (void)unlink(lockfile_name.buf);
359         (void)close(fd);
360         return 0;
361 }
362
363 int messaging_dgm_wipe(void)
364 {
365         struct messaging_dgm_context *ctx = global_dgm_context;
366         DIR *msgdir;
367         struct dirent *dp;
368         pid_t our_pid = getpid();
369         int ret;
370
371         if (ctx == NULL) {
372                 return ENOTCONN;
373         }
374
375         /*
376          * We scan the socket directory and not the lock directory. Otherwise
377          * we would race against messaging_dgm_lockfile_create's open(O_CREAT)
378          * and fcntl(SETLK).
379          */
380
381         msgdir = opendir(ctx->socket_dir.buf);
382         if (msgdir == NULL) {
383                 return errno;
384         }
385
386         while ((dp = readdir(msgdir)) != NULL) {
387                 unsigned long pid;
388
389                 pid = strtoul(dp->d_name, NULL, 10);
390                 if (pid == 0) {
391                         /*
392                          * . and .. and other malformed entries
393                          */
394                         continue;
395                 }
396                 if (pid == our_pid) {
397                         /*
398                          * fcntl(F_GETLK) will succeed for ourselves, we hold
399                          * that lock ourselves.
400                          */
401                         continue;
402                 }
403
404                 ret = messaging_dgm_cleanup(pid);
405                 DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
406                            pid, ret ? strerror(ret) : "ok"));
407         }
408         closedir(msgdir);
409
410         return 0;
411 }
412
413 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
414                                             struct tevent_context *ev)
415 {
416         struct messaging_dgm_context *ctx = global_dgm_context;
417
418         if (ctx == NULL) {
419                 return NULL;
420         }
421         return poll_funcs_tevent_register(mem_ctx, ctx->msg_callbacks, ev);
422 }