2 * Copyright (c) 2016 The FreeBSD Foundation
5 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6 * under sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/procctl.h>
48 str2pid(const char *str)
53 res = strtol(str, &tail, 0);
55 warnx("non-numeric pid");
65 fprintf(stderr, "Usage: proccontrol -m (trace|trapcap) [-q] "
66 "[-s (enable|disable)] [-p pid | command]\n");
71 main(int argc, char *argv[])
73 int arg, ch, error, mode;
75 bool enable, do_command, query;
81 while ((ch = getopt(argc, argv, "m:qs:p:")) != -1) {
84 if (strcmp(optarg, "trace") == 0)
86 else if (strcmp(optarg, "trapcap") == 0)
92 if (strcmp(optarg, "enable") == 0)
94 else if (strcmp(optarg, "disable") == 0)
100 pid = str2pid(optarg);
113 do_command = argc != 0;
115 if (pid != -1 || query)
118 } else if (pid == -1) {
125 error = procctl(P_PID, pid, PROC_TRACE_STATUS, &arg);
128 error = procctl(P_PID, pid, PROC_TRAPCAP_STATUS, &arg);
135 err(1, "procctl status");
139 printf("disabled\n");
141 printf("enabled, no debugger\n");
143 printf("enabled, traced by %d\n", arg);
147 case PROC_TRAPCAP_CTL_ENABLE:
150 case PROC_TRAPCAP_CTL_DISABLE:
151 printf("disabled\n");
159 arg = enable ? PROC_TRACE_CTL_ENABLE :
160 PROC_TRACE_CTL_DISABLE;
161 error = procctl(P_PID, pid, PROC_TRACE_CTL, &arg);
164 arg = enable ? PROC_TRAPCAP_CTL_ENABLE :
165 PROC_TRAPCAP_CTL_DISABLE;
166 error = procctl(P_PID, pid, PROC_TRAPCAP_CTL, &arg);
173 err(1, "procctl ctl");
175 error = execvp(argv[0], argv);