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