xref: /onnv-gate/usr/src/cmd/svc/startd/utmpx.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 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * utmpx.c - utmpx utility routines
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * Since svc.startd(1M) places utmpx records for its launched instances, it must
33*0Sstevel@tonic-gate  * also mark them as dead once completed.
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/stat.h>
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <sys/wait.h>
39*0Sstevel@tonic-gate #include <sys/stat.h>
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <pthread.h>
42*0Sstevel@tonic-gate #include <sac.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <strings.h>
45*0Sstevel@tonic-gate #include <time.h>
46*0Sstevel@tonic-gate #include <unistd.h>
47*0Sstevel@tonic-gate #include <utmpx.h>
48*0Sstevel@tonic-gate #include <fcntl.h>
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include "startd.h"
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static const char rlevels[] = { 'S', '0', '1', '2', '3', '4', '5', '6', 0 };
53*0Sstevel@tonic-gate static int n_prev[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static pthread_mutex_t utmpx_lock;
56*0Sstevel@tonic-gate static int utmpx_truncated = 0;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #define	USEC_PER_MSEC	1000
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate int
utmpx_mark_init(pid_t pid,char * prefix)61*0Sstevel@tonic-gate utmpx_mark_init(pid_t pid, char *prefix)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	struct utmpx ut, *oldu;
64*0Sstevel@tonic-gate 	int tmplen;
65*0Sstevel@tonic-gate 	int ret;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	while (st->st_initial && !utmpx_truncated)
68*0Sstevel@tonic-gate 		(void) usleep(200 * USEC_PER_MSEC);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	/*
71*0Sstevel@tonic-gate 	 * Clean out any preexisting records for this PID, as they must be
72*0Sstevel@tonic-gate 	 * inaccurate.
73*0Sstevel@tonic-gate 	 */
74*0Sstevel@tonic-gate 	utmpx_mark_dead(pid, 0, B_TRUE);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	/*
77*0Sstevel@tonic-gate 	 * Construct a new record with the appropriate prefix.
78*0Sstevel@tonic-gate 	 */
79*0Sstevel@tonic-gate 	(void) memset(&ut, 0, sizeof (ut));
80*0Sstevel@tonic-gate 	(void) strncpy(ut.ut_user, ".startd", sizeof (ut.ut_user));
81*0Sstevel@tonic-gate 	ut.ut_pid = pid;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	ut.ut_id[0] = ut.ut_id[1] = ut.ut_id[2] = ut.ut_id[3] = (char)SC_WILDC;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	for (ret = 0; ret < strlen(prefix); ret++)
86*0Sstevel@tonic-gate 		ut.ut_id[ret] = prefix[ret];
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	ut.ut_type = INIT_PROCESS;
89*0Sstevel@tonic-gate 	(void) time(&ut.ut_tv.tv_sec);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	for (;;) {
92*0Sstevel@tonic-gate 		MUTEX_LOCK(&utmpx_lock);
93*0Sstevel@tonic-gate 		setutxent();
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 		if ((oldu = getutxid(&ut)) != NULL) {
96*0Sstevel@tonic-gate 			/*
97*0Sstevel@tonic-gate 			 * Copy in the old "line" and "host" fields.
98*0Sstevel@tonic-gate 			 */
99*0Sstevel@tonic-gate 			bcopy(oldu->ut_line, ut.ut_line, sizeof (ut.ut_line));
100*0Sstevel@tonic-gate 			bcopy(oldu->ut_host, ut.ut_host, sizeof (ut.ut_host));
101*0Sstevel@tonic-gate 			ut.ut_syslen = (tmplen = strlen(ut.ut_host)) ?
102*0Sstevel@tonic-gate 			    min(tmplen + 1, sizeof (ut.ut_host)) : 0;
103*0Sstevel@tonic-gate 		}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 		if (makeutx(&ut) != NULL)
106*0Sstevel@tonic-gate 			break;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 		if (errno != EROFS)
109*0Sstevel@tonic-gate 			log_framework(LOG_WARNING,
110*0Sstevel@tonic-gate 			    "makeutx failed, retrying: %s\n", strerror(errno));
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&utmpx_lock);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		(void) sleep(1);
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	updwtmpx(WTMPX_FILE, &ut);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	endutxent();
120*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&utmpx_lock);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	return (ret);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate void
utmpx_mark_dead(pid_t pid,int status,boolean_t blocking)126*0Sstevel@tonic-gate utmpx_mark_dead(pid_t pid, int status, boolean_t blocking)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	struct utmpx *up;
129*0Sstevel@tonic-gate 	int logged = 0;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	for (;;) {
132*0Sstevel@tonic-gate 		int found = 0;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		MUTEX_LOCK(&utmpx_lock);
135*0Sstevel@tonic-gate 		setutxent();
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		while (up = getutxent()) {
138*0Sstevel@tonic-gate 			if (up->ut_pid == pid) {
139*0Sstevel@tonic-gate 				found = 1;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 				if (up->ut_type == DEAD_PROCESS) {
142*0Sstevel@tonic-gate 					/*
143*0Sstevel@tonic-gate 					 * Cleaned up elsewhere.
144*0Sstevel@tonic-gate 					 */
145*0Sstevel@tonic-gate 					endutxent();
146*0Sstevel@tonic-gate 					MUTEX_UNLOCK(&utmpx_lock);
147*0Sstevel@tonic-gate 					return;
148*0Sstevel@tonic-gate 				}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 				up->ut_type = DEAD_PROCESS;
151*0Sstevel@tonic-gate 				up->ut_exit.e_termination = WTERMSIG(status);
152*0Sstevel@tonic-gate 				up->ut_exit.e_exit = WEXITSTATUS(status);
153*0Sstevel@tonic-gate 				(void) time(&up->ut_tv.tv_sec);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 				if (pututxline(up) != NULL) {
156*0Sstevel@tonic-gate 					/*
157*0Sstevel@tonic-gate 					 * Now attempt to add to the end of the
158*0Sstevel@tonic-gate 					 * wtmp and wtmpx files.  Do not create
159*0Sstevel@tonic-gate 					 * if they don't already exist.
160*0Sstevel@tonic-gate 					 */
161*0Sstevel@tonic-gate 					updwtmpx(WTMPX_FILE, up);
162*0Sstevel@tonic-gate 					endutxent();
163*0Sstevel@tonic-gate 					MUTEX_UNLOCK(&utmpx_lock);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 					return;
166*0Sstevel@tonic-gate 				}
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 		endutxent();
171*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&utmpx_lock);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		if (!found || !blocking)
174*0Sstevel@tonic-gate 			return;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 		if (!logged) {
177*0Sstevel@tonic-gate 			log_framework(LOG_INFO, "retrying utmpx_dead on PID "
178*0Sstevel@tonic-gate 			    "%ld\n", pid);
179*0Sstevel@tonic-gate 			logged++;
180*0Sstevel@tonic-gate 		}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 		(void) sleep(1);
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate static void
utmpx_check()187*0Sstevel@tonic-gate utmpx_check()
188*0Sstevel@tonic-gate {
189*0Sstevel@tonic-gate 	struct stat sb;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	if (stat(_UTMPX_FILE, &sb) == 0 &&
192*0Sstevel@tonic-gate 	    sb.st_mode != (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
193*0Sstevel@tonic-gate 		(void) chmod(_UTMPX_FILE, S_IRUSR | S_IWUSR | S_IRGRP |
194*0Sstevel@tonic-gate 		    S_IROTH);
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	if (stat(_WTMPX_FILE, &sb) == 0 &&
197*0Sstevel@tonic-gate 	    sb.st_mode != (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
198*0Sstevel@tonic-gate 		(void) chmod(_WTMPX_FILE, S_IRUSR | S_IWUSR | S_IRGRP |
199*0Sstevel@tonic-gate 		    S_IROTH);
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate /*
203*0Sstevel@tonic-gate  * Retrieve the runlevel utmpx entry if there is one; used to recover
204*0Sstevel@tonic-gate  * state when svc.startd is restarted.
205*0Sstevel@tonic-gate  */
206*0Sstevel@tonic-gate char
utmpx_get_runlevel(void)207*0Sstevel@tonic-gate utmpx_get_runlevel(void)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	struct utmpx *up;
210*0Sstevel@tonic-gate 	char rl = '\0';
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	MUTEX_LOCK(&utmpx_lock);
213*0Sstevel@tonic-gate 	setutxent();
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	while (up = getutxent()) {
216*0Sstevel@tonic-gate 		if (up->ut_type == RUN_LVL &&
217*0Sstevel@tonic-gate 		    sscanf(up->ut_line, RUNLVL_MSG, &rl) == 1)
218*0Sstevel@tonic-gate 			break;
219*0Sstevel@tonic-gate 	}
220*0Sstevel@tonic-gate 	endutxent();
221*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&utmpx_lock);
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	return (rl);
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate void
utmpx_set_runlevel(char runlevel,char oldrl,boolean_t do_bump)227*0Sstevel@tonic-gate utmpx_set_runlevel(char runlevel, char oldrl, boolean_t do_bump)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate 	struct utmpx u;
230*0Sstevel@tonic-gate 	struct utmpx *oup;
231*0Sstevel@tonic-gate 	size_t tmplen;
232*0Sstevel@tonic-gate 	int i;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	if (runlevel == 's')
235*0Sstevel@tonic-gate 		runlevel = 'S';
236*0Sstevel@tonic-gate 	if (oldrl == 's')
237*0Sstevel@tonic-gate 		oldrl = 'S';
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	bzero(&u, sizeof (struct utmpx));
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	u.ut_id[0] = u.ut_id[1] = u.ut_id[2] = u.ut_id[3] = '\0';
242*0Sstevel@tonic-gate 	u.ut_pid = 0;
243*0Sstevel@tonic-gate 	u.ut_type = RUN_LVL;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	(void) time(&u.ut_tv.tv_sec);
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	MUTEX_LOCK(&utmpx_lock);
248*0Sstevel@tonic-gate 	setutxent();
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	if ((oup = getutxid(&u)) != NULL) {
251*0Sstevel@tonic-gate 		bcopy(oup->ut_host, u.ut_host, sizeof (u.ut_host));
252*0Sstevel@tonic-gate 		bcopy(oup->ut_line, u.ut_line, sizeof (u.ut_line));
253*0Sstevel@tonic-gate 		bcopy(oup->ut_user, u.ut_user, sizeof (u.ut_user));
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 		tmplen = strlen(u.ut_host);
256*0Sstevel@tonic-gate 		if (tmplen)
257*0Sstevel@tonic-gate 			u.ut_syslen = min(tmplen + 1, sizeof (u.ut_host));
258*0Sstevel@tonic-gate 		else
259*0Sstevel@tonic-gate 			u.ut_syslen =  0;
260*0Sstevel@tonic-gate 	}
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	if (oldrl != '\0')
263*0Sstevel@tonic-gate 		u.ut_exit.e_exit = oldrl;
264*0Sstevel@tonic-gate 	else if (oup != NULL)
265*0Sstevel@tonic-gate 		u.ut_exit.e_exit = oup->ut_exit.e_termination;
266*0Sstevel@tonic-gate 	else
267*0Sstevel@tonic-gate 		u.ut_exit.e_exit = '0';
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	u.ut_exit.e_termination = runlevel;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	for (i = 0; rlevels[i] != '\0'; ++i) {
272*0Sstevel@tonic-gate 		if (rlevels[i] == runlevel)
273*0Sstevel@tonic-gate 			break;
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	u.ut_pid = n_prev[i];
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if (do_bump) {
279*0Sstevel@tonic-gate 		for (i = 0; rlevels[i] != '\0'; ++i) {
280*0Sstevel@tonic-gate 			if (rlevels[i] == u.ut_exit.e_exit)
281*0Sstevel@tonic-gate 				break;
282*0Sstevel@tonic-gate 		}
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 		++n_prev[i];
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	(void) sprintf(u.ut_line, RUNLVL_MSG, runlevel);
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	if (pututxline(&u) == NULL) {
290*0Sstevel@tonic-gate 		endutxent();
291*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&utmpx_lock);
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 		return;
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	updwtmpx(WTMPX_FILE, &u);
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	endutxent();
299*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&utmpx_lock);
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	utmpx_check();
302*0Sstevel@tonic-gate }
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate static void
utmpx_write_entry(short type,const char * msg,time_t tstamp)305*0Sstevel@tonic-gate utmpx_write_entry(short type, const char *msg, time_t tstamp)
306*0Sstevel@tonic-gate {
307*0Sstevel@tonic-gate 	struct utmpx u;
308*0Sstevel@tonic-gate 	struct utmpx *oup;
309*0Sstevel@tonic-gate 	size_t tmplen;
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	bzero(&u, sizeof (struct utmpx));
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	u.ut_id[0] = u.ut_id[1] = u.ut_id[2] = u.ut_id[3] = '\0';
314*0Sstevel@tonic-gate 	u.ut_pid = 0;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	u.ut_exit.e_termination = WTERMSIG(0);
317*0Sstevel@tonic-gate 	u.ut_exit.e_exit = WEXITSTATUS(0);
318*0Sstevel@tonic-gate 	u.ut_type = type;
319*0Sstevel@tonic-gate 	u.ut_tv.tv_sec = tstamp;
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	MUTEX_LOCK(&utmpx_lock);
322*0Sstevel@tonic-gate 	setutxent();
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	if ((oup = getutxid(&u)) != NULL) {
325*0Sstevel@tonic-gate 		bcopy(oup->ut_user, u.ut_user, sizeof (u.ut_user));
326*0Sstevel@tonic-gate 		bcopy(oup->ut_line, u.ut_line, sizeof (u.ut_line));
327*0Sstevel@tonic-gate 		bcopy(oup->ut_host, u.ut_host, sizeof (u.ut_host));
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 		tmplen = strlen(u.ut_host);
330*0Sstevel@tonic-gate 		if (tmplen)
331*0Sstevel@tonic-gate 			u.ut_syslen = min(tmplen + 1, sizeof (u.ut_host));
332*0Sstevel@tonic-gate 		else
333*0Sstevel@tonic-gate 			u.ut_syslen =  0;
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	(void) sprintf(u.ut_line, "%.12s", msg);
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	if (pututxline(&u) == NULL) {
339*0Sstevel@tonic-gate 		endutxent();
340*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&utmpx_lock);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 		return;
343*0Sstevel@tonic-gate 	}
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	updwtmpx(WTMPX_FILE, &u);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	endutxent();
348*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&utmpx_lock);
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	utmpx_check();
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate void
utmpx_write_boottime(void)354*0Sstevel@tonic-gate utmpx_write_boottime(void)
355*0Sstevel@tonic-gate {
356*0Sstevel@tonic-gate 	time_t tstamp;
357*0Sstevel@tonic-gate 	struct stat stbuf;
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	/*
360*0Sstevel@tonic-gate 	 * The DOWN_TIME record tracks when the OS became unavailable
361*0Sstevel@tonic-gate 	 * during the previous boot.  We stat(2) WTMPX and check its
362*0Sstevel@tonic-gate 	 * attributes to determine when (and how) the OS became
363*0Sstevel@tonic-gate 	 * unavailable.  If the file is empty, skip writing a DOWN_TIME
364*0Sstevel@tonic-gate 	 * record.  Otherwise, check the access and modify times and
365*0Sstevel@tonic-gate 	 * use whichever is latest as the time that the OS became
366*0Sstevel@tonic-gate 	 * unavailable.  If st_atime is latest, the instance crashed or
367*0Sstevel@tonic-gate 	 * the machine lost power.  If st_mtime is latest, the shutdown
368*0Sstevel@tonic-gate 	 * was controlled.
369*0Sstevel@tonic-gate 	 */
370*0Sstevel@tonic-gate 	if (stat(WTMPX_FILE, &stbuf) == 0 && stbuf.st_size != 0) {
371*0Sstevel@tonic-gate 		tstamp = (stbuf.st_atime >= stbuf.st_mtime) ?
372*0Sstevel@tonic-gate 		    stbuf.st_atime : stbuf.st_mtime;
373*0Sstevel@tonic-gate 		utmpx_write_entry(DOWN_TIME, DOWN_MSG, tstamp);
374*0Sstevel@tonic-gate 	}
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	/*
377*0Sstevel@tonic-gate 	 * The boot time (or start time, for a non-global zone) is retrieved in
378*0Sstevel@tonic-gate 	 * log_init().
379*0Sstevel@tonic-gate 	 */
380*0Sstevel@tonic-gate 	tstamp = st->st_start_time.tv_sec;
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	utmpx_write_entry(BOOT_TIME, BOOT_MSG, tstamp);
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate /*
386*0Sstevel@tonic-gate  * void utmpx_clear_old(void)
387*0Sstevel@tonic-gate  *   At boot and only at boot, truncate the utmpx file.
388*0Sstevel@tonic-gate  *
389*0Sstevel@tonic-gate  */
390*0Sstevel@tonic-gate void
utmpx_clear_old(void)391*0Sstevel@tonic-gate utmpx_clear_old(void)
392*0Sstevel@tonic-gate {
393*0Sstevel@tonic-gate 	int fd;
394*0Sstevel@tonic-gate 	mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	if (!st->st_initial || utmpx_truncated)
397*0Sstevel@tonic-gate 		return;
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	MUTEX_LOCK(&utmpx_lock);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if ((fd = open(_UTMPX_FILE,
402*0Sstevel@tonic-gate 	    O_WRONLY | O_CREAT | O_TRUNC, mode)) != -1) {
403*0Sstevel@tonic-gate 		(void) fchmod(fd, mode); /* force mode regardless of umask() */
404*0Sstevel@tonic-gate 		(void) fchown(fd, 0, 2); /* force owner to root/bin */
405*0Sstevel@tonic-gate 		(void) close(fd);
406*0Sstevel@tonic-gate 	} else {
407*0Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Unable to create %s: %s\n",
408*0Sstevel@tonic-gate 		    _UTMPX_FILE, strerror(errno));
409*0Sstevel@tonic-gate 	}
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	utmpx_truncated = 1;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&utmpx_lock);
414*0Sstevel@tonic-gate }
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate void
utmpx_init()417*0Sstevel@tonic-gate utmpx_init()
418*0Sstevel@tonic-gate {
419*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&utmpx_lock, &mutex_attrs);
420*0Sstevel@tonic-gate }
421