4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26 /* All Rights Reserved */
29 * Portions of this source code were derived from Berkeley 4.3 BSD
30 * under license from the Regents of the University of California.
34 * This module provides the user level support for the NFSv4
35 * callback program. It is modeled after nfsd. When a nfsv4
36 * mount occurs, the mount command forks and the child runs
37 * start_nfs4_callback. If this is the first mount, then the
38 * process will hang around listening for incoming connection
39 * requests from the nfsv4 server.
41 * For connection-less protocols, the krpc is started immediately.
42 * For connection oriented protocols, the kernel module is informed
43 * of netid and universal address that it can give this
44 * information to the server during setclientid.
47 #include <sys/param.h>
48 #include <sys/types.h>
54 #include <sys/resource.h>
57 #include <nfs/nfssys.h>
60 #include <netconfig.h>
65 #include <sys/tihdr.h>
66 #include <netinet/tcp.h>
67 #include "nfs_tbind.h"
69 #include <rpcsvc/nfs4_prot.h>
73 #include <priv_utils.h>
74 #include <rpcsvc/daemon_utils.h>
76 static int nfs4svc(int, struct netbuf
*, struct netconfig
*, int,
78 extern int _nfssys(int, void *);
83 * The following are all globals used by routines in nfs_tbind.c.
85 size_t end_listen_fds
; /* used by conn_close_oldest() */
86 size_t num_fds
= 0; /* used by multiple routines */
87 int listen_backlog
= 32; /* used by bind_to_{provider,proto}() */
88 int num_servers
; /* used by cots_listen_event() */
89 int (*Mysvc
)(int, struct netbuf
, struct netconfig
*) = NULL
;
90 /* used by cots_listen_event() */
91 int max_conns_allowed
= -1; /* used by cots_listen_event() */
94 main(int argc
, char *argv
[])
98 struct protob
*protobp
;
101 struct svcpool_args cb_svcpool
;
108 * Close existing file descriptors, open "/dev/null" as
109 * standard input, output, and error, and detach from
110 * controlling terminal.
113 (void) open("/dev/null", O_RDONLY
);
114 (void) open("/dev/null", O_WRONLY
);
120 * create a child to continue our work
121 * Parent's exit will tell mount command we're ready to go
123 if ((pi
= fork()) > 0) {
128 (void) syslog(LOG_ERR
,
129 "Could not start NFS4_CALLBACK service");
133 (void) _create_daemon_lock(NFS4CBD
, DAEMON_UID
, DAEMON_GID
);
137 if (__init_daemon_priv(PU_RESETGROUPS
|PU_CLEARLIMITSET
,
138 DAEMON_UID
, DAEMON_GID
, PRIV_SYS_NFS
, (char *)NULL
) == -1) {
139 (void) fprintf(stderr
, "%s must be run with sufficient"
140 " privileges\n", argv
[0]);
143 /* Basic privileges we don't need, remove from E/P. */
144 __fini_daemon_priv(PRIV_PROC_EXEC
, PRIV_PROC_FORK
, PRIV_FILE_LINK_ANY
,
145 PRIV_PROC_SESSION
, PRIV_PROC_INFO
, (char *)NULL
);
148 * establish our lock on the lock file and write our pid to it.
149 * exit if some other process holds the lock, or if there's any
150 * error in writing/locking the file.
152 pid
= _enter_daemon_lock(NFS4CBD
);
157 syslog(LOG_ERR
, "error locking for %s: %s", NFS4CBD
,
161 /* daemon was already running */
165 openlog(MyName
, LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
167 cb_svcpool
.id
= NFS_CB_SVCPOOL_ID
;
168 cb_svcpool
.maxthreads
= 0;
169 cb_svcpool
.redline
= 0;
170 cb_svcpool
.qsize
= 0;
171 cb_svcpool
.timeout
= 0;
172 cb_svcpool
.stksize
= 0;
173 cb_svcpool
.max_same_xprt
= 0;
175 /* create a SVC_POOL for the nfsv4 callback deamon */
176 if (_nfssys(SVCPOOL_CREATE
, &cb_svcpool
)) {
177 (void) syslog(LOG_ERR
, "can't setup NFS_CB SVCPOOL: Exiting");
182 * Set up blocked thread to do LWP creation on behalf of the kernel.
184 if (svcwait(NFS_CB_SVCPOOL_ID
)) {
185 (void) syslog(LOG_ERR
,
186 "Can't set up NFS_CB LWP creator: Exiting");
192 * Build a protocol block list for registration.
194 protobp
= (struct protob
*)malloc(sizeof (struct protob
));
195 protobp
->serv
= "NFS4_CALLBACK";
196 protobp
->versmin
= NFS_CB
;
197 protobp
->versmax
= NFS_CB
;
198 protobp
->program
= NFS4_CALLBACK
;
199 protobp
->next
= NULL
;
201 if (do_all(protobp
, NULL
) == -1) {
208 (void) syslog(LOG_ERR
,
209 "Could not start NFS4_CALLBACK service for any protocol");
213 end_listen_fds
= num_fds
;
215 * Poll for non-data control events on the transport descriptors.
220 * If we get here, something failed in poll_for_action().
226 get_uaddr(int fd
, struct netconfig
*nconf
, struct netbuf
*nb
)
228 struct nfs_svc_args nsa
;
229 char *ua
, *ua2
, *mua
= NULL
;
230 char me
[MAXHOSTNAMELEN
];
231 struct nd_addrlist
*nas
;
232 struct nd_hostserv hs
;
233 struct nd_mergearg ma
;
235 ua
= taddr2uaddr(nconf
, nb
);
239 fprintf(stderr
, "taddr2uaddr failed for netid %s\n",
245 gethostname(me
, MAXHOSTNAMELEN
);
249 if (netdir_getbyname(nconf
, &hs
, &nas
)) {
251 netdir_perror("netdir_getbyname");
256 ua2
= taddr2uaddr(nconf
, nas
->n_addrs
);
260 fprintf(stderr
, "taddr2uaddr failed for netid %s.\n",
270 if (netdir_options(nconf
, ND_MERGEADDR
, 0, (char *)&ma
)) {
272 netdir_perror("netdir_options");
282 * Establish NFS4 callback service thread.
285 nfs4svc(int fd
, struct netbuf
*addrmask
, struct netconfig
*nconf
,
286 int cmd
, struct netbuf
*addr
)
288 struct nfs4_svc_args nsa
;
292 ua
= get_uaddr(fd
, nconf
, addr
);
295 syslog(LOG_NOTICE
, "nfsv4 cannot determine local hostname "
296 "binding for transport %s - delegations will not be "
297 "available on this transport\n", nconf
->nc_netid
);
302 if (cmd
& NFS4_KRPC_START
)
303 fprintf(stderr
, "nfs4cbd: starting callback rpc on %s %s\n",
304 nconf
->nc_netid
, ua
);
306 fprintf(stderr
, "nfs4cbd: listening on %s %s\n",
307 nconf
->nc_netid
, ua
);
312 nsa
.netid
= nconf
->nc_netid
;
314 nsa
.addrmask
= *addrmask
;
316 bzero(&nsa
.addrmask
, sizeof (struct netbuf
));
318 nsa
.protofmly
= nconf
->nc_protofmly
;
319 nsa
.proto
= nconf
->nc_proto
;
320 if ((error
= _nfssys(NFS4_SVC
, &nsa
)) != 0)
321 syslog(LOG_ERR
, "nfssys NFS4_SVC failed\n");