]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/protect/protect.c
Remove spurious newline
[FreeBSD/FreeBSD.git] / usr.bin / protect / protect.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Hudson River Trading LLC
5  * Written by: John H. Baldwin <jhb@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/procctl.h>
34 #include <sys/types.h>
35 #include <sys/mman.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 static void
44 usage(void)
45 {
46
47         fprintf(stderr, "usage: protect [-i] command\n");
48         fprintf(stderr, "       protect [-cdi] -g pgrp | -p pid\n");
49         exit(1);
50 }
51
52 static id_t
53 parse_id(char *id)
54 {
55         static bool first = true;
56         long value;
57         char *ch;
58
59         if (!first) {
60                 warnx("only one -g or -p flag is permitted");
61                 usage();
62         }
63         value = strtol(id, &ch, 0);
64         if (*ch != '\0') {
65                 warnx("invalid process id");
66                 usage();
67         }
68         return (value);
69 }
70
71 int
72 main(int argc, char *argv[])
73 {
74         idtype_t idtype;
75         id_t id;
76         int ch, flags;
77         bool descend, inherit, idset;
78
79         idtype = P_PID;
80         id = getpid();
81         flags = PPROT_SET;
82         descend = inherit = idset = false;
83         while ((ch = getopt(argc, argv, "cdig:p:")) != -1)
84                 switch (ch) {
85                 case 'c':
86                         flags = PPROT_CLEAR;
87                         break;
88                 case 'd':
89                         descend = true;
90                         break;
91                 case 'i':
92                         inherit = true;
93                         break;
94                 case 'g':
95                         idtype = P_PGID;
96                         id = parse_id(optarg);
97                         idset = true;
98                         break;
99                 case 'p':
100                         idtype = P_PID;
101                         id = parse_id(optarg);
102                         idset = true;
103                         break;
104                 }
105         argc -= optind;
106         argv += optind;
107
108         if ((idset && argc != 0) || (!idset && (argc == 0 || descend)))
109                 usage();
110
111         if (descend)
112                 flags |= PPROT_DESCEND;
113         if (inherit)
114                 flags |= PPROT_INHERIT;
115         if (procctl(idtype, id, PROC_SPROTECT, &flags) == -1)
116                 err(1, "procctl");
117
118         if (argc != 0) {
119                 errno = 0;
120                 execvp(*argv, argv);
121                 err(errno == ENOENT ? 127 : 126, "%s", *argv);
122         }
123         return (0);
124 }