Update.
[glibc.git] / sysdeps / generic / utmp_file.c
blobddf49467efea3b60248d47b043c79f5eb1a7af91
1 /* Copyright (C) 1996, 1997, 1998 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 <assert.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <utmp.h>
30 #include "utmp-private.h"
33 /* Descriptor for the file and position. */
34 static int file_fd = -1;
35 static off_t file_offset;
37 /* Cache for the last read entry. */
38 static struct utmp last_entry;
41 /* Locking timeout. */
42 #ifndef TIMEOUT
43 # define TIMEOUT 1
44 #endif
46 /* Do-nothing handler for locking timeout. */
47 static void timeout_handler (int signum) {};
49 #define LOCK_FILE(fd, type) \
50 { \
51 struct flock fl; \
52 struct sigaction action, old_action; \
53 unsigned int old_timeout; \
55 /* Cancel any existing alarm. */ \
56 old_timeout = alarm (0); \
58 /* Establish signal handler. */ \
59 action.sa_handler = timeout_handler; \
60 sigemptyset (&action.sa_mask); \
61 action.sa_flags = 0; \
62 __sigaction (SIGALRM, &action, &old_action); \
64 alarm (TIMEOUT); \
66 /* Try to get the lock. */ \
67 memset (&fl, '\0', sizeof (struct flock)); \
68 fl.l_type = (type); \
69 fl.l_whence = SEEK_SET; \
70 __fcntl ((fd), F_SETLKW, &fl)
72 #define UNLOCK_FILE(fd) \
73 /* Unlock the file. */ \
74 fl.l_type = F_UNLCK; \
75 __fcntl ((fd), F_SETLKW, &fl); \
77 /* Reset the signal handler and alarm. */ \
78 __sigaction (SIGALRM, &old_action, NULL); \
79 alarm (old_timeout); \
80 } while (0)
83 /* Functions defined here. */
84 static int setutent_file (void);
85 static int getutent_r_file (struct utmp *buffer, struct utmp **result);
86 static int getutid_r_file (const struct utmp *key, struct utmp *buffer,
87 struct utmp **result);
88 static int getutline_r_file (const struct utmp *key, struct utmp *buffer,
89 struct utmp **result);
90 static struct utmp *pututline_file (const struct utmp *data);
91 static void endutent_file (void);
92 static int updwtmp_file (const char *file, const struct utmp *utmp);
94 /* Jump table for file functions. */
95 struct utfuncs __libc_utmp_file_functions =
97 setutent_file,
98 getutent_r_file,
99 getutid_r_file,
100 getutline_r_file,
101 pututline_file,
102 endutent_file,
103 updwtmp_file
107 #ifndef TRANSFORM_UTMP_FILE_NAME
108 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
109 #endif
111 static int
112 setutent_file (void)
114 if (file_fd < 0)
116 const char *file_name;
118 file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
120 file_fd = __open (file_name, O_RDWR);
121 if (file_fd == -1)
123 /* Hhm, read-write access did not work. Try read-only. */
124 file_fd = __open (file_name, O_RDONLY);
125 if (file_fd == -1)
126 return 0;
130 __lseek (file_fd, 0, SEEK_SET);
131 file_offset = 0;
133 #if _HAVE_UT_TYPE - 0
134 /* Make sure the entry won't match. */
135 last_entry.ut_type = -1;
136 #endif
138 return 1;
142 static int
143 getutent_r_file (struct utmp *buffer, struct utmp **result)
145 ssize_t nbytes;
147 assert (file_fd >= 0);
149 if (file_offset == -1l)
151 /* Not available. */
152 *result = NULL;
153 return -1;
156 LOCK_FILE (file_fd, F_RDLCK);
158 /* Read the next entry. */
159 nbytes = __read (file_fd, &last_entry, sizeof (struct utmp));
161 UNLOCK_FILE (file_fd);
163 if (nbytes != sizeof (struct utmp))
165 file_offset = -1l;
166 *result = NULL;
167 return -1;
170 /* Update position pointer. */
171 file_offset += sizeof (struct utmp);
173 memcpy (buffer, &last_entry, sizeof (struct utmp));
174 *result = buffer;
176 return 0;
180 static int
181 proc_utmp_eq (const struct utmp *entry, const struct utmp *match)
183 return
185 #if _HAVE_UT_TYPE - 0
186 (entry->ut_type == INIT_PROCESS
187 || entry->ut_type == LOGIN_PROCESS
188 || entry->ut_type == USER_PROCESS
189 || entry->ut_type == DEAD_PROCESS)
191 (match->ut_type == INIT_PROCESS
192 || match->ut_type == LOGIN_PROCESS
193 || match->ut_type == USER_PROCESS
194 || match->ut_type == DEAD_PROCESS)
196 #endif
197 #if _HAVE_UT_ID - 0
198 (entry->ut_id[0] && match->ut_id[0]
199 ? strncmp (entry->ut_id, match->ut_id, sizeof match->ut_id) == 0
200 : strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0)
201 #else
202 strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0
203 #endif
207 static int
208 internal_getut_r (const struct utmp *id, struct utmp *buffer)
210 int result = -1;
212 LOCK_FILE (file_fd, F_RDLCK);
214 #if _HAVE_UT_TYPE - 0
215 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
216 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
218 /* Search for next entry with type RUN_LVL, BOOT_TIME,
219 OLD_TIME, or NEW_TIME. */
221 while (1)
223 /* Read the next entry. */
224 if (__read (file_fd, buffer, sizeof (struct utmp))
225 != sizeof (struct utmp))
227 __set_errno (ESRCH);
228 file_offset = -1l;
229 goto unlock_return;
231 file_offset += sizeof (struct utmp);
233 if (id->ut_type == buffer->ut_type)
234 break;
237 else
238 #endif /* _HAVE_UT_TYPE */
240 /* Search for the next entry with the specified ID and with type
241 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
243 while (1)
245 /* Read the next entry. */
246 if (__read (file_fd, buffer, sizeof (struct utmp))
247 != sizeof (struct utmp))
249 __set_errno (ESRCH);
250 file_offset = -1l;
251 goto unlock_return;
253 file_offset += sizeof (struct utmp);
255 if (proc_utmp_eq (buffer, id))
256 break;
260 result = 0;
262 unlock_return:
263 UNLOCK_FILE (file_fd);
265 return result;
269 /* For implementing this function we don't use the getutent_r function
270 because we can avoid the reposition on every new entry this way. */
271 static int
272 getutid_r_file (const struct utmp *id, struct utmp *buffer,
273 struct utmp **result)
275 assert (file_fd >= 0);
277 if (file_offset == -1l)
279 *result = NULL;
280 return -1;
283 if (internal_getut_r (id, &last_entry) < 0)
285 *result = NULL;
286 return -1;
289 memcpy (buffer, &last_entry, sizeof (struct utmp));
290 *result = buffer;
292 return 0;
296 /* For implementing this function we don't use the getutent_r function
297 because we can avoid the reposition on every new entry this way. */
298 static int
299 getutline_r_file (const struct utmp *line, struct utmp *buffer,
300 struct utmp **result)
302 assert (file_fd >= 0);
304 if (file_offset == -1l)
306 *result = NULL;
307 return -1;
310 LOCK_FILE (file_fd, F_RDLCK);
312 while (1)
314 /* Read the next entry. */
315 if (__read (file_fd, &last_entry, sizeof (struct utmp))
316 != sizeof (struct utmp))
318 __set_errno (ESRCH);
319 file_offset = -1l;
320 *result = NULL;
321 goto unlock_return;
323 file_offset += sizeof (struct utmp);
325 /* Stop if we found a user or login entry. */
326 if (
327 #if _HAVE_UT_TYPE - 0
328 (last_entry.ut_type == USER_PROCESS
329 || last_entry.ut_type == LOGIN_PROCESS)
331 #endif
332 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
333 break;
336 memcpy (buffer, &last_entry, sizeof (struct utmp));
337 *result = buffer;
339 unlock_return:
340 UNLOCK_FILE (file_fd);
342 return ((*result == NULL) ? -1 : 0);
346 static struct utmp *
347 pututline_file (const struct utmp *data)
349 struct utmp buffer;
350 struct utmp *pbuf;
351 int found;
353 assert (file_fd >= 0);
355 /* Find the correct place to insert the data. */
356 if (file_offset > 0
357 && (
358 #if _HAVE_UT_TYPE - 0
359 (last_entry.ut_type == data->ut_type
360 && (last_entry.ut_type == RUN_LVL
361 || last_entry.ut_type == BOOT_TIME
362 || last_entry.ut_type == OLD_TIME
363 || last_entry.ut_type == NEW_TIME))
365 #endif
366 proc_utmp_eq (&last_entry, data)))
367 found = 1;
368 else
369 found = internal_getut_r (data, &buffer);
371 LOCK_FILE (file_fd, F_WRLCK);
373 if (found < 0)
375 /* We append the next entry. */
376 file_offset = __lseek (file_fd, 0, SEEK_END);
377 if (file_offset % sizeof (struct utmp) != 0)
379 file_offset -= file_offset % sizeof (struct utmp);
380 __ftruncate (file_fd, file_offset);
382 if (__lseek (file_fd, 0, SEEK_END) < 0)
384 pbuf = NULL;
385 goto unlock_return;
389 else
391 /* We replace the just read entry. */
392 file_offset -= sizeof (struct utmp);
393 __lseek (file_fd, file_offset, SEEK_SET);
396 /* Write the new data. */
397 if (__write (file_fd, data, sizeof (struct utmp)) != sizeof (struct utmp))
399 /* If we appended a new record this is only partially written.
400 Remove it. */
401 if (found < 0)
402 (void) __ftruncate (file_fd, file_offset);
403 pbuf = NULL;
405 else
407 file_offset += sizeof (struct utmp);
408 pbuf = (struct utmp *) data;
411 unlock_return:
412 UNLOCK_FILE (file_fd);
414 return pbuf;
418 static void
419 endutent_file (void)
421 assert (file_fd >= 0);
423 __close (file_fd);
424 file_fd = -1;
428 static int
429 updwtmp_file (const char *file, const struct utmp *utmp)
431 int result = -1;
432 off_t offset;
433 int fd;
435 /* Open WTMP file. */
436 fd = __open (file, O_WRONLY);
437 if (fd < 0)
438 return -1;
440 LOCK_FILE (fd, F_WRLCK);
442 /* Remember original size of log file. */
443 offset = __lseek (fd, 0, SEEK_END);
444 if (offset % sizeof (struct utmp) != 0)
446 offset -= offset % sizeof (struct utmp);
447 __ftruncate (fd, offset);
449 if (__lseek (fd, 0, SEEK_END) < 0)
450 goto unlock_return;
453 /* Write the entry. If we can't write all the bytes, reset the file
454 size back to the original size. That way, no partial entries
455 will remain. */
456 if (__write (fd, utmp, sizeof (struct utmp)) != sizeof (struct utmp))
458 __ftruncate (fd, offset);
459 goto unlock_return;
462 result = 0;
464 unlock_return:
465 UNLOCK_FILE (fd);
467 /* Close WTMP file. */
468 __close (fd);
470 return result;