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