From 278c4e670b430ae79c054a05fbec9b4d70f1747f Mon Sep 17 00:00:00 2001 From: "Samuel J. Greear" Date: Fri, 10 Dec 2010 04:58:22 +0000 Subject: [PATCH] vkernel - Add usage() * Adds a new argument -h, which prints extended usage information * Prints typical usage information (same as manpage) when no arguments are specified or an invalid argument is given. Otherwise, error output is the same as before. * Convert to sysexits(3) Sponsored-By: Google Code-In --- share/man/man7/vkernel.7 | 4 +- sys/platform/vkernel/platform/init.c | 69 ++++++++++++++++++++------ sys/platform/vkernel64/platform/init.c | 69 ++++++++++++++++++++------ 3 files changed, 111 insertions(+), 31 deletions(-) diff --git a/share/man/man7/vkernel.7 b/share/man/man7/vkernel.7 index 16bd63c5ee..bfabca01d6 100644 --- a/share/man/man7/vkernel.7 +++ b/share/man/man7/vkernel.7 @@ -46,7 +46,7 @@ .Cd "device vke" .Pp .Pa /var/vkernel/boot/kernel -.Op Fl sUv +.Op Fl hsUv .Op Fl c Ar file .Op Fl e Ar name Ns = Ns Li value : Ns Ar name Ns = Ns Li value : Ns ... .Op Fl i Ar file @@ -83,6 +83,8 @@ option specified on the command line will be the boot disk. The CD9660 filesystem is assumed when booting from this media. .It Fl e Ar name Ns = Ns Li value : Ns Ar name Ns = Ns Li value : Ns ... Specify an environment to be used by the kernel. +.It Fl h +Shows a list of available options, each with a short description. .It Fl i Ar file Specify a memory image .Ar file diff --git a/sys/platform/vkernel/platform/init.c b/sys/platform/vkernel/platform/init.c index 3f37effb87..ea500eca9e 100644 --- a/sys/platform/vkernel/platform/init.c +++ b/sys/platform/vkernel/platform/init.c @@ -70,12 +70,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include vm_paddr_t phys_avail[16]; vm_paddr_t Maxmem; @@ -124,10 +126,11 @@ static void init_globaldata(void); static void init_vkernel(void); static void init_disk(char *diskExp[], int diskFileNum, enum vkdisk_type type); static void init_netif(char *netifExp[], int netifFileNum); -static void writepid( void ); -static void cleanpid( void ); +static void writepid(void); +static void cleanpid(void); static int unix_connect(const char *path); -static void usage(const char *ctl, ...); +static void usage_err(const char *ctl, ...); +static void usage_help(_Bool); static int save_ac; static char **save_av; @@ -182,7 +185,10 @@ main(int ac, char **av) vsize = sizeof(real_ncpus); sysctlbyname("hw.ncpu", &real_ncpus, &vsize, NULL, 0); - while ((c = getopt(ac, av, "c:svl:m:n:r:e:i:p:I:U")) != -1) { + if (ac < 2) + usage_help(false); + + while ((c = getopt(ac, av, "c:hsvl:m:n:r:e:i:p:I:U")) != -1) { switch(c) { case 'e': /* @@ -252,7 +258,7 @@ main(int ac, char **av) break; default: Maxmem_bytes = 0; - usage("Bad maxmem option"); + usage_err("Bad maxmem option"); /* NOT REACHED */ break; } @@ -265,21 +271,21 @@ main(int ac, char **av) if (optarg[3] == ',') { next_cpu = strtol(optarg+4, &endp, 0); if (*endp != '\0') - usage("Bad target CPU number at '%s'", endp); + usage_err("Bad target CPU number at '%s'", endp); } else { next_cpu = 0; } if (next_cpu < 0 || next_cpu > real_ncpus - 1) - usage("Bad target CPU, valid range is 0-%d", real_ncpus - 1); + usage_err("Bad target CPU, valid range is 0-%d", real_ncpus - 1); } else if (strncmp("any", optarg, 3) == 0) { lwp_cpu_lock = LCL_NONE; } else { lwp_cpu_lock = LCL_SINGLE_CPU; next_cpu = strtol(optarg, &endp, 0); if (*endp != '\0') - usage("Bad target CPU number at '%s'", endp); + usage_err("Bad target CPU number at '%s'", endp); if (next_cpu < 0 || next_cpu > real_ncpus - 1) - usage("Bad target CPU, valid range is 0-%d", real_ncpus - 1); + usage_err("Bad target CPU, valid range is 0-%d", real_ncpus - 1); } break; case 'n': @@ -290,10 +296,10 @@ main(int ac, char **av) #ifdef SMP optcpus = strtol(optarg, NULL, 0); if (optcpus < 1 || optcpus > MAXCPU) - usage("Bad ncpus, valid range is 1-%d", MAXCPU); + usage_err("Bad ncpus, valid range is 1-%d", MAXCPU); #else if (strtol(optarg, NULL, 0) != 1) { - usage("You built a UP vkernel, only 1 cpu!"); + usage_err("You built a UP vkernel, only 1 cpu!"); } #endif @@ -304,6 +310,12 @@ main(int ac, char **av) case 'U': kernel_mem_readonly = 0; break; + case 'h': + usage_help(true); + break; + case '?': + default: + usage_help(false); } } @@ -350,7 +362,7 @@ main(int ac, char **av) init_exceptions(); mi_startup(); /* NOT REACHED */ - exit(1); + exit(EX_SOFTWARE); } /* @@ -1235,7 +1247,7 @@ cleanpid( void ) static void -usage(const char *ctl, ...) +usage_err(const char *ctl, ...) { va_list va; @@ -1243,7 +1255,34 @@ usage(const char *ctl, ...) vfprintf(stderr, ctl, va); va_end(va); fprintf(stderr, "\n"); - exit(1); + exit(EX_USAGE); +} + +static +void +usage_help(_Bool help) +{ + fprintf(stderr, "Usage: %s [-hsUv] [-c file] [-e name=value:name=value:...]\n" + "\t[-i file] [-I interface[:address1[:address2][/netmask]]] [-l cpulock]\n" + "\t[-m size] [-n numcpus] [-p file] [-r file]\n", save_av[0]); + + if (help) + fprintf(stderr, "\nArguments:\n" + "\t-c\tSpecify a readonly CD-ROM image file to be used by the kernel.\n" + "\t-e\tSpecify an environment to be used by the kernel.\n" + "\t-h\tThis list of options.\n" + "\t-i\tSpecify a memory image file to be used by the virtual kernel.\n" + "\t-I\tCreate a virtual network device.\n" + "\t-l\tSpecify which, if any, real CPUs to lock virtual CPUs to.\n" + "\t-m\tSpecify the amount of memory to be used by the kernel in bytes.\n" + "\t-n\tSpecify the number of CPUs you wish to emulate.\n" + "\t-p\tSpecify a file in which to store the process ID.\n" + "\t-r\tSpecify a R/W disk image file to be used by the kernel.\n" + "\t-s\tBoot into single-user mode.\n" + "\t-U\tEnable writing to kernel memory and module loading.\n" + "\t-v\tTurn on verbose booting.\n"); + + exit(EX_USAGE); } void @@ -1260,7 +1299,7 @@ cpu_halt(void) { kprintf("cpu halt, exiting vkernel\n"); cleanpid(); - exit(0); + exit(EX_OK); } void diff --git a/sys/platform/vkernel64/platform/init.c b/sys/platform/vkernel64/platform/init.c index 4bf17e24df..bcd8d649e2 100644 --- a/sys/platform/vkernel64/platform/init.c +++ b/sys/platform/vkernel64/platform/init.c @@ -70,12 +70,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include vm_paddr_t phys_avail[16]; vm_paddr_t Maxmem; @@ -122,10 +124,11 @@ static void init_globaldata(void); static void init_vkernel(void); static void init_disk(char *diskExp[], int diskFileNum, enum vkdisk_type type); static void init_netif(char *netifExp[], int netifFileNum); -static void writepid( void ); -static void cleanpid( void ); +static void writepid(void); +static void cleanpid(void); static int unix_connect(const char *path); -static void usage(const char *ctl, ...); +static void usage_err(const char *ctl, ...); +static void usage_help(_Bool); static int save_ac; static char **save_av; @@ -180,7 +183,10 @@ main(int ac, char **av) vsize = sizeof(real_ncpus); sysctlbyname("hw.ncpu", &real_ncpus, &vsize, NULL, 0); - while ((c = getopt(ac, av, "c:svl:m:n:r:e:i:p:I:U")) != -1) { + if (ac < 2) + usage_help(false); + + while ((c = getopt(ac, av, "c:hsvl:m:n:r:e:i:p:I:U")) != -1) { switch(c) { case 'e': /* @@ -250,7 +256,7 @@ main(int ac, char **av) break; default: Maxmem_bytes = 0; - usage("Bad maxmem option"); + usage_err("Bad maxmem option"); /* NOT REACHED */ break; } @@ -263,21 +269,21 @@ main(int ac, char **av) if (optarg[3] == ',') { next_cpu = strtol(optarg+4, &endp, 0); if (*endp != '\0') - usage("Bad target CPU number at '%s'", endp); + usage_err("Bad target CPU number at '%s'", endp); } else { next_cpu = 0; } if (next_cpu < 0 || next_cpu > real_ncpus - 1) - usage("Bad target CPU, valid range is 0-%d", real_ncpus - 1); + usage_err("Bad target CPU, valid range is 0-%d", real_ncpus - 1); } else if (strncmp("any", optarg, 3) == 0) { lwp_cpu_lock = LCL_NONE; } else { lwp_cpu_lock = LCL_SINGLE_CPU; next_cpu = strtol(optarg, &endp, 0); if (*endp != '\0') - usage("Bad target CPU number at '%s'", endp); + usage_err("Bad target CPU number at '%s'", endp); if (next_cpu < 0 || next_cpu > real_ncpus - 1) - usage("Bad target CPU, valid range is 0-%d", real_ncpus - 1); + usage_err("Bad target CPU, valid range is 0-%d", real_ncpus - 1); } break; case 'n': @@ -288,10 +294,10 @@ main(int ac, char **av) #ifdef SMP optcpus = strtol(optarg, NULL, 0); if (optcpus < 1 || optcpus > MAXCPU) - usage("Bad ncpus, valid range is 1-%d", MAXCPU); + usage_err("Bad ncpus, valid range is 1-%d", MAXCPU); #else if (strtol(optarg, NULL, 0) != 1) { - usage("You built a UP vkernel, only 1 cpu!"); + usage_err("You built a UP vkernel, only 1 cpu!"); } #endif @@ -302,6 +308,12 @@ main(int ac, char **av) case 'U': kernel_mem_readonly = 0; break; + case 'h': + usage_help(true); + break; + case '?': + default: + usage_help(false); } } @@ -348,7 +360,7 @@ main(int ac, char **av) init_exceptions(); mi_startup(); /* NOT REACHED */ - exit(1); + exit(EX_SOFTWARE); } /* @@ -1198,7 +1210,7 @@ cleanpid( void ) static void -usage(const char *ctl, ...) +usage_err(const char *ctl, ...) { va_list va; @@ -1206,7 +1218,34 @@ usage(const char *ctl, ...) vfprintf(stderr, ctl, va); va_end(va); fprintf(stderr, "\n"); - exit(1); + exit(EX_USAGE); +} + +static +void +usage_help(_Bool help) +{ + fprintf(stderr, "Usage: %s [-hsUv] [-c file] [-e name=value:name=value:...]\n" + "\t[-i file] [-I interface[:address1[:address2][/netmask]]] [-l cpulock]\n" + "\t[-m size] [-n numcpus] [-p file] [-r file]\n", save_av[0]); + + if (help) + fprintf(stderr, "\nArguments:\n" + "\t-c\tSpecify a readonly CD-ROM image file to be used by the kernel.\n" + "\t-e\tSpecify an environment to be used by the kernel.\n" + "\t-h\tThis list of options.\n" + "\t-i\tSpecify a memory image file to be used by the virtual kernel.\n" + "\t-I\tCreate a virtual network device.\n" + "\t-l\tSpecify which, if any, real CPUs to lock virtual CPUs to.\n" + "\t-m\tSpecify the amount of memory to be used by the kernel in bytes.\n" + "\t-n\tSpecify the number of CPUs you wish to emulate.\n" + "\t-p\tSpecify a file in which to store the process ID.\n" + "\t-r\tSpecify a R/W disk image file to be used by the kernel.\n" + "\t-s\tBoot into single-user mode.\n" + "\t-U\tEnable writing to kernel memory and module loading.\n" + "\t-v\tTurn on verbose booting.\n"); + + exit(EX_USAGE); } void @@ -1223,7 +1262,7 @@ cpu_halt(void) { kprintf("cpu halt, exiting vkernel\n"); cleanpid(); - exit(0); + exit(EX_OK); } void -- 2.41.0