xref: /netbsd-src/sys/miscfs/procfs/procfs_linux.c (revision 15a3a67edebf5ebfdabfd7e6e24506df64c36bba)
1 /*      $NetBSD: procfs_linux.c,v 1.37 2007/05/25 19:20:06 agc 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.37 2007/05/25 19:20:06 agc 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_USRSTACK
99 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
100 	    LINUX_USRSTACK < USRSTACK)
101 		*sstack = (unsigned long) LINUX_USRSTACK;
102 	else
103 #endif
104 		*sstack = (unsigned long) USRSTACK;
105 
106 	/*
107 	 * jdk 1.6 compares low <= addr && addr < high
108 	 * if we put addr == high, then the test fails
109 	 * so eat one page.
110 	 */
111 	*sstack -= PAGE_SIZE;
112 
113 	vm_map_unlock_read(map);
114 	uvmspace_free(vm);
115 }
116 
117 /*
118  * Linux compatible /proc/meminfo. Only active when the -o linux
119  * mountflag is used.
120  */
121 int
122 procfs_domeminfo(struct lwp *curl, struct proc *p,
123     struct pfsnode *pfs, struct uio *uio)
124 {
125 	char *bf;
126 	int len;
127 	int error = 0;
128 
129 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
130 
131 	len = snprintf(bf, LBFSZ,
132 		"        total:    used:    free:  shared: buffers: cached:\n"
133 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
134 		"Swap: %8lu %8lu %8lu\n"
135 		"MemTotal:  %8lu kB\n"
136 		"MemFree:   %8lu kB\n"
137 		"MemShared: %8lu kB\n"
138 		"Buffers:   %8lu kB\n"
139 		"Cached:    %8lu kB\n"
140 		"SwapTotal: %8lu kB\n"
141 		"SwapFree:  %8lu kB\n",
142 		PGTOB(uvmexp.npages),
143 		PGTOB(uvmexp.npages - uvmexp.free),
144 		PGTOB(uvmexp.free),
145 		0L,
146 		PGTOB(uvmexp.filepages),
147 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
148 		PGTOB(uvmexp.swpages),
149 		PGTOB(uvmexp.swpginuse),
150 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
151 		PGTOKB(uvmexp.npages),
152 		PGTOKB(uvmexp.free),
153 		0L,
154 		PGTOKB(uvmexp.filepages),
155 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
156 		PGTOKB(uvmexp.swpages),
157 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
158 
159 	if (len == 0)
160 		goto out;
161 
162 	error = uiomove_frombuf(bf, len, uio);
163 out:
164 	free(bf, M_TEMP);
165 	return error;
166 }
167 
168 /*
169  * Linux compatible /proc/devices. Only active when the -o linux
170  * mountflag is used.
171  */
172 int
173 procfs_dodevices(struct lwp *curl, struct proc *p,
174     struct pfsnode *pfs, struct uio *uio)
175 {
176 	char *bf;
177 	int offset = 0;
178 	int i, error = ENAMETOOLONG;
179 
180 	/* XXX elad - may need filtering. */
181 
182 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
183 
184 	offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
185 	if (offset >= LBFSZ)
186 		goto out;
187 
188 	for (i = 0; i < max_devsw_convs; i++) {
189 		if ((devsw_conv[i].d_name == NULL) ||
190 		    (devsw_conv[i].d_cmajor == -1))
191 			continue;
192 
193 		offset += snprintf(&bf[offset], LBFSZ - offset,
194 		    "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
195 		if (offset >= LBFSZ)
196 			goto out;
197 	}
198 
199 	offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
200 	if (offset >= LBFSZ)
201 		goto out;
202 
203 	for (i = 0; i < max_devsw_convs; i++) {
204 		if ((devsw_conv[i].d_name == NULL) ||
205 		    (devsw_conv[i].d_bmajor == -1))
206 			continue;
207 
208 		offset += snprintf(&bf[offset], LBFSZ - offset,
209 		    "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
210 		if (offset >= LBFSZ)
211 			goto out;
212 	}
213 
214 	error = uiomove_frombuf(bf, offset, uio);
215 out:
216 	free(bf, M_TEMP);
217 	return error;
218 }
219 
220 /*
221  * Linux compatible /proc/stat. Only active when the -o linux
222  * mountflag is used.
223  */
224 int
225 procfs_docpustat(struct lwp *curl, struct proc *p,
226     struct pfsnode *pfs, struct uio *uio)
227 {
228 	struct timeval	 runtime;
229 	char		*bf;
230 	int	 	 error;
231 	int	 	 len;
232 #if defined(MULTIPROCESSOR)
233         struct cpu_info *ci;
234         CPU_INFO_ITERATOR cii;
235 	int	 	 i;
236 #endif
237 
238 	error = ENAMETOOLONG;
239 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
240 
241 	len = snprintf(bf, LBFSZ,
242 		"cpu %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
243 		curcpu()->ci_schedstate.spc_cp_time[CP_USER],
244 		curcpu()->ci_schedstate.spc_cp_time[CP_NICE],
245 		curcpu()->ci_schedstate.spc_cp_time[CP_SYS] /*+ [CP_INTR]*/,
246 		curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]);
247 	if (len == 0)
248 		goto out;
249 
250 #if defined(MULTIPROCESSOR)
251 	i = 0;
252 	for (CPU_INFO_FOREACH(cii, ci)) {
253 		len += snprintf(&bf[len], LBFSZ - len,
254 			"cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
255 			"\n", i,
256 			ci->ci_schedstate.spc_cp_time[CP_USER],
257 			ci->ci_schedstate.spc_cp_time[CP_NICE],
258 			ci->ci_schedstate.spc_cp_time[CP_SYS],
259 			ci->ci_schedstate.spc_cp_time[CP_IDLE]);
260 		if (len >= LBFSZ)
261 			goto out;
262 		i += 1;
263 	}
264 #else
265 	len += snprintf(&bf[len], LBFSZ - len,
266 		"cpu0 %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
267 		curcpu()->ci_schedstate.spc_cp_time[CP_USER],
268 		curcpu()->ci_schedstate.spc_cp_time[CP_NICE],
269 		curcpu()->ci_schedstate.spc_cp_time[CP_SYS],
270 		curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]);
271 #endif
272 
273 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
274 	len += snprintf(&bf[len], LBFSZ - len,
275 			"disk 0 0 0 0\n"
276 			"page %u %u\n"
277 			"swap %u %u\n"
278 			"intr %u\n"
279 			"ctxt %u\n"
280 			"btime %lld\n",
281 			uvmexp.pageins, uvmexp.pdpageouts,
282 			uvmexp.pgswapin, uvmexp.pgswapout,
283 			uvmexp.intrs,
284 			uvmexp.swtch,
285 			(long long)boottime.tv_sec);
286 	if (len >= LBFSZ)
287 		goto out;
288 
289 	error = uiomove_frombuf(bf, len, uio);
290 out:
291 	free(bf, M_TEMP);
292 	return error;
293 }
294 
295 /*
296  * Linux compatible /proc/loadavg. Only active when the -o linux
297  * mountflag is used.
298  */
299 int
300 procfs_doloadavg(struct lwp *curl, struct proc *p,
301     struct pfsnode *pfs, struct uio *uio)
302 {
303 	char	*bf;
304 	int 	 error;
305 	int 	 len;
306 
307 	error = ENAMETOOLONG;
308 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
309 
310 	averunnable.fscale = FSCALE;
311 	len = snprintf(bf, LBFSZ,
312 	        "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
313 		(int)(averunnable.ldavg[0] / averunnable.fscale),
314 		(int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
315 		(int)(averunnable.ldavg[1] / averunnable.fscale),
316 		(int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
317 		(int)(averunnable.ldavg[2] / averunnable.fscale),
318 		(int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
319 		1,		/* number of ONPROC processes */
320 		nprocs,
321 		30000);		/* last pid */
322 	if (len == 0)
323 		goto out;
324 
325 	error = uiomove_frombuf(bf, len, uio);
326 out:
327 	free(bf, M_TEMP);
328 	return error;
329 }
330 
331 /*
332  * Linux compatible /proc/<pid>/statm. Only active when the -o linux
333  * mountflag is used.
334  */
335 int
336 procfs_do_pid_statm(struct lwp *curl, struct lwp *l,
337     struct pfsnode *pfs, struct uio *uio)
338 {
339 	struct vmspace	*vm;
340 	struct proc	*p = l->l_proc;
341 	struct rusage	*ru = &p->p_stats->p_ru;
342 	char		*bf;
343 	int	 	 error;
344 	int	 	 len;
345 
346 	error = ENAMETOOLONG;
347 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
348 
349 	/* XXX - we use values from vmspace, since dsl says that ru figures
350 	   are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */
351 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
352 		return error;
353 	}
354 
355 	len = snprintf(bf, LBFSZ,
356 	        "%lu %lu %lu %lu %lu %lu %lu\n",
357 		(unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */
358 		(unsigned long)(vm->vm_rssize),	/* resident */
359 		(unsigned long)(ru->ru_ixrss),	/* shared */
360 		(unsigned long)(vm->vm_tsize),	/* text size in pages */
361 		(unsigned long)(vm->vm_dsize),	/* data size in pages */
362 		(unsigned long)(vm->vm_ssize),	/* stack size in pages */
363 		(unsigned long) 0);
364 
365 	if (len == 0)
366 		goto out;
367 
368 	error = uiomove_frombuf(bf, len, uio);
369 out:
370 	free(bf, M_TEMP);
371 	return error;
372 }
373 
374 #define USEC_2_TICKS(x)		((x) / 10000)
375 
376 /*
377  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
378  * mountflag is used.
379  */
380 int
381 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
382     struct pfsnode *pfs, struct uio *uio)
383 {
384 	char *bf;
385 	struct proc *p = l->l_proc;
386 	int len;
387 	struct tty *tty = p->p_session->s_ttyp;
388 	struct rusage *ru = &p->p_stats->p_ru;
389 	struct rusage *cru = &p->p_stats->p_cru;
390 	unsigned long stext = 0, etext = 0, sstack = 0;
391 	struct timeval rt;
392 	int error = 0;
393 
394 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
395 
396 	get_proc_size_info(l, &stext, &etext, &sstack);
397 
398 	mutex_enter(&proclist_lock);
399 	mutex_enter(&p->p_mutex);
400 	mutex_enter(&p->p_smutex);
401 
402 	calcru(p, NULL, NULL, NULL, &rt);
403 
404 	len = snprintf(bf, LBFSZ,
405 	    "%d (%s) %c %d %d %d %d %d "
406 	    "%u "
407 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
408 	    "%d %d %d "
409 	    "%lu %lu %lu %lu %" PRIu64 " "
410 	    "%lu %lu %lu "
411 	    "%u %u "
412 	    "%u %u %u %u "
413 	    "%lu %lu %lu %d %d\n",
414 
415 	    p->p_pid,
416 	    p->p_comm,
417 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
418 	    (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
419 
420 	    p->p_pgid,
421 	    p->p_session->s_sid,
422 	    tty ? tty->t_dev : 0,
423 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
424 
425 	    p->p_flag,
426 
427 	    ru->ru_minflt,
428 	    cru->ru_minflt,
429 	    ru->ru_majflt,
430 	    cru->ru_majflt,
431 	    USEC_2_TICKS(ru->ru_utime.tv_usec),
432 	    USEC_2_TICKS(ru->ru_stime.tv_usec),
433 	    USEC_2_TICKS(cru->ru_utime.tv_usec),
434 	    USEC_2_TICKS(cru->ru_stime.tv_usec),
435 
436 	    p->p_nice,					/* XXX: priority */
437 	    p->p_nice,
438 	    0,
439 
440 	    rt.tv_sec,
441 	    p->p_stats->p_start.tv_sec,
442 	    ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
443 	    ru->ru_maxrss,
444 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
445 
446 	    stext,					/* start code */
447 	    etext,					/* end code */
448 	    sstack,					/* mm start stack */
449 	    0,						/* XXX: pc */
450 	    0,						/* XXX: sp */
451 	    p->p_sigpend.sp_set.__bits[0],		/* XXX: pending */
452 	    0,						/* XXX: held */
453 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
454 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
455 
456 	    (unsigned long)(intptr_t)l->l_wchan,
457 	    ru->ru_nvcsw,
458 	    ru->ru_nivcsw,
459 	    p->p_exitsig,
460 	    0);						/* XXX: processor */
461 
462 	mutex_exit(&p->p_smutex);
463 	mutex_exit(&p->p_mutex);
464 	mutex_exit(&proclist_lock);
465 
466 	if (len == 0)
467 		goto out;
468 
469 	error = uiomove_frombuf(bf, len, uio);
470 out:
471 	free(bf, M_TEMP);
472 	return error;
473 }
474 
475 int
476 procfs_docpuinfo(struct lwp *curl, struct proc *p,
477     struct pfsnode *pfs, struct uio *uio)
478 {
479 	int len = LBFSZ;
480 	char *bf = malloc(len, M_TEMP, M_WAITOK);
481 	int error;
482 
483 	if (procfs_getcpuinfstr(bf, &len) < 0) {
484 		error = ENOSPC;
485 		goto done;
486 	}
487 
488 	if (len == 0) {
489 		error = 0;
490 		goto done;
491 	}
492 
493 	error = uiomove_frombuf(bf, len, uio);
494 done:
495 	free(bf, M_TEMP);
496 	return error;
497 }
498 
499 int
500 procfs_douptime(struct lwp *curl, struct proc *p,
501     struct pfsnode *pfs, struct uio *uio)
502 {
503 	char *bf;
504 	int len;
505 	struct timeval runtime;
506 	u_int64_t idle;
507 	int error = 0;
508 
509 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
510 
511 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
512 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
513 	len = snprintf(bf, LBFSZ,
514 	    "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
515 	    runtime.tv_sec, runtime.tv_usec / 10000,
516 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
517 
518 	if (len == 0)
519 		goto out;
520 
521 	error = uiomove_frombuf(bf, len, uio);
522 out:
523 	free(bf, M_TEMP);
524 	return error;
525 }
526 
527 int
528 procfs_domounts(struct lwp *curl, struct proc *p,
529     struct pfsnode *pfs, struct uio *uio)
530 {
531 	char *bf, *mtab = NULL;
532 	const char *fsname;
533 	size_t len, mtabsz = 0;
534 	struct mount *mp, *nmp;
535 	struct statvfs *sfs;
536 	int error = 0;
537 
538 	/* XXX elad - may need filtering. */
539 
540 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
541 	simple_lock(&mountlist_slock);
542 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
543 	     mp = nmp) {
544 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
545 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
546 			continue;
547 		}
548 
549 		sfs = &mp->mnt_stat;
550 
551 		/* Linux uses different names for some filesystems */
552 		fsname = sfs->f_fstypename;
553 		if (strcmp(fsname, "procfs") == 0)
554 			fsname = "proc";
555 		else if (strcmp(fsname, "ext2fs") == 0)
556 			fsname = "ext2";
557 
558 		len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n",
559 			sfs->f_mntfromname,
560 			sfs->f_mntonname,
561 			fsname,
562 			(mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
563 			(mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
564 			(mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
565 			(mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
566 			(mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
567 			(mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
568 			);
569 
570 		mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
571 		memcpy(mtab + mtabsz, bf, len);
572 		mtabsz += len;
573 
574 		simple_lock(&mountlist_slock);
575 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
576 		vfs_unbusy(mp);
577 	}
578 	simple_unlock(&mountlist_slock);
579 	free(bf, M_TEMP);
580 
581 	if (mtabsz > 0) {
582 		error = uiomove_frombuf(mtab, mtabsz, uio);
583 		free(mtab, M_TEMP);
584 	}
585 
586 	return error;
587 }
588