mconsole_kern.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
  3. * Copyright (C) 2001 - 2003 Jeff Dike (jdike@addtoit.com)
  4. * Licensed under the GPL
  5. */
  6. #include "linux/kernel.h"
  7. #include "linux/slab.h"
  8. #include "linux/init.h"
  9. #include "linux/notifier.h"
  10. #include "linux/reboot.h"
  11. #include "linux/utsname.h"
  12. #include "linux/ctype.h"
  13. #include "linux/interrupt.h"
  14. #include "linux/sysrq.h"
  15. #include "linux/workqueue.h"
  16. #include "linux/module.h"
  17. #include "linux/file.h"
  18. #include "linux/fs.h"
  19. #include "linux/namei.h"
  20. #include "linux/proc_fs.h"
  21. #include "linux/syscalls.h"
  22. #include "asm/irq.h"
  23. #include "asm/uaccess.h"
  24. #include "user_util.h"
  25. #include "kern_util.h"
  26. #include "kern.h"
  27. #include "mconsole.h"
  28. #include "mconsole_kern.h"
  29. #include "irq_user.h"
  30. #include "init.h"
  31. #include "os.h"
  32. #include "umid.h"
  33. #include "irq_kern.h"
  34. #include "choose-mode.h"
  35. static int do_unlink_socket(struct notifier_block *notifier,
  36. unsigned long what, void *data)
  37. {
  38. return(mconsole_unlink_socket());
  39. }
  40. static struct notifier_block reboot_notifier = {
  41. .notifier_call = do_unlink_socket,
  42. .priority = 0,
  43. };
  44. /* Safe without explicit locking for now. Tasklets provide their own
  45. * locking, and the interrupt handler is safe because it can't interrupt
  46. * itself and it can only happen on CPU 0.
  47. */
  48. LIST_HEAD(mc_requests);
  49. static void mc_work_proc(void *unused)
  50. {
  51. struct mconsole_entry *req;
  52. unsigned long flags;
  53. while(!list_empty(&mc_requests)){
  54. local_save_flags(flags);
  55. req = list_entry(mc_requests.next, struct mconsole_entry,
  56. list);
  57. list_del(&req->list);
  58. local_irq_restore(flags);
  59. req->request.cmd->handler(&req->request);
  60. kfree(req);
  61. }
  62. }
  63. DECLARE_WORK(mconsole_work, mc_work_proc, NULL);
  64. static irqreturn_t mconsole_interrupt(int irq, void *dev_id,
  65. struct pt_regs *regs)
  66. {
  67. /* long to avoid size mismatch warnings from gcc */
  68. long fd;
  69. struct mconsole_entry *new;
  70. struct mc_request req;
  71. fd = (long) dev_id;
  72. while (mconsole_get_request(fd, &req)){
  73. if(req.cmd->context == MCONSOLE_INTR)
  74. (*req.cmd->handler)(&req);
  75. else {
  76. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  77. if(new == NULL)
  78. mconsole_reply(&req, "Out of memory", 1, 0);
  79. else {
  80. new->request = req;
  81. list_add(&new->list, &mc_requests);
  82. }
  83. }
  84. }
  85. if(!list_empty(&mc_requests))
  86. schedule_work(&mconsole_work);
  87. reactivate_fd(fd, MCONSOLE_IRQ);
  88. return(IRQ_HANDLED);
  89. }
  90. void mconsole_version(struct mc_request *req)
  91. {
  92. char version[256];
  93. sprintf(version, "%s %s %s %s %s", system_utsname.sysname,
  94. system_utsname.nodename, system_utsname.release,
  95. system_utsname.version, system_utsname.machine);
  96. mconsole_reply(req, version, 0, 0);
  97. }
  98. void mconsole_log(struct mc_request *req)
  99. {
  100. int len;
  101. char *ptr = req->request.data;
  102. ptr += strlen("log ");
  103. len = req->len - (ptr - req->request.data);
  104. printk("%.*s", len, ptr);
  105. mconsole_reply(req, "", 0, 0);
  106. }
  107. /* This is a more convoluted version of mconsole_proc, which has some stability
  108. * problems; however, we need it fixed, because it is expected that UML users
  109. * mount HPPFS instead of procfs on /proc. And we want mconsole_proc to still
  110. * show the real procfs content, not the ones from hppfs.*/
  111. #if 0
  112. void mconsole_proc(struct mc_request *req)
  113. {
  114. struct nameidata nd;
  115. struct file_system_type *proc;
  116. struct super_block *super;
  117. struct file *file;
  118. int n, err;
  119. char *ptr = req->request.data, *buf;
  120. ptr += strlen("proc");
  121. while(isspace(*ptr)) ptr++;
  122. proc = get_fs_type("proc");
  123. if(proc == NULL){
  124. mconsole_reply(req, "procfs not registered", 1, 0);
  125. goto out;
  126. }
  127. super = (*proc->get_sb)(proc, 0, NULL, NULL);
  128. put_filesystem(proc);
  129. if(super == NULL){
  130. mconsole_reply(req, "Failed to get procfs superblock", 1, 0);
  131. goto out;
  132. }
  133. up_write(&super->s_umount);
  134. nd.dentry = super->s_root;
  135. nd.mnt = NULL;
  136. nd.flags = O_RDONLY + 1;
  137. nd.last_type = LAST_ROOT;
  138. /* START: it was experienced that the stability problems are closed
  139. * if commenting out these two calls + the below read cycle. To
  140. * make UML crash again, it was enough to readd either one.*/
  141. err = link_path_walk(ptr, &nd);
  142. if(err){
  143. mconsole_reply(req, "Failed to look up file", 1, 0);
  144. goto out_kill;
  145. }
  146. file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
  147. if(IS_ERR(file)){
  148. mconsole_reply(req, "Failed to open file", 1, 0);
  149. goto out_kill;
  150. }
  151. /*END*/
  152. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  153. if(buf == NULL){
  154. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  155. goto out_fput;
  156. }
  157. if((file->f_op != NULL) && (file->f_op->read != NULL)){
  158. do {
  159. n = (*file->f_op->read)(file, buf, PAGE_SIZE - 1,
  160. &file->f_pos);
  161. if(n >= 0){
  162. buf[n] = '\0';
  163. mconsole_reply(req, buf, 0, (n > 0));
  164. }
  165. else {
  166. mconsole_reply(req, "Read of file failed",
  167. 1, 0);
  168. goto out_free;
  169. }
  170. } while(n > 0);
  171. }
  172. else mconsole_reply(req, "", 0, 0);
  173. out_free:
  174. kfree(buf);
  175. out_fput:
  176. fput(file);
  177. out_kill:
  178. deactivate_super(super);
  179. out: ;
  180. }
  181. #endif
  182. void mconsole_proc(struct mc_request *req)
  183. {
  184. char path[64];
  185. char *buf;
  186. int len;
  187. int fd;
  188. int first_chunk = 1;
  189. char *ptr = req->request.data;
  190. ptr += strlen("proc");
  191. while(isspace(*ptr)) ptr++;
  192. snprintf(path, sizeof(path), "/proc/%s", ptr);
  193. fd = sys_open(path, 0, 0);
  194. if (fd < 0) {
  195. mconsole_reply(req, "Failed to open file", 1, 0);
  196. printk("open %s: %d\n",path,fd);
  197. goto out;
  198. }
  199. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  200. if(buf == NULL){
  201. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  202. goto out_close;
  203. }
  204. for (;;) {
  205. len = sys_read(fd, buf, PAGE_SIZE-1);
  206. if (len < 0) {
  207. mconsole_reply(req, "Read of file failed", 1, 0);
  208. goto out_free;
  209. }
  210. /*Begin the file content on his own line.*/
  211. if (first_chunk) {
  212. mconsole_reply(req, "\n", 0, 1);
  213. first_chunk = 0;
  214. }
  215. if (len == PAGE_SIZE-1) {
  216. buf[len] = '\0';
  217. mconsole_reply(req, buf, 0, 1);
  218. } else {
  219. buf[len] = '\0';
  220. mconsole_reply(req, buf, 0, 0);
  221. break;
  222. }
  223. }
  224. out_free:
  225. kfree(buf);
  226. out_close:
  227. sys_close(fd);
  228. out:
  229. /* nothing */;
  230. }
  231. #define UML_MCONSOLE_HELPTEXT \
  232. "Commands: \n\
  233. version - Get kernel version \n\
  234. help - Print this message \n\
  235. halt - Halt UML \n\
  236. reboot - Reboot UML \n\
  237. config <dev>=<config> - Add a new device to UML; \n\
  238. same syntax as command line \n\
  239. config <dev> - Query the configuration of a device \n\
  240. remove <dev> - Remove a device from UML \n\
  241. sysrq <letter> - Performs the SysRq action controlled by the letter \n\
  242. cad - invoke the Ctl-Alt-Del handler \n\
  243. stop - pause the UML; it will do nothing until it receives a 'go' \n\
  244. go - continue the UML after a 'stop' \n\
  245. log <string> - make UML enter <string> into the kernel log\n\
  246. proc <file> - returns the contents of the UML's /proc/<file>\n\
  247. stack <pid> - returns the stack of the specified pid\n\
  248. "
  249. void mconsole_help(struct mc_request *req)
  250. {
  251. mconsole_reply(req, UML_MCONSOLE_HELPTEXT, 0, 0);
  252. }
  253. void mconsole_halt(struct mc_request *req)
  254. {
  255. mconsole_reply(req, "", 0, 0);
  256. machine_halt();
  257. }
  258. void mconsole_reboot(struct mc_request *req)
  259. {
  260. mconsole_reply(req, "", 0, 0);
  261. machine_restart(NULL);
  262. }
  263. extern void ctrl_alt_del(void);
  264. void mconsole_cad(struct mc_request *req)
  265. {
  266. mconsole_reply(req, "", 0, 0);
  267. ctrl_alt_del();
  268. }
  269. void mconsole_go(struct mc_request *req)
  270. {
  271. mconsole_reply(req, "Not stopped", 1, 0);
  272. }
  273. void mconsole_stop(struct mc_request *req)
  274. {
  275. deactivate_fd(req->originating_fd, MCONSOLE_IRQ);
  276. os_set_fd_block(req->originating_fd, 1);
  277. mconsole_reply(req, "", 0, 0);
  278. while(mconsole_get_request(req->originating_fd, req)){
  279. if(req->cmd->handler == mconsole_go) break;
  280. (*req->cmd->handler)(req);
  281. }
  282. os_set_fd_block(req->originating_fd, 0);
  283. reactivate_fd(req->originating_fd, MCONSOLE_IRQ);
  284. mconsole_reply(req, "", 0, 0);
  285. }
  286. /* This list is populated by __initcall routines. */
  287. LIST_HEAD(mconsole_devices);
  288. void mconsole_register_dev(struct mc_device *new)
  289. {
  290. list_add(&new->list, &mconsole_devices);
  291. }
  292. static struct mc_device *mconsole_find_dev(char *name)
  293. {
  294. struct list_head *ele;
  295. struct mc_device *dev;
  296. list_for_each(ele, &mconsole_devices){
  297. dev = list_entry(ele, struct mc_device, list);
  298. if(!strncmp(name, dev->name, strlen(dev->name)))
  299. return(dev);
  300. }
  301. return(NULL);
  302. }
  303. #define CONFIG_BUF_SIZE 64
  304. static void mconsole_get_config(int (*get_config)(char *, char *, int,
  305. char **),
  306. struct mc_request *req, char *name)
  307. {
  308. char default_buf[CONFIG_BUF_SIZE], *error, *buf;
  309. int n, size;
  310. if(get_config == NULL){
  311. mconsole_reply(req, "No get_config routine defined", 1, 0);
  312. return;
  313. }
  314. error = NULL;
  315. size = sizeof(default_buf)/sizeof(default_buf[0]);
  316. buf = default_buf;
  317. while(1){
  318. n = (*get_config)(name, buf, size, &error);
  319. if(error != NULL){
  320. mconsole_reply(req, error, 1, 0);
  321. goto out;
  322. }
  323. if(n <= size){
  324. mconsole_reply(req, buf, 0, 0);
  325. goto out;
  326. }
  327. if(buf != default_buf)
  328. kfree(buf);
  329. size = n;
  330. buf = kmalloc(size, GFP_KERNEL);
  331. if(buf == NULL){
  332. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  333. return;
  334. }
  335. }
  336. out:
  337. if(buf != default_buf)
  338. kfree(buf);
  339. }
  340. void mconsole_config(struct mc_request *req)
  341. {
  342. struct mc_device *dev;
  343. char *ptr = req->request.data, *name;
  344. int err;
  345. ptr += strlen("config");
  346. while(isspace(*ptr)) ptr++;
  347. dev = mconsole_find_dev(ptr);
  348. if(dev == NULL){
  349. mconsole_reply(req, "Bad configuration option", 1, 0);
  350. return;
  351. }
  352. name = &ptr[strlen(dev->name)];
  353. ptr = name;
  354. while((*ptr != '=') && (*ptr != '\0'))
  355. ptr++;
  356. if(*ptr == '='){
  357. err = (*dev->config)(name);
  358. mconsole_reply(req, "", err, 0);
  359. }
  360. else mconsole_get_config(dev->get_config, req, name);
  361. }
  362. void mconsole_remove(struct mc_request *req)
  363. {
  364. struct mc_device *dev;
  365. char *ptr = req->request.data, *err_msg = "";
  366. char error[256];
  367. int err, start, end, n;
  368. ptr += strlen("remove");
  369. while(isspace(*ptr)) ptr++;
  370. dev = mconsole_find_dev(ptr);
  371. if(dev == NULL){
  372. mconsole_reply(req, "Bad remove option", 1, 0);
  373. return;
  374. }
  375. ptr = &ptr[strlen(dev->name)];
  376. err = 1;
  377. n = (*dev->id)(&ptr, &start, &end);
  378. if(n < 0){
  379. err_msg = "Couldn't parse device number";
  380. goto out;
  381. }
  382. else if((n < start) || (n > end)){
  383. sprintf(error, "Invalid device number - must be between "
  384. "%d and %d", start, end);
  385. err_msg = error;
  386. goto out;
  387. }
  388. err = (*dev->remove)(n);
  389. switch(err){
  390. case -ENODEV:
  391. err_msg = "Device doesn't exist";
  392. break;
  393. case -EBUSY:
  394. err_msg = "Device is currently open";
  395. break;
  396. default:
  397. break;
  398. }
  399. out:
  400. mconsole_reply(req, err_msg, err, 0);
  401. }
  402. #ifdef CONFIG_MAGIC_SYSRQ
  403. void mconsole_sysrq(struct mc_request *req)
  404. {
  405. char *ptr = req->request.data;
  406. ptr += strlen("sysrq");
  407. while(isspace(*ptr)) ptr++;
  408. mconsole_reply(req, "", 0, 0);
  409. handle_sysrq(*ptr, &current->thread.regs, NULL);
  410. }
  411. #else
  412. void mconsole_sysrq(struct mc_request *req)
  413. {
  414. mconsole_reply(req, "Sysrq not compiled in", 1, 0);
  415. }
  416. #endif
  417. /* Mconsole stack trace
  418. * Added by Allan Graves, Jeff Dike
  419. * Dumps a stacks registers to the linux console.
  420. * Usage stack <pid>.
  421. */
  422. void do_stack(struct mc_request *req)
  423. {
  424. char *ptr = req->request.data;
  425. int pid_requested= -1;
  426. struct task_struct *from = NULL;
  427. struct task_struct *to = NULL;
  428. /* Would be nice:
  429. * 1) Send showregs output to mconsole.
  430. * 2) Add a way to stack dump all pids.
  431. */
  432. ptr += strlen("stack");
  433. while(isspace(*ptr)) ptr++;
  434. /* Should really check for multiple pids or reject bad args here */
  435. /* What do the arguments in mconsole_reply mean? */
  436. if(sscanf(ptr, "%d", &pid_requested) == 0){
  437. mconsole_reply(req, "Please specify a pid", 1, 0);
  438. return;
  439. }
  440. from = current;
  441. to = find_task_by_pid(pid_requested);
  442. if((to == NULL) || (pid_requested == 0)) {
  443. mconsole_reply(req, "Couldn't find that pid", 1, 0);
  444. return;
  445. }
  446. to->thread.saved_task = current;
  447. switch_to(from, to, from);
  448. mconsole_reply(req, "Stack Dumped to console and message log", 0, 0);
  449. }
  450. void mconsole_stack(struct mc_request *req)
  451. {
  452. /* This command doesn't work in TT mode, so let's check and then
  453. * get out of here
  454. */
  455. CHOOSE_MODE(mconsole_reply(req, "Sorry, this doesn't work in TT mode",
  456. 1, 0),
  457. do_stack(req));
  458. }
  459. /* Changed by mconsole_setup, which is __setup, and called before SMP is
  460. * active.
  461. */
  462. static char *notify_socket = NULL;
  463. int mconsole_init(void)
  464. {
  465. /* long to avoid size mismatch warnings from gcc */
  466. long sock;
  467. int err;
  468. char file[256];
  469. if(umid_file_name("mconsole", file, sizeof(file))) return(-1);
  470. snprintf(mconsole_socket_name, sizeof(file), "%s", file);
  471. sock = os_create_unix_socket(file, sizeof(file), 1);
  472. if (sock < 0){
  473. printk("Failed to initialize management console\n");
  474. return(1);
  475. }
  476. register_reboot_notifier(&reboot_notifier);
  477. err = um_request_irq(MCONSOLE_IRQ, sock, IRQ_READ, mconsole_interrupt,
  478. SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
  479. "mconsole", (void *)sock);
  480. if (err){
  481. printk("Failed to get IRQ for management console\n");
  482. return(1);
  483. }
  484. if(notify_socket != NULL){
  485. notify_socket = uml_strdup(notify_socket);
  486. if(notify_socket != NULL)
  487. mconsole_notify(notify_socket, MCONSOLE_SOCKET,
  488. mconsole_socket_name,
  489. strlen(mconsole_socket_name) + 1);
  490. else printk(KERN_ERR "mconsole_setup failed to strdup "
  491. "string\n");
  492. }
  493. printk("mconsole (version %d) initialized on %s\n",
  494. MCONSOLE_VERSION, mconsole_socket_name);
  495. return(0);
  496. }
  497. __initcall(mconsole_init);
  498. static int write_proc_mconsole(struct file *file, const char __user *buffer,
  499. unsigned long count, void *data)
  500. {
  501. char *buf;
  502. buf = kmalloc(count + 1, GFP_KERNEL);
  503. if(buf == NULL)
  504. return(-ENOMEM);
  505. if(copy_from_user(buf, buffer, count)){
  506. count = -EFAULT;
  507. goto out;
  508. }
  509. buf[count] = '\0';
  510. mconsole_notify(notify_socket, MCONSOLE_USER_NOTIFY, buf, count);
  511. out:
  512. kfree(buf);
  513. return(count);
  514. }
  515. static int create_proc_mconsole(void)
  516. {
  517. struct proc_dir_entry *ent;
  518. if(notify_socket == NULL) return(0);
  519. ent = create_proc_entry("mconsole", S_IFREG | 0200, NULL);
  520. if(ent == NULL){
  521. printk(KERN_INFO "create_proc_mconsole : create_proc_entry failed\n");
  522. return(0);
  523. }
  524. ent->read_proc = NULL;
  525. ent->write_proc = write_proc_mconsole;
  526. return(0);
  527. }
  528. static DEFINE_SPINLOCK(notify_spinlock);
  529. void lock_notify(void)
  530. {
  531. spin_lock(&notify_spinlock);
  532. }
  533. void unlock_notify(void)
  534. {
  535. spin_unlock(&notify_spinlock);
  536. }
  537. __initcall(create_proc_mconsole);
  538. #define NOTIFY "=notify:"
  539. static int mconsole_setup(char *str)
  540. {
  541. if(!strncmp(str, NOTIFY, strlen(NOTIFY))){
  542. str += strlen(NOTIFY);
  543. notify_socket = str;
  544. }
  545. else printk(KERN_ERR "mconsole_setup : Unknown option - '%s'\n", str);
  546. return(1);
  547. }
  548. __setup("mconsole", mconsole_setup);
  549. __uml_help(mconsole_setup,
  550. "mconsole=notify:<socket>\n"
  551. " Requests that the mconsole driver send a message to the named Unix\n"
  552. " socket containing the name of the mconsole socket. This also serves\n"
  553. " to notify outside processes when UML has booted far enough to respond\n"
  554. " to mconsole requests.\n\n"
  555. );
  556. static int notify_panic(struct notifier_block *self, unsigned long unused1,
  557. void *ptr)
  558. {
  559. char *message = ptr;
  560. if(notify_socket == NULL) return(0);
  561. mconsole_notify(notify_socket, MCONSOLE_PANIC, message,
  562. strlen(message) + 1);
  563. return(0);
  564. }
  565. static struct notifier_block panic_exit_notifier = {
  566. .notifier_call = notify_panic,
  567. .next = NULL,
  568. .priority = 1
  569. };
  570. static int add_notifier(void)
  571. {
  572. notifier_chain_register(&panic_notifier_list, &panic_exit_notifier);
  573. return(0);
  574. }
  575. __initcall(add_notifier);
  576. char *mconsole_notify_socket(void)
  577. {
  578. return(notify_socket);
  579. }
  580. EXPORT_SYMBOL(mconsole_notify_socket);
  581. /*
  582. * Overrides for Emacs so that we follow Linus's tabbing style.
  583. * Emacs will notice this stuff at the end of the file and automatically
  584. * adjust the settings for this buffer only. This must remain at the end
  585. * of the file.
  586. * ---------------------------------------------------------------------------
  587. * Local variables:
  588. * c-file-style: "linux"
  589. * End:
  590. */