mconsole_kern.c 14 KB

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