1 /* GNU's read utmp module.
2 Copyright (C) 1992-2001, 2003, 2004, 2005 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by jla; revised by djm */
27 #include <sys/types.h>
37 # include "unlocked-io.h"
41 # define SIZE_MAX ((size_t) -1)
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. */
48 extract_trimmed_name (const STRUCT_UTMP *ut)
50 char *p, *trimmed_name;
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
57 trimmed_name[sizeof (UT_USER (ut))] = '\0';
58 for (p = trimmed_name + strlen (trimmed_name);
59 trimmed_name < p && p[-1] == ' ';
65 /* Is the utmp entry U desired by the user who asked for OPTIONS? */
68 desirable_utmp_entry (STRUCT_UTMP const *u, int options)
70 return ! (options & READ_UTMP_CHECK_PIDS
71 && IS_USER_PROCESS (u)
73 || (kill (UT_PID (u), 0) < 0 && errno == ESRCH)));
76 /* Read the utmp entries corresponding to file FILE into freshly-
77 malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
78 the number of entries, and return zero. If there is any error,
79 return -1, setting errno, and don't modify the parameters.
80 If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose
81 process-IDs do not currently exist. */
83 #ifdef UTMP_NAME_FUNCTION
86 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
91 STRUCT_UTMP *utmp = NULL;
94 /* Ignore the return value for now.
95 Solaris' utmpname returns 1 upon success -- which is contrary
96 to what the GNU libc version does. In addition, older GNU libc
97 versions are actually void. */
98 UTMP_NAME_FUNCTION (file);
102 while ((u = GET_UTMP_ENT ()) != NULL)
103 if (desirable_utmp_entry (u, options))
105 if (n_read == n_alloc)
106 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
122 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
127 STRUCT_UTMP *utmp = NULL;
129 FILE *f = fopen (file, "r");
136 if (n_read == n_alloc)
137 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
138 if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0)
140 n_read += desirable_utmp_entry (&utmp[n_read], options);
143 saved_errno = ferror (f) ? errno : 0;
146 if (saved_errno != 0)