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