procfs_linux.c revision 1.10.2.1 1 /* $NetBSD: procfs_linux.c,v 1.10.2.1 2003/07/02 15:26:53 darrenr 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.10.2.1 2003/07/02 15:26:53 darrenr 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/resource.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signal.h>
50 #include <sys/signalvar.h>
51 #include <sys/tty.h>
52
53 #include <miscfs/procfs/procfs.h>
54
55 #include <uvm/uvm_extern.h>
56 #include <uvm/uvm.h>
57
58 #define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT)
59 #define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10))
60
61 /*
62 * Linux compatible /proc/meminfo. Only active when the -o linux
63 * mountflag is used.
64 */
65 int
66 procfs_domeminfo(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
67 struct uio *uio)
68 {
69 char buf[512], *cp;
70 int len, error;
71
72 len = snprintf(buf, sizeof buf,
73 " total: used: free: shared: buffers: cached:\n"
74 "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n"
75 "Swap: %8lu %8lu %8lu\n"
76 "MemTotal: %8lu kB\n"
77 "MemFree: %8lu kB\n"
78 "MemShared: %8lu kB\n"
79 "Buffers: %8lu kB\n"
80 "Cached: %8lu kB\n"
81 "SwapTotal: %8lu kB\n"
82 "SwapFree: %8lu kB\n",
83 PGTOB(uvmexp.npages),
84 PGTOB(uvmexp.npages - uvmexp.free),
85 PGTOB(uvmexp.free),
86 0L,
87 PGTOB(uvmexp.filepages),
88 PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
89 PGTOB(uvmexp.swpages),
90 PGTOB(uvmexp.swpginuse),
91 PGTOB(uvmexp.swpages - uvmexp.swpginuse),
92 PGTOKB(uvmexp.npages),
93 PGTOKB(uvmexp.free),
94 0L,
95 PGTOKB(uvmexp.filepages),
96 PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
97 PGTOKB(uvmexp.swpages),
98 PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
99
100 if (len == 0)
101 return 0;
102
103 len -= uio->uio_offset;
104 cp = buf + uio->uio_offset;
105 len = imin(len, uio->uio_resid);
106 if (len <= 0)
107 error = 0;
108 else
109 error = uiomove(cp, len, uio);
110 return error;
111 }
112
113 /*
114 * Linux compatible /proc/<pid>/stat. Only active when the -o linux
115 * mountflag is used.
116 */
117 int
118 procfs_do_pid_stat(struct lwp *curl, struct lwp *l, struct pfsnode *pfs,
119 struct uio *uio)
120 {
121 struct proc *p = curl->l_proc;
122 char buf[512], *cp;
123 int len, error;
124 struct tty *tty = p->p_session->s_ttyp;
125 struct rusage *ru = &p->p_stats->p_ru;
126 struct rusage *cru = &p->p_stats->p_cru;
127 struct vm_map *map = &p->p_vmspace->vm_map;
128 struct vm_map_entry *entry, *last = NULL;
129 unsigned long stext = 0, etext = 0, sstack = 0;
130
131 if (map != &curproc->p_vmspace->vm_map)
132 vm_map_lock_read(map);
133 for (entry = map->header.next; entry != &map->header;
134 entry = entry->next) {
135 if (UVM_ET_ISSUBMAP(entry))
136 continue;
137 /* assume text is the first entry */
138 if (stext == etext) {
139 stext = entry->start;
140 etext = entry->end;
141 }
142 last = entry;
143 }
144 /* assume stack is the last entry */
145 if (last != NULL)
146 sstack = last->start;
147
148 if (map != &curproc->p_vmspace->vm_map)
149 vm_map_unlock_read(map);
150
151 len = snprintf(buf, sizeof(buf),
152 "%d (%s) %c %d %d %d %d %d "
153 "%u "
154 "%lu %lu %lu %lu %lu %lu %lu %lu "
155 "%d %d %d "
156 "%lu %lu %lu %lu %" PRIu64 " "
157 "%lu %lu %lu "
158 "%u %u "
159 "%u %u %u %u "
160 "%lu %lu %lu %d %d\n",
161
162 p->p_pid,
163 p->p_comm,
164 "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat],
165 p->p_pptr->p_pid,
166
167 p->p_pgid,
168 p->p_session->s_sid,
169 tty ? tty->t_dev : 0,
170 tty ? tty->t_pgrp->pg_id : 0,
171
172 p->p_flag,
173
174 ru->ru_minflt,
175 cru->ru_minflt,
176 ru->ru_majflt,
177 cru->ru_majflt,
178 ru->ru_utime.tv_sec,
179 ru->ru_stime.tv_sec,
180 cru->ru_utime.tv_sec,
181 cru->ru_stime.tv_sec,
182
183 p->p_nice, /* XXX: priority */
184 p->p_nice,
185 0,
186
187 p->p_rtime.tv_sec,
188 p->p_stats->p_start.tv_sec,
189 ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss,
190 ru->ru_maxrss,
191 p->p_rlimit[RLIMIT_RSS].rlim_cur,
192
193 stext, /* start code */
194 etext, /* end code */
195 sstack, /* mm start stack */
196 0, /* XXX: pc */
197 0, /* XXX: sp */
198 p->p_sigctx.ps_siglist.__bits[0], /* pending */
199 p->p_sigctx.ps_sigmask.__bits[0], /* blocked */
200 p->p_sigctx.ps_sigignore.__bits[0], /* ignored */
201 p->p_sigctx.ps_sigcatch.__bits[0], /* caught */
202
203 (unsigned long)(intptr_t)l->l_wchan,
204 ru->ru_nvcsw,
205 ru->ru_nivcsw,
206 p->p_exitsig,
207 0); /* XXX: processor */
208
209 if (len == 0)
210 return 0;
211
212 len -= uio->uio_offset;
213 cp = buf + uio->uio_offset;
214 len = imin(len, uio->uio_resid);
215 if (len <= 0)
216 error = 0;
217 else
218 error = uiomove(cp, len, uio);
219 return error;
220 }
221
222 int
223 procfs_docpuinfo(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
224 struct uio *uio)
225 {
226 char buf[512], *cp;
227 int len, error;
228
229 len = sizeof buf;
230 if (procfs_getcpuinfstr(buf, &len) < 0)
231 return EIO;
232
233 if (len == 0)
234 return 0;
235
236 len -= uio->uio_offset;
237 cp = buf + uio->uio_offset;
238 len = imin(len, uio->uio_resid);
239 if (len <= 0)
240 error = 0;
241 else
242 error = uiomove(cp, len, uio);
243 return error;
244 }
245
246 int
247 procfs_douptime(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
248 struct uio *uio)
249 {
250 char buf[512], *cp;
251 int len, error;
252 struct timeval runtime;
253 u_int64_t idle;
254
255 timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
256 idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
257 /*###251 [cc] unterminated string or character constant%%%*/
258 len = sprintf(buf, "%lu.%02lu %" PRIu64 ".%02" PRIu64 "\n",
259 runtime.tv_sec, runtime.tv_usec / 10000,
260 idle / hz, (((idle % hz) * 100) / hz) % 100);
261
262 if (len == 0)
263 return 0;
264
265 len -= uio->uio_offset;
266 cp = buf + uio->uio_offset;
267 len = imin(len, uio->uio_resid);
268 if (len <= 0)
269 error = 0;
270 else
271 error = uiomove(cp, len, uio);
272 return error;
273 }
274