10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*631Speteh * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*631Speteh * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include "rstat.h"
300Sstevel@tonic-gate #include "rstat_v2.h"
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h> /* getenv, exit */
330Sstevel@tonic-gate #include <signal.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <memory.h>
360Sstevel@tonic-gate #include <stropts.h>
370Sstevel@tonic-gate #include <netconfig.h>
380Sstevel@tonic-gate #include <syslog.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate #ifdef __STDC__
410Sstevel@tonic-gate #define SIG_PF void(*)(int)
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate
440Sstevel@tonic-gate #ifdef DEBUG
450Sstevel@tonic-gate #define RPC_SVC_FG
460Sstevel@tonic-gate #endif
470Sstevel@tonic-gate
480Sstevel@tonic-gate
490Sstevel@tonic-gate int _rpcpmstart; /* Started by a port monitor ? */
500Sstevel@tonic-gate int _rpcfdtype; /* Whether Stream or Datagram ? */
510Sstevel@tonic-gate int _rpcsvcdirty; /* Still serving ? */
520Sstevel@tonic-gate
530Sstevel@tonic-gate static void _msgout(/*char *msg*/);
540Sstevel@tonic-gate static void closedown();
550Sstevel@tonic-gate
560Sstevel@tonic-gate extern void rstatprog_4(/*struct svc_req *rqstp, SVCXPRT *transp*/);
570Sstevel@tonic-gate extern void rstatprog_3(/*struct svc_req *rqstp, SVCXPRT *transp*/);
580Sstevel@tonic-gate extern void rstatprog_2(/*struct svc_req *rqstp, SVCXPRT *transp*/);
590Sstevel@tonic-gate
60*631Speteh int
main(int argc,char * argv[])61*631Speteh main(int argc, char *argv[])
620Sstevel@tonic-gate {
630Sstevel@tonic-gate pid_t pid;
640Sstevel@tonic-gate int i;
650Sstevel@tonic-gate
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate * If stdin looks like a TLI endpoint, we assume
680Sstevel@tonic-gate * that we were started by a port monitor. If
690Sstevel@tonic-gate * t_getstate fails with TBADF, this is not a
700Sstevel@tonic-gate * TLI endpoint.
710Sstevel@tonic-gate */
720Sstevel@tonic-gate if (t_getstate(0) != -1 || t_errno != TBADF) {
730Sstevel@tonic-gate char *netid;
740Sstevel@tonic-gate struct netconfig *nconf = NULL;
750Sstevel@tonic-gate SVCXPRT *transp;
760Sstevel@tonic-gate
770Sstevel@tonic-gate _rpcpmstart = 1;
780Sstevel@tonic-gate openlog("rstatd", LOG_PID, LOG_DAEMON);
790Sstevel@tonic-gate if ((netid = getenv("NLSPROVIDER")) == NULL) {
800Sstevel@tonic-gate #ifdef DEBUG
810Sstevel@tonic-gate _msgout("cannot get transport name");
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate } else if ((nconf = getnetconfigent(netid)) == NULL) {
840Sstevel@tonic-gate #ifdef DEBUG
850Sstevel@tonic-gate _msgout("cannot get transport info");
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate }
880Sstevel@tonic-gate if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
890Sstevel@tonic-gate _msgout("cannot create server handle");
900Sstevel@tonic-gate exit(1);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate if (nconf)
930Sstevel@tonic-gate freenetconfigent(nconf);
940Sstevel@tonic-gate if (!svc_reg(transp, RSTATPROG, RSTATVERS_VAR, rstatprog_4,
950Sstevel@tonic-gate 0)) {
96*631Speteh _msgout("unable to register "
97*631Speteh "(RSTATPROG, RSTATVERS_VAR).");
980Sstevel@tonic-gate exit(1);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate if (!svc_reg(transp, RSTATPROG, RSTATVERS_TIME, rstatprog_3,
1010Sstevel@tonic-gate 0)) {
102*631Speteh _msgout("unable to register "
103*631Speteh "(RSTATPROG, RSTATVERS_TIME).");
1040Sstevel@tonic-gate exit(1);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate if (!svc_reg(transp, RSTATPROG, RSTATVERS_SWTCH, rstatprog_2,
1070Sstevel@tonic-gate 0)) {
108*631Speteh _msgout("unable to register "
109*631Speteh "(RSTATPROG, RSTATVERS_SWTCH).");
1100Sstevel@tonic-gate exit(1);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate svc_run();
1130Sstevel@tonic-gate exit(1);
1140Sstevel@tonic-gate /* NOTREACHED */
115*631Speteh } else {
1160Sstevel@tonic-gate #ifndef RPC_SVC_FG
1170Sstevel@tonic-gate pid = fork();
1180Sstevel@tonic-gate if (pid < 0) {
1190Sstevel@tonic-gate perror("cannot fork");
1200Sstevel@tonic-gate exit(1);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate if (pid)
1230Sstevel@tonic-gate exit(0);
1240Sstevel@tonic-gate closefrom(0);
1250Sstevel@tonic-gate i = open("/dev/console", 2);
1260Sstevel@tonic-gate (void) dup2(i, 1);
1270Sstevel@tonic-gate (void) dup2(i, 2);
1280Sstevel@tonic-gate setsid();
1290Sstevel@tonic-gate openlog("rstatd", LOG_PID, LOG_DAEMON);
1300Sstevel@tonic-gate #endif
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "datagram_v")) {
133*631Speteh _msgout("unable to create (RSTATPROG, RSTATVERS_VAR) "
134*631Speteh "for datagram_v.");
1350Sstevel@tonic-gate exit(1);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME,
1380Sstevel@tonic-gate "datagram_v")) {
139*631Speteh _msgout("unable to create (RSTATPROG, RSTATVERS_TIME) "
140*631Speteh "for datagram_v.");
1410Sstevel@tonic-gate exit(1);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "circuit_v")) {
144*631Speteh _msgout("unable to create (RSTATPROG, RSTATVERS_VAR) "
145*631Speteh "for circuit_v.");
1460Sstevel@tonic-gate exit(1);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME, "circuit_v")) {
149*631Speteh _msgout("unable to create (RSTATPROG, RSTATVERS_TIME) "
150*631Speteh "for circuit_v.");
1510Sstevel@tonic-gate exit(1);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate * V2 supported on datagram transports *only*
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate if (!svc_create(rstatprog_2, RSTATPROG, RSTATVERS_SWTCH,
1580Sstevel@tonic-gate "datagram_v")) {
159*631Speteh _msgout("unable to create (RSTATPROG, RSTATVERS_SWTCH) "
160*631Speteh "for datagram_v.");
1610Sstevel@tonic-gate exit(1);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate svc_run();
1650Sstevel@tonic-gate _msgout("svc_run returned");
166*631Speteh return (1);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
169*631Speteh static void
_msgout(msg)170*631Speteh _msgout(msg)
1710Sstevel@tonic-gate char *msg;
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate #ifdef RPC_SVC_FG
1740Sstevel@tonic-gate if (_rpcpmstart)
1750Sstevel@tonic-gate syslog(LOG_ERR, msg);
1760Sstevel@tonic-gate else
1770Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", msg);
1780Sstevel@tonic-gate #else
1790Sstevel@tonic-gate syslog(LOG_ERR, msg);
1800Sstevel@tonic-gate #endif
1810Sstevel@tonic-gate }
182