]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/procfs/procfs_map.c
unbound: Vendor import 1.14.0
[FreeBSD/FreeBSD.git] / sys / fs / procfs / procfs_map.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)procfs_status.c     8.3 (Berkeley) 2/17/94
36  *
37  * $FreeBSD$
38  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/lock.h>
43 #include <sys/filedesc.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/resourcevar.h>
48 #include <sys/rwlock.h>
49 #include <sys/sbuf.h>
50 #ifdef COMPAT_FREEBSD32
51 #include <sys/sysent.h>
52 #endif
53 #include <sys/uio.h>
54 #include <sys/user.h>
55 #include <sys/vnode.h>
56
57 #include <fs/pseudofs/pseudofs.h>
58 #include <fs/procfs/procfs.h>
59
60 #include <vm/vm.h>
61 #include <vm/vm_extern.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_object.h>
66
67 #define MEBUFFERSIZE 256
68
69 /*
70  * The map entries can *almost* be read with programs like cat.  However,
71  * large maps need special programs to read.  It is not easy to implement
72  * a program that can sense the required size of the buffer, and then
73  * subsequently do a read with the appropriate size.  This operation cannot
74  * be atomic.  The best that we can do is to allow the program to do a read
75  * with an arbitrarily large buffer, and return as much as we can.  We can
76  * return an error code if the buffer is too small (EFBIG), then the program
77  * can try a bigger buffer.
78  */
79 int
80 procfs_doprocmap(PFS_FILL_ARGS)
81 {
82         struct vmspace *vm;
83         vm_map_t map;
84         vm_map_entry_t entry, tmp_entry;
85         struct vnode *vp;
86         char *fullpath, *freepath, *type;
87         struct ucred *cred;
88         vm_object_t lobj, nobj, obj, tobj;
89         int error, flags, kvme, privateresident, ref_count, resident;
90         int shadow_count;
91         vm_offset_t e_start, e_end;
92         vm_eflags_t e_eflags;
93         vm_prot_t e_prot;
94         unsigned int last_timestamp;
95         bool super;
96 #ifdef COMPAT_FREEBSD32
97         bool wrap32;
98 #endif
99
100         PROC_LOCK(p);
101         error = p_candebug(td, p);
102         PROC_UNLOCK(p);
103         if (error)
104                 return (error);
105
106         if (uio->uio_rw != UIO_READ)
107                 return (EOPNOTSUPP);
108
109 #ifdef COMPAT_FREEBSD32
110         wrap32 = false;
111         if (SV_CURPROC_FLAG(SV_ILP32)) {
112                 if (!(SV_PROC_FLAG(p, SV_ILP32)))
113                         return (EOPNOTSUPP);
114                 wrap32 = true;
115         }
116 #endif
117
118         vm = vmspace_acquire_ref(p);
119         if (vm == NULL)
120                 return (ESRCH);
121         map = &vm->vm_map;
122         vm_map_lock_read(map);
123         VM_MAP_ENTRY_FOREACH(entry, map) {
124                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
125                         continue;
126
127                 e_eflags = entry->eflags;
128                 e_prot = entry->protection;
129                 e_start = entry->start;
130                 e_end = entry->end;
131                 privateresident = 0;
132                 resident = 0;
133                 obj = entry->object.vm_object;
134                 if (obj != NULL) {
135                         VM_OBJECT_RLOCK(obj);
136                         if (obj->shadow_count == 1)
137                                 privateresident = obj->resident_page_count;
138                 }
139                 cred = (entry->cred) ? entry->cred : (obj ? obj->cred : NULL);
140
141                 for (lobj = tobj = obj; tobj != NULL;
142                     tobj = tobj->backing_object) {
143                         if (tobj != obj)
144                                 VM_OBJECT_RLOCK(tobj);
145                         lobj = tobj;
146                 }
147                 if (obj != NULL)
148                         kern_proc_vmmap_resident(map, entry, &resident, &super);
149                 for (tobj = obj; tobj != NULL; tobj = nobj) {
150                         nobj = tobj->backing_object;
151                         if (tobj != obj && tobj != lobj)
152                                 VM_OBJECT_RUNLOCK(tobj);
153                 }
154                 last_timestamp = map->timestamp;
155                 vm_map_unlock_read(map);
156
157                 freepath = NULL;
158                 fullpath = "-";
159                 if (lobj) {
160                         kvme = vm_object_kvme_type(lobj, &vp);
161                         if (vp != NULL)
162                                 vref(vp);
163                         switch (kvme) {
164                         default:
165                                 type = "unknown";
166                                 break;
167                         case KVME_TYPE_PHYS:
168                                 type = "phys";
169                                 break;
170                         case KVME_TYPE_DEFAULT:
171                         case KVME_TYPE_SWAP:
172                                 type = "swap";
173                                 break;
174                         case KVME_TYPE_DEAD:
175                                 type = "dead";
176                                 break;
177                         case KVME_TYPE_VNODE:
178                                 type = "vnode";
179                                 break;
180                         case KVME_TYPE_SG:
181                         case KVME_TYPE_DEVICE:
182                         case KVME_TYPE_MGTDEVICE:
183                                 type = "device";
184                                 break;
185                         }
186                         if (lobj != obj)
187                                 VM_OBJECT_RUNLOCK(lobj);
188
189                         flags = obj->flags;
190                         ref_count = obj->ref_count;
191                         shadow_count = obj->shadow_count;
192                         VM_OBJECT_RUNLOCK(obj);
193                         if (vp != NULL) {
194                                 vn_fullpath(vp, &fullpath, &freepath);
195                                 vrele(vp);
196                         }
197                 } else {
198                         type = "none";
199                         flags = 0;
200                         ref_count = 0;
201                         shadow_count = 0;
202                 }
203
204                 /*
205                  * format:
206                  *  start, end, resident, private resident, cow, access, type,
207                  *         charged, charged uid.
208                  */
209                 error = sbuf_printf(sb,
210                     "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s %s %s %d\n",
211                         (u_long)e_start, (u_long)e_end,
212                         resident, privateresident,
213 #ifdef COMPAT_FREEBSD32
214                         wrap32 ? NULL : obj,    /* Hide 64 bit value */
215 #else
216                         obj,
217 #endif
218                         (e_prot & VM_PROT_READ)?"r":"-",
219                         (e_prot & VM_PROT_WRITE)?"w":"-",
220                         (e_prot & VM_PROT_EXECUTE)?"x":"-",
221                         ref_count, shadow_count, flags,
222                         (e_eflags & MAP_ENTRY_COW)?"COW":"NCOW",
223                         (e_eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
224                         type, fullpath,
225                         cred ? "CH":"NCH", cred ? cred->cr_ruid : -1);
226
227                 if (freepath != NULL)
228                         free(freepath, M_TEMP);
229                 vm_map_lock_read(map);
230                 if (error == -1) {
231                         error = 0;
232                         break;
233                 }
234                 if (last_timestamp != map->timestamp) {
235                         /*
236                          * Look again for the entry because the map was
237                          * modified while it was unlocked.  Specifically,
238                          * the entry may have been clipped, merged, or deleted.
239                          */
240                         vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
241                         entry = tmp_entry;
242                 }
243         }
244         vm_map_unlock_read(map);
245         vmspace_free(vm);
246         return (error);
247 }