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