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