1 /* GNU's read utmp module.
3 Copyright (C) 1992-2001, 2003-2006, 2009-2018 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 <https://www.gnu.org/licenses/>. */
18 /* Written by jla; revised by djm */
27 #include <sys/types.h>
38 # include "unlocked-io.h"
42 # pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess"
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. */
49 extract_trimmed_name (const STRUCT_UTMP
*ut
)
51 char *p
, *trimmed_name
;
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
58 trimmed_name
[sizeof (UT_USER (ut
))] = '\0';
59 for (p
= trimmed_name
+ strlen (trimmed_name
);
60 trimmed_name
< p
&& p
[-1] == ' ';
66 /* Is the utmp entry U desired by the user who asked for OPTIONS? */
69 desirable_utmp_entry (STRUCT_UTMP
const *u
, int options
)
71 bool user_proc
= IS_USER_PROCESS (u
);
72 if ((options
& READ_UTMP_USER_PROCESS
) && !user_proc
)
74 if ((options
& READ_UTMP_CHECK_PIDS
)
77 && (kill (UT_PID (u
), 0) < 0 && errno
== ESRCH
))
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. */
89 #ifdef UTMP_NAME_FUNCTION
92 read_utmp (char const *file
, size_t *n_entries
, STRUCT_UTMP
**utmp_buf
,
97 STRUCT_UTMP
*utmp
= NULL
;
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 ((char *) file
);
108 while ((u
= GET_UTMP_ENT ()) != NULL
)
109 if (desirable_utmp_entry (u
, options
))
111 if (n_read
== n_alloc
)
112 utmp
= x2nrealloc (utmp
, &n_alloc
, sizeof *utmp
);
128 read_utmp (char const *file
, size_t *n_entries
, STRUCT_UTMP
**utmp_buf
,
133 STRUCT_UTMP
*utmp
= NULL
;
135 FILE *f
= fopen (file
, "r");
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)
146 n_read
+= desirable_utmp_entry (&utmp
[n_read
], options
);
149 saved_errno
= ferror (f
) ? errno
: 0;
152 if (saved_errno
!= 0)