~jamesodhunt/upstart/test-quiesce-cleanup

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
/* upstart
 *
 * utmp.c - utmp and wtmp handling
 *
 * Copyright © 2009 Canonical Ltd.
 * Author: Scott James Remnant <scott@netsplit.com>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */


#include <sys/time.h>
#include <sys/utsname.h>

#include <utmpx.h>
#include <stdlib.h>
#include <string.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/logging.h>
#include <nih/error.h>

#include "utmp.h"


/* Prototypes for static functions */
static void utmp_entry      (struct utmpx *utmp, short type, pid_t pid,
			     const char *line, const char *id,
			     const char *user);
static int  utmp_write      (const char *utmp_file, const struct utmpx *utmp)
	__attribute__ ((warn_unused_result));
static void wtmp_write      (const char *wtmp_file, const struct utmpx *utmp);


/**
 * SHUTDOWN_TIME:
 *
 * The sysvinit last utility expects a special "shutdown" RUN_LVL entry,
 * and abuses the type to distinguish that.  We'll do the same.
 **/
#define SHUTDOWN_TIME 254


/**
 * utmp_read_runlevel:
 * @utmp_file: utmp or wtmp file to read from,
 * @prevlevel: pointer to store previous runlevel in.
 *
 * Reads the the most recent runlevel entry from @utmp_file, returning
 * the runlevel from it.  If @prevlevel is not NULL, the previous runlevel
 * will be stored in that variable.
 *
 * @utmp_file may be either a utmp or wtmp file, if NULL the default
 * /var/run/utmp is used.
 *
 * Returns: runlevel on success, negative value on raised error.
 **/
int
utmp_read_runlevel (const char *utmp_file,
		    int *       prevlevel)
{
	struct utmpx  utmp;
	struct utmpx *lvl;
	int           runlevel;

	memset (&utmp, 0, sizeof utmp);
	utmp.ut_type = RUN_LVL;

	if (utmp_file)
		utmpxname (utmp_file);
	setutxent ();

	lvl = getutxid (&utmp);
	if (! lvl) {
		nih_error_raise_system ();
		endutxent ();
		return -1;
	}

	runlevel = lvl->ut_pid % 256;
	if (prevlevel)
		*prevlevel = lvl->ut_pid / 256 ?: 'N';

	endutxent ();

	return runlevel;
}

/**
 * utmp_get_runlevel:
 * @utmp_file: utmp or wtmp file to read from,
 * @prevlevel: pointer to store previous runlevel in.
 *
 * If the RUNLEVEL and PREVLEVEL environment variables are set, returns
 * the current and previous runlevels from those otherwise calls
 * utmp_read_runlevel() to read the most recent runlevel entry from
 * @utmp_file.
 *
 * Returns: runlevel on success, negative value on raised error.
 **/
int
utmp_get_runlevel (const char *utmp_file,
		   int *       prevlevel)
{
	const char *renv;
	const char *penv;

	renv = getenv ("RUNLEVEL");
	penv = getenv ("PREVLEVEL");

	if (renv && renv[0]) {
		if (prevlevel)
			*prevlevel = penv && penv[0] ? penv[0] : 'N';

		return renv[0];
	}

	return utmp_read_runlevel (utmp_file, prevlevel);
}


/**
 * utmp_write_runlevel:
 * @utmp_file: utmp file,
 * @wtmp_file: wtmp file,
 * @runlevel: new runlevel,
 * @prevlevel: previous runlevel.
 *
 * Write a runlevel change record from @prevlevel to @runlevel to @utmp_file,
 * or /var/run/utmp if @utmp_file is NULL, and to @wtmp_file, or /var/log/wtmp
 * if @wtmp_file is NULL.
 *
 * Errors writing to the wtmp file are ignored.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
utmp_write_runlevel (const char *utmp_file,
		     const char *wtmp_file,
		     int         runlevel,
		     int         prevlevel)
{
	struct utmpx reboot;
	struct utmpx utmp;
	int          savedlevel;
	int          ret;

	nih_assert (runlevel > 0);
	nih_assert (prevlevel >= 0);

	utmp_entry (&reboot, BOOT_TIME, 0, NULL, NULL, NULL);

	/* Check for the previous runlevel entry in utmp, if it doesn't
	 * match then we assume a missed reboot so write the boot time
	 * record out first.
	 */
	savedlevel = utmp_read_runlevel (utmp_file, NULL);
	if (savedlevel != prevlevel) {
		if (savedlevel < 0)
			nih_free (nih_error_get ());

		if (utmp_write (utmp_file, &reboot) < 0)
			nih_free (nih_error_get ());
	}

	/* Check for the previous runlevel entry in wtmp, if it doesn't
	 * match then we assume a missed reboot so write the boot time
	 * record out first.
	 */
	savedlevel = utmp_read_runlevel (wtmp_file, NULL);
	if (savedlevel != prevlevel) {
		if (savedlevel < 0)
			nih_free (nih_error_get ());

		wtmp_write (wtmp_file, &reboot);
	}

	/* Write the runlevel change record */
	utmp_entry (&utmp, RUN_LVL, runlevel + prevlevel * 256,
		    NULL, NULL, NULL);

	ret = utmp_write (utmp_file, &utmp);
	wtmp_write (wtmp_file, &utmp);

	return ret;
}


/**
 * utmp_write_shutdown:
 * @utmp_file: utmp file to write to,
 * @wtmp_file: wtmp file to write to.
 *
 * Write a shutdown utmp record to @utmp_file, or /var/run/utmp if
 * @utmp_file is NULL, and to @wtmp_file, or /var/log/wtmp if @wtmp_file
 * is NULL.
 *
 * Errors writing to the wtmp file are ignored.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
utmp_write_shutdown (const char *utmp_file,
		     const char *wtmp_file)
{
	struct utmpx utmp;
	int          ret;

	utmp_entry (&utmp, SHUTDOWN_TIME, 0, NULL, NULL, NULL);

	ret = utmp_write (utmp_file, &utmp);
	wtmp_write (wtmp_file, &utmp);

	return ret;
}


/**
 * utmp_entry:
 * @utmp: utmp entry to fill,
 * @type: type of record,
 * @pid: process id of login process,
 * @line: device name of tty,
 * @id: terminal name suffix,
 * @user: username.
 *
 * Fill the utmp entry @utmp with the details passed, setting the auxiliary
 * information such as host and time to sensible defaults.  Depending on
 * @type, the other arguments may be ignored.
 *
 * When @type is BOOT_TIME, or the special SHUTDOWN_TIME, all arguments
 * are ignored.  When @type is RUN_LVL, the @line, @id and @user lines are
 * ignored.
 *
 * Any existing values in @utmp before this call will be lost.
 **/
static void
utmp_entry (struct utmpx *utmp,
	    short         type,
	    pid_t         pid,
	    const char *  line,
	    const char *  id,
	    const char *  user)
{
	struct utsname uts;
	struct timeval tv;

	nih_assert (utmp != NULL);
	nih_assert (type != EMPTY);

	switch (type) {
	case BOOT_TIME:
		pid = 0;
		line = "~";
		id = "~~";
		user = "reboot";
		break;
	case SHUTDOWN_TIME:
		type = RUN_LVL;
		pid = 0;
		line = "~";
		id = "~~";
		user = "shutdown";
		break;
	case RUN_LVL:
		nih_assert (pid != 0);
		line = "~";
		id = "~~";
		user = "runlevel";
		break;
	default:
		nih_assert (line != NULL);
		nih_assert (id != NULL);
		nih_assert (user != NULL);
	}

	memset (utmp, 0, sizeof (struct utmpx));

	utmp->ut_type = type;
	utmp->ut_pid = pid;

	strncpy (utmp->ut_line, line, sizeof utmp->ut_line);
	strncpy (utmp->ut_id, id, sizeof utmp->ut_id);
	strncpy (utmp->ut_user, user, sizeof utmp->ut_user);

	if (uname (&uts) == 0)
		strncpy (utmp->ut_host, uts.release, sizeof utmp->ut_host);

	gettimeofday (&tv, NULL);
	utmp->ut_tv.tv_sec = tv.tv_sec;
	utmp->ut_tv.tv_usec = tv.tv_usec;
}

/**
 * utmp_write:
 * @utmp_file: utmp file to write to,
 * @utmp: utmp entry to write.
 *
 * Write the utmp entry @utmp to @utmp_file, or /var/run/utmp if @utmp_file
 * is NULL.
 *
 * Returns: zero on success, negative value on raised error.
 **/
static int
utmp_write (const char *        utmp_file,
	    const struct utmpx *utmp)
{
	nih_assert (utmp != NULL);

	utmpxname (utmp_file ?: _PATH_UTMPX);

	setutxent ();

	if (! pututxline (utmp)) {
		nih_error_raise_system ();
		endutxent ();
		return -1;
	}

	endutxent ();

	return 0;
}

/**
 * wtmp_write:
 * @wtmp_file: wtmp file to write to,
 * @utmp: utmp entry to write.
 *
 * Write the utmp entry @utmp to @wtmp_file, or /var/log/wtmp if @utmp_file
 * is NULL.
 **/
static void
wtmp_write (const char *        wtmp_file,
	    const struct utmpx *utmp)
{
	nih_assert (utmp != NULL);

	updwtmpx (wtmp_file ?: _PATH_WTMPX, utmp);
}