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
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. */
46 /* Do-nothing handler for locking timeout. */
47 static void timeout_handler (int signum
) {};
49 #define LOCK_FILE(fd, type) \
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); \
66 /* Try to get the lock. */ \
67 memset (&fl, '\0', sizeof (struct flock)); \
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. */ \
84 __sigaction (SIGALRM, &old_action, NULL); \
85 if (old_timeout != 0) \
86 alarm (old_timeout); \
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
=
114 #ifndef TRANSFORM_UTMP_FILE_NAME
115 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
123 const char *file_name
;
126 file_name
= TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name
);
128 file_fd
= __open (file_name
, O_RDWR
);
131 /* Hhm, read-write access did not work. Try read-only. */
132 file_fd
= __open (file_name
, O_RDONLY
);
137 /* We have to make sure the file is `closed on exec'. */
138 result
= __fcntl (file_fd
, F_GETFD
, 0);
140 result
= __fcntl (file_fd
, F_SETFD
, result
| FD_CLOEXEC
);
148 __lseek64 (file_fd
, 0, SEEK_SET
);
151 #if _HAVE_UT_TYPE - 0
152 /* Make sure the entry won't match. */
153 last_entry
.ut_type
= -1;
161 getutent_r_file (struct utmp
*buffer
, struct utmp
**result
)
165 assert (file_fd
>= 0);
167 if (file_offset
== -1l)
174 LOCK_FILE (file_fd
, F_RDLCK
)
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
))
192 /* Update position pointer. */
193 file_offset
+= sizeof (struct utmp
);
195 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
203 proc_utmp_eq (const struct utmp
*entry
, const struct utmp
*match
)
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
)
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)
224 strncmp (entry
->ut_line
, match
->ut_line
, sizeof match
->ut_line
) == 0
230 internal_getut_r (const struct utmp
*id
, struct utmp
*buffer
)
234 LOCK_FILE (file_fd
, F_RDLCK
)
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. */
246 /* Read the next entry. */
247 if (__read (file_fd
, buffer
, sizeof (struct utmp
))
248 != sizeof (struct utmp
))
254 file_offset
+= sizeof (struct utmp
);
256 if (id
->ut_type
== buffer
->ut_type
)
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. */
268 /* Read the next entry. */
269 if (__read (file_fd
, buffer
, sizeof (struct utmp
))
270 != sizeof (struct utmp
))
276 file_offset
+= sizeof (struct utmp
);
278 if (proc_utmp_eq (buffer
, id
))
286 UNLOCK_FILE (file_fd
);
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. */
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)
306 if (internal_getut_r (id
, &last_entry
) < 0)
312 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
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. */
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)
333 LOCK_FILE (file_fd
, F_RDLCK
)
341 /* Read the next entry. */
342 if (__read (file_fd
, &last_entry
, sizeof (struct utmp
))
343 != sizeof (struct utmp
))
350 file_offset
+= sizeof (struct utmp
);
352 /* Stop if we found a user or login entry. */
354 #if _HAVE_UT_TYPE - 0
355 (last_entry
.ut_type
== USER_PROCESS
356 || last_entry
.ut_type
== LOGIN_PROCESS
)
359 !strncmp (line
->ut_line
, last_entry
.ut_line
, sizeof line
->ut_line
))
363 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
367 UNLOCK_FILE (file_fd
);
369 return ((*result
== NULL
) ? -1 : 0);
374 pututline_file (const struct utmp
*data
)
380 assert (file_fd
>= 0);
382 /* Find the correct place to insert the data. */
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
))
393 proc_utmp_eq (&last_entry
, data
)))
396 found
= internal_getut_r (data
, &buffer
);
398 LOCK_FILE (file_fd
, F_WRLCK
)
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)
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.
430 (void) __ftruncate64 (file_fd
, file_offset
);
435 file_offset
+= sizeof (struct utmp
);
436 pbuf
= (struct utmp
*) data
;
440 UNLOCK_FILE (file_fd
);
449 assert (file_fd
>= 0);
457 updwtmp_file (const char *file
, const struct utmp
*utmp
)
463 /* Open WTMP file. */
464 fd
= __open (file
, O_WRONLY
);
468 LOCK_FILE (fd
, F_WRLCK
)
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)
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
488 if (__write (fd
, utmp
, sizeof (struct utmp
)) != sizeof (struct utmp
))
490 __ftruncate64 (fd
, offset
);
499 /* Close WTMP file. */