procfs_linux.c revision 1.76 1 /* $NetBSD: procfs_linux.c,v 1.76 2019/09/07 19:08:28 chs 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.76 2019/09/07 19:08:28 chs 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 memset(&ki, 0, sizeof(ki));
382 fill_kproc2(p, &ki, false, false);
383
384 mutex_exit(p->p_lock);
385 mutex_exit(proc_lock);
386
387 uvmspace_free(vm);
388
389 len = snprintf(bf, LBFSZ,
390 "%lu %lu %lu %lu %lu %lu %lu\n",
391 (unsigned long)(ki.p_vm_msize), /* size */
392 (unsigned long)(ki.p_vm_rssize),/* resident */
393 (unsigned long)(ki.p_uru_ixrss),/* shared */
394 (unsigned long)(ki.p_vm_tsize), /* text */
395 (unsigned long) 0, /* library (unused) */
396 (unsigned long)(ki.p_vm_dsize + ki.p_vm_ssize), /* data+stack */
397 (unsigned long) 0); /* dirty */
398
399 if (len == 0)
400 goto out;
401
402 error = uiomove_frombuf(bf, len, uio);
403 out:
404 free(bf, M_TEMP);
405 return error;
406 }
407
408 #define UTIME2TICKS(s,u) (((uint64_t)(s) * 1000000 + (u)) / 10000)
409
410 /*
411 * Linux compatible /proc/<pid>/stat. Only active when the -o linux
412 * mountflag is used.
413 */
414 int
415 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
416 struct pfsnode *pfs, struct uio *uio)
417 {
418 char *bf;
419 struct proc *p = l->l_proc;
420 int len;
421 struct rusage *cru = &p->p_stats->p_cru;
422 unsigned long stext = 0, etext = 0, sstack = 0;
423 struct timeval rt;
424 struct vmspace *vm;
425 struct kinfo_proc2 ki;
426 int error;
427
428 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
429
430 if ((error = proc_vmspace_getref(p, &vm)) != 0) {
431 goto out;
432 }
433
434 get_proc_size_info(p, &vm->vm_map, &stext, &etext, &sstack);
435
436 mutex_enter(proc_lock);
437 mutex_enter(p->p_lock);
438
439 memset(&ki, 0, sizeof(ki));
440 fill_kproc2(p, &ki, false, false);
441 calcru(p, NULL, NULL, NULL, &rt);
442
443 len = snprintf(bf, LBFSZ,
444 "%d (%s) %c %d %d %d %u %d "
445 "%u "
446 "%"PRIu64" %lu %"PRIu64" %lu %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" "
447 "%d %d %"PRIu64" "
448 "%lld %"PRIu64" %"PRId64" %lu %"PRIu64" "
449 "%lu %lu %lu "
450 "%u %u "
451 "%u %u %u %u "
452 "%"PRIu64" %"PRIu64" %"PRIu64" %d %"PRIu64"\n",
453
454 ki.p_pid, /* 1 pid */
455 ki.p_comm, /* 2 tcomm */
456 "0RRSTZXR8"[(ki.p_stat > 8) ? 0 : (int)ki.p_stat], /* 3 state */
457 ki.p_ppid, /* 4 ppid */
458 ki.p__pgid, /* 5 pgrp */
459 ki.p_sid, /* 6 sid */
460 (ki.p_tdev != (uint32_t)NODEV) ? ki.p_tdev : 0, /* 7 tty_nr */
461 ki.p_tpgid, /* 8 tty_pgrp */
462
463 ki.p_flag, /* 9 flags */
464
465 ki.p_uru_minflt, /* 10 min_flt */
466 cru->ru_minflt,
467 ki.p_uru_majflt, /* 12 maj_flt */
468 cru->ru_majflt,
469 UTIME2TICKS(ki.p_uutime_sec, ki.p_uutime_usec), /* 14 utime */
470 UTIME2TICKS(ki.p_ustime_sec, ki.p_ustime_usec), /* 15 stime */
471 UTIME2TICKS(cru->ru_utime.tv_sec, cru->ru_utime.tv_usec), /* 16 cutime */
472 UTIME2TICKS(cru->ru_stime.tv_sec, cru->ru_stime.tv_usec), /* 17 cstime */
473
474 ki.p_priority, /* XXX: 18 priority */
475 ki.p_nice - NZERO, /* 19 nice */
476 ki.p_nlwps, /* 20 num_threads */
477
478 (long long)rt.tv_sec,
479 UTIME2TICKS(ki.p_ustart_sec, ki.p_ustart_usec), /* 22 start_time */
480 ki.p_vm_msize, /* 23 vsize */
481 PGTOKB(ki.p_vm_rssize), /* 24 rss */
482 p->p_rlimit[RLIMIT_RSS].rlim_cur, /* 25 rsslim */
483
484 stext, /* 26 start_code */
485 etext, /* 27 end_code */
486 sstack, /* 28 start_stack */
487
488 0, /* XXX: 29 esp */
489 0, /* XXX: 30 eip */
490
491 ki.p_siglist.__bits[0], /* XXX: 31 pending */
492 0, /* XXX: 32 blocked */
493 ki.p_sigignore.__bits[0], /* 33 sigign */
494 ki.p_sigcatch.__bits[0], /* 34 sigcatch */
495
496 ki.p_wchan, /* 35 wchan */
497 ki.p_uru_nvcsw,
498 ki.p_uru_nivcsw,
499 ki.p_exitsig, /* 38 exit_signal */
500 ki.p_cpuid); /* 39 task_cpu */
501
502 mutex_exit(p->p_lock);
503 mutex_exit(proc_lock);
504
505 uvmspace_free(vm);
506
507 if (len == 0)
508 goto out;
509
510 error = uiomove_frombuf(bf, len, uio);
511 out:
512 free(bf, M_TEMP);
513 return error;
514 }
515
516 int
517 procfs_docpuinfo(struct lwp *curl, struct proc *p,
518 struct pfsnode *pfs, struct uio *uio)
519 {
520 size_t len = LBFSZ;
521 char *bf = NULL;
522 int error;
523
524 do {
525 if (bf)
526 free(bf, M_TEMP);
527 bf = malloc(len, M_TEMP, M_WAITOK);
528 } while (procfs_getcpuinfstr(bf, &len) < 0);
529
530 if (len == 0) {
531 error = 0;
532 goto done;
533 }
534
535 error = uiomove_frombuf(bf, len, uio);
536 done:
537 free(bf, M_TEMP);
538 return error;
539 }
540
541 int
542 procfs_douptime(struct lwp *curl, struct proc *p,
543 struct pfsnode *pfs, struct uio *uio)
544 {
545 char *bf;
546 int len;
547 struct timeval runtime;
548 u_int64_t idle;
549 int error = 0;
550
551 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
552
553 microuptime(&runtime);
554 idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
555 len = snprintf(bf, LBFSZ,
556 "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n",
557 (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000,
558 idle / hz, (((idle % hz) * 100) / hz) % 100);
559
560 if (len == 0)
561 goto out;
562
563 error = uiomove_frombuf(bf, len, uio);
564 out:
565 free(bf, M_TEMP);
566 return error;
567 }
568
569 static int
570 procfs_format_sfs(char **mtab, size_t *mlen, char *buf, size_t blen,
571 const struct statvfs *sfs, struct lwp *curl, int suser)
572 {
573 const char *fsname;
574
575 /* Linux uses different names for some filesystems */
576 fsname = sfs->f_fstypename;
577 if (strcmp(fsname, "procfs") == 0)
578 fsname = "proc";
579 else if (strcmp(fsname, "ext2fs") == 0)
580 fsname = "ext2";
581
582 blen = snprintf(buf, blen, "%s %s %s %s%s%s%s%s%s 0 0\n",
583 sfs->f_mntfromname, sfs->f_mntonname, fsname,
584 (sfs->f_flag & ST_RDONLY) ? "ro" : "rw",
585 (sfs->f_flag & ST_NOSUID) ? ",nosuid" : "",
586 (sfs->f_flag & ST_NOEXEC) ? ",noexec" : "",
587 (sfs->f_flag & ST_NODEV) ? ",nodev" : "",
588 (sfs->f_flag & ST_SYNCHRONOUS) ? ",sync" : "",
589 (sfs->f_flag & ST_NOATIME) ? ",noatime" : "");
590
591 *mtab = realloc(*mtab, *mlen + blen, M_TEMP, M_WAITOK);
592 memcpy(*mtab + *mlen, buf, blen);
593 *mlen += blen;
594 return sfs->f_mntonname[0] == '/' && sfs->f_mntonname[1] == '\0';
595 }
596
597 int
598 procfs_domounts(struct lwp *curl, struct proc *p,
599 struct pfsnode *pfs, struct uio *uio)
600 {
601 char *bf, *mtab = NULL;
602 size_t mtabsz = 0;
603 mount_iterator_t *iter;
604 struct mount *mp;
605 int error = 0, root = 0;
606 struct cwdinfo *cwdi = curl->l_proc->p_cwdi;
607
608 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
609
610 mountlist_iterator_init(&iter);
611 while ((mp = mountlist_iterator_next(iter)) != NULL) {
612 struct statvfs sfs;
613
614 if ((error = dostatvfs(mp, &sfs, curl, MNT_WAIT, 0)) == 0)
615 root |= procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
616 &sfs, curl, 0);
617 }
618 mountlist_iterator_destroy(iter);
619
620 /*
621 * If we are inside a chroot that is not itself a mount point,
622 * fake a root entry.
623 */
624 if (!root && cwdi->cwdi_rdir)
625 (void)procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
626 &cwdi->cwdi_rdir->v_mount->mnt_stat, curl, 1);
627
628 free(bf, M_TEMP);
629
630 if (mtabsz > 0) {
631 error = uiomove_frombuf(mtab, mtabsz, uio);
632 free(mtab, M_TEMP);
633 }
634
635 return error;
636 }
637
638 /*
639 * Linux compatible /proc/version. Only active when the -o linux
640 * mountflag is used.
641 */
642 int
643 procfs_doversion(struct lwp *curl, struct proc *p,
644 struct pfsnode *pfs, struct uio *uio)
645 {
646 char *bf;
647 char lostype[20], losrelease[20], lversion[80];
648 const char *postype, *posrelease, *pversion;
649 const char *emulname = curlwp->l_proc->p_emul->e_name;
650 int len;
651 int error = 0;
652 int nm[4];
653 size_t buflen;
654
655 CTASSERT(EMUL_LINUX_KERN_OSTYPE == EMUL_LINUX32_KERN_OSTYPE);
656 CTASSERT(EMUL_LINUX_KERN_OSRELEASE == EMUL_LINUX32_KERN_OSRELEASE);
657 CTASSERT(EMUL_LINUX_KERN_VERSION == EMUL_LINUX32_KERN_VERSION);
658
659 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
660
661 sysctl_lock(false);
662
663 if (strncmp(emulname, "linux", 5) == 0) {
664 /*
665 * Lookup the emulation ostype, osrelease, and version.
666 * Since compat_linux and compat_linux32 can be built as
667 * modules, we use sysctl to obtain the values instead of
668 * using the symbols directly.
669 */
670
671 if (strcmp(emulname, "linux32") == 0) {
672 nm[0] = CTL_EMUL;
673 nm[1] = EMUL_LINUX32;
674 nm[2] = EMUL_LINUX32_KERN;
675 } else {
676 nm[0] = CTL_EMUL;
677 nm[1] = EMUL_LINUX;
678 nm[2] = EMUL_LINUX_KERN;
679 }
680
681 nm[3] = EMUL_LINUX_KERN_OSTYPE;
682 buflen = sizeof(lostype);
683 error = sysctl_dispatch(nm, __arraycount(nm),
684 lostype, &buflen,
685 NULL, 0, NULL, NULL, NULL);
686 if (error)
687 goto out;
688
689 nm[3] = EMUL_LINUX_KERN_OSRELEASE;
690 buflen = sizeof(losrelease);
691 error = sysctl_dispatch(nm, __arraycount(nm),
692 losrelease, &buflen,
693 NULL, 0, NULL, NULL, NULL);
694 if (error)
695 goto out;
696
697 nm[3] = EMUL_LINUX_KERN_VERSION;
698 buflen = sizeof(lversion);
699 error = sysctl_dispatch(nm, __arraycount(nm),
700 lversion, &buflen,
701 NULL, 0, NULL, NULL, NULL);
702 if (error)
703 goto out;
704
705 postype = lostype;
706 posrelease = losrelease;
707 pversion = lversion;
708 } else {
709 postype = ostype;
710 posrelease = osrelease;
711 strlcpy(lversion, version, sizeof(lversion));
712 if (strchr(lversion, '\n'))
713 *strchr(lversion, '\n') = '\0';
714 pversion = lversion;
715 }
716
717 len = snprintf(bf, LBFSZ,
718 "%s version %s (%s@localhost) (gcc version %s) %s\n",
719 postype, posrelease, emulname,
720 #ifdef __VERSION__
721 __VERSION__,
722 #else
723 "unknown",
724 #endif
725 pversion);
726
727 if (len == 0)
728 goto out;
729
730 error = uiomove_frombuf(bf, len, uio);
731 out:
732 free(bf, M_TEMP);
733 sysctl_unlock();
734 return error;
735 }
736