rndpseudo_50.c revision 1.2.38.1 1 /* $NetBSD: rndpseudo_50.c,v 1.2.38.1 2018/03/21 02:01:34 pgoyette 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: rndpseudo_50.c,v 1.2.38.1 2018/03/21 02:01:34 pgoyette Exp $");
34
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_netbsd.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/file.h>
41
42 #include <sys/rnd.h>
43 #include <compat/sys/rnd.h>
44
45 /*
46 * Convert from rndsource_t to rndsource50_t, for the results from
47 * RNDGETNUM50 and RNDGETNAME50.
48 */
49 static void
50 rndsource_to_rndsource50(rndsource_t *r, rndsource50_t *r50)
51 {
52 memset(r50, 0, sizeof(*r50));
53 strlcpy(r50->name, r->name, sizeof(r50->name));
54 r50->total = r->total;
55 r50->type = r->type;
56 r50->flags = r->flags;
57 }
58
59 /*
60 * COMPAT_50 handling for rnd_ioctl. This is called from rnd_ioctl.
61 *
62 * It also handles the case of (COMPAT_50 && COMPAT_NETBSD32).
63 */
64 int
65 compat_50_rnd_ioctl(struct file *fp, u_long cmd, void *addr)
66 {
67 int ret = 0;
68
69 switch (cmd) {
70
71 case RNDGETSRCNUM50:
72 {
73 rndstat_t rstbuf = {.start = 0};
74 rndstat50_t *rst50 = (rndstat50_t *)addr;
75 int count;
76
77 if (rst50->count > RND_MAXSTATCOUNT50)
78 return EINVAL;
79
80 rstbuf.start = rst50->start;
81 rstbuf.count = rst50->count;
82
83 ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNUM, &rstbuf);
84 if (ret != 0)
85 return ret;
86
87 for (count = 0; count < rst50->count; count++) {
88 rndsource_to_rndsource50(&rstbuf.source[count],
89 &rst50->source[count]);
90 }
91 rst50->count = rstbuf.count;
92
93 break;
94 }
95
96 case RNDGETSRCNAME50:
97 {
98 rndstat_name_t rstnmbuf = {.name[0] = 0};
99 rndstat_name50_t *rstnm50;
100 rstnm50 = (rndstat_name50_t *)addr;
101
102 strlcpy(rstnmbuf.name, rstnm50->name, sizeof(rstnmbuf.name));
103
104 ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNAME, &rstnmbuf);
105 if (ret != 0)
106 return ret;
107
108 rndsource_to_rndsource50(&rstnmbuf.source, &rstnm50->source);
109
110 break;
111 }
112
113 default:
114 return ENOTTY;
115 }
116
117 return ret;
118 }
119