#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <tiuser.h>
#include <rpc/rpc.h>
#include <errno.h>
#include <thread.h>
#include <sys/time.h>
#include <sys/file.h>
#include <nfs/nfs.h>
#include <nfs/nfssys.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <signal.h>
#include <netconfig.h>
#include <netdir.h>
#include <string.h>
#include <unistd.h>
#include <stropts.h>
#include <sys/tihdr.h>
#include <poll.h>
#include <priv_utils.h>
#include <sys/tiuser.h>
#include <netinet/tcp.h>
#include <deflt.h>
#include <rpcsvc/daemon_utils.h>
#include <rpcsvc/nlm_prot.h>
#include <libintl.h>
#include <libscf.h>
#include <libshare.h>
#include "nfs_tbind.h"
#include "thrpool.h"
#include "smfcfg.h"
struct lm_svc_args lmargs = {
.version = LM_SVC_CUR_VERS,
.debug = 0,
.timout = 5 * 60,
.grace = 90,
.retransmittimeout = 5
};
int max_servers = 256;
#define RET_OK 0
#define RET_ERR 33
static int nlmsvc(int fd, struct netbuf addrmask,
struct netconfig *nconf);
static int nlmsvcpool(int max_servers);
static void usage(void);
extern int _nfssys(int, void *);
static void sigterm_handler(int);
static void shutdown_lockd(void);
extern int daemonize_init(void);
extern void daemonize_fini(int fd);
static char *MyName;
static NETSELDECL(defaultproviders)[] = {
"/dev/ticotsord",
"/dev/tcp",
"/dev/udp",
"/dev/tcp6",
"/dev/udp6",
NULL
};
size_t end_listen_fds;
size_t num_fds = 0;
int listen_backlog = 32;
int (*Mysvc)(int, struct netbuf, struct netconfig *) = nlmsvc;
int max_conns_allowed = -1;
int
main(int ac, char *av[])
{
char *propname = NULL;
char *dir = "/";
char *provider = (char *)NULL;
struct protob *protobp;
NETSELPDECL(providerp);
sigset_t sgset;
int i, c, pid, ret, val;
int pipe_fd = -1;
struct sigaction act;
MyName = *av;
(void) _create_daemon_lock(LOCKD, DAEMON_UID, DAEMON_GID);
svcsetprio();
if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
DAEMON_UID, DAEMON_GID, PRIV_SYS_NFS, NULL) == -1) {
(void) fprintf(stderr, "%s should be run with"
" sufficient privileges\n", av[0]);
exit(1);
}
(void) enable_extended_FILE_stdio(-1, -1);
propname = "grace_period";
ret = nfs_smf_get_iprop(propname, &val,
DEFAULT_INSTANCE, SCF_TYPE_INTEGER, LOCKD);
if (ret == SA_OK) {
if (val <= 0)
fprintf(stderr, gettext(
"Invalid %s from SMF"), propname);
else
lmargs.grace = val;
} else {
syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
"value", propname);
}
propname = "lockd_listen_backlog";
ret = nfs_smf_get_iprop(propname, &val,
DEFAULT_INSTANCE, SCF_TYPE_INTEGER, LOCKD);
if (ret == SA_OK) {
if (val <= 0)
fprintf(stderr, gettext(
"Invalid %s from SMF"), propname);
else
listen_backlog = val;
} else {
syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
"value", propname);
}
propname = "lockd_servers";
ret = nfs_smf_get_iprop(propname, &val,
DEFAULT_INSTANCE, SCF_TYPE_INTEGER, LOCKD);
if (ret == SA_OK) {
if (val <= 0)
fprintf(stderr, gettext(
"Invalid %s from SMF"), propname);
else
max_servers = val;
} else {
syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
"value", propname);
}
propname = "lockd_retransmit_timeout";
ret = nfs_smf_get_iprop(propname, &val,
DEFAULT_INSTANCE, SCF_TYPE_INTEGER, LOCKD);
if (ret == SA_OK) {
if (val <= 0)
fprintf(stderr, gettext(
"Invalid %s from SMF"), propname);
else
lmargs.retransmittimeout = val;
} else {
syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
"value", propname);
}
while ((c = getopt(ac, av, "c:d:g:l:t:")) != EOF)
switch (c) {
case 'c':
if ((val = atoi(optarg)) <= 0)
goto badval;
max_conns_allowed = val;
break;
case 'd':
lmargs.debug = atoi(optarg);
break;
case 'g':
if ((val = atoi(optarg)) <= 0)
goto badval;
lmargs.grace = val;
break;
case 'l':
if ((val = atoi(optarg)) <= 0)
goto badval;
listen_backlog = val;
break;
case 't':
if ((val = atoi(optarg)) <= 0)
goto badval;
lmargs.retransmittimeout = val;
break;
badval:
fprintf(stderr, gettext(
"Invalid -%c option value"), c);
default:
usage();
}
if (optind < ac) {
val = atoi(av[optind]);
if (val <= 0) {
fprintf(stderr, gettext(
"Invalid max_servers argument"));
usage();
}
max_servers = val;
optind++;
}
if (optind != ac)
usage();
if (lmargs.debug) {
printf("%s: debug= %d, conn_idle_timout= %d,"
" grace_period= %d, listen_backlog= %d,"
" max_connections= %d, max_servers= %d,"
" retrans_timeout= %d\n",
MyName, lmargs.debug, lmargs.timout,
lmargs.grace, listen_backlog,
max_conns_allowed, max_servers,
lmargs.retransmittimeout);
}
if (chdir(dir) < 0) {
(void) fprintf(stderr, "%s: ", MyName);
perror(dir);
exit(1);
}
if (lmargs.debug == 0)
pipe_fd = daemonize_init();
openlog(MyName, LOG_PID | LOG_NDELAY, LOG_DAEMON);
pid = _enter_daemon_lock(LOCKD);
switch (pid) {
case 0:
break;
case -1:
fprintf(stderr, "error locking for %s: %s", LOCKD,
strerror(errno));
exit(2);
default:
exit(0);
}
(void) sigfillset(&sgset);
(void) thr_sigsetmask(SIG_BLOCK, &sgset, NULL);
for (i = NLM_VERS; i < NLM4_VERS; i++) {
svc_unreg(NLM_PROG, i);
}
if (nlmsvcpool(max_servers)) {
fprintf(stderr, "Can't set up kernel NLM service: %s. Exiting",
strerror(errno));
exit(1);
}
if (svcwait(NLM_SVCPOOL_ID)) {
fprintf(stderr, "Can't set up NLM pool creator: %s. Exiting",
strerror(errno));
exit(1);
}
act.sa_handler = sigterm_handler;
act.sa_flags = 0;
(void) sigaction(SIGTERM, &act, NULL);
(void) atexit(shutdown_lockd);
(void) thr_sigsetmask(SIG_UNBLOCK, &sgset, NULL);
protobp = (struct protob *)malloc(sizeof (struct protob));
protobp->serv = "NLM";
protobp->versmin = NLM_VERS;
protobp->versmax = NLM4_VERS;
protobp->program = NLM_PROG;
protobp->next = (struct protob *)NULL;
for (providerp = defaultproviders;
*providerp != NULL; providerp++) {
provider = *providerp;
do_one(provider, NULL, protobp, nlmsvc);
}
free(protobp);
if (num_fds == 0) {
fprintf(stderr, "Could not start NLM service for any protocol."
" Exiting");
exit(1);
}
end_listen_fds = num_fds;
if (lmargs.debug == 0)
daemonize_fini(pipe_fd);
__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
poll_for_action();
return (1);
}
static int
nlmsvcpool(int maxservers)
{
struct svcpool_args npa;
npa.id = NLM_SVCPOOL_ID;
npa.maxthreads = maxservers;
npa.redline = 0;
npa.qsize = 0;
npa.timeout = 0;
npa.stksize = 0;
npa.max_same_xprt = 0;
return (_nfssys(SVCPOOL_CREATE, &npa));
}
static int
ncfmly_to_lmfmly(const char *ncfmly)
{
if (0 == strcmp(ncfmly, NC_INET))
return (LM_INET);
if (0 == strcmp(ncfmly, NC_INET6))
return (LM_INET6);
if (0 == strcmp(ncfmly, NC_LOOPBACK))
return (LM_LOOPBACK);
return (-1);
}
static int
nctype_to_lmprot(uint_t semantics)
{
switch (semantics) {
case NC_TPI_CLTS:
return (LM_UDP);
case NC_TPI_COTS_ORD:
return (LM_TCP);
}
return (-1);
}
static dev_t
ncdev_to_rdev(const char *ncdev)
{
struct stat st;
if (stat(ncdev, &st) < 0)
return (NODEV);
return (st.st_rdev);
}
static void
sigterm_handler(int signal __unused)
{
exit(0);
}
static void
shutdown_lockd(void)
{
(void) _nfssys(KILL_LOCKMGR, NULL);
}
static int
nlmsvc(int fd, struct netbuf addrmask, struct netconfig *nconf)
{
struct lm_svc_args lma;
lma = lmargs;
lma.fd = fd;
lma.n_fmly = ncfmly_to_lmfmly(nconf->nc_protofmly);
lma.n_proto = nctype_to_lmprot(nconf->nc_semantics);
lma.n_rdev = ncdev_to_rdev(nconf->nc_device);
return (_nfssys(LM_SVC, &lma));
}
static void
usage(void)
{
(void) fprintf(stderr, gettext(
"usage: %s [options] [max_servers]\n"), MyName);
(void) fprintf(stderr, gettext(
"options: (see SMF property descriptions)\n"));
(void) fprintf(stderr, "\t-c max_connections\n");
(void) fprintf(stderr, "\t-d debug_level\n");
(void) fprintf(stderr, "\t-g grace_period\n");
(void) fprintf(stderr, "\t-l listen_backlog\n");
(void) fprintf(stderr, "\t-t retransmit_timeout\n");
exit(1);
}