procfs_linux.c revision 1.54.8.1 1 /* $NetBSD: procfs_linux.c,v 1.54.8.1 2009/10/27 21:42:33 bouyer 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.54.8.1 2009/10/27 21:42:33 bouyer Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/exec.h>
48 #include <sys/resource.h>
49 #include <sys/resourcevar.h>
50 #include <sys/signal.h>
51 #include <sys/signalvar.h>
52 #include <sys/tty.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/conf.h>
56
57 #include <miscfs/procfs/procfs.h>
58 #include <miscfs/specfs/specdev.h>
59
60 #include <compat/linux/common/linux_exec.h>
61
62 #include <uvm/uvm_extern.h>
63 #include <uvm/uvm.h>
64
65 extern struct devsw_conv *devsw_conv;
66 extern int max_devsw_convs;
67
68 #define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT)
69 #define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10))
70
71 #define LBFSZ (8 * 1024)
72
73 static void
74 get_proc_size_info(struct lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack)
75 {
76 struct proc *p = l->l_proc;
77 struct vmspace *vm;
78 struct vm_map *map;
79 struct vm_map_entry *entry;
80
81 *stext = 0;
82 *etext = 0;
83 *sstack = 0;
84
85 proc_vmspace_getref(p, &vm);
86 map = &vm->vm_map;
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 #ifdef LINUX_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 uvmspace_free(vm);
128 }
129
130 /*
131 * Linux compatible /proc/meminfo. Only active when the -o linux
132 * mountflag is used.
133 */
134 int
135 procfs_domeminfo(struct lwp *curl, struct proc *p,
136 struct pfsnode *pfs, struct uio *uio)
137 {
138 char *bf;
139 int len;
140 int error = 0;
141
142 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
143
144 len = snprintf(bf, LBFSZ,
145 " total: used: free: shared: buffers: cached:\n"
146 "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n"
147 "Swap: %8lu %8lu %8lu\n"
148 "MemTotal: %8lu kB\n"
149 "MemFree: %8lu kB\n"
150 "MemShared: %8lu kB\n"
151 "Buffers: %8lu kB\n"
152 "Cached: %8lu kB\n"
153 "SwapTotal: %8lu kB\n"
154 "SwapFree: %8lu kB\n",
155 PGTOB(uvmexp.npages),
156 PGTOB(uvmexp.npages - uvmexp.free),
157 PGTOB(uvmexp.free),
158 0L,
159 PGTOB(uvmexp.filepages),
160 PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
161 PGTOB(uvmexp.swpages),
162 PGTOB(uvmexp.swpginuse),
163 PGTOB(uvmexp.swpages - uvmexp.swpginuse),
164 PGTOKB(uvmexp.npages),
165 PGTOKB(uvmexp.free),
166 0L,
167 PGTOKB(uvmexp.filepages),
168 PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
169 PGTOKB(uvmexp.swpages),
170 PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
171
172 if (len == 0)
173 goto out;
174
175 error = uiomove_frombuf(bf, len, uio);
176 out:
177 free(bf, M_TEMP);
178 return error;
179 }
180
181 /*
182 * Linux compatible /proc/devices. Only active when the -o linux
183 * mountflag is used.
184 */
185 int
186 procfs_dodevices(struct lwp *curl, struct proc *p,
187 struct pfsnode *pfs, struct uio *uio)
188 {
189 char *bf;
190 int offset = 0;
191 int i, error = ENAMETOOLONG;
192
193 /* XXX elad - may need filtering. */
194
195 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
196
197 offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
198 if (offset >= LBFSZ)
199 goto out;
200
201 mutex_enter(&specfs_lock);
202 for (i = 0; i < max_devsw_convs; i++) {
203 if ((devsw_conv[i].d_name == NULL) ||
204 (devsw_conv[i].d_cmajor == -1))
205 continue;
206
207 offset += snprintf(&bf[offset], LBFSZ - offset,
208 "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
209 if (offset >= LBFSZ) {
210 mutex_exit(&specfs_lock);
211 goto out;
212 }
213 }
214
215 offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
216 if (offset >= LBFSZ) {
217 mutex_exit(&specfs_lock);
218 goto out;
219 }
220
221 for (i = 0; i < max_devsw_convs; i++) {
222 if ((devsw_conv[i].d_name == NULL) ||
223 (devsw_conv[i].d_bmajor == -1))
224 continue;
225
226 offset += snprintf(&bf[offset], LBFSZ - offset,
227 "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
228 if (offset >= LBFSZ) {
229 mutex_exit(&specfs_lock);
230 goto out;
231 }
232 }
233 mutex_exit(&specfs_lock);
234
235 error = uiomove_frombuf(bf, offset, uio);
236 out:
237 free(bf, M_TEMP);
238 return error;
239 }
240
241 /*
242 * Linux compatible /proc/stat. Only active when the -o linux
243 * mountflag is used.
244 */
245 int
246 procfs_docpustat(struct lwp *curl, struct proc *p,
247 struct pfsnode *pfs, struct uio *uio)
248 {
249 char *bf;
250 int error;
251 int len;
252 #if defined(MULTIPROCESSOR)
253 struct cpu_info *ci;
254 CPU_INFO_ITERATOR cii;
255 #endif
256 int i;
257
258 error = ENAMETOOLONG;
259 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
260
261 len = snprintf(bf, LBFSZ,
262 "cpu %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
263 curcpu()->ci_schedstate.spc_cp_time[CP_USER],
264 curcpu()->ci_schedstate.spc_cp_time[CP_NICE],
265 curcpu()->ci_schedstate.spc_cp_time[CP_SYS] /*+ [CP_INTR]*/,
266 curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]);
267 if (len == 0)
268 goto out;
269
270 #if defined(MULTIPROCESSOR)
271 #define ALLCPUS CPU_INFO_FOREACH(cii, ci)
272 #define CPUNAME ci
273 #else
274 #define ALLCPUS ; i < 1 ;
275 #define CPUNAME curcpu()
276 #endif
277
278 i = 0;
279 for (ALLCPUS) {
280 len += snprintf(&bf[len], LBFSZ - len,
281 "cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
282 "\n", i,
283 CPUNAME->ci_schedstate.spc_cp_time[CP_USER],
284 CPUNAME->ci_schedstate.spc_cp_time[CP_NICE],
285 CPUNAME->ci_schedstate.spc_cp_time[CP_SYS],
286 CPUNAME->ci_schedstate.spc_cp_time[CP_IDLE]);
287 if (len >= LBFSZ)
288 goto out;
289 i += 1;
290 }
291
292 len += snprintf(&bf[len], LBFSZ - len,
293 "disk 0 0 0 0\n"
294 "page %u %u\n"
295 "swap %u %u\n"
296 "intr %u\n"
297 "ctxt %u\n"
298 "btime %lld\n",
299 uvmexp.pageins, uvmexp.pdpageouts,
300 uvmexp.pgswapin, uvmexp.pgswapout,
301 uvmexp.intrs,
302 uvmexp.swtch,
303 (long long)boottime.tv_sec);
304 if (len >= LBFSZ)
305 goto out;
306
307 error = uiomove_frombuf(bf, len, uio);
308 out:
309 free(bf, M_TEMP);
310 return error;
311 }
312
313 /*
314 * Linux compatible /proc/loadavg. Only active when the -o linux
315 * mountflag is used.
316 */
317 int
318 procfs_doloadavg(struct lwp *curl, struct proc *p,
319 struct pfsnode *pfs, struct uio *uio)
320 {
321 char *bf;
322 int error;
323 int len;
324
325 error = ENAMETOOLONG;
326 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
327
328 averunnable.fscale = FSCALE;
329 len = snprintf(bf, LBFSZ,
330 "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
331 (int)(averunnable.ldavg[0] / averunnable.fscale),
332 (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
333 (int)(averunnable.ldavg[1] / averunnable.fscale),
334 (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
335 (int)(averunnable.ldavg[2] / averunnable.fscale),
336 (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
337 1, /* number of ONPROC processes */
338 nprocs,
339 30000); /* last pid */
340 if (len == 0)
341 goto out;
342
343 error = uiomove_frombuf(bf, len, uio);
344 out:
345 free(bf, M_TEMP);
346 return error;
347 }
348
349 /*
350 * Linux compatible /proc/<pid>/statm. Only active when the -o linux
351 * mountflag is used.
352 */
353 int
354 procfs_do_pid_statm(struct lwp *curl, struct lwp *l,
355 struct pfsnode *pfs, struct uio *uio)
356 {
357 struct vmspace *vm;
358 struct proc *p = l->l_proc;
359 struct rusage *ru = &p->p_stats->p_ru;
360 char *bf;
361 int error;
362 int len;
363
364 error = ENAMETOOLONG;
365 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
366
367 /* XXX - we use values from vmspace, since dsl says that ru figures
368 are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */
369 if ((error = proc_vmspace_getref(p, &vm)) != 0) {
370 goto out;
371 }
372
373 len = snprintf(bf, LBFSZ,
374 "%lu %lu %lu %lu %lu %lu %lu\n",
375 (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */
376 (unsigned long)(vm->vm_rssize), /* resident */
377 (unsigned long)(ru->ru_ixrss), /* shared */
378 (unsigned long)(vm->vm_tsize), /* text size in pages */
379 (unsigned long)(vm->vm_dsize), /* data size in pages */
380 (unsigned long)(vm->vm_ssize), /* stack size in pages */
381 (unsigned long) 0);
382
383 uvmspace_free(vm);
384
385 if (len == 0)
386 goto out;
387
388 error = uiomove_frombuf(bf, len, uio);
389 out:
390 free(bf, M_TEMP);
391 return error;
392 }
393
394 #define USEC_2_TICKS(x) ((x) / 10000)
395
396 /*
397 * Linux compatible /proc/<pid>/stat. Only active when the -o linux
398 * mountflag is used.
399 */
400 int
401 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
402 struct pfsnode *pfs, struct uio *uio)
403 {
404 char *bf;
405 struct proc *p = l->l_proc;
406 int len;
407 struct tty *tty = p->p_session->s_ttyp;
408 struct rusage *ru = &p->p_stats->p_ru;
409 struct rusage *cru = &p->p_stats->p_cru;
410 unsigned long stext = 0, etext = 0, sstack = 0;
411 struct timeval rt;
412 struct vmspace *vm;
413 int error = 0;
414
415 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
416
417 if ((error = proc_vmspace_getref(p, &vm)) != 0) {
418 goto out;
419 }
420
421 get_proc_size_info(l, &stext, &etext, &sstack);
422
423 mutex_enter(proc_lock);
424 mutex_enter(p->p_lock);
425
426 calcru(p, NULL, NULL, NULL, &rt);
427
428 len = snprintf(bf, LBFSZ,
429 "%d (%s) %c %d %d %d %d %d "
430 "%u "
431 "%lu %lu %lu %lu %lu %lu %lu %lu "
432 "%d %d %d "
433 "%lu %lu %lu %lu %" PRIu64 " "
434 "%lu %lu %lu "
435 "%u %u "
436 "%u %u %u %u "
437 "%lu %lu %lu %d %d\n",
438
439 p->p_pid,
440 p->p_comm,
441 "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
442 (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
443
444 p->p_pgid,
445 p->p_session->s_sid,
446 tty ? tty->t_dev : 0,
447 (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
448
449 p->p_flag,
450
451 ru->ru_minflt,
452 cru->ru_minflt,
453 ru->ru_majflt,
454 cru->ru_majflt,
455 USEC_2_TICKS(ru->ru_utime.tv_usec),
456 USEC_2_TICKS(ru->ru_stime.tv_usec),
457 USEC_2_TICKS(cru->ru_utime.tv_usec),
458 USEC_2_TICKS(cru->ru_stime.tv_usec),
459
460 l->l_priority, /* XXX: priority */
461 p->p_nice - 20,
462 0,
463
464 rt.tv_sec,
465 p->p_stats->p_start.tv_sec,
466 (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */
467 (unsigned long)(vm->vm_rssize), /* resident */
468 p->p_rlimit[RLIMIT_RSS].rlim_cur,
469
470 stext, /* start code */
471 etext, /* end code */
472 sstack, /* mm start stack */
473 0, /* XXX: pc */
474 0, /* XXX: sp */
475 p->p_sigpend.sp_set.__bits[0], /* XXX: pending */
476 0, /* XXX: held */
477 p->p_sigctx.ps_sigignore.__bits[0], /* ignored */
478 p->p_sigctx.ps_sigcatch.__bits[0], /* caught */
479
480 (unsigned long)(intptr_t)l->l_wchan,
481 ru->ru_nvcsw,
482 ru->ru_nivcsw,
483 p->p_exitsig,
484 0); /* XXX: processor */
485
486 mutex_exit(p->p_lock);
487 mutex_exit(proc_lock);
488
489 uvmspace_free(vm);
490
491 if (len == 0)
492 goto out;
493
494 error = uiomove_frombuf(bf, len, uio);
495 out:
496 free(bf, M_TEMP);
497 return error;
498 }
499
500 int
501 procfs_docpuinfo(struct lwp *curl, struct proc *p,
502 struct pfsnode *pfs, struct uio *uio)
503 {
504 int len = LBFSZ;
505 char *bf = malloc(len, M_TEMP, M_WAITOK);
506 int error;
507
508 if (procfs_getcpuinfstr(bf, &len) < 0) {
509 error = ENOSPC;
510 goto done;
511 }
512
513 if (len == 0) {
514 error = 0;
515 goto done;
516 }
517
518 error = uiomove_frombuf(bf, len, uio);
519 done:
520 free(bf, M_TEMP);
521 return error;
522 }
523
524 int
525 procfs_douptime(struct lwp *curl, struct proc *p,
526 struct pfsnode *pfs, struct uio *uio)
527 {
528 char *bf;
529 int len;
530 struct timeval runtime;
531 u_int64_t idle;
532 int error = 0;
533
534 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
535
536 microuptime(&runtime);
537 idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
538 len = snprintf(bf, LBFSZ,
539 "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
540 runtime.tv_sec, runtime.tv_usec / 10000,
541 idle / hz, (((idle % hz) * 100) / hz) % 100);
542
543 if (len == 0)
544 goto out;
545
546 error = uiomove_frombuf(bf, len, uio);
547 out:
548 free(bf, M_TEMP);
549 return error;
550 }
551
552 int
553 procfs_domounts(struct lwp *curl, struct proc *p,
554 struct pfsnode *pfs, struct uio *uio)
555 {
556 char *bf, *mtab = NULL;
557 const char *fsname;
558 size_t len, mtabsz = 0;
559 struct mount *mp, *nmp;
560 struct statvfs *sfs;
561 int error = 0;
562
563 bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
564 mutex_enter(&mountlist_lock);
565 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
566 mp = nmp) {
567 if (vfs_busy(mp, &nmp)) {
568 continue;
569 }
570
571 sfs = &mp->mnt_stat;
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 len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n",
581 sfs->f_mntfromname,
582 sfs->f_mntonname,
583 fsname,
584 (mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
585 (mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
586 (mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
587 (mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
588 (mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
589 (mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
590 );
591
592 mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
593 memcpy(mtab + mtabsz, bf, len);
594 mtabsz += len;
595
596 vfs_unbusy(mp, false, &nmp);
597 }
598 mutex_exit(&mountlist_lock);
599 free(bf, M_TEMP);
600
601 if (mtabsz > 0) {
602 error = uiomove_frombuf(mtab, mtabsz, uio);
603 free(mtab, M_TEMP);
604 }
605
606 return error;
607 }
608