bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame^] | 1 | /* GNU's read utmp module. |
| 2 | |
| 3 | Copyright (C) 1992-2001, 2003-2006, 2009-2014 Free Software Foundation, Inc. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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/>. */ |
| 17 | |
| 18 | /* Written by jla; revised by djm */ |
| 19 | /* extracted for util-linux by ooprala */ |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <signal.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdint.h> |
| 31 | |
| 32 | #include "xalloc.h" |
| 33 | #include "readutmp.h" |
| 34 | |
| 35 | /* Read the utmp entries corresponding to file FILE into freshly- |
| 36 | malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to |
| 37 | the number of entries, and return zero. If there is any error, |
| 38 | return -1, setting errno, and don't modify the parameters. |
| 39 | If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose |
| 40 | process-IDs do not currently exist. */ |
| 41 | int |
| 42 | read_utmp (char const *file, size_t *n_entries, struct utmp **utmp_buf) |
| 43 | { |
| 44 | size_t n_read = 0; |
| 45 | size_t n_alloc = 0; |
| 46 | struct utmp *utmp = NULL; |
| 47 | struct utmp *u; |
| 48 | |
| 49 | /* Ignore the return value for now. |
| 50 | Solaris' utmpname returns 1 upon success -- which is contrary |
| 51 | to what the GNU libc version does. In addition, older GNU libc |
| 52 | versions are actually void. */ |
| 53 | utmpname(file); |
| 54 | |
| 55 | setutent(); |
| 56 | |
| 57 | errno = 0; |
| 58 | while ((u = getutent()) != NULL) { |
| 59 | if (n_read == n_alloc) { |
| 60 | n_alloc += 32; |
| 61 | utmp = xrealloc(utmp, n_alloc * sizeof (struct utmp)); |
| 62 | if (!utmp) |
| 63 | return -1; |
| 64 | } |
| 65 | utmp[n_read++] = *u; |
| 66 | } |
| 67 | if (!u && errno) { |
| 68 | free(utmp); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | endutent(); |
| 73 | |
| 74 | *n_entries = n_read; |
| 75 | *utmp_buf = utmp; |
| 76 | |
| 77 | return 0; |
| 78 | } |