Changeset View
Changeset View
Standalone View
Standalone View
sbin/route/route.c
| Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | |||||
| #include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
| __FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
| #include <sys/param.h> | #include <sys/param.h> | ||||
| #include <sys/file.h> | #include <sys/file.h> | ||||
| #include <sys/socket.h> | #include <sys/socket.h> | ||||
| #include <sys/ioctl.h> | #include <sys/ioctl.h> | ||||
| #ifdef JAIL | |||||
| #include <sys/jail.h> | |||||
| #endif | |||||
| #include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
| #include <sys/types.h> | #include <sys/types.h> | ||||
| #include <sys/queue.h> | #include <sys/queue.h> | ||||
| #include <net/if.h> | #include <net/if.h> | ||||
| #include <net/route.h> | #include <net/route.h> | ||||
| #include <net/if_dl.h> | #include <net/if_dl.h> | ||||
| #include <netinet/in.h> | #include <netinet/in.h> | ||||
| #include <netinet/if_ether.h> | #include <netinet/if_ether.h> | ||||
| #include <arpa/inet.h> | #include <arpa/inet.h> | ||||
| #include <netdb.h> | #include <netdb.h> | ||||
| #include <ctype.h> | #include <ctype.h> | ||||
| #include <err.h> | #include <err.h> | ||||
| #include <errno.h> | #include <errno.h> | ||||
| #ifdef JAIL | |||||
| #include <jail.h> | |||||
| #endif | |||||
| #include <paths.h> | #include <paths.h> | ||||
| #include <signal.h> | #include <signal.h> | ||||
| #include <stdbool.h> | #include <stdbool.h> | ||||
| #include <stdio.h> | #include <stdio.h> | ||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <string.h> | #include <string.h> | ||||
| #include <sysexits.h> | #include <sysexits.h> | ||||
| #include <time.h> | #include <time.h> | ||||
| Show All 12 Lines | static struct keytab { | ||||
| const char *kt_cp; | const char *kt_cp; | ||||
| int kt_i; | int kt_i; | ||||
| } const keywords[] = { | } const keywords[] = { | ||||
| #include "keywords.h" | #include "keywords.h" | ||||
| {0, 0} | {0, 0} | ||||
| }; | }; | ||||
| int verbose, debugonly; | int verbose, debugonly; | ||||
| #ifdef JAIL | |||||
| int usejail; | |||||
| #endif | |||||
| static struct sockaddr_storage so[RTAX_MAX]; | static struct sockaddr_storage so[RTAX_MAX]; | ||||
| static int pid, rtm_addrs; | static int pid, rtm_addrs; | ||||
| static int nflag, af, aflen, qflag, tflag; | static int nflag, af, aflen, qflag, tflag; | ||||
| static int locking, lockrest; | static int locking, lockrest; | ||||
| static struct rt_metrics rt_metrics; | static struct rt_metrics rt_metrics; | ||||
| static u_long rtm_inits; | static u_long rtm_inits; | ||||
| static uid_t uid; | static uid_t uid; | ||||
| static int defaultfib; | static int defaultfib; | ||||
| ▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | stopit(int sig __unused) | ||||
| stop_read = 1; | stop_read = 1; | ||||
| } | } | ||||
| static void | static void | ||||
| usage(const char *cp) | usage(const char *cp) | ||||
| { | { | ||||
| if (cp != NULL) | if (cp != NULL) | ||||
| warnx("bad keyword: %s", cp); | warnx("bad keyword: %s", cp); | ||||
| errx(EX_USAGE, "usage: route [-46dnqtv] command [[modifiers] args]"); | errx(EX_USAGE, "usage: route [-j jail] [-46dnqtv] command [[modifiers] args]"); | ||||
| /* NOTREACHED */ | /* NOTREACHED */ | ||||
| } | } | ||||
| int | int | ||||
| main(int argc, char **argv) | main(int argc, char **argv) | ||||
| { | { | ||||
| int ch; | int ch; | ||||
| #ifdef JAIL | |||||
| int jid; | |||||
| char *jail_name; | |||||
| #endif | |||||
| size_t len; | size_t len; | ||||
| if (argc < 2) | if (argc < 2) | ||||
| usage(NULL); | usage(NULL); | ||||
| while ((ch = getopt(argc, argv, "46nqdtv")) != -1) | while ((ch = getopt(argc, argv, "46nqdtvj:")) != -1) | ||||
| switch(ch) { | switch(ch) { | ||||
| case '4': | case '4': | ||||
| #ifdef INET | #ifdef INET | ||||
| af = AF_INET; | af = AF_INET; | ||||
| aflen = sizeof(struct sockaddr_in); | aflen = sizeof(struct sockaddr_in); | ||||
| #else | #else | ||||
| errx(1, "IPv4 support is not compiled in"); | errx(1, "IPv4 support is not compiled in"); | ||||
| #endif | #endif | ||||
| Show All 16 Lines | case 'v': | ||||
| verbose = 1; | verbose = 1; | ||||
| break; | break; | ||||
| case 't': | case 't': | ||||
| tflag = 1; | tflag = 1; | ||||
| break; | break; | ||||
| case 'd': | case 'd': | ||||
| debugonly = 1; | debugonly = 1; | ||||
| break; | break; | ||||
| case 'j': | |||||
| #ifdef JAIL | |||||
| if (optarg == NULL) | |||||
| usage(NULL); | |||||
| jail_name = optarg; | |||||
melifaro: I'd rather save the parameter here and explicitly call some jail_attach wrapper later. This… | |||||
| usejail = 1; | |||||
melifaroUnsubmitted Done Inline ActionsDo we need usejail at all? melifaro: Do we need usejail at all? | |||||
crest_freebsd_rlwinm.deUnsubmitted Done Inline ActionsIt reads to me like initialising jail_name to NULL and replacing if (usejail) with if (jail_name != NULL) would make the usejail variable superfluous. Splitting a pointer and its validity into two variables like this (jail_name and usejail) introduces the potential for contradiction if only one of the two is updated. If this split is required for something I would like to see them stored together in a struct { char *, bool } instead of destructured into two variables. crest_freebsd_rlwinm.de: It reads to me like initialising `jail_name` to NULL and replacing `if (usejail)` with `if… | |||||
| #else | |||||
| errx(1, "Jail support is not compiled in"); | |||||
| #endif | |||||
| break; | |||||
| case '?': | case '?': | ||||
| default: | default: | ||||
| usage(NULL); | usage(NULL); | ||||
| } | } | ||||
| argc -= optind; | argc -= optind; | ||||
| argv += optind; | argv += optind; | ||||
| pid = getpid(); | pid = getpid(); | ||||
| uid = geteuid(); | uid = geteuid(); | ||||
| #ifdef JAIL | |||||
| if (usejail) { | |||||
| jid = jail_getid(jail_name); | |||||
| if (jid == -1) | |||||
| errx(1, "Jail not found"); | |||||
| if (jail_attach(jid) != 0) | |||||
| errx(1, "Cannot attach to jail"); | |||||
| } | |||||
| #endif | |||||
| #ifdef WITHOUT_NETLINK | #ifdef WITHOUT_NETLINK | ||||
| if (tflag) | if (tflag) | ||||
| s = open(_PATH_DEVNULL, O_WRONLY, 0); | s = open(_PATH_DEVNULL, O_WRONLY, 0); | ||||
| else | else | ||||
| s = socket(PF_ROUTE, SOCK_RAW, 0); | s = socket(PF_ROUTE, SOCK_RAW, 0); | ||||
| if (s < 0) | if (s < 0) | ||||
| err(EX_OSERR, "socket"); | err(EX_OSERR, "socket"); | ||||
| #endif | #endif | ||||
| ▲ Show 20 Lines • Show All 1,752 Lines • Show Last 20 Lines | |||||
I'd rather save the parameter here and explicitly call some jail_attach wrapper later. This will help keep argument parsing and business logic split, hopefully maintaining the same level of readability