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