chiark / gitweb /
Apply https://sourceware.org/git/?p=glibc.git;a=commit;h=d5dd6189d506068ed11c8bfa1e1e...
[eglibc.git] / login / utmp_file.c
1 /* Copyright (C) 1996-2004, 2007, 2008, 2009 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.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
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    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <utmp.h>
30 #include <not-cancel.h>
31 #include <kernel-features.h>
32
33 #include "utmp-private.h"
34 #include "utmp-equal.h"
35
36
37 /* Descriptor for the file and position.  */
38 static int file_fd = -1;
39 static bool file_writable;
40 static off64_t file_offset;
41
42 /* Cache for the last read entry.  */
43 static struct utmp last_entry;
44
45
46 /* Locking timeout.  */
47 #ifndef TIMEOUT
48 # define TIMEOUT 10
49 #endif
50
51 /* Do-nothing handler for locking timeout.  */
52 static void timeout_handler (int signum) {};
53
54 /* LOCK_FILE(fd, type) failure_statement
55      attempts to get a lock on the utmp file referenced by FD.  If it fails,
56      the failure_statement is executed, otherwise it is skipped.
57    LOCKING_FAILED()
58      jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
59    UNLOCK_FILE(fd)
60      unlocks the utmp file referenced by FD and performs the cleanup of
61      LOCK_FILE.
62  */
63 #define LOCK_FILE(fd, type) \
64 {                                                                             \
65   struct flock fl;                                                            \
66   struct sigaction action, old_action;                                        \
67   unsigned int old_timeout;                                                   \
68                                                                               \
69   /* Cancel any existing alarm.  */                                           \
70   old_timeout = alarm (0);                                                    \
71                                                                               \
72   /* Establish signal handler.  */                                            \
73   action.sa_handler = timeout_handler;                                        \
74   __sigemptyset (&action.sa_mask);                                            \
75   action.sa_flags = 0;                                                        \
76   __sigaction (SIGALRM, &action, &old_action);                                \
77                                                                               \
78   alarm (TIMEOUT);                                                            \
79                                                                               \
80   /* Try to get the lock.  */                                                 \
81   memset (&fl, '\0', sizeof (struct flock));                                  \
82   fl.l_type = (type);                                                         \
83   fl.l_whence = SEEK_SET;                                                     \
84   if (fcntl_not_cancel ((fd), F_SETLKW, &fl) < 0)
85
86 #define LOCKING_FAILED() \
87   goto unalarm_return
88
89 #define UNLOCK_FILE(fd) \
90   /* Unlock the file.  */                                                     \
91   fl.l_type = F_UNLCK;                                                        \
92   fcntl_not_cancel ((fd), F_SETLKW, &fl);                                     \
93                                                                               \
94  unalarm_return:                                                              \
95   /* Reset the signal handler and alarm.  We must reset the alarm             \
96      before resetting the handler so our alarm does not generate a            \
97      spurious SIGALRM seen by the user.  However, we cannot just set          \
98      the user's old alarm before restoring the handler, because then          \
99      it's possible our handler could catch the user alarm's SIGARLM           \
100      and then the user would never see the signal he expected.  */            \
101   alarm (0);                                                                  \
102   __sigaction (SIGALRM, &old_action, NULL);                                   \
103   if (old_timeout != 0)                                                       \
104     alarm (old_timeout);                                                      \
105 } while (0)
106
107
108 /* Functions defined here.  */
109 static int setutent_file (void);
110 static int getutent_r_file (struct utmp *buffer, struct utmp **result);
111 static int getutid_r_file (const struct utmp *key, struct utmp *buffer,
112                            struct utmp **result);
113 static int getutline_r_file (const struct utmp *key, struct utmp *buffer,
114                              struct utmp **result);
115 static struct utmp *pututline_file (const struct utmp *data);
116 static void endutent_file (void);
117 static int updwtmp_file (const char *file, const struct utmp *utmp);
118
119 /* Jump table for file functions.  */
120 const struct utfuncs __libc_utmp_file_functions =
121 {
122   setutent_file,
123   getutent_r_file,
124   getutid_r_file,
125   getutline_r_file,
126   pututline_file,
127   endutent_file,
128   updwtmp_file
129 };
130
131
132 #ifndef TRANSFORM_UTMP_FILE_NAME
133 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
134 #endif
135
136 static int
137 setutent_file (void)
138 {
139   if (file_fd < 0)
140     {
141       const char *file_name;
142
143       file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
144
145 #ifdef O_CLOEXEC
146 # define O_flags O_LARGEFILE | O_CLOEXEC
147 #else
148 # define O_flags O_LARGEFILE
149 #endif
150       file_writable = false;
151       file_fd = open_not_cancel_2 (file_name, O_RDONLY | O_flags);
152       if (file_fd == -1)
153         return 0;
154
155 #ifndef __ASSUME_O_CLOEXEC
156 # ifdef O_CLOEXEC
157       if (__have_o_cloexec <= 0)
158 # endif
159         {
160           /* We have to make sure the file is `closed on exec'.  */
161           int result = fcntl_not_cancel (file_fd, F_GETFD, 0);
162           if (result >= 0)
163             {
164 # ifdef O_CLOEXEC
165               if (__have_o_cloexec == 0)
166                 __have_o_cloexec = (result & FD_CLOEXEC) ? 1 : -1;
167
168               if (__have_o_cloexec < 0)
169 # endif
170                 result = fcntl_not_cancel (file_fd, F_SETFD,
171                                            result | FD_CLOEXEC);
172             }
173
174           if (result == -1)
175             {
176               close_not_cancel_no_status (file_fd);
177               return 0;
178             }
179         }
180 #endif
181     }
182
183   __lseek64 (file_fd, 0, SEEK_SET);
184   file_offset = 0;
185
186   /* Make sure the entry won't match.  */
187 #if _HAVE_UT_TYPE - 0
188   last_entry.ut_type = -1;
189 #else
190   last_entry.ut_line[0] = '\177';
191 # if _HAVE_UT_ID - 0
192   last_entry.ut_id[0] = '\0';
193 # endif
194 #endif
195
196   return 1;
197 }
198
199
200 static int
201 getutent_r_file (struct utmp *buffer, struct utmp **result)
202 {
203   ssize_t nbytes;
204
205   assert (file_fd >= 0);
206
207   if (file_offset == -1l)
208     {
209       /* Not available.  */
210       *result = NULL;
211       return -1;
212     }
213
214   LOCK_FILE (file_fd, F_RDLCK)
215     {
216       nbytes = 0;
217       LOCKING_FAILED ();
218     }
219
220   /* Read the next entry.  */
221   nbytes = read_not_cancel (file_fd, &last_entry, sizeof (struct utmp));
222
223   UNLOCK_FILE (file_fd);
224
225   if (nbytes != sizeof (struct utmp))
226     {
227       if (nbytes != 0)
228         file_offset = -1l;
229       *result = NULL;
230       return -1;
231     }
232
233   /* Update position pointer.  */
234   file_offset += sizeof (struct utmp);
235
236   memcpy (buffer, &last_entry, sizeof (struct utmp));
237   *result = buffer;
238
239   return 0;
240 }
241
242
243 static int
244 internal_getut_r (const struct utmp *id, struct utmp *buffer,
245                   bool *lock_failed)
246 {
247   int result = -1;
248
249   LOCK_FILE (file_fd, F_RDLCK)
250     {
251       *lock_failed = true;
252       LOCKING_FAILED ();
253     }
254
255 #if _HAVE_UT_TYPE - 0
256   if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
257       || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
258     {
259       /* Search for next entry with type RUN_LVL, BOOT_TIME,
260          OLD_TIME, or NEW_TIME.  */
261
262       while (1)
263         {
264           /* Read the next entry.  */
265           if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
266               != sizeof (struct utmp))
267             {
268               __set_errno (ESRCH);
269               file_offset = -1l;
270               goto unlock_return;
271             }
272           file_offset += sizeof (struct utmp);
273
274           if (id->ut_type == buffer->ut_type)
275             break;
276         }
277     }
278   else
279 #endif /* _HAVE_UT_TYPE */
280     {
281       /* Search for the next entry with the specified ID and with type
282          INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS.  */
283
284       while (1)
285         {
286           /* Read the next entry.  */
287           if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
288               != sizeof (struct utmp))
289             {
290               __set_errno (ESRCH);
291               file_offset = -1l;
292               goto unlock_return;
293             }
294           file_offset += sizeof (struct utmp);
295
296           if (__utmp_equal (buffer, id))
297             break;
298         }
299     }
300
301   result = 0;
302
303 unlock_return:
304   UNLOCK_FILE (file_fd);
305
306   return result;
307 }
308
309
310 /* For implementing this function we don't use the getutent_r function
311    because we can avoid the reposition on every new entry this way.  */
312 static int
313 getutid_r_file (const struct utmp *id, struct utmp *buffer,
314                 struct utmp **result)
315 {
316   assert (file_fd >= 0);
317
318   if (file_offset == -1l)
319     {
320       *result = NULL;
321       return -1;
322     }
323
324   /* We don't have to distinguish whether we can lock the file or
325      whether there is no entry.  */
326   bool lock_failed = false;
327   if (internal_getut_r (id, &last_entry, &lock_failed) < 0)
328     {
329       *result = NULL;
330       return -1;
331     }
332
333   memcpy (buffer, &last_entry, sizeof (struct utmp));
334   *result = buffer;
335
336   return 0;
337 }
338
339
340 /* For implementing this function we don't use the getutent_r function
341    because we can avoid the reposition on every new entry this way.  */
342 static int
343 getutline_r_file (const struct utmp *line, struct utmp *buffer,
344                   struct utmp **result)
345 {
346   assert (file_fd >= 0);
347
348   if (file_offset == -1l)
349     {
350       *result = NULL;
351       return -1;
352     }
353
354   LOCK_FILE (file_fd, F_RDLCK)
355     {
356       *result = NULL;
357       LOCKING_FAILED ();
358     }
359
360   while (1)
361     {
362       /* Read the next entry.  */
363       if (read_not_cancel (file_fd, &last_entry, sizeof (struct utmp))
364           != sizeof (struct utmp))
365         {
366           __set_errno (ESRCH);
367           file_offset = -1l;
368           *result = NULL;
369           goto unlock_return;
370         }
371       file_offset += sizeof (struct utmp);
372
373       /* Stop if we found a user or login entry.  */
374       if (
375 #if _HAVE_UT_TYPE - 0
376           (last_entry.ut_type == USER_PROCESS
377            || last_entry.ut_type == LOGIN_PROCESS)
378           &&
379 #endif
380           !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
381         break;
382     }
383
384   memcpy (buffer, &last_entry, sizeof (struct utmp));
385   *result = buffer;
386
387 unlock_return:
388   UNLOCK_FILE (file_fd);
389
390   return ((*result == NULL) ? -1 : 0);
391 }
392
393
394 static struct utmp *
395 pututline_file (const struct utmp *data)
396 {
397   struct utmp buffer;
398   struct utmp *pbuf;
399   int found;
400
401   assert (file_fd >= 0);
402
403   if (! file_writable)
404     {
405       /* We must make the file descriptor writable before going on.  */
406       const char *file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
407
408       int new_fd = open_not_cancel_2 (file_name, O_RDWR | O_flags);
409       if (new_fd == -1)
410         return NULL;
411
412 #ifndef __ASSUME_O_CLOEXEC
413 # ifdef O_CLOEXEC
414       if (__have_o_cloexec <= 0)
415 # endif
416         {
417           /* We have to make sure the file is `closed on exec'.  */
418           int result = fcntl_not_cancel (file_fd, F_GETFD, 0);
419           if (result >= 0)
420             {
421 # ifdef O_CLOEXEC
422               if (__have_o_cloexec == 0)
423                 __have_o_cloexec = (result & FD_CLOEXEC) ? 1 : -1;
424
425               if (__have_o_cloexec < 0)
426 # endif
427                 result = fcntl_not_cancel (file_fd, F_SETFD,
428                                            result | FD_CLOEXEC);
429             }
430
431           if (result == -1)
432             {
433               close_not_cancel_no_status (file_fd);
434               return NULL;
435             }
436         }
437 #endif
438
439       if (__lseek64 (new_fd, __lseek64 (file_fd, 0, SEEK_CUR), SEEK_SET) == -1
440           || __dup2 (new_fd, file_fd) < 0)
441         {
442           close_not_cancel_no_status (new_fd);
443           return NULL;
444         }
445       close_not_cancel_no_status (new_fd);
446       file_writable = true;
447     }
448
449   /* Find the correct place to insert the data.  */
450   if (file_offset > 0
451       && (
452 #if _HAVE_UT_TYPE - 0
453           (last_entry.ut_type == data->ut_type
454            && (last_entry.ut_type == RUN_LVL
455                || last_entry.ut_type == BOOT_TIME
456                || last_entry.ut_type == OLD_TIME
457                || last_entry.ut_type == NEW_TIME))
458           ||
459 #endif
460           __utmp_equal (&last_entry, data)))
461     found = 1;
462   else
463     {
464       bool lock_failed = false;
465       found = internal_getut_r (data, &buffer, &lock_failed);
466
467       if (__builtin_expect (lock_failed, false))
468         {
469           __set_errno (EAGAIN);
470           return NULL;
471         }
472     }
473
474   LOCK_FILE (file_fd, F_WRLCK)
475     {
476       pbuf = NULL;
477       LOCKING_FAILED ();
478     }
479
480   if (found < 0)
481     {
482       /* We append the next entry.  */
483       file_offset = __lseek64 (file_fd, 0, SEEK_END);
484       if (file_offset % sizeof (struct utmp) != 0)
485         {
486           file_offset -= file_offset % sizeof (struct utmp);
487           __ftruncate64 (file_fd, file_offset);
488
489           if (__lseek64 (file_fd, 0, SEEK_END) < 0)
490             {
491               pbuf = NULL;
492               goto unlock_return;
493             }
494         }
495     }
496   else
497     {
498       /* We replace the just read entry.  */
499       file_offset -= sizeof (struct utmp);
500       __lseek64 (file_fd, file_offset, SEEK_SET);
501     }
502
503   /* Write the new data.  */
504   if (write_not_cancel (file_fd, data, sizeof (struct utmp))
505       != sizeof (struct utmp))
506     {
507       /* If we appended a new record this is only partially written.
508          Remove it.  */
509       if (found < 0)
510         (void) __ftruncate64 (file_fd, file_offset);
511       pbuf = NULL;
512     }
513   else
514     {
515       file_offset += sizeof (struct utmp);
516       pbuf = (struct utmp *) data;
517     }
518
519  unlock_return:
520   UNLOCK_FILE (file_fd);
521
522   return pbuf;
523 }
524
525
526 static void
527 endutent_file (void)
528 {
529   assert (file_fd >= 0);
530
531   close_not_cancel_no_status (file_fd);
532   file_fd = -1;
533 }
534
535
536 static int
537 updwtmp_file (const char *file, const struct utmp *utmp)
538 {
539   int result = -1;
540   off64_t offset;
541   int fd;
542
543   /* Open WTMP file.  */
544   fd = open_not_cancel_2 (file, O_WRONLY | O_LARGEFILE);
545   if (fd < 0)
546     return -1;
547
548   LOCK_FILE (fd, F_WRLCK)
549     LOCKING_FAILED ();
550
551   /* Remember original size of log file.  */
552   offset = __lseek64 (fd, 0, SEEK_END);
553   if (offset % sizeof (struct utmp) != 0)
554     {
555       offset -= offset % sizeof (struct utmp);
556       __ftruncate64 (fd, offset);
557
558       if (__lseek64 (fd, 0, SEEK_END) < 0)
559         goto unlock_return;
560     }
561
562   /* Write the entry.  If we can't write all the bytes, reset the file
563      size back to the original size.  That way, no partial entries
564      will remain.  */
565   if (write_not_cancel (fd, utmp, sizeof (struct utmp))
566       != sizeof (struct utmp))
567     {
568       __ftruncate64 (fd, offset);
569       goto unlock_return;
570     }
571
572   result = 0;
573
574 unlock_return:
575   UNLOCK_FILE (fd);
576
577   /* Close WTMP file.  */
578   close_not_cancel_no_status (fd);
579
580   return result;
581 }