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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* 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) 2013, Joyent, Inc. All rights reserved.
*/
/*
* utmpx.c - utmpx utility routines
*
* Since svc.startd(1M) places utmpx records for its launched instances, it must
* also mark them as dead once completed.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <pthread.h>
#include <sac.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <utmpx.h>
#include <fcntl.h>
#include "startd.h"
static const char rlevels[] = { 'S', '0', '1', '2', '3', '4', '5', '6', 0 };
static int n_prev[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
static pthread_mutex_t utmpx_lock;
static int utmpx_truncated = 0;
#define USEC_PER_MSEC 1000
int
utmpx_mark_init(pid_t pid, char *prefix)
{
struct utmpx ut, *oldu;
int tmplen;
int ret;
while (st->st_initial && !utmpx_truncated)
(void) usleep(200 * USEC_PER_MSEC);
/*
* Clean out any preexisting records for this PID, as they must be
* inaccurate.
*/
utmpx_mark_dead(pid, 0, B_TRUE);
/*
* Construct a new record with the appropriate prefix.
*/
(void) memset(&ut, 0, sizeof (ut));
(void) strncpy(ut.ut_user, ".startd", sizeof (ut.ut_user));
ut.ut_pid = pid;
ut.ut_id[0] = ut.ut_id[1] = ut.ut_id[2] = ut.ut_id[3] = (char)SC_WILDC;
for (ret = 0; ret < strlen(prefix); ret++)
ut.ut_id[ret] = prefix[ret];
ut.ut_type = INIT_PROCESS;
(void) time(&ut.ut_tv.tv_sec);
for (;;) {
MUTEX_LOCK(&utmpx_lock);
setutxent();
if ((oldu = getutxid(&ut)) != NULL) {
/*
* Copy in the old "line" and "host" fields.
*/
bcopy(oldu->ut_line, ut.ut_line, sizeof (ut.ut_line));
bcopy(oldu->ut_host, ut.ut_host, sizeof (ut.ut_host));
ut.ut_syslen = (tmplen = strlen(ut.ut_host)) ?
min(tmplen + 1, sizeof (ut.ut_host)) : 0;
}
if (makeutx(&ut) != NULL)
break;
if (errno != EROFS)
log_framework(LOG_WARNING,
"makeutx failed, retrying: %s\n", strerror(errno));
MUTEX_UNLOCK(&utmpx_lock);
(void) sleep(1);
}
updwtmpx(WTMPX_FILE, &ut);
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
return (ret);
}
void
utmpx_mark_dead(pid_t pid, int status, boolean_t blocking)
{
struct utmpx *up;
int logged = 0;
for (;;) {
int found = 0;
MUTEX_LOCK(&utmpx_lock);
setutxent();
while (up = getutxent()) {
if (up->ut_pid == pid) {
found = 1;
if (up->ut_type == DEAD_PROCESS) {
/*
* Cleaned up elsewhere.
*/
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
return;
}
up->ut_type = DEAD_PROCESS;
up->ut_exit.e_termination = WTERMSIG(status);
up->ut_exit.e_exit = WEXITSTATUS(status);
(void) time(&up->ut_tv.tv_sec);
if (pututxline(up) != NULL) {
/*
* Now attempt to add to the end of the
* wtmp and wtmpx files. Do not create
* if they don't already exist.
*/
updwtmpx(WTMPX_FILE, up);
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
return;
}
}
}
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
if (!found || !blocking)
return;
if (!logged) {
log_framework(LOG_INFO, "retrying utmpx_dead on PID "
"%ld\n", pid);
logged++;
}
(void) sleep(1);
}
}
static void
utmpx_check()
{
struct stat sb;
if (stat(_UTMPX_FILE, &sb) == 0 &&
sb.st_mode != (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
(void) chmod(_UTMPX_FILE, S_IRUSR | S_IWUSR | S_IRGRP |
S_IROTH);
if (stat(_WTMPX_FILE, &sb) == 0 &&
sb.st_mode != (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
(void) chmod(_WTMPX_FILE, S_IRUSR | S_IWUSR | S_IRGRP |
S_IROTH);
}
/*
* Retrieve the runlevel utmpx entry if there is one; used to recover
* state when svc.startd is restarted.
*/
char
utmpx_get_runlevel(void)
{
struct utmpx *up;
char rl = '\0';
MUTEX_LOCK(&utmpx_lock);
setutxent();
while (up = getutxent()) {
if (up->ut_type == RUN_LVL &&
sscanf(up->ut_line, RUNLVL_MSG, &rl) == 1)
break;
}
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
return (rl);
}
void
utmpx_set_runlevel(char runlevel, char oldrl, boolean_t do_bump)
{
struct utmpx u;
struct utmpx *oup;
size_t tmplen;
int i;
if (runlevel == 's')
runlevel = 'S';
if (oldrl == 's')
oldrl = 'S';
bzero(&u, sizeof (struct utmpx));
u.ut_id[0] = u.ut_id[1] = u.ut_id[2] = u.ut_id[3] = '\0';
u.ut_pid = 0;
u.ut_type = RUN_LVL;
(void) time(&u.ut_tv.tv_sec);
MUTEX_LOCK(&utmpx_lock);
setutxent();
if ((oup = getutxid(&u)) != NULL) {
bcopy(oup->ut_host, u.ut_host, sizeof (u.ut_host));
bcopy(oup->ut_line, u.ut_line, sizeof (u.ut_line));
bcopy(oup->ut_user, u.ut_user, sizeof (u.ut_user));
tmplen = strlen(u.ut_host);
if (tmplen)
u.ut_syslen = min(tmplen + 1, sizeof (u.ut_host));
else
u.ut_syslen = 0;
}
if (oldrl != '\0')
u.ut_exit.e_exit = oldrl;
else if (oup != NULL)
u.ut_exit.e_exit = oup->ut_exit.e_termination;
else
u.ut_exit.e_exit = '0';
u.ut_exit.e_termination = runlevel;
for (i = 0; rlevels[i] != '\0'; ++i) {
if (rlevels[i] == runlevel)
break;
}
u.ut_pid = n_prev[i];
if (do_bump) {
for (i = 0; rlevels[i] != '\0'; ++i) {
if (rlevels[i] == u.ut_exit.e_exit)
break;
}
++n_prev[i];
}
(void) sprintf(u.ut_line, RUNLVL_MSG, runlevel);
if (pututxline(&u) == NULL) {
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
return;
}
updwtmpx(WTMPX_FILE, &u);
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
utmpx_check();
}
static void
utmpx_write_entry(short type, const char *msg, time_t tstamp)
{
struct utmpx u;
struct utmpx *oup;
size_t tmplen;
bzero(&u, sizeof (struct utmpx));
u.ut_id[0] = u.ut_id[1] = u.ut_id[2] = u.ut_id[3] = '\0';
u.ut_pid = 0;
u.ut_exit.e_termination = WTERMSIG(0);
u.ut_exit.e_exit = WEXITSTATUS(0);
u.ut_type = type;
u.ut_tv.tv_sec = tstamp;
MUTEX_LOCK(&utmpx_lock);
setutxent();
if ((oup = getutxid(&u)) != NULL) {
bcopy(oup->ut_user, u.ut_user, sizeof (u.ut_user));
bcopy(oup->ut_line, u.ut_line, sizeof (u.ut_line));
bcopy(oup->ut_host, u.ut_host, sizeof (u.ut_host));
tmplen = strlen(u.ut_host);
if (tmplen)
u.ut_syslen = min(tmplen + 1, sizeof (u.ut_host));
else
u.ut_syslen = 0;
}
(void) sprintf(u.ut_line, "%.12s", msg);
if (pututxline(&u) == NULL) {
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
return;
}
updwtmpx(WTMPX_FILE, &u);
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
utmpx_check();
}
void
utmpx_write_boottime(void)
{
time_t tstamp;
struct stat stbuf;
/*
* The DOWN_TIME record tracks when the OS became unavailable
* during the previous boot. We stat(2) WTMPX and check its
* attributes to determine when (and how) the OS became
* unavailable. If the file is empty, skip writing a DOWN_TIME
* record. Otherwise, check the access and modify times and
* use whichever is latest as the time that the OS became
* unavailable. If st_atime is latest, the instance crashed or
* the machine lost power. If st_mtime is latest, the shutdown
* was controlled.
*/
if (stat(WTMPX_FILE, &stbuf) == 0 && stbuf.st_size != 0) {
tstamp = (stbuf.st_atime >= stbuf.st_mtime) ?
stbuf.st_atime : stbuf.st_mtime;
utmpx_write_entry(DOWN_TIME, DOWN_MSG, tstamp);
}
/*
* The boot time (or start time, for a non-global zone) is retrieved in
* log_init().
*/
tstamp = st->st_start_time.tv_sec;
utmpx_write_entry(BOOT_TIME, BOOT_MSG, tstamp);
}
/*
* void utmpx_clear_old(void)
* At boot and only at boot, truncate the utmpx file.
*
*/
void
utmpx_clear_old(void)
{
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
if (!st->st_initial || utmpx_truncated)
return;
MUTEX_LOCK(&utmpx_lock);
if ((fd = open(_UTMPX_FILE,
O_WRONLY | O_CREAT | O_TRUNC, mode)) != -1) {
(void) fchmod(fd, mode); /* force mode regardless of umask() */
(void) fchown(fd, 0, 2); /* force owner to root/bin */
(void) close(fd);
} else {
log_framework(LOG_NOTICE, "Unable to create %s: %s\n",
_UTMPX_FILE, strerror(errno));
}
utmpx_truncated = 1;
MUTEX_UNLOCK(&utmpx_lock);
}
void
utmpx_init()
{
(void) pthread_mutex_init(&utmpx_lock, &mutex_attrs);
}
void
utmpx_prefork()
{
/*
* The libc utmpx routines are entirely MT-unsafe; we must assure
* that no other thread is in these routines when we fork lest we
* leave the child with inconsistent library state.
*/
MUTEX_LOCK(&utmpx_lock);
}
void
utmpx_postfork()
{
MUTEX_UNLOCK(&utmpx_lock);
}
|