1 /* Copyright (C) 1996, 1997 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. */
32 #include "utmp-private.h"
35 /* This is the default name. */
36 static const char default_file_name
[] = _PATH_UTMP
;
38 /* Current file name. */
39 static const char *file_name
= (const char *) default_file_name
;
41 /* Descriptor for the file and position. */
42 static int file_fd
= INT_MIN
;
43 static off_t file_offset
;
45 static struct utmp last_entry
;
47 /* Functions defined here. */
48 static int setutent_file (int reset
);
49 static int getutent_r_file (struct utmp
*buffer
, struct utmp
**result
);
50 static int getutid_r_file (const struct utmp
*key
, struct utmp
*buffer
,
51 struct utmp
**result
);
52 static int getutline_r_file (const struct utmp
*key
, struct utmp
*buffer
,
53 struct utmp
**result
);
54 static struct utmp
*pututline_file (const struct utmp
*data
);
55 static void endutent_file (void);
56 static int utmpname_file (const char *name
);
59 /* Jump table for file functions. */
60 struct utfuncs __libc_utmp_file_functions
=
73 setutent_file (int reset
)
75 if (file_fd
== INT_MIN
)
77 file_fd
= open (file_name
, O_RDWR
);
80 /* Hhm, read-write access did not work. Try read-only. */
81 file_fd
= open (file_name
, O_RDONLY
);
84 perror (_("while opening UTMP file"));
91 /* Make sure the entry won't match. */
92 last_entry
.ut_type
= -1;
97 lseek (file_fd
, 0, SEEK_SET
);
99 /* Remember we are at beginning of file. */
102 #if _HAVE_UT_TYPE - 0
103 /* Make sure the entry won't match. */
104 last_entry
.ut_type
= -1;
123 getutent_r_file (struct utmp
*buffer
, struct utmp
**result
)
126 struct flock fl
; /* Information struct for locking. */
128 /* Open utmp file if not already done. */
129 if (file_fd
== INT_MIN
)
132 if (file_fd
== -1 || file_offset
== -1l)
139 /* XXX The following is not perfect. Instead of locking the file itself
140 Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl> suggests to
141 use an extra locking file. */
143 /* Try to get the lock. */
144 memset (&fl
, '\0', sizeof (struct flock
));
146 fl
.l_whence
= SEEK_SET
;
147 fcntl (file_fd
, F_SETLKW
, &fl
);
149 /* Read the next entry. */
150 nbytes
= read (file_fd
, &last_entry
, sizeof (struct utmp
));
152 /* And unlock the file. */
154 fcntl (file_fd
, F_SETLKW
, &fl
);
156 if (nbytes
!= sizeof (struct utmp
))
163 /* Update position pointer. */
164 file_offset
+= sizeof (struct utmp
);
166 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
173 /* For implementing this function we don't use the getutent_r function
174 because we can avoid the reposition on every new entry this way. */
176 getutline_r_file (const struct utmp
*line
, struct utmp
*buffer
,
177 struct utmp
**result
)
179 if (file_fd
< 0 || file_offset
== -1l)
187 /* Read the next entry. */
188 if (read (file_fd
, &last_entry
, sizeof (struct utmp
))
189 != sizeof (struct utmp
))
196 file_offset
+= sizeof (struct utmp
);
198 /* Stop if we found a user or login entry. */
200 #if _HAVE_UT_TYPE - 0
201 (last_entry
.ut_type
== USER_PROCESS
202 || last_entry
.ut_type
== LOGIN_PROCESS
)
205 !strncmp (line
->ut_line
, last_entry
.ut_line
, sizeof line
->ut_line
))
209 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
217 proc_utmp_eq (const struct utmp
*entry
, const struct utmp
*match
)
221 #if _HAVE_UT_TYPE - 0
222 (entry
->ut_type
== INIT_PROCESS
223 || entry
->ut_type
== LOGIN_PROCESS
224 || entry
->ut_type
== USER_PROCESS
225 || entry
->ut_type
== DEAD_PROCESS
)
227 (match
->ut_type
== INIT_PROCESS
228 || match
->ut_type
== LOGIN_PROCESS
229 || match
->ut_type
== USER_PROCESS
230 || match
->ut_type
== DEAD_PROCESS
)
234 strncmp (entry
->ut_id
, match
->ut_id
, sizeof match
->ut_id
) == 0
236 strncmp (entry
->ut_line
, match
->ut_line
, sizeof match
->ut_line
) == 0
242 internal_getut_r (const struct utmp
*id
, struct utmp
*buffer
)
244 #if _HAVE_UT_TYPE - 0
245 if (id
->ut_type
== RUN_LVL
|| id
->ut_type
== BOOT_TIME
246 || id
->ut_type
== OLD_TIME
|| id
->ut_type
== NEW_TIME
)
248 /* Search for next entry with type RUN_LVL, BOOT_TIME,
249 OLD_TIME, or NEW_TIME. */
253 /* Read the next entry. */
254 if (read (file_fd
, buffer
, sizeof (struct utmp
))
255 != sizeof (struct utmp
))
261 file_offset
+= sizeof (struct utmp
);
263 if (id
->ut_type
== buffer
->ut_type
)
268 #endif /* _HAVE_UT_TYPE */
270 /* Search for the next entry with the specified ID and with type
271 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
275 /* Read the next entry. */
276 if (read (file_fd
, buffer
, sizeof (struct utmp
))
277 != sizeof (struct utmp
))
283 file_offset
+= sizeof (struct utmp
);
285 if (proc_utmp_eq (buffer
, id
))
294 /* For implementing this function we don't use the getutent_r function
295 because we can avoid the reposition on every new entry this way. */
297 getutid_r_file (const struct utmp
*id
, struct utmp
*buffer
,
298 struct utmp
**result
)
300 if (file_fd
< 0 || file_offset
== -1l)
306 if (internal_getut_r (id
, &last_entry
) < 0)
312 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
320 pututline_file (const struct utmp
*data
)
322 struct flock fl
; /* Information struct for locking. */
328 /* Something went wrong. */
331 if (file_fd
== INT_MIN
)
332 /* The file is closed. Open it again. */
335 /* Find the correct place to insert the data. */
338 #if _HAVE_UT_TYPE - 0
339 (last_entry
.ut_type
== data
->ut_type
340 && (last_entry
.ut_type
== RUN_LVL
341 || last_entry
.ut_type
== BOOT_TIME
342 || last_entry
.ut_type
== OLD_TIME
343 || last_entry
.ut_type
== NEW_TIME
))
346 proc_utmp_eq (&last_entry
, data
)))
349 found
= internal_getut_r (data
, &buffer
);
351 /* Try to lock the file. */
352 memset (&fl
, '\0', sizeof (struct flock
));
354 fl
.l_whence
= SEEK_SET
;
355 fcntl (file_fd
, F_SETLKW
, &fl
);
359 /* We append the next entry. */
360 file_offset
= lseek (file_fd
, 0, SEEK_END
);
361 if (file_offset
% sizeof (struct utmp
) != 0)
363 file_offset
-= file_offset
% sizeof (struct utmp
);
364 ftruncate (file_fd
, file_offset
);
366 if (lseek (file_fd
, 0, SEEK_END
) < 0)
375 /* We replace the just read entry. */
376 file_offset
-= sizeof (struct utmp
);
377 lseek (file_fd
, file_offset
, SEEK_SET
);
380 /* Write the new data. */
381 if (write (file_fd
, data
, sizeof (struct utmp
)) != sizeof (struct utmp
)
382 /* If we appended a new record this is only partially written.
386 (void) ftruncate (file_fd
, file_offset
);
391 file_offset
+= sizeof (struct utmp
);
392 pbuf
= (struct utmp
*) data
;
396 /* And unlock the file. */
398 fcntl (file_fd
, F_SETLKW
, &fl
);
405 utmpname_file (const char *name
)
407 if (strcmp (name
, file_name
) != 0)
409 if (strcmp (name
, default_file_name
) == 0)
411 if (file_name
!= default_file_name
)
412 free ((char *) file_name
);
414 file_name
= default_file_name
;
418 char *new_name
= __strdup (name
);
419 if (new_name
== NULL
)
423 if (file_name
!= default_file_name
)
424 free ((char *) file_name
);
426 file_name
= new_name
;