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. */
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. */
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 __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); \
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
=
107 #ifndef TRANSFORM_UTMP_FILE_NAME
108 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
116 const char *file_name
;
118 file_name
= TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name
);
120 file_fd
= __open (file_name
, O_RDWR
);
123 /* Hhm, read-write access did not work. Try read-only. */
124 file_fd
= __open (file_name
, O_RDONLY
);
130 __lseek (file_fd
, 0, SEEK_SET
);
133 #if _HAVE_UT_TYPE - 0
134 /* Make sure the entry won't match. */
135 last_entry
.ut_type
= -1;
143 getutent_r_file (struct utmp
*buffer
, struct utmp
**result
)
147 assert (file_fd
>= 0);
149 if (file_offset
== -1l)
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
))
170 /* Update position pointer. */
171 file_offset
+= sizeof (struct utmp
);
173 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
181 proc_utmp_eq (const struct utmp
*entry
, const struct utmp
*match
)
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
)
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)
202 strncmp (entry
->ut_line
, match
->ut_line
, sizeof match
->ut_line
) == 0
208 internal_getut_r (const struct utmp
*id
, struct utmp
*buffer
)
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. */
223 /* Read the next entry. */
224 if (__read (file_fd
, buffer
, sizeof (struct utmp
))
225 != sizeof (struct utmp
))
231 file_offset
+= sizeof (struct utmp
);
233 if (id
->ut_type
== buffer
->ut_type
)
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. */
245 /* Read the next entry. */
246 if (__read (file_fd
, buffer
, sizeof (struct utmp
))
247 != sizeof (struct utmp
))
253 file_offset
+= sizeof (struct utmp
);
255 if (proc_utmp_eq (buffer
, id
))
263 UNLOCK_FILE (file_fd
);
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. */
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)
283 if (internal_getut_r (id
, &last_entry
) < 0)
289 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
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. */
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)
310 LOCK_FILE (file_fd
, F_RDLCK
);
314 /* Read the next entry. */
315 if (__read (file_fd
, &last_entry
, sizeof (struct utmp
))
316 != sizeof (struct utmp
))
323 file_offset
+= sizeof (struct utmp
);
325 /* Stop if we found a user or login entry. */
327 #if _HAVE_UT_TYPE - 0
328 (last_entry
.ut_type
== USER_PROCESS
329 || last_entry
.ut_type
== LOGIN_PROCESS
)
332 !strncmp (line
->ut_line
, last_entry
.ut_line
, sizeof line
->ut_line
))
336 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
340 UNLOCK_FILE (file_fd
);
342 return ((*result
== NULL
) ? -1 : 0);
347 pututline_file (const struct utmp
*data
)
353 assert (file_fd
>= 0);
355 /* Find the correct place to insert the data. */
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
))
366 proc_utmp_eq (&last_entry
, data
)))
369 found
= internal_getut_r (data
, &buffer
);
371 LOCK_FILE (file_fd
, F_WRLCK
);
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)
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.
402 (void) __ftruncate (file_fd
, file_offset
);
407 file_offset
+= sizeof (struct utmp
);
408 pbuf
= (struct utmp
*) data
;
412 UNLOCK_FILE (file_fd
);
421 assert (file_fd
>= 0);
429 updwtmp_file (const char *file
, const struct utmp
*utmp
)
435 /* Open WTMP file. */
436 fd
= __open (file
, O_WRONLY
);
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)
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
456 if (__write (fd
, utmp
, sizeof (struct utmp
)) != sizeof (struct utmp
))
458 __ftruncate (fd
, offset
);
467 /* Close WTMP file. */