xref: /netbsd-src/sys/miscfs/procfs/procfs_linux.c (revision 37d37f813e1a080c671271add25ab1243c76cc1f)
1 /*      $NetBSD: procfs_linux.c,v 1.69 2014/07/12 09:58:39 njoly Exp $      */
2 
3 /*
4  * Copyright (c) 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Frank van der Linden for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.69 2014/07/12 09:58:39 njoly Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/exec.h>
48 #include <sys/resource.h>
49 #include <sys/resourcevar.h>
50 #include <sys/signal.h>
51 #include <sys/signalvar.h>
52 #include <sys/tty.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/conf.h>
56 #include <sys/sysctl.h>
57 #include <sys/kauth.h>
58 #include <sys/filedesc.h>
59 
60 #include <miscfs/procfs/procfs.h>
61 
62 #include <compat/linux/common/linux_exec.h>
63 #include <compat/linux32/common/linux32_sysctl.h>
64 
65 #include <uvm/uvm_extern.h>
66 #include <uvm/uvm.h>
67 
68 extern struct devsw_conv *devsw_conv;
69 extern int max_devsw_convs;
70 
71 #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
72 #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
73 
74 #define LBFSZ (8 * 1024)
75 
76 static void
77 get_proc_size_info(struct lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack)
78 {
79 	struct proc *p = l->l_proc;
80 	struct vmspace *vm;
81 	struct vm_map *map;
82 	struct vm_map_entry *entry;
83 
84 	*stext = 0;
85 	*etext = 0;
86 	*sstack = 0;
87 
88 	proc_vmspace_getref(p, &vm);
89 	map = &vm->vm_map;
90 	vm_map_lock_read(map);
91 
92 	for (entry = map->header.next; entry != &map->header;
93 	    entry = entry->next) {
94 		if (UVM_ET_ISSUBMAP(entry))
95 			continue;
96 		/* assume text is the first entry */
97 		if (*stext == *etext) {
98 			*stext = entry->start;
99 			*etext = entry->end;
100 			break;
101 		}
102 	}
103 #if defined(LINUX_USRSTACK32) && defined(USRSTACK32)
104 	if (strcmp(p->p_emul->e_name, "linux32") == 0 &&
105 	    LINUX_USRSTACK32 < USRSTACK32)
106 		*sstack = (unsigned long)LINUX_USRSTACK32;
107 	else
108 #endif
109 #ifdef LINUX_USRSTACK
110 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
111 	    LINUX_USRSTACK < USRSTACK)
112 		*sstack = (unsigned long)LINUX_USRSTACK;
113 	else
114 #endif
115 #ifdef	USRSTACK32
116 	if (strstr(p->p_emul->e_name, "32") != NULL)
117 		*sstack = (unsigned long)USRSTACK32;
118 	else
119 #endif
120 		*sstack = (unsigned long)USRSTACK;
121 
122 	/*
123 	 * jdk 1.6 compares low <= addr && addr < high
124 	 * if we put addr == high, then the test fails
125 	 * so eat one page.
126 	 */
127 	*sstack -= PAGE_SIZE;
128 
129 	vm_map_unlock_read(map);
130 	uvmspace_free(vm);
131 }
132 
133 /*
134  * Linux compatible /proc/meminfo. Only active when the -o linux
135  * mountflag is used.
136  */
137 int
138 procfs_domeminfo(struct lwp *curl, struct proc *p,
139     struct pfsnode *pfs, struct uio *uio)
140 {
141 	char *bf;
142 	int len;
143 	int error = 0;
144 
145 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
146 
147 	len = snprintf(bf, LBFSZ,
148 		"        total:    used:    free:  shared: buffers: cached:\n"
149 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
150 		"Swap: %8lu %8lu %8lu\n"
151 		"MemTotal:  %8lu kB\n"
152 		"MemFree:   %8lu kB\n"
153 		"MemShared: %8lu kB\n"
154 		"Buffers:   %8lu kB\n"
155 		"Cached:    %8lu kB\n"
156 		"SwapTotal: %8lu kB\n"
157 		"SwapFree:  %8lu kB\n",
158 		PGTOB(uvmexp.npages),
159 		PGTOB(uvmexp.npages - uvmexp.free),
160 		PGTOB(uvmexp.free),
161 		0L,
162 		PGTOB(uvmexp.filepages),
163 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
164 		PGTOB(uvmexp.swpages),
165 		PGTOB(uvmexp.swpginuse),
166 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
167 		PGTOKB(uvmexp.npages),
168 		PGTOKB(uvmexp.free),
169 		0L,
170 		PGTOKB(uvmexp.filepages),
171 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
172 		PGTOKB(uvmexp.swpages),
173 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
174 
175 	if (len == 0)
176 		goto out;
177 
178 	error = uiomove_frombuf(bf, len, uio);
179 out:
180 	free(bf, M_TEMP);
181 	return error;
182 }
183 
184 /*
185  * Linux compatible /proc/devices. Only active when the -o linux
186  * mountflag is used.
187  */
188 int
189 procfs_dodevices(struct lwp *curl, struct proc *p,
190     struct pfsnode *pfs, struct uio *uio)
191 {
192 	char *bf;
193 	int offset = 0;
194 	int i, error = ENAMETOOLONG;
195 
196 	/* XXX elad - may need filtering. */
197 
198 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
199 
200 	offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
201 	if (offset >= LBFSZ)
202 		goto out;
203 
204 	mutex_enter(&device_lock);
205 	for (i = 0; i < max_devsw_convs; i++) {
206 		if ((devsw_conv[i].d_name == NULL) ||
207 		    (devsw_conv[i].d_cmajor == -1))
208 			continue;
209 
210 		offset += snprintf(&bf[offset], LBFSZ - offset,
211 		    "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
212 		if (offset >= LBFSZ) {
213 			mutex_exit(&device_lock);
214 			goto out;
215 		}
216 	}
217 
218 	offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
219 	if (offset >= LBFSZ) {
220 		mutex_exit(&device_lock);
221 		goto out;
222 	}
223 
224 	for (i = 0; i < max_devsw_convs; i++) {
225 		if ((devsw_conv[i].d_name == NULL) ||
226 		    (devsw_conv[i].d_bmajor == -1))
227 			continue;
228 
229 		offset += snprintf(&bf[offset], LBFSZ - offset,
230 		    "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
231 		if (offset >= LBFSZ) {
232 			mutex_exit(&device_lock);
233 			goto out;
234 		}
235 	}
236 	mutex_exit(&device_lock);
237 
238 	error = uiomove_frombuf(bf, offset, uio);
239 out:
240 	free(bf, M_TEMP);
241 	return error;
242 }
243 
244 /*
245  * Linux compatible /proc/stat. Only active when the -o linux
246  * mountflag is used.
247  */
248 int
249 procfs_docpustat(struct lwp *curl, struct proc *p,
250     struct pfsnode *pfs, struct uio *uio)
251 {
252 	char		*bf;
253 	int	 	 error;
254 	int	 	 len;
255 #if defined(MULTIPROCESSOR)
256         struct cpu_info *ci;
257         CPU_INFO_ITERATOR cii;
258 #endif
259 	int	 	 i;
260 	uint64_t	nintr;
261 	uint64_t	nswtch;
262 
263 	error = ENAMETOOLONG;
264 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
265 
266 	len = snprintf(bf, LBFSZ,
267 		"cpu %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
268 		curcpu()->ci_schedstate.spc_cp_time[CP_USER],
269 		curcpu()->ci_schedstate.spc_cp_time[CP_NICE],
270 		curcpu()->ci_schedstate.spc_cp_time[CP_SYS] /*+ [CP_INTR]*/,
271 		curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]);
272 	if (len == 0)
273 		goto out;
274 
275 #if defined(MULTIPROCESSOR)
276 #define ALLCPUS	CPU_INFO_FOREACH(cii, ci)
277 #define CPUNAME	ci
278 #else
279 #define ALLCPUS	; i < 1 ;
280 #define CPUNAME	curcpu()
281 #endif
282 
283 	i = 0;
284 	nintr = 0;
285 	nswtch = 0;
286 	for (ALLCPUS) {
287 		len += snprintf(&bf[len], LBFSZ - len,
288 			"cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
289 			"\n", i,
290 			CPUNAME->ci_schedstate.spc_cp_time[CP_USER],
291 			CPUNAME->ci_schedstate.spc_cp_time[CP_NICE],
292 			CPUNAME->ci_schedstate.spc_cp_time[CP_SYS],
293 			CPUNAME->ci_schedstate.spc_cp_time[CP_IDLE]);
294 		if (len >= LBFSZ)
295 			goto out;
296 		i += 1;
297 		nintr += CPUNAME->ci_data.cpu_nintr;
298 		nswtch += CPUNAME->ci_data.cpu_nswtch;
299 	}
300 
301 	len += snprintf(&bf[len], LBFSZ - len,
302 			"disk 0 0 0 0\n"
303 			"page %u %u\n"
304 			"swap %u %u\n"
305 			"intr %"PRIu64"\n"
306 			"ctxt %"PRIu64"\n"
307 			"btime %"PRId64"\n",
308 			uvmexp.pageins, uvmexp.pdpageouts,
309 			uvmexp.pgswapin, uvmexp.pgswapout,
310 			nintr,
311 			nswtch,
312 			boottime.tv_sec);
313 	if (len >= LBFSZ)
314 		goto out;
315 
316 	error = uiomove_frombuf(bf, len, uio);
317 out:
318 	free(bf, M_TEMP);
319 	return error;
320 }
321 
322 /*
323  * Linux compatible /proc/loadavg. Only active when the -o linux
324  * mountflag is used.
325  */
326 int
327 procfs_doloadavg(struct lwp *curl, struct proc *p,
328     struct pfsnode *pfs, struct uio *uio)
329 {
330 	char	*bf;
331 	int 	 error;
332 	int 	 len;
333 
334 	error = ENAMETOOLONG;
335 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
336 
337 	averunnable.fscale = FSCALE;
338 	len = snprintf(bf, LBFSZ,
339 	        "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
340 		(int)(averunnable.ldavg[0] / averunnable.fscale),
341 		(int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
342 		(int)(averunnable.ldavg[1] / averunnable.fscale),
343 		(int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
344 		(int)(averunnable.ldavg[2] / averunnable.fscale),
345 		(int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
346 		1,		/* number of ONPROC processes */
347 		nprocs,
348 		30000);		/* last pid */
349 	if (len == 0)
350 		goto out;
351 
352 	error = uiomove_frombuf(bf, len, uio);
353 out:
354 	free(bf, M_TEMP);
355 	return error;
356 }
357 
358 /*
359  * Linux compatible /proc/<pid>/statm. Only active when the -o linux
360  * mountflag is used.
361  */
362 int
363 procfs_do_pid_statm(struct lwp *curl, struct lwp *l,
364     struct pfsnode *pfs, struct uio *uio)
365 {
366 	struct vmspace	*vm;
367 	struct proc	*p = l->l_proc;
368 	struct rusage	*ru = &p->p_stats->p_ru;
369 	char		*bf;
370 	int	 	 error;
371 	int	 	 len;
372 
373 	error = ENAMETOOLONG;
374 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
375 
376 	/* XXX - we use values from vmspace, since dsl says that ru figures
377 	   are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */
378 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
379 		goto out;
380 	}
381 
382 	len = snprintf(bf, LBFSZ,
383 	        "%lu %lu %lu %lu %lu %lu %lu\n",
384 		(unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */
385 		(unsigned long)(vm->vm_rssize),	/* resident */
386 		(unsigned long)(ru->ru_ixrss),	/* shared */
387 		(unsigned long)(vm->vm_tsize),	/* text size in pages */
388 		(unsigned long)(vm->vm_dsize),	/* data size in pages */
389 		(unsigned long)(vm->vm_ssize),	/* stack size in pages */
390 		(unsigned long) 0);
391 
392 	uvmspace_free(vm);
393 
394 	if (len == 0)
395 		goto out;
396 
397 	error = uiomove_frombuf(bf, len, uio);
398 out:
399 	free(bf, M_TEMP);
400 	return error;
401 }
402 
403 #define UTIME2TICKS(s,u)	(((uint64_t)(s) * 1000000 + (u)) / 10000)
404 
405 /*
406  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
407  * mountflag is used.
408  */
409 int
410 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
411     struct pfsnode *pfs, struct uio *uio)
412 {
413 	char *bf;
414 	struct proc *p = l->l_proc;
415 	int len;
416 	struct rusage *cru = &p->p_stats->p_cru;
417 	unsigned long stext = 0, etext = 0, sstack = 0;
418 	struct timeval rt;
419 	struct vmspace	*vm;
420 	struct kinfo_proc2 ki;
421 	int error = 0;
422 
423 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
424 
425 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
426 		goto out;
427 	}
428 
429 	get_proc_size_info(l, &stext, &etext, &sstack);
430 
431 	mutex_enter(proc_lock);
432 	mutex_enter(p->p_lock);
433 
434 	fill_kproc2(p, &ki, false);
435 	calcru(p, NULL, NULL, NULL, &rt);
436 
437 	len = snprintf(bf, LBFSZ,
438 	    "%d (%s) %c %d %d %d %u %d "
439 	    "%u "
440 	    "%"PRIu64" %lu %"PRIu64" %lu %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" "
441 	    "%d %d %"PRIu64" "
442 	    "%lld %"PRIu64" %"PRId64" %lu %"PRIu64" "
443 	    "%lu %lu %lu "
444 	    "%u %u "
445 	    "%u %u %u %u "
446 	    "%"PRIu64" %"PRIu64" %"PRIu64" %d %"PRIu64"\n",
447 
448 	    ki.p_pid,						/* 1 pid */
449 	    ki.p_comm,						/* 2 tcomm */
450 	    "0RRSTZXR8"[(ki.p_stat > 8) ? 0 : (int)ki.p_stat],	/* 3 state */
451 	    ki.p_ppid,						/* 4 ppid */
452 	    ki.p__pgid,						/* 5 pgrp */
453 	    ki.p_sid,						/* 6 sid */
454 	    (ki.p_tdev != (uint32_t)NODEV) ? ki.p_tdev : 0,	/* 7 tty_nr */
455 	    ki.p_tpgid,						/* 8 tty_pgrp */
456 
457 	    ki.p_flag,						/* 9 flags */
458 
459 	    ki.p_uru_minflt,					/* 10 min_flt */
460 	    cru->ru_minflt,
461 	    ki.p_uru_majflt,					/* 12 maj_flt */
462 	    cru->ru_majflt,
463 	    UTIME2TICKS(ki.p_uutime_sec, ki.p_uutime_usec),	/* 14 utime */
464 	    UTIME2TICKS(ki.p_ustime_sec, ki.p_ustime_usec),	/* 15 stime */
465 	    UTIME2TICKS(cru->ru_utime.tv_sec, cru->ru_utime.tv_usec), /* 16 cutime */
466 	    UTIME2TICKS(cru->ru_stime.tv_sec, cru->ru_stime.tv_usec), /* 17 cstime */
467 
468 	    ki.p_priority,				/* XXX: 18 priority */
469 	    ki.p_nice - NZERO,				/* 19 nice */
470 	    ki.p_nlwps,					/* 20 num_threads */
471 
472 	    (long long)rt.tv_sec,
473 	    UTIME2TICKS(ki.p_ustart_sec, ki.p_ustart_usec), /* 22 start_time */
474 	    ki.p_vm_msize,				/* 23 vsize */
475 	    PGTOKB(ki.p_vm_rssize),			/* 24 rss */
476 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,		/* 25 rsslim */
477 
478 	    stext,					/* 26 start_code */
479 	    etext,					/* 27 end_code */
480 	    sstack,					/* 28 start_stack */
481 
482 	    0,						/* XXX: 29 esp */
483 	    0,						/* XXX: 30 eip */
484 
485 	    ki.p_siglist.__bits[0],			/* XXX: 31 pending */
486 	    0,						/* XXX: 32 blocked */
487 	    ki.p_sigignore.__bits[0],		/* 33 sigign */
488 	    ki.p_sigcatch.__bits[0],		/* 34 sigcatch */
489 
490 	    ki.p_wchan,					/* 35 wchan */
491 	    ki.p_uru_nvcsw,
492 	    ki.p_uru_nivcsw,
493 	    ki.p_exitsig,				/* 38 exit_signal */
494 	    ki.p_cpuid);				/* 39 task_cpu */
495 
496 	mutex_exit(p->p_lock);
497 	mutex_exit(proc_lock);
498 
499 	uvmspace_free(vm);
500 
501 	if (len == 0)
502 		goto out;
503 
504 	error = uiomove_frombuf(bf, len, uio);
505 out:
506 	free(bf, M_TEMP);
507 	return error;
508 }
509 
510 int
511 procfs_docpuinfo(struct lwp *curl, struct proc *p,
512     struct pfsnode *pfs, struct uio *uio)
513 {
514 	size_t len = LBFSZ;
515 	char *bf = NULL;
516 	int error;
517 
518 	do {
519 		if (bf)
520 			free(bf, M_TEMP);
521 		bf = malloc(len, M_TEMP, M_WAITOK);
522 	} while (procfs_getcpuinfstr(bf, &len) < 0);
523 
524 	if (len == 0) {
525 		error = 0;
526 		goto done;
527 	}
528 
529 	error = uiomove_frombuf(bf, len, uio);
530 done:
531 	free(bf, M_TEMP);
532 	return error;
533 }
534 
535 int
536 procfs_douptime(struct lwp *curl, struct proc *p,
537     struct pfsnode *pfs, struct uio *uio)
538 {
539 	char *bf;
540 	int len;
541 	struct timeval runtime;
542 	u_int64_t idle;
543 	int error = 0;
544 
545 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
546 
547 	microuptime(&runtime);
548 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
549 	len = snprintf(bf, LBFSZ,
550 	    "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n",
551 	    (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000,
552 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
553 
554 	if (len == 0)
555 		goto out;
556 
557 	error = uiomove_frombuf(bf, len, uio);
558 out:
559 	free(bf, M_TEMP);
560 	return error;
561 }
562 
563 static int
564 procfs_format_sfs(char **mtab, size_t *mlen, char *buf, size_t blen,
565     const struct statvfs *sfs, struct lwp *curl, int suser)
566 {
567 	const char *fsname;
568 
569 	/* Linux uses different names for some filesystems */
570 	fsname = sfs->f_fstypename;
571 	if (strcmp(fsname, "procfs") == 0)
572 		fsname = "proc";
573 	else if (strcmp(fsname, "ext2fs") == 0)
574 		fsname = "ext2";
575 
576 	blen = snprintf(buf, blen, "%s %s %s %s%s%s%s%s%s 0 0\n",
577 	    sfs->f_mntfromname, sfs->f_mntonname, fsname,
578 	    (sfs->f_flag & ST_RDONLY) ? "ro" : "rw",
579 	    (sfs->f_flag & ST_NOSUID) ? ",nosuid" : "",
580 	    (sfs->f_flag & ST_NOEXEC) ? ",noexec" : "",
581 	    (sfs->f_flag & ST_NODEV) ? ",nodev" : "",
582 	    (sfs->f_flag & ST_SYNCHRONOUS) ? ",sync" : "",
583 	    (sfs->f_flag & ST_NOATIME) ? ",noatime" : "");
584 
585 	*mtab = realloc(*mtab, *mlen + blen, M_TEMP, M_WAITOK);
586 	memcpy(*mtab + *mlen, buf, blen);
587 	*mlen += blen;
588 	return sfs->f_mntonname[0] == '/' && sfs->f_mntonname[1] == '\0';
589 }
590 
591 int
592 procfs_domounts(struct lwp *curl, struct proc *p,
593     struct pfsnode *pfs, struct uio *uio)
594 {
595 	char *bf, *mtab = NULL;
596 	size_t mtabsz = 0;
597 	struct mount *mp, *nmp;
598 	int error = 0, root = 0;
599 	struct cwdinfo *cwdi = curl->l_proc->p_cwdi;
600 
601 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
602 
603 	mutex_enter(&mountlist_lock);
604 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
605 		struct statvfs sfs;
606 
607 		if (vfs_busy(mp, &nmp))
608 			continue;
609 
610 		if ((error = dostatvfs(mp, &sfs, curl, MNT_WAIT, 0)) == 0)
611 			root |= procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
612 			    &sfs, curl, 0);
613 
614 		vfs_unbusy(mp, false, &nmp);
615 	}
616 	mutex_exit(&mountlist_lock);
617 
618 	/*
619 	 * If we are inside a chroot that is not itself a mount point,
620 	 * fake a root entry.
621 	 */
622 	if (!root && cwdi->cwdi_rdir)
623 		(void)procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
624 		    &cwdi->cwdi_rdir->v_mount->mnt_stat, curl, 1);
625 
626 	free(bf, M_TEMP);
627 
628 	if (mtabsz > 0) {
629 		error = uiomove_frombuf(mtab, mtabsz, uio);
630 		free(mtab, M_TEMP);
631 	}
632 
633 	return error;
634 }
635 
636 /*
637  * Linux compatible /proc/version. Only active when the -o linux
638  * mountflag is used.
639  */
640 int
641 procfs_doversion(struct lwp *curl, struct proc *p,
642     struct pfsnode *pfs, struct uio *uio)
643 {
644 	char *bf;
645 	char lostype[20], losrelease[20], lversion[80];
646 	const char *postype, *posrelease, *pversion;
647 	const char *emulname = curlwp->l_proc->p_emul->e_name;
648 	int len;
649 	int error = 0;
650 	int nm[4];
651 	size_t buflen;
652 
653 	CTASSERT(EMUL_LINUX_KERN_OSTYPE == EMUL_LINUX32_KERN_OSTYPE);
654 	CTASSERT(EMUL_LINUX_KERN_OSRELEASE == EMUL_LINUX32_KERN_OSRELEASE);
655 	CTASSERT(EMUL_LINUX_KERN_VERSION == EMUL_LINUX32_KERN_VERSION);
656 
657 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
658 
659 	sysctl_lock(false);
660 
661 	if (strncmp(emulname, "linux", 5) == 0) {
662 		/*
663 		 * Lookup the emulation ostype, osrelease, and version.
664 		 * Since compat_linux and compat_linux32 can be built as
665 		 * modules, we use sysctl to obtain the values instead of
666 		 * using the symbols directly.
667 		 */
668 
669 		if (strcmp(emulname, "linux32") == 0) {
670 			nm[0] = CTL_EMUL;
671 			nm[1] = EMUL_LINUX32;
672 			nm[2] = EMUL_LINUX32_KERN;
673 		} else {
674 			nm[0] = CTL_EMUL;
675 			nm[1] = EMUL_LINUX;
676 			nm[2] = EMUL_LINUX_KERN;
677 		}
678 
679 		nm[3] = EMUL_LINUX_KERN_OSTYPE;
680 		buflen = sizeof(lostype);
681 		error = sysctl_dispatch(nm, __arraycount(nm),
682 		    lostype, &buflen,
683 		    NULL, 0, NULL, NULL, NULL);
684 		if (error)
685 			goto out;
686 
687 		nm[3] = EMUL_LINUX_KERN_OSRELEASE;
688 		buflen = sizeof(losrelease);
689 		error = sysctl_dispatch(nm, __arraycount(nm),
690 		    losrelease, &buflen,
691 		    NULL, 0, NULL, NULL, NULL);
692 		if (error)
693 			goto out;
694 
695 		nm[3] = EMUL_LINUX_KERN_VERSION;
696 		buflen = sizeof(lversion);
697 		error = sysctl_dispatch(nm, __arraycount(nm),
698 		    lversion, &buflen,
699 		    NULL, 0, NULL, NULL, NULL);
700 		if (error)
701 			goto out;
702 
703 		postype = lostype;
704 		posrelease = losrelease;
705 		pversion = lversion;
706 	} else {
707 		postype = ostype;
708 		posrelease = osrelease;
709 		strlcpy(lversion, version, sizeof(lversion));
710 		if (strchr(lversion, '\n'))
711 			*strchr(lversion, '\n') = '\0';
712 		pversion = lversion;
713 	}
714 
715 	len = snprintf(bf, LBFSZ,
716 		"%s version %s (%s@localhost) (gcc version %s) %s\n",
717 		postype, posrelease, emulname,
718 #ifdef __VERSION__
719 		__VERSION__,
720 #else
721 		"unknown",
722 #endif
723 		pversion);
724 
725 	if (len == 0)
726 		goto out;
727 
728 	error = uiomove_frombuf(bf, len, uio);
729 out:
730 	free(bf, M_TEMP);
731 	sysctl_unlock();
732 	return error;
733 }
734