utmpx.c revision 1.3.2.8 1 1.3.2.8 thorpej /* $NetBSD: utmpx.c,v 1.3.2.8 2002/12/10 06:25:49 thorpej Exp $ */
2 1.3.2.2 nathanw
3 1.3.2.2 nathanw /*-
4 1.3.2.2 nathanw * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.3.2.2 nathanw * All rights reserved.
6 1.3.2.2 nathanw *
7 1.3.2.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 nathanw * by Christos Zoulas.
9 1.3.2.2 nathanw *
10 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
12 1.3.2.2 nathanw * are met:
13 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.3.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.3.2.2 nathanw * must display the following acknowledgement:
20 1.3.2.2 nathanw * This product includes software developed by the NetBSD
21 1.3.2.2 nathanw * Foundation, Inc. and its contributors.
22 1.3.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.2.2 nathanw * contributors may be used to endorse or promote products derived
24 1.3.2.2 nathanw * from this software without specific prior written permission.
25 1.3.2.2 nathanw *
26 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.3.2.2 nathanw */
38 1.3.2.2 nathanw #include <sys/cdefs.h>
39 1.3.2.2 nathanw
40 1.3.2.2 nathanw #if defined(LIBC_SCCS) && !defined(lint)
41 1.3.2.8 thorpej __RCSID("$NetBSD: utmpx.c,v 1.3.2.8 2002/12/10 06:25:49 thorpej Exp $");
42 1.3.2.2 nathanw #endif /* LIBC_SCCS and not lint */
43 1.3.2.2 nathanw
44 1.3.2.2 nathanw #include <sys/types.h>
45 1.3.2.2 nathanw #include <sys/param.h>
46 1.3.2.2 nathanw #include <sys/wait.h>
47 1.3.2.2 nathanw #include <sys/socket.h>
48 1.3.2.2 nathanw #include <sys/time.h>
49 1.3.2.2 nathanw #include <sys/stat.h>
50 1.3.2.2 nathanw
51 1.3.2.7 nathanw #include <assert.h>
52 1.3.2.2 nathanw #include <stdio.h>
53 1.3.2.5 thorpej #include <stdlib.h>
54 1.3.2.2 nathanw #include <string.h>
55 1.3.2.2 nathanw #include <vis.h>
56 1.3.2.3 nathanw #include <utmp.h>
57 1.3.2.2 nathanw #include <utmpx.h>
58 1.3.2.2 nathanw #include <unistd.h>
59 1.3.2.2 nathanw #include <fcntl.h>
60 1.3.2.4 nathanw #include <errno.h>
61 1.3.2.4 nathanw #include <db.h>
62 1.3.2.2 nathanw
63 1.3.2.2 nathanw static FILE *fp;
64 1.3.2.2 nathanw static struct utmpx ut;
65 1.3.2.2 nathanw static char utfile[MAXPATHLEN] = _PATH_UTMPX;
66 1.3.2.4 nathanw static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
67 1.3.2.2 nathanw
68 1.3.2.2 nathanw static struct utmpx *utmp_update(const struct utmpx *);
69 1.3.2.2 nathanw
70 1.3.2.2 nathanw static const char vers[] = "utmpx-1.00";
71 1.3.2.2 nathanw
72 1.3.2.2 nathanw void
73 1.3.2.2 nathanw setutxent()
74 1.3.2.2 nathanw {
75 1.3.2.4 nathanw
76 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
77 1.3.2.2 nathanw if (fp == NULL)
78 1.3.2.2 nathanw return;
79 1.3.2.2 nathanw (void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
80 1.3.2.2 nathanw }
81 1.3.2.2 nathanw
82 1.3.2.2 nathanw
83 1.3.2.2 nathanw void
84 1.3.2.2 nathanw endutxent()
85 1.3.2.2 nathanw {
86 1.3.2.4 nathanw
87 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
88 1.3.2.4 nathanw if (fp != NULL) {
89 1.3.2.2 nathanw (void)fclose(fp);
90 1.3.2.4 nathanw fp = NULL;
91 1.3.2.4 nathanw }
92 1.3.2.2 nathanw }
93 1.3.2.2 nathanw
94 1.3.2.2 nathanw
95 1.3.2.2 nathanw struct utmpx *
96 1.3.2.2 nathanw getutxent()
97 1.3.2.2 nathanw {
98 1.3.2.4 nathanw
99 1.3.2.2 nathanw if (fp == NULL) {
100 1.3.2.2 nathanw struct stat st;
101 1.3.2.2 nathanw
102 1.3.2.2 nathanw if ((fp = fopen(utfile, "r+")) == NULL)
103 1.3.2.3 nathanw if ((fp = fopen(utfile, "w+")) == NULL)
104 1.3.2.3 nathanw if ((fp = fopen(utfile, "r")) == NULL)
105 1.3.2.3 nathanw goto fail;
106 1.3.2.2 nathanw
107 1.3.2.2 nathanw /* get file size in order to check if new file */
108 1.3.2.2 nathanw if (fstat(fileno(fp), &st) == -1)
109 1.3.2.2 nathanw goto failclose;
110 1.3.2.2 nathanw
111 1.3.2.2 nathanw if (st.st_size == 0) {
112 1.3.2.2 nathanw /* new file, add signature record */
113 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
114 1.3.2.2 nathanw ut.ut_type = SIGNATURE;
115 1.3.2.2 nathanw (void)memcpy(ut.ut_user, vers, sizeof(vers));
116 1.3.2.3 nathanw if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
117 1.3.2.2 nathanw goto failclose;
118 1.3.2.2 nathanw } else {
119 1.3.2.2 nathanw /* old file, read signature record */
120 1.3.2.3 nathanw if (fread(&ut, sizeof(ut), 1, fp) != 1)
121 1.3.2.2 nathanw goto failclose;
122 1.3.2.2 nathanw if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
123 1.3.2.2 nathanw ut.ut_type != SIGNATURE)
124 1.3.2.2 nathanw goto failclose;
125 1.3.2.2 nathanw }
126 1.3.2.2 nathanw }
127 1.3.2.2 nathanw
128 1.3.2.3 nathanw if (fread(&ut, sizeof(ut), 1, fp) != 1)
129 1.3.2.2 nathanw goto fail;
130 1.3.2.2 nathanw
131 1.3.2.2 nathanw return &ut;
132 1.3.2.2 nathanw failclose:
133 1.3.2.2 nathanw (void)fclose(fp);
134 1.3.2.2 nathanw fail:
135 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
136 1.3.2.2 nathanw return NULL;
137 1.3.2.2 nathanw }
138 1.3.2.2 nathanw
139 1.3.2.2 nathanw
140 1.3.2.2 nathanw struct utmpx *
141 1.3.2.2 nathanw getutxid(const struct utmpx *utx)
142 1.3.2.2 nathanw {
143 1.3.2.4 nathanw
144 1.3.2.7 nathanw _DIAGASSERT(utx != NULL);
145 1.3.2.7 nathanw
146 1.3.2.2 nathanw if (utx->ut_type == EMPTY)
147 1.3.2.2 nathanw return NULL;
148 1.3.2.2 nathanw
149 1.3.2.2 nathanw do {
150 1.3.2.2 nathanw if (ut.ut_type == EMPTY)
151 1.3.2.2 nathanw continue;
152 1.3.2.2 nathanw switch (utx->ut_type) {
153 1.3.2.2 nathanw case EMPTY:
154 1.3.2.2 nathanw return NULL;
155 1.3.2.2 nathanw case RUN_LVL:
156 1.3.2.2 nathanw case BOOT_TIME:
157 1.3.2.2 nathanw case OLD_TIME:
158 1.3.2.2 nathanw case NEW_TIME:
159 1.3.2.2 nathanw if (ut.ut_type == utx->ut_type)
160 1.3.2.2 nathanw return &ut;
161 1.3.2.2 nathanw break;
162 1.3.2.2 nathanw case INIT_PROCESS:
163 1.3.2.2 nathanw case LOGIN_PROCESS:
164 1.3.2.2 nathanw case USER_PROCESS:
165 1.3.2.2 nathanw case DEAD_PROCESS:
166 1.3.2.2 nathanw switch (ut.ut_type) {
167 1.3.2.2 nathanw case INIT_PROCESS:
168 1.3.2.2 nathanw case LOGIN_PROCESS:
169 1.3.2.2 nathanw case USER_PROCESS:
170 1.3.2.2 nathanw case DEAD_PROCESS:
171 1.3.2.2 nathanw if (memcmp(ut.ut_id, utx->ut_id,
172 1.3.2.2 nathanw sizeof(ut.ut_id)) == 0)
173 1.3.2.2 nathanw return &ut;
174 1.3.2.2 nathanw break;
175 1.3.2.2 nathanw default:
176 1.3.2.2 nathanw break;
177 1.3.2.2 nathanw }
178 1.3.2.2 nathanw break;
179 1.3.2.2 nathanw default:
180 1.3.2.2 nathanw return NULL;
181 1.3.2.2 nathanw }
182 1.3.2.4 nathanw } while (getutxent() != NULL);
183 1.3.2.2 nathanw return NULL;
184 1.3.2.2 nathanw }
185 1.3.2.2 nathanw
186 1.3.2.2 nathanw
187 1.3.2.2 nathanw struct utmpx *
188 1.3.2.2 nathanw getutxline(const struct utmpx *utx)
189 1.3.2.2 nathanw {
190 1.3.2.4 nathanw
191 1.3.2.7 nathanw _DIAGASSERT(utx != NULL);
192 1.3.2.7 nathanw
193 1.3.2.2 nathanw do {
194 1.3.2.2 nathanw switch (ut.ut_type) {
195 1.3.2.2 nathanw case EMPTY:
196 1.3.2.2 nathanw break;
197 1.3.2.2 nathanw case LOGIN_PROCESS:
198 1.3.2.2 nathanw case USER_PROCESS:
199 1.3.2.2 nathanw if (strncmp(ut.ut_line, utx->ut_line,
200 1.3.2.2 nathanw sizeof(ut.ut_line)) == 0)
201 1.3.2.2 nathanw return &ut;
202 1.3.2.2 nathanw break;
203 1.3.2.2 nathanw default:
204 1.3.2.2 nathanw break;
205 1.3.2.2 nathanw }
206 1.3.2.4 nathanw } while (getutxent() != NULL);
207 1.3.2.2 nathanw return NULL;
208 1.3.2.2 nathanw }
209 1.3.2.2 nathanw
210 1.3.2.2 nathanw
211 1.3.2.2 nathanw struct utmpx *
212 1.3.2.2 nathanw pututxline(const struct utmpx *utx)
213 1.3.2.2 nathanw {
214 1.3.2.2 nathanw struct utmpx temp, *u = NULL;
215 1.3.2.2 nathanw int gotlock = 0;
216 1.3.2.2 nathanw
217 1.3.2.7 nathanw _DIAGASSERT(utx != NULL);
218 1.3.2.7 nathanw
219 1.3.2.2 nathanw if (strcmp(_PATH_UTMPX, utfile) == 0 && geteuid() != 0)
220 1.3.2.2 nathanw return utmp_update(utx);
221 1.3.2.2 nathanw
222 1.3.2.2 nathanw if (utx == NULL)
223 1.3.2.2 nathanw return NULL;
224 1.3.2.2 nathanw
225 1.3.2.2 nathanw (void)memcpy(&temp, utx, sizeof(temp));
226 1.3.2.2 nathanw
227 1.3.2.2 nathanw if (fp == NULL) {
228 1.3.2.2 nathanw (void)getutxent();
229 1.3.2.2 nathanw if (fp == NULL)
230 1.3.2.2 nathanw return NULL;
231 1.3.2.2 nathanw }
232 1.3.2.2 nathanw
233 1.3.2.2 nathanw if (getutxid(&temp) == NULL) {
234 1.3.2.2 nathanw setutxent();
235 1.3.2.2 nathanw if (getutxid(&temp) == NULL) {
236 1.3.2.2 nathanw if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
237 1.3.2.2 nathanw return NULL;
238 1.3.2.2 nathanw gotlock++;
239 1.3.2.2 nathanw if (fseeko(fp, (off_t)0, SEEK_END) == -1)
240 1.3.2.2 nathanw goto fail;
241 1.3.2.2 nathanw }
242 1.3.2.2 nathanw }
243 1.3.2.2 nathanw
244 1.3.2.2 nathanw if (!gotlock) {
245 1.3.2.2 nathanw /* we are not appending */
246 1.3.2.2 nathanw if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
247 1.3.2.2 nathanw return NULL;
248 1.3.2.2 nathanw }
249 1.3.2.2 nathanw
250 1.3.2.2 nathanw if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
251 1.3.2.2 nathanw goto fail;
252 1.3.2.2 nathanw
253 1.3.2.2 nathanw if (fflush(fp) == -1)
254 1.3.2.2 nathanw goto fail;
255 1.3.2.2 nathanw
256 1.3.2.2 nathanw u = memcpy(&ut, &temp, sizeof(ut));
257 1.3.2.2 nathanw fail:
258 1.3.2.2 nathanw if (gotlock) {
259 1.3.2.2 nathanw if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
260 1.3.2.2 nathanw return NULL;
261 1.3.2.2 nathanw }
262 1.3.2.2 nathanw return u;
263 1.3.2.2 nathanw }
264 1.3.2.2 nathanw
265 1.3.2.2 nathanw
266 1.3.2.2 nathanw static struct utmpx *
267 1.3.2.2 nathanw utmp_update(const struct utmpx *utx)
268 1.3.2.2 nathanw {
269 1.3.2.2 nathanw char buf[sizeof(*utx) * 4 + 1];
270 1.3.2.2 nathanw pid_t pid;
271 1.3.2.2 nathanw int status;
272 1.3.2.2 nathanw
273 1.3.2.7 nathanw _DIAGASSERT(utx != NULL);
274 1.3.2.7 nathanw
275 1.3.2.2 nathanw (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
276 1.3.2.2 nathanw VIS_WHITE);
277 1.3.2.2 nathanw switch (pid = fork()) {
278 1.3.2.2 nathanw case 0:
279 1.3.2.2 nathanw (void)execl(_PATH_UTMP_UPDATE,
280 1.3.2.8 thorpej strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf, NULL);
281 1.3.2.2 nathanw exit(1);
282 1.3.2.2 nathanw /*NOTREACHED*/
283 1.3.2.2 nathanw case -1:
284 1.3.2.2 nathanw return NULL;
285 1.3.2.2 nathanw default:
286 1.3.2.2 nathanw if (waitpid(pid, &status, 0) == -1)
287 1.3.2.2 nathanw return NULL;
288 1.3.2.2 nathanw if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
289 1.3.2.2 nathanw return memcpy(&ut, utx, sizeof(ut));
290 1.3.2.2 nathanw return NULL;
291 1.3.2.2 nathanw }
292 1.3.2.2 nathanw
293 1.3.2.2 nathanw }
294 1.3.2.2 nathanw
295 1.3.2.6 nathanw /*
296 1.3.2.6 nathanw * The following are extensions and not part of the X/Open spec.
297 1.3.2.6 nathanw */
298 1.3.2.4 nathanw int
299 1.3.2.2 nathanw updwtmpx(const char *file, const struct utmpx *utx)
300 1.3.2.2 nathanw {
301 1.3.2.7 nathanw int fd;
302 1.3.2.7 nathanw
303 1.3.2.7 nathanw _DIAGASSERT(file != NULL);
304 1.3.2.7 nathanw _DIAGASSERT(utx != NULL);
305 1.3.2.7 nathanw
306 1.3.2.7 nathanw fd = open(file, O_WRONLY|O_APPEND|O_EXLOCK);
307 1.3.2.4 nathanw
308 1.3.2.2 nathanw if (fd == -1) {
309 1.3.2.4 nathanw if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1)
310 1.3.2.4 nathanw return -1;
311 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
312 1.3.2.2 nathanw ut.ut_type = SIGNATURE;
313 1.3.2.2 nathanw (void)memcpy(ut.ut_user, vers, sizeof(vers));
314 1.3.2.6 nathanw if (write(fd, &ut, sizeof(ut)) == -1)
315 1.3.2.6 nathanw return -1;
316 1.3.2.2 nathanw }
317 1.3.2.6 nathanw if (write(fd, utx, sizeof(*utx)) == -1)
318 1.3.2.6 nathanw return -1;
319 1.3.2.6 nathanw if (close(fd) == -1)
320 1.3.2.6 nathanw return -1;
321 1.3.2.4 nathanw return 0;
322 1.3.2.2 nathanw }
323 1.3.2.2 nathanw
324 1.3.2.2 nathanw
325 1.3.2.2 nathanw int
326 1.3.2.2 nathanw utmpxname(const char *fname)
327 1.3.2.2 nathanw {
328 1.3.2.7 nathanw size_t len;
329 1.3.2.7 nathanw
330 1.3.2.7 nathanw _DIAGASSERT(fname != NULL);
331 1.3.2.7 nathanw
332 1.3.2.7 nathanw len = strlen(fname);
333 1.3.2.2 nathanw
334 1.3.2.2 nathanw if (len >= sizeof(utfile))
335 1.3.2.2 nathanw return 0;
336 1.3.2.2 nathanw
337 1.3.2.2 nathanw /* must end in x! */
338 1.3.2.2 nathanw if (fname[len - 1] != 'x')
339 1.3.2.2 nathanw return 0;
340 1.3.2.2 nathanw
341 1.3.2.8 thorpej (void)strlcpy(utfile, fname, sizeof(utfile));
342 1.3.2.2 nathanw endutxent();
343 1.3.2.2 nathanw return 1;
344 1.3.2.2 nathanw }
345 1.3.2.3 nathanw
346 1.3.2.3 nathanw
347 1.3.2.3 nathanw void
348 1.3.2.3 nathanw getutmp(const struct utmpx *ux, struct utmp *u)
349 1.3.2.3 nathanw {
350 1.3.2.4 nathanw
351 1.3.2.7 nathanw _DIAGASSERT(ux != NULL);
352 1.3.2.7 nathanw _DIAGASSERT(u != NULL);
353 1.3.2.7 nathanw
354 1.3.2.3 nathanw (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
355 1.3.2.3 nathanw (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
356 1.3.2.3 nathanw (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
357 1.3.2.3 nathanw u->ut_time = ux->ut_tv.tv_sec;
358 1.3.2.3 nathanw }
359 1.3.2.3 nathanw
360 1.3.2.3 nathanw void
361 1.3.2.3 nathanw getutmpx(const struct utmp *u, struct utmpx *ux)
362 1.3.2.3 nathanw {
363 1.3.2.4 nathanw
364 1.3.2.7 nathanw _DIAGASSERT(ux != NULL);
365 1.3.2.7 nathanw _DIAGASSERT(u != NULL);
366 1.3.2.7 nathanw
367 1.3.2.3 nathanw (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
368 1.3.2.3 nathanw (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
369 1.3.2.3 nathanw (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
370 1.3.2.3 nathanw ux->ut_tv.tv_sec = u->ut_time;
371 1.3.2.3 nathanw ux->ut_tv.tv_usec = 0;
372 1.3.2.3 nathanw (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
373 1.3.2.3 nathanw ux->ut_pid = 0;
374 1.3.2.3 nathanw ux->ut_type = USER_PROCESS;
375 1.3.2.3 nathanw ux->ut_session = 0;
376 1.3.2.3 nathanw ux->ut_exit.e_termination = 0;
377 1.3.2.3 nathanw ux->ut_exit.e_exit = 0;
378 1.3.2.3 nathanw }
379 1.3.2.4 nathanw
380 1.3.2.4 nathanw int
381 1.3.2.4 nathanw lastlogxname(const char *fname)
382 1.3.2.4 nathanw {
383 1.3.2.7 nathanw size_t len;
384 1.3.2.7 nathanw
385 1.3.2.7 nathanw _DIAGASSERT(fname != NULL);
386 1.3.2.7 nathanw
387 1.3.2.7 nathanw len = strlen(fname);
388 1.3.2.4 nathanw
389 1.3.2.4 nathanw if (len >= sizeof(llfile))
390 1.3.2.4 nathanw return 0;
391 1.3.2.4 nathanw
392 1.3.2.4 nathanw /* must end in x! */
393 1.3.2.4 nathanw if (fname[len - 1] != 'x')
394 1.3.2.4 nathanw return 0;
395 1.3.2.4 nathanw
396 1.3.2.8 thorpej (void)strlcpy(llfile, fname, sizeof(llfile));
397 1.3.2.4 nathanw return 1;
398 1.3.2.4 nathanw }
399 1.3.2.4 nathanw
400 1.3.2.4 nathanw struct lastlogx *
401 1.3.2.4 nathanw getlastlogx(uid_t uid, struct lastlogx *ll)
402 1.3.2.4 nathanw {
403 1.3.2.4 nathanw DBT key, data;
404 1.3.2.7 nathanw DB *db;
405 1.3.2.7 nathanw
406 1.3.2.7 nathanw _DIAGASSERT(ll != NULL);
407 1.3.2.7 nathanw
408 1.3.2.7 nathanw db = dbopen(llfile, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
409 1.3.2.4 nathanw
410 1.3.2.4 nathanw if (db == NULL)
411 1.3.2.4 nathanw return NULL;
412 1.3.2.4 nathanw
413 1.3.2.4 nathanw key.data = &uid;
414 1.3.2.4 nathanw key.size = sizeof(uid);
415 1.3.2.4 nathanw
416 1.3.2.4 nathanw if ((db->get)(db, &key, &data, 0) != 0)
417 1.3.2.4 nathanw goto error;
418 1.3.2.4 nathanw
419 1.3.2.4 nathanw if (data.size != sizeof(*ll)) {
420 1.3.2.4 nathanw errno = EFTYPE;
421 1.3.2.4 nathanw goto error;
422 1.3.2.4 nathanw }
423 1.3.2.4 nathanw
424 1.3.2.4 nathanw if (ll == NULL)
425 1.3.2.4 nathanw if ((ll = malloc(sizeof(*ll))) == NULL)
426 1.3.2.4 nathanw goto done;
427 1.3.2.4 nathanw
428 1.3.2.4 nathanw (void)memcpy(ll, data.data, sizeof(*ll));
429 1.3.2.4 nathanw goto done;
430 1.3.2.4 nathanw error:
431 1.3.2.4 nathanw ll = NULL;
432 1.3.2.4 nathanw done:
433 1.3.2.4 nathanw (db->close)(db);
434 1.3.2.4 nathanw return ll;
435 1.3.2.4 nathanw }
436 1.3.2.4 nathanw
437 1.3.2.4 nathanw int
438 1.3.2.4 nathanw updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
439 1.3.2.4 nathanw {
440 1.3.2.4 nathanw DBT key, data;
441 1.3.2.4 nathanw int error = 0;
442 1.3.2.7 nathanw DB *db;
443 1.3.2.7 nathanw
444 1.3.2.7 nathanw _DIAGASSERT(fname != NULL);
445 1.3.2.7 nathanw _DIAGASSERT(ll != NULL);
446 1.3.2.7 nathanw
447 1.3.2.7 nathanw db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0, DB_HASH, NULL);
448 1.3.2.4 nathanw
449 1.3.2.4 nathanw if (db == NULL)
450 1.3.2.4 nathanw return -1;
451 1.3.2.4 nathanw
452 1.3.2.4 nathanw key.data = &uid;
453 1.3.2.4 nathanw key.size = sizeof(uid);
454 1.3.2.4 nathanw data.data = ll;
455 1.3.2.4 nathanw data.size = sizeof(*ll);
456 1.3.2.4 nathanw if ((db->put)(db, &key, &data, 0) != 0)
457 1.3.2.4 nathanw error = -1;
458 1.3.2.4 nathanw
459 1.3.2.4 nathanw (db->close)(db);
460 1.3.2.4 nathanw return error;
461 1.3.2.4 nathanw }
462