Update.
[glibc.git] / sysdeps / generic / utmp_file.c
blob1bd56f444949a45d4c79a905c3a6ac658faa3371
1 /* Copyright (C) 1996,97,98,99,2000,01,02 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 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 off64_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 if (__fcntl ((fd), F_SETLKW, &fl) < 0)
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. We must reset the alarm \
78 before resetting the handler so our alarm does not generate a \
79 spurious SIGALRM seen by the user. However, we cannot just set \
80 the user's old alarm before restoring the handler, because then \
81 it's possible our handler could catch the user alarm's SIGARLM \
82 and then the user would never see the signal he expected. */ \
83 alarm (0); \
84 __sigaction (SIGALRM, &old_action, NULL); \
85 if (old_timeout != 0) \
86 alarm (old_timeout); \
87 } while (0)
90 /* Functions defined here. */
91 static int setutent_file (void);
92 static int getutent_r_file (struct utmp *buffer, struct utmp **result);
93 static int getutid_r_file (const struct utmp *key, struct utmp *buffer,
94 struct utmp **result);
95 static int getutline_r_file (const struct utmp *key, struct utmp *buffer,
96 struct utmp **result);
97 static struct utmp *pututline_file (const struct utmp *data);
98 static void endutent_file (void);
99 static int updwtmp_file (const char *file, const struct utmp *utmp);
101 /* Jump table for file functions. */
102 struct utfuncs __libc_utmp_file_functions =
104 setutent_file,
105 getutent_r_file,
106 getutid_r_file,
107 getutline_r_file,
108 pututline_file,
109 endutent_file,
110 updwtmp_file
114 #ifndef TRANSFORM_UTMP_FILE_NAME
115 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
116 #endif
118 static int
119 setutent_file (void)
121 if (file_fd < 0)
123 const char *file_name;
124 int result;
126 file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
128 file_fd = __open (file_name, O_RDWR);
129 if (file_fd == -1)
131 /* Hhm, read-write access did not work. Try read-only. */
132 file_fd = __open (file_name, O_RDONLY);
133 if (file_fd == -1)
134 return 0;
137 /* We have to make sure the file is `closed on exec'. */
138 result = __fcntl (file_fd, F_GETFD, 0);
139 if (result >= 0)
140 result = __fcntl (file_fd, F_SETFD, result | FD_CLOEXEC);
141 if (result == -1)
143 __close (file_fd);
144 return 0;
148 __lseek64 (file_fd, 0, SEEK_SET);
149 file_offset = 0;
151 #if _HAVE_UT_TYPE - 0
152 /* Make sure the entry won't match. */
153 last_entry.ut_type = -1;
154 #endif
156 return 1;
160 static int
161 getutent_r_file (struct utmp *buffer, struct utmp **result)
163 ssize_t nbytes;
165 assert (file_fd >= 0);
167 if (file_offset == -1l)
169 /* Not available. */
170 *result = NULL;
171 return -1;
174 LOCK_FILE (file_fd, F_RDLCK)
176 *result = NULL;
177 return -1;
180 /* Read the next entry. */
181 nbytes = __read (file_fd, &last_entry, sizeof (struct utmp));
183 UNLOCK_FILE (file_fd);
185 if (nbytes != sizeof (struct utmp))
187 file_offset = -1l;
188 *result = NULL;
189 return -1;
192 /* Update position pointer. */
193 file_offset += sizeof (struct utmp);
195 memcpy (buffer, &last_entry, sizeof (struct utmp));
196 *result = buffer;
198 return 0;
202 static int
203 proc_utmp_eq (const struct utmp *entry, const struct utmp *match)
205 return
207 #if _HAVE_UT_TYPE - 0
208 (entry->ut_type == INIT_PROCESS
209 || entry->ut_type == LOGIN_PROCESS
210 || entry->ut_type == USER_PROCESS
211 || entry->ut_type == DEAD_PROCESS)
213 (match->ut_type == INIT_PROCESS
214 || match->ut_type == LOGIN_PROCESS
215 || match->ut_type == USER_PROCESS
216 || match->ut_type == DEAD_PROCESS)
218 #endif
219 #if _HAVE_UT_ID - 0
220 (entry->ut_id[0] && match->ut_id[0]
221 ? strncmp (entry->ut_id, match->ut_id, sizeof match->ut_id) == 0
222 : strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0)
223 #else
224 strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0
225 #endif
229 static int
230 internal_getut_r (const struct utmp *id, struct utmp *buffer)
232 int result = -1;
234 LOCK_FILE (file_fd, F_RDLCK)
235 return result;
237 #if _HAVE_UT_TYPE - 0
238 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
239 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
241 /* Search for next entry with type RUN_LVL, BOOT_TIME,
242 OLD_TIME, or NEW_TIME. */
244 while (1)
246 /* Read the next entry. */
247 if (__read (file_fd, buffer, sizeof (struct utmp))
248 != sizeof (struct utmp))
250 __set_errno (ESRCH);
251 file_offset = -1l;
252 goto unlock_return;
254 file_offset += sizeof (struct utmp);
256 if (id->ut_type == buffer->ut_type)
257 break;
260 else
261 #endif /* _HAVE_UT_TYPE */
263 /* Search for the next entry with the specified ID and with type
264 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
266 while (1)
268 /* Read the next entry. */
269 if (__read (file_fd, buffer, sizeof (struct utmp))
270 != sizeof (struct utmp))
272 __set_errno (ESRCH);
273 file_offset = -1l;
274 goto unlock_return;
276 file_offset += sizeof (struct utmp);
278 if (proc_utmp_eq (buffer, id))
279 break;
283 result = 0;
285 unlock_return:
286 UNLOCK_FILE (file_fd);
288 return result;
292 /* For implementing this function we don't use the getutent_r function
293 because we can avoid the reposition on every new entry this way. */
294 static int
295 getutid_r_file (const struct utmp *id, struct utmp *buffer,
296 struct utmp **result)
298 assert (file_fd >= 0);
300 if (file_offset == -1l)
302 *result = NULL;
303 return -1;
306 if (internal_getut_r (id, &last_entry) < 0)
308 *result = NULL;
309 return -1;
312 memcpy (buffer, &last_entry, sizeof (struct utmp));
313 *result = buffer;
315 return 0;
319 /* For implementing this function we don't use the getutent_r function
320 because we can avoid the reposition on every new entry this way. */
321 static int
322 getutline_r_file (const struct utmp *line, struct utmp *buffer,
323 struct utmp **result)
325 assert (file_fd >= 0);
327 if (file_offset == -1l)
329 *result = NULL;
330 return -1;
333 LOCK_FILE (file_fd, F_RDLCK)
335 *result = NULL;
336 return -1;
339 while (1)
341 /* Read the next entry. */
342 if (__read (file_fd, &last_entry, sizeof (struct utmp))
343 != sizeof (struct utmp))
345 __set_errno (ESRCH);
346 file_offset = -1l;
347 *result = NULL;
348 goto unlock_return;
350 file_offset += sizeof (struct utmp);
352 /* Stop if we found a user or login entry. */
353 if (
354 #if _HAVE_UT_TYPE - 0
355 (last_entry.ut_type == USER_PROCESS
356 || last_entry.ut_type == LOGIN_PROCESS)
358 #endif
359 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
360 break;
363 memcpy (buffer, &last_entry, sizeof (struct utmp));
364 *result = buffer;
366 unlock_return:
367 UNLOCK_FILE (file_fd);
369 return ((*result == NULL) ? -1 : 0);
373 static struct utmp *
374 pututline_file (const struct utmp *data)
376 struct utmp buffer;
377 struct utmp *pbuf;
378 int found;
380 assert (file_fd >= 0);
382 /* Find the correct place to insert the data. */
383 if (file_offset > 0
384 && (
385 #if _HAVE_UT_TYPE - 0
386 (last_entry.ut_type == data->ut_type
387 && (last_entry.ut_type == RUN_LVL
388 || last_entry.ut_type == BOOT_TIME
389 || last_entry.ut_type == OLD_TIME
390 || last_entry.ut_type == NEW_TIME))
392 #endif
393 proc_utmp_eq (&last_entry, data)))
394 found = 1;
395 else
396 found = internal_getut_r (data, &buffer);
398 LOCK_FILE (file_fd, F_WRLCK)
399 return NULL;
401 if (found < 0)
403 /* We append the next entry. */
404 file_offset = __lseek64 (file_fd, 0, SEEK_END);
405 if (file_offset % sizeof (struct utmp) != 0)
407 file_offset -= file_offset % sizeof (struct utmp);
408 __ftruncate64 (file_fd, file_offset);
410 if (__lseek64 (file_fd, 0, SEEK_END) < 0)
412 pbuf = NULL;
413 goto unlock_return;
417 else
419 /* We replace the just read entry. */
420 file_offset -= sizeof (struct utmp);
421 __lseek64 (file_fd, file_offset, SEEK_SET);
424 /* Write the new data. */
425 if (__write (file_fd, data, sizeof (struct utmp)) != sizeof (struct utmp))
427 /* If we appended a new record this is only partially written.
428 Remove it. */
429 if (found < 0)
430 (void) __ftruncate64 (file_fd, file_offset);
431 pbuf = NULL;
433 else
435 file_offset += sizeof (struct utmp);
436 pbuf = (struct utmp *) data;
439 unlock_return:
440 UNLOCK_FILE (file_fd);
442 return pbuf;
446 static void
447 endutent_file (void)
449 assert (file_fd >= 0);
451 __close (file_fd);
452 file_fd = -1;
456 static int
457 updwtmp_file (const char *file, const struct utmp *utmp)
459 int result = -1;
460 off64_t offset;
461 int fd;
463 /* Open WTMP file. */
464 fd = __open (file, O_WRONLY);
465 if (fd < 0)
466 return -1;
468 LOCK_FILE (fd, F_WRLCK)
470 __close (fd);
471 return result;
474 /* Remember original size of log file. */
475 offset = __lseek64 (fd, 0, SEEK_END);
476 if (offset % sizeof (struct utmp) != 0)
478 offset -= offset % sizeof (struct utmp);
479 __ftruncate64 (fd, offset);
481 if (__lseek64 (fd, 0, SEEK_END) < 0)
482 goto unlock_return;
485 /* Write the entry. If we can't write all the bytes, reset the file
486 size back to the original size. That way, no partial entries
487 will remain. */
488 if (__write (fd, utmp, sizeof (struct utmp)) != sizeof (struct utmp))
490 __ftruncate64 (fd, offset);
491 goto unlock_return;
494 result = 0;
496 unlock_return:
497 UNLOCK_FILE (fd);
499 /* Close WTMP file. */
500 __close (fd);
502 return result;