Home | History | Annotate | Line # | Download | only in gen
compat_utmpx.c revision 1.2.8.2
      1 /*	$NetBSD: compat_utmpx.c,v 1.2.8.2 2008/11/08 21:45:38 christos Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 __RCSID("$NetBSD: compat_utmpx.c,v 1.2.8.2 2008/11/08 21:45:38 christos Exp $");
     35 #endif /* LIBC_SCCS and not lint */
     36 
     37 #include "namespace.h"
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 
     41 #define __LIBC12_SOURCE__
     42 #include <assert.h>
     43 #include <sys/time.h>
     44 #include <string.h>
     45 #include <compat/sys/time.h>
     46 #include <utmpx.h>
     47 #include <compat/include/utmpx.h>
     48 
     49 __warn_references(getutxent,
     50     "warning: reference to compatibility getutxent(); include <utmpx.h> for correct reference")
     51 __warn_references(getutxid,
     52     "warning: reference to compatibility getutxid(); include <utmpx.h> for correct reference")
     53 __warn_references(getutxline,
     54     "warning: reference to compatibility getutxline(); include <utmpx.h> for correct reference")
     55 __warn_references(pututxline,
     56     "warning: reference to compatibility pututxline(); include <utmpx.h> for correct reference")
     57 __warn_references(updwtmpx,
     58     "warning: reference to compatibility updwtmpx(); include <utmpx.h> for correct reference")
     59 __warn_references(getlastlogx,
     60     "warning: reference to compatibility getlastlogx(); include <utmpx.h> for correct reference")
     61 __warn_references(updlastlogx,
     62     "warning: reference to compatibility updlastlogx(); include <utmpx.h> for correct reference")
     63 __warn_references(getutmp,
     64     "warning: reference to compatibility getutmp(); include <utmpx.h> for correct reference")
     65 __warn_references(getutmpx,
     66     "warning: reference to compatibility getutmpx(); include <utmpx.h> for correct reference")
     67 
     68 static struct utmpx50 *
     69 cvt(struct utmpx *ut)
     70 {
     71 	timeval_to_timeval50(&ut->ut_tv, (void *)&ut->ut_tv);
     72 	return (void *)ut;
     73 }
     74 
     75 static void
     76 utmpx50_to_utmpx(const struct utmpx50 *ut50, struct utmpx *ut)
     77 {
     78 	(void)memcpy(ut, ut50, sizeof(*ut));
     79 	timeval50_to_timeval(&ut50->ut_tv, &ut->ut_tv);
     80 }
     81 
     82 static void
     83 utmpx_to_utmpx50(const struct utmpx *ut, struct utmpx50 *ut50)
     84 {
     85 	(void)memcpy(ut50, ut, sizeof(*ut50));
     86 	timeval_to_timeval50(&ut->ut_tv, &ut50->ut_tv);
     87 }
     88 
     89 static void
     90 lastlogx50_to_lastlogx(const struct lastlogx50 *ll50, struct lastlogx *ll)
     91 {
     92 	(void)memcpy(ll->ll_line, ll50->ll_line, sizeof(ll->ll_line));
     93 	(void)memcpy(ll->ll_host, ll50->ll_host, sizeof(ll->ll_host));
     94 	(void)memcpy(&ll->ll_ss, &ll50->ll_ss, sizeof(&ll->ll_ss));
     95 	timeval50_to_timeval(&ll50->ll_tv, &ll->ll_tv);
     96 }
     97 
     98 static void
     99 lastlogx_to_lastlogx50(const struct lastlogx *ll, struct lastlogx50 *ll50)
    100 {
    101 	(void)memcpy(ll50->ll_line, ll->ll_line, sizeof(ll50->ll_line));
    102 	(void)memcpy(ll50->ll_host, ll->ll_host, sizeof(ll50->ll_host));
    103 	(void)memcpy(&ll50->ll_ss, &ll->ll_ss, sizeof(&ll50->ll_ss));
    104 	timeval_to_timeval50(&ll->ll_tv, &ll50->ll_tv);
    105 }
    106 
    107 struct utmpx50 *
    108 getutxent(void)
    109 {
    110 	return cvt(__getutxent50());
    111 }
    112 
    113 struct utmpx50 *
    114 getutxid(const struct utmpx50 *ut50)
    115 {
    116 	struct utmpx ut;
    117 	utmpx50_to_utmpx(ut50, &ut);
    118 	return cvt(__getutxid50(&ut));
    119 }
    120 
    121 struct utmpx50 *
    122 getutxline(const struct utmpx50 *ut50)
    123 {
    124 	struct utmpx ut;
    125 	utmpx50_to_utmpx(ut50, &ut);
    126 	return cvt(__getutxline50(&ut));
    127 }
    128 
    129 struct utmpx50 *
    130 pututxline(const struct utmpx50 *ut50)
    131 {
    132 	struct utmpx ut;
    133 	utmpx50_to_utmpx(ut50, &ut);
    134 	return cvt(__pututxline50(&ut));
    135 }
    136 
    137 int
    138 updwtmpx(const char *fname, const struct utmpx50 *ut50)
    139 {
    140 	struct utmpx ut;
    141 	utmpx50_to_utmpx(ut50, &ut);
    142 	return __updwtmpx50(fname, &ut);
    143 }
    144 
    145 struct lastlogx50 *
    146 __getlastlogx13(const char *fname, uid_t uid, struct lastlogx50 *ll50)
    147 {
    148 	struct lastlogx ll;
    149 	if (__getlastlogx50(fname, uid, &ll) == NULL)
    150 		return NULL;
    151 	lastlogx_to_lastlogx50(&ll, ll50);
    152 	return ll50;
    153 }
    154 
    155 static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
    156 
    157 int
    158 lastlogxname(const char *fname)
    159 {
    160 	size_t len;
    161 
    162 	_DIAGASSERT(fname != NULL);
    163 
    164 	len = strlen(fname);
    165 
    166 	if (len >= sizeof(llfile))
    167 		return 0;
    168 
    169 	/* must end in x! */
    170 	if (fname[len - 1] != 'x')
    171 		return 0;
    172 
    173 	(void)strlcpy(llfile, fname, sizeof(llfile));
    174 	return 1;
    175 }
    176 
    177 struct lastlogx50 *
    178 getlastlogx(uid_t uid, struct lastlogx50 *ll)
    179 {
    180 
    181 	return __getlastlogx13(llfile, uid, ll);
    182 }
    183 
    184 int
    185 updlastlogx(const char *fname, uid_t uid, struct lastlogx50 *ll50)
    186 {
    187 	struct lastlogx ll;
    188 	lastlogx50_to_lastlogx(ll50, &ll);
    189 	return __updlastlogx50(fname, uid, &ll);
    190 }
    191 
    192 void
    193 getutmp(const struct utmpx50 *utx50, struct utmp *ut)
    194 {
    195 	struct utmpx utx;
    196 	utmpx50_to_utmpx(utx50, &utx);
    197 	__getutmp50(&utx, ut);
    198 }
    199 
    200 void
    201 getutmpx(const struct utmp *ut, struct utmpx50 *utx50)
    202 {
    203 	struct utmpx utx;
    204 	__getutmpx50(ut, &utx);
    205 	utmpx_to_utmpx50(&utx, utx50);
    206 }
    207