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