xref: /freebsd/sys/compat/linprocfs/linprocfs.c (revision b9752d5d1cea30a39e89c83ea3aeb539581418cb)
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
linprocfs_domeminfo(PFS_FILL_ARGS)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
linprocfs_docpuinfo(PFS_FILL_ARGS)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
linprocfs_docpuinfo(PFS_FILL_ARGS)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
_mtab_helper(const struct pfs_node * pn,const struct statfs * sp,const char ** mntfrom,const char ** mntto,const char ** fstype)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
_sbuf_mntoptions_helper(struct sbuf * sb,uint64_t f_flags)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
linprocfs_domtab(PFS_FILL_ARGS)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
linprocfs_doprocmountinfo(PFS_FILL_ARGS)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
linprocfs_dopartitions(PFS_FILL_ARGS)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 rio rmerge rsect "
663 	    "ruse wio wmerge wsect wuse running use aveq\n");
664 
665 	LIST_FOREACH(cp, &g_classes, class) {
666 		if (strcmp(cp->name, "DISK") == 0 ||
667 		    strcmp(cp->name, "PART") == 0)
668 			LIST_FOREACH(gp, &cp->geom, geom) {
669 				LIST_FOREACH(pp, &gp->provider, provider) {
670 					if (linux_driver_get_major_minor(
671 					    pp->name, &major, &minor) != 0) {
672 						major = 0;
673 						minor = 0;
674 					}
675 					sbuf_printf(sb, "%d %d %lld %s "
676 					    "%d %d %d %d %d "
677 					     "%d %d %d %d %d %d\n",
678 					     major, minor,
679 					     (long long)pp->mediasize, pp->name,
680 					     0, 0, 0, 0, 0,
681 					     0, 0, 0, 0, 0, 0);
682 				}
683 			}
684 	}
685 	g_topology_unlock();
686 
687 	return (0);
688 }
689 
690 /*
691  * Filler function for proc/stat
692  *
693  * Output depends on kernel version:
694  *
695  * v2.5.40 <=
696  *   user nice system idle
697  * v2.5.41
698  *   user nice system idle iowait
699  * v2.6.11
700  *   user nice system idle iowait irq softirq steal
701  * v2.6.24
702  *   user nice system idle iowait irq softirq steal guest
703  * v2.6.33 >=
704  *   user nice system idle iowait irq softirq steal guest guest_nice
705  */
706 static int
linprocfs_dostat(PFS_FILL_ARGS)707 linprocfs_dostat(PFS_FILL_ARGS)
708 {
709 	struct pcpu *pcpu;
710 	long cp_time[CPUSTATES];
711 	long *cp;
712 	struct timeval boottime;
713 	int i;
714 	char *zero_pad;
715 	bool has_intr = true;
716 
717 	if (linux_kernver(td) >= LINUX_KERNVER(2,6,33)) {
718 		zero_pad = " 0 0 0 0\n";
719 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,6,24)) {
720 		zero_pad = " 0 0 0\n";
721 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,6,11)) {
722 		zero_pad = " 0 0\n";
723 	} else if (linux_kernver(td) >= LINUX_KERNVER(2,5,41)) {
724 		has_intr = false;
725 		zero_pad = " 0\n";
726 	} else {
727 		has_intr = false;
728 		zero_pad = "\n";
729 	}
730 
731 	read_cpu_time(cp_time);
732 	getboottime(&boottime);
733 	/* Parameters common to all versions */
734 	sbuf_printf(sb, "cpu %lu %lu %lu %lu",
735 	    T2J(cp_time[CP_USER]),
736 	    T2J(cp_time[CP_NICE]),
737 	    T2J(cp_time[CP_SYS]),
738 	    T2J(cp_time[CP_IDLE]));
739 
740 	/* Print interrupt stats if available */
741 	if (has_intr) {
742 		sbuf_printf(sb, " 0 %lu", T2J(cp_time[CP_INTR]));
743 	}
744 
745 	/* Pad out remaining fields depending on version */
746 	sbuf_printf(sb, "%s", zero_pad);
747 
748 	CPU_FOREACH(i) {
749 		pcpu = pcpu_find(i);
750 		cp = pcpu->pc_cp_time;
751 		sbuf_printf(sb, "cpu%d %lu %lu %lu %lu", i,
752 		    T2J(cp[CP_USER]),
753 		    T2J(cp[CP_NICE]),
754 		    T2J(cp[CP_SYS]),
755 		    T2J(cp[CP_IDLE]));
756 
757 		if (has_intr) {
758 			sbuf_printf(sb, " 0 %lu", T2J(cp[CP_INTR]));
759 		}
760 
761 		sbuf_printf(sb, "%s", zero_pad);
762 	}
763 	sbuf_printf(sb,
764 	    "disk 0 0 0 0\n"
765 	    "page %ju %ju\n"
766 	    "swap %ju %ju\n"
767 	    "intr %ju\n"
768 	    "ctxt %ju\n"
769 	    "btime %lld\n",
770 	    (uintmax_t)VM_CNT_FETCH(v_vnodepgsin),
771 	    (uintmax_t)VM_CNT_FETCH(v_vnodepgsout),
772 	    (uintmax_t)VM_CNT_FETCH(v_swappgsin),
773 	    (uintmax_t)VM_CNT_FETCH(v_swappgsout),
774 	    (uintmax_t)VM_CNT_FETCH(v_intr),
775 	    (uintmax_t)VM_CNT_FETCH(v_swtch),
776 	    (long long)boottime.tv_sec);
777 	return (0);
778 }
779 
780 static int
linprocfs_doswaps(PFS_FILL_ARGS)781 linprocfs_doswaps(PFS_FILL_ARGS)
782 {
783 	struct xswdev xsw;
784 	uintmax_t total, used;
785 	int n;
786 	char devname[SPECNAMELEN + 1];
787 
788 	sbuf_printf(sb, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
789 	for (n = 0; ; n++) {
790 		if (swap_dev_info(n, &xsw, devname, sizeof(devname)) != 0)
791 			break;
792 		total = (uintmax_t)xsw.xsw_nblks * PAGE_SIZE / 1024;
793 		used  = (uintmax_t)xsw.xsw_used * PAGE_SIZE / 1024;
794 
795 		/*
796 		 * The space and not tab after the device name is on
797 		 * purpose.  Linux does so.
798 		 */
799 		sbuf_printf(sb, "/dev/%-34s unknown\t\t%jd\t%jd\t-1\n",
800 		    devname, total, used);
801 	}
802 	return (0);
803 }
804 
805 /*
806  * Filler function for proc/uptime
807  */
808 static int
linprocfs_douptime(PFS_FILL_ARGS)809 linprocfs_douptime(PFS_FILL_ARGS)
810 {
811 	long cp_time[CPUSTATES];
812 	struct timeval tv;
813 
814 	getmicrouptime(&tv);
815 	read_cpu_time(cp_time);
816 	sbuf_printf(sb, "%lld.%02ld %ld.%02lu\n",
817 	    (long long)tv.tv_sec, tv.tv_usec / 10000,
818 	    T2S(cp_time[CP_IDLE] / mp_ncpus),
819 	    T2CS(cp_time[CP_IDLE] / mp_ncpus) % 100);
820 	return (0);
821 }
822 
823 /*
824  * Get OS build date
825  */
826 static void
linprocfs_osbuild(struct thread * td,struct sbuf * sb)827 linprocfs_osbuild(struct thread *td, struct sbuf *sb)
828 {
829 #if 0
830 	char osbuild[256];
831 	char *cp1, *cp2;
832 
833 	strncpy(osbuild, version, 256);
834 	osbuild[255] = '\0';
835 	cp1 = strstr(osbuild, "\n");
836 	cp2 = strstr(osbuild, ":");
837 	if (cp1 && cp2) {
838 		*cp1 = *cp2 = '\0';
839 		cp1 = strstr(osbuild, "#");
840 	} else
841 		cp1 = NULL;
842 	if (cp1)
843 		sbuf_printf(sb, "%s%s", cp1, cp2 + 1);
844 	else
845 #endif
846 		sbuf_cat(sb, "#4 Sun Dec 18 04:30:00 CET 1977");
847 }
848 
849 /*
850  * Get OS builder
851  */
852 static void
linprocfs_osbuilder(struct thread * td,struct sbuf * sb)853 linprocfs_osbuilder(struct thread *td, struct sbuf *sb)
854 {
855 #if 0
856 	char builder[256];
857 	char *cp;
858 
859 	cp = strstr(version, "\n    ");
860 	if (cp) {
861 		strncpy(builder, cp + 5, 256);
862 		builder[255] = '\0';
863 		cp = strstr(builder, ":");
864 		if (cp)
865 			*cp = '\0';
866 	}
867 	if (cp)
868 		sbuf_cat(sb, builder);
869 	else
870 #endif
871 		sbuf_cat(sb, "des@freebsd.org");
872 }
873 
874 /*
875  * Filler function for proc/version
876  */
877 static int
linprocfs_doversion(PFS_FILL_ARGS)878 linprocfs_doversion(PFS_FILL_ARGS)
879 {
880 	char osname[LINUX_MAX_UTSNAME];
881 	char osrelease[LINUX_MAX_UTSNAME];
882 
883 	linux_get_osname(td, osname);
884 	linux_get_osrelease(td, osrelease);
885 	sbuf_printf(sb, "%s version %s (", osname, osrelease);
886 	linprocfs_osbuilder(td, sb);
887 	sbuf_cat(sb, ") (gcc version " __VERSION__ ") ");
888 	linprocfs_osbuild(td, sb);
889 	sbuf_cat(sb, "\n");
890 
891 	return (0);
892 }
893 
894 /*
895  * Filler function for proc/loadavg
896  */
897 static int
linprocfs_doloadavg(PFS_FILL_ARGS)898 linprocfs_doloadavg(PFS_FILL_ARGS)
899 {
900 
901 	sbuf_printf(sb,
902 	    "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
903 	    (int)(averunnable.ldavg[0] / averunnable.fscale),
904 	    (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
905 	    (int)(averunnable.ldavg[1] / averunnable.fscale),
906 	    (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
907 	    (int)(averunnable.ldavg[2] / averunnable.fscale),
908 	    (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
909 	    1,				/* number of running tasks */
910 	    nprocs,			/* number of tasks */
911 	    lastpid			/* the last pid */
912 	);
913 	return (0);
914 }
915 
916 static int
linprocfs_get_tty_nr(struct proc * p)917 linprocfs_get_tty_nr(struct proc *p)
918 {
919 	struct session *sp;
920 	const char *ttyname;
921 	int error, major, minor, nr;
922 
923 	PROC_LOCK_ASSERT(p, MA_OWNED);
924 	sx_assert(&proctree_lock, SX_LOCKED);
925 
926 	if ((p->p_flag & P_CONTROLT) == 0)
927 		return (-1);
928 
929 	sp = p->p_pgrp->pg_session;
930 	if (sp == NULL)
931 		return (-1);
932 
933 	ttyname = devtoname(sp->s_ttyp->t_dev);
934 	error = linux_driver_get_major_minor(ttyname, &major, &minor);
935 	if (error != 0)
936 		return (-1);
937 
938 	nr = makedev(major, minor);
939 	return (nr);
940 }
941 
942 /*
943  * Filler function for proc/pid/stat
944  */
945 static int
linprocfs_doprocstat(PFS_FILL_ARGS)946 linprocfs_doprocstat(PFS_FILL_ARGS)
947 {
948 	struct kinfo_proc kp;
949 	struct timeval boottime;
950 	char state;
951 	static int ratelimit = 0;
952 	int tty_nr;
953 	vm_offset_t startcode, startdata;
954 
955 	getboottime(&boottime);
956 	sx_slock(&proctree_lock);
957 	PROC_LOCK(p);
958 	fill_kinfo_proc(p, &kp);
959 	tty_nr = linprocfs_get_tty_nr(p);
960 	sx_sunlock(&proctree_lock);
961 	if (p->p_vmspace) {
962 	   startcode = (vm_offset_t)p->p_vmspace->vm_taddr;
963 	   startdata = (vm_offset_t)p->p_vmspace->vm_daddr;
964 	} else {
965 	   startcode = 0;
966 	   startdata = 0;
967 	}
968 	sbuf_printf(sb, "%d", p->p_pid);
969 #define PS_ADD(name, fmt, arg) sbuf_printf(sb, " " fmt, arg)
970 	PS_ADD("comm",		"(%s)",	p->p_comm);
971 	if (kp.ki_stat > sizeof(linux_state)) {
972 		state = 'R';
973 
974 		if (ratelimit == 0) {
975 			printf("linprocfs: don't know how to handle unknown FreeBSD state %d/%zd, mapping to R\n",
976 			    kp.ki_stat, sizeof(linux_state));
977 			++ratelimit;
978 		}
979 	} else
980 		state = linux_state[kp.ki_stat - 1];
981 	PS_ADD("state",		"%c",	state);
982 	PS_ADD("ppid",		"%d",	p->p_pptr ? p->p_pptr->p_pid : 0);
983 	PS_ADD("pgrp",		"%d",	p->p_pgid);
984 	PS_ADD("session",	"%d",	p->p_session->s_sid);
985 	PROC_UNLOCK(p);
986 	PS_ADD("tty",		"%d",	tty_nr);
987 	PS_ADD("tpgid",		"%d",	kp.ki_tpgid);
988 	PS_ADD("flags",		"%u",	0); /* XXX */
989 	PS_ADD("minflt",	"%lu",	kp.ki_rusage.ru_minflt);
990 	PS_ADD("cminflt",	"%lu",	kp.ki_rusage_ch.ru_minflt);
991 	PS_ADD("majflt",	"%lu",	kp.ki_rusage.ru_majflt);
992 	PS_ADD("cmajflt",	"%lu",	kp.ki_rusage_ch.ru_majflt);
993 	PS_ADD("utime",		"%ld",	TV2J(&kp.ki_rusage.ru_utime));
994 	PS_ADD("stime",		"%ld",	TV2J(&kp.ki_rusage.ru_stime));
995 	PS_ADD("cutime",	"%ld",	TV2J(&kp.ki_rusage_ch.ru_utime));
996 	PS_ADD("cstime",	"%ld",	TV2J(&kp.ki_rusage_ch.ru_stime));
997 	PS_ADD("priority",	"%d",	kp.ki_pri.pri_user);
998 	PS_ADD("nice",		"%d",	kp.ki_nice); /* 19 (nicest) to -19 */
999 	PS_ADD("0",		"%d",	0); /* removed field */
1000 	PS_ADD("itrealvalue",	"%d",	0); /* XXX */
1001 	PS_ADD("starttime",	"%lu",	TV2J(&kp.ki_start) - TV2J(&boottime));
1002 	PS_ADD("vsize",		"%ju",	(uintmax_t)kp.ki_size);
1003 	PS_ADD("rss",		"%ju",	(uintmax_t)kp.ki_rssize);
1004 	PS_ADD("rlim",		"%lu",	kp.ki_rusage.ru_maxrss);
1005 	PS_ADD("startcode",	"%ju",	(uintmax_t)startcode);
1006 	PS_ADD("endcode",	"%ju",	(uintmax_t)startdata);
1007 	PS_ADD("startstack",	"%u",	0); /* XXX */
1008 	PS_ADD("kstkesp",	"%u",	0); /* XXX */
1009 	PS_ADD("kstkeip",	"%u",	0); /* XXX */
1010 	PS_ADD("signal",	"%u",	0); /* XXX */
1011 	PS_ADD("blocked",	"%u",	0); /* XXX */
1012 	PS_ADD("sigignore",	"%u",	0); /* XXX */
1013 	PS_ADD("sigcatch",	"%u",	0); /* XXX */
1014 	PS_ADD("wchan",		"%u",	0); /* XXX */
1015 	PS_ADD("nswap",		"%lu",	kp.ki_rusage.ru_nswap);
1016 	PS_ADD("cnswap",	"%lu",	kp.ki_rusage_ch.ru_nswap);
1017 	PS_ADD("exitsignal",	"%d",	0); /* XXX */
1018 	PS_ADD("processor",	"%u",	kp.ki_lastcpu);
1019 	PS_ADD("rt_priority",	"%u",	0); /* XXX */ /* >= 2.5.19 */
1020 	PS_ADD("policy",	"%u",	kp.ki_pri.pri_class); /* >= 2.5.19 */
1021 #undef PS_ADD
1022 	sbuf_putc(sb, '\n');
1023 
1024 	return (0);
1025 }
1026 
1027 /*
1028  * Filler function for proc/pid/statm
1029  */
1030 static int
linprocfs_doprocstatm(PFS_FILL_ARGS)1031 linprocfs_doprocstatm(PFS_FILL_ARGS)
1032 {
1033 	struct kinfo_proc kp;
1034 	segsz_t lsize;
1035 
1036 	sx_slock(&proctree_lock);
1037 	PROC_LOCK(p);
1038 	fill_kinfo_proc(p, &kp);
1039 	PROC_UNLOCK(p);
1040 	sx_sunlock(&proctree_lock);
1041 
1042 	/*
1043 	 * See comments in linprocfs_doprocstatus() regarding the
1044 	 * computation of lsize.
1045 	 */
1046 	/* size resident share trs drs lrs dt */
1047 	sbuf_printf(sb, "%ju ", B2P((uintmax_t)kp.ki_size));
1048 	sbuf_printf(sb, "%ju ", (uintmax_t)kp.ki_rssize);
1049 	sbuf_printf(sb, "%ju ", (uintmax_t)0); /* XXX */
1050 	sbuf_printf(sb, "%ju ",	(uintmax_t)kp.ki_tsize);
1051 	sbuf_printf(sb, "%ju ", (uintmax_t)(kp.ki_dsize + kp.ki_ssize));
1052 	lsize = B2P(kp.ki_size) - kp.ki_dsize -
1053 	    kp.ki_ssize - kp.ki_tsize - 1;
1054 	sbuf_printf(sb, "%ju ", (uintmax_t)lsize);
1055 	sbuf_printf(sb, "%ju\n", (uintmax_t)0); /* XXX */
1056 
1057 	return (0);
1058 }
1059 
1060 /*
1061  * Filler function for proc/pid/status
1062  */
1063 static int
linprocfs_doprocstatus(PFS_FILL_ARGS)1064 linprocfs_doprocstatus(PFS_FILL_ARGS)
1065 {
1066 	struct kinfo_proc kp;
1067 	char *state;
1068 	segsz_t lsize;
1069 	struct thread *td2;
1070 	struct sigacts *ps;
1071 	l_sigset_t siglist, sigignore, sigcatch;
1072 	int i;
1073 
1074 	sx_slock(&proctree_lock);
1075 	PROC_LOCK(p);
1076 	td2 = FIRST_THREAD_IN_PROC(p);
1077 
1078 	if (P_SHOULDSTOP(p)) {
1079 		state = "T (stopped)";
1080 	} else {
1081 		switch(p->p_state) {
1082 		case PRS_NEW:
1083 			state = "I (idle)";
1084 			break;
1085 		case PRS_NORMAL:
1086 			if (p->p_flag & P_WEXIT) {
1087 				state = "X (exiting)";
1088 				break;
1089 			}
1090 			switch(TD_GET_STATE(td2)) {
1091 			case TDS_INHIBITED:
1092 				state = "S (sleeping)";
1093 				break;
1094 			case TDS_RUNQ:
1095 			case TDS_RUNNING:
1096 				state = "R (running)";
1097 				break;
1098 			default:
1099 				state = "? (unknown)";
1100 				break;
1101 			}
1102 			break;
1103 		case PRS_ZOMBIE:
1104 			state = "Z (zombie)";
1105 			break;
1106 		default:
1107 			state = "? (unknown)";
1108 			break;
1109 		}
1110 	}
1111 
1112 	fill_kinfo_proc(p, &kp);
1113 	sx_sunlock(&proctree_lock);
1114 
1115 	sbuf_printf(sb, "Name:\t%s\n",		p->p_comm); /* XXX escape */
1116 	sbuf_printf(sb, "State:\t%s\n",		state);
1117 
1118 	/*
1119 	 * Credentials
1120 	 */
1121 	sbuf_printf(sb, "Tgid:\t%d\n",		p->p_pid);
1122 	sbuf_printf(sb, "Pid:\t%d\n",		p->p_pid);
1123 	sbuf_printf(sb, "PPid:\t%d\n",		kp.ki_ppid );
1124 	sbuf_printf(sb, "TracerPid:\t%d\n",	kp.ki_tracer );
1125 	sbuf_printf(sb, "Uid:\t%d\t%d\t%d\t%d\n", p->p_ucred->cr_ruid,
1126 						p->p_ucred->cr_uid,
1127 						p->p_ucred->cr_svuid,
1128 						/* FreeBSD doesn't have fsuid */
1129 						p->p_ucred->cr_uid);
1130 	sbuf_printf(sb, "Gid:\t%d\t%d\t%d\t%d\n", p->p_ucred->cr_rgid,
1131 						p->p_ucred->cr_gid,
1132 						p->p_ucred->cr_svgid,
1133 						/* FreeBSD doesn't have fsgid */
1134 						p->p_ucred->cr_gid);
1135 	sbuf_cat(sb, "Groups:\t");
1136 	for (i = 0; i < p->p_ucred->cr_ngroups; i++)
1137 		sbuf_printf(sb, "%d ",		p->p_ucred->cr_groups[i]);
1138 	PROC_UNLOCK(p);
1139 	sbuf_putc(sb, '\n');
1140 
1141 	/*
1142 	 * Memory
1143 	 *
1144 	 * While our approximation of VmLib may not be accurate (I
1145 	 * don't know of a simple way to verify it, and I'm not sure
1146 	 * it has much meaning anyway), I believe it's good enough.
1147 	 *
1148 	 * The same code that could (I think) accurately compute VmLib
1149 	 * could also compute VmLck, but I don't really care enough to
1150 	 * implement it. Submissions are welcome.
1151 	 */
1152 	sbuf_printf(sb, "VmSize:\t%8ju kB\n",	B2K((uintmax_t)kp.ki_size));
1153 	sbuf_printf(sb, "VmLck:\t%8u kB\n",	P2K(0)); /* XXX */
1154 	sbuf_printf(sb, "VmRSS:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_rssize));
1155 	sbuf_printf(sb, "VmData:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_dsize));
1156 	sbuf_printf(sb, "VmStk:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_ssize));
1157 	sbuf_printf(sb, "VmExe:\t%8ju kB\n",	P2K((uintmax_t)kp.ki_tsize));
1158 	lsize = B2P(kp.ki_size) - kp.ki_dsize -
1159 	    kp.ki_ssize - kp.ki_tsize - 1;
1160 	sbuf_printf(sb, "VmLib:\t%8ju kB\n",	P2K((uintmax_t)lsize));
1161 
1162 	/*
1163 	 * Signal masks
1164 	 */
1165 	PROC_LOCK(p);
1166 	bsd_to_linux_sigset(&p->p_siglist, &siglist);
1167 	ps = p->p_sigacts;
1168 	mtx_lock(&ps->ps_mtx);
1169 	bsd_to_linux_sigset(&ps->ps_sigignore, &sigignore);
1170 	bsd_to_linux_sigset(&ps->ps_sigcatch, &sigcatch);
1171 	mtx_unlock(&ps->ps_mtx);
1172 	PROC_UNLOCK(p);
1173 
1174 	sbuf_printf(sb, "SigPnd:\t%016jx\n",	siglist.__mask);
1175 	/*
1176 	 * XXX. SigBlk - target thread's signal mask, td_sigmask.
1177 	 * To implement SigBlk pseudofs should support proc/tid dir entries.
1178 	 */
1179 	sbuf_printf(sb, "SigBlk:\t%016x\n",	0);
1180 	sbuf_printf(sb, "SigIgn:\t%016jx\n",	sigignore.__mask);
1181 	sbuf_printf(sb, "SigCgt:\t%016jx\n",	sigcatch.__mask);
1182 
1183 	/*
1184 	 * Linux also prints the capability masks, but we don't have
1185 	 * capabilities yet, and when we do get them they're likely to
1186 	 * be meaningless to Linux programs, so we lie. XXX
1187 	 */
1188 	sbuf_printf(sb, "CapInh:\t%016x\n",	0);
1189 	sbuf_printf(sb, "CapPrm:\t%016x\n",	0);
1190 	sbuf_printf(sb, "CapEff:\t%016x\n",	0);
1191 
1192 	return (0);
1193 }
1194 
1195 /*
1196  * Filler function for proc/pid/cwd
1197  */
1198 static int
linprocfs_doproccwd(PFS_FILL_ARGS)1199 linprocfs_doproccwd(PFS_FILL_ARGS)
1200 {
1201 	struct pwd *pwd;
1202 	char *fullpath = "unknown";
1203 	char *freepath = NULL;
1204 
1205 	pwd = pwd_hold_proc(p);
1206 	vn_fullpath(pwd->pwd_cdir, &fullpath, &freepath);
1207 	sbuf_printf(sb, "%s", fullpath);
1208 	if (freepath)
1209 		free(freepath, M_TEMP);
1210 	pwd_drop(pwd);
1211 	return (0);
1212 }
1213 
1214 /*
1215  * Filler function for proc/pid/root
1216  */
1217 static int
linprocfs_doprocroot(PFS_FILL_ARGS)1218 linprocfs_doprocroot(PFS_FILL_ARGS)
1219 {
1220 	struct pwd *pwd;
1221 	struct vnode *vp;
1222 	char *fullpath = "unknown";
1223 	char *freepath = NULL;
1224 
1225 	pwd = pwd_hold_proc(p);
1226 	vp = jailed(p->p_ucred) ? pwd->pwd_jdir : pwd->pwd_rdir;
1227 	vn_fullpath(vp, &fullpath, &freepath);
1228 	sbuf_printf(sb, "%s", fullpath);
1229 	if (freepath)
1230 		free(freepath, M_TEMP);
1231 	pwd_drop(pwd);
1232 	return (0);
1233 }
1234 
1235 /*
1236  * Filler function for proc/pid/cmdline
1237  */
1238 static int
linprocfs_doproccmdline(PFS_FILL_ARGS)1239 linprocfs_doproccmdline(PFS_FILL_ARGS)
1240 {
1241 	int ret;
1242 
1243 	PROC_LOCK(p);
1244 	if ((ret = p_cansee(td, p)) != 0) {
1245 		PROC_UNLOCK(p);
1246 		return (ret);
1247 	}
1248 
1249 	/*
1250 	 * Mimic linux behavior and pass only processes with usermode
1251 	 * address space as valid.  Return zero silently otherwize.
1252 	 */
1253 	if (p->p_vmspace == &vmspace0) {
1254 		PROC_UNLOCK(p);
1255 		return (0);
1256 	}
1257 	if (p->p_args != NULL) {
1258 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
1259 		PROC_UNLOCK(p);
1260 		return (0);
1261 	}
1262 
1263 	if ((p->p_flag & P_SYSTEM) != 0) {
1264 		PROC_UNLOCK(p);
1265 		return (0);
1266 	}
1267 
1268 	PROC_UNLOCK(p);
1269 
1270 	ret = proc_getargv(td, p, sb);
1271 	return (ret);
1272 }
1273 
1274 /*
1275  * Filler function for proc/pid/environ
1276  */
1277 static int
linprocfs_doprocenviron(PFS_FILL_ARGS)1278 linprocfs_doprocenviron(PFS_FILL_ARGS)
1279 {
1280 
1281 	/*
1282 	 * Mimic linux behavior and pass only processes with usermode
1283 	 * address space as valid.  Return zero silently otherwize.
1284 	 */
1285 	if (p->p_vmspace == &vmspace0)
1286 		return (0);
1287 
1288 	return (proc_getenvv(td, p, sb));
1289 }
1290 
1291 static char l32_map_str[] = "%08lx-%08lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1292 static char l64_map_str[] = "%016lx-%016lx %s%s%s%s %08lx %02x:%02x %lu%s%s\n";
1293 static char vdso_str[] = "      [vdso]";
1294 static char stack_str[] = "      [stack]";
1295 
1296 /*
1297  * Filler function for proc/pid/maps
1298  */
1299 static int
linprocfs_doprocmaps(PFS_FILL_ARGS)1300 linprocfs_doprocmaps(PFS_FILL_ARGS)
1301 {
1302 	struct vmspace *vm;
1303 	vm_map_t map;
1304 	vm_map_entry_t entry, tmp_entry;
1305 	vm_object_t obj, tobj, lobj;
1306 	vm_offset_t e_start, e_end;
1307 	vm_ooffset_t off;
1308 	vm_prot_t e_prot;
1309 	unsigned int last_timestamp;
1310 	char *name = "", *freename = NULL;
1311 	const char *l_map_str;
1312 	ino_t ino;
1313 	int error;
1314 	struct vnode *vp;
1315 	struct vattr vat;
1316 	bool private;
1317 
1318 	PROC_LOCK(p);
1319 	error = p_candebug(td, p);
1320 	PROC_UNLOCK(p);
1321 	if (error)
1322 		return (error);
1323 
1324 	if (uio->uio_rw != UIO_READ)
1325 		return (EOPNOTSUPP);
1326 
1327 	error = 0;
1328 	vm = vmspace_acquire_ref(p);
1329 	if (vm == NULL)
1330 		return (ESRCH);
1331 
1332 	if (SV_CURPROC_FLAG(SV_LP64))
1333 		l_map_str = l64_map_str;
1334 	else
1335 		l_map_str = l32_map_str;
1336 	map = &vm->vm_map;
1337 	vm_map_lock_read(map);
1338 	VM_MAP_ENTRY_FOREACH(entry, map) {
1339 		name = "";
1340 		freename = NULL;
1341 		/*
1342 		 * Skip printing of the guard page of the stack region, as
1343 		 * it confuses glibc pthread_getattr_np() method, where both
1344 		 * the base address and size of the stack of the initial thread
1345 		 * are calculated.
1346 		 */
1347 		if ((entry->eflags & (MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_GUARD)) != 0)
1348 			continue;
1349 		e_prot = entry->protection;
1350 		e_start = entry->start;
1351 		e_end = entry->end;
1352 		obj = entry->object.vm_object;
1353 		off = entry->offset;
1354 		for (lobj = tobj = obj; tobj != NULL;
1355 		    lobj = tobj, tobj = tobj->backing_object) {
1356 			VM_OBJECT_RLOCK(tobj);
1357 			off += lobj->backing_object_offset;
1358 			if (lobj != obj)
1359 				VM_OBJECT_RUNLOCK(lobj);
1360 		}
1361 		private = (entry->eflags & MAP_ENTRY_COW) != 0 || obj == NULL ||
1362 		    (obj->flags & OBJ_ANON) != 0;
1363 		last_timestamp = map->timestamp;
1364 		vm_map_unlock_read(map);
1365 		ino = 0;
1366 		if (lobj) {
1367 			vp = vm_object_vnode(lobj);
1368 			if (vp != NULL)
1369 				vref(vp);
1370 			if (lobj != obj)
1371 				VM_OBJECT_RUNLOCK(lobj);
1372 			VM_OBJECT_RUNLOCK(obj);
1373 			if (vp != NULL) {
1374 				vn_fullpath(vp, &name, &freename);
1375 				vn_lock(vp, LK_SHARED | LK_RETRY);
1376 				VOP_GETATTR(vp, &vat, td->td_ucred);
1377 				ino = vat.va_fileid;
1378 				vput(vp);
1379 			} else if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
1380 				/*
1381 				 * sv_shared_page_base pointed out to the
1382 				 * FreeBSD sharedpage, PAGE_SIZE is a size
1383 				 * of it. The vDSO page is above.
1384 				 */
1385 				if (e_start == p->p_sysent->sv_shared_page_base +
1386 				    PAGE_SIZE)
1387 					name = vdso_str;
1388 				if (e_end == p->p_sysent->sv_usrstack)
1389 					name = stack_str;
1390 			}
1391 		}
1392 
1393 		/*
1394 		 * format:
1395 		 *  start, end, access, offset, major, minor, inode, name.
1396 		 */
1397 		error = sbuf_printf(sb, l_map_str,
1398 		    (u_long)e_start, (u_long)e_end,
1399 		    (e_prot & VM_PROT_READ)?"r":"-",
1400 		    (e_prot & VM_PROT_WRITE)?"w":"-",
1401 		    (e_prot & VM_PROT_EXECUTE)?"x":"-",
1402 		    private ? "p" : "s",
1403 		    (u_long)off,
1404 		    0,
1405 		    0,
1406 		    (u_long)ino,
1407 		    *name ? "     " : " ",
1408 		    name
1409 		    );
1410 		if (freename)
1411 			free(freename, M_TEMP);
1412 		vm_map_lock_read(map);
1413 		if (error == -1) {
1414 			error = 0;
1415 			break;
1416 		}
1417 		if (last_timestamp != map->timestamp) {
1418 			/*
1419 			 * Look again for the entry because the map was
1420 			 * modified while it was unlocked.  Specifically,
1421 			 * the entry may have been clipped, merged, or deleted.
1422 			 */
1423 			vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
1424 			entry = tmp_entry;
1425 		}
1426 	}
1427 	vm_map_unlock_read(map);
1428 	vmspace_free(vm);
1429 
1430 	return (error);
1431 }
1432 
1433 /*
1434  * Filler function for proc/pid/mem
1435  */
1436 static int
linprocfs_doprocmem(PFS_FILL_ARGS)1437 linprocfs_doprocmem(PFS_FILL_ARGS)
1438 {
1439 	ssize_t resid;
1440 	int error;
1441 
1442 	resid = uio->uio_resid;
1443 	error = procfs_doprocmem(PFS_FILL_ARGNAMES);
1444 
1445 	if (uio->uio_rw == UIO_READ && resid != uio->uio_resid)
1446 		return (0);
1447 
1448 	if (error == EFAULT)
1449 		error = EIO;
1450 
1451 	return (error);
1452 }
1453 
1454 /*
1455  * Filler function for proc/net/dev
1456  */
1457 static int
linprocfs_donetdev_cb(if_t ifp,void * arg)1458 linprocfs_donetdev_cb(if_t ifp, void *arg)
1459 {
1460 	char ifname[LINUX_IFNAMSIZ];
1461 	struct sbuf *sb = arg;
1462 
1463 	if (ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname)) <= 0)
1464 		return (ENODEV);
1465 
1466 	sbuf_printf(sb, "%6.6s: ", ifname);
1467 	sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
1468 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IBYTES),
1469 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IPACKETS),
1470 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IERRORS),
1471 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IQDROPS),
1472 						/* rx_missed_errors */
1473 	    0UL,				/* rx_fifo_errors */
1474 	    0UL,				/* rx_length_errors +
1475 						 * rx_over_errors +
1476 						 * rx_crc_errors +
1477 						 * rx_frame_errors */
1478 	    0UL,				/* rx_compressed */
1479 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IMCASTS));
1480 						/* XXX-BZ rx only? */
1481 	sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
1482 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OBYTES),
1483 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OPACKETS),
1484 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OERRORS),
1485 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OQDROPS),
1486 	    0UL,				/* tx_fifo_errors */
1487 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_COLLISIONS),
1488 	    0UL,				/* tx_carrier_errors +
1489 						 * tx_aborted_errors +
1490 						 * tx_window_errors +
1491 						 * tx_heartbeat_errors*/
1492 	    0UL);				/* tx_compressed */
1493 	return (0);
1494 }
1495 
1496 static int
linprocfs_donetdev(PFS_FILL_ARGS)1497 linprocfs_donetdev(PFS_FILL_ARGS)
1498 {
1499 	struct epoch_tracker et;
1500 
1501 	sbuf_printf(sb, "%6s|%58s|%s\n"
1502 	    "%6s|%58s|%58s\n",
1503 	    "Inter-", "   Receive", "  Transmit",
1504 	    " face",
1505 	    "bytes    packets errs drop fifo frame compressed multicast",
1506 	    "bytes    packets errs drop fifo colls carrier compressed");
1507 
1508 	CURVNET_SET(TD_TO_VNET(curthread));
1509 	NET_EPOCH_ENTER(et);
1510 	if_foreach(linprocfs_donetdev_cb, sb);
1511 	NET_EPOCH_EXIT(et);
1512 	CURVNET_RESTORE();
1513 
1514 	return (0);
1515 }
1516 
1517 struct walkarg {
1518 	struct sbuf *sb;
1519 };
1520 
1521 static int
linux_route_print(struct rtentry * rt,void * vw)1522 linux_route_print(struct rtentry *rt, void *vw)
1523 {
1524 #ifdef INET
1525 	struct walkarg *w = vw;
1526 	struct route_nhop_data rnd;
1527 	struct in_addr dst, mask;
1528 	struct nhop_object *nh;
1529 	char ifname[16];
1530 	uint32_t scopeid = 0;
1531 	uint32_t gw = 0;
1532 	uint32_t linux_flags = 0;
1533 
1534 	rt_get_inet_prefix_pmask(rt, &dst, &mask, &scopeid);
1535 
1536 	rt_get_rnd(rt, &rnd);
1537 
1538 	/* select only first route in case of multipath */
1539 	nh = nhop_select_func(rnd.rnd_nhop, 0);
1540 
1541 	if (ifname_bsd_to_linux_ifp(nh->nh_ifp, ifname, sizeof(ifname)) <= 0)
1542 		return (ENODEV);
1543 
1544 	gw = (nh->nh_flags & NHF_GATEWAY)
1545 		? nh->gw4_sa.sin_addr.s_addr : 0;
1546 
1547 	linux_flags = RTF_UP |
1548 		(nhop_get_rtflags(nh) & (RTF_GATEWAY | RTF_HOST));
1549 
1550 	sbuf_printf(w->sb,
1551 		"%s\t"
1552 		"%08X\t%08X\t%04X\t"
1553 		"%d\t%u\t%d\t"
1554 		"%08X\t%d\t%u\t%u",
1555 		ifname,
1556 		dst.s_addr, gw, linux_flags,
1557 		0, 0, rnd.rnd_weight,
1558 		mask.s_addr, nh->nh_mtu, 0, 0);
1559 
1560 	sbuf_printf(w->sb, "\n\n");
1561 #endif
1562 	return (0);
1563 }
1564 
1565 /*
1566  * Filler function for proc/net/route
1567  */
1568 static int
linprocfs_donetroute(PFS_FILL_ARGS)1569 linprocfs_donetroute(PFS_FILL_ARGS)
1570 {
1571 	struct epoch_tracker et;
1572 	struct walkarg w = {
1573 		.sb = sb
1574 	};
1575 	uint32_t fibnum = curthread->td_proc->p_fibnum;
1576 
1577 	sbuf_printf(w.sb, "%-127s\n", "Iface\tDestination\tGateway "
1578                "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1579                "\tWindow\tIRTT");
1580 
1581 	CURVNET_SET(TD_TO_VNET(curthread));
1582 	NET_EPOCH_ENTER(et);
1583 	rib_walk(fibnum, AF_INET, false, linux_route_print, &w);
1584 	NET_EPOCH_EXIT(et);
1585 	CURVNET_RESTORE();
1586 
1587 	return (0);
1588 }
1589 
1590 /*
1591  * Filler function for proc/sys/kernel/osrelease
1592  */
1593 static int
linprocfs_doosrelease(PFS_FILL_ARGS)1594 linprocfs_doosrelease(PFS_FILL_ARGS)
1595 {
1596 	char osrelease[LINUX_MAX_UTSNAME];
1597 
1598 	linux_get_osrelease(td, osrelease);
1599 	sbuf_printf(sb, "%s\n", osrelease);
1600 
1601 	return (0);
1602 }
1603 
1604 /*
1605  * Filler function for proc/sys/kernel/ostype
1606  */
1607 static int
linprocfs_doostype(PFS_FILL_ARGS)1608 linprocfs_doostype(PFS_FILL_ARGS)
1609 {
1610 	char osname[LINUX_MAX_UTSNAME];
1611 
1612 	linux_get_osname(td, osname);
1613 	sbuf_printf(sb, "%s\n", osname);
1614 
1615 	return (0);
1616 }
1617 
1618 /*
1619  * Filler function for proc/sys/kernel/version
1620  */
1621 static int
linprocfs_doosbuild(PFS_FILL_ARGS)1622 linprocfs_doosbuild(PFS_FILL_ARGS)
1623 {
1624 
1625 	linprocfs_osbuild(td, sb);
1626 	sbuf_cat(sb, "\n");
1627 	return (0);
1628 }
1629 
1630 /*
1631  * Filler function for proc/sys/kernel/msgmax
1632  */
1633 static int
linprocfs_domsgmax(PFS_FILL_ARGS)1634 linprocfs_domsgmax(PFS_FILL_ARGS)
1635 {
1636 
1637 	sbuf_printf(sb, "%d\n", msginfo.msgmax);
1638 	return (0);
1639 }
1640 
1641 /*
1642  * Filler function for proc/sys/kernel/msgmni
1643  */
1644 static int
linprocfs_domsgmni(PFS_FILL_ARGS)1645 linprocfs_domsgmni(PFS_FILL_ARGS)
1646 {
1647 
1648 	sbuf_printf(sb, "%d\n", msginfo.msgmni);
1649 	return (0);
1650 }
1651 
1652 /*
1653  * Filler function for proc/sys/kernel/msgmnb
1654  */
1655 static int
linprocfs_domsgmnb(PFS_FILL_ARGS)1656 linprocfs_domsgmnb(PFS_FILL_ARGS)
1657 {
1658 
1659 	sbuf_printf(sb, "%d\n", msginfo.msgmnb);
1660 	return (0);
1661 }
1662 
1663 /*
1664  * Filler function for proc/sys/kernel/ngroups_max
1665  *
1666  * Note that in Linux it defaults to 65536, not 1023.
1667  */
1668 static int
linprocfs_dongroups_max(PFS_FILL_ARGS)1669 linprocfs_dongroups_max(PFS_FILL_ARGS)
1670 {
1671 
1672 	sbuf_printf(sb, "%d\n", ngroups_max);
1673 	return (0);
1674 }
1675 
1676 /*
1677  * Filler function for proc/sys/kernel/pid_max
1678  */
1679 static int
linprocfs_dopid_max(PFS_FILL_ARGS)1680 linprocfs_dopid_max(PFS_FILL_ARGS)
1681 {
1682 
1683 	sbuf_printf(sb, "%i\n", PID_MAX);
1684 	return (0);
1685 }
1686 
1687 /*
1688  * Filler function for proc/sys/kernel/sem
1689  */
1690 static int
linprocfs_dosem(PFS_FILL_ARGS)1691 linprocfs_dosem(PFS_FILL_ARGS)
1692 {
1693 
1694 	sbuf_printf(sb, "%d %d %d %d\n", seminfo.semmsl, seminfo.semmns,
1695 	    seminfo.semopm, seminfo.semmni);
1696 	return (0);
1697 }
1698 
1699 /*
1700  * Filler function for proc/sys/kernel/shmall
1701  */
1702 static int
linprocfs_doshmall(PFS_FILL_ARGS)1703 linprocfs_doshmall(PFS_FILL_ARGS)
1704 {
1705 
1706 	sbuf_printf(sb, "%lu\n", shminfo.shmall);
1707 	return (0);
1708 }
1709 
1710 /*
1711  * Filler function for proc/sys/kernel/shmmax
1712  */
1713 static int
linprocfs_doshmmax(PFS_FILL_ARGS)1714 linprocfs_doshmmax(PFS_FILL_ARGS)
1715 {
1716 
1717 	sbuf_printf(sb, "%lu\n", shminfo.shmmax);
1718 	return (0);
1719 }
1720 
1721 /*
1722  * Filler function for proc/sys/kernel/shmmni
1723  */
1724 static int
linprocfs_doshmmni(PFS_FILL_ARGS)1725 linprocfs_doshmmni(PFS_FILL_ARGS)
1726 {
1727 
1728 	sbuf_printf(sb, "%lu\n", shminfo.shmmni);
1729 	return (0);
1730 }
1731 
1732 /*
1733  * Filler function for proc/sys/kernel/tainted
1734  */
1735 static int
linprocfs_dotainted(PFS_FILL_ARGS)1736 linprocfs_dotainted(PFS_FILL_ARGS)
1737 {
1738 
1739 	sbuf_printf(sb, "0\n");
1740 	return (0);
1741 }
1742 
1743 /*
1744  * Filler function for proc/sys/vm/min_free_kbytes
1745  *
1746  * This mirrors the approach in illumos to return zero for reads. Effectively,
1747  * it says, no memory is kept in reserve for "atomic allocations". This class
1748  * of allocation can be used at times when a thread cannot be suspended.
1749  */
1750 static int
linprocfs_dominfree(PFS_FILL_ARGS)1751 linprocfs_dominfree(PFS_FILL_ARGS)
1752 {
1753 
1754 	sbuf_printf(sb, "%d\n", 0);
1755 	return (0);
1756 }
1757 
1758 /*
1759  * Filler function for proc/scsi/device_info
1760  */
1761 static int
linprocfs_doscsidevinfo(PFS_FILL_ARGS)1762 linprocfs_doscsidevinfo(PFS_FILL_ARGS)
1763 {
1764 
1765 	return (0);
1766 }
1767 
1768 /*
1769  * Filler function for proc/scsi/scsi
1770  */
1771 static int
linprocfs_doscsiscsi(PFS_FILL_ARGS)1772 linprocfs_doscsiscsi(PFS_FILL_ARGS)
1773 {
1774 
1775 	return (0);
1776 }
1777 
1778 /*
1779  * Filler function for proc/devices
1780  */
1781 static int
linprocfs_dodevices(PFS_FILL_ARGS)1782 linprocfs_dodevices(PFS_FILL_ARGS)
1783 {
1784 	char *char_devices;
1785 	sbuf_printf(sb, "Character devices:\n");
1786 
1787 	char_devices = linux_get_char_devices();
1788 	sbuf_printf(sb, "%s", char_devices);
1789 	linux_free_get_char_devices(char_devices);
1790 
1791 	sbuf_printf(sb, "\nBlock devices:\n");
1792 
1793 	return (0);
1794 }
1795 
1796 /*
1797  * Filler function for proc/cmdline
1798  */
1799 static int
linprocfs_docmdline(PFS_FILL_ARGS)1800 linprocfs_docmdline(PFS_FILL_ARGS)
1801 {
1802 
1803 	sbuf_printf(sb, "BOOT_IMAGE=%s", kernelname);
1804 	sbuf_printf(sb, " ro root=302\n");
1805 	return (0);
1806 }
1807 
1808 /*
1809  * Filler function for proc/filesystems
1810  */
1811 static int
linprocfs_dofilesystems(PFS_FILL_ARGS)1812 linprocfs_dofilesystems(PFS_FILL_ARGS)
1813 {
1814 	struct vfsconf *vfsp;
1815 
1816 	vfsconf_slock();
1817 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
1818 		if (vfsp->vfc_flags & VFCF_SYNTHETIC)
1819 			sbuf_printf(sb, "nodev");
1820 		sbuf_printf(sb, "\t%s\n", vfsp->vfc_name);
1821 	}
1822 	vfsconf_sunlock();
1823 	return(0);
1824 }
1825 
1826 /*
1827  * Filler function for proc/modules
1828  */
1829 static int
linprocfs_domodules(PFS_FILL_ARGS)1830 linprocfs_domodules(PFS_FILL_ARGS)
1831 {
1832 #if 0
1833 	struct linker_file *lf;
1834 
1835 	TAILQ_FOREACH(lf, &linker_files, link) {
1836 		sbuf_printf(sb, "%-20s%8lu%4d\n", lf->filename,
1837 		    (unsigned long)lf->size, lf->refs);
1838 	}
1839 #endif
1840 	return (0);
1841 }
1842 
1843 /*
1844  * Filler function for proc/pid/fd
1845  */
1846 static int
linprocfs_dofdescfs(PFS_FILL_ARGS)1847 linprocfs_dofdescfs(PFS_FILL_ARGS)
1848 {
1849 
1850 	if (p == curproc)
1851 		sbuf_printf(sb, "/dev/fd");
1852 	else
1853 		sbuf_printf(sb, "unknown");
1854 	return (0);
1855 }
1856 
1857 /*
1858  * Filler function for proc/pid/limits
1859  */
1860 static const struct linux_rlimit_ident {
1861 	const char	*desc;
1862 	const char	*unit;
1863 	unsigned int	rlim_id;
1864 } linux_rlimits_ident[] = {
1865 	{ "Max cpu time",	"seconds",	RLIMIT_CPU },
1866 	{ "Max file size", 	"bytes",	RLIMIT_FSIZE },
1867 	{ "Max data size",	"bytes", 	RLIMIT_DATA },
1868 	{ "Max stack size",	"bytes", 	RLIMIT_STACK },
1869 	{ "Max core file size",  "bytes",	RLIMIT_CORE },
1870 	{ "Max resident set",	"bytes",	RLIMIT_RSS },
1871 	{ "Max processes",	"processes",	RLIMIT_NPROC },
1872 	{ "Max open files",	"files",	RLIMIT_NOFILE },
1873 	{ "Max locked memory",	"bytes",	RLIMIT_MEMLOCK },
1874 	{ "Max address space",	"bytes",	RLIMIT_AS },
1875 	{ "Max file locks",	"locks",	LINUX_RLIMIT_LOCKS },
1876 	{ "Max pending signals", "signals",	LINUX_RLIMIT_SIGPENDING },
1877 	{ "Max msgqueue size",	"bytes",	LINUX_RLIMIT_MSGQUEUE },
1878 	{ "Max nice priority", 		"",	LINUX_RLIMIT_NICE },
1879 	{ "Max realtime priority",	"",	LINUX_RLIMIT_RTPRIO },
1880 	{ "Max realtime timeout",	"us",	LINUX_RLIMIT_RTTIME },
1881 	{ 0, 0, 0 }
1882 };
1883 
1884 static int
linprocfs_doproclimits(PFS_FILL_ARGS)1885 linprocfs_doproclimits(PFS_FILL_ARGS)
1886 {
1887 	const struct linux_rlimit_ident *li;
1888 	struct plimit *limp;
1889 	struct rlimit rl;
1890 	ssize_t size;
1891 	int res, error;
1892 
1893 	error = 0;
1894 
1895 	PROC_LOCK(p);
1896 	limp = lim_hold(p->p_limit);
1897 	PROC_UNLOCK(p);
1898 	size = sizeof(res);
1899 	sbuf_printf(sb, "%-26s%-21s%-21s%-21s\n", "Limit", "Soft Limit",
1900 			"Hard Limit", "Units");
1901 	for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
1902 		switch (li->rlim_id)
1903 		{
1904 		case LINUX_RLIMIT_LOCKS:
1905 			/* FALLTHROUGH */
1906 		case LINUX_RLIMIT_RTTIME:
1907 			rl.rlim_cur = RLIM_INFINITY;
1908 			break;
1909 		case LINUX_RLIMIT_SIGPENDING:
1910 			error = kernel_sysctlbyname(td,
1911 			    "kern.sigqueue.max_pending_per_proc",
1912 			    &res, &size, 0, 0, 0, 0);
1913 			if (error != 0)
1914 				goto out;
1915 			rl.rlim_cur = res;
1916 			rl.rlim_max = res;
1917 			break;
1918 		case LINUX_RLIMIT_MSGQUEUE:
1919 			error = kernel_sysctlbyname(td,
1920 			    "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1921 			if (error != 0)
1922 				goto out;
1923 			rl.rlim_cur = res;
1924 			rl.rlim_max = res;
1925 			break;
1926 		case LINUX_RLIMIT_NICE:
1927 			/* FALLTHROUGH */
1928 		case LINUX_RLIMIT_RTPRIO:
1929 			rl.rlim_cur = 0;
1930 			rl.rlim_max = 0;
1931 			break;
1932 		default:
1933 			rl = limp->pl_rlimit[li->rlim_id];
1934 			break;
1935 		}
1936 		if (rl.rlim_cur == RLIM_INFINITY)
1937 			sbuf_printf(sb, "%-26s%-21s%-21s%-10s\n",
1938 			    li->desc, "unlimited", "unlimited", li->unit);
1939 		else
1940 			sbuf_printf(sb, "%-26s%-21llu%-21llu%-10s\n",
1941 			    li->desc, (unsigned long long)rl.rlim_cur,
1942 			    (unsigned long long)rl.rlim_max, li->unit);
1943 	}
1944 out:
1945 	lim_free(limp);
1946 	return (error);
1947 }
1948 
1949 /*
1950  * The point of the following two functions is to work around
1951  * an assertion in Chromium; see kern/240991 for details.
1952  */
1953 static int
linprocfs_dotaskattr(PFS_ATTR_ARGS)1954 linprocfs_dotaskattr(PFS_ATTR_ARGS)
1955 {
1956 
1957 	vap->va_nlink = 3;
1958 	return (0);
1959 }
1960 
1961 /*
1962  * Filler function for proc/<pid>/task/.dummy
1963  */
1964 static int
linprocfs_dotaskdummy(PFS_FILL_ARGS)1965 linprocfs_dotaskdummy(PFS_FILL_ARGS)
1966 {
1967 
1968 	return (0);
1969 }
1970 
1971 /*
1972  * Filler function for proc/sys/kernel/random/uuid
1973  */
1974 static int
linprocfs_douuid(PFS_FILL_ARGS)1975 linprocfs_douuid(PFS_FILL_ARGS)
1976 {
1977 	struct uuid uuid;
1978 
1979 	kern_uuidgen(&uuid, 1);
1980 	sbuf_printf_uuid(sb, &uuid);
1981 	sbuf_printf(sb, "\n");
1982 	return(0);
1983 }
1984 
1985 /*
1986  * Filler function for proc/sys/kernel/random/boot_id
1987  */
1988 static int
linprocfs_doboot_id(PFS_FILL_ARGS)1989 linprocfs_doboot_id(PFS_FILL_ARGS)
1990 {
1991        static bool firstboot = 1;
1992        static struct uuid uuid;
1993 
1994        if (firstboot) {
1995                kern_uuidgen(&uuid, 1);
1996                firstboot = 0;
1997        }
1998        sbuf_printf_uuid(sb, &uuid);
1999        sbuf_printf(sb, "\n");
2000        return(0);
2001 }
2002 
2003 /*
2004  * Filler function for proc/pid/auxv
2005  */
2006 static int
linprocfs_doauxv(PFS_FILL_ARGS)2007 linprocfs_doauxv(PFS_FILL_ARGS)
2008 {
2009 	struct sbuf *asb;
2010 	off_t buflen, resid;
2011 	int error;
2012 
2013 	/*
2014 	 * Mimic linux behavior and pass only processes with usermode
2015 	 * address space as valid. Return zero silently otherwise.
2016 	 */
2017 	if (p->p_vmspace == &vmspace0)
2018 		return (0);
2019 
2020 	if (uio->uio_resid == 0)
2021 		return (0);
2022 	if (uio->uio_offset < 0 || uio->uio_resid < 0)
2023 		return (EINVAL);
2024 
2025 	asb = sbuf_new_auto();
2026 	if (asb == NULL)
2027 		return (ENOMEM);
2028 	error = proc_getauxv(td, p, asb);
2029 	if (error == 0)
2030 		error = sbuf_finish(asb);
2031 
2032 	resid = sbuf_len(asb) - uio->uio_offset;
2033 	if (resid > uio->uio_resid)
2034 		buflen = uio->uio_resid;
2035 	else
2036 		buflen = resid;
2037 	if (buflen > IOSIZE_MAX)
2038 		return (EINVAL);
2039 	if (buflen > maxphys)
2040 		buflen = maxphys;
2041 	if (resid <= 0)
2042 		return (0);
2043 
2044 	if (error == 0)
2045 		error = uiomove(sbuf_data(asb) + uio->uio_offset, buflen, uio);
2046 	sbuf_delete(asb);
2047 	return (error);
2048 }
2049 
2050 /*
2051  * Filler function for proc/self/oom_score_adj
2052  */
2053 static int
linprocfs_do_oom_score_adj(PFS_FILL_ARGS)2054 linprocfs_do_oom_score_adj(PFS_FILL_ARGS)
2055 {
2056 	struct linux_pemuldata *pem;
2057 	long oom;
2058 
2059 	pem = pem_find(p);
2060 	if (pem == NULL || uio == NULL)
2061 		return (EOPNOTSUPP);
2062 	if (uio->uio_rw == UIO_READ) {
2063 		sbuf_printf(sb, "%d\n", pem->oom_score_adj);
2064 	} else {
2065 		sbuf_trim(sb);
2066 		sbuf_finish(sb);
2067 		oom = strtol(sbuf_data(sb), NULL, 10);
2068 		if (oom < LINUX_OOM_SCORE_ADJ_MIN ||
2069 		    oom > LINUX_OOM_SCORE_ADJ_MAX)
2070 			return (EINVAL);
2071 		pem->oom_score_adj = oom;
2072 	}
2073 	return (0);
2074 }
2075 
2076 /*
2077  * Filler function for proc/sys/vm/max_map_count
2078  *
2079  * Maximum number of active map areas, on Linux this limits the number
2080  * of vmaps per mm struct. We don't limit mappings, return a suitable
2081  * large value.
2082  */
2083 static int
linprocfs_domax_map_cnt(PFS_FILL_ARGS)2084 linprocfs_domax_map_cnt(PFS_FILL_ARGS)
2085 {
2086 
2087 	sbuf_printf(sb, "%d\n", INT32_MAX);
2088 	return (0);
2089 }
2090 
2091 /*
2092  * Filler function for proc/sysvipc/msg
2093  */
2094 static int
linprocfs_dosysvipc_msg(PFS_FILL_ARGS)2095 linprocfs_dosysvipc_msg(PFS_FILL_ARGS)
2096 {
2097 	struct msqid_kernel *msqids;
2098 	size_t id, size;
2099 	int error;
2100 
2101 	sbuf_printf(sb,
2102 	    "%10s %10s %4s  %10s %10s %5s %5s %5s %5s %5s %5s %10s %10s %10s\n",
2103 	    "key", "msqid", "perms", "cbytes", "qnum", "lspid", "lrpid",
2104 	    "uid", "gid", "cuid", "cgid", "stime", "rtime", "ctime");
2105 
2106 	error = kern_get_msqids(curthread, &msqids, &size);
2107 	if (error != 0)
2108 		return (error);
2109 
2110 	for (id = 0; id < size; id++) {
2111 		if (msqids[id].u.msg_qbytes == 0)
2112 			continue;
2113 		sbuf_printf(sb,
2114 		    "%10d %10zu  %4o  %10lu %10lu %5u %5u %5u %5u %5u %5u %jd %jd %jd\n",
2115 		    (int)msqids[id].u.msg_perm.key,
2116 		    IXSEQ_TO_IPCID(id, msqids[id].u.msg_perm),
2117 		    msqids[id].u.msg_perm.mode,
2118 		    msqids[id].u.msg_cbytes,
2119 		    msqids[id].u.msg_qnum,
2120 		    msqids[id].u.msg_lspid,
2121 		    msqids[id].u.msg_lrpid,
2122 		    msqids[id].u.msg_perm.uid,
2123 		    msqids[id].u.msg_perm.gid,
2124 		    msqids[id].u.msg_perm.cuid,
2125 		    msqids[id].u.msg_perm.cgid,
2126 		    (intmax_t)msqids[id].u.msg_stime,
2127 		    (intmax_t)msqids[id].u.msg_rtime,
2128 		    (intmax_t)msqids[id].u.msg_ctime);
2129 	}
2130 
2131 	free(msqids, M_TEMP);
2132 	return (0);
2133 }
2134 
2135 /*
2136  * Filler function for proc/sysvipc/sem
2137  */
2138 static int
linprocfs_dosysvipc_sem(PFS_FILL_ARGS)2139 linprocfs_dosysvipc_sem(PFS_FILL_ARGS)
2140 {
2141 	struct semid_kernel *semids;
2142 	size_t id, size;
2143 	int error;
2144 
2145 	sbuf_printf(sb, "%10s %10s %4s %10s %5s %5s %5s %5s %10s %10s\n",
2146 	    "key", "semid", "perms", "nsems", "uid", "gid", "cuid", "cgid",
2147 	    "otime", "ctime");
2148 
2149 	error = kern_get_sema(curthread, &semids, &size);
2150 	if (error != 0)
2151 		return (error);
2152 
2153 	for (id = 0; id < size; id++) {
2154 		if ((semids[id].u.sem_perm.mode & SEM_ALLOC) == 0)
2155 			continue;
2156 		sbuf_printf(sb,
2157 		    "%10d %10zu  %4o %10u %5u %5u %5u %5u %jd %jd\n",
2158 		    (int)semids[id].u.sem_perm.key,
2159 		    IXSEQ_TO_IPCID(id, semids[id].u.sem_perm),
2160 		    semids[id].u.sem_perm.mode,
2161 		    semids[id].u.sem_nsems,
2162 		    semids[id].u.sem_perm.uid,
2163 		    semids[id].u.sem_perm.gid,
2164 		    semids[id].u.sem_perm.cuid,
2165 		    semids[id].u.sem_perm.cgid,
2166 		    (intmax_t)semids[id].u.sem_otime,
2167 		    (intmax_t)semids[id].u.sem_ctime);
2168 	}
2169 
2170 	free(semids, M_TEMP);
2171 	return (0);
2172 }
2173 
2174 /*
2175  * Filler function for proc/sysvipc/shm
2176  */
2177 static int
linprocfs_dosysvipc_shm(PFS_FILL_ARGS)2178 linprocfs_dosysvipc_shm(PFS_FILL_ARGS)
2179 {
2180 	struct shmid_kernel *shmids;
2181 	size_t id, size;
2182 	int error;
2183 
2184 	sbuf_printf(sb,
2185 	    "%10s %10s %s %21s %5s %5s %5s %5s %5s %5s %5s %10s %10s %10s %21s %21s\n",
2186 	    "key", "shmid", "perms", "size", "cpid", "lpid", "nattch", "uid",
2187 	    "gid", "cuid", "cgid", "atime", "dtime", "ctime", "rss", "swap");
2188 
2189 	error = kern_get_shmsegs(curthread, &shmids, &size);
2190 	if (error != 0)
2191 		return (error);
2192 
2193 	for (id = 0; id < size; id++) {
2194 		if ((shmids[id].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0)
2195 			continue;
2196 		sbuf_printf(sb,
2197 		    "%10d %10zu  %4o %21zu %5u %5u  %5u %5u %5u %5u %5u %jd %jd %jd %21d %21d\n",
2198 		    (int)shmids[id].u.shm_perm.key,
2199 		    IXSEQ_TO_IPCID(id, shmids[id].u.shm_perm),
2200 		    shmids[id].u.shm_perm.mode,
2201 		    shmids[id].u.shm_segsz,
2202 		    shmids[id].u.shm_cpid,
2203 		    shmids[id].u.shm_lpid,
2204 		    shmids[id].u.shm_nattch,
2205 		    shmids[id].u.shm_perm.uid,
2206 		    shmids[id].u.shm_perm.gid,
2207 		    shmids[id].u.shm_perm.cuid,
2208 		    shmids[id].u.shm_perm.cgid,
2209 		    (intmax_t)shmids[id].u.shm_atime,
2210 		    (intmax_t)shmids[id].u.shm_dtime,
2211 		    (intmax_t)shmids[id].u.shm_ctime,
2212 		    0, 0);	/* XXX rss & swp are not supported */
2213 	}
2214 
2215 	free(shmids, M_TEMP);
2216 	return (0);
2217 }
2218 
2219 /*
2220  * Filler function for proc/sys/fs/mqueue/msg_default
2221  */
2222 static int
linprocfs_domqueue_msg_default(PFS_FILL_ARGS)2223 linprocfs_domqueue_msg_default(PFS_FILL_ARGS)
2224 {
2225 	int res, error;
2226 	size_t size = sizeof(res);
2227 
2228 	error = kernel_sysctlbyname(curthread, "kern.mqueue.default_maxmsg",
2229 	    &res, &size, NULL, 0, 0, 0);
2230 	if (error != 0)
2231 		return (error);
2232 
2233 	sbuf_printf(sb, "%d\n", res);
2234 	return (0);
2235 }
2236 
2237 /*
2238  * Filler function for proc/sys/fs/mqueue/msgsize_default
2239  */
2240 static int
linprocfs_domqueue_msgsize_default(PFS_FILL_ARGS)2241 linprocfs_domqueue_msgsize_default(PFS_FILL_ARGS)
2242 {
2243 	int res, error;
2244 	size_t size = sizeof(res);
2245 
2246 	error = kernel_sysctlbyname(curthread, "kern.mqueue.default_msgsize",
2247 	    &res, &size, NULL, 0, 0, 0);
2248 	if (error != 0)
2249 		return (error);
2250 
2251 	sbuf_printf(sb, "%d\n", res);
2252 	return (0);
2253 
2254 }
2255 
2256 /*
2257  * Filler function for proc/sys/fs/mqueue/msg_max
2258  */
2259 static int
linprocfs_domqueue_msg_max(PFS_FILL_ARGS)2260 linprocfs_domqueue_msg_max(PFS_FILL_ARGS)
2261 {
2262 	int res, error;
2263 	size_t size = sizeof(res);
2264 
2265 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmsg",
2266 	    &res, &size, NULL, 0, 0, 0);
2267 	if (error != 0)
2268 		return (error);
2269 
2270 	sbuf_printf(sb, "%d\n", res);
2271 	return (0);
2272 }
2273 
2274 /*
2275  * Filler function for proc/sys/fs/mqueue/msgsize_max
2276  */
2277 static int
linprocfs_domqueue_msgsize_max(PFS_FILL_ARGS)2278 linprocfs_domqueue_msgsize_max(PFS_FILL_ARGS)
2279 {
2280 	int res, error;
2281 	size_t size = sizeof(res);
2282 
2283 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmsgsize",
2284 	    &res, &size, NULL, 0, 0, 0);
2285 	if (error != 0)
2286 		return (error);
2287 
2288 	sbuf_printf(sb, "%d\n", res);
2289 	return (0);
2290 }
2291 
2292 /*
2293  * Filler function for proc/sys/fs/mqueue/queues_max
2294  */
2295 static int
linprocfs_domqueue_queues_max(PFS_FILL_ARGS)2296 linprocfs_domqueue_queues_max(PFS_FILL_ARGS)
2297 {
2298 	int res, error;
2299 	size_t size = sizeof(res);
2300 
2301 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmq",
2302 	    &res, &size, NULL, 0, 0, 0);
2303 	if (error != 0)
2304 		return (error);
2305 
2306 	sbuf_printf(sb, "%d\n", res);
2307 	return (0);
2308 }
2309 
2310 /*
2311  * Constructor
2312  */
2313 static int
linprocfs_init(PFS_INIT_ARGS)2314 linprocfs_init(PFS_INIT_ARGS)
2315 {
2316 	struct pfs_node *root;
2317 	struct pfs_node *dir;
2318 	struct pfs_node *sys;
2319 
2320 	root = pi->pi_root;
2321 
2322 	/* /proc/... */
2323 	pfs_create_file(root, "cmdline", &linprocfs_docmdline,
2324 	    NULL, NULL, NULL, PFS_RD);
2325 	pfs_create_file(root, "cpuinfo", &linprocfs_docpuinfo,
2326 	    NULL, NULL, NULL, PFS_RD);
2327 	pfs_create_file(root, "devices", &linprocfs_dodevices,
2328 	    NULL, NULL, NULL, PFS_RD);
2329 	pfs_create_file(root, "filesystems", &linprocfs_dofilesystems,
2330 	    NULL, NULL, NULL, PFS_RD);
2331 	pfs_create_file(root, "loadavg", &linprocfs_doloadavg,
2332 	    NULL, NULL, NULL, PFS_RD);
2333 	pfs_create_file(root, "meminfo", &linprocfs_domeminfo,
2334 	    NULL, NULL, NULL, PFS_RD);
2335 	pfs_create_file(root, "modules", &linprocfs_domodules,
2336 	    NULL, NULL, NULL, PFS_RD);
2337 	pfs_create_file(root, "mounts", &linprocfs_domtab,
2338 	    NULL, NULL, NULL, PFS_RD);
2339 	pfs_create_file(root, "mtab", &linprocfs_domtab,
2340 	    NULL, NULL, NULL, PFS_RD);
2341 	pfs_create_file(root, "partitions", &linprocfs_dopartitions,
2342 	    NULL, NULL, NULL, PFS_RD);
2343 	pfs_create_link(root, "self", &procfs_docurproc,
2344 	    NULL, NULL, NULL, 0);
2345 	pfs_create_file(root, "stat", &linprocfs_dostat,
2346 	    NULL, NULL, NULL, PFS_RD);
2347 	pfs_create_file(root, "swaps", &linprocfs_doswaps,
2348 	    NULL, NULL, NULL, PFS_RD);
2349 	pfs_create_file(root, "uptime", &linprocfs_douptime,
2350 	    NULL, NULL, NULL, PFS_RD);
2351 	pfs_create_file(root, "version", &linprocfs_doversion,
2352 	    NULL, NULL, NULL, PFS_RD);
2353 
2354 	/* /proc/bus/... */
2355 	dir = pfs_create_dir(root, "bus", NULL, NULL, NULL, 0);
2356 	dir = pfs_create_dir(dir, "pci", NULL, NULL, NULL, 0);
2357 	dir = pfs_create_dir(dir, "devices", NULL, NULL, NULL, 0);
2358 
2359 	/* /proc/net/... */
2360 	dir = pfs_create_dir(root, "net", NULL, NULL, NULL, 0);
2361 	pfs_create_file(dir, "dev", &linprocfs_donetdev,
2362 	    NULL, NULL, NULL, PFS_RD);
2363 	pfs_create_file(dir, "route", &linprocfs_donetroute,
2364 	    NULL, NULL, NULL, PFS_RD);
2365 
2366 	/* /proc/<pid>/... */
2367 	dir = pfs_create_dir(root, "pid", NULL, NULL, NULL, PFS_PROCDEP);
2368 	pfs_create_file(dir, "cmdline", &linprocfs_doproccmdline,
2369 	    NULL, NULL, NULL, PFS_RD);
2370 	pfs_create_link(dir, "cwd", &linprocfs_doproccwd,
2371 	    NULL, NULL, NULL, 0);
2372 	pfs_create_file(dir, "environ", &linprocfs_doprocenviron,
2373 	    NULL, &procfs_candebug, NULL, PFS_RD);
2374 	pfs_create_link(dir, "exe", &procfs_doprocfile,
2375 	    NULL, &procfs_notsystem, NULL, 0);
2376 	pfs_create_file(dir, "maps", &linprocfs_doprocmaps,
2377 	    NULL, NULL, NULL, PFS_RD | PFS_AUTODRAIN);
2378 	pfs_create_file(dir, "mem", &linprocfs_doprocmem,
2379 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
2380 	pfs_create_file(dir, "mountinfo", &linprocfs_doprocmountinfo,
2381 	    NULL, NULL, NULL, PFS_RD);
2382 	pfs_create_file(dir, "mounts", &linprocfs_domtab,
2383 	    NULL, NULL, NULL, PFS_RD);
2384 	pfs_create_link(dir, "root", &linprocfs_doprocroot,
2385 	    NULL, NULL, NULL, 0);
2386 	pfs_create_file(dir, "stat", &linprocfs_doprocstat,
2387 	    NULL, NULL, NULL, PFS_RD);
2388 	pfs_create_file(dir, "statm", &linprocfs_doprocstatm,
2389 	    NULL, NULL, NULL, PFS_RD);
2390 	pfs_create_file(dir, "status", &linprocfs_doprocstatus,
2391 	    NULL, NULL, NULL, PFS_RD);
2392 	pfs_create_link(dir, "fd", &linprocfs_dofdescfs,
2393 	    NULL, NULL, NULL, 0);
2394 	pfs_create_file(dir, "auxv", &linprocfs_doauxv,
2395 	    NULL, &procfs_candebug, NULL, PFS_RD|PFS_RAWRD);
2396 	pfs_create_file(dir, "limits", &linprocfs_doproclimits,
2397 	    NULL, NULL, NULL, PFS_RD);
2398 	pfs_create_file(dir, "oom_score_adj", &linprocfs_do_oom_score_adj,
2399 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR);
2400 
2401 	/* /proc/<pid>/task/... */
2402 	dir = pfs_create_dir(dir, "task", linprocfs_dotaskattr, NULL, NULL, 0);
2403 	pfs_create_file(dir, ".dummy", &linprocfs_dotaskdummy,
2404 	    NULL, NULL, NULL, PFS_RD);
2405 
2406 	/* /proc/scsi/... */
2407 	dir = pfs_create_dir(root, "scsi", NULL, NULL, NULL, 0);
2408 	pfs_create_file(dir, "device_info", &linprocfs_doscsidevinfo,
2409 	    NULL, NULL, NULL, PFS_RD);
2410 	pfs_create_file(dir, "scsi", &linprocfs_doscsiscsi,
2411 	    NULL, NULL, NULL, PFS_RD);
2412 
2413 	/* /proc/sys/... */
2414 	sys = pfs_create_dir(root, "sys", NULL, NULL, NULL, 0);
2415 
2416 	/* /proc/sys/kernel/... */
2417 	dir = pfs_create_dir(sys, "kernel", NULL, NULL, NULL, 0);
2418 	pfs_create_file(dir, "osrelease", &linprocfs_doosrelease,
2419 	    NULL, NULL, NULL, PFS_RD);
2420 	pfs_create_file(dir, "ostype", &linprocfs_doostype,
2421 	    NULL, NULL, NULL, PFS_RD);
2422 	pfs_create_file(dir, "version", &linprocfs_doosbuild,
2423 	    NULL, NULL, NULL, PFS_RD);
2424 	pfs_create_file(dir, "msgmax", &linprocfs_domsgmax,
2425 	    NULL, NULL, NULL, PFS_RD);
2426 	pfs_create_file(dir, "msgmni", &linprocfs_domsgmni,
2427 	    NULL, NULL, NULL, PFS_RD);
2428 	pfs_create_file(dir, "msgmnb", &linprocfs_domsgmnb,
2429 	    NULL, NULL, NULL, PFS_RD);
2430 	pfs_create_file(dir, "ngroups_max", &linprocfs_dongroups_max,
2431 	    NULL, NULL, NULL, PFS_RD);
2432 	pfs_create_file(dir, "pid_max", &linprocfs_dopid_max,
2433 	    NULL, NULL, NULL, PFS_RD);
2434 	pfs_create_file(dir, "sem", &linprocfs_dosem,
2435 	    NULL, NULL, NULL, PFS_RD);
2436 	pfs_create_file(dir, "shmall", &linprocfs_doshmall,
2437 	    NULL, NULL, NULL, PFS_RD);
2438 	pfs_create_file(dir, "shmmax", &linprocfs_doshmmax,
2439 	    NULL, NULL, NULL, PFS_RD);
2440 	pfs_create_file(dir, "shmmni", &linprocfs_doshmmni,
2441 	    NULL, NULL, NULL, PFS_RD);
2442 	pfs_create_file(dir, "tainted", &linprocfs_dotainted,
2443 	    NULL, NULL, NULL, PFS_RD);
2444 
2445 	/* /proc/sys/kernel/random/... */
2446 	dir = pfs_create_dir(dir, "random", NULL, NULL, NULL, 0);
2447 	pfs_create_file(dir, "uuid", &linprocfs_douuid,
2448 	    NULL, NULL, NULL, PFS_RD);
2449 	pfs_create_file(dir, "boot_id", &linprocfs_doboot_id,
2450 	    NULL, NULL, NULL, PFS_RD);
2451 
2452 	/* /proc/sys/vm/.... */
2453 	dir = pfs_create_dir(sys, "vm", NULL, NULL, NULL, 0);
2454 	pfs_create_file(dir, "min_free_kbytes", &linprocfs_dominfree,
2455 	    NULL, NULL, NULL, PFS_RD);
2456 	pfs_create_file(dir, "max_map_count", &linprocfs_domax_map_cnt,
2457 	    NULL, NULL, NULL, PFS_RD);
2458 
2459 	/* /proc/sysvipc/... */
2460 	dir = pfs_create_dir(root, "sysvipc", NULL, NULL, NULL, 0);
2461 	pfs_create_file(dir, "msg", &linprocfs_dosysvipc_msg,
2462 	    NULL, NULL, NULL, PFS_RD);
2463 	pfs_create_file(dir, "sem", &linprocfs_dosysvipc_sem,
2464 	    NULL, NULL, NULL, PFS_RD);
2465 	pfs_create_file(dir, "shm", &linprocfs_dosysvipc_shm,
2466 	    NULL, NULL, NULL, PFS_RD);
2467 
2468 	/* /proc/sys/fs/... */
2469 	dir = pfs_create_dir(sys, "fs", NULL, NULL, NULL, 0);
2470 
2471 	/* /proc/sys/fs/mqueue/... */
2472 	dir = pfs_create_dir(dir, "mqueue", NULL, NULL, NULL, 0);
2473 	pfs_create_file(dir, "msg_default", &linprocfs_domqueue_msg_default,
2474 	    NULL, NULL, NULL, PFS_RD);
2475 	pfs_create_file(dir, "msgsize_default", &linprocfs_domqueue_msgsize_default,
2476 	    NULL, NULL, NULL, PFS_RD);
2477 	pfs_create_file(dir, "msg_max", &linprocfs_domqueue_msg_max,
2478 	    NULL, NULL, NULL, PFS_RD);
2479 	pfs_create_file(dir, "msgsize_max", &linprocfs_domqueue_msgsize_max,
2480 	    NULL, NULL, NULL, PFS_RD);
2481 	pfs_create_file(dir, "queues_max", &linprocfs_domqueue_queues_max,
2482 	    NULL, NULL, NULL, PFS_RD);
2483 
2484 	return (0);
2485 }
2486 
2487 /*
2488  * Destructor
2489  */
2490 static int
linprocfs_uninit(PFS_INIT_ARGS)2491 linprocfs_uninit(PFS_INIT_ARGS)
2492 {
2493 
2494 	/* nothing to do, pseudofs will GC */
2495 	return (0);
2496 }
2497 
2498 PSEUDOFS(linprocfs, 1, VFCF_JAIL);
2499 #if defined(__aarch64__) || defined(__amd64__)
2500 MODULE_DEPEND(linprocfs, linux_common, 1, 1, 1);
2501 #else
2502 MODULE_DEPEND(linprocfs, linux, 1, 1, 1);
2503 #endif
2504 MODULE_DEPEND(linprocfs, procfs, 1, 1, 1);
2505 MODULE_DEPEND(linprocfs, sysvmsg, 1, 1, 1);
2506 MODULE_DEPEND(linprocfs, sysvsem, 1, 1, 1);
2507 MODULE_DEPEND(linprocfs, sysvshm, 1, 1, 1);
2508