NetBSD osi_crypto: use cprng(9) for random source on NetBSD 6.99/7.x
[openafs.git] / src / afs / NBSD / osi_kmod.c
1 /*
2  * Copyright (c) 2011 Jonathan A. Kollasch
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 COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <afsconfig.h>
28 #include "afs/param.h"
29
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/kernel.h>
34 #if !defined(AFS_NBSD60_ENV)
35 #include <sys/lkm.h>
36 #endif
37 #include <sys/module.h>
38 #include <sys/systm.h>
39 #include <sys/syscall.h>
40 #include <sys/syscallargs.h>
41 #include <sys/syscallvar.h>
42
43 #include "afs/sysincludes.h"    /* Standard vendor system headers */
44 #include "afs/afsincludes.h"    /* Afs-based standard headers */
45
46 extern int afs3_syscall(struct lwp *, const void *, register_t *);
47 extern int afs_xioctl(struct lwp *, const void *, register_t *);
48 extern int Afs_xsetgroups(struct lwp *, const void *, register_t *);
49
50 #if !defined(AFS_NBSD60_ENV)
51 extern int sys_lkmnosys(struct lwp *, const void *, register_t *);
52 #endif
53
54 extern struct vfsops afs_vfsops;
55
56 static const struct sysent openafs_sysent = {
57         sizeof(struct afs_sysargs)/sizeof(register_t),
58         sizeof(struct afs_sysargs),
59         0,
60         afs3_syscall,
61 };
62
63 static struct sysent old_sysent;
64 static sy_call_t *old_setgroups;
65 static sy_call_t *old_ioctl;
66
67 MODULE(MODULE_CLASS_VFS, openafs, NULL);
68
69 #if defined(AFS_NBSD70_ENV)
70 #define SYS_NOSYSCALL sys_nomodule
71 #elif defined(AFS_NBSD60_ENV)
72 #define SYS_NOSYSCALL sys_nosys
73 #else
74 #define SYS_NOSYSCALL sys_lkmnosys
75 #endif
76
77 static int
78 openafs_modcmd(modcmd_t cmd, void *arg)
79 {
80         struct sysent *se;
81         int error;
82
83 #ifndef RUMP
84         se = sysent;
85 #else
86         se = emul_netbsd.e_sysent;
87 #endif
88
89         switch (cmd) {
90         case MODULE_CMD_INIT:
91                 error = vfs_attach(&afs_vfsops);
92                 if (error != 0)
93                         break;
94                 old_sysent = se[AFS_SYSCALL];
95                 old_setgroups = se[SYS_setgroups].sy_call;
96                 old_ioctl = se[SYS_ioctl].sy_call;
97
98 #if defined(RUMP)
99                 if (true) {
100 #else
101                 if (old_sysent.sy_call == SYS_NOSYSCALL) {
102 #endif
103 #if defined(AFS_NBSD60_ENV)
104                         kernconfig_lock();
105 #endif
106                         se[AFS_SYSCALL] = openafs_sysent;
107                         se[SYS_setgroups].sy_call = Afs_xsetgroups;
108                         se[SYS_ioctl].sy_call = afs_xioctl;
109 #if defined(AFS_NBSD60_ENV)
110                         kernconfig_unlock();
111 #endif
112                 } else {
113                         error = EBUSY;
114                 }
115                 if (error != 0)
116                         break;
117                 break;
118         case MODULE_CMD_FINI:
119 #if defined(AFS_NBSD60_ENV)
120                 kernconfig_lock();
121 #endif
122                 se[SYS_ioctl].sy_call = old_ioctl;
123                 se[SYS_setgroups].sy_call = old_setgroups;
124                 se[AFS_SYSCALL] = old_sysent;
125 #if defined(AFS_NBSD60_ENV)
126                 kernconfig_unlock();
127 #endif
128                 error = vfs_detach(&afs_vfsops);
129                 if (error != 0)
130                         break;
131                 break;
132 #if defined(AFS_NBSD70_ENV)
133         case MODULE_CMD_AUTOUNLOAD:
134                 error = EBUSY;
135                 break;
136 #endif
137         default:
138                 error = ENOTTY;
139                 break;
140         }
141
142         return error;
143 }