procfs_ioctl.c revision 87542
1/*- 2 * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD: head/sys/fs/procfs/procfs_ioctl.c 87542 2001-12-09 00:35:30Z des $ 29 */ 30 31#include <sys/param.h> 32#include <sys/lock.h> 33#include <sys/mutex.h> 34#include <sys/pioctl.h> 35#include <sys/proc.h> 36#include <sys/signalvar.h> 37#include <sys/systm.h> 38 39#include <fs/pseudofs/pseudofs.h> 40#include <fs/procfs/procfs.h> 41 42/* 43 * Process ioctls 44 */ 45int 46procfs_ioctl(PFS_IOCTL_ARGS) 47{ 48 struct procfs_status *ps; 49 int error, flags, sig; 50 51 PROC_LOCK(p); 52 error = 0; 53 switch (cmd) { 54 case PIOCBIS: 55 p->p_stops |= *(unsigned int *)data; 56 break; 57 case PIOCBIC: 58 p->p_stops &= ~*(unsigned int *)data; 59 break; 60 case PIOCSFL: 61 flags = *(unsigned int *)data; 62 if (flags & PF_ISUGID && (error = suser(td->td_proc)) != 0) 63 break; 64 p->p_pfsflags = flags; 65 break; 66 case PIOCGFL: 67 *(unsigned int *)data = p->p_pfsflags; 68 break; 69 case PIOCWAIT: 70 while (p->p_step == 0) { 71 /* sleep until p stops */ 72 error = msleep(&p->p_stype, &p->p_mtx, 73 PWAIT|PCATCH, "pioctl", 0); 74 if (error != 0) 75 break; 76 } 77 /* fall through to PIOCSTATUS */ 78 case PIOCSTATUS: 79 ps = (struct procfs_status *)data; 80 ps->state = (p->p_step == 0); 81 ps->flags = 0; /* nope */ 82 ps->events = p->p_stops; 83 ps->why = p->p_step ? p->p_stype : 0; 84 ps->val = p->p_step ? p->p_xstat : 0; 85 break; 86 case PIOCCONT: 87 if (p->p_step == 0) 88 break; 89 sig = *(int *)data; 90 if (sig != 0 && !_SIG_VALID(sig)) { 91 error = EINVAL; 92 break; 93 } 94#if 0 95 mtx_lock_spin(&sched_lock); 96 p->p_step = 0; 97 if (p->p_stat == SSTOP) { 98 p->p_xstat = sig; 99 setrunnable(&p->p_thread); 100 mtx_unlock_spin(&sched_lock); 101 } else { 102 mtx_unlock_spin(&sched_lock); 103 if (sig) 104 psignal(p, sig); 105 } 106#else 107 if (sig) 108 psignal(p, sig); 109 p->p_step = 0; 110 wakeup(&p->p_step); 111#endif 112 break; 113 default: 114 error = (ENOTTY); 115 } 116 PROC_UNLOCK(p); 117 118 return (error); 119} 120 121/* 122 * Clean up on last close 123 */ 124int 125procfs_close(PFS_CLOSE_ARGS) 126{ 127 if (p != NULL && (p->p_pfsflags & PF_LINGER) == 0) { 128 p->p_pfsflags = 0; 129 p->p_stops = 0; 130 p->p_step = 0; 131 wakeup(&p->p_step); 132 } 133 return (0); 134} 135