1 /* $NetBSD: misc.c,v 1.24 2020/09/13 04:14:48 isaki Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __RCSID("$NetBSD: misc.c,v 1.24 2020/09/13 04:14:48 isaki Exp $"); 34 35 #include <stdbool.h> 36 #include <sys/param.h> 37 #include <sys/types.h> 38 #include <sys/time.h> 39 #include <sys/stat.h> 40 #include <sys/condvar.h> 41 #include <sys/selinfo.h> 42 #include <sys/filedesc.h> 43 #define _KERNEL 44 #include <sys/mqueue.h> 45 #include <sys/eventvar.h> 46 #undef _KERNEL 47 #include <sys/proc.h> 48 #define _KERNEL 49 #include <sys/file.h> 50 #define copyin_t int 51 #define copyout_t int 52 #include <sys/ksem.h> 53 #define _LIB_LIBKERN_LIBKERN_H_ 54 #define mutex_enter(a) 55 #define mutex_exit(a) 56 #undef _KERNEL 57 #include <sys/cprng.h> 58 #include <sys/vnode.h> 59 #include <sys/mount.h> 60 61 #include <net/bpfdesc.h> 62 63 #include <dev/audio/audiodef.h> 64 #include <dev/audio/audio_if.h> 65 66 #include <err.h> 67 #include <util.h> 68 #include <string.h> 69 #include <kvm.h> 70 #include "fstat.h" 71 72 static struct nlist nl[] = { 73 #define NL_BPF 0 74 { .n_name = "bpf_fileops", }, 75 #define NL_CRYPTO 1 76 { .n_name = "cryptofops" }, 77 #define NL_DMIO 2 78 { .n_name = "dmio_fileops", }, 79 #define NL_DRVCTL 3 80 { .n_name = "drvctl_fileops", }, 81 #define NL_DTV_DEMUX 4 82 { .n_name = "dtv_demux_fileops", }, 83 #define NL_FILEMON 5 84 { .n_name = "filemon_fileops", }, 85 #define NL_KQUEUE 6 86 { .n_name = "kqueueops" }, 87 #define NL_MQUEUE 7 88 { .n_name = "mqops" }, 89 #define NL_PIPE 8 90 { .n_name = "pipeops" }, 91 #define NL_PUTTER 9 92 { .n_name = "putter_fileops", }, 93 #define NL_RND 10 94 { .n_name = "rnd_fileops", }, 95 #define NL_SEM 11 96 { .n_name = "semops", }, 97 #define NL_SOCKET 12 98 { .n_name = "socketops" }, 99 #define NL_SVR4_NET 13 100 { .n_name = "svr4_netops" }, 101 #define NL_SVR4_32_NET 14 102 { .n_name = "svr4_32_netops" }, 103 #define NL_TAP 15 104 { .n_name = "tap_fileops", }, 105 #define NL_VNOPS 16 106 { .n_name = "vnops" }, 107 #define NL_XENEVT 17 108 { .n_name = "xenevt_fileops" }, 109 #define NL_AUDIO 18 110 { .n_name = "audio_fileops" }, 111 #define NL_PAD 19 112 { .n_name = "pad_fileops" }, 113 #define NL_MAX 20 114 { .n_name = NULL } 115 }; 116 117 extern int vflg; 118 119 120 static int 121 p_bpf(struct file *f) 122 { 123 struct bpf_d bpf; 124 struct bpf_if bi; 125 struct ifnet ifn; 126 127 strlcpy(ifn.if_xname, "???", sizeof(ifn.if_xname)); 128 129 if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) { 130 dprintf("can't read bpf at %p for pid %d", f->f_data, Pid); 131 return 0; 132 } 133 if (bpf.bd_bif != NULL) { 134 if (!KVM_READ(bpf.bd_bif, &bi, sizeof(bi))) 135 dprintf("can't read bpf interface at %p for pid %d", 136 bpf.bd_bif, Pid); 137 if (bi.bif_ifp != NULL) 138 if (!KVM_READ(bi.bif_ifp, &ifn, sizeof(ifn))) 139 dprintf("can't read net interfsace" 140 " at %p for pid %d", bi.bif_ifp, Pid); 141 } 142 (void)printf("* bpf@%s rec=%lu, dr=%lu, cap=%lu, pid=%lu", ifn.if_xname, 143 bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount, 144 (unsigned long)bpf.bd_pid); 145 if (bpf.bd_promisc) 146 (void)printf(", promisc"); 147 if (bpf.bd_immediate) 148 (void)printf(", immed"); 149 if (bpf.bd_direction == BPF_D_IN) 150 (void)printf(", in"); 151 else if (bpf.bd_direction == BPF_D_INOUT) 152 (void)printf(", inout"); 153 else if (bpf.bd_direction == BPF_D_OUT) 154 (void)printf(", out"); 155 if (bpf.bd_jitcode != NULL) 156 (void)printf(", jit"); 157 if (bpf.bd_async) 158 (void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid); 159 if (bpf.bd_state == BPF_IDLE) 160 (void)printf(", idle"); 161 else if (bpf.bd_state == BPF_WAITING) 162 (void)printf(", waiting"); 163 else if (bpf.bd_state == BPF_TIMED_OUT) 164 (void)printf(", timeout"); 165 oprint(f, "\n"); 166 return 0; 167 } 168 169 static int 170 p_sem(struct file *f) 171 { 172 ksem_t ks; 173 if (!KVM_READ(f->f_data, &ks, sizeof(ks))) { 174 dprintf("can't read sem at %p for pid %d", f->f_data, Pid); 175 return 0; 176 } 177 (void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, " 178 "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters, 179 ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid); 180 if (ks.ks_name && ks.ks_namelen) { 181 char buf[64]; 182 if (ks.ks_namelen >= sizeof(buf)) 183 ks.ks_namelen = sizeof(buf) - 1; 184 if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) { 185 dprintf("can't read sem name at %p for pid %d", 186 ks.ks_name, Pid); 187 } else { 188 buf[ks.ks_namelen] = '\0'; 189 (void)printf(", name=%s", buf); 190 oprint(f, "\n"); 191 return 0; 192 } 193 } 194 oprint(f, "\n"); 195 return 0; 196 } 197 198 static int 199 p_mqueue(struct file *f) 200 { 201 struct mqueue mq; 202 203 if (!KVM_READ(f->f_data, &mq, sizeof(mq))) { 204 dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid); 205 return 0; 206 } 207 (void)printf("* mqueue \"%s\"", mq.mq_name); 208 oprint(f, "\n"); 209 return 0; 210 } 211 212 static int 213 p_kqueue(struct file *f) 214 { 215 struct kqueue kq; 216 217 if (!KVM_READ(f->f_data, &kq, sizeof(kq))) { 218 dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid); 219 return 0; 220 } 221 (void)printf("* kqueue pending %d", kq.kq_count); 222 oprint(f, "\n"); 223 return 0; 224 } 225 226 static int 227 p_audio(struct file *f) 228 { 229 struct audio_file af; 230 const char *devname; 231 const char *modename; 232 233 if (!KVM_READ(f->f_data, &af, sizeof(af))) { 234 dprintf("can't read audio_file at %p for pid %d", 235 f->f_data, Pid); 236 return 0; 237 } 238 239 if (ISDEVAUDIO(af.dev)) { 240 devname = "audio"; 241 } else if (ISDEVSOUND(af.dev)) { 242 devname = "sound"; 243 } else if (ISDEVAUDIOCTL(af.dev)) { 244 devname = "audioctl"; 245 } else if (ISDEVMIXER(af.dev)) { 246 devname = "mixer"; 247 } else { 248 devname = "???"; 249 } 250 251 if (af.ptrack && af.rtrack) { 252 modename = "playback, record"; 253 } else if (af.ptrack) { 254 modename = "playback"; 255 } else if (af.rtrack) { 256 modename = "record"; 257 } else { 258 modename = "-"; 259 } 260 261 (void)printf("* audio@%s%d %s", devname, AUDIOUNIT(af.dev), modename); 262 oprint(f, "\n"); 263 return 0; 264 } 265 266 int 267 pmisc(struct file *f, const char *name) 268 { 269 size_t i; 270 if (nl[0].n_value == 0) { 271 int n; 272 if ((n = KVM_NLIST(nl)) == -1) 273 errx(1, "Cannot list kernel symbols (%s)", 274 KVM_GETERR()); 275 else if (n != 0 && vflg) { 276 char buf[1024]; 277 buf[0] = '\0'; 278 for (struct nlist *l = nl; l->n_name != NULL; l++) { 279 if (l->n_value != 0) 280 continue; 281 strlcat(buf, ", ", sizeof(buf)); 282 strlcat(buf, l->n_name, sizeof(buf)); 283 } 284 warnx("Could not find %d symbols: %s", n, buf + 2); 285 } 286 } 287 for (i = 0; i < NL_MAX; i++) 288 if ((uintptr_t)f->f_ops == nl[i].n_value) 289 break; 290 switch (i) { 291 case NL_BPF: 292 return p_bpf(f); 293 case NL_MQUEUE: 294 return p_mqueue(f); 295 case NL_KQUEUE: 296 return p_kqueue(f); 297 case NL_RND: 298 printf("* random %p", f->f_data); 299 break; 300 case NL_SEM: 301 return p_sem(f); 302 case NL_TAP: 303 printf("* tap %lu", (unsigned long)(intptr_t)f->f_data); 304 break; 305 case NL_CRYPTO: 306 printf("* crypto %p", f->f_data); 307 break; 308 case NL_AUDIO: 309 return p_audio(f); 310 case NL_PAD: 311 printf("* pad %p", f->f_data); 312 break; 313 case NL_MAX: 314 printf("* %s ops=%p %p", name, f->f_ops, f->f_data); 315 break; 316 default: 317 printf("* %s %p", nl[i].n_name, f->f_data); 318 break; 319 } 320 oprint(f, "\n"); 321 return 0; 322 } 323