xref: /titanic_50/usr/src/cmd/rpcsvc/rstat_main.c (revision 49e7ca4919cec3229f6fab9730bafc7cf24dab23)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*49e7ca49Speteh  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*49e7ca49Speteh  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "rstat.h"
307c478bd9Sstevel@tonic-gate #include "rstat_v2.h"
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h> /* getenv, exit */
337c478bd9Sstevel@tonic-gate #include <signal.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <memory.h>
367c478bd9Sstevel@tonic-gate #include <stropts.h>
377c478bd9Sstevel@tonic-gate #include <netconfig.h>
387c478bd9Sstevel@tonic-gate #include <syslog.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifdef __STDC__
417c478bd9Sstevel@tonic-gate #define	SIG_PF void(*)(int)
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifdef DEBUG
457c478bd9Sstevel@tonic-gate #define	RPC_SVC_FG
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate int _rpcpmstart;		/* Started by a port monitor ? */
507c478bd9Sstevel@tonic-gate int _rpcfdtype;			/* Whether Stream or Datagram ? */
517c478bd9Sstevel@tonic-gate int _rpcsvcdirty;		/* Still serving ? */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static void _msgout(/*char *msg*/);
547c478bd9Sstevel@tonic-gate static void closedown();
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate extern void rstatprog_4(/*struct svc_req *rqstp, SVCXPRT *transp*/);
577c478bd9Sstevel@tonic-gate extern void rstatprog_3(/*struct svc_req *rqstp, SVCXPRT *transp*/);
587c478bd9Sstevel@tonic-gate extern void rstatprog_2(/*struct svc_req *rqstp, SVCXPRT *transp*/);
597c478bd9Sstevel@tonic-gate 
60*49e7ca49Speteh int
main(int argc,char * argv[])61*49e7ca49Speteh main(int argc, char *argv[])
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	pid_t pid;
647c478bd9Sstevel@tonic-gate 	int i;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/*
677c478bd9Sstevel@tonic-gate 	 * If stdin looks like a TLI endpoint, we assume
687c478bd9Sstevel@tonic-gate 	 * that we were started by a port monitor. If
697c478bd9Sstevel@tonic-gate 	 * t_getstate fails with TBADF, this is not a
707c478bd9Sstevel@tonic-gate 	 * TLI endpoint.
717c478bd9Sstevel@tonic-gate 	 */
727c478bd9Sstevel@tonic-gate 	if (t_getstate(0) != -1 || t_errno != TBADF) {
737c478bd9Sstevel@tonic-gate 		char *netid;
747c478bd9Sstevel@tonic-gate 		struct netconfig *nconf = NULL;
757c478bd9Sstevel@tonic-gate 		SVCXPRT *transp;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 		_rpcpmstart = 1;
787c478bd9Sstevel@tonic-gate 		openlog("rstatd", LOG_PID, LOG_DAEMON);
797c478bd9Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) == NULL) {
807c478bd9Sstevel@tonic-gate #ifdef DEBUG
817c478bd9Sstevel@tonic-gate 			_msgout("cannot get transport name");
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate 		} else if ((nconf = getnetconfigent(netid)) == NULL) {
847c478bd9Sstevel@tonic-gate #ifdef DEBUG
857c478bd9Sstevel@tonic-gate 			_msgout("cannot get transport info");
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
897c478bd9Sstevel@tonic-gate 			_msgout("cannot create server handle");
907c478bd9Sstevel@tonic-gate 			exit(1);
917c478bd9Sstevel@tonic-gate 		}
927c478bd9Sstevel@tonic-gate 		if (nconf)
937c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
947c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_VAR, rstatprog_4,
957c478bd9Sstevel@tonic-gate 		    0)) {
96*49e7ca49Speteh 			_msgout("unable to register "
97*49e7ca49Speteh 			    "(RSTATPROG, RSTATVERS_VAR).");
987c478bd9Sstevel@tonic-gate 			exit(1);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_TIME, rstatprog_3,
1017c478bd9Sstevel@tonic-gate 		    0)) {
102*49e7ca49Speteh 			_msgout("unable to register "
103*49e7ca49Speteh 			    "(RSTATPROG, RSTATVERS_TIME).");
1047c478bd9Sstevel@tonic-gate 			exit(1);
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_SWTCH, rstatprog_2,
1077c478bd9Sstevel@tonic-gate 		    0)) {
108*49e7ca49Speteh 			_msgout("unable to register "
109*49e7ca49Speteh 			    "(RSTATPROG, RSTATVERS_SWTCH).");
1107c478bd9Sstevel@tonic-gate 			exit(1);
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 		svc_run();
1137c478bd9Sstevel@tonic-gate 		exit(1);
1147c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
115*49e7ca49Speteh 	} else {
1167c478bd9Sstevel@tonic-gate #ifndef RPC_SVC_FG
1177c478bd9Sstevel@tonic-gate 		pid = fork();
1187c478bd9Sstevel@tonic-gate 		if (pid < 0) {
1197c478bd9Sstevel@tonic-gate 			perror("cannot fork");
1207c478bd9Sstevel@tonic-gate 			exit(1);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 		if (pid)
1237c478bd9Sstevel@tonic-gate 			exit(0);
1247c478bd9Sstevel@tonic-gate 		closefrom(0);
1257c478bd9Sstevel@tonic-gate 		i = open("/dev/console", 2);
1267c478bd9Sstevel@tonic-gate 		(void) dup2(i, 1);
1277c478bd9Sstevel@tonic-gate 		(void) dup2(i, 2);
1287c478bd9Sstevel@tonic-gate 		setsid();
1297c478bd9Sstevel@tonic-gate 		openlog("rstatd", LOG_PID, LOG_DAEMON);
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "datagram_v")) {
133*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_VAR) "
134*49e7ca49Speteh 		    "for datagram_v.");
1357c478bd9Sstevel@tonic-gate 		exit(1);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME,
1387c478bd9Sstevel@tonic-gate 	    "datagram_v")) {
139*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_TIME) "
140*49e7ca49Speteh 		    "for datagram_v.");
1417c478bd9Sstevel@tonic-gate 		exit(1);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "circuit_v")) {
144*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_VAR) "
145*49e7ca49Speteh 		    "for circuit_v.");
1467c478bd9Sstevel@tonic-gate 		exit(1);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME, "circuit_v")) {
149*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_TIME) "
150*49e7ca49Speteh 		    "for circuit_v.");
1517c478bd9Sstevel@tonic-gate 		exit(1);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * V2 supported on datagram transports *only*
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_2, RSTATPROG, RSTATVERS_SWTCH,
1587c478bd9Sstevel@tonic-gate 	    "datagram_v")) {
159*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_SWTCH) "
160*49e7ca49Speteh 		    "for datagram_v.");
1617c478bd9Sstevel@tonic-gate 		exit(1);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	svc_run();
1657c478bd9Sstevel@tonic-gate 	_msgout("svc_run returned");
166*49e7ca49Speteh 	return (1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
169*49e7ca49Speteh static void
_msgout(msg)170*49e7ca49Speteh _msgout(msg)
1717c478bd9Sstevel@tonic-gate 	char *msg;
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate #ifdef RPC_SVC_FG
1747c478bd9Sstevel@tonic-gate 	if (_rpcpmstart)
1757c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, msg);
1767c478bd9Sstevel@tonic-gate 	else
1777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", msg);
1787c478bd9Sstevel@tonic-gate #else
1797c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, msg);
1807c478bd9Sstevel@tonic-gate #endif
1817c478bd9Sstevel@tonic-gate }
182