#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <memory.h>
#include <stropts.h>
#include <netconfig.h>
#include <stropts.h>
#include <sys/termios.h>
#include <syslog.h>
#include <rpcsvc/bootparam_prot.h>
#include "bootparam_private.h"
#define _RPCSVC_CLOSEDOWN 120
int debug = 0;
static void bootparamprog_1(struct svc_req *, register SVCXPRT *);
static void closedown(int);
static int server_child = 0;
static int _rpcsvcdirty;
int
main(int argc, char *argv[])
{
pid_t pid;
int c;
char *progname = argv[0];
int connmaxrec = RPC_MAXDATASIZE;
while ((c = getopt(argc, argv, "d")) != -1)
switch ((char)c) {
case 'd':
debug++;
break;
default:
(void) fprintf(stderr, "usage: %s [-d]\n", progname);
exit(EXIT_FAILURE);
}
if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
msgout("unable to set maximum RPC record size");
}
if (t_getstate(0) != -1 || t_errno != TBADF) {
char *netid;
struct netconfig *nconf = NULL;
SVCXPRT *transp;
int pmclose;
if ((netid = getenv("NLSPROVIDER")) == NULL) {
if (debug)
msgout("cannot get transport name");
} else if ((nconf = getnetconfigent(netid)) == NULL) {
if (debug)
msgout("cannot get transport info");
}
pmclose = (t_getstate(0) != T_DATAXFER);
if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
msgout("cannot create server handle");
exit(EXIT_FAILURE);
}
if (nconf)
freenetconfigent(nconf);
if (!svc_reg(transp, BOOTPARAMPROG, BOOTPARAMVERS,
bootparamprog_1, 0)) {
msgout("unable to register (BOOTPARAMPROG, "
"BOOTPARAMVERS).");
exit(EXIT_FAILURE);
}
if (pmclose) {
(void) signal(SIGALRM, closedown);
(void) alarm(_RPCSVC_CLOSEDOWN);
}
svc_run();
exit(EXIT_FAILURE);
}
if (!server_child && !debug) {
pid = fork();
if (pid < 0) {
perror("cannot fork");
exit(EXIT_FAILURE);
}
if (pid)
exit(EXIT_SUCCESS);
closefrom(0);
(void) setsid();
}
if (server_child || !debug)
openlog("bootparam_prot", LOG_PID, LOG_DAEMON);
if (debug) {
if (debug == 1)
msgout("in debug mode.");
else
msgout("in debug mode (level %d).", debug);
}
if (!svc_create(bootparamprog_1, BOOTPARAMPROG, BOOTPARAMVERS,
"netpath")) {
msgout("unable to create (BOOTPARAMPROG, BOOTPARAMVERS) "
"for netpath.");
exit(EXIT_FAILURE);
}
svc_run();
msgout("svc_run returned");
return (EXIT_FAILURE);
}
static void
bootparamprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
{
union {
bp_whoami_arg bootparamproc_whoami_1_arg;
bp_getfile_arg bootparamproc_getfile_1_arg;
} argument;
char *result;
bool_t (*xdr_argument)(), (*xdr_result)();
char *(*local)();
_rpcsvcdirty = 1;
switch (rqstp->rq_proc) {
case NULLPROC:
(void) svc_sendreply(transp, xdr_void, (char *)NULL);
_rpcsvcdirty = 0;
return;
case BOOTPARAMPROC_WHOAMI:
xdr_argument = xdr_bp_whoami_arg;
xdr_result = xdr_bp_whoami_res;
local = (char *(*)()) bootparamproc_whoami_1;
break;
case BOOTPARAMPROC_GETFILE:
xdr_argument = xdr_bp_getfile_arg;
xdr_result = xdr_bp_getfile_res;
local = (char *(*)()) bootparamproc_getfile_1;
break;
default:
svcerr_noproc(transp);
_rpcsvcdirty = 0;
return;
}
(void) memset((char *)&argument, 0, sizeof (argument));
if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
svcerr_decode(transp);
_rpcsvcdirty = 0;
return;
}
result = (*local)(&argument, rqstp);
if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
svcerr_systemerr(transp);
}
if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
msgout("unable to free arguments");
exit(EXIT_FAILURE);
}
_rpcsvcdirty = 0;
}
void
msgout(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (server_child || !debug)
vsyslog(LOG_ERR, fmt, ap);
else {
(void) vfprintf(stderr, fmt, ap);
(void) fputc('\n', stderr);
}
va_end(ap);
}
static void
closedown(int sig)
{
if (_rpcsvcdirty == 0) {
int size;
int i, openfd;
struct t_info tinfo;
if (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))
exit(EXIT_SUCCESS);
size = svc_max_pollfd;
for (i = 0, openfd = 0; i < size && openfd < 2; i++)
if (svc_pollfd[i].fd >= 0)
openfd++;
if (openfd <= 1)
exit(EXIT_SUCCESS);
}
(void) alarm(_RPCSVC_CLOSEDOWN);
}