Home | History | Annotate | Line # | Download | only in common
rndpseudo_50.c revision 1.2.40.3
      1  1.2.40.2    martin /*	$NetBSD: rndpseudo_50.c,v 1.2.40.3 2020/04/13 08:04:14 martin Exp $	*/
      2       1.1       apb 
      3       1.1       apb /*-
      4       1.1       apb  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
      5       1.1       apb  * All rights reserved.
      6       1.1       apb  *
      7       1.1       apb  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1       apb  * by Michael Graff <explorer (at) flame.org> and Thor Lancelot Simon.
      9       1.1       apb  *
     10       1.1       apb  * Redistribution and use in source and binary forms, with or without
     11       1.1       apb  * modification, are permitted provided that the following conditions
     12       1.1       apb  * are met:
     13       1.1       apb  * 1. Redistributions of source code must retain the above copyright
     14       1.1       apb  *    notice, this list of conditions and the following disclaimer.
     15       1.1       apb  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       apb  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       apb  *    documentation and/or other materials provided with the distribution.
     18       1.1       apb  *
     19       1.1       apb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1       apb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1       apb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1       apb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1       apb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1       apb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1       apb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1       apb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1       apb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1       apb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1       apb  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1       apb  */
     31       1.1       apb 
     32       1.1       apb #include <sys/cdefs.h>
     33  1.2.40.2    martin __KERNEL_RCSID(0, "$NetBSD: rndpseudo_50.c,v 1.2.40.3 2020/04/13 08:04:14 martin Exp $");
     34       1.1       apb 
     35       1.1       apb #if defined(_KERNEL_OPT)
     36       1.1       apb #include "opt_compat_netbsd.h"
     37       1.1       apb #endif
     38       1.1       apb 
     39       1.1       apb #include <sys/param.h>
     40       1.1       apb #include <sys/file.h>
     41       1.1       apb 
     42       1.1       apb #include <sys/rnd.h>
     43  1.2.40.1  christos #include <sys/module_hook.h>
     44  1.2.40.1  christos #include <sys/compat_stub.h>
     45  1.2.40.1  christos 
     46       1.1       apb #include <compat/sys/rnd.h>
     47  1.2.40.1  christos #include <compat/common/compat_mod.h>
     48       1.1       apb 
     49       1.1       apb /*
     50       1.1       apb  * Convert from rndsource_t to rndsource50_t, for the results from
     51       1.1       apb  * RNDGETNUM50 and RNDGETNAME50.
     52       1.1       apb  */
     53       1.1       apb static void
     54       1.1       apb rndsource_to_rndsource50(rndsource_t *r, rndsource50_t *r50)
     55       1.1       apb {
     56       1.1       apb 	memset(r50, 0, sizeof(*r50));
     57       1.1       apb 	strlcpy(r50->name, r->name, sizeof(r50->name));
     58       1.1       apb 	r50->total = r->total;
     59       1.1       apb 	r50->type = r->type;
     60       1.1       apb 	r50->flags = r->flags;
     61       1.1       apb }
     62       1.1       apb 
     63       1.1       apb /*
     64       1.1       apb  * COMPAT_50 handling for rnd_ioctl.  This is called from rnd_ioctl.
     65       1.1       apb  *
     66       1.1       apb  * It also handles the case of (COMPAT_50 && COMPAT_NETBSD32).
     67       1.1       apb  */
     68       1.1       apb int
     69       1.1       apb compat_50_rnd_ioctl(struct file *fp, u_long cmd, void *addr)
     70       1.1       apb {
     71       1.1       apb 	int ret = 0;
     72       1.1       apb 
     73       1.1       apb 	switch (cmd) {
     74       1.1       apb 
     75       1.1       apb 	case RNDGETSRCNUM50:
     76       1.1       apb 	{
     77       1.1       apb 		rndstat_t rstbuf = {.start = 0};
     78       1.1       apb 		rndstat50_t *rst50 = (rndstat50_t *)addr;
     79  1.2.40.3    martin 		size_t count;
     80       1.1       apb 
     81       1.1       apb 		if (rst50->count > RND_MAXSTATCOUNT50)
     82       1.1       apb 			return EINVAL;
     83       1.1       apb 
     84       1.1       apb 		rstbuf.start = rst50->start;
     85       1.1       apb 		rstbuf.count = rst50->count;
     86       1.1       apb 
     87       1.1       apb 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNUM, &rstbuf);
     88       1.1       apb 		if (ret != 0)
     89       1.1       apb 			return ret;
     90       1.1       apb 
     91       1.1       apb 		for (count = 0; count < rst50->count; count++) {
     92       1.1       apb 			rndsource_to_rndsource50(&rstbuf.source[count],
     93       1.1       apb 			    &rst50->source[count]);
     94       1.1       apb 		}
     95       1.1       apb 		rst50->count = rstbuf.count;
     96       1.1       apb 
     97       1.1       apb 		break;
     98       1.1       apb 	}
     99       1.1       apb 
    100       1.1       apb 	case RNDGETSRCNAME50:
    101       1.1       apb 	{
    102       1.1       apb 		rndstat_name_t rstnmbuf = {.name[0] = 0};
    103       1.1       apb 		rndstat_name50_t *rstnm50;
    104       1.1       apb 		rstnm50 = (rndstat_name50_t *)addr;
    105       1.1       apb 
    106       1.1       apb 		strlcpy(rstnmbuf.name, rstnm50->name, sizeof(rstnmbuf.name));
    107       1.1       apb 
    108       1.1       apb 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNAME, &rstnmbuf);
    109       1.1       apb 		if (ret != 0)
    110       1.1       apb 			return ret;
    111       1.1       apb 
    112       1.1       apb 		rndsource_to_rndsource50(&rstnmbuf.source, &rstnm50->source);
    113       1.1       apb 
    114       1.1       apb 		break;
    115       1.1       apb 	}
    116       1.1       apb 
    117       1.1       apb 	default:
    118       1.1       apb 		return ENOTTY;
    119       1.1       apb 	}
    120       1.1       apb 
    121       1.1       apb 	return ret;
    122       1.1       apb }
    123  1.2.40.1  christos 
    124  1.2.40.1  christos void
    125  1.2.40.1  christos rndpseudo_50_init(void)
    126  1.2.40.1  christos {
    127  1.2.40.1  christos 
    128  1.2.40.2    martin 	MODULE_HOOK_SET(rnd_ioctl_50_hook, compat_50_rnd_ioctl);
    129  1.2.40.1  christos }
    130  1.2.40.1  christos 
    131  1.2.40.1  christos void
    132  1.2.40.1  christos rndpseudo_50_fini(void)
    133  1.2.40.1  christos {
    134  1.2.40.1  christos 
    135  1.2.40.1  christos 	MODULE_HOOK_UNSET(rnd_ioctl_50_hook);
    136  1.2.40.1  christos }
    137