12447Sdg/*
23291Sdg * Copyright (c) 1994 David Greenman
33291Sdg * Copyright (c) 1994 Henrik Vestergaard Draboel (hvd@terry.ping.dk)
42447Sdg * All rights reserved.
52447Sdg *
62447Sdg * Redistribution and use in source and binary forms, with or without
72447Sdg * modification, are permitted provided that the following conditions
82447Sdg * are met:
92447Sdg * 1. Redistributions of source code must retain the above copyright
102447Sdg *    notice, this list of conditions and the following disclaimer.
112447Sdg * 2. Redistributions in binary form must reproduce the above copyright
122447Sdg *    notice, this list of conditions and the following disclaimer in the
132447Sdg *    documentation and/or other materials provided with the distribution.
142447Sdg * 3. All advertising materials mentioning features or use of this software
152447Sdg *    must display the following acknowledgement:
163291Sdg *	This product includes software developed by Henrik Vestergaard Draboel.
173291Sdg *	This product includes software developed by David Greenman.
183291Sdg * 4. Neither the names of the authors nor the names of contributors
192447Sdg *    may be used to endorse or promote products derived from this software
202447Sdg *    without specific prior written permission.
212447Sdg *
223291Sdg * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
232447Sdg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242447Sdg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253291Sdg * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
262447Sdg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272447Sdg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282447Sdg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292447Sdg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302447Sdg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312447Sdg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322447Sdg * SUCH DAMAGE.
332447Sdg */
342447Sdg
35117280Scharnier#include <sys/cdefs.h>
36117280Scharnier__FBSDID("$FreeBSD$");
3730379Scharnier
383291Sdg#include <sys/param.h>
393291Sdg#include <sys/rtprio.h>
402447Sdg
4130379Scharnier#include <ctype.h>
4230379Scharnier#include <err.h>
43216955Skib#include <errno.h>
442447Sdg#include <stdio.h>
452447Sdg#include <stdlib.h>
462447Sdg#include <string.h>
472447Sdg#include <unistd.h>
482447Sdg
49216955Skibstatic int parseint(const char *, const char *);
50216955Skibstatic void usage(void);
512447Sdg
522447Sdgint
53216955Skibmain(int argc, char *argv[])
542447Sdg{
553291Sdg	struct rtprio rtp;
56235577Sdelphij	const char *progname;
57235577Sdelphij	pid_t proc = 0;
582447Sdg
59235577Sdelphij	progname = getprogname();
602447Sdg
61235577Sdelphij	if (strcmp(progname, "rtprio") == 0)
623291Sdg		rtp.type = RTP_PRIO_REALTIME;
63235577Sdelphij	else if (strcmp(progname, "idprio") == 0)
643291Sdg		rtp.type = RTP_PRIO_IDLE;
65235577Sdelphij	else
66235577Sdelphij		errx(1, "invalid progname");
673291Sdg
682447Sdg	switch (argc) {
693291Sdg	case 2:
70216955Skib		proc = parseint(argv[1], "pid");
71216955Skib		proc = abs(proc);
722447Sdg		/* FALLTHROUGH */
732447Sdg	case 1:
7430379Scharnier		if (rtprio(RTP_LOOKUP, proc, &rtp) != 0)
75216955Skib			err(1, "RTP_LOOKUP");
763291Sdg		switch (rtp.type) {
773291Sdg		case RTP_PRIO_REALTIME:
7836213Sdufault		case RTP_PRIO_FIFO:
79235577Sdelphij			warnx("realtime priority %d", rtp.prio);
803291Sdg			break;
813291Sdg		case RTP_PRIO_NORMAL:
82235577Sdelphij			warnx("normal priority");
833291Sdg			break;
843291Sdg		case RTP_PRIO_IDLE:
85235577Sdelphij			warnx("idle priority %d", rtp.prio);
863291Sdg			break;
872447Sdg		default:
88235577Sdelphij			errx(1, "invalid priority type %d", rtp.type);
893291Sdg			break;
902447Sdg		}
913291Sdg		exit(0);
923291Sdg	default:
933291Sdg		if (argv[1][0] == '-' || isdigit(argv[1][0])) {
943291Sdg			if (argv[1][0] == '-') {
953291Sdg				if (strcmp(argv[1], "-t") == 0) {
963291Sdg					rtp.type = RTP_PRIO_NORMAL;
973291Sdg					rtp.prio = 0;
983291Sdg				} else {
9930379Scharnier					usage();
1003291Sdg					break;
1013291Sdg				}
102216955Skib			} else
103216955Skib				rtp.prio = parseint(argv[1], "priority");
1043291Sdg		} else {
10530379Scharnier			usage();
1063291Sdg			break;
1072447Sdg		}
1082447Sdg
109235577Sdelphij		if (argv[2][0] == '-') {
110235577Sdelphij			proc = parseint(argv[2], "pid");
111235577Sdelphij			proc = abs(proc);
112235577Sdelphij		}
113235577Sdelphij
11430379Scharnier		if (rtprio(RTP_SET, proc, &rtp) != 0)
115216955Skib			err(1, "RTP_SET");
1162447Sdg
1173291Sdg		if (proc == 0) {
1183291Sdg			execvp(argv[2], &argv[2]);
119235577Sdelphij			err(1, "execvp: %s", argv[2]);
1203291Sdg		}
12171640Skuriyama		exit(0);
1222447Sdg	}
123235577Sdelphij	/* NOTREACHED */
1242447Sdg}
1252447Sdg
126216955Skibstatic int
127216955Skibparseint(const char *str, const char *errname)
128216955Skib{
129216955Skib	char *endp;
130216955Skib	long res;
131216955Skib
132216955Skib	errno = 0;
133216955Skib	res = strtol(str, &endp, 10);
134216955Skib	if (errno != 0 || endp == str || *endp != '\0')
135216967Skib		errx(1, "%s must be a number", errname);
136216955Skib	if (res >= INT_MAX)
137216967Skib		errx(1, "Integer overflow parsing %s", errname);
138216955Skib	return (res);
139216955Skib}
140216955Skib
1412447Sdgstatic void
142216955Skibusage(void)
1432447Sdg{
144216955Skib
14530379Scharnier	(void) fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
14630379Scharnier		"usage: [id|rt]prio",
14730379Scharnier		"       [id|rt]prio [-]pid",
14830379Scharnier		"       [id|rt]prio priority command [args]",
14930379Scharnier		"       [id|rt]prio priority -pid",
15030379Scharnier		"       [id|rt]prio -t command [args]",
15130379Scharnier		"       [id|rt]prio -t -pid");
15230379Scharnier	exit(1);
1532447Sdg}
154