procfs_map.c revision 1.10.2.3 1 /* $NetBSD: procfs_map.c,v 1.10.2.3 2001/06/21 20:07:43 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1993 Jan-Simon Pendry
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)procfs_status.c 8.3 (Berkeley) 2/17/94
40 *
41 * $FreeBSD: procfs_map.c,v 1.18 1998/12/04 22:54:51 archie Exp $
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/lwp.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/namei.h>
51 #include <miscfs/procfs/procfs.h>
52
53 #include <sys/lock.h>
54
55 #include <uvm/uvm.h>
56
57 #define MEBUFFERSIZE 256
58
59 extern int getcwd_common __P((struct vnode *, struct vnode *,
60 char **, char *, int, int, struct proc *));
61
62 static int procfs_vnode_to_path(struct vnode *vp, char *path, int len,
63 struct proc *curp, struct proc *p);
64
65 /*
66 * The map entries can *almost* be read with programs like cat. However,
67 * large maps need special programs to read. It is not easy to implement
68 * a program that can sense the required size of the buffer, and then
69 * subsequently do a read with the appropriate size. This operation cannot
70 * be atomic. The best that we can do is to allow the program to do a read
71 * with an arbitrarily large buffer, and return as much as we can. We can
72 * return an error code if the buffer is too small (EFBIG), then the program
73 * can try a bigger buffer.
74 */
75 int
76 procfs_domap(struct proc *curp, struct proc *p, struct pfsnode *pfs,
77 struct uio *uio, int linuxmode)
78 {
79 int len;
80 int error, buf_full;
81 struct vm_map *map = &p->p_vmspace->vm_map;
82 struct vm_map_entry *entry;
83 char mebuffer[MEBUFFERSIZE];
84 char *path;
85 struct vnode *vp;
86 struct vattr va;
87 dev_t dev;
88 long fileid;
89
90 if (uio->uio_rw != UIO_READ)
91 return (EOPNOTSUPP);
92
93 if (uio->uio_offset != 0)
94 return (0);
95
96 error = 0;
97 buf_full = 0;
98 if (map != &curproc->l_proc->p_vmspace->vm_map)
99 vm_map_lock_read(map);
100 for (entry = map->header.next;
101 ((uio->uio_resid > 0) && (entry != &map->header));
102 entry = entry->next) {
103
104 if (UVM_ET_ISSUBMAP(entry))
105 continue;
106
107 if (linuxmode != 0) {
108 path = (char *)malloc(MAXPATHLEN * 4, M_TEMP, M_WAITOK);
109 if (path == NULL) {
110 error = ENOMEM;
111 break;
112 }
113 *path = 0;
114
115 dev = (dev_t)0;
116 fileid = 0;
117 if (UVM_ET_ISOBJ(entry) &&
118 UVM_OBJ_IS_VNODE(entry->object.uvm_obj)) {
119 vp = (struct vnode *)entry->object.uvm_obj;
120 error = VOP_GETATTR(vp, &va, curp->p_ucred,
121 curp);
122 if (error == 0 && vp != pfs->pfs_vnode) {
123 fileid = va.va_fileid;
124 dev = va.va_fsid;
125 error = procfs_vnode_to_path(vp, path,
126 MAXPATHLEN * 4, curp, p);
127 }
128 }
129 snprintf(mebuffer, sizeof(mebuffer),
130 "%0*lx-%0*lx %c%c%c%c %0*lx %02x:%02x %ld %s\n",
131 (int)sizeof(void *) * 2,(unsigned long)entry->start,
132 (int)sizeof(void *) * 2,(unsigned long)entry->end,
133 (entry->protection & VM_PROT_READ) ? 'r' : '-',
134 (entry->protection & VM_PROT_WRITE) ? 'w' : '-',
135 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-',
136 (entry->etype & UVM_ET_COPYONWRITE) ? 'p' : 's',
137 (int)sizeof(void *) * 2,
138 (unsigned long)entry->offset,
139 major(dev), minor(dev), fileid, path);
140 free(path, M_TEMP);
141 } else {
142 snprintf(mebuffer, sizeof(mebuffer),
143 "0x%lx 0x%lx %c%c%c %c%c%c %s %s %d %d %d\n",
144 entry->start, entry->end,
145 (entry->protection & VM_PROT_READ) ? 'r' : '-',
146 (entry->protection & VM_PROT_WRITE) ? 'w' : '-',
147 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-',
148 (entry->max_protection & VM_PROT_READ) ? 'r' : '-',
149 (entry->max_protection & VM_PROT_WRITE) ? 'w' : '-',
150 (entry->max_protection & VM_PROT_EXECUTE) ?
151 'x' : '-',
152 (entry->etype & UVM_ET_COPYONWRITE) ?
153 "COW" : "NCOW",
154 (entry->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC",
155 entry->inheritance, entry->wired_count,
156 entry->advice);
157 }
158
159 len = strlen(mebuffer);
160 if (len > uio->uio_resid) {
161 error = EFBIG;
162 break;
163 }
164 error = uiomove(mebuffer, len, uio);
165 if (error)
166 break;
167 }
168 if (map != &curproc->l_proc->p_vmspace->vm_map)
169 vm_map_unlock_read(map);
170 return error;
171 }
172
173 int
174 procfs_validmap(struct lwp *l, struct mount *mp)
175 {
176 return ((l->l_proc->p_flag & P_SYSTEM) == 0);
177 }
178
179 /*
180 * Try to find a pathname for a vnode. Since there is no mapping
181 * vnode -> parent directory, this needs the NAMECACHE_ENTER_REVERSE
182 * option to work (to make cache_revlookup succeed).
183 */
184 static int procfs_vnode_to_path(struct vnode *vp, char *path, int len,
185 struct proc *curp, struct proc *p)
186 {
187 int error, lenused, elen;
188 char *bp, *bend;
189 struct vnode *dvp;
190
191 bp = bend = &path[len];
192 *(--bp) = '\0';
193
194 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
195 if (error != 0)
196 return error;
197 error = cache_revlookup(vp, &dvp, &bp, path);
198 vput(vp);
199 if (error != 0)
200 return (error == -1 ? ENOENT : error);
201
202 error = vget(dvp, 0);
203 if (error != 0)
204 return error;
205 *(--bp) = '/';
206 /* XXX GETCWD_CHECK_ACCESS == 0x0001 */
207 error = getcwd_common(dvp, NULL, &bp, path, len / 2, 1, curp);
208
209 /*
210 * Strip off emulation path for emulated processes looking at
211 * the maps file of a process of the same emulation. (Won't
212 * work if /emul/xxx is a symlink..)
213 */
214 if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
215 elen = strlen(curp->p_emul->e_path);
216 if (!strncmp(bp, curp->p_emul->e_path, elen))
217 bp = &bp[elen];
218 }
219
220 lenused = bend - bp;
221
222 memcpy(path, bp, lenused);
223 path[lenused] = 0;
224
225 return 0;
226 }
227