1 /* 2 * Copyright 1997 Sean Eric Fagan 3 * Copyright (c) 2004 Liam J. Foy <liamfoy@sepulcrum.org> 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 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Sean Eric Fagan 16 * 4. Neither the name of the author may be used to endorse or promote 17 * products derived from this software without specific prior written 18 * permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD: src/usr.sbin/procctl/procctl.c,v 1.6 2000/02/21 10:22:39 ru Exp $ 33 * $DragonFly: src/usr.sbin/procctl/procctl.c,v 1.3 2004/12/06 21:13:51 liamfoy Exp $ 34 */ 35 36 /* 37 * procctl -- clear the event mask, and continue, any specified processes. 38 * This is largely an example of how to use the procfs interface; however, 39 * for now, it is also sometimes necessary, as a stopped process will not 40 * otherwise continue. (This will be fixed in a later version of the 41 * procfs code, almost certainly; however, this program will still be useful 42 * for some annoying circumstances.) 43 */ 44 45 #include <err.h> 46 #include <errno.h> 47 #include <fcntl.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include <sys/ioctl.h> 53 #include <sys/param.h> 54 #include <sys/pioctl.h> 55 56 static void usage(void); 57 58 int 59 main(int argc, char **argv) 60 { 61 int c, vflag, fd; 62 63 vflag = 0; 64 while ((c = getopt(argc, argv, "v")) != -1) { 65 switch (c) { 66 case 'v': 67 vflag = 1; 68 break; 69 default: 70 usage(); 71 } 72 } 73 74 argc -= optind; 75 argv += optind; 76 77 if (argc == 0) 78 usage(); 79 80 for (; *argv; ++argv) { 81 char buf[MAXPATHLEN]; 82 83 snprintf(buf, sizeof(buf), "/proc/%s/mem", *argv); 84 fd = open(buf, O_RDWR); 85 if (fd == -1) { 86 if (!vflag && errno == ENOENT) 87 continue; 88 warn("cannot open pid %s", *argv); 89 continue; 90 } 91 92 if (ioctl(fd, PIOCBIC, ~0) == -1) 93 warn("cannot clear process %s's event mask", *argv); 94 else if (vflag) 95 printf("successfully cleared process %s\n", *argv); 96 97 if (ioctl(fd, PIOCCONT, 0) == -1 && errno != EINVAL) 98 warn("cannot continue process %s", *argv); 99 else if (vflag) 100 printf("process %s continued\n", *argv); 101 close(fd); 102 } 103 return 0; 104 } 105 106 static void 107 usage(void) 108 { 109 110 fprintf(stderr, "usage: procctl [-v] pid...\n"); 111 exit(EXIT_FAILURE); 112 } 113