getutxent.c (202527) | getutxent.c (202530) |
---|---|
1/*- 2 * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> |
28__FBSDID("$FreeBSD: head/lib/libc/gen/getutxent.c 202527 2010-01-17 21:00:29Z ed $"); | 28__FBSDID("$FreeBSD: head/lib/libc/gen/getutxent.c 202530 2010-01-17 21:40:05Z ed $"); |
29 30#include "namespace.h" 31#include <sys/endian.h> 32#include <sys/param.h> 33#include <sys/stat.h> 34#include <errno.h> 35#include <stdio.h> 36#include <string.h> 37#include <utmpx.h> 38#include "utxdb.h" 39#include "un-namespace.h" 40 41static FILE *uf = NULL; 42static int udb; | 29 30#include "namespace.h" 31#include <sys/endian.h> 32#include <sys/param.h> 33#include <sys/stat.h> 34#include <errno.h> 35#include <stdio.h> 36#include <string.h> 37#include <utmpx.h> 38#include "utxdb.h" 39#include "un-namespace.h" 40 41static FILE *uf = NULL; 42static int udb; |
43static struct utmpx utx; | |
44 45int 46setutxdb(int db, const char *file) 47{ 48 struct stat sb; 49 50 switch (db) { 51 case UTXDB_ACTIVE: --- 44 unchanged lines hidden (view full) --- 96{ 97 98 if (uf != NULL) { 99 fclose(uf); 100 uf = NULL; 101 } 102} 103 | 43 44int 45setutxdb(int db, const char *file) 46{ 47 struct stat sb; 48 49 switch (db) { 50 case UTXDB_ACTIVE: --- 44 unchanged lines hidden (view full) --- 95{ 96 97 if (uf != NULL) { 98 fclose(uf); 99 uf = NULL; 100 } 101} 102 |
104static struct futx * 105getfutxent(void) | 103static int 104getfutxent(struct futx *fu) |
106{ | 105{ |
107 static struct futx fu; | |
108 109 if (uf == NULL) 110 setutxent(); 111 if (uf == NULL) | 106 107 if (uf == NULL) 108 setutxent(); 109 if (uf == NULL) |
112 return (NULL); | 110 return (-1); |
113 114 if (udb == UTXDB_LOG) { 115 uint16_t len; 116 117 if (fread(&len, sizeof len, 1, uf) != 1) | 111 112 if (udb == UTXDB_LOG) { 113 uint16_t len; 114 115 if (fread(&len, sizeof len, 1, uf) != 1) |
118 return (NULL); | 116 return (-1); |
119 len = be16toh(len); | 117 len = be16toh(len); |
120 if (len > sizeof fu) { | 118 if (len > sizeof *fu) { |
121 /* Forward compatibility. */ | 119 /* Forward compatibility. */ |
122 if (fread(&fu, sizeof fu, 1, uf) != 1) 123 return (NULL); 124 fseek(uf, len - sizeof fu, SEEK_CUR); | 120 if (fread(fu, sizeof *fu, 1, uf) != 1) 121 return (-1); 122 fseek(uf, len - sizeof *fu, SEEK_CUR); |
125 } else { 126 /* Partial record. */ | 123 } else { 124 /* Partial record. */ |
127 memset(&fu, 0, sizeof fu); 128 if (fread(&fu, len, 1, uf) != 1) 129 return (NULL); | 125 memset(fu, 0, sizeof *fu); 126 if (fread(fu, len, 1, uf) != 1) 127 return (-1); |
130 } 131 } else { | 128 } 129 } else { |
132 if (fread(&fu, sizeof fu, 1, uf) != 1) 133 return (NULL); | 130 if (fread(fu, sizeof *fu, 1, uf) != 1) 131 return (-1); |
134 } | 132 } |
135 return (&fu); | 133 return (0); |
136} 137 138struct utmpx * 139getutxent(void) 140{ | 134} 135 136struct utmpx * 137getutxent(void) 138{ |
141 struct futx *fu; | 139 struct futx fu; |
142 | 140 |
143 fu = getfutxent(); 144 if (fu == NULL) | 141 if (getfutxent(&fu) != 0) |
145 return (NULL); | 142 return (NULL); |
146 futx_to_utx(fu, &utx); 147 return (&utx); | 143 return (futx_to_utx(&fu)); |
148} 149 150struct utmpx * 151getutxid(const struct utmpx *id) 152{ | 144} 145 146struct utmpx * 147getutxid(const struct utmpx *id) 148{ |
153 struct futx *fu; | 149 struct futx fu; |
154 155 for (;;) { | 150 151 for (;;) { |
156 fu = getfutxent(); 157 if (fu == NULL) | 152 if (getfutxent(&fu) != 0) |
158 return (NULL); 159 | 153 return (NULL); 154 |
160 switch (fu->fu_type) { 161 case BOOT_TIME: 162 case OLD_TIME: 163 case NEW_TIME: 164 case SHUTDOWN_TIME: 165 if (fu->fu_type == id->ut_type) 166 goto found; 167 break; | 155 switch (fu.fu_type) { |
168 case USER_PROCESS: 169 case INIT_PROCESS: 170 case LOGIN_PROCESS: 171 case DEAD_PROCESS: 172 switch (id->ut_type) { 173 case USER_PROCESS: 174 case INIT_PROCESS: 175 case LOGIN_PROCESS: 176 case DEAD_PROCESS: | 156 case USER_PROCESS: 157 case INIT_PROCESS: 158 case LOGIN_PROCESS: 159 case DEAD_PROCESS: 160 switch (id->ut_type) { 161 case USER_PROCESS: 162 case INIT_PROCESS: 163 case LOGIN_PROCESS: 164 case DEAD_PROCESS: |
177 if (memcmp(fu->fu_id, id->ut_id, 178 MIN(sizeof fu->fu_id, sizeof id->ut_id)) == 0) | 165 if (memcmp(fu.fu_id, id->ut_id, 166 MIN(sizeof fu.fu_id, sizeof id->ut_id)) == 0) |
179 goto found; 180 } 181 break; | 167 goto found; 168 } 169 break; |
170 default: 171 if (fu.fu_type == id->ut_type) 172 goto found; 173 break; |
|
182 } 183 } 184 185found: | 174 } 175 } 176 177found: |
186 futx_to_utx(fu, &utx); 187 return (&utx); | 178 return (futx_to_utx(&fu)); |
188} 189 190struct utmpx * 191getutxline(const struct utmpx *line) 192{ | 179} 180 181struct utmpx * 182getutxline(const struct utmpx *line) 183{ |
193 struct futx *fu; | 184 struct futx fu; |
194 195 for (;;) { | 185 186 for (;;) { |
196 fu = getfutxent(); 197 if (fu == NULL) | 187 if (getfutxent(&fu) != 0) |
198 return (NULL); 199 | 188 return (NULL); 189 |
200 switch (fu->fu_type) { | 190 switch (fu.fu_type) { |
201 case USER_PROCESS: 202 case LOGIN_PROCESS: | 191 case USER_PROCESS: 192 case LOGIN_PROCESS: |
203 if (strncmp(fu->fu_line, line->ut_line, 204 MIN(sizeof fu->fu_line, sizeof line->ut_line)) == 0) | 193 if (strncmp(fu.fu_line, line->ut_line, 194 MIN(sizeof fu.fu_line, sizeof line->ut_line)) == 0) |
205 goto found; | 195 goto found; |
196 break; |
|
206 } 207 } 208 209found: | 197 } 198 } 199 200found: |
210 futx_to_utx(fu, &utx); 211 return (&utx); | 201 return (futx_to_utx(&fu)); |
212} 213 214struct utmpx * 215getutxuser(const char *user) 216{ | 202} 203 204struct utmpx * 205getutxuser(const char *user) 206{ |
217 struct futx *fu; | 207 struct futx fu; |
218 219 for (;;) { | 208 209 for (;;) { |
220 fu = getfutxent(); 221 if (fu == NULL) | 210 if (getfutxent(&fu) != 0) |
222 return (NULL); 223 | 211 return (NULL); 212 |
224 switch (fu->fu_type) { | 213 switch (fu.fu_type) { |
225 case USER_PROCESS: | 214 case USER_PROCESS: |
226 if (strncmp(fu->fu_user, user, sizeof fu->fu_user) == 0) | 215 if (strncmp(fu.fu_user, user, sizeof fu.fu_user) == 0) |
227 goto found; | 216 goto found; |
217 break; |
|
228 } 229 } 230 231found: | 218 } 219 } 220 221found: |
232 futx_to_utx(fu, &utx); 233 return (&utx); | 222 return (futx_to_utx(&fu)); |
234} | 223} |