xref: /freebsd/sys/compat/linprocfs/linprocfs.c (revision 4f9f09d95103e1db6b7c75fce7cf3e8c448321e9)
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\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
linprocfs_dostat(PFS_FILL_ARGS)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
linprocfs_doswaps(PFS_FILL_ARGS)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
linprocfs_douptime(PFS_FILL_ARGS)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
linprocfs_osbuild(struct thread * td,struct sbuf * sb)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
linprocfs_osbuilder(struct thread * td,struct sbuf * sb)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
linprocfs_doversion(PFS_FILL_ARGS)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
linprocfs_doloadavg(PFS_FILL_ARGS)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
linprocfs_get_tty_nr(struct proc * p)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
linprocfs_doprocstat(PFS_FILL_ARGS)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
linprocfs_doprocstatm(PFS_FILL_ARGS)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
linprocfs_doprocstatus(PFS_FILL_ARGS)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
linprocfs_doproccwd(PFS_FILL_ARGS)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
linprocfs_doprocroot(PFS_FILL_ARGS)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
linprocfs_doproccmdline(PFS_FILL_ARGS)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
linprocfs_doprocenviron(PFS_FILL_ARGS)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
linprocfs_doprocmaps(PFS_FILL_ARGS)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 	PROC_LOCK(p);
1315 	error = p_candebug(td, p);
1316 	PROC_UNLOCK(p);
1317 	if (error)
1318 		return (error);
1319 
1320 	if (uio->uio_rw != UIO_READ)
1321 		return (EOPNOTSUPP);
1322 
1323 	error = 0;
1324 	vm = vmspace_acquire_ref(p);
1325 	if (vm == NULL)
1326 		return (ESRCH);
1327 
1328 	if (SV_CURPROC_FLAG(SV_LP64))
1329 		l_map_str = l64_map_str;
1330 	else
1331 		l_map_str = l32_map_str;
1332 	map = &vm->vm_map;
1333 	vm_map_lock_read(map);
1334 	VM_MAP_ENTRY_FOREACH(entry, map) {
1335 		name = "";
1336 		freename = NULL;
1337 		/*
1338 		 * Skip printing of the guard page of the stack region, as
1339 		 * it confuses glibc pthread_getattr_np() method, where both
1340 		 * the base address and size of the stack of the initial thread
1341 		 * are calculated.
1342 		 */
1343 		if ((entry->eflags & (MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_GUARD)) != 0)
1344 			continue;
1345 		e_prot = entry->protection;
1346 		e_start = entry->start;
1347 		e_end = entry->end;
1348 		obj = entry->object.vm_object;
1349 		off = entry->offset;
1350 		for (lobj = tobj = obj; tobj != NULL;
1351 		    lobj = tobj, tobj = tobj->backing_object) {
1352 			VM_OBJECT_RLOCK(tobj);
1353 			off += lobj->backing_object_offset;
1354 			if (lobj != obj)
1355 				VM_OBJECT_RUNLOCK(lobj);
1356 		}
1357 		private = (entry->eflags & MAP_ENTRY_COW) != 0 || obj == NULL ||
1358 		    (obj->flags & OBJ_ANON) != 0;
1359 		last_timestamp = map->timestamp;
1360 		vm_map_unlock_read(map);
1361 		ino = 0;
1362 		if (lobj) {
1363 			vp = vm_object_vnode(lobj);
1364 			if (vp != NULL)
1365 				vref(vp);
1366 			if (lobj != obj)
1367 				VM_OBJECT_RUNLOCK(lobj);
1368 			VM_OBJECT_RUNLOCK(obj);
1369 			if (vp != NULL) {
1370 				vn_fullpath(vp, &name, &freename);
1371 				vn_lock(vp, LK_SHARED | LK_RETRY);
1372 				VOP_GETATTR(vp, &vat, td->td_ucred);
1373 				ino = vat.va_fileid;
1374 				vput(vp);
1375 			} else if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
1376 				/*
1377 				 * sv_shared_page_base pointed out to the
1378 				 * FreeBSD sharedpage, PAGE_SIZE is a size
1379 				 * of it. The vDSO page is above.
1380 				 */
1381 				if (e_start == p->p_sysent->sv_shared_page_base +
1382 				    PAGE_SIZE)
1383 					name = vdso_str;
1384 				if (e_end == p->p_sysent->sv_usrstack)
1385 					name = stack_str;
1386 			}
1387 		}
1388 
1389 		/*
1390 		 * format:
1391 		 *  start, end, access, offset, major, minor, inode, name.
1392 		 */
1393 		error = sbuf_printf(sb, l_map_str,
1394 		    (u_long)e_start, (u_long)e_end,
1395 		    (e_prot & VM_PROT_READ)?"r":"-",
1396 		    (e_prot & VM_PROT_WRITE)?"w":"-",
1397 		    (e_prot & VM_PROT_EXECUTE)?"x":"-",
1398 		    private ? "p" : "s",
1399 		    (u_long)off,
1400 		    0,
1401 		    0,
1402 		    (u_long)ino,
1403 		    *name ? "     " : " ",
1404 		    name
1405 		    );
1406 		if (freename)
1407 			free(freename, M_TEMP);
1408 		vm_map_lock_read(map);
1409 		if (error == -1) {
1410 			error = 0;
1411 			break;
1412 		}
1413 		if (last_timestamp != map->timestamp) {
1414 			/*
1415 			 * Look again for the entry because the map was
1416 			 * modified while it was unlocked.  Specifically,
1417 			 * the entry may have been clipped, merged, or deleted.
1418 			 */
1419 			vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
1420 			entry = tmp_entry;
1421 		}
1422 	}
1423 	vm_map_unlock_read(map);
1424 	vmspace_free(vm);
1425 
1426 	return (error);
1427 }
1428 
1429 /*
1430  * Filler function for proc/pid/mem
1431  */
1432 static int
linprocfs_doprocmem(PFS_FILL_ARGS)1433 linprocfs_doprocmem(PFS_FILL_ARGS)
1434 {
1435 	ssize_t resid;
1436 	int error;
1437 
1438 	resid = uio->uio_resid;
1439 	error = procfs_doprocmem(PFS_FILL_ARGNAMES);
1440 
1441 	if (uio->uio_rw == UIO_READ && resid != uio->uio_resid)
1442 		return (0);
1443 
1444 	if (error == EFAULT)
1445 		error = EIO;
1446 
1447 	return (error);
1448 }
1449 
1450 /*
1451  * Filler function for proc/net/dev
1452  */
1453 static int
linprocfs_donetdev_cb(if_t ifp,void * arg)1454 linprocfs_donetdev_cb(if_t ifp, void *arg)
1455 {
1456 	char ifname[LINUX_IFNAMSIZ];
1457 	struct sbuf *sb = arg;
1458 
1459 	if (ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname)) <= 0)
1460 		return (ENODEV);
1461 
1462 	sbuf_printf(sb, "%6.6s: ", ifname);
1463 	sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
1464 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IBYTES),
1465 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IPACKETS),
1466 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IERRORS),
1467 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IQDROPS),
1468 						/* rx_missed_errors */
1469 	    0UL,				/* rx_fifo_errors */
1470 	    0UL,				/* rx_length_errors +
1471 						 * rx_over_errors +
1472 						 * rx_crc_errors +
1473 						 * rx_frame_errors */
1474 	    0UL,				/* rx_compressed */
1475 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_IMCASTS));
1476 						/* XXX-BZ rx only? */
1477 	sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
1478 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OBYTES),
1479 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OPACKETS),
1480 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OERRORS),
1481 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_OQDROPS),
1482 	    0UL,				/* tx_fifo_errors */
1483 	    (uintmax_t)if_getcounter(ifp, IFCOUNTER_COLLISIONS),
1484 	    0UL,				/* tx_carrier_errors +
1485 						 * tx_aborted_errors +
1486 						 * tx_window_errors +
1487 						 * tx_heartbeat_errors*/
1488 	    0UL);				/* tx_compressed */
1489 	return (0);
1490 }
1491 
1492 static int
linprocfs_donetdev(PFS_FILL_ARGS)1493 linprocfs_donetdev(PFS_FILL_ARGS)
1494 {
1495 	struct epoch_tracker et;
1496 
1497 	sbuf_printf(sb, "%6s|%58s|%s\n"
1498 	    "%6s|%58s|%58s\n",
1499 	    "Inter-", "   Receive", "  Transmit",
1500 	    " face",
1501 	    "bytes    packets errs drop fifo frame compressed multicast",
1502 	    "bytes    packets errs drop fifo colls carrier compressed");
1503 
1504 	CURVNET_SET(TD_TO_VNET(curthread));
1505 	NET_EPOCH_ENTER(et);
1506 	if_foreach(linprocfs_donetdev_cb, sb);
1507 	NET_EPOCH_EXIT(et);
1508 	CURVNET_RESTORE();
1509 
1510 	return (0);
1511 }
1512 
1513 struct walkarg {
1514 	struct sbuf *sb;
1515 };
1516 
1517 static int
linux_route_print(struct rtentry * rt,void * vw)1518 linux_route_print(struct rtentry *rt, void *vw)
1519 {
1520 #ifdef INET
1521 	struct walkarg *w = vw;
1522 	struct route_nhop_data rnd;
1523 	struct in_addr dst, mask;
1524 	struct nhop_object *nh;
1525 	char ifname[16];
1526 	uint32_t scopeid = 0;
1527 	uint32_t gw = 0;
1528 	uint32_t linux_flags = 0;
1529 
1530 	rt_get_inet_prefix_pmask(rt, &dst, &mask, &scopeid);
1531 
1532 	rt_get_rnd(rt, &rnd);
1533 
1534 	/* select only first route in case of multipath */
1535 	nh = nhop_select_func(rnd.rnd_nhop, 0);
1536 
1537 	if (ifname_bsd_to_linux_ifp(nh->nh_ifp, ifname, sizeof(ifname)) <= 0)
1538 		return (ENODEV);
1539 
1540 	gw = (nh->nh_flags & NHF_GATEWAY)
1541 		? nh->gw4_sa.sin_addr.s_addr : 0;
1542 
1543 	linux_flags = RTF_UP |
1544 		(nhop_get_rtflags(nh) & (RTF_GATEWAY | RTF_HOST));
1545 
1546 	sbuf_printf(w->sb,
1547 		"%s\t"
1548 		"%08X\t%08X\t%04X\t"
1549 		"%d\t%u\t%d\t"
1550 		"%08X\t%d\t%u\t%u",
1551 		ifname,
1552 		dst.s_addr, gw, linux_flags,
1553 		0, 0, rnd.rnd_weight,
1554 		mask.s_addr, nh->nh_mtu, 0, 0);
1555 
1556 	sbuf_printf(w->sb, "\n\n");
1557 #endif
1558 	return (0);
1559 }
1560 
1561 /*
1562  * Filler function for proc/net/route
1563  */
1564 static int
linprocfs_donetroute(PFS_FILL_ARGS)1565 linprocfs_donetroute(PFS_FILL_ARGS)
1566 {
1567 	struct epoch_tracker et;
1568 	struct walkarg w = {
1569 		.sb = sb
1570 	};
1571 	uint32_t fibnum = curthread->td_proc->p_fibnum;
1572 
1573 	sbuf_printf(w.sb, "%-127s\n", "Iface\tDestination\tGateway "
1574                "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1575                "\tWindow\tIRTT");
1576 
1577 	CURVNET_SET(TD_TO_VNET(curthread));
1578 	NET_EPOCH_ENTER(et);
1579 	rib_walk(fibnum, AF_INET, false, linux_route_print, &w);
1580 	NET_EPOCH_EXIT(et);
1581 	CURVNET_RESTORE();
1582 
1583 	return (0);
1584 }
1585 
1586 /*
1587  * Filler function for proc/sys/kernel/osrelease
1588  */
1589 static int
linprocfs_doosrelease(PFS_FILL_ARGS)1590 linprocfs_doosrelease(PFS_FILL_ARGS)
1591 {
1592 	char osrelease[LINUX_MAX_UTSNAME];
1593 
1594 	linux_get_osrelease(td, osrelease);
1595 	sbuf_printf(sb, "%s\n", osrelease);
1596 
1597 	return (0);
1598 }
1599 
1600 /*
1601  * Filler function for proc/sys/kernel/ostype
1602  */
1603 static int
linprocfs_doostype(PFS_FILL_ARGS)1604 linprocfs_doostype(PFS_FILL_ARGS)
1605 {
1606 	char osname[LINUX_MAX_UTSNAME];
1607 
1608 	linux_get_osname(td, osname);
1609 	sbuf_printf(sb, "%s\n", osname);
1610 
1611 	return (0);
1612 }
1613 
1614 /*
1615  * Filler function for proc/sys/kernel/version
1616  */
1617 static int
linprocfs_doosbuild(PFS_FILL_ARGS)1618 linprocfs_doosbuild(PFS_FILL_ARGS)
1619 {
1620 
1621 	linprocfs_osbuild(td, sb);
1622 	sbuf_cat(sb, "\n");
1623 	return (0);
1624 }
1625 
1626 /*
1627  * Filler function for proc/sys/kernel/msgmax
1628  */
1629 static int
linprocfs_domsgmax(PFS_FILL_ARGS)1630 linprocfs_domsgmax(PFS_FILL_ARGS)
1631 {
1632 
1633 	sbuf_printf(sb, "%d\n", msginfo.msgmax);
1634 	return (0);
1635 }
1636 
1637 /*
1638  * Filler function for proc/sys/kernel/msgmni
1639  */
1640 static int
linprocfs_domsgmni(PFS_FILL_ARGS)1641 linprocfs_domsgmni(PFS_FILL_ARGS)
1642 {
1643 
1644 	sbuf_printf(sb, "%d\n", msginfo.msgmni);
1645 	return (0);
1646 }
1647 
1648 /*
1649  * Filler function for proc/sys/kernel/msgmnb
1650  */
1651 static int
linprocfs_domsgmnb(PFS_FILL_ARGS)1652 linprocfs_domsgmnb(PFS_FILL_ARGS)
1653 {
1654 
1655 	sbuf_printf(sb, "%d\n", msginfo.msgmnb);
1656 	return (0);
1657 }
1658 
1659 /*
1660  * Filler function for proc/sys/kernel/ngroups_max
1661  *
1662  * Note that in Linux it defaults to 65536, not 1023.
1663  */
1664 static int
linprocfs_dongroups_max(PFS_FILL_ARGS)1665 linprocfs_dongroups_max(PFS_FILL_ARGS)
1666 {
1667 
1668 	sbuf_printf(sb, "%d\n", ngroups_max);
1669 	return (0);
1670 }
1671 
1672 /*
1673  * Filler function for proc/sys/kernel/pid_max
1674  */
1675 static int
linprocfs_dopid_max(PFS_FILL_ARGS)1676 linprocfs_dopid_max(PFS_FILL_ARGS)
1677 {
1678 
1679 	sbuf_printf(sb, "%i\n", PID_MAX);
1680 	return (0);
1681 }
1682 
1683 /*
1684  * Filler function for proc/sys/kernel/sem
1685  */
1686 static int
linprocfs_dosem(PFS_FILL_ARGS)1687 linprocfs_dosem(PFS_FILL_ARGS)
1688 {
1689 
1690 	sbuf_printf(sb, "%d %d %d %d\n", seminfo.semmsl, seminfo.semmns,
1691 	    seminfo.semopm, seminfo.semmni);
1692 	return (0);
1693 }
1694 
1695 /*
1696  * Filler function for proc/sys/kernel/shmall
1697  */
1698 static int
linprocfs_doshmall(PFS_FILL_ARGS)1699 linprocfs_doshmall(PFS_FILL_ARGS)
1700 {
1701 
1702 	sbuf_printf(sb, "%lu\n", shminfo.shmall);
1703 	return (0);
1704 }
1705 
1706 /*
1707  * Filler function for proc/sys/kernel/shmmax
1708  */
1709 static int
linprocfs_doshmmax(PFS_FILL_ARGS)1710 linprocfs_doshmmax(PFS_FILL_ARGS)
1711 {
1712 
1713 	sbuf_printf(sb, "%lu\n", shminfo.shmmax);
1714 	return (0);
1715 }
1716 
1717 /*
1718  * Filler function for proc/sys/kernel/shmmni
1719  */
1720 static int
linprocfs_doshmmni(PFS_FILL_ARGS)1721 linprocfs_doshmmni(PFS_FILL_ARGS)
1722 {
1723 
1724 	sbuf_printf(sb, "%lu\n", shminfo.shmmni);
1725 	return (0);
1726 }
1727 
1728 /*
1729  * Filler function for proc/sys/kernel/tainted
1730  */
1731 static int
linprocfs_dotainted(PFS_FILL_ARGS)1732 linprocfs_dotainted(PFS_FILL_ARGS)
1733 {
1734 
1735 	sbuf_printf(sb, "0\n");
1736 	return (0);
1737 }
1738 
1739 /*
1740  * Filler function for proc/sys/kernel/threads-max
1741  */
1742 static int
linprocfs_dothreads_max(PFS_FILL_ARGS)1743 linprocfs_dothreads_max(PFS_FILL_ARGS)
1744 {
1745 	int res, error;
1746 	size_t size = sizeof(res);
1747 
1748 	error = kernel_sysctlbyname(curthread, "kern.maxproc",
1749 	    &res, &size, NULL, 0, 0, 0);
1750 	if (error != 0)
1751 		return (error);
1752 	sbuf_printf(sb, "%d\n", res);
1753 	return (0);
1754 }
1755 
1756 /*
1757  * Filler function for proc/sys/vm/min_free_kbytes
1758  *
1759  * This mirrors the approach in illumos to return zero for reads. Effectively,
1760  * it says, no memory is kept in reserve for "atomic allocations". This class
1761  * of allocation can be used at times when a thread cannot be suspended.
1762  */
1763 static int
linprocfs_dominfree(PFS_FILL_ARGS)1764 linprocfs_dominfree(PFS_FILL_ARGS)
1765 {
1766 
1767 	sbuf_printf(sb, "%d\n", 0);
1768 	return (0);
1769 }
1770 
1771 /*
1772  * Filler function for proc/scsi/device_info
1773  */
1774 static int
linprocfs_doscsidevinfo(PFS_FILL_ARGS)1775 linprocfs_doscsidevinfo(PFS_FILL_ARGS)
1776 {
1777 
1778 	return (0);
1779 }
1780 
1781 /*
1782  * Filler function for proc/scsi/scsi
1783  */
1784 static int
linprocfs_doscsiscsi(PFS_FILL_ARGS)1785 linprocfs_doscsiscsi(PFS_FILL_ARGS)
1786 {
1787 
1788 	return (0);
1789 }
1790 
1791 /*
1792  * Filler function for proc/devices
1793  */
1794 static int
linprocfs_dodevices(PFS_FILL_ARGS)1795 linprocfs_dodevices(PFS_FILL_ARGS)
1796 {
1797 	char *char_devices;
1798 	sbuf_printf(sb, "Character devices:\n");
1799 
1800 	char_devices = linux_get_char_devices();
1801 	sbuf_printf(sb, "%s", char_devices);
1802 	linux_free_get_char_devices(char_devices);
1803 
1804 	sbuf_printf(sb, "\nBlock devices:\n");
1805 
1806 	return (0);
1807 }
1808 
1809 /*
1810  * Filler function for proc/cmdline
1811  */
1812 static int
linprocfs_docmdline(PFS_FILL_ARGS)1813 linprocfs_docmdline(PFS_FILL_ARGS)
1814 {
1815 
1816 	sbuf_printf(sb, "BOOT_IMAGE=%s", kernelname);
1817 	sbuf_printf(sb, " ro root=302\n");
1818 	return (0);
1819 }
1820 
1821 /*
1822  * Filler function for proc/filesystems
1823  */
1824 static int
linprocfs_dofilesystems(PFS_FILL_ARGS)1825 linprocfs_dofilesystems(PFS_FILL_ARGS)
1826 {
1827 	struct vfsconf *vfsp;
1828 
1829 	vfsconf_slock();
1830 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
1831 		if (vfsp->vfc_flags & VFCF_SYNTHETIC)
1832 			sbuf_printf(sb, "nodev");
1833 		sbuf_printf(sb, "\t%s\n", vfsp->vfc_name);
1834 	}
1835 	vfsconf_sunlock();
1836 	return(0);
1837 }
1838 
1839 /*
1840  * Filler function for proc/modules
1841  */
1842 static int
linprocfs_domodules(PFS_FILL_ARGS)1843 linprocfs_domodules(PFS_FILL_ARGS)
1844 {
1845 #if 0
1846 	struct linker_file *lf;
1847 
1848 	TAILQ_FOREACH(lf, &linker_files, link) {
1849 		sbuf_printf(sb, "%-20s%8lu%4d\n", lf->filename,
1850 		    (unsigned long)lf->size, lf->refs);
1851 	}
1852 #endif
1853 	return (0);
1854 }
1855 
1856 /*
1857  * Filler function for proc/pid/fd
1858  */
1859 static int
linprocfs_dofdescfs(PFS_FILL_ARGS)1860 linprocfs_dofdescfs(PFS_FILL_ARGS)
1861 {
1862 
1863 	if (p == curproc)
1864 		sbuf_printf(sb, "/dev/fd");
1865 	else
1866 		sbuf_printf(sb, "unknown");
1867 	return (0);
1868 }
1869 
1870 /*
1871  * Filler function for proc/pid/limits
1872  */
1873 static const struct linux_rlimit_ident {
1874 	const char	*desc;
1875 	const char	*unit;
1876 	unsigned int	rlim_id;
1877 } linux_rlimits_ident[] = {
1878 	{ "Max cpu time",	"seconds",	RLIMIT_CPU },
1879 	{ "Max file size", 	"bytes",	RLIMIT_FSIZE },
1880 	{ "Max data size",	"bytes", 	RLIMIT_DATA },
1881 	{ "Max stack size",	"bytes", 	RLIMIT_STACK },
1882 	{ "Max core file size",  "bytes",	RLIMIT_CORE },
1883 	{ "Max resident set",	"bytes",	RLIMIT_RSS },
1884 	{ "Max processes",	"processes",	RLIMIT_NPROC },
1885 	{ "Max open files",	"files",	RLIMIT_NOFILE },
1886 	{ "Max locked memory",	"bytes",	RLIMIT_MEMLOCK },
1887 	{ "Max address space",	"bytes",	RLIMIT_AS },
1888 	{ "Max file locks",	"locks",	LINUX_RLIMIT_LOCKS },
1889 	{ "Max pending signals", "signals",	LINUX_RLIMIT_SIGPENDING },
1890 	{ "Max msgqueue size",	"bytes",	LINUX_RLIMIT_MSGQUEUE },
1891 	{ "Max nice priority", 		"",	LINUX_RLIMIT_NICE },
1892 	{ "Max realtime priority",	"",	LINUX_RLIMIT_RTPRIO },
1893 	{ "Max realtime timeout",	"us",	LINUX_RLIMIT_RTTIME },
1894 	{ 0, 0, 0 }
1895 };
1896 
1897 static int
linprocfs_doproclimits(PFS_FILL_ARGS)1898 linprocfs_doproclimits(PFS_FILL_ARGS)
1899 {
1900 	const struct linux_rlimit_ident *li;
1901 	struct plimit *limp;
1902 	struct rlimit rl;
1903 	ssize_t size;
1904 	int res, error;
1905 
1906 	error = 0;
1907 
1908 	PROC_LOCK(p);
1909 	limp = lim_hold(p->p_limit);
1910 	PROC_UNLOCK(p);
1911 	size = sizeof(res);
1912 	sbuf_printf(sb, "%-26s%-21s%-21s%-21s\n", "Limit", "Soft Limit",
1913 			"Hard Limit", "Units");
1914 	for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
1915 		switch (li->rlim_id)
1916 		{
1917 		case LINUX_RLIMIT_LOCKS:
1918 			/* FALLTHROUGH */
1919 		case LINUX_RLIMIT_RTTIME:
1920 			rl.rlim_cur = RLIM_INFINITY;
1921 			break;
1922 		case LINUX_RLIMIT_SIGPENDING:
1923 			error = kernel_sysctlbyname(td,
1924 			    "kern.sigqueue.max_pending_per_proc",
1925 			    &res, &size, 0, 0, 0, 0);
1926 			if (error != 0)
1927 				continue;
1928 			rl.rlim_cur = res;
1929 			rl.rlim_max = res;
1930 			break;
1931 		case LINUX_RLIMIT_MSGQUEUE:
1932 			error = kernel_sysctlbyname(td,
1933 			    "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0);
1934 			if (error != 0)
1935 				continue;
1936 			rl.rlim_cur = res;
1937 			rl.rlim_max = res;
1938 			break;
1939 		case LINUX_RLIMIT_NICE:
1940 			/* FALLTHROUGH */
1941 		case LINUX_RLIMIT_RTPRIO:
1942 			rl.rlim_cur = 0;
1943 			rl.rlim_max = 0;
1944 			break;
1945 		default:
1946 			rl = limp->pl_rlimit[li->rlim_id];
1947 			break;
1948 		}
1949 		if (rl.rlim_cur == RLIM_INFINITY)
1950 			sbuf_printf(sb, "%-26s%-21s%-21s%-10s\n",
1951 			    li->desc, "unlimited", "unlimited", li->unit);
1952 		else
1953 			sbuf_printf(sb, "%-26s%-21llu%-21llu%-10s\n",
1954 			    li->desc, (unsigned long long)rl.rlim_cur,
1955 			    (unsigned long long)rl.rlim_max, li->unit);
1956 	}
1957 
1958 	lim_free(limp);
1959 	return (0);
1960 }
1961 
1962 /*
1963  * The point of the following two functions is to work around
1964  * an assertion in Chromium; see kern/240991 for details.
1965  */
1966 static int
linprocfs_dotaskattr(PFS_ATTR_ARGS)1967 linprocfs_dotaskattr(PFS_ATTR_ARGS)
1968 {
1969 
1970 	vap->va_nlink = 3;
1971 	return (0);
1972 }
1973 
1974 /*
1975  * Filler function for proc/<pid>/task/.dummy
1976  */
1977 static int
linprocfs_dotaskdummy(PFS_FILL_ARGS)1978 linprocfs_dotaskdummy(PFS_FILL_ARGS)
1979 {
1980 
1981 	return (0);
1982 }
1983 
1984 /*
1985  * Filler function for proc/sys/kernel/random/uuid
1986  */
1987 static int
linprocfs_douuid(PFS_FILL_ARGS)1988 linprocfs_douuid(PFS_FILL_ARGS)
1989 {
1990 	struct uuid uuid;
1991 
1992 	kern_uuidgen(&uuid, 1);
1993 	sbuf_printf_uuid(sb, &uuid);
1994 	sbuf_printf(sb, "\n");
1995 	return(0);
1996 }
1997 
1998 /*
1999  * Filler function for proc/sys/kernel/random/boot_id
2000  */
2001 static int
linprocfs_doboot_id(PFS_FILL_ARGS)2002 linprocfs_doboot_id(PFS_FILL_ARGS)
2003 {
2004        static bool firstboot = 1;
2005        static struct uuid uuid;
2006 
2007        if (firstboot) {
2008                kern_uuidgen(&uuid, 1);
2009                firstboot = 0;
2010        }
2011        sbuf_printf_uuid(sb, &uuid);
2012        sbuf_printf(sb, "\n");
2013        return(0);
2014 }
2015 
2016 /*
2017  * Filler function for proc/pid/auxv
2018  */
2019 static int
linprocfs_doauxv(PFS_FILL_ARGS)2020 linprocfs_doauxv(PFS_FILL_ARGS)
2021 {
2022 	struct sbuf *asb;
2023 	off_t buflen, resid;
2024 	int error;
2025 
2026 	/*
2027 	 * Mimic linux behavior and pass only processes with usermode
2028 	 * address space as valid. Return zero silently otherwise.
2029 	 */
2030 	if (p->p_vmspace == &vmspace0)
2031 		return (0);
2032 
2033 	if (uio->uio_resid == 0)
2034 		return (0);
2035 	if (uio->uio_offset < 0 || uio->uio_resid < 0)
2036 		return (EINVAL);
2037 
2038 	asb = sbuf_new_auto();
2039 	if (asb == NULL)
2040 		return (ENOMEM);
2041 	error = proc_getauxv(td, p, asb);
2042 	if (error != 0)
2043 		goto out;
2044 	error = sbuf_finish(asb);
2045 	if (error != 0)
2046 		goto out;
2047 
2048 	resid = sbuf_len(asb) - uio->uio_offset;
2049 	if (resid > uio->uio_resid)
2050 		buflen = uio->uio_resid;
2051 	else
2052 		buflen = resid;
2053 	if (buflen > IOSIZE_MAX) {
2054 		error = EINVAL;
2055 		goto out;
2056 	}
2057 	if (buflen > maxphys)
2058 		buflen = maxphys;
2059 	if (resid > 0)
2060 		error = uiomove(sbuf_data(asb) + uio->uio_offset, buflen, uio);
2061 out:
2062 	sbuf_delete(asb);
2063 	return (error);
2064 }
2065 
2066 /*
2067  * Filler function for proc/self/oom_score_adj
2068  */
2069 static int
linprocfs_do_oom_score_adj(PFS_FILL_ARGS)2070 linprocfs_do_oom_score_adj(PFS_FILL_ARGS)
2071 {
2072 	struct linux_pemuldata *pem;
2073 	long oom;
2074 
2075 	pem = pem_find(p);
2076 	if (pem == NULL || uio == NULL)
2077 		return (EOPNOTSUPP);
2078 	if (uio->uio_rw == UIO_READ) {
2079 		sbuf_printf(sb, "%d\n", pem->oom_score_adj);
2080 	} else {
2081 		sbuf_trim(sb);
2082 		sbuf_finish(sb);
2083 		oom = strtol(sbuf_data(sb), NULL, 10);
2084 		if (oom < LINUX_OOM_SCORE_ADJ_MIN ||
2085 		    oom > LINUX_OOM_SCORE_ADJ_MAX)
2086 			return (EINVAL);
2087 		pem->oom_score_adj = oom;
2088 	}
2089 	return (0);
2090 }
2091 
2092 /*
2093  * Filler function for proc/sys/vm/max_map_count
2094  *
2095  * Maximum number of active map areas, on Linux this limits the number
2096  * of vmaps per mm struct. We don't limit mappings, return a suitable
2097  * large value.
2098  */
2099 static int
linprocfs_domax_map_cnt(PFS_FILL_ARGS)2100 linprocfs_domax_map_cnt(PFS_FILL_ARGS)
2101 {
2102 
2103 	sbuf_printf(sb, "%d\n", INT32_MAX);
2104 	return (0);
2105 }
2106 
2107 /*
2108  * Filler function for proc/sysvipc/msg
2109  */
2110 static int
linprocfs_dosysvipc_msg(PFS_FILL_ARGS)2111 linprocfs_dosysvipc_msg(PFS_FILL_ARGS)
2112 {
2113 	struct msqid_kernel *msqids;
2114 	size_t id, size;
2115 	int error;
2116 
2117 	sbuf_printf(sb,
2118 	    "%10s %10s %4s  %10s %10s %5s %5s %5s %5s %5s %5s %10s %10s %10s\n",
2119 	    "key", "msqid", "perms", "cbytes", "qnum", "lspid", "lrpid",
2120 	    "uid", "gid", "cuid", "cgid", "stime", "rtime", "ctime");
2121 
2122 	error = kern_get_msqids(curthread, &msqids, &size);
2123 	if (error != 0)
2124 		return (error);
2125 
2126 	for (id = 0; id < size; id++) {
2127 		if (msqids[id].u.msg_qbytes == 0)
2128 			continue;
2129 		sbuf_printf(sb,
2130 		    "%10d %10zu  %4o  %10lu %10lu %5u %5u %5u %5u %5u %5u %jd %jd %jd\n",
2131 		    (int)msqids[id].u.msg_perm.key,
2132 		    IXSEQ_TO_IPCID(id, msqids[id].u.msg_perm),
2133 		    msqids[id].u.msg_perm.mode,
2134 		    msqids[id].u.msg_cbytes,
2135 		    msqids[id].u.msg_qnum,
2136 		    msqids[id].u.msg_lspid,
2137 		    msqids[id].u.msg_lrpid,
2138 		    msqids[id].u.msg_perm.uid,
2139 		    msqids[id].u.msg_perm.gid,
2140 		    msqids[id].u.msg_perm.cuid,
2141 		    msqids[id].u.msg_perm.cgid,
2142 		    (intmax_t)msqids[id].u.msg_stime,
2143 		    (intmax_t)msqids[id].u.msg_rtime,
2144 		    (intmax_t)msqids[id].u.msg_ctime);
2145 	}
2146 
2147 	free(msqids, M_TEMP);
2148 	return (0);
2149 }
2150 
2151 /*
2152  * Filler function for proc/sysvipc/sem
2153  */
2154 static int
linprocfs_dosysvipc_sem(PFS_FILL_ARGS)2155 linprocfs_dosysvipc_sem(PFS_FILL_ARGS)
2156 {
2157 	struct semid_kernel *semids;
2158 	size_t id, size;
2159 	int error;
2160 
2161 	sbuf_printf(sb, "%10s %10s %4s %10s %5s %5s %5s %5s %10s %10s\n",
2162 	    "key", "semid", "perms", "nsems", "uid", "gid", "cuid", "cgid",
2163 	    "otime", "ctime");
2164 
2165 	error = kern_get_sema(curthread, &semids, &size);
2166 	if (error != 0)
2167 		return (error);
2168 
2169 	for (id = 0; id < size; id++) {
2170 		if ((semids[id].u.sem_perm.mode & SEM_ALLOC) == 0)
2171 			continue;
2172 		sbuf_printf(sb,
2173 		    "%10d %10zu  %4o %10u %5u %5u %5u %5u %jd %jd\n",
2174 		    (int)semids[id].u.sem_perm.key,
2175 		    IXSEQ_TO_IPCID(id, semids[id].u.sem_perm),
2176 		    semids[id].u.sem_perm.mode,
2177 		    semids[id].u.sem_nsems,
2178 		    semids[id].u.sem_perm.uid,
2179 		    semids[id].u.sem_perm.gid,
2180 		    semids[id].u.sem_perm.cuid,
2181 		    semids[id].u.sem_perm.cgid,
2182 		    (intmax_t)semids[id].u.sem_otime,
2183 		    (intmax_t)semids[id].u.sem_ctime);
2184 	}
2185 
2186 	free(semids, M_TEMP);
2187 	return (0);
2188 }
2189 
2190 /*
2191  * Filler function for proc/sysvipc/shm
2192  */
2193 static int
linprocfs_dosysvipc_shm(PFS_FILL_ARGS)2194 linprocfs_dosysvipc_shm(PFS_FILL_ARGS)
2195 {
2196 	struct shmid_kernel *shmids;
2197 	size_t id, size;
2198 	int error;
2199 
2200 	sbuf_printf(sb,
2201 	    "%10s %10s %s %21s %5s %5s %5s %5s %5s %5s %5s %10s %10s %10s %21s %21s\n",
2202 	    "key", "shmid", "perms", "size", "cpid", "lpid", "nattch", "uid",
2203 	    "gid", "cuid", "cgid", "atime", "dtime", "ctime", "rss", "swap");
2204 
2205 	error = kern_get_shmsegs(curthread, &shmids, &size);
2206 	if (error != 0)
2207 		return (error);
2208 
2209 	for (id = 0; id < size; id++) {
2210 		if ((shmids[id].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0)
2211 			continue;
2212 		sbuf_printf(sb,
2213 		    "%10d %10zu  %4o %21zu %5u %5u  %5u %5u %5u %5u %5u %jd %jd %jd %21d %21d\n",
2214 		    (int)shmids[id].u.shm_perm.key,
2215 		    IXSEQ_TO_IPCID(id, shmids[id].u.shm_perm),
2216 		    shmids[id].u.shm_perm.mode,
2217 		    shmids[id].u.shm_segsz,
2218 		    shmids[id].u.shm_cpid,
2219 		    shmids[id].u.shm_lpid,
2220 		    shmids[id].u.shm_nattch,
2221 		    shmids[id].u.shm_perm.uid,
2222 		    shmids[id].u.shm_perm.gid,
2223 		    shmids[id].u.shm_perm.cuid,
2224 		    shmids[id].u.shm_perm.cgid,
2225 		    (intmax_t)shmids[id].u.shm_atime,
2226 		    (intmax_t)shmids[id].u.shm_dtime,
2227 		    (intmax_t)shmids[id].u.shm_ctime,
2228 		    0, 0);	/* XXX rss & swp are not supported */
2229 	}
2230 
2231 	free(shmids, M_TEMP);
2232 	return (0);
2233 }
2234 
2235 /*
2236  * Filler function for proc/sys/fs/file-max
2237  */
2238 static int
linprocfs_dofile_max(PFS_FILL_ARGS)2239 linprocfs_dofile_max(PFS_FILL_ARGS)
2240 {
2241 	int res, error;
2242 	size_t size = sizeof(res);
2243 
2244 	error = kernel_sysctlbyname(curthread, "kern.maxfiles",
2245 	    &res, &size, NULL, 0, 0, 0);
2246 	if (error != 0)
2247 		return (error);
2248 	sbuf_printf(sb, "%d\n", res);
2249 	return (0);
2250 }
2251 
2252 /*
2253  * Filler function for proc/sys/fs/file-nr
2254  */
2255 static int
linprocfs_dofile_nr(PFS_FILL_ARGS)2256 linprocfs_dofile_nr(PFS_FILL_ARGS)
2257 {
2258 	int openfiles, maxfiles, error;
2259 	size_t size;
2260 
2261 	size = sizeof(openfiles);
2262 	error = kernel_sysctlbyname(curthread, "kern.openfiles",
2263 	    &openfiles, &size, NULL, 0, 0, 0);
2264 	if (error != 0)
2265 		return (error);
2266 	size = sizeof(maxfiles);
2267 	error = kernel_sysctlbyname(curthread, "kern.maxfiles",
2268 	    &maxfiles, &size, NULL, 0, 0, 0);
2269 	if (error != 0)
2270 		return (error);
2271 	/*
2272 	 * From Linux's proc_sys_fs(5):
2273 	 * the "free file handles" value is always zero.
2274 	 */
2275 	sbuf_printf(sb, "%d\t0\t%d\n", openfiles, maxfiles);
2276 	return (0);
2277 }
2278 
2279 /*
2280  * Filler function for proc/sys/fs/nr_open
2281  */
2282 static int
linprocfs_donr_open(PFS_FILL_ARGS)2283 linprocfs_donr_open(PFS_FILL_ARGS)
2284 {
2285 	int res, error;
2286 	size_t size = sizeof(res);
2287 
2288 	error = kernel_sysctlbyname(curthread, "kern.maxfilesperproc",
2289 	    &res, &size, NULL, 0, 0, 0);
2290 	if (error != 0)
2291 		return (error);
2292 	sbuf_printf(sb, "%d\n", res);
2293 	return (0);
2294 }
2295 
2296 /*
2297  * Filler function for proc/sys/fs/overflowuid
2298  */
2299 static int
linprocfs_dooverflowuid(PFS_FILL_ARGS)2300 linprocfs_dooverflowuid(PFS_FILL_ARGS)
2301 {
2302 	sbuf_printf(sb, "%u\n", UID_NOBODY);
2303 	return (0);
2304 }
2305 
2306 /*
2307  * Filler function for proc/sys/fs/overflowgid
2308  */
2309 static int
linprocfs_dooverflowgid(PFS_FILL_ARGS)2310 linprocfs_dooverflowgid(PFS_FILL_ARGS)
2311 {
2312 	sbuf_printf(sb, "%u\n", GID_NOGROUP);
2313 	return (0);
2314 }
2315 
2316 /*
2317  * Filler function for proc/sys/fs/suid_dumpable
2318  */
2319 static int
linprocfs_dosuid_dumpable(PFS_FILL_ARGS)2320 linprocfs_dosuid_dumpable(PFS_FILL_ARGS)
2321 {
2322 	int res, error;
2323 	size_t size = sizeof(res);
2324 
2325 	error = kernel_sysctlbyname(curthread, "kern.sugid_coredump",
2326 	    &res, &size, NULL, 0, 0, 0);
2327 	if (error != 0)
2328 		return (error);
2329 	sbuf_printf(sb, "%d\n", res ? 1 : 0);
2330 	return (0);
2331 }
2332 
2333 /*
2334  * Filler function for proc/sys/fs/protected_hardlinks
2335  */
2336 static int
linprocfs_doprotected_hardlinks(PFS_FILL_ARGS)2337 linprocfs_doprotected_hardlinks(PFS_FILL_ARGS)
2338 {
2339 	int res, error;
2340 	size_t size = sizeof(res);
2341 
2342 	error = kernel_sysctlbyname(curthread,
2343 	    "security.bsd.hardlink_check_uid",
2344 	    &res, &size, NULL, 0, 0, 0);
2345 	if (error != 0)
2346 		return (error);
2347 	sbuf_printf(sb, "%d\n", res ? 1 : 0);
2348 	return (0);
2349 }
2350 
2351 static int
linprocfs_doinotify(const char * sysctl,PFS_FILL_ARGS)2352 linprocfs_doinotify(const char *sysctl, PFS_FILL_ARGS)
2353 {
2354 	size_t size;
2355 	int error, val;
2356 
2357 	if (uio->uio_rw == UIO_READ) {
2358 		size = sizeof(val);
2359 		error = kernel_sysctlbyname(curthread,
2360 		    __DECONST(void *, sysctl), &val, &size, NULL, 0, 0, 0);
2361 		if (error == 0)
2362 			sbuf_printf(sb, "%d\n", val);
2363 	} else {
2364 		char *endp, *newval;
2365 		long vall;
2366 
2367 		sbuf_trim(sb);
2368 		sbuf_finish(sb);
2369 		newval = sbuf_data(sb);
2370 		vall = strtol(newval, &endp, 10);
2371 		if (vall < 0 || vall > INT_MAX || endp == newval ||
2372 		    *endp != '\0')
2373 			return (EINVAL);
2374 		val = (int)vall;
2375 		error = kernel_sysctlbyname(curthread,
2376 		    __DECONST(void *, sysctl), NULL, NULL,
2377 		    &val, sizeof(val), 0, 0);
2378 	}
2379 	return (error);
2380 }
2381 
2382 /*
2383  * Filler function for proc/sys/fs/inotify/max_queued_events
2384  */
2385 static int
linprocfs_doinotify_max_queued_events(PFS_FILL_ARGS)2386 linprocfs_doinotify_max_queued_events(PFS_FILL_ARGS)
2387 {
2388 	return (linprocfs_doinotify("vfs.inotify.max_queued_events",
2389 	    PFS_FILL_ARGNAMES));
2390 }
2391 
2392 /*
2393  * Filler function for proc/sys/fs/inotify/max_user_instances
2394  */
2395 static int
linprocfs_doinotify_max_user_instances(PFS_FILL_ARGS)2396 linprocfs_doinotify_max_user_instances(PFS_FILL_ARGS)
2397 {
2398 	return (linprocfs_doinotify("vfs.inotify.max_user_instances",
2399 	    PFS_FILL_ARGNAMES));
2400 }
2401 
2402 /*
2403  * Filler function for proc/sys/fs/inotify/max_user_watches
2404  */
2405 static int
linprocfs_doinotify_max_user_watches(PFS_FILL_ARGS)2406 linprocfs_doinotify_max_user_watches(PFS_FILL_ARGS)
2407 {
2408 	return (linprocfs_doinotify("vfs.inotify.max_user_watches",
2409 	    PFS_FILL_ARGNAMES));
2410 }
2411 
2412 /*
2413  * Filler function for proc/sys/fs/mqueue/msg_default
2414  */
2415 static int
linprocfs_domqueue_msg_default(PFS_FILL_ARGS)2416 linprocfs_domqueue_msg_default(PFS_FILL_ARGS)
2417 {
2418 	int res, error;
2419 	size_t size = sizeof(res);
2420 
2421 	error = kernel_sysctlbyname(curthread, "kern.mqueue.default_maxmsg",
2422 	    &res, &size, NULL, 0, 0, 0);
2423 	if (error != 0)
2424 		return (error);
2425 
2426 	sbuf_printf(sb, "%d\n", res);
2427 	return (0);
2428 }
2429 
2430 /*
2431  * Filler function for proc/sys/fs/mqueue/msgsize_default
2432  */
2433 static int
linprocfs_domqueue_msgsize_default(PFS_FILL_ARGS)2434 linprocfs_domqueue_msgsize_default(PFS_FILL_ARGS)
2435 {
2436 	int res, error;
2437 	size_t size = sizeof(res);
2438 
2439 	error = kernel_sysctlbyname(curthread, "kern.mqueue.default_msgsize",
2440 	    &res, &size, NULL, 0, 0, 0);
2441 	if (error != 0)
2442 		return (error);
2443 
2444 	sbuf_printf(sb, "%d\n", res);
2445 	return (0);
2446 
2447 }
2448 
2449 /*
2450  * Filler function for proc/sys/fs/mqueue/msg_max
2451  */
2452 static int
linprocfs_domqueue_msg_max(PFS_FILL_ARGS)2453 linprocfs_domqueue_msg_max(PFS_FILL_ARGS)
2454 {
2455 	int res, error;
2456 	size_t size = sizeof(res);
2457 
2458 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmsg",
2459 	    &res, &size, NULL, 0, 0, 0);
2460 	if (error != 0)
2461 		return (error);
2462 
2463 	sbuf_printf(sb, "%d\n", res);
2464 	return (0);
2465 }
2466 
2467 /*
2468  * Filler function for proc/sys/fs/mqueue/msgsize_max
2469  */
2470 static int
linprocfs_domqueue_msgsize_max(PFS_FILL_ARGS)2471 linprocfs_domqueue_msgsize_max(PFS_FILL_ARGS)
2472 {
2473 	int res, error;
2474 	size_t size = sizeof(res);
2475 
2476 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmsgsize",
2477 	    &res, &size, NULL, 0, 0, 0);
2478 	if (error != 0)
2479 		return (error);
2480 
2481 	sbuf_printf(sb, "%d\n", res);
2482 	return (0);
2483 }
2484 
2485 /*
2486  * Filler function for proc/sys/fs/mqueue/queues_max
2487  */
2488 static int
linprocfs_domqueue_queues_max(PFS_FILL_ARGS)2489 linprocfs_domqueue_queues_max(PFS_FILL_ARGS)
2490 {
2491 	int res, error;
2492 	size_t size = sizeof(res);
2493 
2494 	error = kernel_sysctlbyname(curthread, "kern.mqueue.maxmq",
2495 	    &res, &size, NULL, 0, 0, 0);
2496 	if (error != 0)
2497 		return (error);
2498 
2499 	sbuf_printf(sb, "%d\n", res);
2500 	return (0);
2501 }
2502 
2503 /*
2504  * Constructor
2505  */
2506 static int
linprocfs_init(PFS_INIT_ARGS)2507 linprocfs_init(PFS_INIT_ARGS)
2508 {
2509 	struct pfs_node *dir, *fs, *root, *sys;
2510 
2511 	root = pi->pi_root;
2512 
2513 	/* /proc/... */
2514 	pfs_create_file(root, NULL, "cmdline", &linprocfs_docmdline, NULL, NULL,
2515 	    NULL, PFS_RD);
2516 	pfs_create_file(root, NULL, "cpuinfo", &linprocfs_docpuinfo, NULL, NULL,
2517 	    NULL, PFS_RD);
2518 	pfs_create_file(root, NULL, "devices", &linprocfs_dodevices, NULL, NULL,
2519 	    NULL, PFS_RD);
2520 	pfs_create_file(root, NULL, "filesystems", &linprocfs_dofilesystems,
2521 	    NULL, NULL, NULL, PFS_RD);
2522 	pfs_create_file(root, NULL, "loadavg", &linprocfs_doloadavg, NULL, NULL,
2523 	    NULL, PFS_RD);
2524 	pfs_create_file(root, NULL, "meminfo", &linprocfs_domeminfo, NULL, NULL,
2525 	    NULL, PFS_RD);
2526 	pfs_create_file(root, NULL, "modules", &linprocfs_domodules, NULL, NULL,
2527 	    NULL, PFS_RD);
2528 	pfs_create_file(root, NULL, "mounts", &linprocfs_domtab, NULL, NULL,
2529 	    NULL, PFS_RD);
2530 	pfs_create_file(root, NULL, "mtab", &linprocfs_domtab, NULL, NULL, NULL,
2531 	    PFS_RD);
2532 	pfs_create_file(root, NULL, "partitions", &linprocfs_dopartitions, NULL,
2533 	    NULL, NULL, PFS_RD);
2534 	pfs_create_link(root, NULL, "self", &procfs_docurproc, NULL, NULL, NULL,
2535 	    0);
2536 	pfs_create_file(root, NULL, "stat", &linprocfs_dostat, NULL, NULL, NULL,
2537 	    PFS_RD);
2538 	pfs_create_file(root, NULL, "swaps", &linprocfs_doswaps, NULL, NULL,
2539 	    NULL, PFS_RD);
2540 	pfs_create_file(root, NULL, "uptime", &linprocfs_douptime, NULL, NULL,
2541 	    NULL, PFS_RD);
2542 	pfs_create_file(root, NULL, "version", &linprocfs_doversion, NULL, NULL,
2543 	    NULL, PFS_RD);
2544 
2545 	/* /proc/bus/... */
2546 	pfs_create_dir(root, &dir, "bus", NULL, NULL, NULL, 0);
2547 	pfs_create_dir(dir, &dir, "pci", NULL, NULL, NULL, 0);
2548 	pfs_create_dir(dir, &dir, "devices", NULL, NULL, NULL, 0);
2549 
2550 	/* /proc/net/... */
2551 	pfs_create_dir(root, &dir, "net", NULL, NULL, NULL, 0);
2552 	pfs_create_file(dir, NULL, "dev", &linprocfs_donetdev, NULL, NULL, NULL,
2553 	    PFS_RD);
2554 	pfs_create_file(dir, NULL, "route", &linprocfs_donetroute, NULL, NULL,
2555 	    NULL, PFS_RD);
2556 
2557 	/* /proc/<pid>/... */
2558 	pfs_create_dir(root, &dir, "pid", NULL, NULL, NULL, PFS_PROCDEP);
2559 	pfs_create_file(dir, NULL, "cmdline", &linprocfs_doproccmdline, NULL,
2560 	    NULL, NULL, PFS_RD);
2561 	pfs_create_link(dir, NULL, "cwd", &linprocfs_doproccwd, NULL, NULL,
2562 	    NULL, 0);
2563 	pfs_create_file(dir, NULL, "environ", &linprocfs_doprocenviron, NULL,
2564 	    &procfs_candebug, NULL, PFS_RD);
2565 	pfs_create_link(dir, NULL, "exe", &procfs_doprocfile, NULL,
2566 	    &procfs_notsystem, NULL, 0);
2567 	pfs_create_file(dir, NULL, "maps", &linprocfs_doprocmaps, NULL, NULL,
2568 	    NULL, PFS_RD | PFS_AUTODRAIN);
2569 	pfs_create_file(dir, NULL, "mem", &linprocfs_doprocmem, procfs_attr_rw,
2570 	    &procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
2571 	pfs_create_file(dir, NULL, "mountinfo", &linprocfs_doprocmountinfo,
2572 	    NULL, NULL, NULL, PFS_RD);
2573 	pfs_create_file(dir, NULL, "mounts", &linprocfs_domtab, NULL, NULL,
2574 	    NULL, PFS_RD);
2575 	pfs_create_link(dir, NULL, "root", &linprocfs_doprocroot, NULL, NULL,
2576 	    NULL, 0);
2577 	pfs_create_file(dir, NULL, "stat", &linprocfs_doprocstat, NULL, NULL,
2578 	    NULL, PFS_RD);
2579 	pfs_create_file(dir, NULL, "statm", &linprocfs_doprocstatm, NULL, NULL,
2580 	    NULL, PFS_RD);
2581 	pfs_create_file(dir, NULL, "status", &linprocfs_doprocstatus, NULL,
2582 	    NULL, NULL, PFS_RD);
2583 	pfs_create_link(dir, NULL, "fd", &linprocfs_dofdescfs, NULL, NULL, NULL,
2584 	    0);
2585 	pfs_create_file(dir, NULL, "auxv", &linprocfs_doauxv, NULL,
2586 	    &procfs_candebug, NULL, PFS_RD | PFS_RAWRD);
2587 	pfs_create_file(dir, NULL, "limits", &linprocfs_doproclimits, NULL,
2588 	    NULL, NULL, PFS_RD);
2589 	pfs_create_file(dir, NULL, "oom_score_adj", &linprocfs_do_oom_score_adj,
2590 	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR);
2591 
2592 	/* /proc/<pid>/task/... */
2593 	pfs_create_dir(dir, &dir, "task", linprocfs_dotaskattr, NULL, NULL, 0);
2594 	pfs_create_file(dir, NULL, ".dummy", &linprocfs_dotaskdummy, NULL, NULL,
2595 	    NULL, PFS_RD);
2596 
2597 	/* /proc/scsi/... */
2598 	pfs_create_dir(root, &dir, "scsi", NULL, NULL, NULL, 0);
2599 	pfs_create_file(dir, NULL, "device_info", &linprocfs_doscsidevinfo,
2600 	    NULL, NULL, NULL, PFS_RD);
2601 	pfs_create_file(dir, NULL, "scsi", &linprocfs_doscsiscsi, NULL, NULL,
2602 	    NULL, PFS_RD);
2603 
2604 	/* /proc/sys/... */
2605 	pfs_create_dir(root, &sys, "sys", NULL, NULL, NULL, 0);
2606 
2607 	/* /proc/sys/kernel/... */
2608 	pfs_create_dir(sys, &dir, "kernel", NULL, NULL, NULL, 0);
2609 	pfs_create_file(dir, NULL, "osrelease", &linprocfs_doosrelease, NULL,
2610 	    NULL, NULL, PFS_RD);
2611 	pfs_create_file(dir, NULL, "ostype", &linprocfs_doostype, NULL, NULL,
2612 	    NULL, PFS_RD);
2613 	pfs_create_file(dir, NULL, "version", &linprocfs_doosbuild, NULL, NULL,
2614 	    NULL, PFS_RD);
2615 	pfs_create_file(dir, NULL, "msgmax", &linprocfs_domsgmax, NULL, NULL,
2616 	    NULL, PFS_RD);
2617 	pfs_create_file(dir, NULL, "msgmni", &linprocfs_domsgmni, NULL, NULL,
2618 	    NULL, PFS_RD);
2619 	pfs_create_file(dir, NULL, "msgmnb", &linprocfs_domsgmnb, NULL, NULL,
2620 	    NULL, PFS_RD);
2621 	pfs_create_file(dir, NULL, "ngroups_max", &linprocfs_dongroups_max,
2622 	    NULL, NULL, NULL, PFS_RD);
2623 	pfs_create_file(dir, NULL, "pid_max", &linprocfs_dopid_max, NULL, NULL,
2624 	    NULL, PFS_RD);
2625 	pfs_create_file(dir, NULL, "sem", &linprocfs_dosem, NULL, NULL, NULL,
2626 	    PFS_RD);
2627 	pfs_create_file(dir, NULL, "shmall", &linprocfs_doshmall, NULL, NULL,
2628 	    NULL, PFS_RD);
2629 	pfs_create_file(dir, NULL, "shmmax", &linprocfs_doshmmax, NULL, NULL,
2630 	    NULL, PFS_RD);
2631 	pfs_create_file(dir, NULL, "shmmni", &linprocfs_doshmmni, NULL, NULL,
2632 	    NULL, PFS_RD);
2633 	pfs_create_file(dir, NULL, "tainted", &linprocfs_dotainted, NULL, NULL,
2634 	    NULL, PFS_RD);
2635 	pfs_create_file(dir, NULL, "threads-max", &linprocfs_dothreads_max,
2636 	    NULL, NULL, NULL, PFS_RD);
2637 
2638 	/* /proc/sys/kernel/random/... */
2639 	pfs_create_dir(dir, &dir, "random", NULL, NULL, NULL, 0);
2640 	pfs_create_file(dir, NULL, "uuid", &linprocfs_douuid, NULL, NULL, NULL,
2641 	    PFS_RD);
2642 	pfs_create_file(dir, NULL, "boot_id", &linprocfs_doboot_id, NULL, NULL,
2643 	    NULL, PFS_RD);
2644 
2645 	/* /proc/sys/vm/.... */
2646 	pfs_create_dir(sys, &dir, "vm", NULL, NULL, NULL, 0);
2647 	pfs_create_file(dir, NULL, "min_free_kbytes", &linprocfs_dominfree,
2648 	    NULL, NULL, NULL, PFS_RD);
2649 	pfs_create_file(dir, NULL, "max_map_count", &linprocfs_domax_map_cnt,
2650 	    NULL, NULL, NULL, PFS_RD);
2651 
2652 	/* /proc/sysvipc/... */
2653 	pfs_create_dir(root, &dir, "sysvipc", NULL, NULL, NULL, 0);
2654 	pfs_create_file(dir, NULL, "msg", &linprocfs_dosysvipc_msg, NULL, NULL,
2655 	    NULL, PFS_RD);
2656 	pfs_create_file(dir, NULL, "sem", &linprocfs_dosysvipc_sem, NULL, NULL,
2657 	    NULL, PFS_RD);
2658 	pfs_create_file(dir, NULL, "shm", &linprocfs_dosysvipc_shm, NULL, NULL,
2659 	    NULL, PFS_RD);
2660 
2661 	/* /proc/sys/fs/... */
2662 	pfs_create_dir(sys, &fs, "fs", NULL, NULL, NULL, 0);
2663 
2664 	pfs_create_file(fs, NULL, "file-nr", &linprocfs_dofile_nr,
2665 	    NULL, NULL, NULL, PFS_RD);
2666 	pfs_create_file(fs, NULL, "file-max", &linprocfs_dofile_max,
2667 	    NULL, NULL, NULL, PFS_RD);
2668 	pfs_create_file(fs, NULL, "nr_open", &linprocfs_donr_open,
2669 	    NULL, NULL, NULL, PFS_RD);
2670 	pfs_create_file(fs, NULL, "overflowgid", &linprocfs_dooverflowgid,
2671 	    NULL, NULL, NULL, PFS_RD);
2672 	pfs_create_file(fs, NULL, "overflowuid", &linprocfs_dooverflowuid,
2673 	    NULL, NULL, NULL, PFS_RD);
2674 	pfs_create_file(fs, NULL, "protected_hardlinks",
2675 	    &linprocfs_doprotected_hardlinks, NULL, NULL, NULL, PFS_RD);
2676 	pfs_create_file(fs, NULL, "suid_dumpable", &linprocfs_dosuid_dumpable,
2677 	    NULL, NULL, NULL, PFS_RD);
2678 
2679 	pfs_create_dir(fs, &dir, "inotify", NULL, NULL, NULL, 0);
2680 	pfs_create_file(dir, NULL, "max_queued_events",
2681 	    &linprocfs_doinotify_max_queued_events, NULL, NULL, NULL, PFS_RDWR);
2682 	pfs_create_file(dir, NULL, "max_user_instances",
2683 	    &linprocfs_doinotify_max_user_instances, NULL, NULL, NULL, PFS_RDWR);
2684 	pfs_create_file(dir, NULL, "max_user_watches",
2685 	    &linprocfs_doinotify_max_user_watches, NULL, NULL, NULL, PFS_RDWR);
2686 
2687 	/* /proc/sys/fs/mqueue/... */
2688 	pfs_create_dir(fs, &dir, "mqueue", NULL, NULL, NULL, 0);
2689 	pfs_create_file(dir, NULL, "msg_default",
2690 	    &linprocfs_domqueue_msg_default, NULL, NULL, NULL, PFS_RD);
2691 	pfs_create_file(dir, NULL, "msgsize_default",
2692 	    &linprocfs_domqueue_msgsize_default, NULL, NULL, NULL, PFS_RD);
2693 	pfs_create_file(dir, NULL, "msg_max", &linprocfs_domqueue_msg_max, NULL,
2694 	    NULL, NULL, PFS_RD);
2695 	pfs_create_file(dir, NULL, "msgsize_max",
2696 	    &linprocfs_domqueue_msgsize_max, NULL, NULL, NULL, PFS_RD);
2697 	pfs_create_file(dir, NULL, "queues_max", &linprocfs_domqueue_queues_max,
2698 	    NULL, NULL, NULL, PFS_RD);
2699 
2700 	return (0);
2701 }
2702 
2703 /*
2704  * Destructor
2705  */
2706 static int
linprocfs_uninit(PFS_INIT_ARGS)2707 linprocfs_uninit(PFS_INIT_ARGS)
2708 {
2709 
2710 	/* nothing to do, pseudofs will GC */
2711 	return (0);
2712 }
2713 
2714 PSEUDOFS(linprocfs, 1, VFCF_JAIL);
2715 #if defined(__aarch64__) || defined(__amd64__)
2716 MODULE_DEPEND(linprocfs, linux_common, 1, 1, 1);
2717 #else
2718 MODULE_DEPEND(linprocfs, linux, 1, 1, 1);
2719 #endif
2720 MODULE_DEPEND(linprocfs, procfs, 1, 1, 1);
2721 MODULE_DEPEND(linprocfs, sysvmsg, 1, 1, 1);
2722 MODULE_DEPEND(linprocfs, sysvsem, 1, 1, 1);
2723 MODULE_DEPEND(linprocfs, sysvshm, 1, 1, 1);
2724