1 /* GNU's read utmp module.
3 Copyright (C) 1992-2001, 2003-2006, 2009-2016 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by jla; revised by djm */
27 #include <sys/types.h>
38 # include "unlocked-io.h"
41 /* Copy UT->ut_name into storage obtained from malloc. Then remove any
42 trailing spaces from the copy, NUL terminate it, and return the copy. */
45 extract_trimmed_name (const STRUCT_UTMP *ut)
47 char *p, *trimmed_name;
49 trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
50 strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
51 /* Append a trailing NUL. Some systems pad names shorter than the
52 maximum with spaces, others pad with NULs. Remove any trailing
54 trimmed_name[sizeof (UT_USER (ut))] = '\0';
55 for (p = trimmed_name + strlen (trimmed_name);
56 trimmed_name < p && p[-1] == ' ';
62 /* Is the utmp entry U desired by the user who asked for OPTIONS? */
65 desirable_utmp_entry (STRUCT_UTMP const *u, int options)
67 bool user_proc = IS_USER_PROCESS (u);
68 if ((options & READ_UTMP_USER_PROCESS) && !user_proc)
70 if ((options & READ_UTMP_CHECK_PIDS)
73 && (kill (UT_PID (u), 0) < 0 && errno == ESRCH))
78 /* Read the utmp entries corresponding to file FILE into freshly-
79 malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
80 the number of entries, and return zero. If there is any error,
81 return -1, setting errno, and don't modify the parameters.
82 If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose
83 process-IDs do not currently exist. */
85 #ifdef UTMP_NAME_FUNCTION
88 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
93 STRUCT_UTMP *utmp = NULL;
96 /* Ignore the return value for now.
97 Solaris' utmpname returns 1 upon success -- which is contrary
98 to what the GNU libc version does. In addition, older GNU libc
99 versions are actually void. */
100 UTMP_NAME_FUNCTION ((char *) file);
104 while ((u = GET_UTMP_ENT ()) != NULL)
105 if (desirable_utmp_entry (u, options))
107 if (n_read == n_alloc)
108 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
124 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
129 STRUCT_UTMP *utmp = NULL;
131 FILE *f = fopen (file, "r");
138 if (n_read == n_alloc)
139 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
140 if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0)
142 n_read += desirable_utmp_entry (&utmp[n_read], options);
145 saved_errno = ferror (f) ? errno : 0;
148 if (saved_errno != 0)