procctl.c revision 302408
1290931Srodrigc/*
2290931Srodrigc * Copyright 1997 Sean Eric Fagan
3290931Srodrigc *
4290931Srodrigc * Redistribution and use in source and binary forms, with or without
5290931Srodrigc * modification, are permitted provided that the following conditions
6290931Srodrigc * are met:
7290931Srodrigc * 1. Redistributions of source code must retain the above copyright
8290931Srodrigc *    notice, this list of conditions and the following disclaimer.
9290931Srodrigc * 2. Redistributions in binary form must reproduce the above copyright
10290931Srodrigc *    notice, this list of conditions and the following disclaimer in the
11290931Srodrigc *    documentation and/or other materials provided with the distribution.
12290931Srodrigc * 3. All advertising materials mentioning features or use of this software
13290931Srodrigc *    must display the following acknowledgement:
14290931Srodrigc *	This product includes software developed by Sean Eric Fagan
15290931Srodrigc * 4. Neither the name of the author may be used to endorse or promote
16290931Srodrigc *    products derived from this software without specific prior written
17290931Srodrigc *    permission.
18290931Srodrigc *
19290931Srodrigc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20290931Srodrigc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21290931Srodrigc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22290931Srodrigc * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23290931Srodrigc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24290931Srodrigc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25290931Srodrigc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26290931Srodrigc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27290931Srodrigc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28290931Srodrigc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29290931Srodrigc * SUCH DAMAGE.
30290931Srodrigc *
31290931Srodrigc */
32290931Srodrigc
33290931Srodrigc#include <sys/cdefs.h>
34290931Srodrigc__FBSDID("$FreeBSD: stable/11/usr.sbin/procctl/procctl.c 114601 2003-05-03 21:06:42Z obrien $");
35290931Srodrigc
36290931Srodrigc/*
37290931Srodrigc * procctl -- clear the event mask, and continue, any specified processes.
38290931Srodrigc * This is largely an example of how to use the procfs interface; however,
39290931Srodrigc * for now, it is also sometimes necessary, as a stopped process will not
40290931Srodrigc * otherwise continue.  (This will be fixed in a later version of the
41290931Srodrigc * procfs code, almost certainly; however, this program will still be useful
42290931Srodrigc * for some annoying circumstances.)
43290931Srodrigc */
44290931Srodrigc
45290931Srodrigc#include <err.h>
46290931Srodrigc#include <errno.h>
47290931Srodrigc#include <fcntl.h>
48290931Srodrigc#include <signal.h>
49290931Srodrigc#include <stdio.h>
50290931Srodrigc#include <stdlib.h>
51290931Srodrigc#include <string.h>
52290931Srodrigc#include <unistd.h>
53290931Srodrigc#include <sys/ioctl.h>
54290931Srodrigc#include <sys/pioctl.h>
55290931Srodrigc
56290931Srodrigcint
57290931Srodrigcmain(int ac, char **av) {
58290931Srodrigc  int fd;
59290931Srodrigc  int i;
60290931Srodrigc
61290931Srodrigc  for (i = 1; i < ac; i++) {
62290931Srodrigc    char buf[32];
63290931Srodrigc
64290931Srodrigc    snprintf(buf, sizeof(buf), "/proc/%s/mem", av[i]);
65290931Srodrigc    fd = open(buf, O_RDWR);
66290931Srodrigc    if (fd == -1) {
67290931Srodrigc      if (errno == ENOENT)
68290931Srodrigc	continue;
69290931Srodrigc      warn("cannot open pid %s", av[i]);
70290931Srodrigc      continue;
71290931Srodrigc    }
72290931Srodrigc    if (ioctl(fd, PIOCBIC, ~0) == -1)
73290931Srodrigc      warn("cannot clear process %s's event mask", av[i]);
74290940Srodrigc    if (ioctl(fd, PIOCCONT, 0) == -1 && errno != EINVAL)
75290931Srodrigc      warn("cannot continue process %s", av[i]);
76290931Srodrigc    close(fd);
77290931Srodrigc  }
78290931Srodrigc  return 0;
79290931Srodrigc}
80290931Srodrigc