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