Update.
[glibc.git] / login / utmp_file.c
bloba0b0aa4346535f2d55509ef25a2bdeb2934f8c06
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. */
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <utmp.h>
29 #include <sys/file.h>
30 #include <sys/stat.h>
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 =
62 setutent_file,
63 getutent_r_file,
64 getutid_r_file,
65 getutline_r_file,
66 pututline_file,
67 endutent_file,
68 utmpname_file
72 static int
73 setutent_file (int reset)
75 if (file_fd == INT_MIN)
77 file_fd = open (file_name, O_RDWR);
78 if (file_fd == -1)
80 /* Hhm, read-write access did not work. Try read-only. */
81 file_fd = open (file_name, O_RDONLY);
82 if (file_fd == -1)
84 perror (_("while opening UTMP file"));
85 return 0;
88 file_offset = 0;
90 #if _HAVE_UT_TYPE - 0
91 /* Make sure the entry won't match. */
92 last_entry.ut_type = -1;
93 #endif
95 else if (reset)
97 lseek (file_fd, 0, SEEK_SET);
99 /* Remember we are at beginning of file. */
100 file_offset = 0;
102 #if _HAVE_UT_TYPE - 0
103 /* Make sure the entry won't match. */
104 last_entry.ut_type = -1;
105 #endif
108 return 1;
112 static void
113 endutent_file (void)
115 if (file_fd >= 0)
116 close (file_fd);
118 file_fd = INT_MIN;
122 static int
123 getutent_r_file (struct utmp *buffer, struct utmp **result)
125 int nbytes;
126 struct flock fl; /* Information struct for locking. */
128 /* Open utmp file if not already done. */
129 if (file_fd == INT_MIN)
130 setutent_file (1);
132 if (file_fd == -1 || file_offset == -1l)
134 /* Not available. */
135 *result = NULL;
136 return -1;
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));
145 fl.l_type = F_WRLCK;
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. */
153 fl.l_type = F_UNLCK;
154 fcntl (file_fd, F_SETLKW, &fl);
156 if (nbytes != sizeof (struct utmp))
158 file_offset = -1l;
159 *result = NULL;
160 return -1;
163 /* Update position pointer. */
164 file_offset += sizeof (struct utmp);
166 memcpy (buffer, &last_entry, sizeof (struct utmp));
167 *result = buffer;
169 return 0;
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. */
175 static int
176 getutline_r_file (const struct utmp *line, struct utmp *buffer,
177 struct utmp **result)
179 if (file_fd < 0 || file_offset == -1l)
181 *result = NULL;
182 return -1;
185 while (1)
187 /* Read the next entry. */
188 if (read (file_fd, &last_entry, sizeof (struct utmp))
189 != sizeof (struct utmp))
191 __set_errno (ESRCH);
192 file_offset = -1l;
193 *result = NULL;
194 return -1;
196 file_offset += sizeof (struct utmp);
198 /* Stop if we found a user or login entry. */
199 if (
200 #if _HAVE_UT_TYPE - 0
201 (last_entry.ut_type == USER_PROCESS
202 || last_entry.ut_type == LOGIN_PROCESS)
204 #endif
205 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
206 break;
209 memcpy (buffer, &last_entry, sizeof (struct utmp));
210 *result = buffer;
212 return 0;
216 static int
217 proc_utmp_eq (const struct utmp *entry, const struct utmp *match)
219 return
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)
232 #endif
233 #if _HAVE_UT_ID - 0
234 strncmp (entry->ut_id, match->ut_id, sizeof match->ut_id) == 0
235 #else
236 strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line) == 0
237 #endif
241 static int
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. */
251 while (1)
253 /* Read the next entry. */
254 if (read (file_fd, buffer, sizeof (struct utmp))
255 != sizeof (struct utmp))
257 __set_errno (ESRCH);
258 file_offset = -1l;
259 return -1;
261 file_offset += sizeof (struct utmp);
263 if (id->ut_type == buffer->ut_type)
264 break;
267 else
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. */
273 while (1)
275 /* Read the next entry. */
276 if (read (file_fd, buffer, sizeof (struct utmp))
277 != sizeof (struct utmp))
279 __set_errno (ESRCH);
280 file_offset = -1l;
281 return -1;
283 file_offset += sizeof (struct utmp);
285 if (proc_utmp_eq (buffer, id))
286 break;
290 return 0;
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. */
296 static int
297 getutid_r_file (const struct utmp *id, struct utmp *buffer,
298 struct utmp **result)
300 if (file_fd < 0 || file_offset == -1l)
302 *result = NULL;
303 return -1;
306 if (internal_getut_r (id, &last_entry) < 0)
308 *result = NULL;
309 return -1;
312 memcpy (buffer, &last_entry, sizeof (struct utmp));
313 *result = buffer;
315 return 0;
319 static struct utmp *
320 pututline_file (const struct utmp *data)
322 struct flock fl; /* Information struct for locking. */
323 struct utmp buffer;
324 struct utmp *pbuf;
325 int found;
327 if (file_fd < 0)
328 /* Something went wrong. */
329 return NULL;
331 if (file_fd == INT_MIN)
332 /* The file is closed. Open it again. */
333 setutent_file (0);
335 /* Find the correct place to insert the data. */
336 if (file_offset > 0
337 && (
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))
345 #endif
346 proc_utmp_eq (&last_entry, data)))
347 found = 1;
348 else
349 found = internal_getut_r (data, &buffer);
351 /* Try to lock the file. */
352 memset (&fl, '\0', sizeof (struct flock));
353 fl.l_type = F_WRLCK;
354 fl.l_whence = SEEK_SET;
355 fcntl (file_fd, F_SETLKW, &fl);
357 if (found < 0)
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)
368 pbuf = NULL;
369 goto unlock_return;
373 else
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.
383 Remove it. */
384 && found < 0)
386 (void) ftruncate (file_fd, file_offset);
387 pbuf = NULL;
389 else
391 file_offset += sizeof (struct utmp);
392 pbuf = (struct utmp *) data;
395 unlock_return:
396 /* And unlock the file. */
397 fl.l_type = F_UNLCK;
398 fcntl (file_fd, F_SETLKW, &fl);
400 return pbuf;
404 static int
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;
416 else
418 char *new_name = __strdup (name);
419 if (new_name == NULL)
420 /* Out of memory. */
421 return -1;
423 if (file_name != default_file_name)
424 free ((char *) file_name);
426 file_name = new_name;
429 return 0;