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. */
29 #include "utmp-private.h"
32 /* Descriptor for the file and position. */
33 static int file_fd
= -1;
34 static off_t file_offset
;
36 /* Cache for the last read entry. */
37 static struct utmp last_entry
;
40 /* Functions defined here. */
41 static int setutent_file (void);
42 static int getutent_r_file (struct utmp
*buffer
, struct utmp
**result
);
43 static int getutid_r_file (const struct utmp
*key
, struct utmp
*buffer
,
44 struct utmp
**result
);
45 static int getutline_r_file (const struct utmp
*key
, struct utmp
*buffer
,
46 struct utmp
**result
);
47 static struct utmp
*pututline_file (const struct utmp
*data
);
48 static void endutent_file (void);
49 static int updwtmp_file (const char *file
, const struct utmp
*utmp
);
51 /* Jump table for file functions. */
52 struct utfuncs __libc_utmp_file_functions
=
69 file_fd
= open (__libc_utmp_file_name
, O_RDWR
);
72 /* Hhm, read-write access did not work. Try read-only. */
73 file_fd
= open (__libc_utmp_file_name
, O_RDONLY
);
76 perror (_("while opening UTMP file"));
82 lseek (file_fd
, 0, SEEK_SET
);
86 /* Make sure the entry won't match. */
87 last_entry
.ut_type
= -1;
95 getutent_r_file (struct utmp
*buffer
, struct utmp
**result
)
98 struct flock fl
; /* Information struct for locking. */
100 assert (file_fd
>= 0);
102 if (file_offset
== -1l)
109 /* XXX The following is not perfect. Instead of locking the file itself
110 Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl> suggests to
111 use an extra locking file. */
112 /* XXX I think using an extra locking file does not solve the
113 problems. Instead we should set an alarm, which causes fcntl to
114 fail, as in ../nis/lckcache.c.
115 Mark Kettenis <kettenis@phys.uva.nl>. */
117 /* Try to get the lock. */
118 memset (&fl
, '\0', sizeof (struct flock
));
120 fl
.l_whence
= SEEK_SET
;
121 fcntl (file_fd
, F_SETLK
, &fl
);
123 /* Read the next entry. */
124 nbytes
= read (file_fd
, &last_entry
, sizeof (struct utmp
));
126 /* And unlock the file. */
128 fcntl (file_fd
, F_SETLKW
, &fl
);
130 if (nbytes
!= sizeof (struct utmp
))
137 /* Update position pointer. */
138 file_offset
+= sizeof (struct utmp
);
140 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
148 proc_utmp_eq (const struct utmp
*entry
, const struct utmp
*match
)
152 #if _HAVE_UT_TYPE - 0
153 (entry
->ut_type
== INIT_PROCESS
154 || entry
->ut_type
== LOGIN_PROCESS
155 || entry
->ut_type
== USER_PROCESS
156 || entry
->ut_type
== DEAD_PROCESS
)
158 (match
->ut_type
== INIT_PROCESS
159 || match
->ut_type
== LOGIN_PROCESS
160 || match
->ut_type
== USER_PROCESS
161 || match
->ut_type
== DEAD_PROCESS
)
165 (entry
->ut_id
[0] && match
->ut_id
[0]
166 ? strncmp (entry
->ut_id
, match
->ut_id
, sizeof match
->ut_id
) == 0
167 : strncmp (entry
->ut_line
, match
->ut_line
, sizeof match
->ut_line
) == 0)
169 strncmp (entry
->ut_line
, match
->ut_line
, sizeof match
->ut_line
) == 0
175 internal_getut_r (const struct utmp
*id
, struct utmp
*buffer
)
180 /* Try to get the lock. */
181 memset (&fl
, '\0', sizeof (struct flock
));
183 fl
.l_whence
= SEEK_SET
;
184 fcntl (file_fd
, F_SETLKW
, &fl
);
186 #if _HAVE_UT_TYPE - 0
187 if (id
->ut_type
== RUN_LVL
|| id
->ut_type
== BOOT_TIME
188 || id
->ut_type
== OLD_TIME
|| id
->ut_type
== NEW_TIME
)
190 /* Search for next entry with type RUN_LVL, BOOT_TIME,
191 OLD_TIME, or NEW_TIME. */
195 /* Read the next entry. */
196 if (read (file_fd
, buffer
, sizeof (struct utmp
))
197 != sizeof (struct utmp
))
203 file_offset
+= sizeof (struct utmp
);
205 if (id
->ut_type
== buffer
->ut_type
)
210 #endif /* _HAVE_UT_TYPE */
212 /* Search for the next entry with the specified ID and with type
213 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
217 /* Read the next entry. */
218 if (read (file_fd
, buffer
, sizeof (struct utmp
))
219 != sizeof (struct utmp
))
225 file_offset
+= sizeof (struct utmp
);
227 if (proc_utmp_eq (buffer
, id
))
235 /* And unlock the file. */
237 fcntl (file_fd
, F_SETLK
, &fl
);
243 /* For implementing this function we don't use the getutent_r function
244 because we can avoid the reposition on every new entry this way. */
246 getutid_r_file (const struct utmp
*id
, struct utmp
*buffer
,
247 struct utmp
**result
)
249 assert (file_fd
>= 0);
251 if (file_offset
== -1l)
257 if (internal_getut_r (id
, &last_entry
) < 0)
263 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
270 /* For implementing this function we don't use the getutent_r function
271 because we can avoid the reposition on every new entry this way. */
273 getutline_r_file (const struct utmp
*line
, struct utmp
*buffer
,
274 struct utmp
**result
)
278 assert (file_fd
>= 0);
280 if (file_offset
== -1l)
286 /* Try to get the lock. */
287 memset (&fl
, '\0', sizeof (struct flock
));
289 fl
.l_whence
= SEEK_SET
;
290 fcntl (file_fd
, F_SETLKW
, &fl
);
294 /* Read the next entry. */
295 if (read (file_fd
, &last_entry
, sizeof (struct utmp
))
296 != sizeof (struct utmp
))
303 file_offset
+= sizeof (struct utmp
);
305 /* Stop if we found a user or login entry. */
307 #if _HAVE_UT_TYPE - 0
308 (last_entry
.ut_type
== USER_PROCESS
309 || last_entry
.ut_type
== LOGIN_PROCESS
)
312 !strncmp (line
->ut_line
, last_entry
.ut_line
, sizeof line
->ut_line
))
316 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
320 /* And unlock the file. */
322 fcntl (file_fd
, F_SETLK
, &fl
);
324 return ((*result
== NULL
) ? -1 : 0);
329 pututline_file (const struct utmp
*data
)
331 struct flock fl
; /* Information struct for locking. */
336 assert (file_fd
>= 0);
338 /* Find the correct place to insert the data. */
341 #if _HAVE_UT_TYPE - 0
342 (last_entry
.ut_type
== data
->ut_type
343 && (last_entry
.ut_type
== RUN_LVL
344 || last_entry
.ut_type
== BOOT_TIME
345 || last_entry
.ut_type
== OLD_TIME
346 || last_entry
.ut_type
== NEW_TIME
))
349 proc_utmp_eq (&last_entry
, data
)))
352 found
= internal_getut_r (data
, &buffer
);
354 /* Try to lock the file. */
355 memset (&fl
, '\0', sizeof (struct flock
));
357 fl
.l_whence
= SEEK_SET
;
358 fcntl (file_fd
, F_SETLK
, &fl
);
362 /* We append the next entry. */
363 file_offset
= lseek (file_fd
, 0, SEEK_END
);
364 if (file_offset
% sizeof (struct utmp
) != 0)
366 file_offset
-= file_offset
% sizeof (struct utmp
);
367 ftruncate (file_fd
, file_offset
);
369 if (lseek (file_fd
, 0, SEEK_END
) < 0)
378 /* We replace the just read entry. */
379 file_offset
-= sizeof (struct utmp
);
380 lseek (file_fd
, file_offset
, SEEK_SET
);
383 /* Write the new data. */
384 if (write (file_fd
, data
, sizeof (struct utmp
)) != sizeof (struct utmp
))
386 /* If we appended a new record this is only partially written.
389 (void) ftruncate (file_fd
, file_offset
);
394 file_offset
+= sizeof (struct utmp
);
395 pbuf
= (struct utmp
*) data
;
399 /* And unlock the file. */
401 fcntl (file_fd
, F_SETLK
, &fl
);
410 assert (file_fd
>= 0);
418 updwtmp_file (const char *file
, const struct utmp
*utmp
)
425 /* Open WTMP file. */
426 fd
= open (file
, O_WRONLY
);
430 /* Try to get the lock. */
431 memset (&fl
, '\0', sizeof (struct flock
));
433 fl
.l_whence
= SEEK_SET
;
434 fcntl (fd
, F_SETLK
, &fl
);
436 /* Remember original size of log file. */
437 offset
= lseek (fd
, 0, SEEK_END
);
438 if (offset
% sizeof (struct utmp
) != 0)
440 offset
-= offset
% sizeof (struct utmp
);
441 ftruncate (fd
, offset
);
443 if (lseek (fd
, 0, SEEK_END
) < 0)
447 /* Write the entry. If we can't write all the bytes, reset the file
448 size back to the original size. That way, no partial entries
450 if (write (fd
, utmp
, sizeof (struct utmp
)) != sizeof (struct utmp
))
452 ftruncate (fd
, offset
);
459 /* And unlock the file. */
461 fcntl (fd
, F_SETLKW
, &fl
);
463 /* Close WTMP file. */