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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
45 * openpid - open the pid and put ttymon's pid in it
46 * - put an advisory lock on the file
47 * - to prevent another instance of ttymon in same directory
48 * - SAC also makes use of the lock
49 * - fd 0 is reserved for pid file
55 char lockbuf
[16]; /* large enough for a PID string */
58 /* open for read first, otherwise, may delete the pid already there*/
59 if ((Lckfd
= open(PIDFILE
, O_RDONLY
)) != -1) {
60 if (lockf(Lckfd
, F_TEST
, 0L) == -1)
61 fatal("pid file is locked. ttymon may already be "
66 if ((Lckfd
= open(PIDFILE
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644 )) != 0)
67 fatal("open pid file failed: %s", strerror(errno
));
69 if (lockf(Lckfd
, F_LOCK
, 0L) == -1)
70 fatal("lock pid file failed: %s", strerror(errno
));
72 (void) snprintf(lockbuf
, sizeof (lockbuf
), "%ld", getpid());
73 (void) write(Lckfd
, lockbuf
, strlen(lockbuf
) + 1);
75 log("fd(pid)\t = %d", Lckfd
);
80 * openpipes() -- open pmpipe and sacpipe to communicate with SAC
81 * -- Pfd, Sfd are global file descriptors for pmpipe, sacpipe
89 Sfd
= open(SACPIPE
, O_WRONLY
);
91 fatal("open sacpipe failed: %s", strerror(errno
));
93 Pfd
= open(PMPIPE
, O_RDWR
|O_NONBLOCK
);
95 fatal("open pmpipe failed: %s", strerror(errno
));
98 log("fd(sacpipe)\t = %d",Sfd
);
99 log("fd(pmpipe)\t = %d",Pfd
);
104 * remove_env(env) - remove an environment variable from the environment
110 extern char **environ
;
118 if (strncmp(*p
, env
,strlen(env
)) == 0)
129 * get_environ() -- get env variables PMTAG, ISTATE
130 * -- set global variables Tag, State
136 extern char State
, *Istate
, *Tag
;
138 if ((Tag
= getenv("PMTAG")) == NULL
)
139 fatal("PMTAG is missing");
141 if ((Istate
= getenv("ISTATE")) == NULL
)
142 fatal("ISTATE is missing");
144 State
= (!strcmp(Istate
, "enabled")) ? PM_ENABLED
: PM_DISABLED
;
147 * remove the environment variables so they will not
148 * be passed to the children
150 remove_env("ISTATE");
155 * sacpoll - the event handler when sac event is posted
162 struct sacmsg sacmsg
;
171 /* we don't want to be interrupted by sigchild now */
172 (void)sigprocmask(SIG_SETMASK
, NULL
, &cset
);
174 (void)sigaddset(&tset
, SIGCLD
);
175 (void)sigprocmask(SIG_SETMASK
, &tset
, NULL
);
178 * read sac messages, one at a time until no message
179 * is left on the pipe.
180 * the pipe is open with O_NONBLOCK, read will return -1
181 * and errno = EAGAIN if nothing is on the pipe
185 ret
= read(Pfd
, &sacmsg
, sizeof(sacmsg
));
189 /* no more data on the pipe */
190 (void)sigprocmask(SIG_SETMASK
, &cset
, NULL
);
195 fatal("pmpipe read failed: %s",
197 break; /*NOTREACHED*/
201 /* no more data on the pipe */
202 (void)sigprocmask(SIG_SETMASK
, &cset
, NULL
);
207 (void) strcpy(pmmsg
.pm_tag
, Tag
);
208 pmmsg
.pm_maxclass
= TM_MAXCLASS
;
209 pmmsg
.pm_type
= PM_STATUS
;
210 switch(sacmsg
.sc_type
) {
214 log("Got SC_ENABLE message");
217 if (State
!= oldState
) {
219 debug("state changed to ENABLED");
225 log("Got SC_DISABLE message");
228 if (State
!= oldState
) {
230 debug("state changed to DISABLED");
236 log("Got SC_READDB message");
240 log("Got unknown message %d", sacmsg
.sc_type
);
241 pmmsg
.pm_type
= PM_UNKNOWN
;
244 pmmsg
.pm_state
= State
;
246 while (write(Sfd
, &pmmsg
, sizeof(pmmsg
)) != sizeof(pmmsg
)) {
249 log("sanity response to SAC failed: %s",