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