Annotation of prex/usr/test/kmon/cmd.c, Revision 1.1.1.1
1.1 nbrk 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 <prex/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: int cmd_help(int argc, char **argv);
45: int cmd_ver(int argc, char **argv);
46: int cmd_mem(int argc, char **argv);
47: int cmd_clear(int argc, char **argv);
48: int cmd_kill(int argc, char **argv);
49: #ifdef DEBUG
50: int cmd_thread(int argc, char **argv);
51: int cmd_task(int argc, char **argv);
52: int cmd_object(int argc, char **argv);
53: int cmd_timer(int argc, char **argv);
54: int cmd_irq(int argc, char **argv);
55: int cmd_device(int argc, char **argv);
56: int cmd_vm(int argc, char **argv);
57: #endif
58: int cmd_reboot(int argc, char **argv);
59: int cmd_shutdown(int argc, char **argv);
60:
61: static const char *err_msg[] = {
62: "Syntax error",
63: };
64:
65: struct cmd_entry {
66: char *cmd;
67: int (*func) (int, char **);
68: char *usage;
69: };
70:
71: static struct cmd_entry cmd_table[] = {
72: { "help" ,cmd_help ,"help - This help" },
73: { "ver" ,cmd_ver ,"ver - Kernel version information" },
74: { "mem" ,cmd_mem ,"mem - Show memory usage" },
75: { "clear" ,cmd_clear ,"clear - Clear screen" },
76: { "kill" ,cmd_kill ,"kill - Terminate thread" },
77: #ifdef DEBUG
78: { "thread" ,cmd_thread ,"thread - Dump threads" },
79: { "task" ,cmd_task ,"task - Dump tasks" },
80: { "vm" ,cmd_vm ,"vm - Dump virtual memory information" },
81: #endif
82: { "reboot" ,cmd_reboot ,"reboot - Reboot system" },
83: { "shutdown" ,cmd_shutdown ,"shutdown - Shutdown system" },
84: { NULL ,NULL ,NULL },
85: };
86:
87: int
88: cmd_help(int argc, char **argv)
89: {
90: int i = 0;
91:
92: while (cmd_table[i].cmd != NULL) {
93: puts(cmd_table[i].usage);
94: i++;
95: }
96: return 0;
97: }
98:
99: int
100: cmd_ver(int argc, char **argv)
101: {
102: struct info_kernel info;
103:
104: sys_info(INFO_KERNEL, &info);
105:
106: printf("Kernel version:\n");
107: printf("%s version %s for %s\n",
108: info.sysname, info.version, info.machine);
109: return 0;
110: }
111:
112: int
113: cmd_mem(int argc, char **argv)
114: {
115: struct info_memory info;
116:
117: sys_info(INFO_MEMORY, &info);
118:
119: printf("Memory usage:\n");
120: printf(" total used free\n");
121: printf(" %8d %8d %8d %8d\n", (u_int)info.total,
122: (u_int)(info.total - info.free), (u_int)info.free);
123: return 0;
124: }
125:
126: int
127: cmd_clear(int argc, char **argv)
128: {
129: printf("\33[2J");
130: return 0;
131: }
132:
133: int
134: cmd_kill(int argc, char **argv)
135: {
136: thread_t th;
137: char *ep;
138:
139: if (argc < 2)
140: return 1;
141: th = (thread_t)strtoul(argv[1], &ep, 16);
142: printf("Kill thread id:%x\n", (u_int)th);
143: if (thread_terminate(th)) {
144: printf("Thread %x does not exist\n", (u_int)th);
145: return 1;
146: }
147: return 0;
148: }
149:
150: #ifdef DEBUG
151: int
152: cmd_thread(int argc, char **argv)
153: {
154: int item = DUMP_THREAD;
155:
156: sys_debug(DCMD_DUMP, &item);
157: return 0;
158: }
159:
160: int
161: cmd_task(int argc, char **argv)
162: {
163: int item = DUMP_TASK;
164:
165: sys_debug(DCMD_DUMP, &item);
166: return 0;
167: }
168:
169: int
170: cmd_vm(int argc, char **argv)
171: {
172: int item = DUMP_VM;
173:
174: sys_debug(DCMD_DUMP, &item);
175: return 0;
176: }
177: #endif /* DEBUG */
178:
179: int
180: cmd_reboot(int argc, char **argv)
181: {
182: device_t pm_dev;
183: int err, state;
184:
185: if ((err = device_open("pm", 0, &pm_dev)) == 0) {
186: state = POWER_REBOOT;
187: err = device_ioctl(pm_dev, PMIOC_SET_POWER, &state);
188: device_close(pm_dev);
189: }
190: return err;
191: }
192:
193: int
194: cmd_shutdown(int argc, char **argv)
195: {
196: device_t pm_dev;
197: int err, state;
198:
199: if ((err = device_open("pm", 0, &pm_dev)) == 0) {
200: state = POWER_OFF;
201: err = device_ioctl(pm_dev, PMIOC_SET_POWER, &state);
202: device_close(pm_dev);
203: }
204: return err;
205: }
206:
207: int
208: dispatch_cmd(int argc, char **argv)
209: {
210: int i = 0;
211: int err = 0;
212:
213: while (cmd_table[i].cmd != NULL) {
214: if (!strcmp(argv[0], cmd_table[i].cmd)) {
215: err = (cmd_table[i].func)(argc, argv);
216: break;
217: }
218: i++;
219: }
220: if (cmd_table[i].cmd == NULL)
221: printf("%s: command not found\n", argv[0]);
222: if (err)
223: printf("Error %d:%s\n", err, err_msg[err - 1]);
224: return 0;
225: }
CVSweb