xref: /freebsd/sys/compat/linprocfs/linprocfs.c (revision 5db31f1b47b586a5091eb41fc957236f28b78dba)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2000 Dag-Erling Smørgrav
5  * Copyright (c) 1999 Pierre Beyssac
6  * Copyright (c) 1993 Jan-Simon Pendry
7  * Copyright (c) 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41 
42 #include "opt_inet.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/blist.h>
47 #include <sys/conf.h>
48 #include <sys/exec.h>
49 #include <sys/fcntl.h>
50 #include <sys/filedesc.h>
51 #include <sys/jail.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/linker.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/msg.h>
58 #include <sys/mutex.h>
59 #include <sys/namei.h>
60 #include <sys/proc.h>
61 #include <sys/ptrace.h>
62 #include <sys/queue.h>
63 #include <sys/resourcevar.h>
64 #include <sys/resource.h>
65 #include <sys/sbuf.h>
66 #include <sys/sem.h>
67 #include <sys/shm.h>
68 #include <sys/smp.h>
69 #include <sys/socket.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
73 #include <sys/time.h>
74 #include <sys/tty.h>
75 #include <sys/user.h>
76 #include <sys/uuid.h>
77 #include <sys/vmmeter.h>
78 #include <sys/vnode.h>
79 #include <sys/bus.h>
80 #include <sys/uio.h>
81 
82 #include <net/if.h>
83 #include <net/if_var.h>
84 #include <net/if_types.h>
85 
86 #include <net/route.h>
87 #include <net/route/nhop.h>
88 #include <net/route/route_ctl.h>
89 
90 #include <vm/vm.h>
91 #include <vm/vm_extern.h>
92 #include <vm/pmap.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/swap_pager.h>
97 
98 #include <machine/clock.h>
99 
100 #include <geom/geom.h>
101 #include <geom/geom_int.h>
102 
103 #if defined(__i386__) || defined(__amd64__)
104 #include <machine/cputypes.h>
105 #include <machine/md_var.h>
106 #endif /* __i386__ || __amd64__ */
107 
108 #include <compat/linux/linux.h>
109 #include <compat/linux/linux_common.h>
110 #include <compat/linux/linux_emul.h>
111 #include <compat/linux/linux_mib.h>
112 #include <compat/linux/linux_misc.h>
113 #include <compat/linux/linux_util.h>
114 #include <fs/pseudofs/pseudofs.h>
115 #include <fs/procfs/procfs.h>
116 
117 /*
118  * Various conversion macros
119  */
120 #define T2J(x) ((long)(((x) * 100ULL) / (stathz ? stathz : hz)))	/* ticks to jiffies */
121 #define T2CS(x) ((unsigned long)(((x) * 100ULL) / (stathz ? stathz : hz)))	/* ticks to centiseconds */
122 #define T2S(x) ((x) / (stathz ? stathz : hz))		/* ticks to seconds */
123 #define B2K(x) ((x) >> 10)				/* bytes to kbytes */
124 #define B2P(x) ((x) >> PAGE_SHIFT)			/* bytes to pages */
125 #define P2B(x) ((x) << PAGE_SHIFT)			/* pages to bytes */
126 #define P2K(x) ((x) << (PAGE_SHIFT - 10))		/* pages to kbytes */
127 #define TV2J(x)	((x)->tv_sec * 100UL + (x)->tv_usec / 10000)
128 
129 /**
130  * @brief Mapping of ki_stat in struct kinfo_proc to the linux state
131  *
132  * The linux procfs state field displays one of the characters RSDZTW to
133  * denote running, sleeping in an interruptible wait, waiting in an
134  * uninterruptible disk sleep, a zombie process, process is being traced
135  * or stopped, or process is paging respectively.
136  *
137  * Our struct kinfo_proc contains the variable ki_stat which contains a
138  * value out of SIDL, SRUN, SSLEEP, SSTOP, SZOMB, SWAIT and SLOCK.
139  *
140  * This character array is used with ki_stati-1 as an index and tries to
141  * map our states to suitable linux states.
142  */
143 static char linux_state[] = "RRSTZDD";
144 
145 /*
146  * Filler function for proc/meminfo
147  */
148 static int
149 linprocfs_domeminfo(PFS_FILL_ARGS)
150 {
151 	unsigned long memtotal;		/* total memory in bytes */
152 	unsigned long memfree;		/* free memory in bytes */
153 	unsigned long cached;		/* page cache */
154 	unsigned long buffers;		/* buffer cache */
155 	unsigned long long swaptotal;	/* total swap space in bytes */
156 	unsigned long long swapused;	/* used swap space in bytes */
157 	unsigned long long swapfree;	/* free swap space in bytes */
158 	size_t sz;
159 	int error, i, j;
160 
161 	memtotal = physmem * PAGE_SIZE;
162 	memfree = (unsigned long)vm_free_count() * PAGE_SIZE;
163 	swap_pager_status(&i, &j);
164 	swaptotal = (unsigned long long)i * PAGE_SIZE;
165 	swapused = (unsigned long long)j * PAGE_SIZE;
166 	swapfree = swaptotal - swapused;
167 
168 	/*
169 	 * This value may exclude wired pages, but we have no good way of
170 	 * accounting for that.
171 	 */
172 	cached =
173 	    (vm_active_count() + vm_inactive_count() + vm_laundry_count()) *
174 	    PAGE_SIZE;
175 
176 	sz = sizeof(buffers);
177 	error = kernel_sysctlbyname(curthread, "vfs.bufspace", &buffers, &sz,
178 	    NULL, 0, 0, 0);
179 	if (error != 0)
180 		buffers = 0;
181 
182 	sbuf_printf(sb,
183 	    "MemTotal: %9lu kB\n"
184 	    "MemFree:  %9lu kB\n"
185 	    "Buffers:  %9lu kB\n"
186 	    "Cached:   %9lu kB\n"
187 	    "SwapTotal:%9llu kB\n"
188 	    "SwapFree: %9llu kB\n",
189 	    B2K(memtotal), B2K(memfree), B2K(buffers),
190 	    B2K(cached), B2K(swaptotal), B2K(swapfree));
191 
192 	return (0);
193 }
194 
195 #if defined(__i386__) || defined(__amd64__)
196 /*
197  * Filler function for proc/cpuinfo (i386 & amd64 version)
198  */
199 static int
200 linprocfs_docpuinfo(PFS_FILL_ARGS)
201 {
202 	uint64_t freq;
203 	u_int cache_size[4];
204 	u_int regs[4] = { 0 };
205 	int fqmhz, fqkhz;
206 	int i, j;
207 
208 	/*
209 	 * We default the flags to include all non-conflicting flags,
210 	 * and the Intel versions of conflicting flags.
211 	 */
212 	static char *cpu_feature_names[] = {
213 		/*  0 */ "fpu", "vme", "de", "pse",
214 		/*  4 */ "tsc", "msr", "pae", "mce",
215 		/*  8 */ "cx8", "apic", "", "sep",
216 		/* 12 */ "mtrr", "pge", "mca", "cmov",
217 		/* 16 */ "pat", "pse36", "pn", "clflush",
218 		/* 20 */ "", "dts", "acpi", "mmx",
219 		/* 24 */ "fxsr", "sse", "sse2", "ss",
220 		/* 28 */ "ht", "tm", "ia64", "pbe"
221 	};
222 
223 	static char *amd_feature_names[] = {
224 		/*  0 */ "", "", "", "",
225 		/*  4 */ "", "", "", "",
226 		/*  8 */ "", "", "", "syscall",
227 		/* 12 */ "", "", "", "",
228 		/* 16 */ "", "", "", "mp",
229 		/* 20 */ "nx", "", "mmxext", "",
230 		/* 24 */ "", "fxsr_opt", "pdpe1gb", "rdtscp",
231 		/* 28 */ "", "lm", "3dnowext", "3dnow"
232 	};
233 
234 	static char *cpu_feature2_names[] = {
235 		/*  0 */ "pni", "pclmulqdq", "dtes64", "monitor",
236 		/*  4 */ "ds_cpl", "vmx", "smx", "est",
237 		/*  8 */ "tm2", "ssse3", "cid", "sdbg",
238 		/* 12 */ "fma", "cx16", "xtpr", "pdcm",
239 		/* 16 */ "", "pcid", "dca", "sse4_1",
240 		/* 20 */ "sse4_2", "x2apic", "movbe", "popcnt",
241 		/* 24 */ "tsc_deadline_timer", "aes", "xsave", "",
242 		/* 28 */ "avx", "f16c", "rdrand", "hypervisor"
243 	};
244 
245 	static char *amd_feature2_names[] = {
246 		/*  0 */ "lahf_lm", "cmp_legacy", "svm", "extapic",
247 		/*  4 */ "cr8_legacy", "abm", "sse4a", "misalignsse",
248 		/*  8 */ "3dnowprefetch", "osvw", "ibs", "xop",
249 		/* 12 */ "skinit", "wdt", "", "lwp",
250 		/* 16 */ "fma4", "tce", "", "nodeid_msr",
251 		/* 20 */ "", "tbm", "topoext", "perfctr_core",
252 		/* 24 */ "perfctr_nb", "", "bpext", "ptsc",
253 		/* 28 */ "perfctr_llc", "mwaitx", "", ""
254 	};
255 
256 	static char *cpu_stdext_feature_names[] = {
257 		/*  0 */ "fsgsbase", "tsc_adjust", "sgx", "bmi1",
258 		/*  4 */ "hle", "avx2", "", "smep",
259 		/*  8 */ "bmi2", "erms", "invpcid", "rtm",
260 		/* 12 */ "cqm", "", "mpx", "rdt_a",
261 		/* 16 */ "avx512f", "avx512dq", "rdseed", "adx",
262 		/* 20 */ "smap", "avx512ifma", "", "clflushopt",
263 		/* 24 */ "clwb", "intel_pt", "avx512pf", "avx512er",
264 		/* 28 */ "avx512cd", "sha_ni", "avx512bw", "avx512vl"
265 	};
266 
267 	static char *cpu_stdext_feature2_names[] = {
268 		/*  0 */ "prefetchwt1", "avx512vbmi", "umip", "pku",
269 		/*  4 */ "ospke", "waitpkg", "avx512_vbmi2", "",
270 		/*  8 */ "gfni", "vaes", "vpclmulqdq", "avx512_vnni",
271 		/* 12 */ "avx512_bitalg", "", "avx512_vpopcntdq", "",
272 		/* 16 */ "", "", "", "",
273 		/* 20 */ "", "", "rdpid", "",
274 		/* 24 */ "", "cldemote", "", "movdiri",
275 		/* 28 */ "movdir64b", "enqcmd", "sgx_lc", ""
276 	};
277 
278 	static char *cpu_stdext_feature3_names[] = {
279 		/*  0 */ "", "", "avx512_4vnniw", "avx512_4fmaps",
280 		/*  4 */ "fsrm", "", "", "",
281 		/*  8 */ "avx512_vp2intersect", "", "md_clear", "",
282 		/* 12 */ "", "", "", "",
283 		/* 16 */ "", "", "pconfig", "",
284 		/* 20 */ "", "", "", "",
285 		/* 24 */ "", "", "ibrs", "stibp",
286 		/* 28 */ "flush_l1d", "arch_capabilities", "core_capabilities", "ssbd"
287 	};
288 
289 	static char *cpu_stdext_feature_l1_names[] = {
290 		/*  0 */ "xsaveopt", "xsavec", "xgetbv1", "xsaves",
291 		/*  4 */ "xfd"
292 	};
293 
294 	static char *power_flags[] = {
295 		"ts",           "fid",          "vid",
296 		"ttp",          "tm",           "stc",
297 		"100mhzsteps",  "hwpstate",     "",
298 		"cpb",          "eff_freq_ro",  "proc_feedback",
299 		"acc_power",
300 	};
301 
302 #ifdef __i386__
303 	switch (cpu_vendor_id) {
304 	case CPU_VENDOR_AMD:
305 		if (cpu_class < CPUCLASS_686)
306 			cpu_feature_names[16] = "fcmov";
307 		break;
308 	case CPU_VENDOR_CYRIX:
309 		cpu_feature_names[24] = "cxmmx";
310 		break;
311 	}
312 #endif
313 	if (cpu_exthigh >= 0x80000006)
314 		do_cpuid(0x80000006, cache_size);
315 	else
316 		memset(cache_size, 0, sizeof(cache_size));
317 	for (i = 0; i < mp_ncpus; ++i) {
318 		fqmhz = 0;
319 		fqkhz = 0;
320 		freq = atomic_load_acq_64(&tsc_freq);
321 		if (freq != 0) {
322 			fqmhz = (freq + 4999) / 1000000;
323 			fqkhz = ((freq + 4999) / 10000) % 100;
324 		}
325 		sbuf_printf(sb,
326 		    "processor\t: %d\n"
327 		    "vendor_id\t: %.20s\n"
328 		    "cpu family\t: %u\n"
329 		    "model\t\t: %u\n"
330 		    "model name\t: %s\n"
331 		    "stepping\t: %u\n"
332 		    "cpu MHz\t\t: %d.%02d\n"
333 		    "cache size\t: %d KB\n"
334 		    "physical id\t: %d\n"
335 		    "siblings\t: %d\n"
336 		    "core id\t\t: %d\n"
337 		    "cpu cores\t: %d\n"
338 		    "apicid\t\t: %d\n"
339 		    "initial apicid\t: %d\n"
340 		    "fpu\t\t: %s\n"
341 		    "fpu_exception\t: %s\n"
342 		    "cpuid level\t: %d\n"
343 		    "wp\t\t: %s\n",
344 		    i, cpu_vendor, CPUID_TO_FAMILY(cpu_id),
345 		    CPUID_TO_MODEL(cpu_id), cpu_model, cpu_id & CPUID_STEPPING,
346 		    fqmhz, fqkhz,
347 		    (cache_size[2] >> 16), 0, mp_ncpus, i, mp_ncpus,
348 		    i, i, /*cpu_id & CPUID_LOCAL_APIC_ID ??*/
349 		    (cpu_feature & CPUID_FPU) ? "yes" : "no", "yes",
350 		    CPUID_TO_FAMILY(cpu_id), "yes");
351 		sbuf_cat(sb, "flags\t\t:");
352 		for (j = 0; j < nitems(cpu_feature_names); j++)
353 			if (cpu_feature & (1 << j) &&
354 			    cpu_feature_names[j][0] != '\0')
355 				sbuf_printf(sb, " %s", cpu_feature_names[j]);
356 		for (j = 0; j < nitems(amd_feature_names); j++)
357 			if (amd_feature & (1 << j) &&
358 			    amd_feature_names[j][0] != '\0')
359 				sbuf_printf(sb, " %s", amd_feature_names[j]);
360 		for (j = 0; j < nitems(cpu_feature2_names); j++)
361 			if (cpu_feature2 & (1 << j) &&
362 			    cpu_feature2_names[j][0] != '\0')
363 				sbuf_printf(sb, " %s", cpu_feature2_names[j]);
364 		for (j = 0; j < nitems(amd_feature2_names); j++)
365 			if (amd_feature2 & (1 << j) &&
366 			    amd_feature2_names[j][0] != '\0')
367 				sbuf_printf(sb, " %s", amd_feature2_names[j]);
368 		for (j = 0; j < nitems(cpu_stdext_feature_names); j++)
369 			if (cpu_stdext_feature & (1 << j) &&
370 			    cpu_stdext_feature_names[j][0] != '\0')
371 				sbuf_printf(sb, " %s",
372 				    cpu_stdext_feature_names[j]);
373 		if (tsc_is_invariant)
374 			sbuf_cat(sb, " constant_tsc");
375 		for (j = 0; j < nitems(cpu_stdext_feature2_names); j++)
376 			if (cpu_stdext_feature2 & (1 << j) &&
377 			    cpu_stdext_feature2_names[j][0] != '\0')
378 				sbuf_printf(sb, " %s",
379 				    cpu_stdext_feature2_names[j]);
380 		for (j = 0; j < nitems(cpu_stdext_feature3_names); j++)
381 			if (cpu_stdext_feature3 & (1 << j) &&
382 			    cpu_stdext_feature3_names[j][0] != '\0')
383 				sbuf_printf(sb, " %s",
384 				    cpu_stdext_feature3_names[j]);
385 		if ((cpu_feature2 & CPUID2_XSAVE) != 0) {
386 			cpuid_count(0xd, 0x1, regs);
387 			for (j = 0; j < nitems(cpu_stdext_feature_l1_names); j++)
388 				if (regs[0] & (1 << j) &&
389 				    cpu_stdext_feature_l1_names[j][0] != '\0')
390 					sbuf_printf(sb, " %s",
391 					    cpu_stdext_feature_l1_names[j]);
392 		}
393 		sbuf_cat(sb, "\n");
394 		sbuf_printf(sb,
395 		    "bugs\t\t: %s\n"
396 		    "bogomips\t: %d.%02d\n"
397 		    "clflush size\t: %d\n"
398 		    "cache_alignment\t: %d\n"
399 		    "address sizes\t: %d bits physical, %d bits virtual\n",
400 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
401 		    (has_f00f_bug) ? "Intel F00F" : "",
402 #else
403 		    "",
404 #endif
405 		    fqmhz * 2, fqkhz,
406 		    cpu_clflush_line_size, cpu_clflush_line_size,
407 		    cpu_maxphyaddr,
408 		    (cpu_maxphyaddr > 32) ? 48 : 0);
409 		sbuf_cat(sb, "power management: ");
410 		for (j = 0; j < nitems(power_flags); j++)
411 			if (amd_pminfo & (1 << j))
412 				sbuf_printf(sb, " %s", power_flags[j]);
413 		sbuf_cat(sb, "\n\n");
414 
415 		/* XXX per-cpu vendor / class / model / id? */
416 	}
417 	sbuf_cat(sb, "\n");
418 
419 	return (0);
420 }
421 #else
422 /* ARM64TODO: implement non-stubbed linprocfs_docpuinfo */
423 static int
424 linprocfs_docpuinfo(PFS_FILL_ARGS)
425 {
426 	int i;
427 
428 	for (i = 0; i < mp_ncpus; ++i) {
429 		sbuf_printf(sb,
430 		    "processor\t: %d\n"
431 		    "BogoMIPS\t: %d.%02d\n",
432 		    i, 0, 0);
433 		sbuf_cat(sb, "Features\t: ");
434 		sbuf_cat(sb, "\n");
435 		sbuf_printf(sb,
436 		    "CPU implementer\t: \n"
437 		    "CPU architecture: \n"
438 		    "CPU variant\t: 0x%x\n"
439 		    "CPU part\t: 0x%x\n"
440 		    "CPU revision\t: %d\n",
441 		    0, 0, 0);
442 		sbuf_cat(sb, "\n");
443 	}
444 
445 	return (0);
446 }
447 #endif /* __i386__ || __amd64__ */
448 
449 static int
450 _mtab_helper(const struct pfs_node *pn, const struct statfs *sp,
451     const char **mntfrom, const char **mntto, const char **fstype)
452 {
453 	/* determine device name */
454 	*mntfrom = sp->f_mntfromname;
455 
456 	/* determine mount point */
457 	*mntto = sp->f_mntonname;
458 
459 	/* determine fs type */
460 	*fstype = sp->f_fstypename;
461 	if (strcmp(*fstype, pn->pn_info->pi_name) == 0)
462 		*mntfrom = *fstype = "proc";
463 	else if (strcmp(*fstype, "procfs") == 0)
464 		return (ECANCELED);
465 
466 	if (strcmp(*fstype, "autofs") == 0) {
467 		/*
468 		 * FreeBSD uses eg "map -hosts", whereas Linux
469 		 * expects just "-hosts".
470 		 */
471 		if (strncmp(*mntfrom, "map ", 4) == 0)
472 			*mntfrom += 4;
473 	}
474 
475 	if (strcmp(*fstype, "linsysfs") == 0) {
476 		*mntfrom = *fstype = "sysfs";
477 	} else {
478 		/* For Linux msdosfs is called vfat */
479 		if (strcmp(*fstype, "msdosfs") == 0)
480 			*fstype = "vfat";
481 	}
482 	return (0);
483 }
484 
485 static void
486 _sbuf_mntoptions_helper(struct sbuf *sb, uint64_t f_flags)
487 {
488 	sbuf_cat(sb, (f_flags & MNT_RDONLY) ? "ro" : "rw");
489 #define ADD_OPTION(opt, name) \
490 	if (f_flags & (opt)) sbuf_cat(sb, "," name);
491 	ADD_OPTION(MNT_SYNCHRONOUS,	"sync");
492 	ADD_OPTION(MNT_NOEXEC,		"noexec");
493 	ADD_OPTION(MNT_NOSUID,		"nosuid");
494 	ADD_OPTION(MNT_UNION,		"union");
495 	ADD_OPTION(MNT_ASYNC,		"async");
496 	ADD_OPTION(MNT_SUIDDIR,		"suiddir");
497 	ADD_OPTION(MNT_NOSYMFOLLOW,	"nosymfollow");
498 	ADD_OPTION(MNT_NOATIME,		"noatime");
499 #undef ADD_OPTION
500 }
501 
502 /*
503  * Filler function for proc/mtab and proc/<pid>/mounts.
504  *
505  * /proc/mtab doesn't exist in Linux' procfs, but is included here so
506  * users can symlink /compat/linux/etc/mtab to /proc/mtab
507  */
508 static int
509 linprocfs_domtab(PFS_FILL_ARGS)
510 {
511 	const char *mntto, *mntfrom, *fstype;
512 	char *dlep, *flep;
513 	struct vnode *vp;
514 	struct pwd *pwd;
515 	size_t lep_len;
516 	int error;
517 	struct statfs *buf, *sp;
518 	size_t count;
519 
520 	/*
521 	 * Resolve emulation tree prefix
522 	 */
523 	flep = NULL;
524 	pwd = pwd_hold(td);
525 	vp = pwd->pwd_adir;
526 	error = vn_fullpath_global(vp, &dlep, &flep);
527 	pwd_drop(pwd);
528 	if (error != 0)
529 		return (error);
530 	lep_len = strlen(dlep);
531 
532 	buf = NULL;
533 	error = kern_getfsstat(td, &buf, SIZE_T_MAX, &count,
534 	    UIO_SYSSPACE, MNT_WAIT);
535 	if (error != 0) {
536 		goto out;
537 	}
538 
539 	for (sp = buf; count > 0; sp++, count--) {
540 		error = _mtab_helper(pn, sp, &mntfrom, &mntto, &fstype);
541 		if (error != 0) {
542 			MPASS(error == ECANCELED);
543 			continue;
544 		}
545 
546 		/* determine mount point */
547 		if (strncmp(mntto, dlep, lep_len) == 0 && mntto[lep_len] == '/')
548 			mntto += lep_len;
549 
550 		sbuf_printf(sb, "%s %s %s ", mntfrom, mntto, fstype);
551 		_sbuf_mntoptions_helper(sb, sp->f_flags);
552 		/* a real Linux mtab will also show NFS options */
553 		sbuf_printf(sb, " 0 0\n");
554 	}
555 
556 	error = 0;
557 out:
558 	free(buf, M_TEMP);
559 	free(flep, M_TEMP);
560 	return (error);
561 }
562 
563 static int
564 linprocfs_doprocmountinfo(PFS_FILL_ARGS)
565 {
566 	const char *mntfrom, *mntto, *fstype;
567 	char *dlep, *flep;
568 	struct statfs *buf, *sp;
569 	size_t count, lep_len;
570 	struct vnode *vp;
571 	struct pwd *pwd;
572 	int error;
573 
574 	/*
575 	 * Resolve emulation tree prefix
576 	 */
577 	flep = NULL;
578 	pwd = pwd_hold(td);
579 	vp = pwd->pwd_adir;
580 	error = vn_fullpath_global(vp, &dlep, &flep);
581 	pwd_drop(pwd);
582 	if (error != 0)
583 		return (error);
584 	lep_len = strlen(dlep);
585 
586 	buf = NULL;
587 	error = kern_getfsstat(td, &buf, SIZE_T_MAX, &count,
588 	    UIO_SYSSPACE, MNT_WAIT);
589 	if (error != 0)
590 		goto out;
591 
592 	for (sp = buf; count > 0; sp++, count--) {
593 		error = _mtab_helper(pn, sp, &mntfrom, &mntto, &fstype);
594 		if (error != 0) {
595 			MPASS(error == ECANCELED);
596 			continue;
597 		}
598 
599 		if (strncmp(mntto, dlep, lep_len) == 0 && mntto[lep_len] == '/')
600 			mntto += lep_len;
601 #if 0
602 		/*
603 		 * If the prefix is a chroot, and this mountpoint is not under
604 		 * the prefix, we should skip it.  Leave it for now for
605 		 * consistency with procmtab above.
606 		 */
607 		else
608 			continue;
609 #endif
610 
611 		/*
612 		 * (1) mount id
613 		 *
614 		 * (2) parent mount id -- we don't have this cheaply, so
615 		 * provide a dummy value
616 		 *
617 		 * (3) major:minor -- ditto
618 		 *
619 		 * (4) root filesystem mount -- probably a namespaces thing
620 		 *
621 		 * (5) mountto path
622 		 */
623 		sbuf_printf(sb, "%u 0 0:0 / %s ",
624 		    sp->f_fsid.val[0] ^ sp->f_fsid.val[1], mntto);
625 		/* (6) mount options */
626 		_sbuf_mntoptions_helper(sb, sp->f_flags);
627 		/*
628 		 * (7) zero or more optional fields -- again, namespace related
629 		 *
630 		 * (8) End of variable length fields separator ("-")
631 		 *
632 		 * (9) fstype
633 		 *
634 		 * (10) mount from
635 		 *
636 		 * (11) "superblock" options -- like (6), but different
637 		 * semantics in Linux
638 		 */
639 		sbuf_printf(sb, " - %s %s %s\n", fstype, mntfrom,
640 		    (sp->f_flags & MNT_RDONLY) ? "ro" : "rw");
641 	}
642 
643 	error = 0;
644 out:
645 	free(buf, M_TEMP);
646 	free(flep, M_TEMP);
647 	return (error);
648 }
649 
650 /*
651  * Filler function for proc/partitions
652  */
653 static int
654 linprocfs_dopartitions(PFS_FILL_ARGS)
655 {
656 	struct g_class *cp;
657 	struct g_geom *gp;
658 	struct g_provider *pp;
659 	int major, minor;
660 
661 	g_topology_lock();
662 	sbuf_printf(sb, "major minor  #blocks  name\n\n");
663 
664 	LIST_FOREACH(cp, &g_classes, class) {
665 		if (strcmp(cp->name, "DISK") == 0 ||
666 		    strcmp(cp->name, "PART") == 0)
667 			LIST_FOREACH(gp, &cp->geom, geom) {
668 				LIST_FOREACH(pp, &gp->provider, provider) {
669 					if (linux_driver_get_major_minor(
670 					    pp->name, &major, &minor) != 0) {
671 						major = 0;
672 						minor = 0;
673 					}
674 					sbuf_printf(sb, "%4d  %7d %10lld %s\n",
675 					    major, minor,
676 					    B2K((long long)pp->mediasize),
677 					    pp->name);
678 				}
679 			}
680 	}
681 	g_topology_unlock();
682 
683 	return (0);
684 }
685 
686 /*
687  * Filler function for proc/stat
688  *
689  * Output depends on kernel version:
690  *
691  * v2.5.40 <=
692  *   user nice system idle
693  * v2.5.41
694  *   user nice system idle iowait
695  * v2.6.11
696  *   user nice system idle iowait irq softirq steal
697  * v2.6.24
698  *   user nice system idle iowait irq softirq steal guest
699  * v2.6.33 >=
700  *   user nice system idle iowait irq softirq steal guest guest_nice
701  */
702 static int
703 linprocfs_dostat(PFS_FILL_ARGS)
704 {
705 	struct pcpu *pcpu;
706 	long cp_time[CPUSTATES];
707 	long *cp;
708 	struct timeval boottime;
709 	int i;
710 	char *zero_pad;
711 	bool has_intr = true;
712 
713 	if (linux_kernver(td) >= LINUX_KERNVER(2,6,33)) {
714 		zero_pad = " 0 0 0 0\n";
715 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,6,24)) {
716 		zero_pad = " 0 0 0\n";
717 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,6,11)) {
718 		zero_pad = " 0 0\n";
719 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,5,41)) {
720 		has_intr = false;
721 		zero_pad = " 0\n";
722 	} else {
723 		has_intr = false;
724 		zero_pad = "\n";
725 	}
726 
727 	read_cpu_time(cp_time);
728 	getboottime(&boottime);
729 	/* Parameters common to all versions */
730 	sbuf_printf(sb, "cpu %lu %lu %lu %lu",
731 	    T2J(cp_time[CP_USER]),
732 	    T2J(cp_time[CP_NICE]),
733 	    T2J(cp_time[CP_SYS]),
734 	    T2J(cp_time[CP_IDLE]));
735 
736 	/* Print interrupt stats if available */
737 	if (has_intr) {
738 		sbuf_printf(sb, " 0 %lu", T2J(cp_time[CP_INTR]));
739 	}
740 
741 	/* Pad out remaining fields depending on version */
742 	sbuf_printf(sb, "%s", zero_pad);
743 
744 	CPU_FOREACH(i) {
745 		pcpu = pcpu_find(i);
746 		cp = pcpu->pc_cp_time;
747 		sbuf_printf(sb, "cpu%d %lu %lu %lu %lu", i,
748 		    T2J(cp[CP_USER]),
749 		    T2J(cp[CP_NICE]),
750 		    T2J(cp[CP_SYS]),
751 		    T2J(cp[CP_IDLE]));
752 
753 		if (has_intr) {
754 			sbuf_printf(sb, " 0 %lu", T2J(cp[CP_INTR]));
755 		}
756 
757 		sbuf_printf(sb, "%s", zero_pad);
758 	}
759 	sbuf_printf(sb,
760 	    "disk 0 0 0 0\n"
761 	    "page %ju %ju\n"
762 	    "swap %ju %ju\n"
763 	    "intr %ju\n"
764 	    "ctxt %ju\n"
765 	    "btime %lld\n",
766 	    (uintmax_t)VM_CNT_FETCH(v_vnodepgsin),
767 	    (uintmax_t)VM_CNT_FETCH(v_vnodepgsout),
768 	    (uintmax_t)VM_CNT_FETCH(v_swappgsin),
769 	    (uintmax_t)VM_CNT_FETCH(v_swappgsout),
770 	    (uintmax_t)VM_CNT_FETCH(v_intr),
771 	    (uintmax_t)VM_CNT_FETCH(v_swtch),
772 	    (long long)boottime.tv_sec);
773 	return (0);
774 }
775 
776 static int
777 linprocfs_doswaps(PFS_FILL_ARGS)
778 {
779 	struct xswdev xsw;
780 	uintmax_t total, used;
781 	int n;
782 	char devname[SPECNAMELEN + 1];
783 
784 	sbuf_printf(sb, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
785 	for (n = 0; ; n++) {
786 		if (swap_dev_info(n, &xsw, devname, sizeof(devname)) != 0)
787 			break;
788 		total = (uintmax_t)xsw.xsw_nblks * PAGE_SIZE / 1024;
789 		used  = (uintmax_t)xsw.xsw_used * PAGE_SIZE / 1024;
790 
791 		/*
792 		 * The space and not tab after the device name is on
793 		 * purpose.  Linux does so.
794 		 */
795 		sbuf_printf(sb, "/dev/%-34s unknown\t\t%jd\t%jd\t-1\n",
796 		    devname, total, used);
797 	}
798 	return (0);
799 }
800 
801 /*
802  * Filler function for proc/uptime
803  */
804 static int
805 linprocfs_douptime(PFS_FILL_ARGS)
806 {
807 	long cp_time[CPUSTATES];
808 	struct timeval tv;
809 
810 	getmicrouptime(&tv);
811 	read_cpu_time(cp_time);
812 	sbuf_printf(sb, "%lld.%02ld %ld.%02lu\n",
813 	    (long long)tv.tv_sec, tv.tv_usec / 10000,
814 	    T2S(cp_time[CP_IDLE] / mp_ncpus),
815 	    T2CS(cp_time[CP_IDLE] / mp_ncpus) % 100);
816 	return (0);
817 }
818 
819 /*
820  * Get OS build date
821  */
822 static void
823 linprocfs_osbuild(struct thread *td, struct sbuf *sb)
824 {
825 #if 0
826 	char osbuild[256];
827 	char *cp1, *cp2;
828 
829 	strncpy(osbuild, version, 256);
830 	osbuild[255] = '\0';
831 	cp1 = strstr(osbuild, "\n");
832 	cp2 = strstr(osbuild, ":");
833 	if (cp1 && cp2) {
834 		*cp1 = *cp2 = '\0';
835 		cp1 = strstr(osbuild, "#");
836 	} else
837 		cp1 = NULL;
838 	if (cp1)
839 		sbuf_printf(sb, "%s%s", cp1, cp2 + 1);
840 	else
841 #endif
842 		sbuf_cat(sb, "#4 Sun Dec 18 04:30:00 CET 1977");
843 }
844 
845 /*
846  * Get OS builder
847  */
848 static void
849 linprocfs_osbuilder(struct thread *td, struct sbuf *sb)
850 {
851 #if 0
852 	char builder[256];
853 	char *cp;
854 
855 	cp = strstr(version, "\n    ");
856 	if (cp) {
857 		strncpy(builder, cp + 5, 256);
858 		builder[255] = '\0';
859 		cp = strstr(builder, ":");
860 		if (cp)
861 			*cp = '\0';
862 	}
863 	if (cp)
864 		sbuf_cat(sb, builder);
865 	else
866 #endif
867 		sbuf_cat(sb, "des@freebsd.org");
868 }
869 
870 /*
871  * Filler function for proc/version
872  */
873 static int
874 linprocfs_doversion(PFS_FILL_ARGS)
875 {
876 	char osname[LINUX_MAX_UTSNAME];
877 	char osrelease[LINUX_MAX_UTSNAME];
878 
879 	linux_get_osname(td, osname);
880 	linux_get_osrelease(td, osrelease);
881 	sbuf_printf(sb, "%s version %s (", osname, osrelease);
882 	linprocfs_osbuilder(td, sb);
883 	sbuf_cat(sb, ") (gcc version " __VERSION__ ") ");
884 	linprocfs_osbuild(td, sb);
885 	sbuf_cat(sb, "\n");
886 
887 	return (0);
888 }
889 
890 /*
891  * Filler function for proc/loadavg
892  */
893 static int
894 linprocfs_doloadavg(PFS_FILL_ARGS)
895 {
896 
897 	sbuf_printf(sb,
898 	    "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
899 	    (int)(averunnable.ldavg[0] / averunnable.fscale),
900 	    (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
901 	    (int)(averunnable.ldavg[1] / averunnable.fscale),
902 	    (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
903 	    (int)(averunnable.ldavg[2] / averunnable.fscale),
904 	    (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
905 	    1,				/* number of running tasks */
906 	    nprocs,			/* number of tasks */
907 	    lastpid			/* the last pid */
908 	);
909 	return (0);
910 }
911 
912 static int
913 linprocfs_get_tty_nr(struct proc *p)
914 {
915 	struct session *sp;
916 	const char *ttyname;
917 	int error, major, minor, nr;
918 
919 	PROC_LOCK_ASSERT(p, MA_OWNED);
920 	sx_assert(&proctree_lock, SX_LOCKED);
921 
922 	if ((p->p_flag & P_CONTROLT) == 0)
923 		return (-1);
924 
925 	sp = p->p_pgrp->pg_session;
926 	if (sp == NULL)
927 		return (-1);
928 
929 	ttyname = devtoname(sp->s_ttyp->t_dev);
930 	error = linux_driver_get_major_minor(ttyname, &major, &minor);
931 	if (error != 0)
932 		return (-1);
933 
934 	nr = makedev(major, minor);
935 	return (nr);
936 }
937 
938 /*
939  * Filler function for proc/pid/stat
940  */
941 static int
942 linprocfs_doprocstat(PFS_FILL_ARGS)
943 {
944 	struct kinfo_proc kp;
945 	struct timeval boottime;
946 	char state;
947 	static int ratelimit = 0;
948 	int tty_nr;
949 	vm_offset_t startcode, startdata;
950 
951 	getboottime(&boottime);
952 	sx_slock(&proctree_lock);
953 	PROC_LOCK(p);
954 	fill_kinfo_proc(p, &kp);
955 	tty_nr = linprocfs_get_tty_nr(p);
956 	sx_sunlock(&proctree_lock);
957 	if (p->p_vmspace) {
958 	   startcode = (vm_offset_t)p->p_vmspace->vm_taddr;
959 	   startdata = (vm_offset_t)p->p_vmspace->vm_daddr;
960 	} else {
961 	   startcode = 0;
962 	   startdata = 0;
963 	}
964 	sbuf_printf(sb, "%d", p->p_pid);
965 #define PS_ADD(name, fmt, arg) sbuf_printf(sb, " " fmt, arg)
966 	PS_ADD("comm",		"(%s)",	p->p_comm);
967 	if (kp.ki_stat > sizeof(linux_state)) {
968 		state = 'R';
969 
970 		if (ratelimit == 0) {
971 			printf("linprocfs: don't know how to handle unknown FreeBSD state %d/%zd, mapping to R\n",
972 			    kp.ki_stat, sizeof(linux_state));
973 			++ratelimit;
974 		}
975 	} else
976 		state = linux_state[kp.ki_stat - 1];
977 	PS_ADD("state",		"%c",	state);
978 	PS_ADD("ppid",		"%d",	p->p_pptr ? p->p_pptr->p_pid : 0);
979 	PS_ADD("pgrp",		"%d",	p->p_pgid);
980 	PS_ADD("session",	"%d",	p->p_session->s_sid);
981 	PROC_UNLOCK(p);
982 	PS_ADD("tty",		"%d",	tty_nr);
983 	PS_ADD("tpgid",		"%d",	kp.ki_tpgid);
984 	PS_ADD("flags",		"%u",	0); /* XXX */
985 	PS_ADD("minflt",	"%lu",	kp.ki_rusage.ru_minflt);
986 	PS_ADD("cminflt",	"%lu",	kp.ki_rusage_ch.ru_minflt);
987 	PS_ADD("majflt",	"%lu",	kp.ki_rusage.ru_majflt);
988 	PS_ADD("cmajflt",	"%lu",	kp.ki_rusage_ch.ru_majflt);
989 	PS_ADD("utime",		"%ld",	TV2J(&kp.ki_rusage.ru_utime));
990 	PS_ADD("stime",		"%ld",	TV2J(&kp.ki_rusage.ru_stime));
991 	PS_ADD("cutime",	"%ld",	TV2J(&kp.ki_rusage_ch.ru_utime));
992 	PS_ADD("cstime",	"%ld",	TV2J(&kp.ki_rusage_ch.ru_stime));
993 	PS_ADD("priority",	"%d",	kp.ki_pri.pri_user);
994 	PS_ADD("nice",		"%d",	kp.ki_nice); /* 19 (nicest) to -19 */
995 	PS_ADD("0",		"%d",	0); /* removed field */
996 	PS_ADD("itrealvalue",	"%d",	0); /* XXX */
997 	PS_ADD("starttime",	"%lu",	TV2J(&kp.ki_start) - TV2J(&boottime));
998 	PS_ADD("vsize",		"%ju",	(uintmax_t)kp.ki_size);
999 	PS_ADD("rss",		"%ju",	(uintmax_t)kp.ki_rssize);
1000 	PS_ADD("rlim",		"%lu",	kp.ki_rusage.ru_maxrss);
1001 	PS_ADD("startcode",	"%ju",	(uintmax_t)startcode);
1002 	PS_ADD("endcode",	"%ju",	(uintmax_t)startdata);
1003 	PS_ADD("startstack",	"%u",	0); /* XXX */
1004 	PS_ADD("kstkesp",	"%u",	0); /* XXX */
1005 	PS_ADD("kstkeip",	"%u",	0); /* XXX */
1006 	PS_ADD("signal",	"%u",	0); /* XXX */
1007 	PS_ADD("blocked",	"%u",	0); /* XXX */
1008 	PS_ADD("sigignore",	"%u",	0); /* XXX */
1009 	PS_ADD("sigcatch",	"%u",	0); /* XXX */
1010 	PS_ADD("wchan",		"%u",	0); /* XXX */
1011 	PS_ADD("nswap",		"%lu",	kp.ki_rusage.ru_nswap);
1012 	PS_ADD("cnswap",	"%lu",	kp.ki_rusage_ch.ru_nswap);
1013 	PS_ADD("exitsignal",	"%d",	0); /* XXX */
1014 	PS_ADD("processor",	"%u",	kp.ki_lastcpu);
1015 	PS_ADD("rt_priority",	"%u",	0); /* XXX */ /* >= 2.5.19 */
1016 	PS_ADD("policy",	"%u",	kp.ki_pri.pri_class); /* >= 2.5.19 */
1017 #undef PS_ADD
1018 	sbuf_putc(sb, '\n');
1019 
1020 	return (0);
1021 }
1022 
1023 /*
1024  * Filler function for proc/pid/statm
1025  */
1026 static int
1027 linprocfs_doprocstatm(PFS_FILL_ARGS)
1028 {
1029 	struct kinfo_proc kp;
1030 	segsz_t lsize;
1031 
1032 	sx_slock(&proctree_lock);
1033 	PROC_LOCK(p);
1034 	fill_kinfo_proc(p, &kp);
1035 	PROC_UNLOCK(p);
1036 	sx_sunlock(&proctree_lock);
1037 
1038 	/*
1039 	 * See comments in linprocfs_doprocstatus() regarding the
1040 	 * computation of lsize.
1041 	 */
1042 	/* size resident share trs drs lrs dt */
1043 	sbuf_printf(sb, "%ju ", B2P((uintmax_t)kp.ki_size));
1044 	sbuf_printf(sb, "%ju ", (uintmax_t)kp.ki_rssize);
1045 	sbuf_printf(sb, "%ju ", (uintmax_t)0); /* XXX */
1046 	sbuf_printf(sb, "%ju ",	(uintmax_t)kp.ki_tsize);
1047 	sbuf_printf(sb, "%ju ", (uintmax_t)(kp.ki_dsize + kp.ki_ssize));
1048 	lsize = B2P(kp.ki_size) - kp.ki_dsize -
1049 	    kp.ki_ssize - kp.ki_tsize - 1;
1050 	sbuf_printf(sb, "%ju ", (uintmax_t)lsize);
1051 	sbuf_printf(sb, "%ju\n", (uintmax_t)0); /* XXX */
1052 
1053 	return (0);
1054 }
1055 
1056 /*
1057  * Filler function for proc/pid/status
1058  */
1059 static int
1060 linprocfs_doprocstatus(PFS_FILL_ARGS)
1061 {
1062 	struct kinfo_proc kp;
1063 	char *state;
1064 	segsz_t lsize;
1065 	struct thread *td2;
1066 	struct sigacts *ps;
1067 	l_sigset_t siglist, sigignore, sigcatch;
1068 	int i;
1069 
1070 	sx_slock(&proctree_lock);
1071 	PROC_LOCK(p);
1072 	td2 = FIRST_THREAD_IN_PROC(p);
1073 
1074 	if (P_SHOULDSTOP(p)) {
1075 		state = "T (stopped)";
1076 	} else {
1077 		switch(p->p_state) {
1078 		case PRS_NEW:
1079 			state = "I (idle)";
1080 			break;
1081 		case PRS_NORMAL:
1082 			if (p->p_flag & P_WEXIT) {
1083 				state = "X (exiting)";
1084 				break;
1085 			}
1086 			switch(TD_GET_STATE(td2)) {
1087 			case TDS_INHIBITED:
1088 				state = "S (sleeping)";
1089 				break;
1090 			case TDS_RUNQ:
1091 			case TDS_RUNNING:
1092 				state = "R (running)";
1093 				break;
1094 			default:
1095 				state = "? (unknown)";
1096 				break;
1097 			}
1098 			break;
1099 		case PRS_ZOMBIE:
1100 			state = "Z (zombie)";
1101 			break;
1102 		default:
1103 			state = "? (unknown)";
1104 			break;
1105 		}
1106 	}
1107 
1108 	fill_kinfo_proc(p, &kp);
1109 	sx_sunlock(&proctree_lock);
1110 
1111 	sbuf_printf(sb, "Name:\t%s\n",		p->p_comm); /* XXX escape */
1112 	sbuf_printf(sb, "State:\t%s\n",		state);
1113 
1114 	/*
1115 	 * Credentials
1116 	 */
1117 	sbuf_printf(sb, "Tgid:\t%d\n",		p->p_pid);
1118 	sbuf_printf(sb, "Pid:\t%d\n",		p->p_pid);
1119 	sbuf_printf(sb, "PPid:\t%d\n",		kp.ki_ppid );
1120 	sbuf_printf(sb, "TracerPid:\t%d\n",	kp.ki_tracer );
1121 	sbuf_printf(sb, "Uid:\t%d\t%d\t%d\t%d\n", p->p_ucred->cr_ruid,
1122 						p->p_ucred->cr_uid,
1123 						p->p_ucred->cr_svuid,
1124 						/* FreeBSD doesn't have fsuid */
1125 						p->p_ucred->cr_uid);
1126 	sbuf_printf(sb, "Gid:\t%d\t%d\t%d\t%d\n", p->p_ucred->cr_rgid,
1127 						p->p_ucred->cr_gid,
1128 						p->p_ucred->cr_svgid,
1129 						/* FreeBSD doesn't have fsgid */
1130 						p->p_ucred->cr_gid);
1131 	sbuf_cat(sb, "Groups:\t");
1132 	for (i = 0; i < p->p_ucred->cr_ngroups; i++)
1133 		sbuf_printf(sb, "%d ",		p->p_ucred->cr_groups[i]);
1134 	PROC_UNLOCK(p);
1135 	sbuf_putc(sb, '\n');
1136 
1137 	/*
1138 	 * Memory
1139 	 *
1140 	 * While our approximation of VmLib may not be accurate (I
1141 	 * don't know of a simple way to verify it, and I'm not sure
1142 	 * it has much meaning anyway), I believe it's good enough.
1143 	 *
1144 	 * The same code that could (I think) accurately compute VmLib
1145 	 * could also compute VmLck, but I don't really care enough to
1146 	 * implement it. Submissions are welcome.
1147 	 */
1148 	sbuf_printf(sb, "VmSize:\t%8ju kB\n",	B2K((uintmax_t)kp.ki_size));
1149 	sbuf_printf(sb, "VmLck:\t%8u kB\n",	P2K(0)); /* XXX */
1150 	sbuf_printf(sb, "VmRSS:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_rssize));
1151 	sbuf_printf(sb, "VmData:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_dsize));
1152 	sbuf_printf(sb, "VmStk:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_ssize));
1153 	sbuf_printf(sb, "VmExe:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_tsize));
1154 	lsize = B2P(kp.ki_size) - kp.ki_dsize -
1155 	    kp.ki_ssize - kp.ki_tsize - 1;
1156 	sbuf_printf(sb, "VmLib:\t%8ju kB\n",	P2K((uintmax_t)lsize));
1157 
1158 	/*
1159 	 * Signal masks
1160 	 */
1161 	PROC_LOCK(p);
1162 	bsd_to_linux_sigset(&p->p_siglist, &siglist);
1163 	ps = p->p_sigacts;
1164 	mtx_lock(&ps->ps_mtx);
1165 	bsd_to_linux_sigset(&ps->ps_sigignore, &sigignore);
1166 	bsd_to_linux_sigset(&ps->ps_sigcatch, &sigcatch);
1167 	mtx_unlock(&ps->ps_mtx);
1168 	PROC_UNLOCK(p);
1169 
1170 	sbuf_printf(sb, "SigPnd:\t%016jx\n",	siglist.__mask);
1171 	/*
1172 	 * XXX. SigBlk - target thread's signal mask, td_sigmask.
1173 	 * To implement SigBlk pseudofs should support proc/tid dir entries.
1174 	 */
1175 	sbuf_printf(sb, "SigBlk:\t%016x\n",	0);
1176 	sbuf_printf(sb, "SigIgn:\t%016jx\n",	sigignore.__mask);
1177 	sbuf_printf(sb, "SigCgt:\t%016jx\n",	sigcatch.__mask);
1178 
1179 	/*
1180 	 * Linux also prints the capability masks, but we don't have
1181 	 * capabilities yet, and when we do get them they're likely to
1182 	 * be meaningless to Linux programs, so we lie. XXX
1183 	 */
1184 	sbuf_printf(sb, "CapInh:\t%016x\n",	0);
1185 	sbuf_printf(sb, "CapPrm:\t%016x\n",	0);
1186 	sbuf_printf(sb, "CapEff:\t%016x\n",	0);
1187 
1188 	return (0);
1189 }
1190 
1191 /*
1192  * Filler function for proc/pid/cwd
1193  */
1194 static int
1195 linprocfs_doproccwd(PFS_FILL_ARGS)
1196 {
1197 	struct pwd *pwd;
1198 	char *fullpath = "unknown";
1199 	char *freepath = NULL;
1200 
1201 	pwd = pwd_hold_proc(p);
1202 	vn_fullpath(pwd->pwd_cdir, &fullpath, &freepath);
1203 	sbuf_printf(sb, "%s", fullpath);
1204 	if (freepath)
1205 		free(freepath, M_TEMP);
1206 	pwd_drop(pwd);
1207 	return (0);
1208 }
1209 
1210 /*
1211  * Filler function for proc/pid/root
1212  */
1213 static int
1214 linprocfs_doprocroot(PFS_FILL_ARGS)
1215 {
1216 	struct pwd *pwd;
1217 	struct vnode *vp;
1218 	char *fullpath = "unknown";
1219 	char *freepath = NULL;
1220 
1221 	pwd = pwd_hold_proc(p);
1222 	vp = jailed(p->p_ucred) ? pwd->pwd_jdir : pwd->pwd_rdir;
1223 	vn_fullpath(vp, &fullpath, &freepath);
1224 	sbuf_printf(sb, "%s", fullpath);
1225 	if (freepath)
1226 		free(freepath, M_TEMP);
1227 	pwd_drop(pwd);
1228 	return (0);
1229 }
1230 
1231 /*
1232  * Filler function for proc/pid/cmdline
1233  */
1234 static int
1235 linprocfs_doproccmdline(PFS_FILL_ARGS)
1236 {
1237 	int ret;
1238 
1239 	PROC_LOCK(p);
1240 	if ((ret = p_cansee(td, p)) != 0) {
1241 		PROC_UNLOCK(p);
1242 		return (ret);
1243 	}
1244 
1245 	/*
1246 	 * Mimic linux behavior and pass only processes with usermode
1247 	 * address space as valid.  Return zero silently otherwize.
1248 	 */
1249 	if (p->p_vmspace == &vmspace0) {
1250 		PROC_UNLOCK(p);
1251 		return (0);
1252 	}
1253 	if (p->p_args != NULL) {
1254 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
1255 		PROC_UNLOCK(p);
1256 		return (0);
1257 	}
1258 
1259 	if ((p->p_flag & P_SYSTEM) != 0) {
1260 		PROC_UNLOCK(p);
1261 		return (0);
1262 	}
1263 
1264 	PROC_UNLOCK(p);
1265 
1266 	ret = proc_getargv(td, p, sb);
1267 	return (ret);
1268 }
1269 
1270 /*
1271  * Filler function for proc/pid/environ
1272  */
1273 static int
1274 linprocfs_doprocenviron(PFS_FILL_ARGS)
1275 {
1276 
1277 	/*
1278 	 * Mimic linux behavior and pass only processes with usermode
1279 	 * address space as valid.  Return zero silently otherwize.
1280 	 */
1281 	if (p->p_vmspace == &vmspace0)
1282 		return (0);
1283 
1284 	return (proc_getenvv(td, p, sb));
1285 }
1286 
1287 static char l32_map_str[] = "%08lx-%08lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1288 static char l64_map_str[] = "%016lx-%016lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1289 static char vdso_str[] = "      [vdso]";
1290 static char stack_str[] = "      [stack]";
1291 
1292 /*
1293  * Filler function for proc/pid/maps
1294  */
1295 static int
1296 linprocfs_doprocmaps(PFS_FILL_ARGS)
1297 {
1298 	struct vmspace *vm;
1299 	vm_map_t map;
1300 	vm_map_entry_t entry, tmp_entry;
1301 	vm_object_t obj, tobj, lobj;
1302 	vm_offset_t e_start, e_end;
1303 	vm_ooffset_t off;
1304 	vm_prot_t e_prot;
1305 	unsigned int last_timestamp;
1306 	char *name = "", *freename = NULL;
1307 	const char *l_map_str;
1308 	ino_t ino;
1309 	int error;
1310 	struct vnode *vp;
1311 	struct vattr vat;
1312 	bool private;
1313 
1314 	if (uio->uio_rw != UIO_READ)
1315 		return (EOPNOTSUPP);
1316 
1317 	error = proc_vmspace_ref(td, p, PRVM_BLOCK_EXEC | PRVM_CHECK_DEBUG,
1318 	    &vm);
1319 	if (error != 0)
1320 		return (error);
1321 
1322 	if (SV_CURPROC_FLAG(SV_LP64))
1323 		l_map_str = l64_map_str;
1324 	else
1325 		l_map_str = l32_map_str;
1326 	map = &vm->vm_map;
1327 	vm_map_lock_read(map);
1328 	VM_MAP_ENTRY_FOREACH(entry, map) {
1329 		name = "";
1330 		freename = NULL;
1331 		/*
1332 		 * Skip printing of the guard page of the stack region, as
1333 		 * it confuses glibc pthread_getattr_np() method, where both
1334 		 * the base address and size of the stack of the initial thread
1335 		 * are calculated.
1336 		 */
1337 		if ((entry->eflags & (MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_GUARD)) != 0)
1338 			continue;
1339 		e_prot = entry->protection;
1340 		e_start = entry->start;
1341 		e_end = entry->end;
1342 		obj = entry->object.vm_object;
1343 		off = entry->offset;
1344 		for (lobj = tobj = obj; tobj != NULL;
1345 		    lobj = tobj, tobj = tobj->backing_object) {
1346 			VM_OBJECT_RLOCK(tobj);
1347 			off += lobj->backing_object_offset;
1348 			if (lobj != obj)
1349 				VM_OBJECT_RUNLOCK(lobj);
1350 		}
1351 		private = (entry->eflags & MAP_ENTRY_COW) != 0 || obj == NULL ||
1352 		    (obj->flags & OBJ_ANON) != 0;
1353 		last_timestamp = map->timestamp;
1354 		vm_map_unlock_read(map);
1355 		ino = 0;
1356 		if (lobj) {
1357 			vp = vm_object_vnode(lobj);
1358 			if (vp != NULL)
1359 				vref(vp);
1360 			if (lobj != obj)
1361 				VM_OBJECT_RUNLOCK(lobj);
1362 			VM_OBJECT_RUNLOCK(obj);
1363 			if (vp != NULL) {
1364 				vn_fullpath(vp, &name, &freename);
1365 				vn_lock(vp, LK_SHARED | LK_RETRY);
1366 				VOP_GETATTR(vp, &vat, td->td_ucred);
1367 				ino = vat.va_fileid;
1368 				vput(vp);
1369 			} else if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
1370 				/*
1371 				 * sv_shared_page_base pointed out to the
1372 				 * FreeBSD sharedpage, PAGE_SIZE is a size
1373 				 * of it. The vDSO page is above.
1374 				 */
1375 				if (e_start == p->p_sysent->sv_shared_page_base +
1376 				    PAGE_SIZE)
1377 					name = vdso_str;
1378 				if (e_end == p->p_sysent->sv_usrstack)
1379 					name = stack_str;
1380 			}
1381 		}
1382 
1383 		/*
1384 		 * format:
1385 		 *  start, end, access, offset, major, minor, inode, name.
1386 		 */
1387 		error = sbuf_printf(sb, l_map_str,
1388 		    (u_long)e_start, (u_long)e_end,
1389 		    (e_prot & VM_PROT_READ)?"r":"-",
1390 		    (e_prot & VM_PROT_WRITE)?"w":"-",
1391 		    (e_prot & VM_PROT_EXECUTE)?"x":"-",
1392 		    private ? "p" : "s",
1393 		    (u_long)off,
1394 		    0,
1395 		    0,
1396 		    (u_long)ino,
1397 		    *name ? "     " : " ",
1398 		    name
1399 		    );
1400 		if (freename)
1401 			free(freename, M_TEMP);
1402 		vm_map_lock_read(map);
1403 		if (error == -1) {
1404 			error = 0;
1405 			break;
1406 		}
1407 		if (last_timestamp != map->timestamp) {
1408 			/*
1409 			 * Look again for the entry because the map was
1410 			 * modified while it was unlocked.  Specifically,
1411 			 * the entry may have been clipped, merged, or deleted.
1412 			 */
1413 			vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
1414 			entry = tmp_entry;
1415 		}
1416 	}
1417 	vm_map_unlock_read(map);
1418 	proc_vmspace_unref(td, p, PRVM_CHECK_DEBUG | PRVM_BLOCK_EXEC, vm);
1419 
1420 	return (error);
1421 }
1422 
1423 /*
1424  * Filler function for proc/pid/mem
1425  */
1426 static int
1427 linprocfs_doprocmem(PFS_FILL_ARGS)
1428 {
1429 	ssize_t resid;
1430 	int error;
1431 
1432 	resid = uio->uio_resid;
1433 	error = procfs_doprocmem(PFS_FILL_ARGNAMES);
1434 
1435 	if (uio->uio_rw == UIO_READ && resid != uio->uio_resid)
1436 		return (0);
1437 
1438 	if (error == EFAULT)
1439 		error = EIO;
1440 
1441 	return (error);
1442 }
1443 
1444 /*
1445  * Filler function for proc/net/dev
1446  */
1447 static int
1448 linprocfs_donetdev_cb(if_t ifp, void *arg)
1449 {
1450 	char ifname[LINUX_IFNAMSIZ];
1451 	struct sbuf *sb = arg;
1452 
1453 	if (ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname)) <= 0)
1454 		return (ENODEV);
1455 
1456 	sbuf_printf(sb, "%6.6s: ", ifname);
1457 	sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
1458 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IBYTES),
1459 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IPACKETS),
1460 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IERRORS),
1461 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IQDROPS),
1462 						/* rx_missed_errors */
1463 	    0UL,				/* rx_fifo_errors */
1464 	    0UL,				/* rx_length_errors +
1465 						 * rx_over_errors +
1466 						 * rx_crc_errors +
1467 						 * rx_frame_errors */
1468 	    0UL,				/* rx_compressed */
1469 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IMCASTS));
1470 						/* XXX-BZ rx only? */
1471 	sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
1472 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OBYTES),
1473 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OPACKETS),
1474 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OERRORS),
1475 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OQDROPS),
1476 	    0UL,				/* tx_fifo_errors */
1477 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_COLLISIONS),
1478 	    0UL,				/* tx_carrier_errors +
1479 						 * tx_aborted_errors +
1480 						 * tx_window_errors +
1481 						 * tx_heartbeat_errors*/
1482 	    0UL);				/* tx_compressed */
1483 	return (0);
1484 }
1485 
1486 static int
1487 linprocfs_donetdev(PFS_FILL_ARGS)
1488 {
1489 	struct epoch_tracker et;
1490 
1491 	sbuf_printf(sb, "%6s|%58s|%s\n"
1492 	    "%6s|%58s|%58s\n",
1493 	    "Inter-", "   Receive", "  Transmit",
1494 	    " face",
1495 	    "bytes    packets errs drop fifo frame compressed multicast",
1496 	    "bytes    packets errs drop fifo colls carrier compressed");
1497 
1498 	CURVNET_SET(TD_TO_VNET(curthread));
1499 	NET_EPOCH_ENTER(et);
1500 	if_foreach(linprocfs_donetdev_cb, sb);
1501 	NET_EPOCH_EXIT(et);
1502 	CURVNET_RESTORE();
1503 
1504 	return (0);
1505 }
1506 
1507 struct walkarg {
1508 	struct sbuf *sb;
1509 };
1510 
1511 static int
1512 linux_route_print(struct rtentry *rt, void *vw)
1513 {
1514 #ifdef INET
1515 	struct walkarg *w = vw;
1516 	struct route_nhop_data rnd;
1517 	struct in_addr dst, mask;
1518 	struct nhop_object *nh;
1519 	char ifname[16];
1520 	uint32_t scopeid = 0;
1521 	uint32_t gw = 0;
1522 	uint32_t linux_flags = 0;
1523 
1524 	rt_get_inet_prefix_pmask(rt, &dst, &mask, &scopeid);
1525 
1526 	rt_get_rnd(rt, &rnd);
1527 
1528 	/* select only first route in case of multipath */
1529 	nh = nhop_select_func(rnd.rnd_nhop, 0);
1530 
1531 	if (ifname_bsd_to_linux_ifp(nh->nh_ifp, ifname, sizeof(ifname)) <= 0)
1532 		return (ENODEV);
1533 
1534 	gw = (nh->nh_flags & NHF_GATEWAY)
1535 		? nh->gw4_sa.sin_addr.s_addr : 0;
1536 
1537 	linux_flags = RTF_UP |
1538 		(nhop_get_rtflags(nh) & (RTF_GATEWAY | RTF_HOST));
1539 
1540 	sbuf_printf(w->sb,
1541 		"%s\t"
1542 		"%08X\t%08X\t%04X\t"
1543 		"%d\t%u\t%d\t"
1544 		"%08X\t%d\t%u\t%u",
1545 		ifname,
1546 		dst.s_addr, gw, linux_flags,
1547 		0, 0, rnd.rnd_weight,
1548 		mask.s_addr, nh->nh_mtu, 0, 0);
1549 
1550 	sbuf_printf(w->sb, "\n\n");
1551 #endif
1552 	return (0);
1553 }
1554 
1555 /*
1556  * Filler function for proc/net/route
1557  */
1558 static int
1559 linprocfs_donetroute(PFS_FILL_ARGS)
1560 {
1561 	struct epoch_tracker et;
1562 	struct walkarg w = {
1563 		.sb = sb
1564 	};
1565 	uint32_t fibnum = curthread->td_proc->p_fibnum;
1566 
1567 	sbuf_printf(w.sb, "%-127s\n", "Iface\tDestination\tGateway "
1568                "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1569                "\tWindow\tIRTT");
1570 
1571 	CURVNET_SET(TD_TO_VNET(curthread));
1572 	NET_EPOCH_ENTER(et);
1573 	rib_walk(fibnum, AF_INET, false, linux_route_print, &w);
1574 	NET_EPOCH_EXIT(et);
1575 	CURVNET_RESTORE();
1576 
1577 	return (0);
1578 }
1579 
1580 /*
1581  * Filler function for proc/sys/kernel/osrelease
1582  */
1583 static int
1584 linprocfs_doosrelease(PFS_FILL_ARGS)
1585 {
1586 	char osrelease[LINUX_MAX_UTSNAME];
1587 
1588 	linux_get_osrelease(td, osrelease);
1589 	sbuf_printf(sb, "%s\n", osrelease);
1590 
1591 	return (0);
1592 }
1593 
1594 /*
1595  * Filler function for proc/sys/kernel/ostype
1596  */
1597 static int
1598 linprocfs_doostype(PFS_FILL_ARGS)
1599 {
1600 	char osname[LINUX_MAX_UTSNAME];
1601 
1602 	linux_get_osname(td, osname);
1603 	sbuf_printf(sb, "%s\n", osname);
1604 
1605 	return (0);
1606 }
1607 
1608 /*
1609  * Filler function for proc/sys/kernel/version
1610  */
1611 static int
1612 linprocfs_doosbuild(PFS_FILL_ARGS)
1613 {
1614 
1615 	linprocfs_osbuild(td, sb);
1616 	sbuf_cat(sb, "\n");
1617 	return (0);
1618 }
1619 
1620 /*
1621  * Filler function for proc/sys/kernel/msgmax
1622  */
1623 static int
1624 linprocfs_domsgmax(PFS_FILL_ARGS)
1625 {
1626 
1627 	sbuf_printf(sb, "%d\n", msginfo.msgmax);
1628 	return (0);
1629 }
1630 
1631 /*
1632  * Filler function for proc/sys/kernel/msgmni
1633  */
1634 static int
1635 linprocfs_domsgmni(PFS_FILL_ARGS)
1636 {
1637 
1638 	sbuf_printf(sb, "%d\n", msginfo.msgmni);
1639 	return (0);
1640 }
1641 
1642 /*
1643  * Filler function for proc/sys/kernel/msgmnb
1644  */
1645 static int
1646 linprocfs_domsgmnb(PFS_FILL_ARGS)
1647 {
1648 
1649 	sbuf_printf(sb, "%d\n", msginfo.msgmnb);
1650 	return (0);
1651 }
1652 
1653 /*
1654  * Filler function for proc/sys/kernel/ngroups_max
1655  *
1656  * Note that in Linux it defaults to 65536, not 1023.
1657  */
1658 static int
1659 linprocfs_dongroups_max(PFS_FILL_ARGS)
1660 {
1661 
1662 	sbuf_printf(sb, "%d\n", ngroups_max);
1663 	return (0);
1664 }
1665 
1666 /*
1667  * Filler function for proc/sys/kernel/pid_max
1668  */
1669 static int
1670 linprocfs_dopid_max(PFS_FILL_ARGS)
1671 {
1672 
1673 	sbuf_printf(sb, "%i\n", PID_MAX);
1674 	return (0);
1675 }
1676 
1677 /*
1678  * Filler function for proc/sys/kernel/sem
1679  */
1680 static int
1681 linprocfs_dosem(PFS_FILL_ARGS)
1682 {
1683 
1684 	sbuf_printf(sb, "%d %d %d %d\n", seminfo.semmsl, seminfo.semmns,
1685 	    seminfo.semopm, seminfo.semmni);
1686 	return (0);
1687 }
1688 
1689 /*
1690  * Filler function for proc/sys/kernel/shmall
1691  */
1692 static int
1693 linprocfs_doshmall(PFS_FILL_ARGS)
1694 {
1695 
1696 	sbuf_printf(sb, "%lu\n", shminfo.shmall);
1697 	return (0);
1698 }
1699 
1700 /*
1701  * Filler function for proc/sys/kernel/shmmax
1702  */
1703 static int
1704 linprocfs_doshmmax(PFS_FILL_ARGS)
1705 {
1706 
1707 	sbuf_printf(sb, "%lu\n", shminfo.shmmax);
1708 	return (0);
1709 }
1710 
1711 /*
1712  * Filler function for proc/sys/kernel/shmmni
1713  */
1714 static int
1715 linprocfs_doshmmni(PFS_FILL_ARGS)
1716 {
1717 
1718 	sbuf_printf(sb, "%lu\n", shminfo.shmmni);
1719 	return (0);
1720 }
1721 
1722 /*
1723  * Filler function for proc/sys/kernel/tainted
1724  */
1725 static int
1726 linprocfs_dotainted(PFS_FILL_ARGS)
1727 {
1728 
1729 	sbuf_printf(sb, "0\n");
1730 	return (0);
1731 }
1732 
1733 /*
1734  * Filler function for proc/sys/kernel/threads-max
1735  */
1736 static int
1737 linprocfs_dothreads_max(PFS_FILL_ARGS)
1738 {
1739 	int res, error;
1740 	size_t size = sizeof(res);
1741 
1742 	error = kernel_sysctlbyname(curthread, "kern.maxproc",
1743 	    &res, &size, NULL, 0, 0, 0);
1744 	if (error != 0)
1745 		return (error);
1746 	sbuf_printf(sb, "%d\n", res);
1747 	return (0);
1748 }
1749 
1750 /*
1751  * Filler function for proc/sys/vm/min_free_kbytes
1752  *
1753  * This mirrors the approach in illumos to return zero for reads. Effectively,
1754  * it says, no memory is kept in reserve for "atomic allocations". This class
1755  * of allocation can be used at times when a thread cannot be suspended.
1756  */
1757 static int
1758 linprocfs_dominfree(PFS_FILL_ARGS)
1759 {
1760 
1761 	sbuf_printf(sb, "%d\n", 0);
1762 	return (0);
1763 }
1764 
1765 /*
1766  * Filler function for proc/scsi/device_info
1767  */
1768 static int
1769 linprocfs_doscsidevinfo(PFS_FILL_ARGS)
1770 {
1771 
1772 	return (0);
1773 }
1774 
1775 /*
1776  * Filler function for proc/scsi/scsi
1777  */
1778 static int
1779 linprocfs_doscsiscsi(PFS_FILL_ARGS)
1780 {
1781 
1782 	return (0);
1783 }
1784 
1785 /*
1786  * Filler function for proc/devices
1787  */
1788 static int
1789 linprocfs_dodevices(PFS_FILL_ARGS)
1790 {
1791 	char *char_devices;
1792 	sbuf_printf(sb, "Character devices:\n");
1793 
1794 	char_devices = linux_get_char_devices();
1795 	sbuf_printf(sb, "%s", char_devices);
1796 	linux_free_get_char_devices(char_devices);
1797 
1798 	sbuf_printf(sb, "\nBlock devices:\n");
1799 
1800 	return (0);
1801 }
1802 
1803 /*
1804  * Filler function for proc/cmdline
1805  */
1806 static int
1807 linprocfs_docmdline(PFS_FILL_ARGS)
1808 {
1809 
1810 	sbuf_printf(sb, "BOOT_IMAGE=%s", kernelname);
1811 	sbuf_printf(sb, " ro root=302\n");
1812 	return (0);
1813 }
1814 
1815 /*
1816  * Filler function for proc/filesystems
1817  */
1818 static int
1819 linprocfs_dofilesystems(PFS_FILL_ARGS)
1820 {
1821 	struct vfsconf *vfsp;
1822 
1823 	vfsconf_slock();
1824 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
1825 		if (vfsp->vfc_flags & VFCF_SYNTHETIC)
1826 			sbuf_printf(sb, "nodev");
1827 		sbuf_printf(sb, "\t%s\n", vfsp->vfc_name);
1828 	}
1829 	vfsconf_sunlock();
1830 	return(0);
1831 }
1832 
1833 /*
1834  * Filler function for proc/modules
1835  */
1836 static int
1837 linprocfs_domodules(PFS_FILL_ARGS)
1838 {
1839 #if 0
1840 	struct linker_file *lf;
1841 
1842 	TAILQ_FOREACH(lf, &linker_files, link) {
1843 		sbuf_printf(sb, "%-20s%8lu%4d\n", lf->filename,
1844 		    (unsigned long)lf->size, lf->refs);
1845 	}
1846 #endif
1847 	return (0);
1848 }
1849 
1850 /*
1851  * Filler function for proc/pid/fd
1852  */
1853 static int
1854 linprocfs_dofdescfs(PFS_FILL_ARGS)
1855 {
1856 
1857 	if (p == curproc)
1858 		sbuf_printf(sb, "/dev/fd");
1859 	else
1860 		sbuf_printf(sb, "unknown");
1861 	return (0);
1862 }
1863 
1864 /*
1865  * Filler function for proc/pid/limits
1866  */
1867 static const struct linux_rlimit_ident {
1868 	const char	*desc;
1869 	const char	*unit;
1870 	unsigned int	rlim_id;
1871 } linux_rlimits_ident[] = {
1872 	{ "Max cpu time",	"seconds",	RLIMIT_CPU },
1873 	{ "Max file size", 	"bytes",	RLIMIT_FSIZE },
1874 	{ "Max data size",	"bytes", 	RLIMIT_DATA },
1875 	{ "Max stack size",	"bytes", 	RLIMIT_STACK },
1876 	{ "Max core file size",  "bytes",	RLIMIT_CORE },
1877 	{ "Max resident set",	"bytes",	RLIMIT_RSS },
1878 	{ "Max processes",	"processes",	RLIMIT_NPROC },
1879 	{ "Max open files",	"files",	RLIMIT_NOFILE },
1880 	{ "Max locked memory",	"bytes",	RLIMIT_MEMLOCK },
1881 	{ "Max address space",	"bytes",	RLIMIT_AS },
1882 	{ "Max file locks",	"locks",	LINUX_RLIMIT_LOCKS },
1883 	{ "Max pending signals", "signals",	LINUX_RLIMIT_SIGPENDING },
1884 	{ "Max msgqueue size",	"bytes",	LINUX_RLIMIT_MSGQUEUE },
1885 	{ "Max nice priority", 		"",	LINUX_RLIMIT_NICE },
1886 	{ "Max realtime priority",	"",	LINUX_RLIMIT_RTPRIO },
1887 	{ "Max realtime timeout",	"us",	LINUX_RLIMIT_RTTIME },
1888 	{ 0, 0, 0 }
1889 };
1890 
1891 static int
1892 linprocfs_doproclimits(PFS_FILL_ARGS)
1893 {
1894 	const struct linux_rlimit_ident *li;
1895 	struct plimit *limp;
1896 	struct rlimit rl;
1897 	ssize_t size;
1898 	int res, error;
1899 
1900 	error = 0;
1901 
1902 	PROC_LOCK(p);
1903 	limp = lim_hold(p->p_limit);
1904 	PROC_UNLOCK(p);
1905 	size = sizeof(res);
1906 	sbuf_printf(sb, "%-26s%-21s%-21s%-21s\n", "Limit", "Soft Limit",
1907 			"Hard Limit", "Units");
1908 	for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
1909 		switch (li->rlim_id)
1910 		{
1911 		case LINUX_RLIMIT_LOCKS:
1912 			/* FALLTHROUGH */
1913 		case LINUX_RLIMIT_RTTIME:
1914 			rl.rlim_cur = RLIM_INFINITY;
1915 			break;
1916 		case LINUX_RLIMIT_SIGPENDING:
1917 			error = kernel_sysctlbyname(td,
1918 			    "kern.sigqueue.max_pending_per_proc",
1919 			    &res, &size, 0, 0, 0, 0);
1920 			if (error != 0)
1921 				continue;
1922 			rl.rlim_cur = res;
1923 			rl.rlim_max = res;
1924 			break;
1925 		case LINUX_RLIMIT_MSGQUEUE:
1926 			error = kernel_sysctlbyname(td,
1927 			    "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1928 			if (error != 0)
1929 				continue;
1930 			rl.rlim_cur = res;
1931 			rl.rlim_max = res;
1932 			break;
1933 		case LINUX_RLIMIT_NICE:
1934 			/* FALLTHROUGH */
1935 		case LINUX_RLIMIT_RTPRIO:
1936 			rl.rlim_cur = 0;
1937 			rl.rlim_max = 0;
1938 			break;
1939 		default:
1940 			rl = limp->pl_rlimit[li->rlim_id];
1941 			break;
1942 		}
1943 		if (rl.rlim_cur == RLIM_INFINITY)
1944 			sbuf_printf(sb, "%-26s%-21s%-21s%-10s\n",
1945 			    li->desc, "unlimited", "unlimited", li->unit);
1946 		else
1947 			sbuf_printf(sb, "%-26s%-21llu%-21llu%-10s\n",
1948 			    li->desc, (unsigned long long)rl.rlim_cur,
1949 			    (unsigned long long)rl.rlim_max, li->unit);
1950 	}
1951 
1952 	lim_free(limp);
1953 	return (0);
1954 }
1955 
1956 /*
1957  * The point of the following two functions is to work around
1958  * an assertion in Chromium; see kern/240991 for details.
1959  */
1960 static int
1961 linprocfs_dotaskattr(PFS_ATTR_ARGS)
1962 {
1963 
1964 	vap->va_nlink = 3;
1965 	return (0);
1966 }
1967 
1968 /*
1969  * Filler function for proc/<pid>/task/.dummy
1970  */
1971 static int
1972 linprocfs_dotaskdummy(PFS_FILL_ARGS)
1973 {
1974 
1975 	return (0);
1976 }
1977 
1978 /*
1979  * Filler function for proc/sys/kernel/random/uuid
1980  */
1981 static int
1982 linprocfs_douuid(PFS_FILL_ARGS)
1983 {
1984 	struct uuid uuid;
1985 
1986 	kern_uuidgen(&uuid, 1);
1987 	sbuf_printf_uuid(sb, &uuid);
1988 	sbuf_printf(sb, "\n");
1989 	return(0);
1990 }
1991 
1992 /*
1993  * Filler function for proc/sys/kernel/random/boot_id
1994  */
1995 static int
1996 linprocfs_doboot_id(PFS_FILL_ARGS)
1997 {
1998        static bool firstboot = 1;
1999        static struct uuid uuid;
2000 
2001        if (firstboot) {
2002                kern_uuidgen(&uuid, 1);
2003                firstboot = 0;
2004        }
2005        sbuf_printf_uuid(sb, &uuid);
2006        sbuf_printf(sb, "\n");
2007        return(0);
2008 }
2009 
2010 /*
2011  * Filler function for proc/pid/auxv
2012  */
2013 static int
2014 linprocfs_doauxv(PFS_FILL_ARGS)
2015 {
2016 	struct sbuf *asb;
2017 	off_t buflen, resid;
2018 	int error;
2019 
2020 	/*
2021 	 * Mimic linux behavior and pass only processes with usermode
2022 	 * address space as valid. Return zero silently otherwise.
2023 	 */
2024 	if (p->p_vmspace == &vmspace0)
2025 		return (0);
2026 
2027 	if (uio->uio_resid == 0)
2028 		return (0);
2029 	if (uio->uio_offset < 0 || uio->uio_resid < 0)
2030 		return (EINVAL);
2031 
2032 	asb = sbuf_new_auto();
2033 	if (asb == NULL)
2034 		return (ENOMEM);
2035 	error = proc_getauxv(td, p, asb);
2036 	if (error != 0)
2037 		goto out;
2038 	error = sbuf_finish(asb);
2039 	if (error != 0)
2040 		goto out;
2041 
2042 	resid = sbuf_len(asb) - uio->uio_offset;
2043 	if (resid > uio->uio_resid)
2044 		buflen = uio->uio_resid;
2045 	else
2046 		buflen = resid;
2047 	if (buflen > IOSIZE_MAX) {
2048 		error = EINVAL;
2049 		goto out;
2050 	}
2051 	if (buflen > maxphys)
2052 		buflen = maxphys;
2053 	if (resid > 0)
2054 		error = uiomove(sbuf_data(asb) + uio->uio_offset, buflen, uio);
2055 out:
2056 	sbuf_delete(asb);
2057 	return (error);
2058 }
2059 
2060 /*
2061  * Filler function for proc/self/oom_score_adj
2062  */
2063 static int
2064 linprocfs_do_oom_score_adj(PFS_FILL_ARGS)
2065 {
2066 	struct linux_pemuldata *pem;
2067 	long oom;
2068 
2069 	pem = pem_find(p);
2070 	if (pem == NULL || uio == NULL)
2071 		return (EOPNOTSUPP);
2072 	if (uio->uio_rw == UIO_READ) {
2073 		sbuf_printf(sb, "%d\n", pem->oom_score_adj);
2074 	} else {
2075 		sbuf_trim(sb);
2076 		sbuf_finish(sb);
2077 		oom = strtol(sbuf_data(sb), NULL, 10);
2078 		if (oom < LINUX_OOM_SCORE_ADJ_MIN ||
2079 		    oom > LINUX_OOM_SCORE_ADJ_MAX)
2080 			return (EINVAL);
2081 		pem->oom_score_adj = oom;
2082 	}
2083 	return (0);
2084 }
2085 
2086 /*
2087  * Filler function for proc/sys/vm/max_map_count
2088  *
2089  * Maximum number of active map areas, on Linux this limits the number
2090  * of vmaps per mm struct. We don't limit mappings, return a suitable
2091  * large value.
2092  */
2093 static int
2094 linprocfs_domax_map_cnt(PFS_FILL_ARGS)
2095 {
2096 
2097 	sbuf_printf(sb, "%d\n", INT32_MAX);
2098 	return (0);
2099 }
2100 
2101 /*
2102  * Filler function for proc/sysvipc/msg
2103  */
2104 static int
2105 linprocfs_dosysvipc_msg(PFS_FILL_ARGS)
2106 {
2107 	struct msqid_kernel *msqids;
2108 	size_t id, size;
2109 	int error;
2110 
2111 	sbuf_printf(sb,
2112 	    "%10s %10s %4s  %10s %10s %5s %5s %5s %5s %5s %5s %10s %10s %10s\n",
2113 	    "key", "msqid", "perms", "cbytes", "qnum", "lspid", "lrpid",
2114 	    "uid", "gid", "cuid", "cgid", "stime", "rtime", "ctime");
2115 
2116 	error = kern_get_msqids(curthread, &msqids, &size);
2117 	if (error != 0)
2118 		return (error);
2119 
2120 	for (id = 0; id < size; id++) {
2121 		if (msqids[id].u.msg_qbytes == 0)
2122 			continue;
2123 		sbuf_printf(sb,
2124 		    "%10d %10zu  %4o  %10lu %10lu %5u %5u %5u %5u %5u %5u %jd %jd %jd\n",
2125 		    (int)msqids[id].u.msg_perm.key,
2126 		    IXSEQ_TO_IPCID(id, msqids[id].u.msg_perm),
2127 		    msqids[id].u.msg_perm.mode,
2128 		    msqids[id].u.msg_cbytes,
2129 		    msqids[id].u.msg_qnum,
2130 		    msqids[id].u.msg_lspid,
2131 		    msqids[id].u.msg_lrpid,
2132 		    msqids[id].u.msg_perm.uid,
2133 		    msqids[id].u.msg_perm.gid,
2134 		    msqids[id].u.msg_perm.cuid,
2135 		    msqids[id].u.msg_perm.cgid,
2136 		    (intmax_t)msqids[id].u.msg_stime,
2137 		    (intmax_t)msqids[id].u.msg_rtime,
2138 		    (intmax_t)msqids[id].u.msg_ctime);
2139 	}
2140 
2141 	free(msqids, M_TEMP);
2142 	return (0);
2143 }
2144 
2145 /*
2146  * Filler function for proc/sysvipc/sem
2147  */
2148 static int
2149 linprocfs_dosysvipc_sem(PFS_FILL_ARGS)
2150 {
2151 	struct semid_kernel *semids;
2152 	size_t id, size;
2153 	int error;
2154 
2155 	sbuf_printf(sb, "%10s %10s %4s %10s %5s %5s %5s %5s %10s %10s\n",
2156 	    "key", "semid", "perms", "nsems", "uid", "gid", "cuid", "cgid",
2157 	    "otime", "ctime");
2158 
2159 	error = kern_get_sema(curthread, &semids, &size);
2160 	if (error != 0)
2161 		return (error);
2162 
2163 	for (id = 0; id < size; id++) {
2164 		if ((semids[id].u.sem_perm.mode & SEM_ALLOC) == 0)
2165 			continue;
2166 		sbuf_printf(sb,
2167 		    "%10d %10zu  %4o %10u %5u %5u %5u %5u %jd %jd\n",
2168 		    (int)semids[id].u.sem_perm.key,
2169 		    IXSEQ_TO_IPCID(id, semids[id].u.sem_perm),
2170 		    semids[id].u.sem_perm.mode,
2171 		    semids[id].u.sem_nsems,
2172 		    semids[id].u.sem_perm.uid,
2173 		    semids[id].u.sem_perm.gid,
2174 		    semids[id].u.sem_perm.cuid,
2175 		    semids[id].u.sem_perm.cgid,
2176 		    (intmax_t)semids[id].u.sem_otime,
2177 		    (intmax_t)semids[id].u.sem_ctime);
2178 	}
2179 
2180 	free(semids, M_TEMP);
2181 	return (0);
2182 }
2183 
2184 /*
2185  * Filler function for proc/sysvipc/shm
2186  */
2187 static int
2188 linprocfs_dosysvipc_shm(PFS_FILL_ARGS)
2189 {
2190 	struct shmid_kernel *shmids;
2191 	size_t id, size;
2192 	int error;
2193 
2194 	sbuf_printf(sb,
2195 	    "%10s %10s %s %21s %5s %5s %5s %5s %5s %5s %5s %10s %10s %10s %21s %21s\n",
2196 	    "key", "shmid", "perms", "size", "cpid", "lpid", "nattch", "uid",
2197 	    "gid", "cuid", "cgid", "atime", "dtime", "ctime", "rss", "swap");
2198 
2199 	error = kern_get_shmsegs(curthread, &shmids, &size);
2200 	if (error != 0)
2201 		return (error);
2202 
2203 	for (id = 0; id < size; id++) {
2204 		if ((shmids[id].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0)
2205 			continue;
2206 		sbuf_printf(sb,
2207 		    "%10d %10zu  %4o %21zu %5u %5u  %5u %5u %5u %5u %5u %jd %jd %jd %21d %21d\n",
2208 		    (int)shmids[id].u.shm_perm.key,
2209 		    IXSEQ_TO_IPCID(id, shmids[id].u.shm_perm),
2210 		    shmids[id].u.shm_perm.mode,
2211 		    shmids[id].u.shm_segsz,
2212 		    shmids[id].u.shm_cpid,
2213 		    shmids[id].u.shm_lpid,
2214 		    shmids[id].u.shm_nattch,
2215 		    shmids[id].u.shm_perm.uid,
2216 		    shmids[id].u.shm_perm.gid,
2217 		    shmids[id].u.shm_perm.cuid,
2218 		    shmids[id].u.shm_perm.cgid,
2219 		    (intmax_t)shmids[id].u.shm_atime,
2220 		    (intmax_t)shmids[id].u.shm_dtime,
2221 		    (intmax_t)shmids[id].u.shm_ctime,
2222 		    0, 0);	/* XXX rss & swp are not supported */
2223 	}
2224 
2225 	free(shmids, M_TEMP);
2226 	return (0);
2227 }
2228 
2229 /*
2230  * Filler function for proc/sys/fs/file-max
2231  */
2232 static int
2233 linprocfs_dofile_max(PFS_FILL_ARGS)
2234 {
2235 	int res, error;
2236 	size_t size = sizeof(res);
2237 
2238 	error = kernel_sysctlbyname(curthread, "kern.maxfiles",
2239 	    &res, &size, NULL, 0, 0, 0);
2240 	if (error != 0)
2241 		return (error);
2242 	sbuf_printf(sb, "%d\n", res);
2243 	return (0);
2244 }
2245 
2246 /*
2247  * Filler function for proc/sys/fs/file-nr
2248  */
2249 static int
2250 linprocfs_dofile_nr(PFS_FILL_ARGS)
2251 {
2252 	int openfiles, maxfiles, error;
2253 	size_t size;
2254 
2255 	size = sizeof(openfiles);
2256 	error = kernel_sysctlbyname(curthread, "kern.openfiles",
2257 	    &openfiles, &size, NULL, 0, 0, 0);
2258 	if (error != 0)
2259 		return (error);
2260 	size = sizeof(maxfiles);
2261 	error = kernel_sysctlbyname(curthread, "kern.maxfiles",
2262 	    &maxfiles, &size, NULL, 0, 0, 0);
2263 	if (error != 0)
2264 		return (error);
2265 	/*
2266 	 * From Linux's proc_sys_fs(5):
2267 	 * the "free file handles" value is always zero.
2268 	 */
2269 	sbuf_printf(sb, "%d\t0\t%d\n", openfiles, maxfiles);
2270 	return (0);
2271 }
2272 
2273 /*
2274  * Filler function for proc/sys/fs/nr_open
2275  */
2276 static int
2277 linprocfs_donr_open(PFS_FILL_ARGS)
2278 {
2279 	int res, error;
2280 	size_t size = sizeof(res);
2281 
2282 	error = kernel_sysctlbyname(curthread, "kern.maxfilesperproc",
2283 	    &res, &size, NULL, 0, 0, 0);
2284 	if (error != 0)
2285 		return (error);
2286 	sbuf_printf(sb, "%d\n", res);
2287 	return (0);
2288 }
2289 
2290 /*
2291  * Filler function for proc/sys/fs/overflowuid
2292  */
2293 static int
2294 linprocfs_dooverflowuid(PFS_FILL_ARGS)
2295 {
2296 	sbuf_printf(sb, "%u\n", UID_NOBODY);
2297 	return (0);
2298 }
2299 
2300 /*
2301  * Filler function for proc/sys/fs/overflowgid
2302  */
2303 static int
2304 linprocfs_dooverflowgid(PFS_FILL_ARGS)
2305 {
2306 	sbuf_printf(sb, "%u\n", GID_NOGROUP);
2307 	return (0);
2308 }
2309 
2310 /*
2311  * Filler function for proc/sys/fs/suid_dumpable
2312  */
2313 static int
2314 linprocfs_dosuid_dumpable(PFS_FILL_ARGS)
2315 {
2316 	int res, error;
2317 	size_t size = sizeof(res);
2318 
2319 	error = kernel_sysctlbyname(curthread, "kern.sugid_coredump",
2320 	    &res, &size, NULL, 0, 0, 0);
2321 	if (error != 0)
2322 		return (error);
2323 	sbuf_printf(sb, "%d\n", res ? 1 : 0);
2324 	return (0);
2325 }
2326 
2327 /*
2328  * Filler function for proc/sys/fs/protected_hardlinks
2329  */
2330 static int
2331 linprocfs_doprotected_hardlinks(PFS_FILL_ARGS)
2332 {
2333 	int res, error;
2334 	size_t size = sizeof(res);
2335 
2336 	error = kernel_sysctlbyname(curthread,
2337 	    "security.bsd.hardlink_check_uid",
2338 	    &res, &size, NULL, 0, 0, 0);
2339 	if (error != 0)
2340 		return (error);
2341 	sbuf_printf(sb, "%d\n", res ? 1 : 0);
2342 	return (0);
2343 }
2344 
2345 static int
2346 linprocfs_doinotify(const char *sysctl, PFS_FILL_ARGS)
2347 {
2348 	size_t size;
2349 	int error, val;
2350 
2351 	if (uio->uio_rw == UIO_READ) {
2352 		size = sizeof(val);
2353 		error = kernel_sysctlbyname(curthread,
2354 		    __DECONST(void *, sysctl), &val, &size, NULL, 0, 0, 0);
2355 		if (error == 0)
2356 			sbuf_printf(sb, "%d\n", val);
2357 	} else {
2358 		char *endp, *newval;
2359 		long vall;
2360 
2361 		sbuf_trim(sb);
2362 		sbuf_finish(sb);
2363 		newval = sbuf_data(sb);
2364 		vall = strtol(newval, &endp, 10);
2365 		if (vall < 0 || vall > INT_MAX || endp == newval ||
2366 		    *endp != '\0')
2367 			return (EINVAL);
2368 		val = (int)vall;
2369 		error = kernel_sysctlbyname(curthread,
2370 		    __DECONST(void *, sysctl), NULL, NULL,
2371 		    &val, sizeof(val), 0, 0);
2372 	}
2373 	return (error);
2374 }
2375 
2376 /*
2377  * Filler function for proc/sys/fs/inotify/max_queued_events
2378  */
2379 static int
2380 linprocfs_doinotify_max_queued_events(PFS_FILL_ARGS)
2381 {
2382 	return (linprocfs_doinotify("vfs.inotify.max_queued_events",
2383 	    PFS_FILL_ARGNAMES));
2384 }
2385 
2386 /*
2387  * Filler function for proc/sys/fs/inotify/max_user_instances
2388  */
2389 static int
2390 linprocfs_doinotify_max_user_instances(PFS_FILL_ARGS)
2391 {
2392 	return (linprocfs_doinotify("vfs.inotify.max_user_instances",
2393 	    PFS_FILL_ARGNAMES));
2394 }
2395 
2396 /*
2397  * Filler function for proc/sys/fs/inotify/max_user_watches
2398  */
2399 static int
2400 linprocfs_doinotify_max_user_watches(PFS_FILL_ARGS)
2401 {
2402 	return (linprocfs_doinotify("vfs.inotify.max_user_watches",
2403 	    PFS_FILL_ARGNAMES));
2404 }
2405 
2406 /*
2407  * Filler function for proc/sys/fs/mqueue/msg_default
2408  */
2409 static int
2410 linprocfs_domqueue_msg_default(PFS_FILL_ARGS)
2411 {
2412 	int res, error;
2413 	size_t size = sizeof(res);
2414 
2415 	error = kernel_sysctlbyname(curthread, "kern.mqueue.default_maxmsg",
2416 	    &res, &size, NULL, 0, 0, 0);
2417 	if (error != 0)
2418 		return (error);
2419 
2420 	sbuf_printf(sb, "%d\n", res);
2421 	return (0);
2422 }
2423 
2424 /*
2425  * Filler function for proc/sys/fs/mqueue/msgsize_default
2426  */
2427 static int
2428 linprocfs_domqueue_msgsize_default(PFS_FILL_ARGS)
2429 {
2430 	int res, error;
2431 	size_t size = sizeof(res);
2432 
2433 	error = kernel_sysctlbyname(curthread, "kern.mqueue.default_msgsize",
2434 	    &res, &size, NULL, 0, 0, 0);
2435 	if (error != 0)
2436 		return (error);
2437 
2438 	sbuf_printf(sb, "%d\n", res);
2439 	return (0);
2440 
2441 }
2442 
2443 /*
2444  * Filler function for proc/sys/fs/mqueue/msg_max
2445  */
2446 static int
2447 linprocfs_domqueue_msg_max(PFS_FILL_ARGS)
2448 {
2449 	int res, error;
2450 	size_t size = sizeof(res);
2451 
2452 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmsg",
2453 	    &res, &size, NULL, 0, 0, 0);
2454 	if (error != 0)
2455 		return (error);
2456 
2457 	sbuf_printf(sb, "%d\n", res);
2458 	return (0);
2459 }
2460 
2461 /*
2462  * Filler function for proc/sys/fs/mqueue/msgsize_max
2463  */
2464 static int
2465 linprocfs_domqueue_msgsize_max(PFS_FILL_ARGS)
2466 {
2467 	int res, error;
2468 	size_t size = sizeof(res);
2469 
2470 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmsgsize",
2471 	    &res, &size, NULL, 0, 0, 0);
2472 	if (error != 0)
2473 		return (error);
2474 
2475 	sbuf_printf(sb, "%d\n", res);
2476 	return (0);
2477 }
2478 
2479 /*
2480  * Filler function for proc/sys/fs/mqueue/queues_max
2481  */
2482 static int
2483 linprocfs_domqueue_queues_max(PFS_FILL_ARGS)
2484 {
2485 	int res, error;
2486 	size_t size = sizeof(res);
2487 
2488 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmq",
2489 	    &res, &size, NULL, 0, 0, 0);
2490 	if (error != 0)
2491 		return (error);
2492 
2493 	sbuf_printf(sb, "%d\n", res);
2494 	return (0);
2495 }
2496 
2497 /*
2498  * Constructor
2499  */
2500 static int
2501 linprocfs_init(PFS_INIT_ARGS)
2502 {
2503 	struct pfs_node *dir, *fs, *root, *sys;
2504 
2505 	root = pi->pi_root;
2506 
2507 	/* /proc/... */
2508 	pfs_create_file(root, NULL, "cmdline", &linprocfs_docmdline, NULL, NULL,
2509 	    NULL, PFS_RD);
2510 	pfs_create_file(root, NULL, "cpuinfo", &linprocfs_docpuinfo, NULL, NULL,
2511 	    NULL, PFS_RD);
2512 	pfs_create_file(root, NULL, "devices", &linprocfs_dodevices, NULL, NULL,
2513 	    NULL, PFS_RD);
2514 	pfs_create_file(root, NULL, "filesystems", &linprocfs_dofilesystems,
2515 	    NULL, NULL, NULL, PFS_RD);
2516 	pfs_create_file(root, NULL, "loadavg", &linprocfs_doloadavg, NULL, NULL,
2517 	    NULL, PFS_RD);
2518 	pfs_create_file(root, NULL, "meminfo", &linprocfs_domeminfo, NULL, NULL,
2519 	    NULL, PFS_RD);
2520 	pfs_create_file(root, NULL, "modules", &linprocfs_domodules, NULL, NULL,
2521 	    NULL, PFS_RD);
2522 	pfs_create_file(root, NULL, "mounts", &linprocfs_domtab, NULL, NULL,
2523 	    NULL, PFS_RD);
2524 	pfs_create_file(root, NULL, "mtab", &linprocfs_domtab, NULL, NULL, NULL,
2525 	    PFS_RD);
2526 	pfs_create_file(root, NULL, "partitions", &linprocfs_dopartitions, NULL,
2527 	    NULL, NULL, PFS_RD);
2528 	pfs_create_link(root, NULL, "self", &procfs_docurproc, NULL, NULL, NULL,
2529 	    0);
2530 	pfs_create_file(root, NULL, "stat", &linprocfs_dostat, NULL, NULL, NULL,
2531 	    PFS_RD);
2532 	pfs_create_file(root, NULL, "swaps", &linprocfs_doswaps, NULL, NULL,
2533 	    NULL, PFS_RD);
2534 	pfs_create_file(root, NULL, "uptime", &linprocfs_douptime, NULL, NULL,
2535 	    NULL, PFS_RD);
2536 	pfs_create_file(root, NULL, "version", &linprocfs_doversion, NULL, NULL,
2537 	    NULL, PFS_RD);
2538 
2539 	/* /proc/bus/... */
2540 	pfs_create_dir(root, &dir, "bus", NULL, NULL, NULL, 0);
2541 	pfs_create_dir(dir, &dir, "pci", NULL, NULL, NULL, 0);
2542 	pfs_create_dir(dir, &dir, "devices", NULL, NULL, NULL, 0);
2543 
2544 	/* /proc/net/... */
2545 	pfs_create_dir(root, &dir, "net", NULL, NULL, NULL, 0);
2546 	pfs_create_file(dir, NULL, "dev", &linprocfs_donetdev, NULL, NULL, NULL,
2547 	    PFS_RD);
2548 	pfs_create_file(dir, NULL, "route", &linprocfs_donetroute, NULL, NULL,
2549 	    NULL, PFS_RD);
2550 
2551 	/* /proc/<pid>/... */
2552 	pfs_create_dir(root, &dir, "pid", NULL, NULL, NULL, PFS_PROCDEP);
2553 	pfs_create_file(dir, NULL, "cmdline", &linprocfs_doproccmdline, NULL,
2554 	    NULL, NULL, PFS_RD);
2555 	pfs_create_link(dir, NULL, "cwd", &linprocfs_doproccwd, NULL, NULL,
2556 	    NULL, 0);
2557 	pfs_create_file(dir, NULL, "environ", &linprocfs_doprocenviron, NULL,
2558 	    &procfs_candebug, NULL, PFS_RD);
2559 	pfs_create_link(dir, NULL, "exe", &procfs_doprocfile, NULL,
2560 	    &procfs_notsystem, NULL, 0);
2561 	pfs_create_file(dir, NULL, "maps", &linprocfs_doprocmaps, NULL, NULL,
2562 	    NULL, PFS_RD | PFS_AUTODRAIN);
2563 	pfs_create_file(dir, NULL, "mem", &linprocfs_doprocmem, procfs_attr_rw,
2564 	    &procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
2565 	pfs_create_file(dir, NULL, "mountinfo", &linprocfs_doprocmountinfo,
2566 	    NULL, NULL, NULL, PFS_RD);
2567 	pfs_create_file(dir, NULL, "mounts", &linprocfs_domtab, NULL, NULL,
2568 	    NULL, PFS_RD);
2569 	pfs_create_link(dir, NULL, "root", &linprocfs_doprocroot, NULL, NULL,
2570 	    NULL, 0);
2571 	pfs_create_file(dir, NULL, "stat", &linprocfs_doprocstat, NULL, NULL,
2572 	    NULL, PFS_RD);
2573 	pfs_create_file(dir, NULL, "statm", &linprocfs_doprocstatm, NULL, NULL,
2574 	    NULL, PFS_RD);
2575 	pfs_create_file(dir, NULL, "status", &linprocfs_doprocstatus, NULL,
2576 	    NULL, NULL, PFS_RD);
2577 	pfs_create_link(dir, NULL, "fd", &linprocfs_dofdescfs, NULL, NULL, NULL,
2578 	    0);
2579 	pfs_create_file(dir, NULL, "auxv", &linprocfs_doauxv, NULL,
2580 	    &procfs_candebug, NULL, PFS_RD | PFS_RAWRD);
2581 	pfs_create_file(dir, NULL, "limits", &linprocfs_doproclimits, NULL,
2582 	    NULL, NULL, PFS_RD);
2583 	pfs_create_file(dir, NULL, "oom_score_adj", &linprocfs_do_oom_score_adj,
2584 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR);
2585 
2586 	/* /proc/<pid>/task/... */
2587 	pfs_create_dir(dir, &dir, "task", linprocfs_dotaskattr, NULL, NULL, 0);
2588 	pfs_create_file(dir, NULL, ".dummy", &linprocfs_dotaskdummy, NULL, NULL,
2589 	    NULL, PFS_RD);
2590 
2591 	/* /proc/scsi/... */
2592 	pfs_create_dir(root, &dir, "scsi", NULL, NULL, NULL, 0);
2593 	pfs_create_file(dir, NULL, "device_info", &linprocfs_doscsidevinfo,
2594 	    NULL, NULL, NULL, PFS_RD);
2595 	pfs_create_file(dir, NULL, "scsi", &linprocfs_doscsiscsi, NULL, NULL,
2596 	    NULL, PFS_RD);
2597 
2598 	/* /proc/sys/... */
2599 	pfs_create_dir(root, &sys, "sys", NULL, NULL, NULL, 0);
2600 
2601 	/* /proc/sys/kernel/... */
2602 	pfs_create_dir(sys, &dir, "kernel", NULL, NULL, NULL, 0);
2603 	pfs_create_file(dir, NULL, "osrelease", &linprocfs_doosrelease, NULL,
2604 	    NULL, NULL, PFS_RD);
2605 	pfs_create_file(dir, NULL, "ostype", &linprocfs_doostype, NULL, NULL,
2606 	    NULL, PFS_RD);
2607 	pfs_create_file(dir, NULL, "version", &linprocfs_doosbuild, NULL, NULL,
2608 	    NULL, PFS_RD);
2609 	pfs_create_file(dir, NULL, "msgmax", &linprocfs_domsgmax, NULL, NULL,
2610 	    NULL, PFS_RD);
2611 	pfs_create_file(dir, NULL, "msgmni", &linprocfs_domsgmni, NULL, NULL,
2612 	    NULL, PFS_RD);
2613 	pfs_create_file(dir, NULL, "msgmnb", &linprocfs_domsgmnb, NULL, NULL,
2614 	    NULL, PFS_RD);
2615 	pfs_create_file(dir, NULL, "ngroups_max", &linprocfs_dongroups_max,
2616 	    NULL, NULL, NULL, PFS_RD);
2617 	pfs_create_file(dir, NULL, "pid_max", &linprocfs_dopid_max, NULL, NULL,
2618 	    NULL, PFS_RD);
2619 	pfs_create_file(dir, NULL, "sem", &linprocfs_dosem, NULL, NULL, NULL,
2620 	    PFS_RD);
2621 	pfs_create_file(dir, NULL, "shmall", &linprocfs_doshmall, NULL, NULL,
2622 	    NULL, PFS_RD);
2623 	pfs_create_file(dir, NULL, "shmmax", &linprocfs_doshmmax, NULL, NULL,
2624 	    NULL, PFS_RD);
2625 	pfs_create_file(dir, NULL, "shmmni", &linprocfs_doshmmni, NULL, NULL,
2626 	    NULL, PFS_RD);
2627 	pfs_create_file(dir, NULL, "tainted", &linprocfs_dotainted, NULL, NULL,
2628 	    NULL, PFS_RD);
2629 	pfs_create_file(dir, NULL, "threads-max", &linprocfs_dothreads_max,
2630 	    NULL, NULL, NULL, PFS_RD);
2631 
2632 	/* /proc/sys/kernel/random/... */
2633 	pfs_create_dir(dir, &dir, "random", NULL, NULL, NULL, 0);
2634 	pfs_create_file(dir, NULL, "uuid", &linprocfs_douuid, NULL, NULL, NULL,
2635 	    PFS_RD);
2636 	pfs_create_file(dir, NULL, "boot_id", &linprocfs_doboot_id, NULL, NULL,
2637 	    NULL, PFS_RD);
2638 
2639 	/* /proc/sys/vm/.... */
2640 	pfs_create_dir(sys, &dir, "vm", NULL, NULL, NULL, 0);
2641 	pfs_create_file(dir, NULL, "min_free_kbytes", &linprocfs_dominfree,
2642 	    NULL, NULL, NULL, PFS_RD);
2643 	pfs_create_file(dir, NULL, "max_map_count", &linprocfs_domax_map_cnt,
2644 	    NULL, NULL, NULL, PFS_RD);
2645 
2646 	/* /proc/sysvipc/... */
2647 	pfs_create_dir(root, &dir, "sysvipc", NULL, NULL, NULL, 0);
2648 	pfs_create_file(dir, NULL, "msg", &linprocfs_dosysvipc_msg, NULL, NULL,
2649 	    NULL, PFS_RD);
2650 	pfs_create_file(dir, NULL, "sem", &linprocfs_dosysvipc_sem, NULL, NULL,
2651 	    NULL, PFS_RD);
2652 	pfs_create_file(dir, NULL, "shm", &linprocfs_dosysvipc_shm, NULL, NULL,
2653 	    NULL, PFS_RD);
2654 
2655 	/* /proc/sys/fs/... */
2656 	pfs_create_dir(sys, &fs, "fs", NULL, NULL, NULL, 0);
2657 
2658 	pfs_create_file(fs, NULL, "file-nr", &linprocfs_dofile_nr,
2659 	    NULL, NULL, NULL, PFS_RD);
2660 	pfs_create_file(fs, NULL, "file-max", &linprocfs_dofile_max,
2661 	    NULL, NULL, NULL, PFS_RD);
2662 	pfs_create_file(fs, NULL, "nr_open", &linprocfs_donr_open,
2663 	    NULL, NULL, NULL, PFS_RD);
2664 	pfs_create_file(fs, NULL, "overflowgid", &linprocfs_dooverflowgid,
2665 	    NULL, NULL, NULL, PFS_RD);
2666 	pfs_create_file(fs, NULL, "overflowuid", &linprocfs_dooverflowuid,
2667 	    NULL, NULL, NULL, PFS_RD);
2668 	pfs_create_file(fs, NULL, "protected_hardlinks",
2669 	    &linprocfs_doprotected_hardlinks, NULL, NULL, NULL, PFS_RD);
2670 	pfs_create_file(fs, NULL, "suid_dumpable", &linprocfs_dosuid_dumpable,
2671 	    NULL, NULL, NULL, PFS_RD);
2672 
2673 	pfs_create_dir(fs, &dir, "inotify", NULL, NULL, NULL, 0);
2674 	pfs_create_file(dir, NULL, "max_queued_events",
2675 	    &linprocfs_doinotify_max_queued_events, NULL, NULL, NULL, PFS_RDWR);
2676 	pfs_create_file(dir, NULL, "max_user_instances",
2677 	    &linprocfs_doinotify_max_user_instances, NULL, NULL, NULL, PFS_RDWR);
2678 	pfs_create_file(dir, NULL, "max_user_watches",
2679 	    &linprocfs_doinotify_max_user_watches, NULL, NULL, NULL, PFS_RDWR);
2680 
2681 	/* /proc/sys/fs/mqueue/... */
2682 	pfs_create_dir(fs, &dir, "mqueue", NULL, NULL, NULL, 0);
2683 	pfs_create_file(dir, NULL, "msg_default",
2684 	    &linprocfs_domqueue_msg_default, NULL, NULL, NULL, PFS_RD);
2685 	pfs_create_file(dir, NULL, "msgsize_default",
2686 	    &linprocfs_domqueue_msgsize_default, NULL, NULL, NULL, PFS_RD);
2687 	pfs_create_file(dir, NULL, "msg_max", &linprocfs_domqueue_msg_max, NULL,
2688 	    NULL, NULL, PFS_RD);
2689 	pfs_create_file(dir, NULL, "msgsize_max",
2690 	    &linprocfs_domqueue_msgsize_max, NULL, NULL, NULL, PFS_RD);
2691 	pfs_create_file(dir, NULL, "queues_max", &linprocfs_domqueue_queues_max,
2692 	    NULL, NULL, NULL, PFS_RD);
2693 
2694 	return (0);
2695 }
2696 
2697 /*
2698  * Destructor
2699  */
2700 static int
2701 linprocfs_uninit(PFS_INIT_ARGS)
2702 {
2703 
2704 	/* nothing to do, pseudofs will GC */
2705 	return (0);
2706 }
2707 
2708 PSEUDOFS(linprocfs, 1, VFCF_JAIL);
2709 #if defined(__aarch64__) || defined(__amd64__)
2710 MODULE_DEPEND(linprocfs, linux_common, 1, 1, 1);
2711 #else
2712 MODULE_DEPEND(linprocfs, linux, 1, 1, 1);
2713 #endif
2714 MODULE_DEPEND(linprocfs, procfs, 1, 1, 1);
2715 MODULE_DEPEND(linprocfs, sysvmsg, 1, 1, 1);
2716 MODULE_DEPEND(linprocfs, sysvsem, 1, 1, 1);
2717 MODULE_DEPEND(linprocfs, sysvshm, 1, 1, 1);
2718