utmpx.c revision 1.3.2.4 1 1.3.2.4 nathanw /* $NetBSD: utmpx.c,v 1.3.2.4 2002/08/01 03:28:12 nathanw 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.4 nathanw __RCSID("$NetBSD: utmpx.c,v 1.3.2.4 2002/08/01 03:28:12 nathanw 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.2 nathanw #include <stdio.h>
52 1.3.2.2 nathanw #include <string.h>
53 1.3.2.2 nathanw #include <vis.h>
54 1.3.2.3 nathanw #include <utmp.h>
55 1.3.2.2 nathanw #include <utmpx.h>
56 1.3.2.2 nathanw #include <unistd.h>
57 1.3.2.2 nathanw #include <fcntl.h>
58 1.3.2.4 nathanw #include <errno.h>
59 1.3.2.4 nathanw #include <db.h>
60 1.3.2.2 nathanw
61 1.3.2.2 nathanw static FILE *fp;
62 1.3.2.2 nathanw static struct utmpx ut;
63 1.3.2.2 nathanw static char utfile[MAXPATHLEN] = _PATH_UTMPX;
64 1.3.2.4 nathanw static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
65 1.3.2.2 nathanw
66 1.3.2.2 nathanw static struct utmpx *utmp_update(const struct utmpx *);
67 1.3.2.2 nathanw
68 1.3.2.2 nathanw static const char vers[] = "utmpx-1.00";
69 1.3.2.2 nathanw
70 1.3.2.2 nathanw void
71 1.3.2.2 nathanw setutxent()
72 1.3.2.2 nathanw {
73 1.3.2.4 nathanw
74 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
75 1.3.2.2 nathanw if (fp == NULL)
76 1.3.2.2 nathanw return;
77 1.3.2.2 nathanw (void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
78 1.3.2.2 nathanw }
79 1.3.2.2 nathanw
80 1.3.2.2 nathanw
81 1.3.2.2 nathanw void
82 1.3.2.2 nathanw endutxent()
83 1.3.2.2 nathanw {
84 1.3.2.4 nathanw
85 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
86 1.3.2.4 nathanw if (fp != NULL) {
87 1.3.2.2 nathanw (void)fclose(fp);
88 1.3.2.4 nathanw fp = NULL;
89 1.3.2.4 nathanw }
90 1.3.2.2 nathanw }
91 1.3.2.2 nathanw
92 1.3.2.2 nathanw
93 1.3.2.2 nathanw struct utmpx *
94 1.3.2.2 nathanw getutxent()
95 1.3.2.2 nathanw {
96 1.3.2.4 nathanw
97 1.3.2.2 nathanw if (fp == NULL) {
98 1.3.2.2 nathanw struct stat st;
99 1.3.2.2 nathanw
100 1.3.2.2 nathanw if ((fp = fopen(utfile, "r+")) == NULL)
101 1.3.2.3 nathanw if ((fp = fopen(utfile, "w+")) == NULL)
102 1.3.2.3 nathanw if ((fp = fopen(utfile, "r")) == NULL)
103 1.3.2.3 nathanw goto fail;
104 1.3.2.2 nathanw
105 1.3.2.2 nathanw /* get file size in order to check if new file */
106 1.3.2.2 nathanw if (fstat(fileno(fp), &st) == -1)
107 1.3.2.2 nathanw goto failclose;
108 1.3.2.2 nathanw
109 1.3.2.2 nathanw if (st.st_size == 0) {
110 1.3.2.2 nathanw /* new file, add signature record */
111 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
112 1.3.2.2 nathanw ut.ut_type = SIGNATURE;
113 1.3.2.2 nathanw (void)memcpy(ut.ut_user, vers, sizeof(vers));
114 1.3.2.3 nathanw if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
115 1.3.2.2 nathanw goto failclose;
116 1.3.2.2 nathanw } else {
117 1.3.2.2 nathanw /* old file, read signature record */
118 1.3.2.3 nathanw if (fread(&ut, sizeof(ut), 1, fp) != 1)
119 1.3.2.2 nathanw goto failclose;
120 1.3.2.2 nathanw if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
121 1.3.2.2 nathanw ut.ut_type != SIGNATURE)
122 1.3.2.2 nathanw goto failclose;
123 1.3.2.2 nathanw }
124 1.3.2.2 nathanw }
125 1.3.2.2 nathanw
126 1.3.2.3 nathanw if (fread(&ut, sizeof(ut), 1, fp) != 1)
127 1.3.2.2 nathanw goto fail;
128 1.3.2.2 nathanw
129 1.3.2.2 nathanw return &ut;
130 1.3.2.2 nathanw failclose:
131 1.3.2.2 nathanw (void)fclose(fp);
132 1.3.2.2 nathanw fail:
133 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
134 1.3.2.2 nathanw return NULL;
135 1.3.2.2 nathanw }
136 1.3.2.2 nathanw
137 1.3.2.2 nathanw
138 1.3.2.2 nathanw struct utmpx *
139 1.3.2.2 nathanw getutxid(const struct utmpx *utx)
140 1.3.2.2 nathanw {
141 1.3.2.4 nathanw
142 1.3.2.2 nathanw if (utx->ut_type == EMPTY)
143 1.3.2.2 nathanw return NULL;
144 1.3.2.2 nathanw
145 1.3.2.2 nathanw do {
146 1.3.2.2 nathanw if (ut.ut_type == EMPTY)
147 1.3.2.2 nathanw continue;
148 1.3.2.2 nathanw switch (utx->ut_type) {
149 1.3.2.2 nathanw case EMPTY:
150 1.3.2.2 nathanw return NULL;
151 1.3.2.2 nathanw case RUN_LVL:
152 1.3.2.2 nathanw case BOOT_TIME:
153 1.3.2.2 nathanw case OLD_TIME:
154 1.3.2.2 nathanw case NEW_TIME:
155 1.3.2.2 nathanw if (ut.ut_type == utx->ut_type)
156 1.3.2.2 nathanw return &ut;
157 1.3.2.2 nathanw break;
158 1.3.2.2 nathanw case INIT_PROCESS:
159 1.3.2.2 nathanw case LOGIN_PROCESS:
160 1.3.2.2 nathanw case USER_PROCESS:
161 1.3.2.2 nathanw case DEAD_PROCESS:
162 1.3.2.2 nathanw switch (ut.ut_type) {
163 1.3.2.2 nathanw case INIT_PROCESS:
164 1.3.2.2 nathanw case LOGIN_PROCESS:
165 1.3.2.2 nathanw case USER_PROCESS:
166 1.3.2.2 nathanw case DEAD_PROCESS:
167 1.3.2.2 nathanw if (memcmp(ut.ut_id, utx->ut_id,
168 1.3.2.2 nathanw sizeof(ut.ut_id)) == 0)
169 1.3.2.2 nathanw return &ut;
170 1.3.2.2 nathanw break;
171 1.3.2.2 nathanw default:
172 1.3.2.2 nathanw break;
173 1.3.2.2 nathanw }
174 1.3.2.2 nathanw break;
175 1.3.2.2 nathanw default:
176 1.3.2.2 nathanw return NULL;
177 1.3.2.2 nathanw }
178 1.3.2.4 nathanw } while (getutxent() != NULL);
179 1.3.2.2 nathanw return NULL;
180 1.3.2.2 nathanw }
181 1.3.2.2 nathanw
182 1.3.2.2 nathanw
183 1.3.2.2 nathanw struct utmpx *
184 1.3.2.2 nathanw getutxline(const struct utmpx *utx)
185 1.3.2.2 nathanw {
186 1.3.2.4 nathanw
187 1.3.2.2 nathanw do {
188 1.3.2.2 nathanw switch (ut.ut_type) {
189 1.3.2.2 nathanw case EMPTY:
190 1.3.2.2 nathanw break;
191 1.3.2.2 nathanw case LOGIN_PROCESS:
192 1.3.2.2 nathanw case USER_PROCESS:
193 1.3.2.2 nathanw if (strncmp(ut.ut_line, utx->ut_line,
194 1.3.2.2 nathanw sizeof(ut.ut_line)) == 0)
195 1.3.2.2 nathanw return &ut;
196 1.3.2.2 nathanw break;
197 1.3.2.2 nathanw default:
198 1.3.2.2 nathanw break;
199 1.3.2.2 nathanw }
200 1.3.2.4 nathanw } while (getutxent() != NULL);
201 1.3.2.2 nathanw return NULL;
202 1.3.2.2 nathanw }
203 1.3.2.2 nathanw
204 1.3.2.2 nathanw
205 1.3.2.2 nathanw struct utmpx *
206 1.3.2.2 nathanw pututxline(const struct utmpx *utx)
207 1.3.2.2 nathanw {
208 1.3.2.2 nathanw struct utmpx temp, *u = NULL;
209 1.3.2.2 nathanw int gotlock = 0;
210 1.3.2.2 nathanw
211 1.3.2.2 nathanw if (strcmp(_PATH_UTMPX, utfile) == 0 && geteuid() != 0)
212 1.3.2.2 nathanw return utmp_update(utx);
213 1.3.2.2 nathanw
214 1.3.2.2 nathanw if (utx == NULL)
215 1.3.2.2 nathanw return NULL;
216 1.3.2.2 nathanw
217 1.3.2.2 nathanw (void)memcpy(&temp, utx, sizeof(temp));
218 1.3.2.2 nathanw
219 1.3.2.2 nathanw if (fp == NULL) {
220 1.3.2.2 nathanw (void)getutxent();
221 1.3.2.2 nathanw if (fp == NULL)
222 1.3.2.2 nathanw return NULL;
223 1.3.2.2 nathanw }
224 1.3.2.2 nathanw
225 1.3.2.2 nathanw if (getutxid(&temp) == NULL) {
226 1.3.2.2 nathanw setutxent();
227 1.3.2.2 nathanw if (getutxid(&temp) == NULL) {
228 1.3.2.2 nathanw if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
229 1.3.2.2 nathanw return NULL;
230 1.3.2.2 nathanw gotlock++;
231 1.3.2.2 nathanw if (fseeko(fp, (off_t)0, SEEK_END) == -1)
232 1.3.2.2 nathanw goto fail;
233 1.3.2.2 nathanw }
234 1.3.2.2 nathanw }
235 1.3.2.2 nathanw
236 1.3.2.2 nathanw if (!gotlock) {
237 1.3.2.2 nathanw /* we are not appending */
238 1.3.2.2 nathanw if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
239 1.3.2.2 nathanw return NULL;
240 1.3.2.2 nathanw }
241 1.3.2.2 nathanw
242 1.3.2.2 nathanw if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
243 1.3.2.2 nathanw goto fail;
244 1.3.2.2 nathanw
245 1.3.2.2 nathanw if (fflush(fp) == -1)
246 1.3.2.2 nathanw goto fail;
247 1.3.2.2 nathanw
248 1.3.2.2 nathanw u = memcpy(&ut, &temp, sizeof(ut));
249 1.3.2.2 nathanw fail:
250 1.3.2.2 nathanw if (gotlock) {
251 1.3.2.2 nathanw if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
252 1.3.2.2 nathanw return NULL;
253 1.3.2.2 nathanw }
254 1.3.2.2 nathanw return u;
255 1.3.2.2 nathanw }
256 1.3.2.2 nathanw
257 1.3.2.2 nathanw
258 1.3.2.2 nathanw static struct utmpx *
259 1.3.2.2 nathanw utmp_update(const struct utmpx *utx)
260 1.3.2.2 nathanw {
261 1.3.2.2 nathanw char buf[sizeof(*utx) * 4 + 1];
262 1.3.2.2 nathanw pid_t pid;
263 1.3.2.2 nathanw int status;
264 1.3.2.2 nathanw
265 1.3.2.2 nathanw (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
266 1.3.2.2 nathanw VIS_WHITE);
267 1.3.2.2 nathanw switch (pid = fork()) {
268 1.3.2.2 nathanw case 0:
269 1.3.2.2 nathanw (void)execl(_PATH_UTMP_UPDATE,
270 1.3.2.2 nathanw strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf);
271 1.3.2.2 nathanw exit(1);
272 1.3.2.2 nathanw /*NOTREACHED*/
273 1.3.2.2 nathanw case -1:
274 1.3.2.2 nathanw return NULL;
275 1.3.2.2 nathanw default:
276 1.3.2.2 nathanw if (waitpid(pid, &status, 0) == -1)
277 1.3.2.2 nathanw return NULL;
278 1.3.2.2 nathanw if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
279 1.3.2.2 nathanw return memcpy(&ut, utx, sizeof(ut));
280 1.3.2.2 nathanw return NULL;
281 1.3.2.2 nathanw }
282 1.3.2.2 nathanw
283 1.3.2.2 nathanw }
284 1.3.2.2 nathanw
285 1.3.2.4 nathanw int
286 1.3.2.2 nathanw updwtmpx(const char *file, const struct utmpx *utx)
287 1.3.2.2 nathanw {
288 1.3.2.4 nathanw int fd = open(file, O_WRONLY|O_APPEND|O_EXLOCK);
289 1.3.2.4 nathanw
290 1.3.2.2 nathanw if (fd == -1) {
291 1.3.2.4 nathanw if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1)
292 1.3.2.4 nathanw return -1;
293 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
294 1.3.2.2 nathanw ut.ut_type = SIGNATURE;
295 1.3.2.2 nathanw (void)memcpy(ut.ut_user, vers, sizeof(vers));
296 1.3.2.2 nathanw (void)write(fd, &ut, sizeof(ut));
297 1.3.2.2 nathanw }
298 1.3.2.2 nathanw (void)write(fd, utx, sizeof(*utx));
299 1.3.2.2 nathanw (void)close(fd);
300 1.3.2.4 nathanw return 0;
301 1.3.2.2 nathanw }
302 1.3.2.2 nathanw
303 1.3.2.2 nathanw
304 1.3.2.2 nathanw /*
305 1.3.2.2 nathanw * The following are extensions and not part of the X/Open spec
306 1.3.2.2 nathanw */
307 1.3.2.2 nathanw int
308 1.3.2.2 nathanw utmpxname(const char *fname)
309 1.3.2.2 nathanw {
310 1.3.2.2 nathanw size_t len = strlen(fname);
311 1.3.2.2 nathanw
312 1.3.2.2 nathanw if (len >= sizeof(utfile))
313 1.3.2.2 nathanw return 0;
314 1.3.2.2 nathanw
315 1.3.2.2 nathanw /* must end in x! */
316 1.3.2.2 nathanw if (fname[len - 1] != 'x')
317 1.3.2.2 nathanw return 0;
318 1.3.2.2 nathanw
319 1.3.2.2 nathanw (void)strcpy(utfile, fname);
320 1.3.2.2 nathanw endutxent();
321 1.3.2.2 nathanw return 1;
322 1.3.2.2 nathanw }
323 1.3.2.3 nathanw
324 1.3.2.3 nathanw
325 1.3.2.3 nathanw void
326 1.3.2.3 nathanw getutmp(const struct utmpx *ux, struct utmp *u)
327 1.3.2.3 nathanw {
328 1.3.2.4 nathanw
329 1.3.2.3 nathanw (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
330 1.3.2.3 nathanw (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
331 1.3.2.3 nathanw (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
332 1.3.2.3 nathanw u->ut_time = ux->ut_tv.tv_sec;
333 1.3.2.3 nathanw }
334 1.3.2.3 nathanw
335 1.3.2.3 nathanw void
336 1.3.2.3 nathanw getutmpx(const struct utmp *u, struct utmpx *ux)
337 1.3.2.3 nathanw {
338 1.3.2.4 nathanw
339 1.3.2.3 nathanw (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
340 1.3.2.3 nathanw (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
341 1.3.2.3 nathanw (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
342 1.3.2.3 nathanw ux->ut_tv.tv_sec = u->ut_time;
343 1.3.2.3 nathanw ux->ut_tv.tv_usec = 0;
344 1.3.2.3 nathanw (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
345 1.3.2.3 nathanw ux->ut_pid = 0;
346 1.3.2.3 nathanw ux->ut_type = USER_PROCESS;
347 1.3.2.3 nathanw ux->ut_session = 0;
348 1.3.2.3 nathanw ux->ut_exit.e_termination = 0;
349 1.3.2.3 nathanw ux->ut_exit.e_exit = 0;
350 1.3.2.3 nathanw }
351 1.3.2.4 nathanw
352 1.3.2.4 nathanw int
353 1.3.2.4 nathanw lastlogxname(const char *fname)
354 1.3.2.4 nathanw {
355 1.3.2.4 nathanw size_t len = strlen(fname);
356 1.3.2.4 nathanw
357 1.3.2.4 nathanw if (len >= sizeof(llfile))
358 1.3.2.4 nathanw return 0;
359 1.3.2.4 nathanw
360 1.3.2.4 nathanw /* must end in x! */
361 1.3.2.4 nathanw if (fname[len - 1] != 'x')
362 1.3.2.4 nathanw return 0;
363 1.3.2.4 nathanw
364 1.3.2.4 nathanw (void)strcpy(llfile, fname);
365 1.3.2.4 nathanw return 1;
366 1.3.2.4 nathanw }
367 1.3.2.4 nathanw
368 1.3.2.4 nathanw struct lastlogx *
369 1.3.2.4 nathanw getlastlogx(uid_t uid, struct lastlogx *ll)
370 1.3.2.4 nathanw {
371 1.3.2.4 nathanw DBT key, data;
372 1.3.2.4 nathanw DB *db = dbopen(llfile, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
373 1.3.2.4 nathanw
374 1.3.2.4 nathanw if (db == NULL)
375 1.3.2.4 nathanw return NULL;
376 1.3.2.4 nathanw
377 1.3.2.4 nathanw key.data = &uid;
378 1.3.2.4 nathanw key.size = sizeof(uid);
379 1.3.2.4 nathanw
380 1.3.2.4 nathanw if ((db->get)(db, &key, &data, 0) != 0)
381 1.3.2.4 nathanw goto error;
382 1.3.2.4 nathanw
383 1.3.2.4 nathanw if (data.size != sizeof(*ll)) {
384 1.3.2.4 nathanw errno = EFTYPE;
385 1.3.2.4 nathanw goto error;
386 1.3.2.4 nathanw }
387 1.3.2.4 nathanw
388 1.3.2.4 nathanw if (ll == NULL)
389 1.3.2.4 nathanw if ((ll = malloc(sizeof(*ll))) == NULL)
390 1.3.2.4 nathanw goto done;
391 1.3.2.4 nathanw
392 1.3.2.4 nathanw (void)memcpy(ll, data.data, sizeof(*ll));
393 1.3.2.4 nathanw goto done;
394 1.3.2.4 nathanw error:
395 1.3.2.4 nathanw ll = NULL;
396 1.3.2.4 nathanw done:
397 1.3.2.4 nathanw (db->close)(db);
398 1.3.2.4 nathanw return ll;
399 1.3.2.4 nathanw }
400 1.3.2.4 nathanw
401 1.3.2.4 nathanw int
402 1.3.2.4 nathanw updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
403 1.3.2.4 nathanw {
404 1.3.2.4 nathanw DBT key, data;
405 1.3.2.4 nathanw int error = 0;
406 1.3.2.4 nathanw DB *db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0, DB_HASH, NULL);
407 1.3.2.4 nathanw
408 1.3.2.4 nathanw if (db == NULL)
409 1.3.2.4 nathanw return -1;
410 1.3.2.4 nathanw
411 1.3.2.4 nathanw key.data = &uid;
412 1.3.2.4 nathanw key.size = sizeof(uid);
413 1.3.2.4 nathanw data.data = ll;
414 1.3.2.4 nathanw data.size = sizeof(*ll);
415 1.3.2.4 nathanw if ((db->put)(db, &key, &data, 0) != 0)
416 1.3.2.4 nathanw error = -1;
417 1.3.2.4 nathanw
418 1.3.2.4 nathanw (db->close)(db);
419 1.3.2.4 nathanw return error;
420 1.3.2.4 nathanw }
421