xref: /titanic_44/usr/src/cmd/ttymon/tmsac.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <errno.h>
36*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
37*7c478bd9Sstevel@tonic-gate #include <signal.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <unistd.h>
40*7c478bd9Sstevel@tonic-gate #include "ttymon.h"
41*7c478bd9Sstevel@tonic-gate #include "tmextern.h"
42*7c478bd9Sstevel@tonic-gate #include "sac.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  *	openpid	- open the pid and put ttymon's pid in it
46*7c478bd9Sstevel@tonic-gate  *		- put an advisory lock on the file
47*7c478bd9Sstevel@tonic-gate  *		- to prevent another instance of ttymon in same directory
48*7c478bd9Sstevel@tonic-gate  *		- SAC also makes use of the lock
49*7c478bd9Sstevel@tonic-gate  *		- fd 0 is reserved for pid file
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate void
openpid()52*7c478bd9Sstevel@tonic-gate openpid()
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	extern	int Lckfd;
55*7c478bd9Sstevel@tonic-gate 	char lockbuf[16];	/* large enough for a PID string */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	(void)close(0);
58*7c478bd9Sstevel@tonic-gate 	/* open for read first, otherwise, may delete the pid already there*/
59*7c478bd9Sstevel@tonic-gate 	if ((Lckfd = open(PIDFILE, O_RDONLY)) != -1) {
60*7c478bd9Sstevel@tonic-gate 	     	if (lockf(Lckfd, F_TEST, 0L) == -1)
61*7c478bd9Sstevel@tonic-gate 			fatal("pid file is locked. ttymon may already be "
62*7c478bd9Sstevel@tonic-gate 			    "running!");
63*7c478bd9Sstevel@tonic-gate 		(void)close(Lckfd);
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	if ((Lckfd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0644 )) != 0)
67*7c478bd9Sstevel@tonic-gate 		fatal("open pid file failed: %s", strerror(errno));
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (lockf(Lckfd, F_LOCK, 0L) == -1)
70*7c478bd9Sstevel@tonic-gate 		fatal("lock pid file failed: %s", strerror(errno));
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	(void) snprintf(lockbuf, sizeof (lockbuf), "%ld", getpid());
73*7c478bd9Sstevel@tonic-gate 	(void) write(Lckfd, lockbuf, strlen(lockbuf) + 1);
74*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
75*7c478bd9Sstevel@tonic-gate 	log("fd(pid)\t = %d", Lckfd);
76*7c478bd9Sstevel@tonic-gate #endif
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate  * openpipes() -- open pmpipe and sacpipe to communicate with SAC
81*7c478bd9Sstevel@tonic-gate  *	       -- Pfd, Sfd are global file descriptors for pmpipe, sacpipe
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate void
openpipes()85*7c478bd9Sstevel@tonic-gate openpipes()
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	extern	int Pfd, Sfd;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	Sfd = open(SACPIPE, O_WRONLY);
90*7c478bd9Sstevel@tonic-gate 	if (Sfd < 0)
91*7c478bd9Sstevel@tonic-gate 		fatal("open sacpipe failed: %s", strerror(errno));
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	Pfd = open(PMPIPE, O_RDWR|O_NONBLOCK);
94*7c478bd9Sstevel@tonic-gate 	if (Pfd < 0)
95*7c478bd9Sstevel@tonic-gate 		fatal("open pmpipe failed: %s", strerror(errno));
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
98*7c478bd9Sstevel@tonic-gate 	log("fd(sacpipe)\t = %d",Sfd);
99*7c478bd9Sstevel@tonic-gate 	log("fd(pmpipe)\t = %d",Pfd);
100*7c478bd9Sstevel@tonic-gate #endif
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate /*
104*7c478bd9Sstevel@tonic-gate  * remove_env(env) - remove an environment variable from the environment
105*7c478bd9Sstevel@tonic-gate  */
106*7c478bd9Sstevel@tonic-gate static	void
remove_env(env)107*7c478bd9Sstevel@tonic-gate remove_env(env)
108*7c478bd9Sstevel@tonic-gate char	*env;
109*7c478bd9Sstevel@tonic-gate {
110*7c478bd9Sstevel@tonic-gate 	extern	char	**environ;
111*7c478bd9Sstevel@tonic-gate 	char	**p;
112*7c478bd9Sstevel@tonic-gate 	char	**rp = NULL;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	p = environ;
115*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
116*7c478bd9Sstevel@tonic-gate 		return;
117*7c478bd9Sstevel@tonic-gate 	while (*p) {
118*7c478bd9Sstevel@tonic-gate 		if (strncmp(*p, env,strlen(env)) == 0)
119*7c478bd9Sstevel@tonic-gate 			rp = p;
120*7c478bd9Sstevel@tonic-gate 		p++;
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	if (rp) {
123*7c478bd9Sstevel@tonic-gate 		*rp = *--p;
124*7c478bd9Sstevel@tonic-gate 		*p = NULL;
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate  * get_environ() -- get env variables PMTAG, ISTATE
130*7c478bd9Sstevel@tonic-gate  *		 -- set global variables Tag, State
131*7c478bd9Sstevel@tonic-gate  */
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate void
get_environ()134*7c478bd9Sstevel@tonic-gate get_environ()
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	extern 	char State, *Istate, *Tag;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	if ((Tag = getenv("PMTAG")) == NULL)
139*7c478bd9Sstevel@tonic-gate 		fatal("PMTAG is missing");
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	if ((Istate = getenv("ISTATE")) == NULL)
142*7c478bd9Sstevel@tonic-gate 		fatal("ISTATE is missing");
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	State = (!strcmp(Istate, "enabled")) ? PM_ENABLED : PM_DISABLED;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	/*
147*7c478bd9Sstevel@tonic-gate 	 * remove the environment variables so they will not
148*7c478bd9Sstevel@tonic-gate 	 * be passed to the children
149*7c478bd9Sstevel@tonic-gate 	 */
150*7c478bd9Sstevel@tonic-gate 	remove_env("ISTATE");
151*7c478bd9Sstevel@tonic-gate 	remove_env("PMTAG");
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate  * sacpoll	- the event handler when sac event is posted
156*7c478bd9Sstevel@tonic-gate  */
157*7c478bd9Sstevel@tonic-gate void
sacpoll()158*7c478bd9Sstevel@tonic-gate sacpoll()
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate 	int 	ret;
161*7c478bd9Sstevel@tonic-gate 	char	oldState;
162*7c478bd9Sstevel@tonic-gate 	struct 	sacmsg sacmsg;
163*7c478bd9Sstevel@tonic-gate 	struct 	pmmsg pmmsg;
164*7c478bd9Sstevel@tonic-gate 	sigset_t	cset;
165*7c478bd9Sstevel@tonic-gate 	sigset_t	tset;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
168*7c478bd9Sstevel@tonic-gate 	debug("in sacpoll");
169*7c478bd9Sstevel@tonic-gate #endif
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	/* we don't want to be interrupted by sigchild now */
172*7c478bd9Sstevel@tonic-gate 	(void)sigprocmask(SIG_SETMASK, NULL, &cset);
173*7c478bd9Sstevel@tonic-gate 	tset = cset;
174*7c478bd9Sstevel@tonic-gate 	(void)sigaddset(&tset, SIGCLD);
175*7c478bd9Sstevel@tonic-gate 	(void)sigprocmask(SIG_SETMASK, &tset, NULL);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	/*
178*7c478bd9Sstevel@tonic-gate 	 *	read sac messages, one at a time until no message
179*7c478bd9Sstevel@tonic-gate 	 *	is left on the pipe.
180*7c478bd9Sstevel@tonic-gate 	 *	the pipe is open with O_NONBLOCK, read will return -1
181*7c478bd9Sstevel@tonic-gate 	 *	and errno = EAGAIN if nothing is on the pipe
182*7c478bd9Sstevel@tonic-gate 	 */
183*7c478bd9Sstevel@tonic-gate 	for (;;) {
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 		ret = read(Pfd, &sacmsg, sizeof(sacmsg));
186*7c478bd9Sstevel@tonic-gate 		if (ret < 0) {
187*7c478bd9Sstevel@tonic-gate 			switch(errno) {
188*7c478bd9Sstevel@tonic-gate 			case EAGAIN:
189*7c478bd9Sstevel@tonic-gate 				/* no more data on the pipe */
190*7c478bd9Sstevel@tonic-gate 				(void)sigprocmask(SIG_SETMASK, &cset, NULL);
191*7c478bd9Sstevel@tonic-gate 				return;
192*7c478bd9Sstevel@tonic-gate 			case EINTR:
193*7c478bd9Sstevel@tonic-gate 				break;
194*7c478bd9Sstevel@tonic-gate 			default:
195*7c478bd9Sstevel@tonic-gate 				fatal("pmpipe read failed: %s",
196*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
197*7c478bd9Sstevel@tonic-gate 				break;  /*NOTREACHED*/
198*7c478bd9Sstevel@tonic-gate 			}
199*7c478bd9Sstevel@tonic-gate 		}
200*7c478bd9Sstevel@tonic-gate 		else if (ret == 0) {
201*7c478bd9Sstevel@tonic-gate 			/* no more data on the pipe */
202*7c478bd9Sstevel@tonic-gate 			(void)sigprocmask(SIG_SETMASK, &cset, NULL);
203*7c478bd9Sstevel@tonic-gate 			return;
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 		else {
206*7c478bd9Sstevel@tonic-gate 			pmmsg.pm_size = 0;
207*7c478bd9Sstevel@tonic-gate 			(void) strcpy(pmmsg.pm_tag, Tag);
208*7c478bd9Sstevel@tonic-gate 			pmmsg.pm_maxclass = TM_MAXCLASS;
209*7c478bd9Sstevel@tonic-gate 			pmmsg.pm_type = PM_STATUS;
210*7c478bd9Sstevel@tonic-gate 			switch(sacmsg.sc_type) {
211*7c478bd9Sstevel@tonic-gate 			case SC_STATUS:
212*7c478bd9Sstevel@tonic-gate 				break;
213*7c478bd9Sstevel@tonic-gate 			case SC_ENABLE:
214*7c478bd9Sstevel@tonic-gate 				log("Got SC_ENABLE message");
215*7c478bd9Sstevel@tonic-gate 				oldState = State;
216*7c478bd9Sstevel@tonic-gate 				State = PM_ENABLED;
217*7c478bd9Sstevel@tonic-gate 				if (State != oldState) {
218*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
219*7c478bd9Sstevel@tonic-gate 					debug("state changed to ENABLED");
220*7c478bd9Sstevel@tonic-gate #endif
221*7c478bd9Sstevel@tonic-gate 					state_change();
222*7c478bd9Sstevel@tonic-gate 				}
223*7c478bd9Sstevel@tonic-gate 				break;
224*7c478bd9Sstevel@tonic-gate 			case SC_DISABLE:
225*7c478bd9Sstevel@tonic-gate 				log("Got SC_DISABLE message");
226*7c478bd9Sstevel@tonic-gate 				oldState = State;
227*7c478bd9Sstevel@tonic-gate 				State = PM_DISABLED;
228*7c478bd9Sstevel@tonic-gate 				if (State != oldState) {
229*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
230*7c478bd9Sstevel@tonic-gate 					debug("state changed to DISABLED");
231*7c478bd9Sstevel@tonic-gate #endif
232*7c478bd9Sstevel@tonic-gate 					state_change();
233*7c478bd9Sstevel@tonic-gate 				}
234*7c478bd9Sstevel@tonic-gate 				break;
235*7c478bd9Sstevel@tonic-gate 			case SC_READDB:
236*7c478bd9Sstevel@tonic-gate 				log("Got SC_READDB message");
237*7c478bd9Sstevel@tonic-gate 				Reread_flag = 1;
238*7c478bd9Sstevel@tonic-gate 				break;
239*7c478bd9Sstevel@tonic-gate 			default:
240*7c478bd9Sstevel@tonic-gate 				log("Got unknown message %d", sacmsg.sc_type);
241*7c478bd9Sstevel@tonic-gate 				pmmsg.pm_type = PM_UNKNOWN;
242*7c478bd9Sstevel@tonic-gate 				break;
243*7c478bd9Sstevel@tonic-gate 			} /* end switch */
244*7c478bd9Sstevel@tonic-gate 			pmmsg.pm_state = State;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 			while (write(Sfd, &pmmsg, sizeof(pmmsg)) != sizeof(pmmsg)) {
247*7c478bd9Sstevel@tonic-gate 				if (errno == EINTR)
248*7c478bd9Sstevel@tonic-gate 					continue;
249*7c478bd9Sstevel@tonic-gate 				log("sanity response to SAC failed: %s",
250*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
251*7c478bd9Sstevel@tonic-gate 				break;
252*7c478bd9Sstevel@tonic-gate 			}
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 	} /* end for loop */
255*7c478bd9Sstevel@tonic-gate }
256