xref: /freebsd/usr.bin/lastcomm/readrec.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*-
2  * Copyright (c) 2007 Diomidis Spinellis
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/acct.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 int	 readrec_forward(FILE *f, struct acctv2 *av2);
46 int	 readrec_backward(FILE *f, struct acctv2 *av2);
47 
48 /*
49  * Reverse offsetof: return the offset of field f
50  * from the end of the structure s.
51  */
52 #define roffsetof(s, f) (sizeof(s) - offsetof(s, f))
53 
54 /*
55  * Read exactly one record of size size from stream f into ptr.
56  * Failure to read the complete record is considered a file format error,
57  * and will set errno to EFTYPE.
58  * Return 0 on success, EOF on end of file or error.
59  */
60 static int
61 fread_record(void *ptr, size_t size, FILE *f)
62 {
63 	size_t rv;
64 
65 	if ((rv = fread(ptr, 1, size, f)) == size)
66 		return (0);
67 	else if (ferror(f) || rv == 0)
68 		return (EOF);
69 	else {
70 		/* Short read. */
71 		errno = EFTYPE;
72 		return (EOF);
73 	}
74 }
75 
76 /*
77  * Return the value of a comp_t field.
78  */
79 static float
80 decode_comp(comp_t v)
81 {
82 	int result, exp;
83 
84 	result = v & 017777;
85 	for (exp = v >> 13; exp; exp--)
86 		result <<= 3;
87 	return ((double)result / AHZV1);
88 }
89 
90 /*
91  * Read a v1 accounting record stored at the current
92  * position of stream f.
93  * Convert the data to the current record format.
94  * Return EOF on error or end-of-file.
95  */
96 static int
97 readrec_v1(FILE *f, struct acctv2 *av2)
98 {
99 	struct acctv1 av1;
100 	int rv;
101 
102 	if ((rv = fread_record(&av1, sizeof(av1), f)) == EOF)
103 		return (EOF);
104 	av2->ac_zero = 0;
105 	av2->ac_version = 2;
106 	av2->ac_len = av2->ac_len2 = sizeof(*av2);
107 	memcpy(av2->ac_comm, av1.ac_comm, AC_COMM_LEN);
108 	av2->ac_utime = decode_comp(av1.ac_utime) * 1000000;
109 	av2->ac_stime = decode_comp(av1.ac_stime) * 1000000;
110 	av2->ac_etime = decode_comp(av1.ac_etime) * 1000000;
111 	av2->ac_btime = av1.ac_btime;
112 	av2->ac_uid = av1.ac_uid;
113 	av2->ac_gid = av1.ac_gid;
114 	av2->ac_mem = av1.ac_mem;
115 	av2->ac_io = decode_comp(av1.ac_io);
116 	av2->ac_tty = av1.ac_tty;
117 	av2->ac_flagx = av1.ac_flag | ANVER;
118 	return (0);
119 }
120 
121 /*
122  * Read an v2 accounting record stored at the current
123  * position of stream f.
124  * Return EOF on error or end-of-file.
125  */
126 static int
127 readrec_v2(FILE *f, struct acctv2 *av2)
128 {
129 	return (fread_record(av2, sizeof(*av2), f));
130 }
131 
132 /*
133  * Read a new-style (post-v1) accounting record stored at
134  * the current position of stream f.
135  * Convert the data to the current record format.
136  * Return EOF on error or end-of-file.
137  */
138 static int
139 readrec_vx(FILE *f, struct acctv2 *av2)
140 {
141 	uint8_t magic, version;
142 
143 	if (fread_record(&magic, sizeof(magic), f) == EOF ||
144 	    fread_record(&version, sizeof(version), f) == EOF ||
145 	    ungetc(version, f) == EOF ||
146 	    ungetc(magic, f) == EOF)
147 		return (EOF);
148 	switch (version) {
149 	case 2:
150 		return (readrec_v2(f, av2));
151 
152 	/* Add handling for more versions here. */
153 
154 	default:
155 		errno = EFTYPE;
156 		return (EOF);
157 	}
158 }
159 
160 /*
161  * Read an accounting record stored at the current
162  * position of stream f.
163  * Old-format records are converted to the current record
164  * format.
165  * Return the number of records read (1 or 0 at the end-of-file),
166  * or EOF on error.
167  */
168 int
169 readrec_forward(FILE *f, struct acctv2 *av2)
170 {
171 	int magic, rv;
172 
173 	if ((magic = getc(f)) == EOF)
174 		return (ferror(f) ? EOF : 0);
175 	if (ungetc(magic, f) == EOF)
176 		return (EOF);
177 	if (magic != 0)
178 		/* Old record format. */
179 		rv = readrec_v1(f, av2);
180 	else
181 		/* New record formats. */
182 		rv = readrec_vx(f, av2);
183 	return (rv == EOF ? EOF : 1);
184 }
185 
186 /*
187  * Read an accounting record ending at the current
188  * position of stream f.
189  * Old-format records are converted to the current record
190  * format.
191  * The file pointer is positioned at the beginning of the
192  * record read.
193  * Return the number of records read (1 or 0 at the end-of-file),
194  * or EOF on error.
195  */
196 int
197 readrec_backward(FILE *f, struct acctv2 *av2)
198 {
199 	off_t pos;
200 	int c;
201 	uint16_t len;
202 
203 	if ((pos = ftell(f)) == -1)
204 		return (EOF);
205 	if (pos == 0)
206 		return (0);
207 	if (fseek(f, -roffsetof(struct acctv2, ac_trailer),
208 	    SEEK_CUR) == EOF ||
209 	    (c = getc(f)) == EOF)
210 		return (EOF);
211 	if (c & ANVER) {
212 		/* New record formats. */
213 		if (fseeko(f, pos - roffsetof(struct acctv2, ac_len2),
214 		    SEEK_SET) == EOF ||
215 		    fread_record(&len, sizeof(len), f) == EOF ||
216 		    fseeko(f, pos - len, SEEK_SET) == EOF ||
217 		    readrec_vx(f, av2) == EOF ||
218 		    fseeko(f, pos - len, SEEK_SET) == EOF)
219 			return (EOF);
220 		else
221 			return (1);
222 	} else {
223 		/* Old record format. */
224 		if (fseeko(f, pos - sizeof(struct acctv1), SEEK_SET) == EOF ||
225 		    readrec_v1(f, av2) == EOF ||
226 		    fseeko(f, pos - sizeof(struct acctv1), SEEK_SET) == EOF)
227 			return (EOF);
228 		else
229 			return (1);
230 	}
231 }
232