1 /* Copyright (C) 1996 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"));
90 /* Make sure the entry won't match. */
91 last_entry.ut_type = -1;
95 /* Remember we are at beginning of file. */
98 /* Make sure the entry won't match. */
99 last_entry.ut_type = -1;
117 getutent_r_file (struct utmp *buffer, struct utmp **result)
121 /* Open utmp file if not already done. */
122 if (file_fd == INT_MIN)
125 if (file_fd == -1 || file_offset == -1l)
132 /* Read the next entry. */
133 flock (file_fd, LOCK_SH);
134 nbytes = read (file_fd, &last_entry, sizeof (struct utmp));
135 flock (file_fd, LOCK_UN);
137 if (nbytes != sizeof (struct utmp))
144 /* Update position pointer. */
145 file_offset += sizeof (struct utmp);
147 memcpy (buffer, &last_entry, sizeof (struct utmp));
154 /* For implementing this function we don't use the getutent_r function
155 because we can avoid the reposition on every new entry this way. */
157 getutline_r_file (const struct utmp *line, struct utmp *buffer,
158 struct utmp **result)
160 if (file_fd < 0 || file_offset == -1l)
168 /* Read the next entry. */
169 if (read (file_fd, &last_entry, sizeof (struct utmp))
170 != sizeof (struct utmp))
177 file_offset += sizeof (struct utmp);
179 /* Stop if we found a user or login entry. */
181 #if _HAVE_UT_TYPE - 0
182 (last_entry.ut_type == USER_PROCESS
183 || last_entry.ut_type == LOGIN_PROCESS)
186 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
190 memcpy (buffer, &last_entry, sizeof (struct utmp));
198 internal_getutid_r (const struct utmp *id, struct utmp *buffer)
200 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
201 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
203 /* Search for next entry with type RUN_LVL, BOOT_TIME,
204 OLD_TIME, or NEW_TIME. */
208 /* Read the next entry. */
209 if (read (file_fd, buffer, sizeof (struct utmp))
210 != sizeof (struct utmp))
216 file_offset += sizeof (struct utmp);
218 if (id->ut_type == buffer->ut_type)
224 /* Search for the next entry with the specified ID and with type
225 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
229 /* Read the next entry. */
230 if (read (file_fd, buffer, sizeof (struct utmp))
231 != sizeof (struct utmp))
237 file_offset += sizeof (struct utmp);
239 if (( buffer->ut_type == INIT_PROCESS
240 || buffer->ut_type == LOGIN_PROCESS
241 || buffer->ut_type == USER_PROCESS
242 || buffer->ut_type == DEAD_PROCESS)
243 && strncmp (buffer->ut_id, id->ut_id, sizeof id->ut_id) == 0)
252 /* For implementing this function we don't use the getutent_r function
253 because we can avoid the reposition on every new entry this way. */
255 getutid_r_file (const struct utmp *id, struct utmp *buffer,
256 struct utmp **result)
258 if (file_fd < 0 || file_offset == -1l)
264 if (internal_getutid_r (id, &last_entry) < 0)
270 memcpy (buffer, &last_entry, sizeof (struct utmp));
278 pututline_file (const struct utmp *data)
285 /* Something went wrong. */
288 /* Find the correct place to insert the data. */
292 if ( last_entry.ut_type == RUN_LVL
293 || last_entry.ut_type == BOOT_TIME
294 || last_entry.ut_type == OLD_TIME
295 || last_entry.ut_type == NEW_TIME
296 || (( last_entry.ut_type == INIT_PROCESS
297 || last_entry.ut_type == LOGIN_PROCESS
298 || last_entry.ut_type == USER_PROCESS
299 || last_entry.ut_type == DEAD_PROCESS)
300 && !strncmp (last_entry.ut_id, data->ut_id, sizeof data->ut_id)))
303 found = internal_getutid_r (data, &buffer);
305 /* Try to lock the file. */
306 if (flock (file_fd, LOCK_EX | LOCK_NB) < 0 && errno != ENOSYS)
308 /* Oh, oh. The file is already locked. Wait a bit and try again. */
311 /* This time we ignore the error. */
312 (void) flock (file_fd, LOCK_EX | LOCK_NB);
317 /* We append the next entry. */
318 file_offset = lseek (file_fd, 0, SEEK_END);
319 if (file_offset % sizeof (struct utmp) != 0)
321 file_offset -= file_offset % sizeof (struct utmp);
322 ftruncate (file_fd, file_offset);
324 if (lseek (file_fd, 0, SEEK_END) < 0)
326 (void) flock (file_fd, LOCK_UN);
333 /* We replace the just read entry. */
334 file_offset -= sizeof (struct utmp);
335 lseek (file_fd, file_offset, SEEK_SET);
338 /* Write the new data. */
339 if (write (file_fd, data, sizeof (struct utmp)) != sizeof (struct utmp)
340 /* If we appended a new record this is only partially written.
344 (void) ftruncate (file_fd, file_offset);
349 file_offset += sizeof (struct utmp);
350 pbuf = (struct utmp *) data;
353 /* And unlock the file. */
354 (void) flock (file_fd, LOCK_UN);
361 utmpname_file (const char *name)
363 if (strcmp (name, file_name) != 0)
365 if (strcmp (name, default_file_name) == 0)
367 if (file_name != default_file_name)
368 free ((char *) file_name);
370 file_name = default_file_name;
374 char *new_name = __strdup (name);
375 if (new_name == NULL)
379 if (file_name != default_file_name)
380 free ((char *) file_name);
382 file_name = new_name;