utxdb.c revision 202188
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 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 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/utxdb.c 202188 2010-01-13 17:29:55Z ed $"); 29 30#include "namespace.h" 31#include <sys/endian.h> 32#include <sys/param.h> 33#include <string.h> 34#include <utmpx.h> 35#include "utxdb.h" 36#include "un-namespace.h" 37 38#define UTOF_STRING(ut, fu, field) do { \ 39 strncpy((fu)->fu_ ## field, (ut)->ut_ ## field, \ 40 MIN(sizeof (fu)->fu_ ## field, sizeof (ut)->ut_ ## field)); \ 41} while (0) 42#define UTOF_ID(ut, fu) do { \ 43 memcpy((fu)->fu_id, (ut)->ut_id, \ 44 MIN(sizeof (fu)->fu_id, sizeof (ut)->ut_id)); \ 45} while (0) 46#define UTOF_PID(ut, fu) do { \ 47 (fu)->fu_pid = htobe32((ut)->ut_pid); \ 48} while (0) 49#define UTOF_TYPE(ut, fu) do { \ 50 (fu)->fu_type = (ut)->ut_type; \ 51} while (0) 52#define UTOF_TV(ut, fu) do { \ 53 (fu)->fu_tv = htobe64((uint64_t)(ut)->ut_tv.tv_sec * 1000000 + \ 54 (uint64_t)(ut)->ut_tv.tv_usec); \ 55} while (0) 56 57void 58utx_to_futx(const struct utmpx *ut, struct futx *fu) 59{ 60 61 memset(fu, 0, sizeof *fu); 62 63 switch (ut->ut_type) { 64 case BOOT_TIME: 65 case OLD_TIME: 66 case NEW_TIME: 67 /* Extension: shutdown time. */ 68 case SHUTDOWN_TIME: 69 break; 70 case USER_PROCESS: 71 UTOF_ID(ut, fu); 72 UTOF_STRING(ut, fu, user); 73 UTOF_STRING(ut, fu, line); 74 /* Extension: host name. */ 75 UTOF_STRING(ut, fu, host); 76 UTOF_PID(ut, fu); 77 break; 78 case INIT_PROCESS: 79 UTOF_ID(ut, fu); 80 UTOF_PID(ut, fu); 81 break; 82 case LOGIN_PROCESS: 83 UTOF_ID(ut, fu); 84 UTOF_STRING(ut, fu, user); 85 UTOF_PID(ut, fu); 86 break; 87 case DEAD_PROCESS: 88 UTOF_ID(ut, fu); 89 UTOF_PID(ut, fu); 90 break; 91 default: 92 fu->fu_type = EMPTY; 93 return; 94 } 95 96 UTOF_TYPE(ut, fu); 97 UTOF_TV(ut, fu); 98} 99 100#define FTOU_STRING(fu, ut, field) do { \ 101 strncpy((ut)->ut_ ## field, (fu)->fu_ ## field, \ 102 MIN(sizeof (ut)->ut_ ## field - 1, sizeof (fu)->fu_ ## field)); \ 103} while (0) 104#define FTOU_ID(fu, ut) do { \ 105 memcpy((ut)->ut_id, (fu)->fu_id, \ 106 MIN(sizeof (ut)->ut_id, sizeof (fu)->fu_id)); \ 107} while (0) 108#define FTOU_PID(fu, ut) do { \ 109 (ut)->ut_pid = be32toh((fu)->fu_pid); \ 110} while (0) 111#define FTOU_TYPE(fu, ut) do { \ 112 (ut)->ut_type = (fu)->fu_type; \ 113} while (0) 114#define FTOU_TV(fu, ut) do { \ 115 uint64_t t; \ 116 t = be64toh((fu)->fu_tv); \ 117 (ut)->ut_tv.tv_sec = t / 1000000; \ 118 (ut)->ut_tv.tv_usec = t % 1000000; \ 119} while (0) 120 121void 122futx_to_utx(const struct futx *fu, struct utmpx *ut) 123{ 124 125 memset(ut, 0, sizeof *ut); 126 127 switch (fu->fu_type) { 128 case BOOT_TIME: 129 case OLD_TIME: 130 case NEW_TIME: 131 /* Extension: shutdown time. */ 132 case SHUTDOWN_TIME: 133 break; 134 case USER_PROCESS: 135 FTOU_ID(fu, ut); 136 FTOU_STRING(fu, ut, user); 137 FTOU_STRING(fu, ut, line); 138 /* Extension: host name. */ 139 FTOU_STRING(fu, ut, host); 140 FTOU_PID(fu, ut); 141 break; 142 case INIT_PROCESS: 143 FTOU_ID(fu, ut); 144 FTOU_PID(fu, ut); 145 break; 146 case LOGIN_PROCESS: 147 FTOU_ID(fu, ut); 148 FTOU_STRING(fu, ut, user); 149 FTOU_PID(fu, ut); 150 break; 151 case DEAD_PROCESS: 152 FTOU_ID(fu, ut); 153 FTOU_PID(fu, ut); 154 break; 155 default: 156 ut->ut_type = EMPTY; 157 return; 158 } 159 160 FTOU_TYPE(fu, ut); 161 FTOU_TV(fu, ut); 162} 163