xref: /titanic_53/usr/src/cmd/rpcsvc/rstat_main.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) 1991-2000 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 #include "rstat.h"
30*7c478bd9Sstevel@tonic-gate #include "rstat_v2.h"
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h> /* getenv, exit */
33*7c478bd9Sstevel@tonic-gate #include <signal.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <memory.h>
36*7c478bd9Sstevel@tonic-gate #include <stropts.h>
37*7c478bd9Sstevel@tonic-gate #include <netconfig.h>
38*7c478bd9Sstevel@tonic-gate #include <syslog.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
41*7c478bd9Sstevel@tonic-gate #define	SIG_PF void(*)(int)
42*7c478bd9Sstevel@tonic-gate #endif
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
45*7c478bd9Sstevel@tonic-gate #define	RPC_SVC_FG
46*7c478bd9Sstevel@tonic-gate #endif
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate int _rpcpmstart;		/* Started by a port monitor ? */
50*7c478bd9Sstevel@tonic-gate int _rpcfdtype;			/* Whether Stream or Datagram ? */
51*7c478bd9Sstevel@tonic-gate int _rpcsvcdirty;		/* Still serving ? */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static void _msgout(/*char *msg*/);
54*7c478bd9Sstevel@tonic-gate static void closedown();
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate extern void rstatprog_4(/*struct svc_req *rqstp, SVCXPRT *transp*/);
57*7c478bd9Sstevel@tonic-gate extern void rstatprog_3(/*struct svc_req *rqstp, SVCXPRT *transp*/);
58*7c478bd9Sstevel@tonic-gate extern void rstatprog_2(/*struct svc_req *rqstp, SVCXPRT *transp*/);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate main()
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	pid_t pid;
63*7c478bd9Sstevel@tonic-gate 	int i;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	/*
66*7c478bd9Sstevel@tonic-gate 	 * If stdin looks like a TLI endpoint, we assume
67*7c478bd9Sstevel@tonic-gate 	 * that we were started by a port monitor. If
68*7c478bd9Sstevel@tonic-gate 	 * t_getstate fails with TBADF, this is not a
69*7c478bd9Sstevel@tonic-gate 	 * TLI endpoint.
70*7c478bd9Sstevel@tonic-gate 	 */
71*7c478bd9Sstevel@tonic-gate 	if (t_getstate(0) != -1 || t_errno != TBADF) {
72*7c478bd9Sstevel@tonic-gate 		char *netid;
73*7c478bd9Sstevel@tonic-gate 		struct netconfig *nconf = NULL;
74*7c478bd9Sstevel@tonic-gate 		SVCXPRT *transp;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 		_rpcpmstart = 1;
77*7c478bd9Sstevel@tonic-gate 		openlog("rstatd", LOG_PID, LOG_DAEMON);
78*7c478bd9Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) == NULL) {
79*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
80*7c478bd9Sstevel@tonic-gate 			_msgout("cannot get transport name");
81*7c478bd9Sstevel@tonic-gate #endif
82*7c478bd9Sstevel@tonic-gate 		} else if ((nconf = getnetconfigent(netid)) == NULL) {
83*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
84*7c478bd9Sstevel@tonic-gate 			_msgout("cannot get transport info");
85*7c478bd9Sstevel@tonic-gate #endif
86*7c478bd9Sstevel@tonic-gate 		}
87*7c478bd9Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
88*7c478bd9Sstevel@tonic-gate 			_msgout("cannot create server handle");
89*7c478bd9Sstevel@tonic-gate 			exit(1);
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 		if (nconf)
92*7c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
93*7c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_VAR, rstatprog_4,
94*7c478bd9Sstevel@tonic-gate 		    0)) {
95*7c478bd9Sstevel@tonic-gate     _msgout("unable to register (RSTATPROG, RSTATVERS_VAR).");
96*7c478bd9Sstevel@tonic-gate 			exit(1);
97*7c478bd9Sstevel@tonic-gate 		}
98*7c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_TIME, rstatprog_3,
99*7c478bd9Sstevel@tonic-gate 		    0)) {
100*7c478bd9Sstevel@tonic-gate     _msgout("unable to register (RSTATPROG, RSTATVERS_TIME).");
101*7c478bd9Sstevel@tonic-gate 			exit(1);
102*7c478bd9Sstevel@tonic-gate 		}
103*7c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_SWTCH, rstatprog_2,
104*7c478bd9Sstevel@tonic-gate 		    0)) {
105*7c478bd9Sstevel@tonic-gate     _msgout("unable to register (RSTATPROG, RSTATVERS_SWTCH).");
106*7c478bd9Sstevel@tonic-gate 			exit(1);
107*7c478bd9Sstevel@tonic-gate 		}
108*7c478bd9Sstevel@tonic-gate 		svc_run();
109*7c478bd9Sstevel@tonic-gate 		exit(1);
110*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 	else {
113*7c478bd9Sstevel@tonic-gate #ifndef RPC_SVC_FG
114*7c478bd9Sstevel@tonic-gate 		pid = fork();
115*7c478bd9Sstevel@tonic-gate 		if (pid < 0) {
116*7c478bd9Sstevel@tonic-gate 			perror("cannot fork");
117*7c478bd9Sstevel@tonic-gate 			exit(1);
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 		if (pid)
120*7c478bd9Sstevel@tonic-gate 			exit(0);
121*7c478bd9Sstevel@tonic-gate 		closefrom(0);
122*7c478bd9Sstevel@tonic-gate 		i = open("/dev/console", 2);
123*7c478bd9Sstevel@tonic-gate 		(void) dup2(i, 1);
124*7c478bd9Sstevel@tonic-gate 		(void) dup2(i, 2);
125*7c478bd9Sstevel@tonic-gate 		setsid();
126*7c478bd9Sstevel@tonic-gate 		openlog("rstatd", LOG_PID, LOG_DAEMON);
127*7c478bd9Sstevel@tonic-gate #endif
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "datagram_v")) {
130*7c478bd9Sstevel@tonic-gate     _msgout("unable to create (RSTATPROG, RSTATVERS_VAR) for datagram_v.");
131*7c478bd9Sstevel@tonic-gate 		exit(1);
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME,
134*7c478bd9Sstevel@tonic-gate 	    "datagram_v")) {
135*7c478bd9Sstevel@tonic-gate     _msgout("unable to create (RSTATPROG, RSTATVERS_TIME) for datagram_v.");
136*7c478bd9Sstevel@tonic-gate 		exit(1);
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "circuit_v")) {
139*7c478bd9Sstevel@tonic-gate     _msgout("unable to create (RSTATPROG, RSTATVERS_VAR) for circuit_v.");
140*7c478bd9Sstevel@tonic-gate 		exit(1);
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME, "circuit_v")) {
143*7c478bd9Sstevel@tonic-gate     _msgout("unable to create (RSTATPROG, RSTATVERS_TIME) for circuit_v.");
144*7c478bd9Sstevel@tonic-gate 		exit(1);
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	/*
148*7c478bd9Sstevel@tonic-gate 	 * V2 supported on datagram transports *only*
149*7c478bd9Sstevel@tonic-gate 	 */
150*7c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_2, RSTATPROG, RSTATVERS_SWTCH,
151*7c478bd9Sstevel@tonic-gate 	    "datagram_v")) {
152*7c478bd9Sstevel@tonic-gate     _msgout("unable to create (RSTATPROG, RSTATVERS_SWTCH) for datagram_v.");
153*7c478bd9Sstevel@tonic-gate 		exit(1);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	svc_run();
157*7c478bd9Sstevel@tonic-gate 	_msgout("svc_run returned");
158*7c478bd9Sstevel@tonic-gate 	exit(1);
159*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate static
163*7c478bd9Sstevel@tonic-gate void _msgout(msg)
164*7c478bd9Sstevel@tonic-gate 	char *msg;
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate #ifdef RPC_SVC_FG
167*7c478bd9Sstevel@tonic-gate 	if (_rpcpmstart)
168*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, msg);
169*7c478bd9Sstevel@tonic-gate 	else
170*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", msg);
171*7c478bd9Sstevel@tonic-gate #else
172*7c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, msg);
173*7c478bd9Sstevel@tonic-gate #endif
174*7c478bd9Sstevel@tonic-gate }
175