]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/random/rwfile.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / random / rwfile.c
1 /*-
2  * Copyright (c) 2013 Mark R V Murray
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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_random.h"
32
33 #ifdef RANDOM_RWFILE
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/namei.h>
40 #include <sys/fcntl.h>
41 #include <sys/vnode.h>
42
43 #include <dev/random/rwfile.h>
44
45 int
46 randomdev_read_file(const char *filename, void *buf, size_t length)
47 {
48         struct nameidata nd;
49         struct thread* td = curthread;
50         int error;
51         ssize_t resid;
52         int flags;
53
54         NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
55         flags = FREAD;
56         error = vn_open(&nd, &flags, 0, NULL);
57         if (error == 0) {
58                 NDFREE(&nd, NDF_ONLY_PNBUF);
59                 if (nd.ni_vp->v_type != VREG)
60                         error = ENOEXEC;
61                 else
62                         error = vn_rdwr(UIO_READ, nd.ni_vp, buf, length, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
63                 VOP_UNLOCK(nd.ni_vp, 0);
64                 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
65         }
66
67         return (error);
68 }
69
70 int
71 randomdev_write_file(const char *filename, void *buf, size_t length)
72 {
73         struct nameidata nd;
74         struct thread* td = curthread;
75         int error;
76         ssize_t resid;
77         int flags;
78
79         NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
80         flags = FWRITE | O_CREAT | O_TRUNC;
81         error = vn_open(&nd, &flags, 0, NULL);
82         if (error == 0) {
83                 NDFREE(&nd, NDF_ONLY_PNBUF);
84                 if (nd.ni_vp->v_type != VREG)
85                         error = ENOEXEC;
86                 else
87                         error = vn_rdwr(UIO_WRITE, nd.ni_vp, buf, length, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
88
89                 VOP_UNLOCK(nd.ni_vp, 0);
90                 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
91         }
92
93         return (error);
94 }
95
96 #endif