xref: /netbsd-src/sys/miscfs/procfs/procfs_linux.c (revision 26bb1685bd1425bcd855e721b426de108ec62e22)
1 /*      $NetBSD: procfs_linux.c,v 1.29 2006/10/27 16:49:01 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.29 2006/10/27 16:49:01 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 __unused, struct proc *p __unused,
77     struct pfsnode *pfs __unused, 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 __unused, struct proc *p __unused,
128     struct pfsnode *pfs __unused, struct uio *uio)
129 {
130 	char *bf;
131 	int offset = 0;
132 	int i, error = ENAMETOOLONG;
133 
134 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
135 
136 	offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
137 	if (offset >= LBFSZ)
138 		goto out;
139 
140 	for (i = 0; i < max_devsw_convs; i++) {
141 		if ((devsw_conv[i].d_name == NULL) ||
142 		    (devsw_conv[i].d_cmajor == -1))
143 			continue;
144 
145 		offset += snprintf(&bf[offset], LBFSZ - offset,
146 		    "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
147 		if (offset >= LBFSZ)
148 			goto out;
149 	}
150 
151 	offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
152 	if (offset >= LBFSZ)
153 		goto out;
154 
155 	for (i = 0; i < max_devsw_convs; i++) {
156 		if ((devsw_conv[i].d_name == NULL) ||
157 		    (devsw_conv[i].d_bmajor == -1))
158 			continue;
159 
160 		offset += snprintf(&bf[offset], LBFSZ - offset,
161 		    "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
162 		if (offset >= LBFSZ)
163 			goto out;
164 	}
165 
166 	error = uiomove_frombuf(bf, offset, uio);
167 out:
168 	free(bf, M_TEMP);
169 	return error;
170 }
171 
172 /*
173  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
174  * mountflag is used.
175  */
176 int
177 procfs_do_pid_stat(struct lwp *curl __unused, struct lwp *l,
178     struct pfsnode *pfs __unused, struct uio *uio)
179 {
180 	char *bf;
181 	struct proc *p = l->l_proc;
182 	int len;
183 	struct tty *tty = p->p_session->s_ttyp;
184 	struct rusage *ru = &p->p_stats->p_ru;
185 	struct rusage *cru = &p->p_stats->p_cru;
186 	struct vm_map *map = &p->p_vmspace->vm_map;
187 	struct vm_map_entry *entry;
188 	unsigned long stext = 0, etext = 0, sstack = 0;
189 	int error = 0;
190 
191 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
192 
193 	if (map != &curproc->p_vmspace->vm_map)
194 		vm_map_lock_read(map);
195 	for (entry = map->header.next; entry != &map->header;
196 	    entry = entry->next) {
197 		if (UVM_ET_ISSUBMAP(entry))
198 			continue;
199 		/* assume text is the first entry */
200 		if (stext == etext) {
201 			stext = entry->start;
202 			etext = entry->end;
203 			break;
204 		}
205 	}
206 #ifdef LINUX_USRSTACK
207 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
208 	    LINUX_USRSTACK < USRSTACK)
209 		sstack = (unsigned long) LINUX_USRSTACK;
210 	else
211 #endif
212 		sstack = (unsigned long) USRSTACK;
213 
214 	if (map != &curproc->p_vmspace->vm_map)
215 		vm_map_unlock_read(map);
216 
217 	len = snprintf(bf, LBFSZ,
218 	    "%d (%s) %c %d %d %d %d %d "
219 	    "%u "
220 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
221 	    "%d %d %d "
222 	    "%lu %lu %lu %lu %" PRIu64 " "
223 	    "%lu %lu %lu "
224 	    "%u %u "
225 	    "%u %u %u %u "
226 	    "%lu %lu %lu %d %d\n",
227 
228 	    p->p_pid,
229 	    p->p_comm,
230 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
231 	    (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
232 
233 	    p->p_pgid,
234 	    p->p_session->s_sid,
235 	    tty ? tty->t_dev : 0,
236 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
237 
238 	    p->p_flag,
239 
240 	    ru->ru_minflt,
241 	    cru->ru_minflt,
242 	    ru->ru_majflt,
243 	    cru->ru_majflt,
244 	    ru->ru_utime.tv_sec,
245 	    ru->ru_stime.tv_sec,
246 	    cru->ru_utime.tv_sec,
247 	    cru->ru_stime.tv_sec,
248 
249 	    p->p_nice,					/* XXX: priority */
250 	    p->p_nice,
251 	    0,
252 
253 	    p->p_rtime.tv_sec,
254 	    p->p_stats->p_start.tv_sec,
255 	    ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
256 	    ru->ru_maxrss,
257 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
258 
259 	    stext,					/* start code */
260 	    etext,					/* end code */
261 	    sstack,					/* mm start stack */
262 	    0,						/* XXX: pc */
263 	    0,						/* XXX: sp */
264 	    p->p_sigctx.ps_siglist.__bits[0],		/* pending */
265 	    p->p_sigctx.ps_sigmask.__bits[0],		/* blocked */
266 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
267 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
268 
269 	    (unsigned long)(intptr_t)l->l_wchan,
270 	    ru->ru_nvcsw,
271 	    ru->ru_nivcsw,
272 	    p->p_exitsig,
273 	    0);						/* XXX: processor */
274 
275 	if (len == 0)
276 		goto out;
277 
278 	error = uiomove_frombuf(bf, len, uio);
279 out:
280 	free(bf, M_TEMP);
281 	return error;
282 }
283 
284 int
285 procfs_docpuinfo(struct lwp *curl __unused, struct proc *p __unused,
286     struct pfsnode *pfs __unused, struct uio *uio)
287 {
288 	int len = LBFSZ;
289 	char *bf = malloc(len, M_TEMP, M_WAITOK);
290 	int error;
291 
292 	if (procfs_getcpuinfstr(bf, &len) < 0) {
293 		error = ENOSPC;
294 		goto done;
295 	}
296 
297 	if (len == 0) {
298 		error = 0;
299 		goto done;
300 	}
301 
302 	error = uiomove_frombuf(bf, len, uio);
303 done:
304 	free(bf, M_TEMP);
305 	return error;
306 }
307 
308 int
309 procfs_douptime(struct lwp *curl __unused, struct proc *p __unused,
310     struct pfsnode *pfs __unused, struct uio *uio)
311 {
312 	char *bf;
313 	int len;
314 	struct timeval runtime;
315 	u_int64_t idle;
316 	int error = 0;
317 
318 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
319 
320 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
321 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
322 	len = snprintf(bf, LBFSZ,
323 	    "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
324 	    runtime.tv_sec, runtime.tv_usec / 10000,
325 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
326 
327 	if (len == 0)
328 		goto out;
329 
330 	error = uiomove_frombuf(bf, len, uio);
331 out:
332 	free(bf, M_TEMP);
333 	return error;
334 }
335 
336 int
337 procfs_domounts(struct lwp *curl __unused, struct proc *p __unused,
338     struct pfsnode *pfs __unused, struct uio *uio)
339 {
340 	char *bf, *mtab = NULL;
341 	const char *fsname;
342 	size_t len, mtabsz = 0;
343 	struct mount *mp, *nmp;
344 	struct statvfs *sfs;
345 	int error = 0;
346 
347 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
348 	simple_lock(&mountlist_slock);
349 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
350 	     mp = nmp) {
351 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
352 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
353 			continue;
354 		}
355 
356 		sfs = &mp->mnt_stat;
357 
358 		/* Linux uses different names for some filesystems */
359 		fsname = sfs->f_fstypename;
360 		if (strcmp(fsname, "procfs") == 0)
361 			fsname = "proc";
362 		else if (strcmp(fsname, "ext2fs") == 0)
363 			fsname = "ext2";
364 
365 		len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n",
366 			sfs->f_mntfromname,
367 			sfs->f_mntonname,
368 			fsname,
369 			(mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
370 			(mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
371 			(mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
372 			(mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
373 			(mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
374 			(mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
375 			);
376 
377 		mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
378 		memcpy(mtab + mtabsz, bf, len);
379 		mtabsz += len;
380 
381 		simple_lock(&mountlist_slock);
382 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
383 		vfs_unbusy(mp);
384 	}
385 	simple_unlock(&mountlist_slock);
386 	free(bf, M_TEMP);
387 
388 	if (mtabsz > 0) {
389 		error = uiomove_frombuf(mtab, mtabsz, uio);
390 		free(mtab, M_TEMP);
391 	}
392 
393 	return error;
394 }
395