]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsdcld/nfsdcld.c
nfsdcld: make it watch for inotify events in the containing directory
[nfs-utils.git] / utils / nfsdcld / nfsdcld.c
1 /*
2  * nfsdcld.c -- NFSv4 client name tracking daemon
3  *
4  * Copyright (C) 2011  Red Hat, Jeff Layton <jlayton@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif /* HAVE_CONFIG_H */
25
26 #include <errno.h>
27 #include <event.h>
28 #include <stdbool.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <sys/inotify.h>
37
38 #include "xlog.h"
39 #include "nfslib.h"
40 #include "cld.h"
41
42 #ifndef PIPEFS_DIR
43 #define PIPEFS_DIR NFS_STATEDIR "/rpc_pipefs"
44 #endif
45
46 #define DEFAULT_CLD_PATH        PIPEFS_DIR "/nfsd/cld"
47
48 #define UPCALL_VERSION          1
49
50 /* private data structures */
51 struct cld_client {
52         int                     cl_fd;
53         struct event            cl_event;
54         struct cld_msg  cl_msg;
55 };
56
57 /* global variables */
58 static char *pipepath = DEFAULT_CLD_PATH;
59 static int              inotify_fd = -1;
60 static struct event     pipedir_event;
61
62 static struct option longopts[] =
63 {
64         { "help", 0, NULL, 'h' },
65         { "foreground", 0, NULL, 'F' },
66         { "debug", 0, NULL, 'd' },
67         { "pipe", 1, NULL, 'p' },
68         { "storagedir", 1, NULL, 's' },
69         { NULL, 0, 0, 0 },
70 };
71
72 /* forward declarations */
73 static void cldcb(int UNUSED(fd), short which, void *data);
74
75 static void
76 usage(char *progname)
77 {
78         printf("%s [ -hFd ] [ -p pipe ] [ -s dir ]\n", progname);
79 }
80
81 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX)
82
83 static int
84 cld_pipe_open(struct cld_client *clnt)
85 {
86         int fd;
87
88         xlog(D_GENERAL, "%s: opening upcall pipe %s", __func__, pipepath);
89         fd = open(pipepath, O_RDWR, 0);
90         if (fd < 0) {
91                 xlog(L_ERROR, "%s: open of %s failed: %m", __func__, pipepath);
92                 return -errno;
93         }
94
95         if (clnt->cl_event.ev_flags & EVLIST_INIT)
96                 event_del(&clnt->cl_event);
97         if (clnt->cl_fd >= 0)
98                 close(clnt->cl_fd);
99
100         clnt->cl_fd = fd;
101         event_set(&clnt->cl_event, clnt->cl_fd, EV_READ, cldcb, clnt);
102         /* event_add is done by the caller */
103         return 0;
104 }
105
106 static void
107 cld_inotify_cb(int UNUSED(fd), short which, void *data)
108 {
109         int ret;
110         size_t elen;
111         ssize_t rret;
112         char evbuf[INOTIFY_EVENT_MAX];
113         char *dirc = NULL, *pname;
114         struct inotify_event *event = (struct inotify_event *)evbuf;
115         struct cld_client *clnt = data;
116
117         if (which != EV_READ)
118                 return;
119
120         xlog(D_GENERAL, "%s: called for EV_READ", __func__);
121
122         dirc = strndup(pipepath, PATH_MAX);
123         if (!dirc) {
124                 xlog(L_ERROR, "%s: unable to allocate memory", __func__);
125                 goto out;
126         }
127
128         rret = read(inotify_fd, evbuf, INOTIFY_EVENT_MAX);
129         if (rret < 0) {
130                 xlog(L_ERROR, "%s: read from inotify fd failed: %m", __func__);
131                 goto out;
132         }
133
134         /* check to see if we have a filename in the evbuf */
135         if (!event->len) {
136                 xlog(D_GENERAL, "%s: no filename in inotify event", __func__);
137                 goto out;
138         }
139
140         pname = basename(dirc);
141         elen = strnlen(event->name, event->len);
142
143         /* does the filename match our pipe? */
144         if (strlen(pname) != elen || memcmp(pname, event->name, elen)) {
145                 xlog(D_GENERAL, "%s: wrong filename (%s)", __func__,
146                                 event->name);
147                 goto out;
148         }
149
150         ret = cld_pipe_open(clnt);
151         switch (ret) {
152         case 0:
153                 /* readd the event for the cl_event pipe */
154                 event_add(&clnt->cl_event, NULL);
155                 break;
156         case -ENOENT:
157                 /* pipe must have disappeared, wait for it to come back */
158                 goto out;
159         default:
160                 /* anything else is fatal */
161                 xlog(L_FATAL, "%s: unable to open new pipe (%d). Aborting.",
162                         ret, __func__);
163                 exit(ret);
164         }
165
166 out:
167         event_add(&pipedir_event, NULL);
168         free(dirc);
169 }
170
171 static int
172 cld_inotify_setup(void)
173 {
174         int ret;
175         char *dirc, *dname;
176
177         dirc = strndup(pipepath, PATH_MAX);
178         if (!dirc) {
179                 xlog_err("%s: unable to allocate memory", __func__);
180                 ret = -ENOMEM;
181                 goto out_free;
182         }
183
184         dname = dirname(dirc);
185
186         inotify_fd = inotify_init();
187         if (inotify_fd < 0) {
188                 xlog_err("%s: inotify_init failed: %m", __func__);
189                 ret = -errno;
190                 goto out_free;
191         }
192
193         ret = inotify_add_watch(inotify_fd, dname, IN_CREATE);
194         if (ret < 0) {
195                 xlog_err("%s: inotify_add_watch failed: %m", __func__);
196                 ret = -errno;
197                 goto out_err;
198         }
199
200 out_free:
201         free(dirc);
202         return 0;
203 out_err:
204         close(inotify_fd);
205         goto out_free;
206 }
207
208 /*
209  * Set an inotify watch on the directory that should contain the pipe, and then
210  * try to open it. If it fails with anything but -ENOENT, return the error
211  * immediately.
212  *
213  * If it succeeds, then set up the pipe event handler. At that point, set up
214  * the inotify event handler and go ahead and return success.
215  */
216 static int
217 cld_pipe_init(struct cld_client *clnt)
218 {
219         int ret;
220
221         xlog(D_GENERAL, "%s: init pipe handlers", __func__);
222
223         ret = cld_inotify_setup();
224         if (ret != 0)
225                 goto out;
226
227         clnt->cl_fd = -1;
228         ret = cld_pipe_open(clnt);
229         switch (ret) {
230         case 0:
231                 /* add the event and we're good to go */
232                 event_add(&clnt->cl_event, NULL);
233                 break;
234         case -ENOENT:
235                 /* ignore this error -- cld_inotify_cb will handle it */
236                 ret = 0;
237                 break;
238         default:
239                 /* anything else is fatal */
240                 close(inotify_fd);
241                 goto out;
242         }
243
244         /* set event for inotify read */
245         event_set(&pipedir_event, inotify_fd, EV_READ, cld_inotify_cb, clnt);
246         event_add(&pipedir_event, NULL);
247 out:
248         return ret;
249 }
250
251 static void
252 cld_not_implemented(struct cld_client *clnt)
253 {
254         int ret;
255         ssize_t bsize, wsize;
256         struct cld_msg *cmsg = &clnt->cl_msg;
257
258         xlog(D_GENERAL, "%s: downcalling with not implemented error", __func__);
259
260         /* set up reply */
261         cmsg->cm_status = -EOPNOTSUPP;
262
263         bsize = sizeof(*cmsg);
264
265         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
266         if (wsize != bsize)
267                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
268                          __func__, wsize);
269
270         /* reopen pipe, just to be sure */
271         ret = cld_pipe_open(clnt);
272         if (ret) {
273                 xlog(L_FATAL, "%s: unable to reopen pipe: %d", __func__, ret);
274                 exit(ret);
275         }
276 }
277
278 static void
279 cld_create(struct cld_client *clnt)
280 {
281         int ret;
282         ssize_t bsize, wsize;
283         struct cld_msg *cmsg = &clnt->cl_msg;
284
285         xlog(D_GENERAL, "%s: create client record.", __func__);
286
287         ret = sqlite_insert_client(cmsg->cm_u.cm_name.cn_id,
288                                    cmsg->cm_u.cm_name.cn_len);
289
290         cmsg->cm_status = ret ? -EREMOTEIO : ret;
291
292         bsize = sizeof(*cmsg);
293
294         xlog(D_GENERAL, "Doing downcall with status %d", cmsg->cm_status);
295         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
296         if (wsize != bsize) {
297                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
298                          __func__, wsize);
299                 ret = cld_pipe_open(clnt);
300                 if (ret) {
301                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
302                                         __func__, ret);
303                         exit(ret);
304                 }
305         }
306 }
307
308 static void
309 cld_remove(struct cld_client *clnt)
310 {
311         int ret;
312         ssize_t bsize, wsize;
313         struct cld_msg *cmsg = &clnt->cl_msg;
314
315         xlog(D_GENERAL, "%s: remove client record.", __func__);
316
317         ret = sqlite_remove_client(cmsg->cm_u.cm_name.cn_id,
318                                    cmsg->cm_u.cm_name.cn_len);
319
320         cmsg->cm_status = ret ? -EREMOTEIO : ret;
321
322         bsize = sizeof(*cmsg);
323
324         xlog(D_GENERAL, "%s: downcall with status %d", __func__,
325                         cmsg->cm_status);
326         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
327         if (wsize != bsize) {
328                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
329                          __func__, wsize);
330                 ret = cld_pipe_open(clnt);
331                 if (ret) {
332                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
333                                         __func__, ret);
334                         exit(ret);
335                 }
336         }
337 }
338
339 static void
340 cld_check(struct cld_client *clnt)
341 {
342         int ret;
343         ssize_t bsize, wsize;
344         struct cld_msg *cmsg = &clnt->cl_msg;
345
346         xlog(D_GENERAL, "%s: check client record", __func__);
347
348         ret = sqlite_check_client(cmsg->cm_u.cm_name.cn_id,
349                                   cmsg->cm_u.cm_name.cn_len);
350
351         /* set up reply */
352         cmsg->cm_status = ret ? -EACCES : ret;
353
354         bsize = sizeof(*cmsg);
355
356         xlog(D_GENERAL, "%s: downcall with status %d", __func__,
357                         cmsg->cm_status);
358         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
359         if (wsize != bsize) {
360                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
361                          __func__, wsize);
362                 ret = cld_pipe_open(clnt);
363                 if (ret) {
364                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
365                                         __func__, ret);
366                         exit(ret);
367                 }
368         }
369 }
370
371 static void
372 cld_gracedone(struct cld_client *clnt)
373 {
374         int ret;
375         ssize_t bsize, wsize;
376         struct cld_msg *cmsg = &clnt->cl_msg;
377
378         xlog(D_GENERAL, "%s: grace done. cm_gracetime=%ld", __func__,
379                         cmsg->cm_u.cm_gracetime);
380
381         ret = sqlite_remove_unreclaimed(cmsg->cm_u.cm_gracetime);
382
383         /* set up reply: downcall with 0 status */
384         cmsg->cm_status = ret ? -EREMOTEIO : ret;
385
386         bsize = sizeof(*cmsg);
387
388         xlog(D_GENERAL, "Doing downcall with status %d", cmsg->cm_status);
389         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
390         if (wsize != bsize) {
391                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
392                          __func__, wsize);
393                 ret = cld_pipe_open(clnt);
394                 if (ret) {
395                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
396                                         __func__, ret);
397                         exit(ret);
398                 }
399         }
400 }
401
402 static void
403 cldcb(int UNUSED(fd), short which, void *data)
404 {
405         ssize_t len;
406         struct cld_client *clnt = data;
407         struct cld_msg *cmsg = &clnt->cl_msg;
408
409         if (which != EV_READ)
410                 goto out;
411
412         len = atomicio(read, clnt->cl_fd, cmsg, sizeof(*cmsg));
413         if (len <= 0) {
414                 xlog(L_ERROR, "%s: pipe read failed: %m", __func__);
415                 cld_pipe_open(clnt);
416                 goto out;
417         }
418
419         if (cmsg->cm_vers != UPCALL_VERSION) {
420                 xlog(L_ERROR, "%s: unsupported upcall version: %hu",
421                                 cmsg->cm_vers);
422                 cld_pipe_open(clnt);
423                 goto out;
424         }
425
426         switch(cmsg->cm_cmd) {
427         case Cld_Create:
428                 cld_create(clnt);
429                 break;
430         case Cld_Remove:
431                 cld_remove(clnt);
432                 break;
433         case Cld_Check:
434                 cld_check(clnt);
435                 break;
436         case Cld_GraceDone:
437                 cld_gracedone(clnt);
438                 break;
439         default:
440                 xlog(L_WARNING, "%s: command %u is not yet implemented",
441                                 __func__, cmsg->cm_cmd);
442                 cld_not_implemented(clnt);
443         }
444 out:
445         event_add(&clnt->cl_event, NULL);
446 }
447
448 int
449 main(int argc, char **argv)
450 {
451         char arg;
452         int rc = 0;
453         bool foreground = false;
454         char *progname;
455         char *storagedir = NULL;
456         struct cld_client clnt;
457
458         memset(&clnt, 0, sizeof(clnt));
459
460         progname = strdup(basename(argv[0]));
461         if (!progname) {
462                 fprintf(stderr, "%s: unable to allocate memory.\n", argv[0]);
463                 return 1;
464         }
465
466         event_init();
467         xlog_syslog(0);
468         xlog_stderr(1);
469
470         /* process command-line options */
471         while ((arg = getopt_long(argc, argv, "hdFp:s:", longopts,
472                                   NULL)) != EOF) {
473                 switch (arg) {
474                 case 'd':
475                         xlog_config(D_ALL, 1);
476                         break;
477                 case 'F':
478                         foreground = true;
479                         break;
480                 case 'p':
481                         pipepath = optarg;
482                         break;
483                 case 's':
484                         storagedir = optarg;
485                         break;
486                 default:
487                         usage(progname);
488                         return 0;
489                 }
490         }
491
492
493         xlog_open(progname);
494         if (!foreground) {
495                 xlog_syslog(1);
496                 xlog_stderr(0);
497                 rc = daemon(0, 0);
498                 if (rc) {
499                         xlog(L_ERROR, "Unable to daemonize: %m");
500                         goto out;
501                 }
502         }
503
504         /* set up storage db */
505         rc = sqlite_maindb_init(storagedir);
506         if (rc) {
507                 xlog(L_ERROR, "Failed to open main database: %d", rc);
508                 goto out;
509         }
510
511         /* set up event handler */
512         rc = cld_pipe_init(&clnt);
513         if (rc)
514                 goto out;
515
516         xlog(D_GENERAL, "%s: Starting event dispatch handler.", __func__);
517         rc = event_dispatch();
518         if (rc < 0)
519                 xlog(L_ERROR, "%s: event_dispatch failed: %m", __func__);
520
521         close(clnt.cl_fd);
522         close(inotify_fd);
523 out:
524         free(progname);
525         return rc;
526 }