]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/compat/linux/linux_util.c
MFC r283421:
[FreeBSD/stable/10.git] / sys / compat / linux / linux_util.c
1 /*-
2  * Copyright (c) 1994 Christos Zoulas
3  * Copyright (c) 1995 Frank van der Linden
4  * Copyright (c) 1995 Scott Bartram
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *      from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_compat.h"
36 #include "opt_kdtrace.h"
37
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/fcntl.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/linker_set.h>
45 #include <sys/mutex.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/sdt.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/systm.h>
51 #include <sys/vnode.h>
52
53 #include <machine/stdarg.h>
54
55 #include <compat/linux/linux_util.h>
56
57 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
58
59 const char      linux_emul_path[] = "/compat/linux";
60
61 /*
62  * Search an alternate path before passing pathname arguments on to
63  * system calls. Useful for keeping a separate 'emulation tree'.
64  *
65  * If cflag is set, we check if an attempt can be made to create the
66  * named file, i.e. we check if the directory it should be in exists.
67  */
68 int
69 linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg,
70     char **pbuf, int cflag, int dfd)
71 {
72         int retval;
73
74         retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
75             cflag, dfd);
76
77         return (retval);
78 }
79
80 void
81 linux_msg(const struct thread *td, const char *fmt, ...)
82 {
83         va_list ap;
84         struct proc *p;
85
86         p = td->td_proc;
87         printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
88         va_start(ap, fmt);
89         vprintf(fmt, ap);
90         va_end(ap);
91         printf("\n");
92 }
93
94 struct device_element
95 {
96         TAILQ_ENTRY(device_element) list;
97         struct linux_device_handler entry;
98 };
99
100 static TAILQ_HEAD(, device_element) devices =
101         TAILQ_HEAD_INITIALIZER(devices);
102
103 static struct linux_device_handler null_handler =
104         { "mem", "mem", "null", "null", 1, 3, 1};
105
106 DATA_SET(linux_device_handler_set, null_handler);
107
108 char *
109 linux_driver_get_name_dev(device_t dev)
110 {
111         struct device_element *de;
112         const char *device_name = device_get_name(dev);
113
114         if (device_name == NULL)
115                 return NULL;
116         TAILQ_FOREACH(de, &devices, list) {
117                 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
118                         return (de->entry.linux_driver_name);
119         }
120
121         return (NULL);
122 }
123
124 int
125 linux_driver_get_major_minor(const char *node, int *major, int *minor)
126 {
127         struct device_element *de;
128
129         if (node == NULL || major == NULL || minor == NULL)
130                 return 1;
131
132         if (strlen(node) > strlen("pts/") &&
133             strncmp(node, "pts/", strlen("pts/")) == 0) {
134                 unsigned long devno;
135
136                 /*
137                  * Linux checks major and minors of the slave device
138                  * to make sure it's a pty device, so let's make him
139                  * believe it is.
140                  */
141                 devno = strtoul(node + strlen("pts/"), NULL, 10);
142                 *major = 136 + (devno / 256);
143                 *minor = devno % 256;
144
145                 return (0);
146         }
147
148         TAILQ_FOREACH(de, &devices, list) {
149                 if (strcmp(node, de->entry.bsd_device_name) == 0) {
150                         *major = de->entry.linux_major;
151                         *minor = de->entry.linux_minor;
152                         return (0);
153                 }
154         }
155
156         return (1);
157 }
158
159 char *
160 linux_get_char_devices()
161 {
162         struct device_element *de;
163         char *temp, *string, *last;
164         char formated[256];
165         int current_size = 0, string_size = 1024;
166
167         string = malloc(string_size, M_LINUX, M_WAITOK);
168         string[0] = '\000';
169         last = "";
170         TAILQ_FOREACH(de, &devices, list) {
171                 if (!de->entry.linux_char_device)
172                         continue;
173                 temp = string;
174                 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
175                         last = de->entry.bsd_driver_name;
176
177                         snprintf(formated, sizeof(formated), "%3d %s\n",
178                                  de->entry.linux_major,
179                                  de->entry.linux_device_name);
180                         if (strlen(formated) + current_size
181                             >= string_size) {
182                                 string_size *= 2;
183                                 string = malloc(string_size,
184                                     M_LINUX, M_WAITOK);
185                                 bcopy(temp, string, current_size);
186                                 free(temp, M_LINUX);
187                         }
188                         strcat(string, formated);
189                         current_size = strlen(string);
190                 }
191         }
192
193         return (string);
194 }
195
196 void
197 linux_free_get_char_devices(char *string)
198 {
199
200         free(string, M_LINUX);
201 }
202
203 static int linux_major_starting = 200;
204
205 int
206 linux_device_register_handler(struct linux_device_handler *d)
207 {
208         struct device_element *de;
209
210         if (d == NULL)
211                 return (EINVAL);
212
213         de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
214         if (d->linux_major < 0) {
215                 d->linux_major = linux_major_starting++;
216         }
217         bcopy(d, &de->entry, sizeof(*d));
218
219         /* Add the element to the list, sorted on span. */
220         TAILQ_INSERT_TAIL(&devices, de, list);
221
222         return (0);
223 }
224
225 int
226 linux_device_unregister_handler(struct linux_device_handler *d)
227 {
228         struct device_element *de;
229
230         if (d == NULL)
231                 return (EINVAL);
232
233         TAILQ_FOREACH(de, &devices, list) {
234                 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
235                         TAILQ_REMOVE(&devices, de, list);
236                         free(de, M_LINUX);
237
238                         return (0);
239                 }
240         }
241
242         return (EINVAL);
243 }