xref: /freebsd/usr.sbin/rtprio/rtprio.c (revision 8df8b2d3e51d1b816201d8a1fe8bc29fe192e562)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1994 David Greenman
5  * Copyright (c) 1994 Henrik Vestergaard Draboel (hvd@terry.ping.dk)
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Henrik Vestergaard Draboel.
19  *	This product includes software developed by David Greenman.
20  * 4. Neither the names of the authors nor the names of contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/rtprio.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 static int parseint(const char *, const char *);
52 static void usage(void);
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	struct rtprio rtp;
58 	const char *progname;
59 	pid_t proc = 0;
60 
61 	progname = getprogname();
62 
63 	if (strcmp(progname, "rtprio") == 0)
64 		rtp.type = RTP_PRIO_REALTIME;
65 	else if (strcmp(progname, "idprio") == 0)
66 		rtp.type = RTP_PRIO_IDLE;
67 	else
68 		errx(1, "invalid progname");
69 
70 	switch (argc) {
71 	case 2:
72 		proc = parseint(argv[1], "pid");
73 		proc = abs(proc);
74 		/* FALLTHROUGH */
75 	case 1:
76 		if (rtprio(RTP_LOOKUP, proc, &rtp) != 0)
77 			err(1, "RTP_LOOKUP");
78 		switch (rtp.type) {
79 		case RTP_PRIO_REALTIME:
80 		case RTP_PRIO_FIFO:
81 			warnx("realtime priority %d", rtp.prio);
82 			break;
83 		case RTP_PRIO_NORMAL:
84 			warnx("normal priority");
85 			break;
86 		case RTP_PRIO_IDLE:
87 			warnx("idle priority %d", rtp.prio);
88 			break;
89 		default:
90 			errx(1, "invalid priority type %d", rtp.type);
91 			break;
92 		}
93 		exit(0);
94 	default:
95 		if (argv[1][0] == '-' || isdigit(argv[1][0])) {
96 			if (argv[1][0] == '-') {
97 				if (strcmp(argv[1], "-t") == 0) {
98 					rtp.type = RTP_PRIO_NORMAL;
99 					rtp.prio = 0;
100 				} else {
101 					usage();
102 					break;
103 				}
104 			} else
105 				rtp.prio = parseint(argv[1], "priority");
106 		} else {
107 			usage();
108 			break;
109 		}
110 
111 		if (argv[2][0] == '-') {
112 			proc = parseint(argv[2], "pid");
113 			proc = abs(proc);
114 		}
115 
116 		if (rtprio(RTP_SET, proc, &rtp) != 0)
117 			err(1, "RTP_SET");
118 
119 		if (proc == 0) {
120 			execvp(argv[2], &argv[2]);
121 			err(1, "execvp: %s", argv[2]);
122 		}
123 		exit(0);
124 	}
125 	/* NOTREACHED */
126 }
127 
128 static int
129 parseint(const char *str, const char *errname)
130 {
131 	char *endp;
132 	long res;
133 
134 	errno = 0;
135 	res = strtol(str, &endp, 10);
136 	if (errno != 0 || endp == str || *endp != '\0')
137 		errx(1, "%s must be a number", errname);
138 	if (res >= INT_MAX)
139 		errx(1, "Integer overflow parsing %s", errname);
140 	return (res);
141 }
142 
143 static void
144 usage(void)
145 {
146 
147 	(void) fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
148 		"usage: [id|rt]prio",
149 		"       [id|rt]prio [-]pid",
150 		"       [id|rt]prio priority command [args]",
151 		"       [id|rt]prio priority -pid",
152 		"       [id|rt]prio -t command [args]",
153 		"       [id|rt]prio -t -pid");
154 	exit(1);
155 }
156