]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/kmem.c
Import DTS files for riscv from Linux 5.4
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / kmem.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  */
8 /*
9  * kmemcpy() - copies n bytes from kernel memory into user buffer.
10  * returns 0 on success, -1 on error.
11  */
12
13 #include <stdio.h>
14 #include <sys/param.h>
15 #include <sys/types.h>
16 #include <sys/uio.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <fcntl.h>
20 #include <sys/file.h>
21 #include <kvm.h>
22 #include <fcntl.h>
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <net/if.h>
30
31 #include "kmem.h"
32
33 #ifndef __STDC__
34 # define        const
35 #endif
36
37 #if !defined(lint)
38 static const char sccsid[] = "@(#)kmem.c        1.4 1/12/96 (C) 1992 Darren Reed";
39 static const char rcsid[] = "@(#)$Id$";
40 #endif
41
42
43
44 static  kvm_t   *kvm_f = NULL;
45
46
47 int     openkmem(kern, core)
48         char    *kern, *core;
49 {
50         kvm_f = kvm_open(kern, core, NULL, O_RDONLY, NULL);
51         if (kvm_f == NULL)
52             {
53                 perror("openkmem:open");
54                 return -1;
55             }
56         return kvm_f != NULL;
57 }
58
59 int     kmemcpy(buf, pos, n)
60         register char   *buf;
61         long    pos;
62         register int    n;
63 {
64         register int    r;
65
66         if (!n)
67                 return 0;
68
69         if (kvm_f == NULL)
70                 if (openkmem(NULL, NULL) == -1)
71                         return -1;
72
73         while ((r = kvm_read(kvm_f, pos, buf, n)) < n)
74                 if (r <= 0)
75                     {
76                         fprintf(stderr, "pos=0x%lx ", (u_long)pos);
77                         perror("kmemcpy:read");
78                         return -1;
79                     }
80                 else
81                     {
82                         buf += r;
83                         pos += r;
84                         n -= r;
85                     }
86         return 0;
87 }
88
89 int     kstrncpy(buf, pos, n)
90         register char   *buf;
91         long    pos;
92         register int    n;
93 {
94         register int    r;
95
96         if (!n)
97                 return 0;
98
99         if (kvm_f == NULL)
100                 if (openkmem(NULL, NULL) == -1)
101                         return -1;
102
103         while (n > 0)
104             {
105                 r = kvm_read(kvm_f, pos, buf, 1);
106                 if (r <= 0)
107                     {
108                         fprintf(stderr, "pos=0x%lx ", (u_long)pos);
109                         perror("kmemcpy:read");
110                         return -1;
111                     }
112                 else
113                     {
114                         if (*buf == '\0')
115                                 break;
116                         buf++;
117                         pos++;
118                         n--;
119                     }
120             }
121         return 0;
122 }