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