summaryrefslogtreecommitdiffstats
path: root/usr.bin/lastcomm/readrec.c
blob: 5de7c5c4c8089d61844cbae0ec64c82a8ce7978e (plain)
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
/*-
 * Copyright (c) 2007 Diomidis Spinellis
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/acct.h>

#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

int	 readrec_forward(FILE *f, struct acctv2 *av2);
int	 readrec_backward(FILE *f, struct acctv2 *av2);

/*
 * Reverse offsetof: return the offset of field f
 * from the end of the structure s.
 */
#define roffsetof(s, f) (sizeof(s) - offsetof(s, f))

/*
 * Read exactly one record of size size from stream f into ptr.
 * Failure to read the complete record is considered a file format error,
 * and will set errno to EFTYPE.
 * Return 0 on success, EOF on end of file or error.
 */
static int
fread_record(void *ptr, size_t size, FILE *f)
{
	size_t rv;

	if ((rv = fread(ptr, 1, size, f)) == size)
		return (0);
	else if (ferror(f) || rv == 0)
		return (EOF);
	else {
		/* Short read. */
		errno = EFTYPE;
		return (EOF);
	}
}

/*
 * Return the value of a comp_t field.
 */
static float
decode_comp(comp_t v)
{
	int result, exp;

	result = v & 017777;
	for (exp = v >> 13; exp; exp--)
		result <<= 3;
	return ((double)result / AHZV1);
}

/*
 * Read a v1 accounting record stored at the current
 * position of stream f.
 * Convert the data to the current record format.
 * Return EOF on error or end-of-file.
 */
static int
readrec_v1(FILE *f, struct acctv2 *av2)
{
	struct acctv1 av1;
	int rv;

	if ((rv = fread_record(&av1, sizeof(av1), f)) == EOF)
		return (EOF);
	av2->ac_zero = 0;
	av2->ac_version = 2;
	av2->ac_len = av2->ac_len2 = sizeof(*av2);
	memcpy(av2->ac_comm, av1.ac_comm, AC_COMM_LEN);
	av2->ac_utime = decode_comp(av1.ac_utime) * 1000000;
	av2->ac_stime = decode_comp(av1.ac_stime) * 1000000;
	av2->ac_etime = decode_comp(av1.ac_etime) * 1000000;
	av2->ac_btime = av1.ac_btime;
	av2->ac_uid = av1.ac_uid;
	av2->ac_gid = av1.ac_gid;
	av2->ac_mem = av1.ac_mem;
	av2->ac_io = decode_comp(av1.ac_io);
	av2->ac_tty = av1.ac_tty;
	av2->ac_flagx = av1.ac_flag | ANVER;
	return (0);
}

/*
 * Read an v2 accounting record stored at the current
 * position of stream f.
 * Return EOF on error or end-of-file.
 */
static int
readrec_v2(FILE *f, struct acctv2 *av2)
{
	return (fread_record(av2, sizeof(*av2), f));
}

/*
 * Read a new-style (post-v1) accounting record stored at
 * the current position of stream f.
 * Convert the data to the current record format.
 * Return EOF on error or end-of-file.
 */
static int
readrec_vx(FILE *f, struct acctv2 *av2)
{
	uint8_t magic, version;

	if (fread_record(&magic, sizeof(magic), f) == EOF ||
	    fread_record(&version, sizeof(version), f) == EOF ||
	    ungetc(version, f) == EOF ||
	    ungetc(magic, f) == EOF)
		return (EOF);
	switch (version) {
	case 2:
		return (readrec_v2(f, av2));

	/* Add handling for more versions here. */

	default:
		errno = EFTYPE;
		return (EOF);
	}
}

/*
 * Read an accounting record stored at the current
 * position of stream f.
 * Old-format records are converted to the current record
 * format.
 * Return the number of records read (1 or 0 at the end-of-file),
 * or EOF on error.
 */
int
readrec_forward(FILE *f, struct acctv2 *av2)
{
	int magic, rv;

	if ((magic = getc(f)) == EOF)
		return (ferror(f) ? EOF : 0);
	if (ungetc(magic, f) == EOF)
		return (EOF);
	if (magic != 0)
		/* Old record format. */
		rv = readrec_v1(f, av2);
	else
		/* New record formats. */
		rv = readrec_vx(f, av2);
	return (rv == EOF ? EOF : 1);
}

/*
 * Read an accounting record ending at the current
 * position of stream f.
 * Old-format records are converted to the current record
 * format.
 * The file pointer is positioned at the beginning of the
 * record read.
 * Return the number of records read (1 or 0 at the end-of-file),
 * or EOF on error.
 */
int
readrec_backward(FILE *f, struct acctv2 *av2)
{
	off_t pos;
	int c;
	uint16_t len;

	if ((pos = ftell(f)) == -1)
		return (EOF);
	if (pos == 0)
		return (0);
	if (fseek(f, -roffsetof(struct acctv2, ac_trailer),
	    SEEK_CUR) == EOF ||
	    (c = getc(f)) == EOF)
		return (EOF);
	if (c & ANVER) {
		/* New record formats. */
		if (fseeko(f, pos - roffsetof(struct acctv2, ac_len2),
		    SEEK_SET) == EOF ||
		    fread_record(&len, sizeof(len), f) == EOF ||
		    fseeko(f, pos - len, SEEK_SET) == EOF ||
		    readrec_vx(f, av2) == EOF ||
		    fseeko(f, pos - len, SEEK_SET) == EOF)
			return (EOF);
		else
			return (1);
	} else {
		/* Old record format. */
		if (fseeko(f, pos - sizeof(struct acctv1), SEEK_SET) == EOF ||
		    readrec_v1(f, av2) == EOF ||
		    fseeko(f, pos - sizeof(struct acctv1), SEEK_SET) == EOF)
			return (EOF);
		else
			return (1);
	}
}
OpenPOWER on IntegriCloud