1 /* $NetBSD: procfs_linux.c,v 1.45 2007/11/12 14:11:47 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.45 2007/11/12 14:11:47 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 <compat/linux/common/linux_exec.h> 59 60 #include <uvm/uvm_extern.h> 61 #include <uvm/uvm.h> 62 63 extern struct devsw_conv *devsw_conv; 64 extern int max_devsw_convs; 65 66 #define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT) 67 #define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10)) 68 69 #define LBFSZ (8 * 1024) 70 71 static void 72 get_proc_size_info(struct lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack) 73 { 74 struct proc *p = l->l_proc; 75 struct vmspace *vm; 76 struct vm_map *map; 77 struct vm_map_entry *entry; 78 79 *stext = 0; 80 *etext = 0; 81 *sstack = 0; 82 83 proc_vmspace_getref(p, &vm); 84 map = &vm->vm_map; 85 vm_map_lock_read(map); 86 87 for (entry = map->header.next; entry != &map->header; 88 entry = entry->next) { 89 if (UVM_ET_ISSUBMAP(entry)) 90 continue; 91 /* assume text is the first entry */ 92 if (*stext == *etext) { 93 *stext = entry->start; 94 *etext = entry->end; 95 break; 96 } 97 } 98 #ifdef LINUX_USRSTACK32 99 if (strcmp(p->p_emul->e_name, "linux32") == 0 && 100 LINUX_USRSTACK32 < USRSTACK32) 101 *sstack = (unsigned long)LINUX_USRSTACK32; 102 else 103 #endif 104 #ifdef LINUX_USRSTACK 105 if (strcmp(p->p_emul->e_name, "linux") == 0 && 106 LINUX_USRSTACK < USRSTACK) 107 *sstack = (unsigned long)LINUX_USRSTACK; 108 else 109 #endif 110 #ifdef USRSTACK32 111 if (strstr(p->p_emul->e_name, "32") != NULL) 112 *sstack = (unsigned long)USRSTACK32; 113 else 114 #endif 115 *sstack = (unsigned long)USRSTACK; 116 117 /* 118 * jdk 1.6 compares low <= addr && addr < high 119 * if we put addr == high, then the test fails 120 * so eat one page. 121 */ 122 *sstack -= PAGE_SIZE; 123 124 vm_map_unlock_read(map); 125 uvmspace_free(vm); 126 } 127 128 /* 129 * Linux compatible /proc/meminfo. Only active when the -o linux 130 * mountflag is used. 131 */ 132 int 133 procfs_domeminfo(struct lwp *curl, struct proc *p, 134 struct pfsnode *pfs, struct uio *uio) 135 { 136 char *bf; 137 int len; 138 int error = 0; 139 140 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 141 142 len = snprintf(bf, LBFSZ, 143 " total: used: free: shared: buffers: cached:\n" 144 "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n" 145 "Swap: %8lu %8lu %8lu\n" 146 "MemTotal: %8lu kB\n" 147 "MemFree: %8lu kB\n" 148 "MemShared: %8lu kB\n" 149 "Buffers: %8lu kB\n" 150 "Cached: %8lu kB\n" 151 "SwapTotal: %8lu kB\n" 152 "SwapFree: %8lu kB\n", 153 PGTOB(uvmexp.npages), 154 PGTOB(uvmexp.npages - uvmexp.free), 155 PGTOB(uvmexp.free), 156 0L, 157 PGTOB(uvmexp.filepages), 158 PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages), 159 PGTOB(uvmexp.swpages), 160 PGTOB(uvmexp.swpginuse), 161 PGTOB(uvmexp.swpages - uvmexp.swpginuse), 162 PGTOKB(uvmexp.npages), 163 PGTOKB(uvmexp.free), 164 0L, 165 PGTOKB(uvmexp.filepages), 166 PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages), 167 PGTOKB(uvmexp.swpages), 168 PGTOKB(uvmexp.swpages - uvmexp.swpginuse)); 169 170 if (len == 0) 171 goto out; 172 173 error = uiomove_frombuf(bf, len, uio); 174 out: 175 free(bf, M_TEMP); 176 return error; 177 } 178 179 /* 180 * Linux compatible /proc/devices. Only active when the -o linux 181 * mountflag is used. 182 */ 183 int 184 procfs_dodevices(struct lwp *curl, struct proc *p, 185 struct pfsnode *pfs, struct uio *uio) 186 { 187 char *bf; 188 int offset = 0; 189 int i, error = ENAMETOOLONG; 190 extern kmutex_t devsw_lock; 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(&devsw_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(&devsw_lock); 210 goto out; 211 } 212 } 213 214 offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n"); 215 if (offset >= LBFSZ) { 216 mutex_exit(&devsw_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(&devsw_lock); 229 goto out; 230 } 231 } 232 mutex_exit(&devsw_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 struct timeval runtime; 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 timersub(&curlwp->l_stime, &boottime, &runtime); 293 len += snprintf(&bf[len], LBFSZ - len, 294 "disk 0 0 0 0\n" 295 "page %u %u\n" 296 "swap %u %u\n" 297 "intr %u\n" 298 "ctxt %u\n" 299 "btime %lld\n", 300 uvmexp.pageins, uvmexp.pdpageouts, 301 uvmexp.pgswapin, uvmexp.pgswapout, 302 uvmexp.intrs, 303 uvmexp.swtch, 304 (long long)boottime.tv_sec); 305 if (len >= LBFSZ) 306 goto out; 307 308 error = uiomove_frombuf(bf, len, uio); 309 out: 310 free(bf, M_TEMP); 311 return error; 312 } 313 314 /* 315 * Linux compatible /proc/loadavg. Only active when the -o linux 316 * mountflag is used. 317 */ 318 int 319 procfs_doloadavg(struct lwp *curl, struct proc *p, 320 struct pfsnode *pfs, struct uio *uio) 321 { 322 char *bf; 323 int error; 324 int len; 325 326 error = ENAMETOOLONG; 327 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 328 329 averunnable.fscale = FSCALE; 330 len = snprintf(bf, LBFSZ, 331 "%d.%02d %d.%02d %d.%02d %d/%d %d\n", 332 (int)(averunnable.ldavg[0] / averunnable.fscale), 333 (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100), 334 (int)(averunnable.ldavg[1] / averunnable.fscale), 335 (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100), 336 (int)(averunnable.ldavg[2] / averunnable.fscale), 337 (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100), 338 1, /* number of ONPROC processes */ 339 nprocs, 340 30000); /* last pid */ 341 if (len == 0) 342 goto out; 343 344 error = uiomove_frombuf(bf, len, uio); 345 out: 346 free(bf, M_TEMP); 347 return error; 348 } 349 350 /* 351 * Linux compatible /proc/<pid>/statm. Only active when the -o linux 352 * mountflag is used. 353 */ 354 int 355 procfs_do_pid_statm(struct lwp *curl, struct lwp *l, 356 struct pfsnode *pfs, struct uio *uio) 357 { 358 struct vmspace *vm; 359 struct proc *p = l->l_proc; 360 struct rusage *ru = &p->p_stats->p_ru; 361 char *bf; 362 int error; 363 int len; 364 365 error = ENAMETOOLONG; 366 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 367 368 /* XXX - we use values from vmspace, since dsl says that ru figures 369 are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */ 370 if ((error = proc_vmspace_getref(p, &vm)) != 0) { 371 goto out; 372 } 373 374 len = snprintf(bf, LBFSZ, 375 "%lu %lu %lu %lu %lu %lu %lu\n", 376 (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */ 377 (unsigned long)(vm->vm_rssize), /* resident */ 378 (unsigned long)(ru->ru_ixrss), /* shared */ 379 (unsigned long)(vm->vm_tsize), /* text size in pages */ 380 (unsigned long)(vm->vm_dsize), /* data size in pages */ 381 (unsigned long)(vm->vm_ssize), /* stack size in pages */ 382 (unsigned long) 0); 383 384 if (len == 0) 385 goto out; 386 387 error = uiomove_frombuf(bf, len, uio); 388 out: 389 free(bf, M_TEMP); 390 return error; 391 } 392 393 #define USEC_2_TICKS(x) ((x) / 10000) 394 395 /* 396 * Linux compatible /proc/<pid>/stat. Only active when the -o linux 397 * mountflag is used. 398 */ 399 int 400 procfs_do_pid_stat(struct lwp *curl, struct lwp *l, 401 struct pfsnode *pfs, struct uio *uio) 402 { 403 char *bf; 404 struct proc *p = l->l_proc; 405 int len; 406 struct tty *tty = p->p_session->s_ttyp; 407 struct rusage *ru = &p->p_stats->p_ru; 408 struct rusage *cru = &p->p_stats->p_cru; 409 unsigned long stext = 0, etext = 0, sstack = 0; 410 struct timeval rt; 411 struct vmspace *vm; 412 int error = 0; 413 414 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 415 416 if ((error = proc_vmspace_getref(p, &vm)) != 0) { 417 goto out; 418 } 419 420 get_proc_size_info(l, &stext, &etext, &sstack); 421 422 mutex_enter(&proclist_lock); 423 mutex_enter(&p->p_mutex); 424 mutex_enter(&p->p_smutex); 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_smutex); 487 mutex_exit(&p->p_mutex); 488 mutex_exit(&proclist_lock); 489 490 if (len == 0) 491 goto out; 492 493 error = uiomove_frombuf(bf, len, uio); 494 out: 495 free(bf, M_TEMP); 496 return error; 497 } 498 499 int 500 procfs_docpuinfo(struct lwp *curl, struct proc *p, 501 struct pfsnode *pfs, struct uio *uio) 502 { 503 int len = LBFSZ; 504 char *bf = malloc(len, M_TEMP, M_WAITOK); 505 int error; 506 507 if (procfs_getcpuinfstr(bf, &len) < 0) { 508 error = ENOSPC; 509 goto done; 510 } 511 512 if (len == 0) { 513 error = 0; 514 goto done; 515 } 516 517 error = uiomove_frombuf(bf, len, uio); 518 done: 519 free(bf, M_TEMP); 520 return error; 521 } 522 523 int 524 procfs_douptime(struct lwp *curl, struct proc *p, 525 struct pfsnode *pfs, struct uio *uio) 526 { 527 char *bf; 528 int len; 529 struct timeval runtime; 530 u_int64_t idle; 531 int error = 0; 532 533 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 534 535 timersub(&curlwp->l_stime, &boottime, &runtime); 536 idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]; 537 len = snprintf(bf, LBFSZ, 538 "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n", 539 runtime.tv_sec, runtime.tv_usec / 10000, 540 idle / hz, (((idle % hz) * 100) / hz) % 100); 541 542 if (len == 0) 543 goto out; 544 545 error = uiomove_frombuf(bf, len, uio); 546 out: 547 free(bf, M_TEMP); 548 return error; 549 } 550 551 int 552 procfs_domounts(struct lwp *curl, struct proc *p, 553 struct pfsnode *pfs, struct uio *uio) 554 { 555 char *bf, *mtab = NULL; 556 const char *fsname; 557 size_t len, mtabsz = 0; 558 struct mount *mp, *nmp; 559 struct statvfs *sfs; 560 int error = 0; 561 562 bf = malloc(LBFSZ, M_TEMP, M_WAITOK); 563 mutex_enter(&mountlist_lock); 564 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 565 mp = nmp) { 566 if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) { 567 nmp = CIRCLEQ_NEXT(mp, mnt_list); 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 mutex_enter(&mountlist_lock); 597 nmp = CIRCLEQ_NEXT(mp, mnt_list); 598 vfs_unbusy(mp); 599 } 600 mutex_exit(&mountlist_lock); 601 free(bf, M_TEMP); 602 603 if (mtabsz > 0) { 604 error = uiomove_frombuf(mtab, mtabsz, uio); 605 free(mtab, M_TEMP); 606 } 607 608 return error; 609 } 610