Sync from coreutils.
[gnulib.git] / lib / readutmp.c
1 /* GNU's read utmp module.
2    Copyright (C) 1992-2001, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by jla; revised by djm */
19
20 #include <config.h>
21
22 #include "readutmp.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <signal.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "xalloc.h"
35
36 #if USE_UNLOCKED_IO
37 # include "unlocked-io.h"
38 #endif
39
40 #ifndef SIZE_MAX
41 # define SIZE_MAX ((size_t) -1)
42 #endif
43
44 /* Copy UT->ut_name into storage obtained from malloc.  Then remove any
45    trailing spaces from the copy, NUL terminate it, and return the copy.  */
46
47 char *
48 extract_trimmed_name (const STRUCT_UTMP *ut)
49 {
50   char *p, *trimmed_name;
51
52   trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
53   strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
54   /* Append a trailing NUL.  Some systems pad names shorter than the
55      maximum with spaces, others pad with NULs.  Remove any trailing
56      spaces.  */
57   trimmed_name[sizeof (UT_USER (ut))] = '\0';
58   for (p = trimmed_name + strlen (trimmed_name);
59        trimmed_name < p && p[-1] == ' ';
60        *--p = '\0')
61     continue;
62   return trimmed_name;
63 }
64
65 /* Is the utmp entry U desired by the user who asked for OPTIONS?  */
66
67 static inline bool
68 desirable_utmp_entry (STRUCT_UTMP const *u, int options)
69 {
70   return ! (options & READ_UTMP_CHECK_PIDS
71             && (UT_PID (u) <= 0
72                 || (kill (UT_PID (u), 0) < 0 && errno == ESRCH)));
73 }
74
75 /* Read the utmp entries corresponding to file FILE into freshly-
76    malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
77    the number of entries, and return zero.  If there is any error,
78    return -1, setting errno, and don't modify the parameters.
79    If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose
80    process-IDs do not currently exist.  */
81
82 #ifdef UTMP_NAME_FUNCTION
83
84 int
85 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
86            int options)
87 {
88   size_t n_read = 0;
89   size_t n_alloc = 0;
90   STRUCT_UTMP *utmp = NULL;
91   STRUCT_UTMP *u;
92
93   /* Ignore the return value for now.
94      Solaris' utmpname returns 1 upon success -- which is contrary
95      to what the GNU libc version does.  In addition, older GNU libc
96      versions are actually void.   */
97   UTMP_NAME_FUNCTION (file);
98
99   SET_UTMP_ENT ();
100
101   while ((u = GET_UTMP_ENT ()) != NULL)
102     if (desirable_utmp_entry (u, options))
103       {
104         if (n_read == n_alloc)
105           utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
106
107         utmp[n_read++] = *u;
108       }
109
110   END_UTMP_ENT ();
111
112   *n_entries = n_read;
113   *utmp_buf = utmp;
114
115   return 0;
116 }
117
118 #else
119
120 int
121 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
122            int options)
123 {
124   size_t n_read = 0;
125   size_t n_alloc = 0;
126   STRUCT_UTMP *utmp = NULL;
127   int saved_errno;
128   FILE *f = fopen (file, "r");
129
130   if (! f)
131     return -1;
132
133   for (;;)
134     {
135       if (n_read == n_alloc)
136         utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
137       if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0)
138         break;
139       n_read += desirable_utmp_entry (&utmp[n_read], options);
140     }
141
142   saved_errno = ferror (f) ? errno : 0;
143   if (fclose (f) != 0)
144     saved_errno = errno;
145   if (saved_errno != 0)
146     {
147       free (utmp);
148       errno = saved_errno;
149       return -1;
150     }
151
152   *n_entries = n_read;
153   *utmp_buf = utmp;
154
155   return 0;
156 }
157
158 #endif