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