xref: /netbsd-src/sys/miscfs/procfs/procfs_linux.c (revision a687717695232fce70067c2acc6583297da39c9f)
1 /*      $NetBSD: procfs_linux.c,v 1.31 2006/12/24 16:45:23 elad 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.31 2006/12/24 16:45:23 elad 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 vm_map *map = &p->p_vmspace->vm_map;
189 	struct vm_map_entry *entry;
190 	unsigned long stext = 0, etext = 0, sstack = 0;
191 	int error = 0;
192 
193 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
194 
195 	if (map != &curproc->p_vmspace->vm_map)
196 		vm_map_lock_read(map);
197 	for (entry = map->header.next; entry != &map->header;
198 	    entry = entry->next) {
199 		if (UVM_ET_ISSUBMAP(entry))
200 			continue;
201 		/* assume text is the first entry */
202 		if (stext == etext) {
203 			stext = entry->start;
204 			etext = entry->end;
205 			break;
206 		}
207 	}
208 #ifdef LINUX_USRSTACK
209 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
210 	    LINUX_USRSTACK < USRSTACK)
211 		sstack = (unsigned long) LINUX_USRSTACK;
212 	else
213 #endif
214 		sstack = (unsigned long) USRSTACK;
215 
216 	if (map != &curproc->p_vmspace->vm_map)
217 		vm_map_unlock_read(map);
218 
219 	len = snprintf(bf, LBFSZ,
220 	    "%d (%s) %c %d %d %d %d %d "
221 	    "%u "
222 	    "%lu %lu %lu %lu %lu %lu %lu %lu "
223 	    "%d %d %d "
224 	    "%lu %lu %lu %lu %" PRIu64 " "
225 	    "%lu %lu %lu "
226 	    "%u %u "
227 	    "%u %u %u %u "
228 	    "%lu %lu %lu %d %d\n",
229 
230 	    p->p_pid,
231 	    p->p_comm,
232 	    "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
233 	    (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
234 
235 	    p->p_pgid,
236 	    p->p_session->s_sid,
237 	    tty ? tty->t_dev : 0,
238 	    (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
239 
240 	    p->p_flag,
241 
242 	    ru->ru_minflt,
243 	    cru->ru_minflt,
244 	    ru->ru_majflt,
245 	    cru->ru_majflt,
246 	    ru->ru_utime.tv_sec,
247 	    ru->ru_stime.tv_sec,
248 	    cru->ru_utime.tv_sec,
249 	    cru->ru_stime.tv_sec,
250 
251 	    p->p_nice,					/* XXX: priority */
252 	    p->p_nice,
253 	    0,
254 
255 	    p->p_rtime.tv_sec,
256 	    p->p_stats->p_start.tv_sec,
257 	    ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
258 	    ru->ru_maxrss,
259 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,
260 
261 	    stext,					/* start code */
262 	    etext,					/* end code */
263 	    sstack,					/* mm start stack */
264 	    0,						/* XXX: pc */
265 	    0,						/* XXX: sp */
266 	    p->p_sigctx.ps_siglist.__bits[0],		/* pending */
267 	    p->p_sigctx.ps_sigmask.__bits[0],		/* blocked */
268 	    p->p_sigctx.ps_sigignore.__bits[0],		/* ignored */
269 	    p->p_sigctx.ps_sigcatch.__bits[0],		/* caught */
270 
271 	    (unsigned long)(intptr_t)l->l_wchan,
272 	    ru->ru_nvcsw,
273 	    ru->ru_nivcsw,
274 	    p->p_exitsig,
275 	    0);						/* XXX: processor */
276 
277 	if (len == 0)
278 		goto out;
279 
280 	error = uiomove_frombuf(bf, len, uio);
281 out:
282 	free(bf, M_TEMP);
283 	return error;
284 }
285 
286 int
287 procfs_docpuinfo(struct lwp *curl, struct proc *p,
288     struct pfsnode *pfs, struct uio *uio)
289 {
290 	int len = LBFSZ;
291 	char *bf = malloc(len, M_TEMP, M_WAITOK);
292 	int error;
293 
294 	if (procfs_getcpuinfstr(bf, &len) < 0) {
295 		error = ENOSPC;
296 		goto done;
297 	}
298 
299 	if (len == 0) {
300 		error = 0;
301 		goto done;
302 	}
303 
304 	error = uiomove_frombuf(bf, len, uio);
305 done:
306 	free(bf, M_TEMP);
307 	return error;
308 }
309 
310 int
311 procfs_douptime(struct lwp *curl, struct proc *p,
312     struct pfsnode *pfs, struct uio *uio)
313 {
314 	char *bf;
315 	int len;
316 	struct timeval runtime;
317 	u_int64_t idle;
318 	int error = 0;
319 
320 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
321 
322 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
323 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
324 	len = snprintf(bf, LBFSZ,
325 	    "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
326 	    runtime.tv_sec, runtime.tv_usec / 10000,
327 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
328 
329 	if (len == 0)
330 		goto out;
331 
332 	error = uiomove_frombuf(bf, len, uio);
333 out:
334 	free(bf, M_TEMP);
335 	return error;
336 }
337 
338 int
339 procfs_domounts(struct lwp *curl, struct proc *p,
340     struct pfsnode *pfs, struct uio *uio)
341 {
342 	char *bf, *mtab = NULL;
343 	const char *fsname;
344 	size_t len, mtabsz = 0;
345 	struct mount *mp, *nmp;
346 	struct statvfs *sfs;
347 	int error = 0;
348 
349 	/* XXX elad - may need filtering. */
350 
351 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
352 	simple_lock(&mountlist_slock);
353 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
354 	     mp = nmp) {
355 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
356 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
357 			continue;
358 		}
359 
360 		sfs = &mp->mnt_stat;
361 
362 		/* Linux uses different names for some filesystems */
363 		fsname = sfs->f_fstypename;
364 		if (strcmp(fsname, "procfs") == 0)
365 			fsname = "proc";
366 		else if (strcmp(fsname, "ext2fs") == 0)
367 			fsname = "ext2";
368 
369 		len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n",
370 			sfs->f_mntfromname,
371 			sfs->f_mntonname,
372 			fsname,
373 			(mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
374 			(mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
375 			(mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
376 			(mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
377 			(mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
378 			(mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
379 			);
380 
381 		mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
382 		memcpy(mtab + mtabsz, bf, len);
383 		mtabsz += len;
384 
385 		simple_lock(&mountlist_slock);
386 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
387 		vfs_unbusy(mp);
388 	}
389 	simple_unlock(&mountlist_slock);
390 	free(bf, M_TEMP);
391 
392 	if (mtabsz > 0) {
393 		error = uiomove_frombuf(mtab, mtabsz, uio);
394 		free(mtab, M_TEMP);
395 	}
396 
397 	return error;
398 }
399