procfs_linux.c revision 1.74.4.1 1 /* $NetBSD: procfs_linux.c,v 1.74.4.1 2019/09/10 16:11:00 martin Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Frank van der Linden for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.74.4.1 2019/09/10 16:11:00 martin Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44 #include <sys/cpu.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/exec.h>
49 #include <sys/resource.h>
50 #include <sys/resourcevar.h>
51 #include <sys/signal.h>
52 #include <sys/signalvar.h>
53 #include <sys/tty.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/conf.h>
57 #include <sys/sysctl.h>
58 #include <sys/kauth.h>
59 #include <sys/filedesc.h>
60
61 #include <miscfs/procfs/procfs.h>
62
63 #include <compat/linux/common/linux_exec.h>
64 #include <compat/linux32/common/linux32_sysctl.h>
65
66 #include <uvm/uvm_extern.h>
67 #include <uvm/uvm.h>
68
69 extern struct devsw_conv *devsw_conv;
70 extern int max_devsw_convs;
71
72 #define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT)
73 #define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10))
74
75 #define LBFSZ (8 * 1024)
76
77 static void
78 get_proc_size_info(struct proc *p, struct vm_map *map, unsigned long *stext,
79 unsigned long *etext, unsigned long *sstack)
80 {
81 struct vm_map_entry *entry;
82
83 *stext = 0;
84 *etext = 0;
85 *sstack = 0;
86
87 vm_map_lock_read(map);
88
89 for (entry = map->header.next; entry != &map->header;
90 entry = entry->next) {
91 if (UVM_ET_ISSUBMAP(entry))
92 continue;
93 /* assume text is the first entry */
94 if (*stext == *etext) {
95 *stext = entry->start;
96 *etext = entry->end;
97 break;
98 }
99 }
100 #if defined(LINUX_USRSTACK32) && defined(USRSTACK32)
101 if (strcmp(p->p_emul->e_name, "linux32") == 0 &&
102 LINUX_USRSTACK32 < USRSTACK32)
103 *sstack = (unsigned long)LINUX_USRSTACK32;
104 else
105 #endif
106 #ifdef LINUX_USRSTACK
107 if (strcmp(p->p_emul->e_name, "linux") == 0 &&
108 LINUX_USRSTACK < USRSTACK)
109 *sstack = (unsigned long)LINUX_USRSTACK;
110 else
111 #endif
112 #ifdef USRSTACK32
113 if (strstr(p->p_emul->e_name, "32") != NULL)
114 *sstack = (unsigned long)USRSTACK32;
115 else
116 #endif
117 *sstack = (unsigned long)USRSTACK;
118
119 /*
120 * jdk 1.6 compares low <= addr && addr < high
121 * if we put addr == high, then the test fails
122 * so eat one page.
123 */
124 *sstack -= PAGE_SIZE;
125
126 vm_map_unlock_read(map);
127 }
128
129 /*
130 * Linux compatible /proc/meminfo. Only active when the -o linux
131 * mountflag is used.
132 */
133 int
134 procfs_domeminfo(struct lwp *curl, struct proc *p,
135 struct pfsnode *pfs, struct uio *uio)
136 {
137 char *bf;
138 int len;
139 int error = 0;
140
141 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
142
143 len = snprintf(bf, LBFSZ,
144 " total: used: free: shared: buffers: cached:\n"
145 "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n"
146 "Swap: %8lu %8lu %8lu\n"
147 "MemTotal: %8lu kB\n"
148 "MemFree: %8lu kB\n"
149 "MemShared: %8lu kB\n"
150 "Buffers: %8lu kB\n"
151 "Cached: %8lu kB\n"
152 "SwapTotal: %8lu kB\n"
153 "SwapFree: %8lu kB\n",
154 PGTOB(uvmexp.npages),
155 PGTOB(uvmexp.npages - uvmexp.free),
156 PGTOB(uvmexp.free),
157 0L,
158 PGTOB(uvmexp.filepages),
159 PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
160 PGTOB(uvmexp.swpages),
161 PGTOB(uvmexp.swpginuse),
162 PGTOB(uvmexp.swpages - uvmexp.swpginuse),
163 PGTOKB(uvmexp.npages),
164 PGTOKB(uvmexp.free),
165 0L,
166 PGTOKB(uvmexp.filepages),
167 PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
168 PGTOKB(uvmexp.swpages),
169 PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
170
171 if (len == 0)
172 goto out;
173
174 error = uiomove_frombuf(bf, len, uio);
175 out:
176 free(bf, M_TEMP);
177 return error;
178 }
179
180 /*
181 * Linux compatible /proc/devices. Only active when the -o linux
182 * mountflag is used.
183 */
184 int
185 procfs_dodevices(struct lwp *curl, struct proc *p,
186 struct pfsnode *pfs, struct uio *uio)
187 {
188 char *bf;
189 int offset = 0;
190 int i, error = ENAMETOOLONG;
191
192 /* XXX elad - may need filtering. */
193
194 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
195
196 offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
197 if (offset >= LBFSZ)
198 goto out;
199
200 mutex_enter(&device_lock);
201 for (i = 0; i < max_devsw_convs; i++) {
202 if ((devsw_conv[i].d_name == NULL) ||
203 (devsw_conv[i].d_cmajor == -1))
204 continue;
205
206 offset += snprintf(&bf[offset], LBFSZ - offset,
207 "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
208 if (offset >= LBFSZ) {
209 mutex_exit(&device_lock);
210 goto out;
211 }
212 }
213
214 offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
215 if (offset >= LBFSZ) {
216 mutex_exit(&device_lock);
217 goto out;
218 }
219
220 for (i = 0; i < max_devsw_convs; i++) {
221 if ((devsw_conv[i].d_name == NULL) ||
222 (devsw_conv[i].d_bmajor == -1))
223 continue;
224
225 offset += snprintf(&bf[offset], LBFSZ - offset,
226 "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
227 if (offset >= LBFSZ) {
228 mutex_exit(&device_lock);
229 goto out;
230 }
231 }
232 mutex_exit(&device_lock);
233
234 error = uiomove_frombuf(bf, offset, uio);
235 out:
236 free(bf, M_TEMP);
237 return error;
238 }
239
240 /*
241 * Linux compatible /proc/stat. Only active when the -o linux
242 * mountflag is used.
243 */
244 int
245 procfs_docpustat(struct lwp *curl, struct proc *p,
246 struct pfsnode *pfs, struct uio *uio)
247 {
248 char *bf;
249 int error;
250 int len;
251 #if defined(MULTIPROCESSOR)
252 struct cpu_info *ci;
253 CPU_INFO_ITERATOR cii;
254 #endif
255 int i;
256 uint64_t nintr;
257 uint64_t nswtch;
258
259 error = ENAMETOOLONG;
260 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
261
262 len = snprintf(bf, LBFSZ,
263 "cpu %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
264 curcpu()->ci_schedstate.spc_cp_time[CP_USER],
265 curcpu()->ci_schedstate.spc_cp_time[CP_NICE],
266 curcpu()->ci_schedstate.spc_cp_time[CP_SYS] /*+ [CP_INTR]*/,
267 curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]);
268 if (len == 0)
269 goto out;
270
271 #if defined(MULTIPROCESSOR)
272 #define ALLCPUS CPU_INFO_FOREACH(cii, ci)
273 #define CPUNAME ci
274 #else
275 #define ALLCPUS ; i < 1 ;
276 #define CPUNAME curcpu()
277 #endif
278
279 i = 0;
280 nintr = 0;
281 nswtch = 0;
282 for (ALLCPUS) {
283 len += snprintf(&bf[len], LBFSZ - len,
284 "cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
285 "\n", i,
286 CPUNAME->ci_schedstate.spc_cp_time[CP_USER],
287 CPUNAME->ci_schedstate.spc_cp_time[CP_NICE],
288 CPUNAME->ci_schedstate.spc_cp_time[CP_SYS],
289 CPUNAME->ci_schedstate.spc_cp_time[CP_IDLE]);
290 if (len >= LBFSZ)
291 goto out;
292 i += 1;
293 nintr += CPUNAME->ci_data.cpu_nintr;
294 nswtch += CPUNAME->ci_data.cpu_nswtch;
295 }
296
297 len += snprintf(&bf[len], LBFSZ - len,
298 "disk 0 0 0 0\n"
299 "page %u %u\n"
300 "swap %u %u\n"
301 "intr %"PRIu64"\n"
302 "ctxt %"PRIu64"\n"
303 "btime %"PRId64"\n",
304 uvmexp.pageins, uvmexp.pdpageouts,
305 uvmexp.pgswapin, uvmexp.pgswapout,
306 nintr,
307 nswtch,
308 boottime.tv_sec);
309 if (len >= LBFSZ)
310 goto out;
311
312 error = uiomove_frombuf(bf, len, uio);
313 out:
314 free(bf, M_TEMP);
315 return error;
316 }
317
318 /*
319 * Linux compatible /proc/loadavg. Only active when the -o linux
320 * mountflag is used.
321 */
322 int
323 procfs_doloadavg(struct lwp *curl, struct proc *p,
324 struct pfsnode *pfs, struct uio *uio)
325 {
326 char *bf;
327 int error;
328 int len;
329
330 error = ENAMETOOLONG;
331 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
332
333 averunnable.fscale = FSCALE;
334 len = snprintf(bf, LBFSZ,
335 "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
336 (int)(averunnable.ldavg[0] / averunnable.fscale),
337 (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
338 (int)(averunnable.ldavg[1] / averunnable.fscale),
339 (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
340 (int)(averunnable.ldavg[2] / averunnable.fscale),
341 (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
342 1, /* number of ONPROC processes */
343 nprocs,
344 30000); /* last pid */
345 if (len == 0)
346 goto out;
347
348 error = uiomove_frombuf(bf, len, uio);
349 out:
350 free(bf, M_TEMP);
351 return error;
352 }
353
354 /*
355 * Linux compatible /proc/<pid>/statm. Only active when the -o linux
356 * mountflag is used.
357 */
358 int
359 procfs_do_pid_statm(struct lwp *curl, struct lwp *l,
360 struct pfsnode *pfs, struct uio *uio)
361 {
362 struct vmspace *vm;
363 struct proc *p = l->l_proc;
364 char *bf;
365 int error;
366 int len;
367 struct kinfo_proc2 ki;
368
369 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
370
371 /* XXX - we use values from vmspace, since dsl says that ru figures
372 are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */
373 if ((error = proc_vmspace_getref(p, &vm)) != 0) {
374 goto out;
375 }
376
377 mutex_enter(proc_lock);
378 mutex_enter(p->p_lock);
379
380 /* retrieve RSS size */
381 fill_kproc2(p, &ki, false, false);
382
383 mutex_exit(p->p_lock);
384 mutex_exit(proc_lock);
385
386 uvmspace_free(vm);
387
388 len = snprintf(bf, LBFSZ,
389 "%lu %lu %lu %lu %lu %lu %lu\n",
390 (unsigned long)(ki.p_vm_msize), /* size */
391 (unsigned long)(ki.p_vm_rssize),/* resident */
392 (unsigned long)(ki.p_uru_ixrss),/* shared */
393 (unsigned long)(ki.p_vm_tsize), /* text */
394 (unsigned long) 0, /* library (unused) */
395 (unsigned long)(ki.p_vm_dsize + ki.p_vm_ssize), /* data+stack */
396 (unsigned long) 0); /* dirty */
397
398 if (len == 0)
399 goto out;
400
401 error = uiomove_frombuf(bf, len, uio);
402 out:
403 free(bf, M_TEMP);
404 return error;
405 }
406
407 #define UTIME2TICKS(s,u) (((uint64_t)(s) * 1000000 + (u)) / 10000)
408
409 /*
410 * Linux compatible /proc/<pid>/stat. Only active when the -o linux
411 * mountflag is used.
412 */
413 int
414 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
415 struct pfsnode *pfs, struct uio *uio)
416 {
417 char *bf;
418 struct proc *p = l->l_proc;
419 int len;
420 struct rusage *cru = &p->p_stats->p_cru;
421 unsigned long stext = 0, etext = 0, sstack = 0;
422 struct timeval rt;
423 struct vmspace *vm;
424 struct kinfo_proc2 ki;
425 int error;
426
427 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
428
429 if ((error = proc_vmspace_getref(p, &vm)) != 0) {
430 goto out;
431 }
432
433 get_proc_size_info(p, &vm->vm_map, &stext, &etext, &sstack);
434
435 mutex_enter(proc_lock);
436 mutex_enter(p->p_lock);
437
438 fill_kproc2(p, &ki, false, false);
439 calcru(p, NULL, NULL, NULL, &rt);
440
441 len = snprintf(bf, LBFSZ,
442 "%d (%s) %c %d %d %d %u %d "
443 "%u "
444 "%"PRIu64" %lu %"PRIu64" %lu %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" "
445 "%d %d %"PRIu64" "
446 "%lld %"PRIu64" %"PRId64" %lu %"PRIu64" "
447 "%lu %lu %lu "
448 "%u %u "
449 "%u %u %u %u "
450 "%"PRIu64" %"PRIu64" %"PRIu64" %d %"PRIu64"\n",
451
452 ki.p_pid, /* 1 pid */
453 ki.p_comm, /* 2 tcomm */
454 "0RRSTZXR8"[(ki.p_stat > 8) ? 0 : (int)ki.p_stat], /* 3 state */
455 ki.p_ppid, /* 4 ppid */
456 ki.p__pgid, /* 5 pgrp */
457 ki.p_sid, /* 6 sid */
458 (ki.p_tdev != (uint32_t)NODEV) ? ki.p_tdev : 0, /* 7 tty_nr */
459 ki.p_tpgid, /* 8 tty_pgrp */
460
461 ki.p_flag, /* 9 flags */
462
463 ki.p_uru_minflt, /* 10 min_flt */
464 cru->ru_minflt,
465 ki.p_uru_majflt, /* 12 maj_flt */
466 cru->ru_majflt,
467 UTIME2TICKS(ki.p_uutime_sec, ki.p_uutime_usec), /* 14 utime */
468 UTIME2TICKS(ki.p_ustime_sec, ki.p_ustime_usec), /* 15 stime */
469 UTIME2TICKS(cru->ru_utime.tv_sec, cru->ru_utime.tv_usec), /* 16 cutime */
470 UTIME2TICKS(cru->ru_stime.tv_sec, cru->ru_stime.tv_usec), /* 17 cstime */
471
472 ki.p_priority, /* XXX: 18 priority */
473 ki.p_nice - NZERO, /* 19 nice */
474 ki.p_nlwps, /* 20 num_threads */
475
476 (long long)rt.tv_sec,
477 UTIME2TICKS(ki.p_ustart_sec, ki.p_ustart_usec), /* 22 start_time */
478 ki.p_vm_msize, /* 23 vsize */
479 PGTOKB(ki.p_vm_rssize), /* 24 rss */
480 p->p_rlimit[RLIMIT_RSS].rlim_cur, /* 25 rsslim */
481
482 stext, /* 26 start_code */
483 etext, /* 27 end_code */
484 sstack, /* 28 start_stack */
485
486 0, /* XXX: 29 esp */
487 0, /* XXX: 30 eip */
488
489 ki.p_siglist.__bits[0], /* XXX: 31 pending */
490 0, /* XXX: 32 blocked */
491 ki.p_sigignore.__bits[0], /* 33 sigign */
492 ki.p_sigcatch.__bits[0], /* 34 sigcatch */
493
494 ki.p_wchan, /* 35 wchan */
495 ki.p_uru_nvcsw,
496 ki.p_uru_nivcsw,
497 ki.p_exitsig, /* 38 exit_signal */
498 ki.p_cpuid); /* 39 task_cpu */
499
500 mutex_exit(p->p_lock);
501 mutex_exit(proc_lock);
502
503 uvmspace_free(vm);
504
505 if (len == 0)
506 goto out;
507
508 error = uiomove_frombuf(bf, len, uio);
509 out:
510 free(bf, M_TEMP);
511 return error;
512 }
513
514 int
515 procfs_docpuinfo(struct lwp *curl, struct proc *p,
516 struct pfsnode *pfs, struct uio *uio)
517 {
518 size_t len = LBFSZ;
519 char *bf = NULL;
520 int error;
521
522 do {
523 if (bf)
524 free(bf, M_TEMP);
525 bf = malloc(len, M_TEMP, M_WAITOK);
526 } while (procfs_getcpuinfstr(bf, &len) < 0);
527
528 if (len == 0) {
529 error = 0;
530 goto done;
531 }
532
533 error = uiomove_frombuf(bf, len, uio);
534 done:
535 free(bf, M_TEMP);
536 return error;
537 }
538
539 int
540 procfs_douptime(struct lwp *curl, struct proc *p,
541 struct pfsnode *pfs, struct uio *uio)
542 {
543 char *bf;
544 int len;
545 struct timeval runtime;
546 u_int64_t idle;
547 int error = 0;
548
549 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
550
551 microuptime(&runtime);
552 idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
553 len = snprintf(bf, LBFSZ,
554 "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n",
555 (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000,
556 idle / hz, (((idle % hz) * 100) / hz) % 100);
557
558 if (len == 0)
559 goto out;
560
561 error = uiomove_frombuf(bf, len, uio);
562 out:
563 free(bf, M_TEMP);
564 return error;
565 }
566
567 static int
568 procfs_format_sfs(char **mtab, size_t *mlen, char *buf, size_t blen,
569 const struct statvfs *sfs, struct lwp *curl, int suser)
570 {
571 const char *fsname;
572
573 /* Linux uses different names for some filesystems */
574 fsname = sfs->f_fstypename;
575 if (strcmp(fsname, "procfs") == 0)
576 fsname = "proc";
577 else if (strcmp(fsname, "ext2fs") == 0)
578 fsname = "ext2";
579
580 blen = snprintf(buf, blen, "%s %s %s %s%s%s%s%s%s 0 0\n",
581 sfs->f_mntfromname, sfs->f_mntonname, fsname,
582 (sfs->f_flag & ST_RDONLY) ? "ro" : "rw",
583 (sfs->f_flag & ST_NOSUID) ? ",nosuid" : "",
584 (sfs->f_flag & ST_NOEXEC) ? ",noexec" : "",
585 (sfs->f_flag & ST_NODEV) ? ",nodev" : "",
586 (sfs->f_flag & ST_SYNCHRONOUS) ? ",sync" : "",
587 (sfs->f_flag & ST_NOATIME) ? ",noatime" : "");
588
589 *mtab = realloc(*mtab, *mlen + blen, M_TEMP, M_WAITOK);
590 memcpy(*mtab + *mlen, buf, blen);
591 *mlen += blen;
592 return sfs->f_mntonname[0] == '/' && sfs->f_mntonname[1] == '\0';
593 }
594
595 int
596 procfs_domounts(struct lwp *curl, struct proc *p,
597 struct pfsnode *pfs, struct uio *uio)
598 {
599 char *bf, *mtab = NULL;
600 size_t mtabsz = 0;
601 mount_iterator_t *iter;
602 struct mount *mp;
603 int error = 0, root = 0;
604 struct cwdinfo *cwdi = curl->l_proc->p_cwdi;
605
606 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
607
608 mountlist_iterator_init(&iter);
609 while ((mp = mountlist_iterator_next(iter)) != NULL) {
610 struct statvfs sfs;
611
612 if ((error = dostatvfs(mp, &sfs, curl, MNT_WAIT, 0)) == 0)
613 root |= procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
614 &sfs, curl, 0);
615 }
616 mountlist_iterator_destroy(iter);
617
618 /*
619 * If we are inside a chroot that is not itself a mount point,
620 * fake a root entry.
621 */
622 if (!root && cwdi->cwdi_rdir)
623 (void)procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
624 &cwdi->cwdi_rdir->v_mount->mnt_stat, curl, 1);
625
626 free(bf, M_TEMP);
627
628 if (mtabsz > 0) {
629 error = uiomove_frombuf(mtab, mtabsz, uio);
630 free(mtab, M_TEMP);
631 }
632
633 return error;
634 }
635
636 /*
637 * Linux compatible /proc/version. Only active when the -o linux
638 * mountflag is used.
639 */
640 int
641 procfs_doversion(struct lwp *curl, struct proc *p,
642 struct pfsnode *pfs, struct uio *uio)
643 {
644 char *bf;
645 char lostype[20], losrelease[20], lversion[80];
646 const char *postype, *posrelease, *pversion;
647 const char *emulname = curlwp->l_proc->p_emul->e_name;
648 int len;
649 int error = 0;
650 int nm[4];
651 size_t buflen;
652
653 CTASSERT(EMUL_LINUX_KERN_OSTYPE == EMUL_LINUX32_KERN_OSTYPE);
654 CTASSERT(EMUL_LINUX_KERN_OSRELEASE == EMUL_LINUX32_KERN_OSRELEASE);
655 CTASSERT(EMUL_LINUX_KERN_VERSION == EMUL_LINUX32_KERN_VERSION);
656
657 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
658
659 sysctl_lock(false);
660
661 if (strncmp(emulname, "linux", 5) == 0) {
662 /*
663 * Lookup the emulation ostype, osrelease, and version.
664 * Since compat_linux and compat_linux32 can be built as
665 * modules, we use sysctl to obtain the values instead of
666 * using the symbols directly.
667 */
668
669 if (strcmp(emulname, "linux32") == 0) {
670 nm[0] = CTL_EMUL;
671 nm[1] = EMUL_LINUX32;
672 nm[2] = EMUL_LINUX32_KERN;
673 } else {
674 nm[0] = CTL_EMUL;
675 nm[1] = EMUL_LINUX;
676 nm[2] = EMUL_LINUX_KERN;
677 }
678
679 nm[3] = EMUL_LINUX_KERN_OSTYPE;
680 buflen = sizeof(lostype);
681 error = sysctl_dispatch(nm, __arraycount(nm),
682 lostype, &buflen,
683 NULL, 0, NULL, NULL, NULL);
684 if (error)
685 goto out;
686
687 nm[3] = EMUL_LINUX_KERN_OSRELEASE;
688 buflen = sizeof(losrelease);
689 error = sysctl_dispatch(nm, __arraycount(nm),
690 losrelease, &buflen,
691 NULL, 0, NULL, NULL, NULL);
692 if (error)
693 goto out;
694
695 nm[3] = EMUL_LINUX_KERN_VERSION;
696 buflen = sizeof(lversion);
697 error = sysctl_dispatch(nm, __arraycount(nm),
698 lversion, &buflen,
699 NULL, 0, NULL, NULL, NULL);
700 if (error)
701 goto out;
702
703 postype = lostype;
704 posrelease = losrelease;
705 pversion = lversion;
706 } else {
707 postype = ostype;
708 posrelease = osrelease;
709 strlcpy(lversion, version, sizeof(lversion));
710 if (strchr(lversion, '\n'))
711 *strchr(lversion, '\n') = '\0';
712 pversion = lversion;
713 }
714
715 len = snprintf(bf, LBFSZ,
716 "%s version %s (%s@localhost) (gcc version %s) %s\n",
717 postype, posrelease, emulname,
718 #ifdef __VERSION__
719 __VERSION__,
720 #else
721 "unknown",
722 #endif
723 pversion);
724
725 if (len == 0)
726 goto out;
727
728 error = uiomove_frombuf(bf, len, uio);
729 out:
730 free(bf, M_TEMP);
731 sysctl_unlock();
732 return error;
733 }
734