|
|||
Prex Home / Browse Source - Prex Version: 0.9.0 |
|||
root/usr/test/kmon/cmd.c/* [<][>][^][v][top][bottom][index][help] */DEFINITIONSThis source file includes following definitions.1 /* 2 * Copyright (c) 2005-2006, Kohsuke Ohtani 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * cmd.c - command processor 32 */ 33 34 #include <sys/prex.h> 35 #include <sys/ioctl.h> 36 #include <sys/types.h> 37 38 #include <unistd.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <errno.h> 42 #include <stdio.h> 43 44 static void cmd_help(int argc, char **argv); 45 static void cmd_ver(int argc, char **argv); 46 static void cmd_mem(int argc, char **argv); 47 static void cmd_clear(int argc, char **argv); 48 static void cmd_kill(int argc, char **argv); 49 static void cmd_reboot(int argc, char **argv); 50 static void cmd_shutdown(int argc, char **argv); 51 52 struct cmd_entry { 53 const char *cmd; 54 void (*func)(int, char **); 55 const char *usage; 56 }; 57 58 static const struct cmd_entry cmd_table[] = { 59 { "ver" ,cmd_ver ,"Version information" }, 60 { "mem" ,cmd_mem ,"Show memory usage" }, 61 { "clear" ,cmd_clear ,"Clear screen" }, 62 { "kill" ,cmd_kill ,"Terminate thread" }, 63 { "reboot" ,cmd_reboot ,"Reboot system" }, 64 { "shutdown" ,cmd_shutdown ,"Shutdown system" }, 65 { "help" ,cmd_help ,"This help" }, 66 { NULL ,NULL ,NULL }, 67 }; 68 69 static void 70 cmd_help(int argc, char **argv) 71 { 72 int i = 0; 73 74 while (cmd_table[i].cmd != NULL) { 75 printf("%s - %s\n", cmd_table[i].cmd, cmd_table[i].usage); 76 i++; 77 } 78 } 79 80 static void 81 cmd_ver(int argc, char **argv) 82 { 83 struct kerninfo info; 84 85 sys_info(INFO_KERNEL, &info); 86 87 printf("Kernel version:\n"); 88 printf("%s version %s for %s\n", 89 info.sysname, info.version, info.machine); 90 } 91 92 static void 93 cmd_mem(int argc, char **argv) 94 { 95 struct meminfo info; 96 97 sys_info(INFO_MEMORY, &info); 98 99 printf("Memory usage:\n"); 100 printf(" Used : %8d KB\n", 101 (u_int)((info.total - info.free) / 1024)); 102 printf(" Free : %8d KB\n", (u_int)(info.free / 1024)); 103 printf(" Total : %8d KB\n", (u_int)(info.total / 1024)); 104 printf(" Bootdisk : %8d KB\n", (u_int)(info.bootdisk / 1024)); 105 } 106 107 static void 108 cmd_clear(int argc, char **argv) 109 { 110 111 printf("\33[2J"); 112 } 113 114 static void 115 cmd_kill(int argc, char **argv) 116 { 117 thread_t t; 118 char *ep; 119 120 if (argc < 2) { 121 puts("Usage: kill thread"); 122 return; 123 } 124 t = (thread_t)strtoul(argv[1], &ep, 16); 125 printf("Kill thread id:%x\n", (u_int)t); 126 127 if (thread_terminate(t)) 128 printf("Thread %x does not exist\n", (u_int)t); 129 } 130 131 static void 132 cmd_reboot(int argc, char **argv) 133 { 134 device_t pm_dev; 135 int error, state = PWR_REBOOT; 136 137 if ((error = device_open("pm", 0, &pm_dev)) == 0) { 138 error = device_ioctl(pm_dev, PMIOC_SET_POWER, &state); 139 device_close(pm_dev); 140 } 141 if (error) 142 printf("Error %d\n", error); 143 } 144 145 static void 146 cmd_shutdown(int argc, char **argv) 147 { 148 device_t pm_dev; 149 int error, state = PWR_OFF; 150 151 if ((error = device_open("pm", 0, &pm_dev)) == 0) { 152 error = device_ioctl(pm_dev, PMIOC_SET_POWER, &state); 153 device_close(pm_dev); 154 } 155 if (error) 156 printf("Error %d\n", error); 157 } 158 159 int 160 dispatch_cmd(int argc, char **argv) 161 { 162 int i = 0; 163 164 while (cmd_table[i].cmd != NULL) { 165 if (!strcmp(argv[0], cmd_table[i].cmd)) { 166 (cmd_table[i].func)(argc, argv); 167 break; 168 } 169 i++; 170 } 171 if (cmd_table[i].cmd == NULL) 172 printf("%s: command not found\n", argv[0]); 173 174 return 0; 175 } /* [<][>][^][v][top][bottom][index][help] */ | |||
Copyright© 2005-2009 Kohsuke Ohtani |