update from main archive 970619
[glibc/history.git] / login / utmp_file.c
blob51b33226e02204bd8847b8ecb0c2b434eb40b03e
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>
4 and Paul Janzen <pcj@primenet.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <utmp.h>
31 #include "utmp-private.h"
34 /* Descriptor for the file and position. */
35 static int file_fd = INT_MIN;
36 static off_t file_offset;
38 static struct utmp last_entry;
41 /* Functions defined here. */
42 static int setutent_file (int reset);
43 static int getutent_r_file (struct utmp *buffer, struct utmp **result);
44 static int getutid_r_file (const struct utmp *key, struct utmp *buffer,
45 struct utmp **result);
46 static int getutline_r_file (const struct utmp *key, struct utmp *buffer,
47 struct utmp **result);
48 static struct utmp *pututline_file (const struct utmp *data);
49 static void endutent_file (void);
50 static int updwtmp_file (const char *file, const struct utmp *utmp);
52 /* Jump table for file functions. */
53 struct utfuncs __libc_utmp_file_functions =
55 setutent_file,
56 getutent_r_file,
57 getutid_r_file,
58 getutline_r_file,
59 pututline_file,
60 endutent_file,
61 updwtmp_file
65 static int
66 setutent_file (int reset)
68 if (file_fd == INT_MIN)
70 file_fd = open (__libc_utmp_file_name, O_RDWR);
71 if (file_fd == -1)
73 /* Hhm, read-write access did not work. Try read-only. */
74 file_fd = open (__libc_utmp_file_name, O_RDONLY);
75 if (file_fd == -1)
77 perror (_("while opening UTMP file"));
78 return 0;
81 file_offset = 0;
83 #if _HAVE_UT_TYPE - 0
84 /* Make sure the entry won't match. */
85 last_entry.ut_type = -1;
86 #endif
88 else if (reset)
90 lseek (file_fd, 0, SEEK_SET);
92 /* Remember we are at beginning of file. */
93 file_offset = 0;
95 #if _HAVE_UT_TYPE - 0
96 /* Make sure the entry won't match. */
97 last_entry.ut_type = -1;
98 #endif
101 return 1;
105 static void
106 endutent_file (void)
108 if (file_fd >= 0)
109 close (file_fd);
111 file_fd = INT_MIN;
115 static int
116 getutent_r_file (struct utmp *buffer, struct utmp **result)
118 ssize_t nbytes;
119 struct flock fl; /* Information struct for locking. */
121 /* Open utmp file if not already done. */
122 if (file_fd == INT_MIN)
123 setutent_file (1);
125 if (file_fd == -1 || file_offset == -1l)
127 /* Not available. */
128 *result = NULL;
129 return -1;
132 /* XXX The following is not perfect. Instead of locking the file itself
133 Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl> suggests to
134 use an extra locking file. */
136 /* Try to get the lock. */
137 memset (&fl, '\0', sizeof (struct flock));
138 fl.l_type = F_RDLCK;
139 fl.l_whence = SEEK_SET;
140 fcntl (file_fd, F_SETLKW, &fl);
142 /* Read the next entry. */
143 nbytes = read (file_fd, &last_entry, sizeof (struct utmp));
145 /* And unlock the file. */
146 fl.l_type = F_UNLCK;
147 fcntl (file_fd, F_SETLKW, &fl);
149 if (nbytes != sizeof (struct utmp))
151 file_offset = -1l;
152 *result = NULL;
153 return -1;
156 /* Update position pointer. */
157 file_offset += sizeof (struct utmp);
159 memcpy (buffer, &last_entry, sizeof (struct utmp));
160 *result = buffer;
162 return 0;
166 /* For implementing this function we don't use the getutent_r function
167 because we can avoid the reposition on every new entry this way. */
168 static int
169 getutline_r_file (const struct utmp *line, struct utmp *buffer,
170 struct utmp **result)
172 struct flock fl;
174 if (file_fd < 0 || file_offset == -1l)
176 *result = NULL;
177 return -1;
180 /* Try to get the lock. */
181 memset (&fl, '\0', sizeof (struct flock));
182 fl.l_type = F_RDLCK;
183 fl.l_whence = SEEK_SET;
184 fcntl (file_fd, F_SETLKW, &fl);
186 while (1)
188 /* Read the next entry. */
189 if (read (file_fd, &last_entry, sizeof (struct utmp))
190 != sizeof (struct utmp))
192 __set_errno (ESRCH);
193 file_offset = -1l;
194 *result = NULL;
195 goto unlock_return;
197 file_offset += sizeof (struct utmp);
199 /* Stop if we found a user or login entry. */
200 if (
201 #if _HAVE_UT_TYPE - 0
202 (last_entry.ut_type == USER_PROCESS
203 || last_entry.ut_type == LOGIN_PROCESS)
205 #endif
206 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
207 break;
210 memcpy (buffer, &last_entry, sizeof (struct utmp));
211 *result = buffer;
213 unlock_return:
214 /* And unlock the file. */
215 fl.l_type = F_UNLCK;
216 fcntl (file_fd, F_SETLKW, &fl);
218 return ((result == NULL) ? -1 : 0);
222 static int
223 proc_utmp_eq (const struct utmp *entry, const struct utmp *match)
225 return
227 #if _HAVE_UT_TYPE - 0
228 (entry->ut_type == INIT_PROCESS
229 || entry->ut_type == LOGIN_PROCESS
230 || entry->ut_type == USER_PROCESS
231 || entry->ut_type == DEAD_PROCESS)
233 (match->ut_type == INIT_PROCESS
234 || match->ut_type == LOGIN_PROCESS
235 || match->ut_type == USER_PROCESS
236 || match->ut_type == DEAD_PROCESS)
238 #endif
239 #if _HAVE_UT_ID - 0
240 (entry->ut_id[0] && match->ut_id[0]
241 ? strncmp (entry->ut_id, match->ut_id, sizeof match->ut_id) == 0
242 : strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0)
243 #else
244 strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0
245 #endif
249 static int
250 internal_getut_r (const struct utmp *id, struct utmp *buffer)
252 int result = -1;
253 struct flock fl;
255 /* Try to get the lock. */
256 memset (&fl, '\0', sizeof (struct flock));
257 fl.l_type = F_RDLCK;
258 fl.l_whence = SEEK_SET;
259 fcntl (file_fd, F_SETLKW, &fl);
261 #if _HAVE_UT_TYPE - 0
262 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
263 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
265 /* Search for next entry with type RUN_LVL, BOOT_TIME,
266 OLD_TIME, or NEW_TIME. */
268 while (1)
270 /* Read the next entry. */
271 if (read (file_fd, buffer, sizeof (struct utmp))
272 != sizeof (struct utmp))
274 __set_errno (ESRCH);
275 file_offset = -1l;
276 goto unlock_return;
278 file_offset += sizeof (struct utmp);
280 if (id->ut_type == buffer->ut_type)
281 break;
284 else
285 #endif /* _HAVE_UT_TYPE */
287 /* Search for the next entry with the specified ID and with type
288 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
290 while (1)
292 /* Read the next entry. */
293 if (read (file_fd, buffer, sizeof (struct utmp))
294 != sizeof (struct utmp))
296 __set_errno (ESRCH);
297 file_offset = -1l;
298 goto unlock_return;
300 file_offset += sizeof (struct utmp);
302 if (proc_utmp_eq (buffer, id))
303 break;
307 result = 0;
309 unlock_return:
310 /* And unlock the file. */
311 fl.l_type = F_UNLCK;
312 fcntl (file_fd, F_SETLKW, &fl);
314 return result;
318 /* For implementing this function we don't use the getutent_r function
319 because we can avoid the reposition on every new entry this way. */
320 static int
321 getutid_r_file (const struct utmp *id, struct utmp *buffer,
322 struct utmp **result)
324 if (file_fd < 0 || file_offset == -1l)
326 *result = NULL;
327 return -1;
330 if (internal_getut_r (id, &last_entry) < 0)
332 *result = NULL;
333 return -1;
336 memcpy (buffer, &last_entry, sizeof (struct utmp));
337 *result = buffer;
339 return 0;
343 static struct utmp *
344 pututline_file (const struct utmp *data)
346 struct flock fl; /* Information struct for locking. */
347 struct utmp buffer;
348 struct utmp *pbuf;
349 int found;
351 if (file_fd < 0)
352 /* Something went wrong. */
353 return NULL;
355 if (file_fd == INT_MIN)
356 /* The file is closed. Open it again. */
357 setutent_file (0);
359 /* Find the correct place to insert the data. */
360 if (file_offset > 0
361 && (
362 #if _HAVE_UT_TYPE - 0
363 (last_entry.ut_type == data->ut_type
364 && (last_entry.ut_type == RUN_LVL
365 || last_entry.ut_type == BOOT_TIME
366 || last_entry.ut_type == OLD_TIME
367 || last_entry.ut_type == NEW_TIME))
369 #endif
370 proc_utmp_eq (&last_entry, data)))
371 found = 1;
372 else
373 found = internal_getut_r (data, &buffer);
375 /* Try to lock the file. */
376 memset (&fl, '\0', sizeof (struct flock));
377 fl.l_type = F_WRLCK;
378 fl.l_whence = SEEK_SET;
379 fcntl (file_fd, F_SETLKW, &fl);
381 if (found < 0)
383 /* We append the next entry. */
384 file_offset = lseek (file_fd, 0, SEEK_END);
385 if (file_offset % sizeof (struct utmp) != 0)
387 file_offset -= file_offset % sizeof (struct utmp);
388 ftruncate (file_fd, file_offset);
390 if (lseek (file_fd, 0, SEEK_END) < 0)
392 pbuf = NULL;
393 goto unlock_return;
397 else
399 /* We replace the just read entry. */
400 file_offset -= sizeof (struct utmp);
401 lseek (file_fd, file_offset, SEEK_SET);
404 /* Write the new data. */
405 if (write (file_fd, data, sizeof (struct utmp)) != sizeof (struct utmp)
406 /* If we appended a new record this is only partially written.
407 Remove it. */
408 && found < 0)
410 (void) ftruncate (file_fd, file_offset);
411 pbuf = NULL;
413 else
415 file_offset += sizeof (struct utmp);
416 pbuf = (struct utmp *) data;
419 unlock_return:
420 /* And unlock the file. */
421 fl.l_type = F_UNLCK;
422 fcntl (file_fd, F_SETLKW, &fl);
424 return pbuf;
428 static int
429 updwtmp_file (const char *file, const struct utmp *utmp)
431 int result = -1;
432 struct flock fl;
433 off_t offset;
434 int fd;
436 /* Open WTMP file. */
437 fd = open (file, O_WRONLY);
438 if (fd < 0)
439 return -1;
441 /* Try to get the lock. */
442 memset (&fl, '\0', sizeof (struct flock));
443 fl.l_type = F_WRLCK;
444 fl.l_whence = SEEK_SET;
445 fcntl (fd, F_SETLKW, &fl);
447 /* Remember original size of log file. */
448 offset = lseek (fd, 0, SEEK_END);
449 if (offset % sizeof (struct utmp) != 0)
451 offset -= offset % sizeof (struct utmp);
452 ftruncate (fd, offset);
454 if (lseek (fd, 0, SEEK_END) < 0)
455 goto unlock_return;
458 /* Write the entry. If we can't write all the bytes, reset the file
459 size back to the original size. That way, no partial entries
460 will remain. */
461 if (write (fd, utmp, sizeof (struct utmp)) != sizeof (struct utmp))
463 ftruncate (fd, offset);
464 goto unlock_return;
467 result = 0;
469 unlock_return:
470 /* And unlock the file. */
471 fl.l_type = F_UNLCK;
472 fcntl (fd, F_SETLKW, &fl);
474 /* Close WTMP file. */
475 close (fd);
477 return result;