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