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