1 /* $NetBSD: procfs_linux.c,v 1.54 2008/05/31 21:34:42 ad 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 2008/05/31 21:34:42 ad 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 if (len == 0) 384 goto out; 385 386 error = uiomove_frombuf(bf, len, uio); 387 out: 388 free(bf, M_TEMP); 389 return error; 390 } 391 392 #define USEC_2_TICKS(x) ((x) / 10000) 393 394 /* 395 * Linux compatible /proc/<pid>/stat. Only active when the -o linux 396 * mountflag is used. 397 */ 398 int 399 procfs_do_pid_stat(struct lwp *curl, struct lwp *l, 400 struct pfsnode *pfs, struct uio *uio) 401 { 402 char *bf; 403 struct proc *p = l->l_proc; 404 int len; 405 struct tty *tty = p->p_session->s_ttyp; 406 struct rusage *ru = &p->p_stats->p_ru; 407 struct rusage *cru = &p->p_stats->p_cru; 408 unsigned long stext = 0, etext = 0, sstack = 0; 409 struct timeval rt; 410 struct vmspace *vm; 411 int error = 0; 412 413 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 414 415 if ((error = proc_vmspace_getref(p, &vm)) != 0) { 416 goto out; 417 } 418 419 get_proc_size_info(l, &stext, &etext, &sstack); 420 421 mutex_enter(proc_lock); 422 mutex_enter(p->p_lock); 423 424 calcru(p, NULL, NULL, NULL, &rt); 425 426 len = snprintf(bf, LBFSZ, 427 "%d (%s) %c %d %d %d %d %d " 428 "%u " 429 "%lu %lu %lu %lu %lu %lu %lu %lu " 430 "%d %d %d " 431 "%lu %lu %lu %lu %" PRIu64 " " 432 "%lu %lu %lu " 433 "%u %u " 434 "%u %u %u %u " 435 "%lu %lu %lu %d %d\n", 436 437 p->p_pid, 438 p->p_comm, 439 "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat], 440 (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0, 441 442 p->p_pgid, 443 p->p_session->s_sid, 444 tty ? tty->t_dev : 0, 445 (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0, 446 447 p->p_flag, 448 449 ru->ru_minflt, 450 cru->ru_minflt, 451 ru->ru_majflt, 452 cru->ru_majflt, 453 USEC_2_TICKS(ru->ru_utime.tv_usec), 454 USEC_2_TICKS(ru->ru_stime.tv_usec), 455 USEC_2_TICKS(cru->ru_utime.tv_usec), 456 USEC_2_TICKS(cru->ru_stime.tv_usec), 457 458 l->l_priority, /* XXX: priority */ 459 p->p_nice - 20, 460 0, 461 462 rt.tv_sec, 463 p->p_stats->p_start.tv_sec, 464 (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */ 465 (unsigned long)(vm->vm_rssize), /* resident */ 466 p->p_rlimit[RLIMIT_RSS].rlim_cur, 467 468 stext, /* start code */ 469 etext, /* end code */ 470 sstack, /* mm start stack */ 471 0, /* XXX: pc */ 472 0, /* XXX: sp */ 473 p->p_sigpend.sp_set.__bits[0], /* XXX: pending */ 474 0, /* XXX: held */ 475 p->p_sigctx.ps_sigignore.__bits[0], /* ignored */ 476 p->p_sigctx.ps_sigcatch.__bits[0], /* caught */ 477 478 (unsigned long)(intptr_t)l->l_wchan, 479 ru->ru_nvcsw, 480 ru->ru_nivcsw, 481 p->p_exitsig, 482 0); /* XXX: processor */ 483 484 mutex_exit(p->p_lock); 485 mutex_exit(proc_lock); 486 487 if (len == 0) 488 goto out; 489 490 error = uiomove_frombuf(bf, len, uio); 491 out: 492 free(bf, M_TEMP); 493 return error; 494 } 495 496 int 497 procfs_docpuinfo(struct lwp *curl, struct proc *p, 498 struct pfsnode *pfs, struct uio *uio) 499 { 500 int len = LBFSZ; 501 char *bf = malloc(len, M_TEMP, M_WAITOK); 502 int error; 503 504 if (procfs_getcpuinfstr(bf, &len) < 0) { 505 error = ENOSPC; 506 goto done; 507 } 508 509 if (len == 0) { 510 error = 0; 511 goto done; 512 } 513 514 error = uiomove_frombuf(bf, len, uio); 515 done: 516 free(bf, M_TEMP); 517 return error; 518 } 519 520 int 521 procfs_douptime(struct lwp *curl, struct proc *p, 522 struct pfsnode *pfs, struct uio *uio) 523 { 524 char *bf; 525 int len; 526 struct timeval runtime; 527 u_int64_t idle; 528 int error = 0; 529 530 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 531 532 microuptime(&runtime); 533 idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]; 534 len = snprintf(bf, LBFSZ, 535 "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n", 536 runtime.tv_sec, runtime.tv_usec / 10000, 537 idle / hz, (((idle % hz) * 100) / hz) % 100); 538 539 if (len == 0) 540 goto out; 541 542 error = uiomove_frombuf(bf, len, uio); 543 out: 544 free(bf, M_TEMP); 545 return error; 546 } 547 548 int 549 procfs_domounts(struct lwp *curl, struct proc *p, 550 struct pfsnode *pfs, struct uio *uio) 551 { 552 char *bf, *mtab = NULL; 553 const char *fsname; 554 size_t len, mtabsz = 0; 555 struct mount *mp, *nmp; 556 struct statvfs *sfs; 557 int error = 0; 558 559 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 560 mutex_enter(&mountlist_lock); 561 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 562 mp = nmp) { 563 if (vfs_busy(mp, &nmp)) { 564 continue; 565 } 566 567 sfs = &mp->mnt_stat; 568 569 /* Linux uses different names for some filesystems */ 570 fsname = sfs->f_fstypename; 571 if (strcmp(fsname, "procfs") == 0) 572 fsname = "proc"; 573 else if (strcmp(fsname, "ext2fs") == 0) 574 fsname = "ext2"; 575 576 len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n", 577 sfs->f_mntfromname, 578 sfs->f_mntonname, 579 fsname, 580 (mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw", 581 (mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "", 582 (mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "", 583 (mp->mnt_flag & MNT_NODEV) ? ",nodev" : "", 584 (mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "", 585 (mp->mnt_flag & MNT_NOATIME) ? ",noatime" : "" 586 ); 587 588 mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK); 589 memcpy(mtab + mtabsz, bf, len); 590 mtabsz += len; 591 592 vfs_unbusy(mp, false, &nmp); 593 } 594 mutex_exit(&mountlist_lock); 595 free(bf, M_TEMP); 596 597 if (mtabsz > 0) { 598 error = uiomove_frombuf(mtab, mtabsz, uio); 599 free(mtab, M_TEMP); 600 } 601 602 return error; 603 } 604