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/param.h>
38#include <sys/rtprio.h>
39
40#include <ctype.h>
41#include <err.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48static int parseint(const char *, const char *);
49static void usage(void) __dead2;
50
51int
52main(int argc, char *argv[])
53{
54	struct rtprio rtp;
55	const char *progname;
56	pid_t proc = 0;
57
58	progname = getprogname();
59
60	if (strcmp(progname, "rtprio") == 0)
61		rtp.type = RTP_PRIO_REALTIME;
62	else if (strcmp(progname, "idprio") == 0)
63		rtp.type = RTP_PRIO_IDLE;
64	else
65		errx(1, "invalid progname");
66
67	switch (argc) {
68	case 2:
69		proc = parseint(argv[1], "pid");
70		proc = abs(proc);
71		/* FALLTHROUGH */
72	case 1:
73		if (rtprio(RTP_LOOKUP, proc, &rtp) != 0)
74			err(1, "RTP_LOOKUP");
75		switch (rtp.type) {
76		case RTP_PRIO_REALTIME:
77		case RTP_PRIO_FIFO:
78			warnx("realtime priority %d", rtp.prio);
79			break;
80		case RTP_PRIO_NORMAL:
81			warnx("normal priority");
82			break;
83		case RTP_PRIO_IDLE:
84			warnx("idle priority %d", rtp.prio);
85			break;
86		default:
87			errx(1, "invalid priority type %d", rtp.type);
88			break;
89		}
90		exit(0);
91	default:
92		if (argv[1][0] == '-' || isdigit(argv[1][0])) {
93			if (argv[1][0] == '-') {
94				if (strcmp(argv[1], "-t") == 0) {
95					rtp.type = RTP_PRIO_NORMAL;
96					rtp.prio = 0;
97				} else {
98					usage();
99					break;
100				}
101			} else
102				rtp.prio = parseint(argv[1], "priority");
103		} else {
104			usage();
105			break;
106		}
107
108		if (argv[2][0] == '-') {
109			proc = parseint(argv[2], "pid");
110			proc = abs(proc);
111		}
112
113		if (rtprio(RTP_SET, proc, &rtp) != 0)
114			err(1, "RTP_SET");
115
116		if (proc == 0) {
117			execvp(argv[2], &argv[2]);
118			err(1, "execvp: %s", argv[2]);
119		}
120		exit(0);
121	}
122	/* NOTREACHED */
123}
124
125static int
126parseint(const char *str, const char *errname)
127{
128	char *endp;
129	long res;
130
131	errno = 0;
132	res = strtol(str, &endp, 10);
133	if (errno != 0 || endp == str || *endp != '\0')
134		errx(1, "%s must be a number", errname);
135	if (res >= INT_MAX)
136		errx(1, "Integer overflow parsing %s", errname);
137	return (res);
138}
139
140static void
141usage(void)
142{
143
144	(void) fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
145		"usage: [id|rt]prio",
146		"       [id|rt]prio [-]pid",
147		"       [id|rt]prio priority command [args]",
148		"       [id|rt]prio priority -pid",
149		"       [id|rt]prio -t command [args]",
150		"       [id|rt]prio -t -pid");
151	exit(1);
152}
153