linux_util.c (256281) | linux_util.c (293527) |
---|---|
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 --- 16 unchanged lines hidden (view full) --- 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> | 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 --- 16 unchanged lines hidden (view full) --- 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: stable/10/sys/compat/linux/linux_util.c 235063 2012-05-05 19:42:38Z netchild $"); | 33__FBSDID("$FreeBSD: stable/10/sys/compat/linux/linux_util.c 293527 2016-01-09 16:08:22Z dchagin $"); |
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> --- 6 unchanged lines hidden (view full) --- 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> | 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> --- 6 unchanged lines hidden (view full) --- 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#ifdef COMPAT_LINUX32 57#include <machine/../linux32/linux.h> 58#else 59#include <machine/../linux/linux.h> 60#endif | |
61 | 56 |
62#include <compat/linux/linux_dtrace.h> | 57MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures"); |
63 64const char linux_emul_path[] = "/compat/linux"; 65 | 58 59const char linux_emul_path[] = "/compat/linux"; 60 |
66/* DTrace init */ 67LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 68 69/** 70 * DTrace probes in this module. 71 */ 72LIN_SDT_PROBE_DEFINE5(util, linux_emul_convpath, entry, "const char *", 73 "enum uio_seg", "char **", "int", "int"); 74LIN_SDT_PROBE_DEFINE1(util, linux_emul_convpath, return, "int"); 75LIN_SDT_PROBE_DEFINE1(util, linux_msg, entry, "const char *"); 76LIN_SDT_PROBE_DEFINE0(util, linux_msg, return); 77LIN_SDT_PROBE_DEFINE2(util, linux_driver_get_name_dev, entry, "device_t", 78 "const char *"); 79LIN_SDT_PROBE_DEFINE0(util, linux_driver_get_name_dev, nullcall); 80LIN_SDT_PROBE_DEFINE1(util, linux_driver_get_name_dev, return, "char *"); 81LIN_SDT_PROBE_DEFINE3(util, linux_driver_get_major_minor, entry, "char *", 82 "int *", "int *"); 83LIN_SDT_PROBE_DEFINE0(util, linux_driver_get_major_minor, nullcall); 84LIN_SDT_PROBE_DEFINE1(util, linux_driver_get_major_minor, notfound, "char *"); 85LIN_SDT_PROBE_DEFINE3(util, linux_driver_get_major_minor, return, "int", 86 "int", "int"); 87LIN_SDT_PROBE_DEFINE0(util, linux_get_char_devices, entry); 88LIN_SDT_PROBE_DEFINE1(util, linux_get_char_devices, return, "char *"); 89LIN_SDT_PROBE_DEFINE1(util, linux_free_get_char_devices, entry, "char *"); 90LIN_SDT_PROBE_DEFINE0(util, linux_free_get_char_devices, return); 91LIN_SDT_PROBE_DEFINE1(util, linux_device_register_handler, entry, 92 "struct linux_device_handler *"); 93LIN_SDT_PROBE_DEFINE1(util, linux_device_register_handler, return, "int"); 94LIN_SDT_PROBE_DEFINE1(util, linux_device_unregister_handler, entry, 95 "struct linux_device_handler *"); 96LIN_SDT_PROBE_DEFINE1(util, linux_device_unregister_handler, return, "int"); 97 | |
98/* 99 * Search an alternate path before passing pathname arguments on to 100 * system calls. Useful for keeping a separate 'emulation tree'. 101 * 102 * If cflag is set, we check if an attempt can be made to create the 103 * named file, i.e. we check if the directory it should be in exists. 104 */ 105int 106linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg, 107 char **pbuf, int cflag, int dfd) 108{ 109 int retval; 110 | 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 */ 68int 69linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg, 70 char **pbuf, int cflag, int dfd) 71{ 72 int retval; 73 |
111 LIN_SDT_PROBE5(util, linux_emul_convpath, entry, path, pathseg, pbuf, 112 cflag, dfd); 113 | |
114 retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf, 115 cflag, dfd); 116 | 74 retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf, 75 cflag, dfd); 76 |
117 LIN_SDT_PROBE1(util, linux_emul_convpath, return, retval); | |
118 return (retval); 119} 120 121void 122linux_msg(const struct thread *td, const char *fmt, ...) 123{ 124 va_list ap; 125 struct proc *p; 126 | 77 return (retval); 78} 79 80void 81linux_msg(const struct thread *td, const char *fmt, ...) 82{ 83 va_list ap; 84 struct proc *p; 85 |
127 LIN_SDT_PROBE1(util, linux_msg, entry, fmt); 128 | |
129 p = td->td_proc; 130 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm); 131 va_start(ap, fmt); 132 vprintf(fmt, ap); 133 va_end(ap); 134 printf("\n"); | 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"); |
135 136 LIN_SDT_PROBE0(util, linux_msg, return); | |
137} 138 139struct device_element 140{ 141 TAILQ_ENTRY(device_element) list; 142 struct linux_device_handler entry; 143}; 144 --- 6 unchanged lines hidden (view full) --- 151DATA_SET(linux_device_handler_set, null_handler); 152 153char * 154linux_driver_get_name_dev(device_t dev) 155{ 156 struct device_element *de; 157 const char *device_name = device_get_name(dev); 158 | 92} 93 94struct device_element 95{ 96 TAILQ_ENTRY(device_element) list; 97 struct linux_device_handler entry; 98}; 99 --- 6 unchanged lines hidden (view full) --- 106DATA_SET(linux_device_handler_set, null_handler); 107 108char * 109linux_driver_get_name_dev(device_t dev) 110{ 111 struct device_element *de; 112 const char *device_name = device_get_name(dev); 113 |
159 LIN_SDT_PROBE2(util, linux_driver_get_name_dev, entry, dev, 160 device_name); 161 162 if (device_name == NULL) { 163 LIN_SDT_PROBE0(util, linux_driver_get_name_dev, nullcall); 164 LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, NULL); | 114 if (device_name == NULL) |
165 return NULL; | 115 return NULL; |
166 } | |
167 TAILQ_FOREACH(de, &devices, list) { | 116 TAILQ_FOREACH(de, &devices, list) { |
168 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) { 169 LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, 170 de->entry.linux_driver_name); | 117 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) |
171 return (de->entry.linux_driver_name); | 118 return (de->entry.linux_driver_name); |
172 } | |
173 } 174 | 119 } 120 |
175 LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, NULL); 176 return NULL; | 121 return (NULL); |
177} 178 179int 180linux_driver_get_major_minor(const char *node, int *major, int *minor) 181{ 182 struct device_element *de; 183 | 122} 123 124int 125linux_driver_get_major_minor(const char *node, int *major, int *minor) 126{ 127 struct device_element *de; 128 |
184 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, entry, node, major, 185 minor); 186 187 if (node == NULL || major == NULL || minor == NULL) { 188 LIN_SDT_PROBE0(util, linux_driver_get_major_minor, nullcall); 189 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 1, 190 0, 0); | 129 if (node == NULL || major == NULL || minor == NULL) |
191 return 1; | 130 return 1; |
192 } | |
193 194 if (strlen(node) > strlen("pts/") && 195 strncmp(node, "pts/", strlen("pts/")) == 0) { 196 unsigned long devno; 197 198 /* 199 * Linux checks major and minors of the slave device 200 * to make sure it's a pty device, so let's make him 201 * believe it is. 202 */ 203 devno = strtoul(node + strlen("pts/"), NULL, 10); 204 *major = 136 + (devno / 256); 205 *minor = devno % 256; 206 | 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 |
207 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 0, 208 *major, *minor); 209 return 0; | 145 return (0); |
210 } 211 212 TAILQ_FOREACH(de, &devices, list) { 213 if (strcmp(node, de->entry.bsd_device_name) == 0) { 214 *major = de->entry.linux_major; 215 *minor = de->entry.linux_minor; | 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; |
216 217 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, 218 return, 0, *major, *minor); 219 return 0; | 152 return (0); |
220 } 221 } 222 | 153 } 154 } 155 |
223 LIN_SDT_PROBE1(util, linux_driver_get_major_minor, notfound, node); 224 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 1, 0, 0); 225 return 1; | 156 return (1); |
226} 227 228char * 229linux_get_char_devices() 230{ 231 struct device_element *de; 232 char *temp, *string, *last; 233 char formated[256]; 234 int current_size = 0, string_size = 1024; 235 | 157} 158 159char * 160linux_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 |
236 LIN_SDT_PROBE0(util, linux_get_char_devices, entry); 237 | |
238 string = malloc(string_size, M_LINUX, M_WAITOK); 239 string[0] = '\000'; 240 last = ""; 241 TAILQ_FOREACH(de, &devices, list) { 242 if (!de->entry.linux_char_device) 243 continue; 244 temp = string; 245 if (strcmp(last, de->entry.bsd_driver_name) != 0) { --- 10 unchanged lines hidden (view full) --- 256 bcopy(temp, string, current_size); 257 free(temp, M_LINUX); 258 } 259 strcat(string, formated); 260 current_size = strlen(string); 261 } 262 } 263 | 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) { --- 10 unchanged lines hidden (view full) --- 185 bcopy(temp, string, current_size); 186 free(temp, M_LINUX); 187 } 188 strcat(string, formated); 189 current_size = strlen(string); 190 } 191 } 192 |
264 LIN_SDT_PROBE1(util, linux_get_char_devices, return, string); 265 return string; | 193 return (string); |
266} 267 268void 269linux_free_get_char_devices(char *string) 270{ 271 | 194} 195 196void 197linux_free_get_char_devices(char *string) 198{ 199 |
272 LIN_SDT_PROBE1(util, linux_get_char_devices, entry, string); 273 | |
274 free(string, M_LINUX); | 200 free(string, M_LINUX); |
275 276 LIN_SDT_PROBE0(util, linux_get_char_devices, return); | |
277} 278 279static int linux_major_starting = 200; 280 281int 282linux_device_register_handler(struct linux_device_handler *d) 283{ 284 struct device_element *de; 285 | 201} 202 203static int linux_major_starting = 200; 204 205int 206linux_device_register_handler(struct linux_device_handler *d) 207{ 208 struct device_element *de; 209 |
286 LIN_SDT_PROBE1(util, linux_device_register_handler, entry, d); 287 288 if (d == NULL) { 289 LIN_SDT_PROBE1(util, linux_device_register_handler, return, 290 EINVAL); | 210 if (d == NULL) |
291 return (EINVAL); | 211 return (EINVAL); |
292 } | |
293 294 de = malloc(sizeof(*de), M_LINUX, M_WAITOK); 295 if (d->linux_major < 0) { 296 d->linux_major = linux_major_starting++; 297 } 298 bcopy(d, &de->entry, sizeof(*d)); 299 300 /* Add the element to the list, sorted on span. */ 301 TAILQ_INSERT_TAIL(&devices, de, list); 302 | 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 |
303 LIN_SDT_PROBE1(util, linux_device_register_handler, return, 0); | |
304 return (0); 305} 306 307int 308linux_device_unregister_handler(struct linux_device_handler *d) 309{ 310 struct device_element *de; 311 | 222 return (0); 223} 224 225int 226linux_device_unregister_handler(struct linux_device_handler *d) 227{ 228 struct device_element *de; 229 |
312 LIN_SDT_PROBE1(util, linux_device_unregister_handler, entry, d); 313 314 if (d == NULL) { 315 LIN_SDT_PROBE1(util, linux_device_unregister_handler, return, 316 EINVAL); | 230 if (d == NULL) |
317 return (EINVAL); | 231 return (EINVAL); |
318 } | |
319 320 TAILQ_FOREACH(de, &devices, list) { 321 if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 322 TAILQ_REMOVE(&devices, de, list); 323 free(de, M_LINUX); 324 | 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 |
325 LIN_SDT_PROBE1(util, linux_device_unregister_handler, 326 return, 0); | |
327 return (0); 328 } 329 } 330 | 238 return (0); 239 } 240 } 241 |
331 LIN_SDT_PROBE1(util, linux_device_unregister_handler, return, EINVAL); | |
332 return (EINVAL); 333} | 242 return (EINVAL); 243} |