Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_rndpseudo_50.c revision 1.3
      1 /*	$NetBSD: netbsd32_rndpseudo_50.c,v 1.3 2019/06/27 02:36:27 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Michael Graff <explorer (at) flame.org> and Thor Lancelot Simon.
      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 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: netbsd32_rndpseudo_50.c,v 1.3 2019/06/27 02:36:27 christos Exp $");
     34 
     35 #if defined(_KERNEL_OPT)
     36 #include "opt_compat_netbsd.h"
     37 #include "opt_compat_netbsd32.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/file.h>
     42 
     43 #include <sys/rnd.h>
     44 
     45 #include <compat/netbsd32/netbsd32.h>
     46 #include <compat/sys/rnd.h>
     47 
     48 /*
     49  * Convert from rndsource_t to rndsource50_t, for the results from
     50  * RNDGETNUM50 and RNDGETNAME50.
     51  */
     52 
     53 /*
     54  * Convert from rndsource_t to rndsource50_32_t, for the results from
     55  * RNDGETNUM50_32 and RNDGETNAME50_32.
     56  */
     57 static void
     58 rndsource_to_rndsource50_32(rndsource_t *r, rndsource50_32_t *r50_32)
     59 {
     60 	memset(r50_32, 0, sizeof(*r50_32));
     61 	strlcpy(r50_32->name, r->name, sizeof(r50_32->name));
     62 	r50_32->total = r->total;
     63 	r50_32->type = r->type;
     64 	r50_32->flags = r->flags;
     65 }
     66 
     67 /*
     68  * COMPAT32_50 handling for rnd_ioctl.  This is called from rnd_ioctl.
     69  *
     70  * It also handles the case of (COMPAT_50 && COMPAT_NETBSD32).
     71  */
     72 int
     73 compat32_50_rnd_ioctl(struct file *fp, u_long cmd, void *addr)
     74 {
     75 	int ret = 0;
     76 
     77 	switch (cmd) {
     78 	case RNDGETSRCNUM50_32:
     79 	{
     80 		rndstat_t rstbuf = {.start = 0};
     81 		rndstat50_32_t *rst50_32 = (rndstat50_32_t *)addr;
     82 		int count;
     83 
     84 		if (rst50_32->count > RND_MAXSTATCOUNT50)
     85 			return (EINVAL);
     86 
     87 		rstbuf.start = rst50_32->start;
     88 		rstbuf.count = rst50_32->count;
     89 
     90 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNUM, &rstbuf);
     91 		if (ret != 0)
     92 			return ret;
     93 
     94 		for (count = 0; count < rst50_32->count; count++) {
     95 			rndsource_to_rndsource50_32(&rstbuf.source[count],
     96 			    &rst50_32->source[count]);
     97 		}
     98 		rst50_32->count = rstbuf.count;
     99 
    100 		break;
    101 	}
    102 
    103 	case RNDGETSRCNAME50_32:
    104 	{
    105 		rndstat_name_t rstnmbuf = {.name[0] = 0};
    106 		rndstat_name50_32_t *rstnm50_32;
    107 		rstnm50_32 = (rndstat_name50_32_t *)addr;
    108 
    109 		strlcpy(rstnmbuf.name, rstnm50_32->name, sizeof(rstnmbuf.name));
    110 
    111 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNAME, &rstnmbuf);
    112 		if (ret != 0)
    113 			return ret;
    114 
    115 		rndsource_to_rndsource50_32(&rstnmbuf.source,
    116 		    &rstnm50_32->source);
    117 
    118 		break;
    119 	}
    120 
    121 	default:
    122 		return ENOTTY;
    123 	}
    124 
    125 	return ret;
    126 }
    127