xref: /titanic_51/usr/src/cmd/rpcsvc/rstat_main.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1991-2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "rstat.h"
30 #include "rstat_v2.h"
31 #include <stdio.h>
32 #include <stdlib.h> /* getenv, exit */
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <memory.h>
36 #include <stropts.h>
37 #include <netconfig.h>
38 #include <syslog.h>
39 
40 #ifdef __STDC__
41 #define	SIG_PF void(*)(int)
42 #endif
43 
44 #ifdef DEBUG
45 #define	RPC_SVC_FG
46 #endif
47 
48 
49 int _rpcpmstart;		/* Started by a port monitor ? */
50 int _rpcfdtype;			/* Whether Stream or Datagram ? */
51 int _rpcsvcdirty;		/* Still serving ? */
52 
53 static void _msgout(/*char *msg*/);
54 static void closedown();
55 
56 extern void rstatprog_4(/*struct svc_req *rqstp, SVCXPRT *transp*/);
57 extern void rstatprog_3(/*struct svc_req *rqstp, SVCXPRT *transp*/);
58 extern void rstatprog_2(/*struct svc_req *rqstp, SVCXPRT *transp*/);
59 
60 main()
61 {
62 	pid_t pid;
63 	int i;
64 
65 	/*
66 	 * If stdin looks like a TLI endpoint, we assume
67 	 * that we were started by a port monitor. If
68 	 * t_getstate fails with TBADF, this is not a
69 	 * TLI endpoint.
70 	 */
71 	if (t_getstate(0) != -1 || t_errno != TBADF) {
72 		char *netid;
73 		struct netconfig *nconf = NULL;
74 		SVCXPRT *transp;
75 
76 		_rpcpmstart = 1;
77 		openlog("rstatd", LOG_PID, LOG_DAEMON);
78 		if ((netid = getenv("NLSPROVIDER")) == NULL) {
79 #ifdef DEBUG
80 			_msgout("cannot get transport name");
81 #endif
82 		} else if ((nconf = getnetconfigent(netid)) == NULL) {
83 #ifdef DEBUG
84 			_msgout("cannot get transport info");
85 #endif
86 		}
87 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
88 			_msgout("cannot create server handle");
89 			exit(1);
90 		}
91 		if (nconf)
92 			freenetconfigent(nconf);
93 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_VAR, rstatprog_4,
94 		    0)) {
95     _msgout("unable to register (RSTATPROG, RSTATVERS_VAR).");
96 			exit(1);
97 		}
98 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_TIME, rstatprog_3,
99 		    0)) {
100     _msgout("unable to register (RSTATPROG, RSTATVERS_TIME).");
101 			exit(1);
102 		}
103 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_SWTCH, rstatprog_2,
104 		    0)) {
105     _msgout("unable to register (RSTATPROG, RSTATVERS_SWTCH).");
106 			exit(1);
107 		}
108 		svc_run();
109 		exit(1);
110 		/* NOTREACHED */
111 	}
112 	else {
113 #ifndef RPC_SVC_FG
114 		pid = fork();
115 		if (pid < 0) {
116 			perror("cannot fork");
117 			exit(1);
118 		}
119 		if (pid)
120 			exit(0);
121 		closefrom(0);
122 		i = open("/dev/console", 2);
123 		(void) dup2(i, 1);
124 		(void) dup2(i, 2);
125 		setsid();
126 		openlog("rstatd", LOG_PID, LOG_DAEMON);
127 #endif
128 	}
129 	if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "datagram_v")) {
130     _msgout("unable to create (RSTATPROG, RSTATVERS_VAR) for datagram_v.");
131 		exit(1);
132 	}
133 	if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME,
134 	    "datagram_v")) {
135     _msgout("unable to create (RSTATPROG, RSTATVERS_TIME) for datagram_v.");
136 		exit(1);
137 	}
138 	if (!svc_create(rstatprog_4, RSTATPROG, RSTATVERS_VAR, "circuit_v")) {
139     _msgout("unable to create (RSTATPROG, RSTATVERS_VAR) for circuit_v.");
140 		exit(1);
141 	}
142 	if (!svc_create(rstatprog_3, RSTATPROG, RSTATVERS_TIME, "circuit_v")) {
143     _msgout("unable to create (RSTATPROG, RSTATVERS_TIME) for circuit_v.");
144 		exit(1);
145 	}
146 
147 	/*
148 	 * V2 supported on datagram transports *only*
149 	 */
150 	if (!svc_create(rstatprog_2, RSTATPROG, RSTATVERS_SWTCH,
151 	    "datagram_v")) {
152     _msgout("unable to create (RSTATPROG, RSTATVERS_SWTCH) for datagram_v.");
153 		exit(1);
154 	}
155 
156 	svc_run();
157 	_msgout("svc_run returned");
158 	exit(1);
159 	/* NOTREACHED */
160 }
161 
162 static
163 void _msgout(msg)
164 	char *msg;
165 {
166 #ifdef RPC_SVC_FG
167 	if (_rpcpmstart)
168 		syslog(LOG_ERR, msg);
169 	else
170 		(void) fprintf(stderr, "%s\n", msg);
171 #else
172 	syslog(LOG_ERR, msg);
173 #endif
174 }
175