procfs_linux.c revision 1.25.4.4 1 1.25.4.4 ad /* $NetBSD: procfs_linux.c,v 1.25.4.4 2007/01/12 01:04:11 ad Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * Written by Frank van der Linden for Wasabi Systems, Inc.
8 1.1 fvdl *
9 1.1 fvdl * Redistribution and use in source and binary forms, with or without
10 1.1 fvdl * modification, are permitted provided that the following conditions
11 1.1 fvdl * are met:
12 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
13 1.1 fvdl * notice, this list of conditions and the following disclaimer.
14 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
16 1.1 fvdl * documentation and/or other materials provided with the distribution.
17 1.1 fvdl * 3. All advertising materials mentioning features or use of this software
18 1.1 fvdl * must display the following acknowledgement:
19 1.1 fvdl * This product includes software developed for the NetBSD Project by
20 1.1 fvdl * Wasabi Systems, Inc.
21 1.1 fvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 fvdl * or promote products derived from this software without specific prior
23 1.1 fvdl * written permission.
24 1.1 fvdl *
25 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 fvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 fvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 fvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 fvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 fvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 fvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 fvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 fvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 fvdl * POSSIBILITY OF SUCH DAMAGE.
36 1.1 fvdl */
37 1.3 lukem
38 1.3 lukem #include <sys/cdefs.h>
39 1.25.4.4 ad __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.25.4.4 2007/01/12 01:04:11 ad Exp $");
40 1.1 fvdl
41 1.1 fvdl #include <sys/param.h>
42 1.1 fvdl #include <sys/systm.h>
43 1.1 fvdl #include <sys/time.h>
44 1.1 fvdl #include <sys/kernel.h>
45 1.1 fvdl #include <sys/proc.h>
46 1.1 fvdl #include <sys/vnode.h>
47 1.11 christos #include <sys/exec.h>
48 1.7 christos #include <sys/resource.h>
49 1.7 christos #include <sys/resourcevar.h>
50 1.7 christos #include <sys/signal.h>
51 1.7 christos #include <sys/signalvar.h>
52 1.7 christos #include <sys/tty.h>
53 1.19 jdolecek #include <sys/malloc.h>
54 1.19 jdolecek #include <sys/mount.h>
55 1.25.4.3 ad #include <sys/conf.h>
56 1.1 fvdl
57 1.1 fvdl #include <miscfs/procfs/procfs.h>
58 1.11 christos #include <compat/linux/common/linux_exec.h>
59 1.1 fvdl
60 1.1 fvdl #include <uvm/uvm_extern.h>
61 1.7 christos #include <uvm/uvm.h>
62 1.1 fvdl
63 1.25.4.3 ad extern struct devsw_conv *devsw_conv;
64 1.25.4.3 ad extern int max_devsw_convs;
65 1.25.4.3 ad
66 1.1 fvdl #define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT)
67 1.1 fvdl #define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10))
68 1.1 fvdl
69 1.25.4.3 ad #define LBFSZ (8 * 1024)
70 1.25.4.3 ad
71 1.1 fvdl /*
72 1.1 fvdl * Linux compatible /proc/meminfo. Only active when the -o linux
73 1.1 fvdl * mountflag is used.
74 1.1 fvdl */
75 1.1 fvdl int
76 1.25.4.3 ad procfs_domeminfo(struct lwp *curl, struct proc *p,
77 1.25.4.3 ad struct pfsnode *pfs, struct uio *uio)
78 1.1 fvdl {
79 1.25.4.3 ad char *bf;
80 1.16 itojun int len;
81 1.25.4.3 ad int error = 0;
82 1.1 fvdl
83 1.25.4.3 ad bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
84 1.25.4.3 ad
85 1.25.4.3 ad len = snprintf(bf, LBFSZ,
86 1.1 fvdl " total: used: free: shared: buffers: cached:\n"
87 1.1 fvdl "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n"
88 1.1 fvdl "Swap: %8lu %8lu %8lu\n"
89 1.1 fvdl "MemTotal: %8lu kB\n"
90 1.1 fvdl "MemFree: %8lu kB\n"
91 1.1 fvdl "MemShared: %8lu kB\n"
92 1.1 fvdl "Buffers: %8lu kB\n"
93 1.1 fvdl "Cached: %8lu kB\n"
94 1.20 perry "SwapTotal: %8lu kB\n"
95 1.1 fvdl "SwapFree: %8lu kB\n",
96 1.1 fvdl PGTOB(uvmexp.npages),
97 1.1 fvdl PGTOB(uvmexp.npages - uvmexp.free),
98 1.1 fvdl PGTOB(uvmexp.free),
99 1.1 fvdl 0L,
100 1.4 chs PGTOB(uvmexp.filepages),
101 1.4 chs PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
102 1.1 fvdl PGTOB(uvmexp.swpages),
103 1.1 fvdl PGTOB(uvmexp.swpginuse),
104 1.1 fvdl PGTOB(uvmexp.swpages - uvmexp.swpginuse),
105 1.1 fvdl PGTOKB(uvmexp.npages),
106 1.1 fvdl PGTOKB(uvmexp.free),
107 1.1 fvdl 0L,
108 1.4 chs PGTOKB(uvmexp.filepages),
109 1.4 chs PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
110 1.1 fvdl PGTOKB(uvmexp.swpages),
111 1.1 fvdl PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
112 1.1 fvdl
113 1.1 fvdl if (len == 0)
114 1.25.4.3 ad goto out;
115 1.1 fvdl
116 1.25.4.3 ad error = uiomove_frombuf(bf, len, uio);
117 1.25.4.3 ad out:
118 1.25.4.3 ad free(bf, M_TEMP);
119 1.25.4.3 ad return error;
120 1.25.4.3 ad }
121 1.25.4.3 ad
122 1.25.4.3 ad /*
123 1.25.4.3 ad * Linux compatible /proc/devices. Only active when the -o linux
124 1.25.4.3 ad * mountflag is used.
125 1.25.4.3 ad */
126 1.25.4.3 ad int
127 1.25.4.3 ad procfs_dodevices(struct lwp *curl, struct proc *p,
128 1.25.4.3 ad struct pfsnode *pfs, struct uio *uio)
129 1.25.4.3 ad {
130 1.25.4.3 ad char *bf;
131 1.25.4.3 ad int offset = 0;
132 1.25.4.3 ad int i, error = ENAMETOOLONG;
133 1.25.4.3 ad
134 1.25.4.4 ad /* XXX elad - may need filtering. */
135 1.25.4.4 ad
136 1.25.4.3 ad bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
137 1.25.4.3 ad
138 1.25.4.3 ad offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
139 1.25.4.3 ad if (offset >= LBFSZ)
140 1.25.4.3 ad goto out;
141 1.25.4.3 ad
142 1.25.4.3 ad for (i = 0; i < max_devsw_convs; i++) {
143 1.25.4.3 ad if ((devsw_conv[i].d_name == NULL) ||
144 1.25.4.3 ad (devsw_conv[i].d_cmajor == -1))
145 1.25.4.3 ad continue;
146 1.25.4.3 ad
147 1.25.4.3 ad offset += snprintf(&bf[offset], LBFSZ - offset,
148 1.25.4.3 ad "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
149 1.25.4.3 ad if (offset >= LBFSZ)
150 1.25.4.3 ad goto out;
151 1.25.4.3 ad }
152 1.25.4.3 ad
153 1.25.4.3 ad offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
154 1.25.4.3 ad if (offset >= LBFSZ)
155 1.25.4.3 ad goto out;
156 1.25.4.3 ad
157 1.25.4.3 ad for (i = 0; i < max_devsw_convs; i++) {
158 1.25.4.3 ad if ((devsw_conv[i].d_name == NULL) ||
159 1.25.4.3 ad (devsw_conv[i].d_bmajor == -1))
160 1.25.4.3 ad continue;
161 1.25.4.3 ad
162 1.25.4.3 ad offset += snprintf(&bf[offset], LBFSZ - offset,
163 1.25.4.3 ad "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
164 1.25.4.3 ad if (offset >= LBFSZ)
165 1.25.4.3 ad goto out;
166 1.25.4.3 ad }
167 1.25.4.3 ad
168 1.25.4.3 ad error = uiomove_frombuf(bf, offset, uio);
169 1.25.4.3 ad out:
170 1.25.4.3 ad free(bf, M_TEMP);
171 1.25.4.3 ad return error;
172 1.1 fvdl }
173 1.1 fvdl
174 1.7 christos /*
175 1.7 christos * Linux compatible /proc/<pid>/stat. Only active when the -o linux
176 1.7 christos * mountflag is used.
177 1.7 christos */
178 1.7 christos int
179 1.25.4.3 ad procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
180 1.25.4.3 ad struct pfsnode *pfs, struct uio *uio)
181 1.7 christos {
182 1.25.4.3 ad char *bf;
183 1.25 christos struct proc *p = l->l_proc;
184 1.16 itojun int len;
185 1.7 christos struct tty *tty = p->p_session->s_ttyp;
186 1.7 christos struct rusage *ru = &p->p_stats->p_ru;
187 1.7 christos struct rusage *cru = &p->p_stats->p_cru;
188 1.25.4.2 ad struct vmspace *vm;
189 1.25.4.2 ad struct vm_map *map;
190 1.11 christos struct vm_map_entry *entry;
191 1.7 christos unsigned long stext = 0, etext = 0, sstack = 0;
192 1.25.4.1 ad struct timeval rt;
193 1.25.4.3 ad int error = 0;
194 1.25.4.3 ad
195 1.25.4.3 ad bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
196 1.7 christos
197 1.25.4.2 ad proc_vmspace_getref(p, &vm);
198 1.25.4.2 ad map = &vm->vm_map;
199 1.25.4.2 ad vm_map_lock_read(map);
200 1.25.4.2 ad
201 1.7 christos for (entry = map->header.next; entry != &map->header;
202 1.7 christos entry = entry->next) {
203 1.7 christos if (UVM_ET_ISSUBMAP(entry))
204 1.7 christos continue;
205 1.7 christos /* assume text is the first entry */
206 1.7 christos if (stext == etext) {
207 1.7 christos stext = entry->start;
208 1.7 christos etext = entry->end;
209 1.11 christos break;
210 1.7 christos }
211 1.7 christos }
212 1.13 christos #ifdef LINUX_USRSTACK
213 1.12 christos if (strcmp(p->p_emul->e_name, "linux") == 0 &&
214 1.12 christos LINUX_USRSTACK < USRSTACK)
215 1.14 he sstack = (unsigned long) LINUX_USRSTACK;
216 1.11 christos else
217 1.13 christos #endif
218 1.14 he sstack = (unsigned long) USRSTACK;
219 1.7 christos
220 1.25.4.2 ad vm_map_unlock_read(map);
221 1.25.4.2 ad uvmspace_free(vm);
222 1.25.4.2 ad
223 1.25.4.2 ad rw_enter(&proclist_lock, RW_READER);
224 1.25.4.2 ad mutex_enter(&p->p_mutex);
225 1.25.4.2 ad mutex_enter(&p->p_smutex);
226 1.7 christos
227 1.25.4.1 ad calcru(p, NULL, NULL, NULL, &rt);
228 1.25.4.1 ad
229 1.25.4.3 ad len = snprintf(bf, LBFSZ,
230 1.7 christos "%d (%s) %c %d %d %d %d %d "
231 1.7 christos "%u "
232 1.7 christos "%lu %lu %lu %lu %lu %lu %lu %lu "
233 1.7 christos "%d %d %d "
234 1.8 hannken "%lu %lu %lu %lu %" PRIu64 " "
235 1.7 christos "%lu %lu %lu "
236 1.7 christos "%u %u "
237 1.7 christos "%u %u %u %u "
238 1.7 christos "%lu %lu %lu %d %d\n",
239 1.7 christos
240 1.7 christos p->p_pid,
241 1.7 christos p->p_comm,
242 1.7 christos "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
243 1.25.4.3 ad (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0,
244 1.7 christos
245 1.7 christos p->p_pgid,
246 1.7 christos p->p_session->s_sid,
247 1.7 christos tty ? tty->t_dev : 0,
248 1.15 christos (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0,
249 1.7 christos
250 1.7 christos p->p_flag,
251 1.7 christos
252 1.7 christos ru->ru_minflt,
253 1.7 christos cru->ru_minflt,
254 1.7 christos ru->ru_majflt,
255 1.7 christos cru->ru_majflt,
256 1.7 christos ru->ru_utime.tv_sec,
257 1.7 christos ru->ru_stime.tv_sec,
258 1.7 christos cru->ru_utime.tv_sec,
259 1.7 christos cru->ru_stime.tv_sec,
260 1.7 christos
261 1.7 christos p->p_nice, /* XXX: priority */
262 1.7 christos p->p_nice,
263 1.7 christos 0,
264 1.7 christos
265 1.25.4.1 ad rt.tv_sec,
266 1.7 christos p->p_stats->p_start.tv_sec,
267 1.7 christos ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
268 1.7 christos ru->ru_maxrss,
269 1.7 christos p->p_rlimit[RLIMIT_RSS].rlim_cur,
270 1.7 christos
271 1.7 christos stext, /* start code */
272 1.7 christos etext, /* end code */
273 1.7 christos sstack, /* mm start stack */
274 1.7 christos 0, /* XXX: pc */
275 1.7 christos 0, /* XXX: sp */
276 1.25.4.1 ad p->p_sigpend.sp_set.__bits[0], /* XXX: pending */
277 1.25.4.1 ad 0, /* XXX: held */
278 1.7 christos p->p_sigctx.ps_sigignore.__bits[0], /* ignored */
279 1.7 christos p->p_sigctx.ps_sigcatch.__bits[0], /* caught */
280 1.7 christos
281 1.7 christos (unsigned long)(intptr_t)l->l_wchan,
282 1.7 christos ru->ru_nvcsw,
283 1.7 christos ru->ru_nivcsw,
284 1.7 christos p->p_exitsig,
285 1.7 christos 0); /* XXX: processor */
286 1.7 christos
287 1.25.4.2 ad mutex_exit(&p->p_smutex);
288 1.25.4.2 ad mutex_exit(&p->p_mutex);
289 1.25.4.2 ad rw_exit(&proclist_lock);
290 1.25.4.2 ad
291 1.7 christos if (len == 0)
292 1.25.4.3 ad goto out;
293 1.7 christos
294 1.25.4.3 ad error = uiomove_frombuf(bf, len, uio);
295 1.25.4.3 ad out:
296 1.25.4.3 ad free(bf, M_TEMP);
297 1.25.4.3 ad return error;
298 1.7 christos }
299 1.7 christos
300 1.1 fvdl int
301 1.25.4.3 ad procfs_docpuinfo(struct lwp *curl, struct proc *p,
302 1.25.4.3 ad struct pfsnode *pfs, struct uio *uio)
303 1.1 fvdl {
304 1.25.4.3 ad int len = LBFSZ;
305 1.23 christos char *bf = malloc(len, M_TEMP, M_WAITOK);
306 1.21 christos int error;
307 1.1 fvdl
308 1.23 christos if (procfs_getcpuinfstr(bf, &len) < 0) {
309 1.21 christos error = ENOSPC;
310 1.21 christos goto done;
311 1.21 christos }
312 1.5 jrf
313 1.21 christos if (len == 0) {
314 1.21 christos error = 0;
315 1.21 christos goto done;
316 1.21 christos }
317 1.5 jrf
318 1.23 christos error = uiomove_frombuf(bf, len, uio);
319 1.21 christos done:
320 1.23 christos free(bf, M_TEMP);
321 1.21 christos return error;
322 1.5 jrf }
323 1.5 jrf
324 1.5 jrf int
325 1.25.4.3 ad procfs_douptime(struct lwp *curl, struct proc *p,
326 1.25.4.3 ad struct pfsnode *pfs, struct uio *uio)
327 1.5 jrf {
328 1.25.4.3 ad char *bf;
329 1.16 itojun int len;
330 1.5 jrf struct timeval runtime;
331 1.5 jrf u_int64_t idle;
332 1.25.4.3 ad int error = 0;
333 1.25.4.3 ad
334 1.25.4.3 ad bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
335 1.5 jrf
336 1.5 jrf timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
337 1.5 jrf idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
338 1.25.4.3 ad len = snprintf(bf, LBFSZ,
339 1.16 itojun "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
340 1.16 itojun runtime.tv_sec, runtime.tv_usec / 10000,
341 1.16 itojun idle / hz, (((idle % hz) * 100) / hz) % 100);
342 1.1 fvdl
343 1.1 fvdl if (len == 0)
344 1.25.4.3 ad goto out;
345 1.1 fvdl
346 1.25.4.3 ad error = uiomove_frombuf(bf, len, uio);
347 1.25.4.3 ad out:
348 1.25.4.3 ad free(bf, M_TEMP);
349 1.25.4.3 ad return error;
350 1.1 fvdl }
351 1.19 jdolecek
352 1.19 jdolecek int
353 1.25.4.3 ad procfs_domounts(struct lwp *curl, struct proc *p,
354 1.25.4.3 ad struct pfsnode *pfs, struct uio *uio)
355 1.19 jdolecek {
356 1.25.4.3 ad char *bf, *mtab = NULL;
357 1.19 jdolecek const char *fsname;
358 1.19 jdolecek size_t len, mtabsz = 0;
359 1.19 jdolecek struct mount *mp, *nmp;
360 1.19 jdolecek struct statvfs *sfs;
361 1.19 jdolecek int error = 0;
362 1.19 jdolecek
363 1.25.4.4 ad /* XXX elad - may need filtering. */
364 1.25.4.4 ad
365 1.25.4.3 ad bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
366 1.19 jdolecek simple_lock(&mountlist_slock);
367 1.19 jdolecek for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
368 1.19 jdolecek mp = nmp) {
369 1.19 jdolecek if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
370 1.19 jdolecek nmp = CIRCLEQ_NEXT(mp, mnt_list);
371 1.19 jdolecek continue;
372 1.19 jdolecek }
373 1.19 jdolecek
374 1.19 jdolecek sfs = &mp->mnt_stat;
375 1.19 jdolecek
376 1.19 jdolecek /* Linux uses different names for some filesystems */
377 1.19 jdolecek fsname = sfs->f_fstypename;
378 1.19 jdolecek if (strcmp(fsname, "procfs") == 0)
379 1.19 jdolecek fsname = "proc";
380 1.19 jdolecek else if (strcmp(fsname, "ext2fs") == 0)
381 1.19 jdolecek fsname = "ext2";
382 1.20 perry
383 1.25.4.3 ad len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n",
384 1.19 jdolecek sfs->f_mntfromname,
385 1.19 jdolecek sfs->f_mntonname,
386 1.19 jdolecek fsname,
387 1.19 jdolecek (mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw",
388 1.19 jdolecek (mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "",
389 1.19 jdolecek (mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "",
390 1.19 jdolecek (mp->mnt_flag & MNT_NODEV) ? ",nodev" : "",
391 1.19 jdolecek (mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "",
392 1.19 jdolecek (mp->mnt_flag & MNT_NOATIME) ? ",noatime" : ""
393 1.19 jdolecek );
394 1.19 jdolecek
395 1.19 jdolecek mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK);
396 1.23 christos memcpy(mtab + mtabsz, bf, len);
397 1.19 jdolecek mtabsz += len;
398 1.19 jdolecek
399 1.19 jdolecek simple_lock(&mountlist_slock);
400 1.19 jdolecek nmp = CIRCLEQ_NEXT(mp, mnt_list);
401 1.19 jdolecek vfs_unbusy(mp);
402 1.19 jdolecek }
403 1.19 jdolecek simple_unlock(&mountlist_slock);
404 1.25.4.3 ad free(bf, M_TEMP);
405 1.19 jdolecek
406 1.19 jdolecek if (mtabsz > 0) {
407 1.19 jdolecek error = uiomove_frombuf(mtab, mtabsz, uio);
408 1.19 jdolecek free(mtab, M_TEMP);
409 1.19 jdolecek }
410 1.19 jdolecek
411 1.19 jdolecek return error;
412 1.19 jdolecek }
413