1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
28*7c478bd9Sstevel@tonic-gate #include <stdio.h>
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <signal.h>
31*7c478bd9Sstevel@tonic-gate #include <unistd.h>
32*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
33*7c478bd9Sstevel@tonic-gate #include <memory.h>
34*7c478bd9Sstevel@tonic-gate #include <stropts.h>
35*7c478bd9Sstevel@tonic-gate #include <netconfig.h>
36*7c478bd9Sstevel@tonic-gate #include <stropts.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/termios.h>
38*7c478bd9Sstevel@tonic-gate #include <syslog.h>
39*7c478bd9Sstevel@tonic-gate #include <rpcsvc/bootparam_prot.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include "bootparam_private.h"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	_RPCSVC_CLOSEDOWN 120
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate int debug = 0;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate static void bootparamprog_1(struct svc_req *, register SVCXPRT *);
48*7c478bd9Sstevel@tonic-gate static void closedown(int);
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static int server_child = 0;	/* program was started by another server */
51*7c478bd9Sstevel@tonic-gate static int _rpcsvcdirty;	/* Still serving ? */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])54*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	pid_t pid;
57*7c478bd9Sstevel@tonic-gate 	int c;
58*7c478bd9Sstevel@tonic-gate 	char *progname = argv[0];
59*7c478bd9Sstevel@tonic-gate 	int connmaxrec = RPC_MAXDATASIZE;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != -1)
62*7c478bd9Sstevel@tonic-gate 		switch ((char)c) {
63*7c478bd9Sstevel@tonic-gate 		case 'd':
64*7c478bd9Sstevel@tonic-gate 			debug++;
65*7c478bd9Sstevel@tonic-gate 			break;
66*7c478bd9Sstevel@tonic-gate 		default:
67*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "usage: %s [-d]\n", progname);
68*7c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
69*7c478bd9Sstevel@tonic-gate 		}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/*
73*7c478bd9Sstevel@tonic-gate 	 * Set non-blocking mode and maximum record size for
74*7c478bd9Sstevel@tonic-gate 	 * connection oriented RPC transports.
75*7c478bd9Sstevel@tonic-gate 	 */
76*7c478bd9Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
77*7c478bd9Sstevel@tonic-gate 		msgout("unable to set maximum RPC record size");
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	/*
81*7c478bd9Sstevel@tonic-gate 	 * If stdin looks like a TLI endpoint, we assume
82*7c478bd9Sstevel@tonic-gate 	 * that we were started by a port monitor. If
83*7c478bd9Sstevel@tonic-gate 	 * t_getstate fails with TBADF, this is not a
84*7c478bd9Sstevel@tonic-gate 	 * TLI endpoint.
85*7c478bd9Sstevel@tonic-gate 	 */
86*7c478bd9Sstevel@tonic-gate 	if (t_getstate(0) != -1 || t_errno != TBADF) {
87*7c478bd9Sstevel@tonic-gate 		char *netid;
88*7c478bd9Sstevel@tonic-gate 		struct netconfig *nconf = NULL;
89*7c478bd9Sstevel@tonic-gate 		SVCXPRT *transp;
90*7c478bd9Sstevel@tonic-gate 		int pmclose;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) == NULL) {
93*7c478bd9Sstevel@tonic-gate 			if (debug)
94*7c478bd9Sstevel@tonic-gate 				msgout("cannot get transport name");
95*7c478bd9Sstevel@tonic-gate 		} else if ((nconf = getnetconfigent(netid)) == NULL) {
96*7c478bd9Sstevel@tonic-gate 			if (debug)
97*7c478bd9Sstevel@tonic-gate 				msgout("cannot get transport info");
98*7c478bd9Sstevel@tonic-gate 		}
99*7c478bd9Sstevel@tonic-gate 		pmclose = (t_getstate(0) != T_DATAXFER);
100*7c478bd9Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
101*7c478bd9Sstevel@tonic-gate 			msgout("cannot create server handle");
102*7c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 		if (nconf)
105*7c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
106*7c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, BOOTPARAMPROG, BOOTPARAMVERS,
107*7c478bd9Sstevel@tonic-gate 		    bootparamprog_1, 0)) {
108*7c478bd9Sstevel@tonic-gate 			msgout("unable to register (BOOTPARAMPROG, "
109*7c478bd9Sstevel@tonic-gate 			    "BOOTPARAMVERS).");
110*7c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 		if (pmclose) {
113*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGALRM, closedown);
114*7c478bd9Sstevel@tonic-gate 			(void) alarm(_RPCSVC_CLOSEDOWN);
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		svc_run();
118*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
119*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	/*
123*7c478bd9Sstevel@tonic-gate 	 * run this process in the background only if it was started from
124*7c478bd9Sstevel@tonic-gate 	 * a shell and the debug flag was not given.
125*7c478bd9Sstevel@tonic-gate 	 */
126*7c478bd9Sstevel@tonic-gate 	if (!server_child && !debug) {
127*7c478bd9Sstevel@tonic-gate 		pid = fork();
128*7c478bd9Sstevel@tonic-gate 		if (pid < 0) {
129*7c478bd9Sstevel@tonic-gate 			perror("cannot fork");
130*7c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
131*7c478bd9Sstevel@tonic-gate 		}
132*7c478bd9Sstevel@tonic-gate 		if (pid)
133*7c478bd9Sstevel@tonic-gate 			exit(EXIT_SUCCESS);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 		closefrom(0);
136*7c478bd9Sstevel@tonic-gate 		(void) setsid();
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	/*
140*7c478bd9Sstevel@tonic-gate 	 * messges go to syslog if the program was started by
141*7c478bd9Sstevel@tonic-gate 	 * another server, or if it was run from the command line without
142*7c478bd9Sstevel@tonic-gate 	 * the debug flag.
143*7c478bd9Sstevel@tonic-gate 	 */
144*7c478bd9Sstevel@tonic-gate 	if (server_child || !debug)
145*7c478bd9Sstevel@tonic-gate 		openlog("bootparam_prot", LOG_PID, LOG_DAEMON);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if (debug) {
148*7c478bd9Sstevel@tonic-gate 		if (debug == 1)
149*7c478bd9Sstevel@tonic-gate 			msgout("in debug mode.");
150*7c478bd9Sstevel@tonic-gate 		else
151*7c478bd9Sstevel@tonic-gate 			msgout("in debug mode (level %d).", debug);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	if (!svc_create(bootparamprog_1, BOOTPARAMPROG, BOOTPARAMVERS,
155*7c478bd9Sstevel@tonic-gate 			"netpath")) {
156*7c478bd9Sstevel@tonic-gate 		msgout("unable to create (BOOTPARAMPROG, BOOTPARAMVERS) "
157*7c478bd9Sstevel@tonic-gate 		    "for netpath.");
158*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	svc_run();
162*7c478bd9Sstevel@tonic-gate 	msgout("svc_run returned");
163*7c478bd9Sstevel@tonic-gate 	return (EXIT_FAILURE);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate static void
bootparamprog_1(struct svc_req * rqstp,register SVCXPRT * transp)167*7c478bd9Sstevel@tonic-gate bootparamprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	union {
170*7c478bd9Sstevel@tonic-gate 		bp_whoami_arg bootparamproc_whoami_1_arg;
171*7c478bd9Sstevel@tonic-gate 		bp_getfile_arg bootparamproc_getfile_1_arg;
172*7c478bd9Sstevel@tonic-gate 	} argument;
173*7c478bd9Sstevel@tonic-gate 	char *result;
174*7c478bd9Sstevel@tonic-gate 	bool_t (*xdr_argument)(), (*xdr_result)();
175*7c478bd9Sstevel@tonic-gate 	char *(*local)();
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	_rpcsvcdirty = 1;
178*7c478bd9Sstevel@tonic-gate 	switch (rqstp->rq_proc) {
179*7c478bd9Sstevel@tonic-gate 	case NULLPROC:
180*7c478bd9Sstevel@tonic-gate 		(void) svc_sendreply(transp, xdr_void, (char *)NULL);
181*7c478bd9Sstevel@tonic-gate 		_rpcsvcdirty = 0;
182*7c478bd9Sstevel@tonic-gate 		return;
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	case BOOTPARAMPROC_WHOAMI:
185*7c478bd9Sstevel@tonic-gate 		xdr_argument = xdr_bp_whoami_arg;
186*7c478bd9Sstevel@tonic-gate 		xdr_result = xdr_bp_whoami_res;
187*7c478bd9Sstevel@tonic-gate 		local = (char *(*)()) bootparamproc_whoami_1;
188*7c478bd9Sstevel@tonic-gate 		break;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	case BOOTPARAMPROC_GETFILE:
191*7c478bd9Sstevel@tonic-gate 		xdr_argument = xdr_bp_getfile_arg;
192*7c478bd9Sstevel@tonic-gate 		xdr_result = xdr_bp_getfile_res;
193*7c478bd9Sstevel@tonic-gate 		local = (char *(*)()) bootparamproc_getfile_1;
194*7c478bd9Sstevel@tonic-gate 		break;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	default:
197*7c478bd9Sstevel@tonic-gate 		svcerr_noproc(transp);
198*7c478bd9Sstevel@tonic-gate 		_rpcsvcdirty = 0;
199*7c478bd9Sstevel@tonic-gate 		return;
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 	(void) memset((char *)&argument, 0, sizeof (argument));
202*7c478bd9Sstevel@tonic-gate 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
203*7c478bd9Sstevel@tonic-gate 		svcerr_decode(transp);
204*7c478bd9Sstevel@tonic-gate 		_rpcsvcdirty = 0;
205*7c478bd9Sstevel@tonic-gate 		return;
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 	result = (*local)(&argument, rqstp);
208*7c478bd9Sstevel@tonic-gate 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
209*7c478bd9Sstevel@tonic-gate 		svcerr_systemerr(transp);
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
212*7c478bd9Sstevel@tonic-gate 		msgout("unable to free arguments");
213*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
214*7c478bd9Sstevel@tonic-gate 	}
215*7c478bd9Sstevel@tonic-gate 	_rpcsvcdirty = 0;
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
219*7c478bd9Sstevel@tonic-gate void
msgout(char * fmt,...)220*7c478bd9Sstevel@tonic-gate msgout(char *fmt, ...)
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate 	va_list	ap;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
225*7c478bd9Sstevel@tonic-gate 	/*
226*7c478bd9Sstevel@tonic-gate 	 * messges go to syslog if the program was started by
227*7c478bd9Sstevel@tonic-gate 	 * another server, or if it was run from the command line without
228*7c478bd9Sstevel@tonic-gate 	 * the debug flag.
229*7c478bd9Sstevel@tonic-gate 	 */
230*7c478bd9Sstevel@tonic-gate 	if (server_child || !debug)
231*7c478bd9Sstevel@tonic-gate 		vsyslog(LOG_ERR, fmt, ap);
232*7c478bd9Sstevel@tonic-gate 	else {
233*7c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
234*7c478bd9Sstevel@tonic-gate 		(void) fputc('\n', stderr);
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 	va_end(ap);
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
240*7c478bd9Sstevel@tonic-gate static void
closedown(int sig)241*7c478bd9Sstevel@tonic-gate closedown(int sig)
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	if (_rpcsvcdirty == 0) {
244*7c478bd9Sstevel@tonic-gate 		int size;
245*7c478bd9Sstevel@tonic-gate 		int i, openfd;
246*7c478bd9Sstevel@tonic-gate 		struct t_info tinfo;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 		if (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))
249*7c478bd9Sstevel@tonic-gate 			exit(EXIT_SUCCESS);
250*7c478bd9Sstevel@tonic-gate 		size = svc_max_pollfd;
251*7c478bd9Sstevel@tonic-gate 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
252*7c478bd9Sstevel@tonic-gate 			if (svc_pollfd[i].fd >= 0)
253*7c478bd9Sstevel@tonic-gate 				openfd++;
254*7c478bd9Sstevel@tonic-gate 		if (openfd <= 1)
255*7c478bd9Sstevel@tonic-gate 			exit(EXIT_SUCCESS);
256*7c478bd9Sstevel@tonic-gate 	}
257*7c478bd9Sstevel@tonic-gate 	(void) alarm(_RPCSVC_CLOSEDOWN);
258*7c478bd9Sstevel@tonic-gate }
259