]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsdcld/nfsdcld.c
2d118681abbb5f11809898a072cbea1a5d944bf1
[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
36 #include "xlog.h"
37 #include "nfslib.h"
38 #include "cld.h"
39
40 #ifndef PIPEFS_DIR
41 #define PIPEFS_DIR NFS_STATEDIR "/rpc_pipefs"
42 #endif
43
44 #define DEFAULT_CLD_PATH        PIPEFS_DIR "/nfsd/cld"
45
46 #define UPCALL_VERSION          1
47
48 /* private data structures */
49 struct cld_client {
50         int                     cl_fd;
51         struct event            cl_event;
52         struct cld_msg  cl_msg;
53 };
54
55 /* global variables */
56 static char *pipepath = DEFAULT_CLD_PATH;
57
58 static struct option longopts[] =
59 {
60         { "help", 0, NULL, 'h' },
61         { "foreground", 0, NULL, 'F' },
62         { "debug", 0, NULL, 'd' },
63         { "pipe", 1, NULL, 'p' },
64         { "storagedir", 1, NULL, 's' },
65         { NULL, 0, 0, 0 },
66 };
67
68 /* forward declarations */
69 static void cldcb(int UNUSED(fd), short which, void *data);
70
71 static void
72 usage(char *progname)
73 {
74         printf("%s [ -hFd ] [ -p pipe ] [ -s dir ]\n", progname);
75 }
76
77 static int
78 cld_pipe_open(struct cld_client *clnt)
79 {
80         int fd;
81
82         xlog(D_GENERAL, "%s: opening upcall pipe %s", __func__, pipepath);
83         fd = open(pipepath, O_RDWR, 0);
84         if (fd < 0) {
85                 xlog(L_ERROR, "%s: open of %s failed: %m", __func__, pipepath);
86                 return -errno;
87         }
88
89         if (clnt->cl_event.ev_flags & EVLIST_INIT)
90                 event_del(&clnt->cl_event);
91         if (clnt->cl_fd >= 0)
92                 close(clnt->cl_fd);
93
94         clnt->cl_fd = fd;
95         event_set(&clnt->cl_event, clnt->cl_fd, EV_READ, cldcb, clnt);
96         /* event_add is done by the caller */
97         return 0;
98 }
99
100 static int
101 cld_pipe_init(struct cld_client *clnt)
102 {
103         int ret;
104
105         clnt->cl_fd = -1;
106         ret = cld_pipe_open(clnt);
107         if (ret)
108                 return ret;
109
110         event_add(&clnt->cl_event, NULL);
111         return 0;
112 }
113
114 static void
115 cld_not_implemented(struct cld_client *clnt)
116 {
117         int ret;
118         ssize_t bsize, wsize;
119         struct cld_msg *cmsg = &clnt->cl_msg;
120
121         xlog(D_GENERAL, "%s: downcalling with not implemented error", __func__);
122
123         /* set up reply */
124         cmsg->cm_status = -EOPNOTSUPP;
125
126         bsize = sizeof(*cmsg);
127
128         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
129         if (wsize != bsize)
130                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
131                          __func__, wsize);
132
133         /* reopen pipe, just to be sure */
134         ret = cld_pipe_open(clnt);
135         if (ret) {
136                 xlog(L_FATAL, "%s: unable to reopen pipe: %d", __func__, ret);
137                 exit(ret);
138         }
139 }
140
141 static void
142 cld_create(struct cld_client *clnt)
143 {
144         int ret;
145         ssize_t bsize, wsize;
146         struct cld_msg *cmsg = &clnt->cl_msg;
147
148         xlog(D_GENERAL, "%s: create client record.", __func__);
149
150         ret = sqlite_insert_client(cmsg->cm_u.cm_name.cn_id,
151                                    cmsg->cm_u.cm_name.cn_len);
152
153         cmsg->cm_status = ret ? -EREMOTEIO : ret;
154
155         bsize = sizeof(*cmsg);
156
157         xlog(D_GENERAL, "Doing downcall with status %d", cmsg->cm_status);
158         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
159         if (wsize != bsize) {
160                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
161                          __func__, wsize);
162                 ret = cld_pipe_open(clnt);
163                 if (ret) {
164                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
165                                         __func__, ret);
166                         exit(ret);
167                 }
168         }
169 }
170
171 static void
172 cld_remove(struct cld_client *clnt)
173 {
174         int ret;
175         ssize_t bsize, wsize;
176         struct cld_msg *cmsg = &clnt->cl_msg;
177
178         xlog(D_GENERAL, "%s: remove client record.", __func__);
179
180         ret = sqlite_remove_client(cmsg->cm_u.cm_name.cn_id,
181                                    cmsg->cm_u.cm_name.cn_len);
182
183         cmsg->cm_status = ret ? -EREMOTEIO : ret;
184
185         bsize = sizeof(*cmsg);
186
187         xlog(D_GENERAL, "%s: downcall with status %d", __func__,
188                         cmsg->cm_status);
189         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
190         if (wsize != bsize) {
191                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
192                          __func__, wsize);
193                 ret = cld_pipe_open(clnt);
194                 if (ret) {
195                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
196                                         __func__, ret);
197                         exit(ret);
198                 }
199         }
200 }
201
202 static void
203 cld_check(struct cld_client *clnt)
204 {
205         int ret;
206         ssize_t bsize, wsize;
207         struct cld_msg *cmsg = &clnt->cl_msg;
208
209         xlog(D_GENERAL, "%s: check client record", __func__);
210
211         ret = sqlite_check_client(cmsg->cm_u.cm_name.cn_id,
212                                   cmsg->cm_u.cm_name.cn_len);
213
214         /* set up reply */
215         cmsg->cm_status = ret ? -EACCES : ret;
216
217         bsize = sizeof(*cmsg);
218
219         xlog(D_GENERAL, "%s: downcall with status %d", __func__,
220                         cmsg->cm_status);
221         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
222         if (wsize != bsize) {
223                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
224                          __func__, wsize);
225                 ret = cld_pipe_open(clnt);
226                 if (ret) {
227                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
228                                         __func__, ret);
229                         exit(ret);
230                 }
231         }
232 }
233
234 static void
235 cld_gracedone(struct cld_client *clnt)
236 {
237         int ret;
238         ssize_t bsize, wsize;
239         struct cld_msg *cmsg = &clnt->cl_msg;
240
241         xlog(D_GENERAL, "%s: grace done. cm_gracetime=%ld", __func__,
242                         cmsg->cm_u.cm_gracetime);
243
244         ret = sqlite_remove_unreclaimed(cmsg->cm_u.cm_gracetime);
245
246         /* set up reply: downcall with 0 status */
247         cmsg->cm_status = ret ? -EREMOTEIO : ret;
248
249         bsize = sizeof(*cmsg);
250
251         xlog(D_GENERAL, "Doing downcall with status %d", cmsg->cm_status);
252         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
253         if (wsize != bsize) {
254                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
255                          __func__, wsize);
256                 ret = cld_pipe_open(clnt);
257                 if (ret) {
258                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
259                                         __func__, ret);
260                         exit(ret);
261                 }
262         }
263 }
264
265 static void
266 cldcb(int UNUSED(fd), short which, void *data)
267 {
268         ssize_t len;
269         struct cld_client *clnt = data;
270         struct cld_msg *cmsg = &clnt->cl_msg;
271
272         if (which != EV_READ)
273                 goto out;
274
275         len = atomicio(read, clnt->cl_fd, cmsg, sizeof(*cmsg));
276         if (len <= 0) {
277                 xlog(L_ERROR, "%s: pipe read failed: %m", __func__);
278                 cld_pipe_open(clnt);
279                 goto out;
280         }
281
282         if (cmsg->cm_vers != UPCALL_VERSION) {
283                 xlog(L_ERROR, "%s: unsupported upcall version: %hu",
284                                 cmsg->cm_vers);
285                 cld_pipe_open(clnt);
286                 goto out;
287         }
288
289         switch(cmsg->cm_cmd) {
290         case Cld_Create:
291                 cld_create(clnt);
292                 break;
293         case Cld_Remove:
294                 cld_remove(clnt);
295                 break;
296         case Cld_Check:
297                 cld_check(clnt);
298                 break;
299         case Cld_GraceDone:
300                 cld_gracedone(clnt);
301                 break;
302         default:
303                 xlog(L_WARNING, "%s: command %u is not yet implemented",
304                                 __func__, cmsg->cm_cmd);
305                 cld_not_implemented(clnt);
306         }
307 out:
308         event_add(&clnt->cl_event, NULL);
309 }
310
311 int
312 main(int argc, char **argv)
313 {
314         char arg;
315         int rc = 0;
316         bool foreground = false;
317         char *progname;
318         char *storagedir = NULL;
319         struct cld_client clnt;
320
321         memset(&clnt, 0, sizeof(clnt));
322
323         progname = strdup(basename(argv[0]));
324         if (!progname) {
325                 fprintf(stderr, "%s: unable to allocate memory.\n", argv[0]);
326                 return 1;
327         }
328
329         event_init();
330         xlog_syslog(0);
331         xlog_stderr(1);
332
333         /* process command-line options */
334         while ((arg = getopt_long(argc, argv, "hdFp:s:", longopts,
335                                   NULL)) != EOF) {
336                 switch (arg) {
337                 case 'd':
338                         xlog_config(D_ALL, 1);
339                         break;
340                 case 'F':
341                         foreground = true;
342                         break;
343                 case 'p':
344                         pipepath = optarg;
345                         break;
346                 case 's':
347                         storagedir = optarg;
348                         break;
349                 default:
350                         usage(progname);
351                         return 0;
352                 }
353         }
354
355
356         xlog_open(progname);
357         if (!foreground) {
358                 xlog_syslog(1);
359                 xlog_stderr(0);
360                 rc = daemon(0, 0);
361                 if (rc) {
362                         xlog(L_ERROR, "Unable to daemonize: %m");
363                         goto out;
364                 }
365         }
366
367         /* set up storage db */
368         rc = sqlite_maindb_init(storagedir);
369         if (rc) {
370                 xlog(L_ERROR, "Failed to open main database: %d", rc);
371                 goto out;
372         }
373
374         /* set up event handler */
375         rc = cld_pipe_init(&clnt);
376         if (rc)
377                 goto out;
378
379         xlog(D_GENERAL, "%s: Starting event dispatch handler.", __func__);
380         rc = event_dispatch();
381         if (rc < 0)
382                 xlog(L_ERROR, "%s: event_dispatch failed: %m", __func__);
383
384         close(clnt.cl_fd);
385 out:
386         free(progname);
387         return rc;
388 }