Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | #include <config.h> | - |
21 | | - |
22 | #include "readutmp.h" | - |
23 | | - |
24 | #include <errno.h> | - |
25 | #include <stdio.h> | - |
26 | | - |
27 | #include <sys/types.h> | - |
28 | #include <sys/stat.h> | - |
29 | #include <signal.h> | - |
30 | #include <stdbool.h> | - |
31 | #include <string.h> | - |
32 | #include <stdlib.h> | - |
33 | #include <stdint.h> | - |
34 | | - |
35 | #include "xalloc.h" | - |
36 | | - |
37 | #if USE_UNLOCKED_IO | - |
38 | # include "unlocked-io.h" | - |
39 | #endif | - |
40 | | - |
41 | | - |
42 | | - |
43 | | - |
44 | char * | - |
45 | extract_trimmed_name (const STRUCT_UTMP *ut) | - |
46 | { | - |
47 | char *p, *trimmed_name; | - |
48 | | - |
49 | trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1); | - |
50 | strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut))); | - |
51 | | - |
52 | | - |
53 | | - |
54 | trimmed_name[sizeof (UT_USER (ut))] = '\0'; | - |
55 | for (p = trimmed_name + strlen (trimmed_name); | - |
56 | trimmed_name < p && p[-1] == ' ';TRUE | evaluated 1 time by 1 test | FALSE | never evaluated |
TRUE | never evaluated | FALSE | evaluated 1 time by 1 test |
| 0-1 |
57 | *--p = '\0') | - |
58 | continue; never executed: continue; | 0 |
59 | return trimmed_name;executed 1 time by 1 test: return trimmed_name; | 1 |
60 | } | - |
61 | | - |
62 | | - |
63 | | - |
64 | static bool | - |
65 | desirable_utmp_entry (STRUCT_UTMP const *u, int options) | - |
66 | { | - |
67 | bool user_proc = IS_USER_PROCESS (u); dead code: ((u)->ut_tv.tv_sec) != 0 TRUE | evaluated 12 times by 3 tests | FALSE | never evaluated |
TRUE | evaluated 3 times by 3 tests | FALSE | evaluated 9 times by 3 tests |
| - |
68 | if ((options & READ_UTMP_USER_PROCESS) && !user_proc)TRUE | never evaluated | FALSE | evaluated 12 times by 3 tests |
TRUE | never evaluated | FALSE | never evaluated |
| 0-12 |
69 | return false; never executed: return 0 ; | 0 |
70 | if ((options & READ_UTMP_CHECK_PIDS)TRUE | evaluated 8 times by 2 tests | FALSE | evaluated 4 times by 1 test |
| 4-8 |
71 | && user_procTRUE | evaluated 2 times by 2 tests | FALSE | evaluated 6 times by 2 tests |
| 2-6 |
72 | && 0 < UT_PID (u)TRUE | evaluated 2 times by 2 tests | FALSE | never evaluated |
| 0-2 |
73 | && (kill (UT_PID (u), 0) < 0 && errno == ESRCH))TRUE | never evaluated | FALSE | evaluated 2 times by 2 tests |
TRUE | never evaluated | FALSE | never evaluated |
| 0-2 |
74 | return false; never executed: return 0 ; | 0 |
75 | return true;executed 12 times by 3 tests: return 1 ; | 12 |
76 | } | - |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | | - |
84 | | - |
85 | #ifdef UTMP_NAME_FUNCTION | - |
86 | | - |
87 | int | - |
88 | read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf, | - |
89 | int options) | - |
90 | { | - |
91 | size_t n_read = 0; | - |
92 | size_t n_alloc = 0; | - |
93 | STRUCT_UTMP *utmp = NULL; | - |
94 | STRUCT_UTMP *u; | - |
95 | | - |
96 | | - |
97 | | - |
98 | | - |
99 | | - |
100 | UTMP_NAME_FUNCTION ((char *) file); | - |
101 | | - |
102 | SET_UTMP_ENT (); | - |
103 | | - |
104 | while ((u = GET_UTMP_ENT ()) != NULL)TRUE | evaluated 12 times by 3 tests | FALSE | evaluated 3 times by 3 tests |
| 3-12 |
105 | if (desirable_utmp_entry (u, options))TRUE | evaluated 12 times by 3 tests | FALSE | never evaluated |
| 0-12 |
106 | { | - |
107 | if (n_read == n_alloc)TRUE | evaluated 9 times by 3 tests | FALSE | evaluated 3 times by 3 tests |
| 3-9 |
108 | utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);executed 9 times by 3 tests: utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp); | 9 |
109 | | - |
110 | utmp[n_read++] = *u; | - |
111 | }executed 12 times by 3 tests: end of block | 12 |
112 | | - |
113 | END_UTMP_ENT (); | - |
114 | | - |
115 | *n_entries = n_read; | - |
116 | *utmp_buf = utmp; | - |
117 | | - |
118 | return 0;executed 3 times by 3 tests: return 0; | 3 |
119 | } | - |
120 | | - |
121 | #else | - |
122 | | - |
123 | int | - |
124 | read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf, | - |
125 | int options) | - |
126 | { | - |
127 | size_t n_read = 0; | - |
128 | size_t n_alloc = 0; | - |
129 | STRUCT_UTMP *utmp = NULL; | - |
130 | int saved_errno; | - |
131 | FILE *f = fopen (file, "r"); | - |
132 | | - |
133 | if (! f) | - |
134 | return -1; | - |
135 | | - |
136 | for (;;) | - |
137 | { | - |
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) | - |
141 | break; | - |
142 | n_read += desirable_utmp_entry (&utmp[n_read], options); | - |
143 | } | - |
144 | | - |
145 | saved_errno = ferror (f) ? errno : 0; | - |
146 | if (fclose (f) != 0) | - |
147 | saved_errno = errno; | - |
148 | if (saved_errno != 0) | - |
149 | { | - |
150 | free (utmp); | - |
151 | errno = saved_errno; | - |
152 | return -1; | - |
153 | } | - |
154 | | - |
155 | *n_entries = n_read; | - |
156 | *utmp_buf = utmp; | - |
157 | | - |
158 | return 0; | - |
159 | } | - |
160 | | - |
161 | #endif | - |
| | |