]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsdcld/nfsdcld.c
nfsdcld: add client tracking daemon stub
[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         { NULL, 0, 0, 0 },
65 };
66
67 /* forward declarations */
68 static void cldcb(int UNUSED(fd), short which, void *data);
69
70 static void
71 usage(char *progname)
72 {
73         printf("Usage: %s [ -hFd ] [ -p pipe ]\n", progname);
74 }
75
76 static int
77 cld_pipe_open(struct cld_client *clnt)
78 {
79         int fd;
80
81         xlog(D_GENERAL, "%s: opening upcall pipe %s", __func__, pipepath);
82         fd = open(pipepath, O_RDWR, 0);
83         if (fd < 0) {
84                 xlog(L_ERROR, "%s: open of %s failed: %m", __func__, pipepath);
85                 return -errno;
86         }
87
88         if (clnt->cl_event.ev_flags & EVLIST_INIT)
89                 event_del(&clnt->cl_event);
90         if (clnt->cl_fd >= 0)
91                 close(clnt->cl_fd);
92
93         clnt->cl_fd = fd;
94         event_set(&clnt->cl_event, clnt->cl_fd, EV_READ, cldcb, clnt);
95         /* event_add is done by the caller */
96         return 0;
97 }
98
99 static int
100 cld_pipe_init(struct cld_client *clnt)
101 {
102         int ret;
103
104         clnt->cl_fd = -1;
105         ret = cld_pipe_open(clnt);
106         if (ret)
107                 return ret;
108
109         event_add(&clnt->cl_event, NULL);
110         return 0;
111 }
112
113 static void
114 cld_not_implemented(struct cld_client *clnt)
115 {
116         int ret;
117         ssize_t bsize, wsize;
118         struct cld_msg *cmsg = &clnt->cl_msg;
119
120         xlog(D_GENERAL, "%s: downcalling with not implemented error", __func__);
121
122         /* set up reply */
123         cmsg->cm_status = -EOPNOTSUPP;
124
125         bsize = sizeof(*cmsg);
126
127         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
128         if (wsize != bsize)
129                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
130                          __func__, wsize);
131
132         /* reopen pipe, just to be sure */
133         ret = cld_pipe_open(clnt);
134         if (ret) {
135                 xlog(L_FATAL, "%s: unable to reopen pipe: %d", __func__, ret);
136                 exit(ret);
137         }
138 }
139
140 static void
141 cld_create(struct cld_client *clnt)
142 {
143         int ret;
144         ssize_t bsize, wsize;
145         struct cld_msg *cmsg = &clnt->cl_msg;
146
147         xlog(D_GENERAL, "%s: create client record", __func__);
148
149         /* FIXME: create client record on storage here */
150
151         /* set up reply */
152         cmsg->cm_status = 0;
153
154         bsize = sizeof(*cmsg);
155
156         wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
157         if (wsize != bsize) {
158                 xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
159                          __func__, wsize);
160                 ret = cld_pipe_open(clnt);
161                 if (ret) {
162                         xlog(L_FATAL, "%s: unable to reopen pipe: %d",
163                                         __func__, ret);
164                         exit(ret);
165                 }
166         }
167 }
168
169 static void
170 cldcb(int UNUSED(fd), short which, void *data)
171 {
172         ssize_t len;
173         struct cld_client *clnt = data;
174         struct cld_msg *cmsg = &clnt->cl_msg;
175
176         if (which != EV_READ)
177                 goto out;
178
179         len = atomicio(read, clnt->cl_fd, cmsg, sizeof(*cmsg));
180         if (len <= 0) {
181                 xlog(L_ERROR, "%s: pipe read failed: %m", __func__);
182                 cld_pipe_open(clnt);
183                 goto out;
184         }
185
186         if (cmsg->cm_vers != UPCALL_VERSION) {
187                 xlog(L_ERROR, "%s: unsupported upcall version: %hu",
188                                 cmsg->cm_vers);
189                 cld_pipe_open(clnt);
190                 goto out;
191         }
192
193         switch(cmsg->cm_cmd) {
194         case Cld_Create:
195                 cld_create(clnt);
196                 break;
197         default:
198                 xlog(L_WARNING, "%s: command %u is not yet implemented",
199                                 __func__, cmsg->cm_cmd);
200                 cld_not_implemented(clnt);
201         }
202 out:
203         event_add(&clnt->cl_event, NULL);
204 }
205
206 int
207 main(int argc, char **argv)
208 {
209         char arg;
210         int rc = 0;
211         bool foreground = false;
212         char *progname;
213         struct cld_client clnt;
214
215         memset(&clnt, 0, sizeof(clnt));
216
217         progname = strdup(basename(argv[0]));
218         if (!progname) {
219                 fprintf(stderr, "%s: unable to allocate memory.\n", argv[0]);
220                 return 1;
221         }
222
223         event_init();
224         xlog_syslog(0);
225         xlog_stderr(1);
226
227         /* process command-line options */
228         while ((arg = getopt_long(argc, argv, "hdFp:", longopts,
229                                   NULL)) != EOF) {
230                 switch (arg) {
231                 case 'd':
232                         xlog_config(D_ALL, 1);
233                         break;
234                 case 'F':
235                         foreground = true;
236                         break;
237                 case 'p':
238                         pipepath = optarg;
239                         break;
240                 default:
241                         usage(progname);
242                         return 0;
243                 }
244         }
245
246
247         xlog_open(progname);
248         if (!foreground) {
249                 xlog_syslog(1);
250                 xlog_stderr(0);
251                 rc = daemon(0, 0);
252                 if (rc) {
253                         xlog(L_ERROR, "Unable to daemonize: %m");
254                         goto out;
255                 }
256         }
257
258         /* set up storage db */
259
260         /* set up event handler */
261         rc = cld_pipe_init(&clnt);
262         if (rc)
263                 goto out;
264
265         xlog(D_GENERAL, "%s: Starting event dispatch handler.", __func__);
266         rc = event_dispatch();
267         if (rc < 0)
268                 xlog(L_ERROR, "%s: event_dispatch failed: %m", __func__);
269
270         close(clnt.cl_fd);
271 out:
272         free(progname);
273         return rc;
274 }