xref: /illumos-gate/usr/src/cmd/rpcsvc/rstat_main.c (revision 2a8bcb4e)
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 #include "rstat.h"
287c478bd9Sstevel@tonic-gate #include "rstat_v2.h"
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h> /* getenv, exit */
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <memory.h>
347c478bd9Sstevel@tonic-gate #include <stropts.h>
357c478bd9Sstevel@tonic-gate #include <netconfig.h>
367c478bd9Sstevel@tonic-gate #include <syslog.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifdef __STDC__
397c478bd9Sstevel@tonic-gate #define	SIG_PF void(*)(int)
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifdef DEBUG
437c478bd9Sstevel@tonic-gate #define	RPC_SVC_FG
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate int _rpcpmstart;		/* Started by a port monitor ? */
487c478bd9Sstevel@tonic-gate int _rpcfdtype;			/* Whether Stream or Datagram ? */
497c478bd9Sstevel@tonic-gate int _rpcsvcdirty;		/* Still serving ? */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static void _msgout(/*char *msg*/);
527c478bd9Sstevel@tonic-gate static void closedown();
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate extern void rstatprog_4(/*struct svc_req *rqstp, SVCXPRT *transp*/);
557c478bd9Sstevel@tonic-gate extern void rstatprog_3(/*struct svc_req *rqstp, SVCXPRT *transp*/);
567c478bd9Sstevel@tonic-gate extern void rstatprog_2(/*struct svc_req *rqstp, SVCXPRT *transp*/);
577c478bd9Sstevel@tonic-gate 
58*49e7ca49Speteh int
main(int argc,char * argv[])59*49e7ca49Speteh main(int argc, char *argv[])
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	pid_t pid;
627c478bd9Sstevel@tonic-gate 	int i;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * If stdin looks like a TLI endpoint, we assume
667c478bd9Sstevel@tonic-gate 	 * that we were started by a port monitor. If
677c478bd9Sstevel@tonic-gate 	 * t_getstate fails with TBADF, this is not a
687c478bd9Sstevel@tonic-gate 	 * TLI endpoint.
697c478bd9Sstevel@tonic-gate 	 */
707c478bd9Sstevel@tonic-gate 	if (t_getstate(0) != -1 || t_errno != TBADF) {
717c478bd9Sstevel@tonic-gate 		char *netid;
727c478bd9Sstevel@tonic-gate 		struct netconfig *nconf = NULL;
737c478bd9Sstevel@tonic-gate 		SVCXPRT *transp;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		_rpcpmstart = 1;
767c478bd9Sstevel@tonic-gate 		openlog("rstatd", LOG_PID, LOG_DAEMON);
777c478bd9Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) == NULL) {
787c478bd9Sstevel@tonic-gate #ifdef DEBUG
797c478bd9Sstevel@tonic-gate 			_msgout("cannot get transport name");
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 		} else if ((nconf = getnetconfigent(netid)) == NULL) {
827c478bd9Sstevel@tonic-gate #ifdef DEBUG
837c478bd9Sstevel@tonic-gate 			_msgout("cannot get transport info");
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
877c478bd9Sstevel@tonic-gate 			_msgout("cannot create server handle");
887c478bd9Sstevel@tonic-gate 			exit(1);
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 		if (nconf)
917c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
927c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_VAR, rstatprog_4,
937c478bd9Sstevel@tonic-gate 		    0)) {
94*49e7ca49Speteh 			_msgout("unable to register "
95*49e7ca49Speteh 			    "(RSTATPROG, RSTATVERS_VAR).");
967c478bd9Sstevel@tonic-gate 			exit(1);
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_TIME, rstatprog_3,
997c478bd9Sstevel@tonic-gate 		    0)) {
100*49e7ca49Speteh 			_msgout("unable to register "
101*49e7ca49Speteh 			    "(RSTATPROG, RSTATVERS_TIME).");
1027c478bd9Sstevel@tonic-gate 			exit(1);
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_SWTCH, rstatprog_2,
1057c478bd9Sstevel@tonic-gate 		    0)) {
106*49e7ca49Speteh 			_msgout("unable to register "
107*49e7ca49Speteh 			    "(RSTATPROG, RSTATVERS_SWTCH).");
1087c478bd9Sstevel@tonic-gate 			exit(1);
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		svc_run();
1117c478bd9Sstevel@tonic-gate 		exit(1);
1127c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
113*49e7ca49Speteh 	} else {
1147c478bd9Sstevel@tonic-gate #ifndef RPC_SVC_FG
1157c478bd9Sstevel@tonic-gate 		pid = fork();
1167c478bd9Sstevel@tonic-gate 		if (pid < 0) {
1177c478bd9Sstevel@tonic-gate 			perror("cannot fork");
1187c478bd9Sstevel@tonic-gate 			exit(1);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 		if (pid)
1217c478bd9Sstevel@tonic-gate 			exit(0);
1227c478bd9Sstevel@tonic-gate 		closefrom(0);
1237c478bd9Sstevel@tonic-gate 		i = open("/dev/console", 2);
1247c478bd9Sstevel@tonic-gate 		(void) dup2(i, 1);
1257c478bd9Sstevel@tonic-gate 		(void) dup2(i, 2);
1267c478bd9Sstevel@tonic-gate 		setsid();
1277c478bd9Sstevel@tonic-gate 		openlog("rstatd", LOG_PID, LOG_DAEMON);
1287c478bd9Sstevel@tonic-gate #endif
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "datagram_v")) {
131*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_VAR) "
132*49e7ca49Speteh 		    "for datagram_v.");
1337c478bd9Sstevel@tonic-gate 		exit(1);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME,
1367c478bd9Sstevel@tonic-gate 	    "datagram_v")) {
137*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_TIME) "
138*49e7ca49Speteh 		    "for datagram_v.");
1397c478bd9Sstevel@tonic-gate 		exit(1);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "circuit_v")) {
142*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_VAR) "
143*49e7ca49Speteh 		    "for circuit_v.");
1447c478bd9Sstevel@tonic-gate 		exit(1);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME, "circuit_v")) {
147*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_TIME) "
148*49e7ca49Speteh 		    "for circuit_v.");
1497c478bd9Sstevel@tonic-gate 		exit(1);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * V2 supported on datagram transports *only*
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (!svc_create(rstatprog_2, RSTATPROG, RSTATVERS_SWTCH,
1567c478bd9Sstevel@tonic-gate 	    "datagram_v")) {
157*49e7ca49Speteh 		_msgout("unable to create (RSTATPROG, RSTATVERS_SWTCH) "
158*49e7ca49Speteh 		    "for datagram_v.");
1597c478bd9Sstevel@tonic-gate 		exit(1);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	svc_run();
1637c478bd9Sstevel@tonic-gate 	_msgout("svc_run returned");
164*49e7ca49Speteh 	return (1);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
167*49e7ca49Speteh static void
_msgout(msg)168*49e7ca49Speteh _msgout(msg)
1697c478bd9Sstevel@tonic-gate 	char *msg;
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate #ifdef RPC_SVC_FG
1727c478bd9Sstevel@tonic-gate 	if (_rpcpmstart)
1737c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, msg);
1747c478bd9Sstevel@tonic-gate 	else
1757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", msg);
1767c478bd9Sstevel@tonic-gate #else
1777c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, msg);
1787c478bd9Sstevel@tonic-gate #endif
1797c478bd9Sstevel@tonic-gate }
180