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