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