xref: /onnv-gate/usr/src/cmd/saf/misc.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"       /* SVr4.0 1.9*/
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate # include <stdio.h>
30*0Sstevel@tonic-gate # include <unistd.h>
31*0Sstevel@tonic-gate # include <fcntl.h>
32*0Sstevel@tonic-gate # include <sys/types.h>
33*0Sstevel@tonic-gate # include <sys/stropts.h>
34*0Sstevel@tonic-gate # include <signal.h>
35*0Sstevel@tonic-gate # include <sys/stat.h>
36*0Sstevel@tonic-gate # include <poll.h>
37*0Sstevel@tonic-gate # include "misc.h"
38*0Sstevel@tonic-gate # include "msgs.h"
39*0Sstevel@tonic-gate # include "extern.h"
40*0Sstevel@tonic-gate # include <sac.h>
41*0Sstevel@tonic-gate # include "adm.h"
42*0Sstevel@tonic-gate # include "structs.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * findpm - find a port monitor entry
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  *	args:	tag - tag of desired port monitor
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate struct sactab *
53*0Sstevel@tonic-gate findpm(tag)
54*0Sstevel@tonic-gate register char *tag;
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	register struct sactab *sp;	/* working pointer */
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	for (sp = Sactab; sp; sp = sp->sc_next) {
59*0Sstevel@tonic-gate 		if (!strcmp(tag, sp->sc_tag))
60*0Sstevel@tonic-gate 			return(sp);
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 	return(NULL);
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate  * sigpoll - handle messages coming in on the command pipe (SIGPOLL signal
68*0Sstevel@tonic-gate  *		handler)
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate void
73*0Sstevel@tonic-gate sigpoll()
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	struct pollfd fds;			/* array of fds to poll */
76*0Sstevel@tonic-gate 	struct admcmd cmd;			/* incoming command */
77*0Sstevel@tonic-gate 	register struct admcmd *ap = &cmd;	/* and a pointer to it */
78*0Sstevel@tonic-gate 	struct admack ack;			/* acknowledgment */
79*0Sstevel@tonic-gate 	register struct admack *ak = &ack;	/* and a pointer to it */
80*0Sstevel@tonic-gate 	register struct sactab *sp;		/* working pointer */
81*0Sstevel@tonic-gate 	struct sacmsg sacmsg;			/* message to port monitor */
82*0Sstevel@tonic-gate 	char **data;				/* "dumped" sactab */
83*0Sstevel@tonic-gate 	char *p;				/* scratch pointer */
84*0Sstevel@tonic-gate 	register int i;				/* loop control variable */
85*0Sstevel@tonic-gate 	int ret;				/* return value */
86*0Sstevel@tonic-gate 	sigset_t cset;				/* for signal handling */
87*0Sstevel@tonic-gate 	sigset_t tset;				/* for signal handling */
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate # ifdef DEBUG
90*0Sstevel@tonic-gate 	debug("in sigpoll");
91*0Sstevel@tonic-gate # endif
92*0Sstevel@tonic-gate 	fds.fd = Cfd;
93*0Sstevel@tonic-gate 	fds.events = POLLIN;
94*0Sstevel@tonic-gate 	fds.revents = 0;
95*0Sstevel@tonic-gate 	if (poll(&fds, 1, 0) < 0)
96*0Sstevel@tonic-gate 		error(E_POLL, EXIT);
97*0Sstevel@tonic-gate 	switch (fds.revents) {
98*0Sstevel@tonic-gate 	case POLLIN:
99*0Sstevel@tonic-gate 		if (read(Cfd, ap, sizeof(struct admcmd)) < 0) {
100*0Sstevel@tonic-gate 			error(E_READ, EXIT);
101*0Sstevel@tonic-gate 		}
102*0Sstevel@tonic-gate 		switch (ap->ac_mtype) {
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate  * request to start a port monitor
106*0Sstevel@tonic-gate  */
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 		case AC_START:
109*0Sstevel@tonic-gate # ifdef DEBUG
110*0Sstevel@tonic-gate 			(void) sprintf(Scratch, "Got AC_START for <%s>", ap->ac_tag);
111*0Sstevel@tonic-gate 			log(Scratch);
112*0Sstevel@tonic-gate # endif
113*0Sstevel@tonic-gate 			if ((sp = findpm(ap->ac_tag)) == NULL) {
114*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
115*0Sstevel@tonic-gate 				ak->ak_resp = AK_NOPM;
116*0Sstevel@tonic-gate 				ak->ak_size = 0;
117*0Sstevel@tonic-gate 				sendack(ak);
118*0Sstevel@tonic-gate 				break;
119*0Sstevel@tonic-gate 			}
120*0Sstevel@tonic-gate 			switch (sp->sc_sstate) {
121*0Sstevel@tonic-gate 			case UNKNOWN:
122*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
123*0Sstevel@tonic-gate 				ak->ak_resp = AK_RECOVER;
124*0Sstevel@tonic-gate 				ak->ak_size = 0;
125*0Sstevel@tonic-gate 				sendack(ak);
126*0Sstevel@tonic-gate 				break;
127*0Sstevel@tonic-gate 			case FAILED:
128*0Sstevel@tonic-gate 			case NOTRUNNING:
129*0Sstevel@tonic-gate 				sp->sc_rscnt = 0;	/* fresh start in life */
130*0Sstevel@tonic-gate 				if (ret = startpm(sp)) {
131*0Sstevel@tonic-gate 					ak->ak_pid = ap->ac_pid;
132*0Sstevel@tonic-gate 					if (ret == -1)
133*0Sstevel@tonic-gate 						ak->ak_resp = AK_PMLOCK;
134*0Sstevel@tonic-gate 					else
135*0Sstevel@tonic-gate 						ak->ak_resp = AK_REQFAIL;
136*0Sstevel@tonic-gate 					ak->ak_size = 0;
137*0Sstevel@tonic-gate 					sendack(ak);
138*0Sstevel@tonic-gate 					break;
139*0Sstevel@tonic-gate 				}
140*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
141*0Sstevel@tonic-gate 				ak->ak_resp = AK_ACK;
142*0Sstevel@tonic-gate 				ak->ak_size = 0;
143*0Sstevel@tonic-gate 				sendack(ak);
144*0Sstevel@tonic-gate 				break;
145*0Sstevel@tonic-gate 			case ENABLED:
146*0Sstevel@tonic-gate 			case DISABLED:
147*0Sstevel@tonic-gate 			case STARTING:
148*0Sstevel@tonic-gate 			case STOPPING:
149*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
150*0Sstevel@tonic-gate 				ak->ak_resp = AK_PMRUN;
151*0Sstevel@tonic-gate 				ak->ak_size = 0;
152*0Sstevel@tonic-gate 				sendack(ak);
153*0Sstevel@tonic-gate 				break;
154*0Sstevel@tonic-gate 			}
155*0Sstevel@tonic-gate 			break;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * request to kill a port monitor
159*0Sstevel@tonic-gate  */
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 		case AC_KILL:
162*0Sstevel@tonic-gate # ifdef DEBUG
163*0Sstevel@tonic-gate 			(void) sprintf(Scratch, "Got AC_KILL for <%s>", ap->ac_tag);
164*0Sstevel@tonic-gate 			log(Scratch);
165*0Sstevel@tonic-gate # endif
166*0Sstevel@tonic-gate 			if ((sp = findpm(ap->ac_tag)) == NULL) {
167*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
168*0Sstevel@tonic-gate 				ak->ak_resp = AK_NOPM;
169*0Sstevel@tonic-gate 				ak->ak_size = 0;
170*0Sstevel@tonic-gate 				sendack(ak);
171*0Sstevel@tonic-gate 				break;
172*0Sstevel@tonic-gate 			}
173*0Sstevel@tonic-gate 			switch (sp->sc_sstate) {
174*0Sstevel@tonic-gate 			case UNKNOWN:
175*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
176*0Sstevel@tonic-gate 				ak->ak_resp = AK_RECOVER;
177*0Sstevel@tonic-gate 				ak->ak_size = 0;
178*0Sstevel@tonic-gate 				sendack(ak);
179*0Sstevel@tonic-gate 				break;
180*0Sstevel@tonic-gate 			case NOTRUNNING:
181*0Sstevel@tonic-gate 			case FAILED:
182*0Sstevel@tonic-gate 			case STOPPING:
183*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
184*0Sstevel@tonic-gate 				ak->ak_resp = AK_PMNOTRUN;
185*0Sstevel@tonic-gate 				ak->ak_size = 0;
186*0Sstevel@tonic-gate 				sendack(ak);
187*0Sstevel@tonic-gate 				break;
188*0Sstevel@tonic-gate 			case STARTING:
189*0Sstevel@tonic-gate 			case ENABLED:
190*0Sstevel@tonic-gate 			case DISABLED:
191*0Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, NULL, &cset);
192*0Sstevel@tonic-gate 				tset = cset;
193*0Sstevel@tonic-gate 				(void) sigaddset(&tset, SIGALRM);
194*0Sstevel@tonic-gate 				(void) sigaddset(&tset, SIGCLD);
195*0Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &tset, NULL);
196*0Sstevel@tonic-gate 				if (sendsig(sp, SIGTERM)) {
197*0Sstevel@tonic-gate 					(void) sprintf(Scratch, "could not send SIGTERM to <%s>", sp->sc_tag);
198*0Sstevel@tonic-gate 					log(Scratch);
199*0Sstevel@tonic-gate 					ak->ak_pid = ap->ac_pid;
200*0Sstevel@tonic-gate 					ak->ak_resp = AK_NOCONTACT;
201*0Sstevel@tonic-gate 					ak->ak_size = 0;
202*0Sstevel@tonic-gate 					sendack(ak);
203*0Sstevel@tonic-gate 					(void) sigprocmask(SIG_SETMASK, &cset, NULL);
204*0Sstevel@tonic-gate 					break;
205*0Sstevel@tonic-gate 				}
206*0Sstevel@tonic-gate 				/* signal sent ok */
207*0Sstevel@tonic-gate 				sp->sc_lstate = NOTRUNNING;
208*0Sstevel@tonic-gate 				sp->sc_sstate = NOTRUNNING;
209*0Sstevel@tonic-gate 				sp->sc_pstate = STOPPING;
210*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
211*0Sstevel@tonic-gate 				ak->ak_resp = AK_ACK;
212*0Sstevel@tonic-gate 				ak->ak_size = 0;
213*0Sstevel@tonic-gate 				sendack(ak);
214*0Sstevel@tonic-gate 				(void) sprintf(Scratch, "terminating <%s>", sp->sc_tag);
215*0Sstevel@tonic-gate 				log(Scratch);
216*0Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &cset, NULL);
217*0Sstevel@tonic-gate 				break;
218*0Sstevel@tonic-gate 			}
219*0Sstevel@tonic-gate 			break;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate /*
222*0Sstevel@tonic-gate  * request to enable a port monitor
223*0Sstevel@tonic-gate  */
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 		case AC_ENABLE:
226*0Sstevel@tonic-gate # ifdef DEBUG
227*0Sstevel@tonic-gate 			(void) sprintf(Scratch, "Got AC_ENABLE for <%s>", ap->ac_tag);
228*0Sstevel@tonic-gate 			log(Scratch);
229*0Sstevel@tonic-gate # endif
230*0Sstevel@tonic-gate 			if ((sp = findpm(ap->ac_tag)) == NULL) {
231*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
232*0Sstevel@tonic-gate 				ak->ak_resp = AK_NOPM;
233*0Sstevel@tonic-gate 				ak->ak_size = 0;
234*0Sstevel@tonic-gate 				sendack(ak);
235*0Sstevel@tonic-gate 				break;
236*0Sstevel@tonic-gate 			}
237*0Sstevel@tonic-gate 			switch (sp->sc_sstate) {
238*0Sstevel@tonic-gate 			case UNKNOWN:
239*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
240*0Sstevel@tonic-gate 				ak->ak_resp = AK_RECOVER;
241*0Sstevel@tonic-gate 				ak->ak_size = 0;
242*0Sstevel@tonic-gate 				sendack(ak);
243*0Sstevel@tonic-gate 				break;
244*0Sstevel@tonic-gate 			case NOTRUNNING:
245*0Sstevel@tonic-gate 			case FAILED:
246*0Sstevel@tonic-gate 			case STOPPING:
247*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
248*0Sstevel@tonic-gate 				ak->ak_resp = AK_PMNOTRUN;
249*0Sstevel@tonic-gate 				ak->ak_size = 0;
250*0Sstevel@tonic-gate 				sendack(ak);
251*0Sstevel@tonic-gate 				break;
252*0Sstevel@tonic-gate 			case STARTING:
253*0Sstevel@tonic-gate 			case DISABLED:
254*0Sstevel@tonic-gate 				sacmsg.sc_type = SC_ENABLE;
255*0Sstevel@tonic-gate 				sacmsg.sc_size = 0;
256*0Sstevel@tonic-gate 				sp->sc_sstate = ENABLED;
257*0Sstevel@tonic-gate 				sp->sc_lstate = ENABLED;
258*0Sstevel@tonic-gate 				sendpmmsg(sp, &sacmsg);
259*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
260*0Sstevel@tonic-gate 				ak->ak_resp = AK_ACK;
261*0Sstevel@tonic-gate 				ak->ak_size = 0;
262*0Sstevel@tonic-gate 				sendack(ak);
263*0Sstevel@tonic-gate 				break;
264*0Sstevel@tonic-gate 			case ENABLED:
265*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
266*0Sstevel@tonic-gate 				ak->ak_resp = AK_ACK;
267*0Sstevel@tonic-gate 				ak->ak_size = 0;
268*0Sstevel@tonic-gate 				sendack(ak);
269*0Sstevel@tonic-gate 				break;
270*0Sstevel@tonic-gate 			}
271*0Sstevel@tonic-gate 			break;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate /*
274*0Sstevel@tonic-gate  * request to disable a port monitor
275*0Sstevel@tonic-gate  */
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 		case AC_DISABLE:
278*0Sstevel@tonic-gate # ifdef DEBUG
279*0Sstevel@tonic-gate 			(void) sprintf(Scratch, "Got AC_DISABLE for <%s>", ap->ac_tag);
280*0Sstevel@tonic-gate 			log(Scratch);
281*0Sstevel@tonic-gate # endif
282*0Sstevel@tonic-gate 			if ((sp = findpm(ap->ac_tag)) == NULL) {
283*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
284*0Sstevel@tonic-gate 				ak->ak_resp = AK_NOPM;
285*0Sstevel@tonic-gate 				ak->ak_size = 0;
286*0Sstevel@tonic-gate 				sendack(ak);
287*0Sstevel@tonic-gate 				break;
288*0Sstevel@tonic-gate 			}
289*0Sstevel@tonic-gate 			switch (sp->sc_sstate) {
290*0Sstevel@tonic-gate 			case UNKNOWN:
291*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
292*0Sstevel@tonic-gate 				ak->ak_resp = AK_RECOVER;
293*0Sstevel@tonic-gate 				ak->ak_size = 0;
294*0Sstevel@tonic-gate 				sendack(ak);
295*0Sstevel@tonic-gate 				break;
296*0Sstevel@tonic-gate 			case NOTRUNNING:
297*0Sstevel@tonic-gate 			case FAILED:
298*0Sstevel@tonic-gate 			case STOPPING:
299*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
300*0Sstevel@tonic-gate 				ak->ak_resp = AK_PMNOTRUN;
301*0Sstevel@tonic-gate 				ak->ak_size = 0;
302*0Sstevel@tonic-gate 				sendack(ak);
303*0Sstevel@tonic-gate 				break;
304*0Sstevel@tonic-gate 			case STARTING:
305*0Sstevel@tonic-gate 			case ENABLED:
306*0Sstevel@tonic-gate 				sacmsg.sc_type = SC_DISABLE;
307*0Sstevel@tonic-gate 				sacmsg.sc_size = 0;
308*0Sstevel@tonic-gate 				sp->sc_sstate = DISABLED;
309*0Sstevel@tonic-gate 				sp->sc_lstate = DISABLED;
310*0Sstevel@tonic-gate 				sendpmmsg(sp, &sacmsg);
311*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
312*0Sstevel@tonic-gate 				ak->ak_resp = AK_ACK;
313*0Sstevel@tonic-gate 				ak->ak_size = 0;
314*0Sstevel@tonic-gate 				sendack(ak);
315*0Sstevel@tonic-gate 				break;
316*0Sstevel@tonic-gate 			case DISABLED:
317*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
318*0Sstevel@tonic-gate 				ak->ak_resp = AK_ACK;
319*0Sstevel@tonic-gate 				ak->ak_size = 0;
320*0Sstevel@tonic-gate 				sendack(ak);
321*0Sstevel@tonic-gate 				break;
322*0Sstevel@tonic-gate 			}
323*0Sstevel@tonic-gate 			break;
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate  * request for port monitor status information
327*0Sstevel@tonic-gate  */
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 		case AC_STATUS:
330*0Sstevel@tonic-gate # ifdef DEBUG
331*0Sstevel@tonic-gate 			log("Got AC_STATUS");
332*0Sstevel@tonic-gate # endif
333*0Sstevel@tonic-gate 			/* get all the info in one convenient place */
334*0Sstevel@tonic-gate 			data = dump_table();
335*0Sstevel@tonic-gate 			if ((data == NULL) && (Nentries > 0)) {
336*0Sstevel@tonic-gate 				/* something bad happened in dump_table */
337*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
338*0Sstevel@tonic-gate 				ak->ak_resp = AK_REQFAIL;
339*0Sstevel@tonic-gate 				ak->ak_size = 0;
340*0Sstevel@tonic-gate 				sendack(ak);
341*0Sstevel@tonic-gate 				break;
342*0Sstevel@tonic-gate 			}
343*0Sstevel@tonic-gate 			/* count how big it is */
344*0Sstevel@tonic-gate 			ak->ak_size = 0;
345*0Sstevel@tonic-gate 			for (i = 0; i < Nentries; ++i)
346*0Sstevel@tonic-gate 				ak->ak_size += strlen(data[i]);
347*0Sstevel@tonic-gate # ifdef DEBUG
348*0Sstevel@tonic-gate 			(void) sprintf(Scratch, "ak_size is %d", ak->ak_size);
349*0Sstevel@tonic-gate 			debug(Scratch);
350*0Sstevel@tonic-gate # endif
351*0Sstevel@tonic-gate 			/* get a contiguous chunk */
352*0Sstevel@tonic-gate 			if ((p = malloc((unsigned) (ak->ak_size + 1))) == NULL) {
353*0Sstevel@tonic-gate 				error(E_MALLOC, CONT);
354*0Sstevel@tonic-gate 				for (i = 0; i < Nentries; ++i)
355*0Sstevel@tonic-gate 					free(data[i]);
356*0Sstevel@tonic-gate 				free((char *) data);
357*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
358*0Sstevel@tonic-gate 				ak->ak_resp = AK_REQFAIL;
359*0Sstevel@tonic-gate 				ak->ak_size = 0;
360*0Sstevel@tonic-gate 				sendack(ak);
361*0Sstevel@tonic-gate 				break;
362*0Sstevel@tonic-gate 			}
363*0Sstevel@tonic-gate 			/* condense the data into the contiguous chunk */
364*0Sstevel@tonic-gate 			*p = '\0';
365*0Sstevel@tonic-gate 			for (i = 0; i < Nentries; ++i) {
366*0Sstevel@tonic-gate 				(void) strcat(p, data[i]);
367*0Sstevel@tonic-gate 				free(data[i]);
368*0Sstevel@tonic-gate 			}
369*0Sstevel@tonic-gate # ifdef DEBUG
370*0Sstevel@tonic-gate 			debug(p);
371*0Sstevel@tonic-gate # endif
372*0Sstevel@tonic-gate 			if (data)
373*0Sstevel@tonic-gate 				free((char *) data);
374*0Sstevel@tonic-gate 			/* ak->ak_size was set above */
375*0Sstevel@tonic-gate 			ak->ak_pid = ap->ac_pid;
376*0Sstevel@tonic-gate 			ak->ak_resp = AK_ACK;
377*0Sstevel@tonic-gate 			sendack(ak);
378*0Sstevel@tonic-gate 			if (ak->ak_size)
379*0Sstevel@tonic-gate 				if (write(Cfd, p, (unsigned) ak->ak_size) != ak->ak_size)
380*0Sstevel@tonic-gate 					log("could not send info");
381*0Sstevel@tonic-gate 			free(p);
382*0Sstevel@tonic-gate 			break;
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate /*
385*0Sstevel@tonic-gate  * request for sac to read sactab
386*0Sstevel@tonic-gate  */
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 		case AC_SACREAD:
389*0Sstevel@tonic-gate # ifdef DEBUG
390*0Sstevel@tonic-gate 			log("Got AC_SACREAD");
391*0Sstevel@tonic-gate # endif
392*0Sstevel@tonic-gate 			ak->ak_pid = ap->ac_pid;
393*0Sstevel@tonic-gate 			ak->ak_resp = AK_ACK;
394*0Sstevel@tonic-gate 			ak->ak_size = 0;
395*0Sstevel@tonic-gate 			read_table(TRUE);
396*0Sstevel@tonic-gate 			sendack(ak);
397*0Sstevel@tonic-gate 			break;
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate /*
400*0Sstevel@tonic-gate  * request for port monitor to read _pmtab
401*0Sstevel@tonic-gate  */
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 		case AC_PMREAD:
404*0Sstevel@tonic-gate # ifdef DEBUG
405*0Sstevel@tonic-gate 			(void) sprintf(Scratch, "Got AC_PMREAD for <%s>", ap->ac_tag);
406*0Sstevel@tonic-gate 			log(Scratch);
407*0Sstevel@tonic-gate # endif
408*0Sstevel@tonic-gate 			if ((sp = findpm(ap->ac_tag)) == NULL) {
409*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
410*0Sstevel@tonic-gate 				ak->ak_resp = AK_NOPM;
411*0Sstevel@tonic-gate 				ak->ak_size = 0;
412*0Sstevel@tonic-gate 				sendack(ak);
413*0Sstevel@tonic-gate 				break;
414*0Sstevel@tonic-gate 			}
415*0Sstevel@tonic-gate 			switch (sp->sc_sstate) {
416*0Sstevel@tonic-gate 			case UNKNOWN:
417*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
418*0Sstevel@tonic-gate 				ak->ak_resp = AK_RECOVER;
419*0Sstevel@tonic-gate 				ak->ak_size = 0;
420*0Sstevel@tonic-gate 				sendack(ak);
421*0Sstevel@tonic-gate 				break;
422*0Sstevel@tonic-gate 			case NOTRUNNING:
423*0Sstevel@tonic-gate 			case FAILED:
424*0Sstevel@tonic-gate 			case STOPPING:
425*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
426*0Sstevel@tonic-gate 				ak->ak_resp = AK_PMNOTRUN;
427*0Sstevel@tonic-gate 				ak->ak_size = 0;
428*0Sstevel@tonic-gate 				sendack(ak);
429*0Sstevel@tonic-gate 				break;
430*0Sstevel@tonic-gate 			case STARTING:
431*0Sstevel@tonic-gate 			case ENABLED:
432*0Sstevel@tonic-gate 			case DISABLED:
433*0Sstevel@tonic-gate 				sacmsg.sc_type = SC_READDB;
434*0Sstevel@tonic-gate 				sacmsg.sc_size = 0;
435*0Sstevel@tonic-gate 				sendpmmsg(sp, &sacmsg);
436*0Sstevel@tonic-gate 				ak->ak_pid = ap->ac_pid;
437*0Sstevel@tonic-gate 				ak->ak_resp = AK_ACK;
438*0Sstevel@tonic-gate 				ak->ak_size = 0;
439*0Sstevel@tonic-gate 				sendack(ak);
440*0Sstevel@tonic-gate 				break;
441*0Sstevel@tonic-gate 			}
442*0Sstevel@tonic-gate 			break;
443*0Sstevel@tonic-gate /*
444*0Sstevel@tonic-gate  * garbled message
445*0Sstevel@tonic-gate  */
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 		default:
448*0Sstevel@tonic-gate 			(void) sprintf(Scratch, "Got unknown message for <%s>", ap->ac_tag);
449*0Sstevel@tonic-gate 			log(Scratch);
450*0Sstevel@tonic-gate 			ak->ak_pid = ap->ac_pid;
451*0Sstevel@tonic-gate 			ak->ak_resp = AK_UNKNOWN;
452*0Sstevel@tonic-gate 			ak->ak_size = 0;
453*0Sstevel@tonic-gate 			sendack(ak);
454*0Sstevel@tonic-gate 			break;
455*0Sstevel@tonic-gate 		}
456*0Sstevel@tonic-gate 		break;
457*0Sstevel@tonic-gate 	default:
458*0Sstevel@tonic-gate 		error(E_POLL, EXIT);
459*0Sstevel@tonic-gate 	}
460*0Sstevel@tonic-gate }
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate /*
464*0Sstevel@tonic-gate  * sendack - send a response to the administrative command
465*0Sstevel@tonic-gate  *
466*0Sstevel@tonic-gate  *	args:	ap - pointer to acknowlegment message
467*0Sstevel@tonic-gate  */
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate void
470*0Sstevel@tonic-gate sendack(ap)
471*0Sstevel@tonic-gate struct admack *ap;
472*0Sstevel@tonic-gate {
473*0Sstevel@tonic-gate # ifdef DEBUG
474*0Sstevel@tonic-gate 	debug("in sendack");
475*0Sstevel@tonic-gate # endif
476*0Sstevel@tonic-gate 	if (write(Cfd, ap, sizeof(struct admack)) != sizeof(struct admack))
477*0Sstevel@tonic-gate 		log("Could not send ack");
478*0Sstevel@tonic-gate }
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate /*
482*0Sstevel@tonic-gate  * sendpmmsg - send a message to a PM.  Note: sc_size is always 0 in
483*0Sstevel@tonic-gate  *	       this version so just send the header.
484*0Sstevel@tonic-gate  *
485*0Sstevel@tonic-gate  *	args:	sp - pointer to sac's port monitor information for
486*0Sstevel@tonic-gate  *		     designated port monitor
487*0Sstevel@tonic-gate  *		sm - pointer to message to send
488*0Sstevel@tonic-gate  */
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate void
491*0Sstevel@tonic-gate sendpmmsg(sp, sm)
492*0Sstevel@tonic-gate register struct sactab *sp;
493*0Sstevel@tonic-gate register struct sacmsg *sm;
494*0Sstevel@tonic-gate {
495*0Sstevel@tonic-gate 	char buf[SIZE];			/* scratch buffer */
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate # ifdef DEBUG
498*0Sstevel@tonic-gate 	debug("in sendpmmsg");
499*0Sstevel@tonic-gate # endif
500*0Sstevel@tonic-gate 	if (write(sp->sc_fd, sm, sizeof(struct sacmsg)) != sizeof(struct sacmsg)) {
501*0Sstevel@tonic-gate 		(void) sprintf(buf, "message to <%s> failed", sp->sc_tag);
502*0Sstevel@tonic-gate 		log(buf);
503*0Sstevel@tonic-gate 	}
504*0Sstevel@tonic-gate }
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate /*
507*0Sstevel@tonic-gate  * sendsig - send a signal to the port monitor
508*0Sstevel@tonic-gate  *
509*0Sstevel@tonic-gate  *	args:	sp - pointer to sac's port monitor infomation for
510*0Sstevel@tonic-gate  *		     designated port monitor
511*0Sstevel@tonic-gate  *		signo - signal number to send
512*0Sstevel@tonic-gate  */
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate sendsig(sp, signo)
516*0Sstevel@tonic-gate register struct sactab *sp;
517*0Sstevel@tonic-gate int signo;
518*0Sstevel@tonic-gate {
519*0Sstevel@tonic-gate 	pid_t pid;	/* pid of designated port monitor */
520*0Sstevel@tonic-gate 	pid_t checklock();
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate # ifdef DEBUG
523*0Sstevel@tonic-gate 	(void) sprintf(Scratch, "in sendsig - sending signo %d to %s", signo, sp->sc_tag);
524*0Sstevel@tonic-gate 	debug(Scratch);
525*0Sstevel@tonic-gate # endif
526*0Sstevel@tonic-gate 	if (pid = checklock(sp)) {
527*0Sstevel@tonic-gate 		if (kill(pid, signo) < 0) {
528*0Sstevel@tonic-gate # ifdef DEBUG
529*0Sstevel@tonic-gate 			debug("in sendsig - kill failed");
530*0Sstevel@tonic-gate # endif
531*0Sstevel@tonic-gate 			return(-1);
532*0Sstevel@tonic-gate 		}
533*0Sstevel@tonic-gate 		else
534*0Sstevel@tonic-gate 			return(0);
535*0Sstevel@tonic-gate 	}
536*0Sstevel@tonic-gate 	else {
537*0Sstevel@tonic-gate # ifdef DEBUG
538*0Sstevel@tonic-gate 		debug("in sendsig - checklock failed");
539*0Sstevel@tonic-gate # endif
540*0Sstevel@tonic-gate 		return(-1);
541*0Sstevel@tonic-gate 	}
542*0Sstevel@tonic-gate }
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate /*
546*0Sstevel@tonic-gate  * checklock - check to see if a _pid file is locked
547*0Sstevel@tonic-gate  *		if so, return pid in file, else 0
548*0Sstevel@tonic-gate  *
549*0Sstevel@tonic-gate  *	args:	sp - pointer to sac's port monitor infomation for
550*0Sstevel@tonic-gate  *		     designated port monitor
551*0Sstevel@tonic-gate  */
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate pid_t
554*0Sstevel@tonic-gate checklock(sp)
555*0Sstevel@tonic-gate register struct sactab *sp;
556*0Sstevel@tonic-gate {
557*0Sstevel@tonic-gate 	int fd;			/* scratch file descriptor */
558*0Sstevel@tonic-gate 	char buf[SIZE];		/* scratch buffer */
559*0Sstevel@tonic-gate 	int ret;		/* return value */
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate # ifdef DEBUG
562*0Sstevel@tonic-gate 	debug("in checklock");
563*0Sstevel@tonic-gate # endif
564*0Sstevel@tonic-gate 	(void) sprintf(Scratch, "%s/%s/_pid", HOME, sp->sc_tag);
565*0Sstevel@tonic-gate 	fd = open(Scratch, O_RDONLY);
566*0Sstevel@tonic-gate 	if (fd < 0) {
567*0Sstevel@tonic-gate 		(void) sprintf(Scratch, "can not open _pid file for <%s>", sp->sc_tag);
568*0Sstevel@tonic-gate 		log(Scratch);
569*0Sstevel@tonic-gate 		return((pid_t)0);
570*0Sstevel@tonic-gate 	}
571*0Sstevel@tonic-gate 	if (lockf(fd, F_TEST, 0) < 0) {
572*0Sstevel@tonic-gate 		if ((ret = read(fd, buf, SIZE - 1)) < 0) {
573*0Sstevel@tonic-gate 			(void) close(fd);
574*0Sstevel@tonic-gate 			return((pid_t)0);
575*0Sstevel@tonic-gate 		}
576*0Sstevel@tonic-gate 		(void) close(fd);
577*0Sstevel@tonic-gate 		/* in case pid wasn't null-terminated */
578*0Sstevel@tonic-gate 		buf[ret] = '\0';
579*0Sstevel@tonic-gate 		return((pid_t)atol(buf));
580*0Sstevel@tonic-gate 	}
581*0Sstevel@tonic-gate 	(void) close(fd);
582*0Sstevel@tonic-gate 	return((pid_t)0);
583*0Sstevel@tonic-gate }
584