diff options
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/fetch/fetch.c | 2 | ||||
-rw-r--r-- | usr.bin/kstat/kstat.c | 52 | ||||
-rw-r--r-- | usr.bin/osh/osh.c | 6 | ||||
-rw-r--r-- | usr.bin/sysctl/sysctl.c | 209 |
4 files changed, 243 insertions, 26 deletions
diff --git a/usr.bin/fetch/fetch.c b/usr.bin/fetch/fetch.c index c81ee84..1e8ef92 100644 --- a/usr.bin/fetch/fetch.c +++ b/usr.bin/fetch/fetch.c @@ -33,8 +33,6 @@ #include <stdlib.h> #include <stdio.h> -static const char *user = "unknown"; - #define CPUID(level, a, b, c, d) \ __ASMV("cpuid\n\t" \ : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \ diff --git a/usr.bin/kstat/kstat.c b/usr.bin/kstat/kstat.c index e3b7e6a..cbbe602 100644 --- a/usr.bin/kstat/kstat.c +++ b/usr.bin/kstat/kstat.c @@ -28,10 +28,51 @@ */ #include <sys/sched.h> +#include <sys/vmstat.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> +#define MIB_PER_GIB 1024 + +static void +print_size_mib(const char *name, size_t mib) +{ + if (name == NULL) { + return; + } + + if (mib >= MIB_PER_GIB) { + printf("%s: %d GiB\n", name, mib / MIB_PER_GIB); + } else { + printf("%s: %d MiB\n", name, mib); + } +} + +static void +get_vm_stat(void) +{ + struct vm_stat vmstat; + int retval, fd; + + fd = open("/ctl/vm/stat", O_RDONLY); + if (fd < 0) { + printf("failed to open '/ctl/vm/stat'\n"); + return; + } + + retval = read(fd, &vmstat, sizeof(vmstat)); + if (retval <= 0) { + printf("failed to read vmstat\n"); + return; + } + + close(fd); + print_size_mib("memory available", vmstat.mem_avail); + print_size_mib("memory used", vmstat.mem_used); + print_size_mib("memory total", vmstat.mem_total); +} + static void get_sched_stat(void) { @@ -56,12 +97,10 @@ get_sched_stat(void) nonline = (stat.ncpu - noffline); online_percent = (uint16_t)(((double)nonline / (nonline + noffline)) * 100); - printf("-------------------------------\n"); - printf("Number of tasks: %d\n", stat.nproc); - printf("Number of cores online: %d\n", stat.ncpu); - printf("Scheduler quantum: %d usec\n", stat.quantum_usec); + printf("number of tasks: %d\n", stat.nproc); + printf("number of cores online: %d\n", stat.ncpu); + printf("scheduler quantum: %d usec\n", stat.quantum_usec); printf("CPU is %d%% online\n", online_percent); - printf("-------------------------------\n"); /* * Log out some per-cpu information @@ -75,6 +114,9 @@ get_sched_stat(void) int main(void) { + printf("-- scheduler statistics --\n"); get_sched_stat(); + printf("-- memory statistics --\n"); + get_vm_stat(); return 0; } diff --git a/usr.bin/osh/osh.c b/usr.bin/osh/osh.c index 545f95a..5062e64 100644 --- a/usr.bin/osh/osh.c +++ b/usr.bin/osh/osh.c @@ -67,7 +67,7 @@ "clear - Clear the screen\n" \ "exit - Exit the shell" -#define PROMPT "[%s::osmora]~ " +#define PROMPT "[%s::%s]~ " static char last_command[INPUT_SIZE]; static char buf[INPUT_SIZE]; @@ -466,6 +466,7 @@ main(int argc, char **argv) { int found, prog_argc; int stdout_fd; + char hostname[128] = "osmora"; uint8_t buf_i; char *p; char c; @@ -478,9 +479,10 @@ main(int argc, char **argv) running = 1; bell_fd = open("/dev/beep", O_WRONLY); dump_file("/etc/motd"); + gethostname(hostname, sizeof(hostname)); while (running) { - printf(PROMPT, getlogin()); + printf(PROMPT, getlogin(), hostname); buf_i = getstr(); if (buf[0] == '\0') { diff --git a/usr.bin/sysctl/sysctl.c b/usr.bin/sysctl/sysctl.c index a23bfe8..4a84484 100644 --- a/usr.bin/sysctl/sysctl.c +++ b/usr.bin/sysctl/sysctl.c @@ -31,14 +31,56 @@ #include <stdint.h> #include <stdio.h> #include <string.h> +#include <stdbool.h> +#define BUF_SIZE 128 + +/* Kern var string constants */ #define NAME_OSTYPE "ostype" #define NAME_OSRELEASE "osrelease" #define NAME_VERSION "version" #define NAME_VCACHE_TYPE "vcache_type" +/* Hw var string constants */ +#define NAME_PAGESIZE "pagesize" +#define NAME_NCPU "ncpu" +#define NAME_MACHINE "machine" + +/* Proc var string constants */ +#define NAME_COUNT "count" + +/* Name start string constants */ +#define NAME_KERN "kern" +#define NAME_HW "hw" +#define NAME_PROC "proc" + +/* Name start int constants */ +#define NAME_DEF_KERN 0 +#define NAME_DEF_HW 1 +#define NAME_DEF_PROC 2 + /* - * Convert string name to a sysctl name + * Print the contents read from a sysctl + * variable depending on its type. + * + * @data: Data to print + * @is_str: True if a string + */ +static inline void +varbuf_print(char data[BUF_SIZE], bool is_str) +{ + uint32_t *val; + + if (is_str) { + printf("%s\n", data); + } else { + val = (uint32_t *)data; + printf("%d\n", *val); + } +} + +/* + * Convert string name to a internal name * definition. * * @name: Name to convert @@ -46,7 +88,7 @@ * Convert to int def * / * kern.ostype - * ^^ name + * ^^ * * -- * Returns a less than zero value on failure @@ -56,26 +98,158 @@ static int name_to_def(const char *name) { switch (*name) { + case 'k': + if (strcmp(name, NAME_KERN) == 0) { + return NAME_DEF_KERN; + } + + return -1; + case 'h': + if (strcmp(name, NAME_HW) == 0) { + return NAME_DEF_HW; + } + + return -1; + case 'p': + if (strcmp(name, NAME_PROC) == 0) { + return NAME_DEF_PROC; + } + + return -1; + } + + return -1; +} + +/* + * Handle parsing of 'kern.*' node names + * + * @node: Node name to parse + * @is_str: Set to true if string + */ +static int +kern_node(const char *node, bool *is_str) +{ + switch (*node) { case 'v': - if (strcmp(name, NAME_VERSION) == 0) { + if (strcmp(node, NAME_VERSION) == 0) { return KERN_VERSION; } - if (strcmp(name, NAME_VCACHE_TYPE) == 0) { + if (strcmp(node, NAME_VCACHE_TYPE) == 0) { return KERN_VCACHE_TYPE; } - return -1; case 'o': - if (strcmp(name, NAME_OSTYPE) == 0) { + if (strcmp(node, NAME_OSTYPE) == 0) { return KERN_OSTYPE; } - if (strcmp(name, NAME_OSRELEASE) == 0) { + if (strcmp(node, NAME_OSRELEASE) == 0) { return KERN_OSRELEASE; } + return -1; + } + + return -1; +} + +/* + * Handle parsing of 'hw.*' node names + * + * @node: Node name to parse + * @is_str: Set to true if string + */ +static int +hw_node(const char *node, bool *is_str) +{ + switch (*node) { + case 'p': + if (strcmp(node, NAME_PAGESIZE) == 0) { + *is_str = false; + return HW_PAGESIZE; + } return -1; + case 'n': + if (strcmp(node, NAME_NCPU) == 0) { + *is_str = false; + return HW_NCPU; + } + + return -1; + case 'm': + if (strcmp(node, NAME_MACHINE) == 0) { + return HW_MACHINE; + } + return -1; + } + + return -1; +} + +/* + * Handle parsing of 'proc.*' node names + * + * @node: Node name to parse + * @is_str: Set to true if string + */ +static int +proc_node(const char *node, bool *is_str) +{ + switch (*node) { + case 'c': + if (strcmp(node, NAME_COUNT) == 0) { + *is_str = false; + return PROC_COUNT; + } + + return -1; + } + + return -1; +} + +/* + * Convert string node to a sysctl name + * definition. + * + * @name: Name to convert + * @is_str: Set to true if string + * + * Convert to int def + * / + * kern.ostype + * ^^ name + * + * -- + * Returns a less than zero value on failure + * (e.g., entry not found). + */ +static int +node_to_def(int name, const char *node, bool *is_str) +{ + int retval; + bool dmmy; + + /* + * If the caller did not set `is_str' just + * set it to a dummy value. Otherwise, we will + * make it *default* to a 'true' value. + */ + if (is_str == NULL) { + is_str = &dmmy; + } else { + *is_str = true; + } + + switch (name) { + case NAME_DEF_KERN: + return kern_node(node, is_str); + case NAME_DEF_HW: + return hw_node(node, is_str); + case NAME_DEF_PROC: + return proc_node(node, is_str); } return -1; @@ -86,9 +260,11 @@ main(int argc, char **argv) { struct sysctl_args args; char *var, *p; - int type, name, error; + int type, error; + int root, name; size_t oldlen; - char buf[128]; + bool is_str; + char buf[BUF_SIZE]; if (argc < 2) { printf("sysctl: usage: sysctl <var>\n"); @@ -99,14 +275,13 @@ main(int argc, char **argv) p = strtok(var, "."); if (p == NULL) { - printf("sysctl: bad var\n"); + printf("sysctl: bad var \"%s\"\n", p); return -1; } - /* TODO: Non kern.* vars */ - if (strcmp(p, "kern") != 0) { - printf("unknown var \"%s\"\n", p); - return -1; + if ((root = name_to_def(p)) < 0) { + printf("sysctl: bad var \"%s\"\n", p); + return root; } p = strtok(NULL, "."); @@ -115,12 +290,12 @@ main(int argc, char **argv) return -1; } - if ((name = name_to_def(p)) < 0) { + if ((name = node_to_def(root, p, &is_str)) < 0) { printf("sysctl: bad var \"%s\"\n", p); return name; } - name = name; + memset(buf, 0, sizeof(buf)); oldlen = sizeof(buf); args.name = &name; args.nlen = 1; @@ -134,6 +309,6 @@ main(int argc, char **argv) return error; } - printf("%s\n", buf); + varbuf_print(buf, is_str); return 0; } |