setfib.c revision 180784
1178888Sjulian/*
2178888Sjulian * Copyright (c) 1989, 1993, 1994
3178888Sjulian *	The Regents of the University of California.  All rights reserved.
4178888Sjulian * Copyright (c) 2008 Cisco Systems, All rights reserved
5178888Sjulian *
6178888Sjulian * Redistribution and use in source and binary forms, with or without
7178888Sjulian * modification, are permitted provided that the following conditions
8178888Sjulian * are met:
9178888Sjulian * 1. Redistributions of source code must retain the above copyright
10178888Sjulian *    notice, this list of conditions and the following disclaimer.
11178888Sjulian * 2. Redistributions in binary form must reproduce the above copyright
12178888Sjulian *    notice, this list of conditions and the following disclaimer in the
13178888Sjulian *    documentation and/or other materials provided with the distribution.
14178888Sjulian * 4. Neither the name of the University nor the names of its contributors
15178888Sjulian *    may be used to endorse or promote products derived from this software
16178888Sjulian *    without specific prior written permission.
17178888Sjulian *
18178888Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19178888Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20178888Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21178888Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22178888Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23178888Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24178888Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25178888Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26178888Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27178888Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28178888Sjulian * SUCH DAMAGE.
29178888Sjulian *
30178888Sjulian * setfib file skelaton taken from nice.c
31178888Sjulian */
32178888Sjulian
33178888Sjulian#include <sys/cdefs.h>
34178888Sjulian__FBSDID("$FreeBSD: head/usr.sbin/setfib/setfib.c 180784 2008-07-24 18:01:50Z julian $");
35178888Sjulian
36178888Sjulian
37178888Sjulian#include <errno.h>
38178888Sjulian#include <limits.h>
39178888Sjulian#include <stdio.h>
40178888Sjulian#include <stdlib.h>
41178888Sjulian#include <unistd.h>
42178888Sjulian#include <sys/sysctl.h>
43178888Sjulian
44178888Sjulianvoid usage(void);
45178888Sjulian
46178888Sjulianint
47178888Sjulianmain(int argc, char *argv[])
48178888Sjulian{
49178888Sjulian	long fib = 0;
50178888Sjulian	int ch;
51178888Sjulian	char *ep;
52178888Sjulian	int	numfibs;
53178916Sjulian	size_t intsize = sizeof(int);
54178888Sjulian
55178888Sjulian        if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
56178888Sjulian		errx(1, "Multiple FIBS not supported");
57178888Sjulian	if (argc < 2)
58178888Sjulian		usage();
59178888Sjulian	ep = argv[1];
60178888Sjulian	/*
61178888Sjulian	 * convert -N or N to -FN. (N is a number)
62178888Sjulian	 */
63178888Sjulian	if (ep[0]== '-' && isdigit((unsigned char)ep[1]))
64178888Sjulian		ep++;
65178888Sjulian	if (isdigit((unsigned char)*ep))
66178888Sjulian               if (asprintf(&argv[1], "-F%s", ep) < 0)
67178888Sjulian                        err(1, "asprintf");
68178888Sjulian
69178888Sjulian	while ((ch = getopt(argc, argv, "F:")) != -1) {
70178888Sjulian		switch (ch) {
71178888Sjulian		case 'F':
72178888Sjulian			errno = 0;
73178888Sjulian			fib = strtol(optarg, &ep, 10);
74178888Sjulian			if (ep == optarg || *ep != '\0' || errno ||
75178888Sjulian			    fib < 0 || fib >= numfibs)
76178888Sjulian				errx(1, "%s: invalid FIB (max %s)",
77178888Sjulian				    optarg, numfibs - 1);
78178888Sjulian			break;
79178888Sjulian		default:
80178888Sjulian			usage();
81178888Sjulian		}
82178888Sjulian	}
83178888Sjulian	argc -= optind;
84178888Sjulian	argv += optind;
85178888Sjulian
86178888Sjulian	if (argc == 0)
87178888Sjulian		usage();
88178888Sjulian
89178888Sjulian	errno = 0;
90180784Sjulian	if (setfib((int)fib))
91178888Sjulian		warn("setfib");
92178888Sjulian	execvp(*argv, argv);
93178888Sjulian	err(errno == ENOENT ? 127 : 126, "%s", *argv);
94178888Sjulian}
95178888Sjulian
96178888Sjulianvoid
97178888Sjulianusage(void)
98178888Sjulian{
99178888Sjulian
100178888Sjulian	(void)fprintf(stderr,
101179552Smlaier	    "usage: setfib [-[F]]value command\n");
102178888Sjulian	exit(1);
103178888Sjulian}
104