1 /* $NetBSD: procfs_linux.c,v 1.60 2011/08/28 18:48:14 jmcneill 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.60 2011/08/28 18:48:14 jmcneill 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 59 #include <compat/linux/common/linux_exec.h> 60 61 #include <uvm/uvm_extern.h> 62 #include <uvm/uvm.h> 63 64 extern struct devsw_conv *devsw_conv; 65 extern int max_devsw_convs; 66 67 #define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT) 68 #define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10)) 69 70 #define LBFSZ (8 * 1024) 71 72 static void 73 get_proc_size_info(struct lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack) 74 { 75 struct proc *p = l->l_proc; 76 struct vmspace *vm; 77 struct vm_map *map; 78 struct vm_map_entry *entry; 79 80 *stext = 0; 81 *etext = 0; 82 *sstack = 0; 83 84 proc_vmspace_getref(p, &vm); 85 map = &vm->vm_map; 86 vm_map_lock_read(map); 87 88 for (entry = map->header.next; entry != &map->header; 89 entry = entry->next) { 90 if (UVM_ET_ISSUBMAP(entry)) 91 continue; 92 /* assume text is the first entry */ 93 if (*stext == *etext) { 94 *stext = entry->start; 95 *etext = entry->end; 96 break; 97 } 98 } 99 #if defined(LINUX_USRSTACK32) && defined(USRSTACK32) 100 if (strcmp(p->p_emul->e_name, "linux32") == 0 && 101 LINUX_USRSTACK32 < USRSTACK32) 102 *sstack = (unsigned long)LINUX_USRSTACK32; 103 else 104 #endif 105 #ifdef LINUX_USRSTACK 106 if (strcmp(p->p_emul->e_name, "linux") == 0 && 107 LINUX_USRSTACK < USRSTACK) 108 *sstack = (unsigned long)LINUX_USRSTACK; 109 else 110 #endif 111 #ifdef USRSTACK32 112 if (strstr(p->p_emul->e_name, "32") != NULL) 113 *sstack = (unsigned long)USRSTACK32; 114 else 115 #endif 116 *sstack = (unsigned long)USRSTACK; 117 118 /* 119 * jdk 1.6 compares low <= addr && addr < high 120 * if we put addr == high, then the test fails 121 * so eat one page. 122 */ 123 *sstack -= PAGE_SIZE; 124 125 vm_map_unlock_read(map); 126 uvmspace_free(vm); 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 struct rusage *ru = &p->p_stats->p_ru; 365 char *bf; 366 int error; 367 int len; 368 369 error = ENAMETOOLONG; 370 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 371 372 /* XXX - we use values from vmspace, since dsl says that ru figures 373 are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */ 374 if ((error = proc_vmspace_getref(p, &vm)) != 0) { 375 goto out; 376 } 377 378 len = snprintf(bf, LBFSZ, 379 "%lu %lu %lu %lu %lu %lu %lu\n", 380 (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */ 381 (unsigned long)(vm->vm_rssize), /* resident */ 382 (unsigned long)(ru->ru_ixrss), /* shared */ 383 (unsigned long)(vm->vm_tsize), /* text size in pages */ 384 (unsigned long)(vm->vm_dsize), /* data size in pages */ 385 (unsigned long)(vm->vm_ssize), /* stack size in pages */ 386 (unsigned long) 0); 387 388 uvmspace_free(vm); 389 390 if (len == 0) 391 goto out; 392 393 error = uiomove_frombuf(bf, len, uio); 394 out: 395 free(bf, M_TEMP); 396 return error; 397 } 398 399 #define USEC_2_TICKS(x) ((x) / 10000) 400 401 /* 402 * Linux compatible /proc/<pid>/stat. Only active when the -o linux 403 * mountflag is used. 404 */ 405 int 406 procfs_do_pid_stat(struct lwp *curl, struct lwp *l, 407 struct pfsnode *pfs, struct uio *uio) 408 { 409 char *bf; 410 struct proc *p = l->l_proc; 411 int len; 412 struct tty *tty = p->p_session->s_ttyp; 413 struct rusage *ru = &p->p_stats->p_ru; 414 struct rusage *cru = &p->p_stats->p_cru; 415 unsigned long stext = 0, etext = 0, sstack = 0; 416 struct timeval rt; 417 struct vmspace *vm; 418 int error = 0; 419 420 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 421 422 if ((error = proc_vmspace_getref(p, &vm)) != 0) { 423 goto out; 424 } 425 426 get_proc_size_info(l, &stext, &etext, &sstack); 427 428 mutex_enter(proc_lock); 429 mutex_enter(p->p_lock); 430 431 calcru(p, NULL, NULL, NULL, &rt); 432 433 len = snprintf(bf, LBFSZ, 434 "%d (%s) %c %d %d %d %lld %d " 435 "%u " 436 "%lu %lu %lu %lu %lu %lu %lu %lu " 437 "%d %d %d " 438 "%lld %lld %lu %lu %" PRIu64 " " 439 "%lu %lu %lu " 440 "%u %u " 441 "%u %u %u %u " 442 "%lu %lu %lu %d %d\n", 443 444 p->p_pid, 445 p->p_comm, 446 "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat], 447 (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0, 448 449 p->p_pgid, 450 p->p_session->s_sid, 451 (unsigned long long)(tty ? tty->t_dev : 0), 452 (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0, 453 454 p->p_flag, 455 456 ru->ru_minflt, 457 cru->ru_minflt, 458 ru->ru_majflt, 459 cru->ru_majflt, 460 (long)USEC_2_TICKS(ru->ru_utime.tv_usec), 461 (long)USEC_2_TICKS(ru->ru_stime.tv_usec), 462 (long)USEC_2_TICKS(cru->ru_utime.tv_usec), 463 (long)USEC_2_TICKS(cru->ru_stime.tv_usec), 464 465 l->l_priority, /* XXX: priority */ 466 p->p_nice - 20, 467 0, 468 469 (long long)rt.tv_sec, 470 (long long)p->p_stats->p_start.tv_sec, 471 (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */ 472 (unsigned long)(vm->vm_rssize), /* resident */ 473 p->p_rlimit[RLIMIT_RSS].rlim_cur, 474 475 stext, /* start code */ 476 etext, /* end code */ 477 sstack, /* mm start stack */ 478 0, /* XXX: pc */ 479 0, /* XXX: sp */ 480 p->p_sigpend.sp_set.__bits[0], /* XXX: pending */ 481 0, /* XXX: held */ 482 p->p_sigctx.ps_sigignore.__bits[0], /* ignored */ 483 p->p_sigctx.ps_sigcatch.__bits[0], /* caught */ 484 485 (unsigned long)(intptr_t)l->l_wchan, 486 ru->ru_nvcsw, 487 ru->ru_nivcsw, 488 p->p_exitsig, 489 0); /* XXX: processor */ 490 491 mutex_exit(p->p_lock); 492 mutex_exit(proc_lock); 493 494 uvmspace_free(vm); 495 496 if (len == 0) 497 goto out; 498 499 error = uiomove_frombuf(bf, len, uio); 500 out: 501 free(bf, M_TEMP); 502 return error; 503 } 504 505 int 506 procfs_docpuinfo(struct lwp *curl, struct proc *p, 507 struct pfsnode *pfs, struct uio *uio) 508 { 509 int len = LBFSZ; 510 char *bf = malloc(len, M_TEMP, M_WAITOK); 511 int error; 512 513 if (procfs_getcpuinfstr(bf, &len) < 0) { 514 error = ENOSPC; 515 goto done; 516 } 517 518 if (len == 0) { 519 error = 0; 520 goto done; 521 } 522 523 error = uiomove_frombuf(bf, len, uio); 524 done: 525 free(bf, M_TEMP); 526 return error; 527 } 528 529 int 530 procfs_douptime(struct lwp *curl, struct proc *p, 531 struct pfsnode *pfs, struct uio *uio) 532 { 533 char *bf; 534 int len; 535 struct timeval runtime; 536 u_int64_t idle; 537 int error = 0; 538 539 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 540 541 microuptime(&runtime); 542 idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]; 543 len = snprintf(bf, LBFSZ, 544 "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n", 545 (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000, 546 idle / hz, (((idle % hz) * 100) / hz) % 100); 547 548 if (len == 0) 549 goto out; 550 551 error = uiomove_frombuf(bf, len, uio); 552 out: 553 free(bf, M_TEMP); 554 return error; 555 } 556 557 int 558 procfs_domounts(struct lwp *curl, struct proc *p, 559 struct pfsnode *pfs, struct uio *uio) 560 { 561 char *bf, *mtab = NULL; 562 const char *fsname; 563 size_t len, mtabsz = 0; 564 struct mount *mp, *nmp; 565 struct statvfs *sfs; 566 int error = 0; 567 568 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 569 mutex_enter(&mountlist_lock); 570 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 571 mp = nmp) { 572 if (vfs_busy(mp, &nmp)) { 573 continue; 574 } 575 576 sfs = &mp->mnt_stat; 577 578 /* Linux uses different names for some filesystems */ 579 fsname = sfs->f_fstypename; 580 if (strcmp(fsname, "procfs") == 0) 581 fsname = "proc"; 582 else if (strcmp(fsname, "ext2fs") == 0) 583 fsname = "ext2"; 584 585 len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n", 586 sfs->f_mntfromname, 587 sfs->f_mntonname, 588 fsname, 589 (mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw", 590 (mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "", 591 (mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "", 592 (mp->mnt_flag & MNT_NODEV) ? ",nodev" : "", 593 (mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "", 594 (mp->mnt_flag & MNT_NOATIME) ? ",noatime" : "" 595 ); 596 597 mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK); 598 memcpy(mtab + mtabsz, bf, len); 599 mtabsz += len; 600 601 vfs_unbusy(mp, false, &nmp); 602 } 603 mutex_exit(&mountlist_lock); 604 free(bf, M_TEMP); 605 606 if (mtabsz > 0) { 607 error = uiomove_frombuf(mtab, mtabsz, uio); 608 free(mtab, M_TEMP); 609 } 610 611 return error; 612 } 613