xref: /netbsd-src/sys/miscfs/procfs/procfs_linux.c (revision 6a4825167bf279b6e5e5c37ce0fc733d2f0806fa)
1 /*      $NetBSD: procfs_linux.c,v 1.34 2007/04/01 03:16:44 christos 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.34 2007/04/01 03:16:44 christos 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 /*
72  * Linux compatible /proc/meminfo. Only active when the -o linux
73  * mountflag is used.
74  */
75 int
76 procfs_domeminfo(struct lwp *curl, struct proc *p,
77     struct pfsnode *pfs, struct uio *uio)
78 {
79 	char *bf;
80 	int len;
81 	int error = 0;
82 
83 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
84 
85 	len = snprintf(bf, LBFSZ,
86 		"        total:    used:    free:  shared: buffers: cached:\n"
87 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
88 		"Swap: %8lu %8lu %8lu\n"
89 		"MemTotal:  %8lu kB\n"
90 		"MemFree:   %8lu kB\n"
91 		"MemShared: %8lu kB\n"
92 		"Buffers:   %8lu kB\n"
93 		"Cached:    %8lu kB\n"
94 		"SwapTotal: %8lu kB\n"
95 		"SwapFree:  %8lu kB\n",
96 		PGTOB(uvmexp.npages),
97 		PGTOB(uvmexp.npages - uvmexp.free),
98 		PGTOB(uvmexp.free),
99 		0L,
100 		PGTOB(uvmexp.filepages),
101 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
102 		PGTOB(uvmexp.swpages),
103 		PGTOB(uvmexp.swpginuse),
104 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
105 		PGTOKB(uvmexp.npages),
106 		PGTOKB(uvmexp.free),
107 		0L,
108 		PGTOKB(uvmexp.filepages),
109 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
110 		PGTOKB(uvmexp.swpages),
111 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
112 
113 	if (len == 0)
114 		goto out;
115 
116 	error = uiomove_frombuf(bf, len, uio);
117 out:
118 	free(bf, M_TEMP);
119 	return error;
120 }
121 
122 /*
123  * Linux compatible /proc/devices. Only active when the -o linux
124  * mountflag is used.
125  */
126 int
127 procfs_dodevices(struct lwp *curl, struct proc *p,
128     struct pfsnode *pfs, struct uio *uio)
129 {
130 	char *bf;
131 	int offset = 0;
132 	int i, error = ENAMETOOLONG;
133 
134 	/* XXX elad - may need filtering. */
135 
136 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
137 
138 	offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
139 	if (offset >= LBFSZ)
140 		goto out;
141 
142 	for (i = 0; i < max_devsw_convs; i++) {
143 		if ((devsw_conv[i].d_name == NULL) ||
144 		    (devsw_conv[i].d_cmajor == -1))
145 			continue;
146 
147 		offset += snprintf(&bf[offset], LBFSZ - offset,
148 		    "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
149 		if (offset >= LBFSZ)
150 			goto out;
151 	}
152 
153 	offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
154 	if (offset >= LBFSZ)
155 		goto out;
156 
157 	for (i = 0; i < max_devsw_convs; i++) {
158 		if ((devsw_conv[i].d_name == NULL) ||
159 		    (devsw_conv[i].d_bmajor == -1))
160 			continue;
161 
162 		offset += snprintf(&bf[offset], LBFSZ - offset,
163 		    "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
164 		if (offset >= LBFSZ)
165 			goto out;
166 	}
167 
168 	error = uiomove_frombuf(bf, offset, uio);
169 out:
170 	free(bf, M_TEMP);
171 	return error;
172 }
173 
174 /*
175  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
176  * mountflag is used.
177  */
178 int
179 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
180     struct pfsnode *pfs, struct uio *uio)
181 {
182 	char *bf;
183 	struct proc *p = l->l_proc;
184 	int len;
185 	struct tty *tty = p->p_session->s_ttyp;
186 	struct rusage *ru = &p->p_stats->p_ru;
187 	struct rusage *cru = &p->p_stats->p_cru;
188 	struct vmspace *vm;
189 	struct vm_map *map;
190 	struct vm_map_entry *entry;
191 	unsigned long stext = 0, etext = 0, sstack = 0;
192 	struct timeval rt;
193 	int error = 0;
194 
195 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
196 
197 	proc_vmspace_getref(p, &vm);
198 	map = &vm->vm_map;
199 	vm_map_lock_read(map);
200 
201 	for (entry = map->header.next; entry != &map->header;
202 	    entry = entry->next) {
203 		if (UVM_ET_ISSUBMAP(entry))
204 			continue;
205 		/* assume text is the first entry */
206 		if (stext == etext) {
207 			stext = entry->start;
208 			etext = entry->end;
209 			break;
210 		}
211 	}
212 #ifdef LINUX_USRSTACK
213 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
214 	    LINUX_USRSTACK < USRSTACK)
215 		sstack = (unsigned long) LINUX_USRSTACK;
216 	else
217 #endif
218 		sstack = (unsigned long) USRSTACK;
219 
220 	/*
221 	 * jdk 1.6 compares low <= addr && addr < high
222 	 * if we put addr == high, then the test fails
223 	 * so eat one page.
224 	 */
225 	sstack -= PAGE_SIZE;
226 
227 	vm_map_unlock_read(map);
228 	uvmspace_free(vm);
229 
230 	mutex_enter(&proclist_lock);
231 	mutex_enter(&p->p_mutex);
232 	mutex_enter(&p->p_smutex);
233 
234 	calcru(p, NULL, NULL, NULL, &rt);
235 
236 	len = snprintf(bf, LBFSZ,
237 	    "%d (%s) %c %d %d %d %d %d "
238 	    "%u "
239 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
240 	    "%d %d %d "
241 	    "%lu %lu %lu %lu %" PRIu64 " "
242 	    "%lu %lu %lu "
243 	    "%u %u "
244 	    "%u %u %u %u "
245 	    "%lu %lu %lu %d %d\n",
246 
247 	    p->p_pid,
248 	    p->p_comm,
249 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
250 	    (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
251 
252 	    p->p_pgid,
253 	    p->p_session->s_sid,
254 	    tty ? tty->t_dev : 0,
255 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
256 
257 	    p->p_flag,
258 
259 	    ru->ru_minflt,
260 	    cru->ru_minflt,
261 	    ru->ru_majflt,
262 	    cru->ru_majflt,
263 	    ru->ru_utime.tv_sec,
264 	    ru->ru_stime.tv_sec,
265 	    cru->ru_utime.tv_sec,
266 	    cru->ru_stime.tv_sec,
267 
268 	    p->p_nice,					/* XXX: priority */
269 	    p->p_nice,
270 	    0,
271 
272 	    rt.tv_sec,
273 	    p->p_stats->p_start.tv_sec,
274 	    ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
275 	    ru->ru_maxrss,
276 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
277 
278 	    stext,					/* start code */
279 	    etext,					/* end code */
280 	    sstack,					/* mm start stack */
281 	    0,						/* XXX: pc */
282 	    0,						/* XXX: sp */
283 	    p->p_sigpend.sp_set.__bits[0],		/* XXX: pending */
284 	    0,						/* XXX: held */
285 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
286 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
287 
288 	    (unsigned long)(intptr_t)l->l_wchan,
289 	    ru->ru_nvcsw,
290 	    ru->ru_nivcsw,
291 	    p->p_exitsig,
292 	    0);						/* XXX: processor */
293 
294 	mutex_exit(&p->p_smutex);
295 	mutex_exit(&p->p_mutex);
296 	mutex_exit(&proclist_lock);
297 
298 	if (len == 0)
299 		goto out;
300 
301 	error = uiomove_frombuf(bf, len, uio);
302 out:
303 	free(bf, M_TEMP);
304 	return error;
305 }
306 
307 int
308 procfs_docpuinfo(struct lwp *curl, struct proc *p,
309     struct pfsnode *pfs, struct uio *uio)
310 {
311 	int len = LBFSZ;
312 	char *bf = malloc(len, M_TEMP, M_WAITOK);
313 	int error;
314 
315 	if (procfs_getcpuinfstr(bf, &len) < 0) {
316 		error = ENOSPC;
317 		goto done;
318 	}
319 
320 	if (len == 0) {
321 		error = 0;
322 		goto done;
323 	}
324 
325 	error = uiomove_frombuf(bf, len, uio);
326 done:
327 	free(bf, M_TEMP);
328 	return error;
329 }
330 
331 int
332 procfs_douptime(struct lwp *curl, struct proc *p,
333     struct pfsnode *pfs, struct uio *uio)
334 {
335 	char *bf;
336 	int len;
337 	struct timeval runtime;
338 	u_int64_t idle;
339 	int error = 0;
340 
341 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
342 
343 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
344 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
345 	len = snprintf(bf, LBFSZ,
346 	    "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
347 	    runtime.tv_sec, runtime.tv_usec / 10000,
348 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
349 
350 	if (len == 0)
351 		goto out;
352 
353 	error = uiomove_frombuf(bf, len, uio);
354 out:
355 	free(bf, M_TEMP);
356 	return error;
357 }
358 
359 int
360 procfs_domounts(struct lwp *curl, struct proc *p,
361     struct pfsnode *pfs, struct uio *uio)
362 {
363 	char *bf, *mtab = NULL;
364 	const char *fsname;
365 	size_t len, mtabsz = 0;
366 	struct mount *mp, *nmp;
367 	struct statvfs *sfs;
368 	int error = 0;
369 
370 	/* XXX elad - may need filtering. */
371 
372 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
373 	simple_lock(&mountlist_slock);
374 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
375 	     mp = nmp) {
376 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
377 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
378 			continue;
379 		}
380 
381 		sfs = &mp->mnt_stat;
382 
383 		/* Linux uses different names for some filesystems */
384 		fsname = sfs->f_fstypename;
385 		if (strcmp(fsname, "procfs") == 0)
386 			fsname = "proc";
387 		else if (strcmp(fsname, "ext2fs") == 0)
388 			fsname = "ext2";
389 
390 		len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n",
391 			sfs->f_mntfromname,
392 			sfs->f_mntonname,
393 			fsname,
394 			(mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
395 			(mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
396 			(mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
397 			(mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
398 			(mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
399 			(mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
400 			);
401 
402 		mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
403 		memcpy(mtab + mtabsz, bf, len);
404 		mtabsz += len;
405 
406 		simple_lock(&mountlist_slock);
407 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
408 		vfs_unbusy(mp);
409 	}
410 	simple_unlock(&mountlist_slock);
411 	free(bf, M_TEMP);
412 
413 	if (mtabsz > 0) {
414 		error = uiomove_frombuf(mtab, mtabsz, uio);
415 		free(mtab, M_TEMP);
416 	}
417 
418 	return error;
419 }
420