mconsole_kern.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
  3. * Copyright (C) 2001 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Licensed under the GPL
  5. */
  6. #include <linux/console.h>
  7. #include <linux/ctype.h>
  8. #include <linux/string.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/list.h>
  11. #include <linux/mm.h>
  12. #include <linux/module.h>
  13. #include <linux/notifier.h>
  14. #include <linux/reboot.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/slab.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/utsname.h>
  19. #include <linux/socket.h>
  20. #include <linux/un.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/mutex.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/switch_to.h>
  25. #include "init.h"
  26. #include "irq_kern.h"
  27. #include "irq_user.h"
  28. #include "kern_util.h"
  29. #include "mconsole.h"
  30. #include "mconsole_kern.h"
  31. #include "os.h"
  32. static int do_unlink_socket(struct notifier_block *notifier,
  33. unsigned long what, void *data)
  34. {
  35. return mconsole_unlink_socket();
  36. }
  37. static struct notifier_block reboot_notifier = {
  38. .notifier_call = do_unlink_socket,
  39. .priority = 0,
  40. };
  41. /* Safe without explicit locking for now. Tasklets provide their own
  42. * locking, and the interrupt handler is safe because it can't interrupt
  43. * itself and it can only happen on CPU 0.
  44. */
  45. static LIST_HEAD(mc_requests);
  46. static void mc_work_proc(struct work_struct *unused)
  47. {
  48. struct mconsole_entry *req;
  49. unsigned long flags;
  50. while (!list_empty(&mc_requests)) {
  51. local_irq_save(flags);
  52. req = list_entry(mc_requests.next, struct mconsole_entry, list);
  53. list_del(&req->list);
  54. local_irq_restore(flags);
  55. req->request.cmd->handler(&req->request);
  56. kfree(req);
  57. }
  58. }
  59. static DECLARE_WORK(mconsole_work, mc_work_proc);
  60. static irqreturn_t mconsole_interrupt(int irq, void *dev_id)
  61. {
  62. /* long to avoid size mismatch warnings from gcc */
  63. long fd;
  64. struct mconsole_entry *new;
  65. static struct mc_request req; /* that's OK */
  66. fd = (long) dev_id;
  67. while (mconsole_get_request(fd, &req)) {
  68. if (req.cmd->context == MCONSOLE_INTR)
  69. (*req.cmd->handler)(&req);
  70. else {
  71. new = kmalloc(sizeof(*new), GFP_NOWAIT);
  72. if (new == NULL)
  73. mconsole_reply(&req, "Out of memory", 1, 0);
  74. else {
  75. new->request = req;
  76. new->request.regs = get_irq_regs()->regs;
  77. list_add(&new->list, &mc_requests);
  78. }
  79. }
  80. }
  81. if (!list_empty(&mc_requests))
  82. schedule_work(&mconsole_work);
  83. reactivate_fd(fd, MCONSOLE_IRQ);
  84. return IRQ_HANDLED;
  85. }
  86. void mconsole_version(struct mc_request *req)
  87. {
  88. char version[256];
  89. sprintf(version, "%s %s %s %s %s", utsname()->sysname,
  90. utsname()->nodename, utsname()->release, utsname()->version,
  91. utsname()->machine);
  92. mconsole_reply(req, version, 0, 0);
  93. }
  94. void mconsole_log(struct mc_request *req)
  95. {
  96. int len;
  97. char *ptr = req->request.data;
  98. ptr += strlen("log ");
  99. len = req->len - (ptr - req->request.data);
  100. printk(KERN_WARNING "%.*s", len, ptr);
  101. mconsole_reply(req, "", 0, 0);
  102. }
  103. /* This is a more convoluted version of mconsole_proc, which has some stability
  104. * problems; however, we need it fixed, because it is expected that UML users
  105. * mount HPPFS instead of procfs on /proc. And we want mconsole_proc to still
  106. * show the real procfs content, not the ones from hppfs.*/
  107. #if 0
  108. void mconsole_proc(struct mc_request *req)
  109. {
  110. struct vfsmount *mnt = current->nsproxy->pid_ns->proc_mnt;
  111. struct file *file;
  112. int n;
  113. char *ptr = req->request.data, *buf;
  114. mm_segment_t old_fs = get_fs();
  115. ptr += strlen("proc");
  116. ptr = skip_spaces(ptr);
  117. file = file_open_root(mnt->mnt_root, mnt, ptr, O_RDONLY);
  118. if (IS_ERR(file)) {
  119. mconsole_reply(req, "Failed to open file", 1, 0);
  120. goto out;
  121. }
  122. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  123. if (buf == NULL) {
  124. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  125. goto out_fput;
  126. }
  127. if (file->f_op->read) {
  128. do {
  129. loff_t pos;
  130. set_fs(KERNEL_DS);
  131. n = vfs_read(file, buf, PAGE_SIZE - 1, &pos);
  132. file_pos_write(file, pos);
  133. set_fs(old_fs);
  134. if (n >= 0) {
  135. buf[n] = '\0';
  136. mconsole_reply(req, buf, 0, (n > 0));
  137. }
  138. else {
  139. mconsole_reply(req, "Read of file failed",
  140. 1, 0);
  141. goto out_free;
  142. }
  143. } while (n > 0);
  144. }
  145. else mconsole_reply(req, "", 0, 0);
  146. out_free:
  147. kfree(buf);
  148. out_fput:
  149. fput(file);
  150. out: ;
  151. }
  152. #endif
  153. void mconsole_proc(struct mc_request *req)
  154. {
  155. char path[64];
  156. char *buf;
  157. int len;
  158. int fd;
  159. int first_chunk = 1;
  160. char *ptr = req->request.data;
  161. ptr += strlen("proc");
  162. ptr = skip_spaces(ptr);
  163. snprintf(path, sizeof(path), "/proc/%s", ptr);
  164. fd = sys_open(path, 0, 0);
  165. if (fd < 0) {
  166. mconsole_reply(req, "Failed to open file", 1, 0);
  167. printk(KERN_ERR "open %s: %d\n",path,fd);
  168. goto out;
  169. }
  170. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  171. if (buf == NULL) {
  172. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  173. goto out_close;
  174. }
  175. for (;;) {
  176. len = sys_read(fd, buf, PAGE_SIZE-1);
  177. if (len < 0) {
  178. mconsole_reply(req, "Read of file failed", 1, 0);
  179. goto out_free;
  180. }
  181. /* Begin the file content on his own line. */
  182. if (first_chunk) {
  183. mconsole_reply(req, "\n", 0, 1);
  184. first_chunk = 0;
  185. }
  186. if (len == PAGE_SIZE-1) {
  187. buf[len] = '\0';
  188. mconsole_reply(req, buf, 0, 1);
  189. } else {
  190. buf[len] = '\0';
  191. mconsole_reply(req, buf, 0, 0);
  192. break;
  193. }
  194. }
  195. out_free:
  196. kfree(buf);
  197. out_close:
  198. sys_close(fd);
  199. out:
  200. /* nothing */;
  201. }
  202. #define UML_MCONSOLE_HELPTEXT \
  203. "Commands: \n\
  204. version - Get kernel version \n\
  205. help - Print this message \n\
  206. halt - Halt UML \n\
  207. reboot - Reboot UML \n\
  208. config <dev>=<config> - Add a new device to UML; \n\
  209. same syntax as command line \n\
  210. config <dev> - Query the configuration of a device \n\
  211. remove <dev> - Remove a device from UML \n\
  212. sysrq <letter> - Performs the SysRq action controlled by the letter \n\
  213. cad - invoke the Ctrl-Alt-Del handler \n\
  214. stop - pause the UML; it will do nothing until it receives a 'go' \n\
  215. go - continue the UML after a 'stop' \n\
  216. log <string> - make UML enter <string> into the kernel log\n\
  217. proc <file> - returns the contents of the UML's /proc/<file>\n\
  218. stack <pid> - returns the stack of the specified pid\n\
  219. "
  220. void mconsole_help(struct mc_request *req)
  221. {
  222. mconsole_reply(req, UML_MCONSOLE_HELPTEXT, 0, 0);
  223. }
  224. void mconsole_halt(struct mc_request *req)
  225. {
  226. mconsole_reply(req, "", 0, 0);
  227. machine_halt();
  228. }
  229. void mconsole_reboot(struct mc_request *req)
  230. {
  231. mconsole_reply(req, "", 0, 0);
  232. machine_restart(NULL);
  233. }
  234. void mconsole_cad(struct mc_request *req)
  235. {
  236. mconsole_reply(req, "", 0, 0);
  237. ctrl_alt_del();
  238. }
  239. void mconsole_go(struct mc_request *req)
  240. {
  241. mconsole_reply(req, "Not stopped", 1, 0);
  242. }
  243. void mconsole_stop(struct mc_request *req)
  244. {
  245. deactivate_fd(req->originating_fd, MCONSOLE_IRQ);
  246. os_set_fd_block(req->originating_fd, 1);
  247. mconsole_reply(req, "stopped", 0, 0);
  248. for (;;) {
  249. if (!mconsole_get_request(req->originating_fd, req))
  250. continue;
  251. if (req->cmd->handler == mconsole_go)
  252. break;
  253. if (req->cmd->handler == mconsole_stop) {
  254. mconsole_reply(req, "Already stopped", 1, 0);
  255. continue;
  256. }
  257. if (req->cmd->handler == mconsole_sysrq) {
  258. struct pt_regs *old_regs;
  259. old_regs = set_irq_regs((struct pt_regs *)&req->regs);
  260. mconsole_sysrq(req);
  261. set_irq_regs(old_regs);
  262. continue;
  263. }
  264. (*req->cmd->handler)(req);
  265. }
  266. os_set_fd_block(req->originating_fd, 0);
  267. reactivate_fd(req->originating_fd, MCONSOLE_IRQ);
  268. mconsole_reply(req, "", 0, 0);
  269. }
  270. static DEFINE_SPINLOCK(mc_devices_lock);
  271. static LIST_HEAD(mconsole_devices);
  272. void mconsole_register_dev(struct mc_device *new)
  273. {
  274. spin_lock(&mc_devices_lock);
  275. BUG_ON(!list_empty(&new->list));
  276. list_add(&new->list, &mconsole_devices);
  277. spin_unlock(&mc_devices_lock);
  278. }
  279. static struct mc_device *mconsole_find_dev(char *name)
  280. {
  281. struct list_head *ele;
  282. struct mc_device *dev;
  283. list_for_each(ele, &mconsole_devices) {
  284. dev = list_entry(ele, struct mc_device, list);
  285. if (!strncmp(name, dev->name, strlen(dev->name)))
  286. return dev;
  287. }
  288. return NULL;
  289. }
  290. #define UNPLUGGED_PER_PAGE \
  291. ((PAGE_SIZE - sizeof(struct list_head)) / sizeof(unsigned long))
  292. struct unplugged_pages {
  293. struct list_head list;
  294. void *pages[UNPLUGGED_PER_PAGE];
  295. };
  296. static DEFINE_MUTEX(plug_mem_mutex);
  297. static unsigned long long unplugged_pages_count = 0;
  298. static LIST_HEAD(unplugged_pages);
  299. static int unplug_index = UNPLUGGED_PER_PAGE;
  300. static int mem_config(char *str, char **error_out)
  301. {
  302. unsigned long long diff;
  303. int err = -EINVAL, i, add;
  304. char *ret;
  305. if (str[0] != '=') {
  306. *error_out = "Expected '=' after 'mem'";
  307. goto out;
  308. }
  309. str++;
  310. if (str[0] == '-')
  311. add = 0;
  312. else if (str[0] == '+') {
  313. add = 1;
  314. }
  315. else {
  316. *error_out = "Expected increment to start with '-' or '+'";
  317. goto out;
  318. }
  319. str++;
  320. diff = memparse(str, &ret);
  321. if (*ret != '\0') {
  322. *error_out = "Failed to parse memory increment";
  323. goto out;
  324. }
  325. diff /= PAGE_SIZE;
  326. mutex_lock(&plug_mem_mutex);
  327. for (i = 0; i < diff; i++) {
  328. struct unplugged_pages *unplugged;
  329. void *addr;
  330. if (add) {
  331. if (list_empty(&unplugged_pages))
  332. break;
  333. unplugged = list_entry(unplugged_pages.next,
  334. struct unplugged_pages, list);
  335. if (unplug_index > 0)
  336. addr = unplugged->pages[--unplug_index];
  337. else {
  338. list_del(&unplugged->list);
  339. addr = unplugged;
  340. unplug_index = UNPLUGGED_PER_PAGE;
  341. }
  342. free_page((unsigned long) addr);
  343. unplugged_pages_count--;
  344. }
  345. else {
  346. struct page *page;
  347. page = alloc_page(GFP_ATOMIC);
  348. if (page == NULL)
  349. break;
  350. unplugged = page_address(page);
  351. if (unplug_index == UNPLUGGED_PER_PAGE) {
  352. list_add(&unplugged->list, &unplugged_pages);
  353. unplug_index = 0;
  354. }
  355. else {
  356. struct list_head *entry = unplugged_pages.next;
  357. addr = unplugged;
  358. unplugged = list_entry(entry,
  359. struct unplugged_pages,
  360. list);
  361. err = os_drop_memory(addr, PAGE_SIZE);
  362. if (err) {
  363. printk(KERN_ERR "Failed to release "
  364. "memory - errno = %d\n", err);
  365. *error_out = "Failed to release memory";
  366. goto out_unlock;
  367. }
  368. unplugged->pages[unplug_index++] = addr;
  369. }
  370. unplugged_pages_count++;
  371. }
  372. }
  373. err = 0;
  374. out_unlock:
  375. mutex_unlock(&plug_mem_mutex);
  376. out:
  377. return err;
  378. }
  379. static int mem_get_config(char *name, char *str, int size, char **error_out)
  380. {
  381. char buf[sizeof("18446744073709551615")];
  382. int len = 0;
  383. sprintf(buf, "%ld", uml_physmem);
  384. CONFIG_CHUNK(str, size, len, buf, 1);
  385. return len;
  386. }
  387. static int mem_id(char **str, int *start_out, int *end_out)
  388. {
  389. *start_out = 0;
  390. *end_out = 0;
  391. return 0;
  392. }
  393. static int mem_remove(int n, char **error_out)
  394. {
  395. *error_out = "Memory doesn't support the remove operation";
  396. return -EBUSY;
  397. }
  398. static struct mc_device mem_mc = {
  399. .list = LIST_HEAD_INIT(mem_mc.list),
  400. .name = "mem",
  401. .config = mem_config,
  402. .get_config = mem_get_config,
  403. .id = mem_id,
  404. .remove = mem_remove,
  405. };
  406. static int __init mem_mc_init(void)
  407. {
  408. if (can_drop_memory())
  409. mconsole_register_dev(&mem_mc);
  410. else printk(KERN_ERR "Can't release memory to the host - memory "
  411. "hotplug won't be supported\n");
  412. return 0;
  413. }
  414. __initcall(mem_mc_init);
  415. #define CONFIG_BUF_SIZE 64
  416. static void mconsole_get_config(int (*get_config)(char *, char *, int,
  417. char **),
  418. struct mc_request *req, char *name)
  419. {
  420. char default_buf[CONFIG_BUF_SIZE], *error, *buf;
  421. int n, size;
  422. if (get_config == NULL) {
  423. mconsole_reply(req, "No get_config routine defined", 1, 0);
  424. return;
  425. }
  426. error = NULL;
  427. size = ARRAY_SIZE(default_buf);
  428. buf = default_buf;
  429. while (1) {
  430. n = (*get_config)(name, buf, size, &error);
  431. if (error != NULL) {
  432. mconsole_reply(req, error, 1, 0);
  433. goto out;
  434. }
  435. if (n <= size) {
  436. mconsole_reply(req, buf, 0, 0);
  437. goto out;
  438. }
  439. if (buf != default_buf)
  440. kfree(buf);
  441. size = n;
  442. buf = kmalloc(size, GFP_KERNEL);
  443. if (buf == NULL) {
  444. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  445. return;
  446. }
  447. }
  448. out:
  449. if (buf != default_buf)
  450. kfree(buf);
  451. }
  452. void mconsole_config(struct mc_request *req)
  453. {
  454. struct mc_device *dev;
  455. char *ptr = req->request.data, *name, *error_string = "";
  456. int err;
  457. ptr += strlen("config");
  458. ptr = skip_spaces(ptr);
  459. dev = mconsole_find_dev(ptr);
  460. if (dev == NULL) {
  461. mconsole_reply(req, "Bad configuration option", 1, 0);
  462. return;
  463. }
  464. name = &ptr[strlen(dev->name)];
  465. ptr = name;
  466. while ((*ptr != '=') && (*ptr != '\0'))
  467. ptr++;
  468. if (*ptr == '=') {
  469. err = (*dev->config)(name, &error_string);
  470. mconsole_reply(req, error_string, err, 0);
  471. }
  472. else mconsole_get_config(dev->get_config, req, name);
  473. }
  474. void mconsole_remove(struct mc_request *req)
  475. {
  476. struct mc_device *dev;
  477. char *ptr = req->request.data, *err_msg = "";
  478. char error[256];
  479. int err, start, end, n;
  480. ptr += strlen("remove");
  481. ptr = skip_spaces(ptr);
  482. dev = mconsole_find_dev(ptr);
  483. if (dev == NULL) {
  484. mconsole_reply(req, "Bad remove option", 1, 0);
  485. return;
  486. }
  487. ptr = &ptr[strlen(dev->name)];
  488. err = 1;
  489. n = (*dev->id)(&ptr, &start, &end);
  490. if (n < 0) {
  491. err_msg = "Couldn't parse device number";
  492. goto out;
  493. }
  494. else if ((n < start) || (n > end)) {
  495. sprintf(error, "Invalid device number - must be between "
  496. "%d and %d", start, end);
  497. err_msg = error;
  498. goto out;
  499. }
  500. err_msg = NULL;
  501. err = (*dev->remove)(n, &err_msg);
  502. switch(err) {
  503. case 0:
  504. err_msg = "";
  505. break;
  506. case -ENODEV:
  507. if (err_msg == NULL)
  508. err_msg = "Device doesn't exist";
  509. break;
  510. case -EBUSY:
  511. if (err_msg == NULL)
  512. err_msg = "Device is currently open";
  513. break;
  514. default:
  515. break;
  516. }
  517. out:
  518. mconsole_reply(req, err_msg, err, 0);
  519. }
  520. struct mconsole_output {
  521. struct list_head list;
  522. struct mc_request *req;
  523. };
  524. static DEFINE_SPINLOCK(client_lock);
  525. static LIST_HEAD(clients);
  526. static char console_buf[MCONSOLE_MAX_DATA];
  527. static void console_write(struct console *console, const char *string,
  528. unsigned int len)
  529. {
  530. struct list_head *ele;
  531. int n;
  532. if (list_empty(&clients))
  533. return;
  534. while (len > 0) {
  535. n = min((size_t) len, ARRAY_SIZE(console_buf));
  536. strncpy(console_buf, string, n);
  537. string += n;
  538. len -= n;
  539. list_for_each(ele, &clients) {
  540. struct mconsole_output *entry;
  541. entry = list_entry(ele, struct mconsole_output, list);
  542. mconsole_reply_len(entry->req, console_buf, n, 0, 1);
  543. }
  544. }
  545. }
  546. static struct console mc_console = { .name = "mc",
  547. .write = console_write,
  548. .flags = CON_ENABLED,
  549. .index = -1 };
  550. static int mc_add_console(void)
  551. {
  552. register_console(&mc_console);
  553. return 0;
  554. }
  555. late_initcall(mc_add_console);
  556. static void with_console(struct mc_request *req, void (*proc)(void *),
  557. void *arg)
  558. {
  559. struct mconsole_output entry;
  560. unsigned long flags;
  561. entry.req = req;
  562. spin_lock_irqsave(&client_lock, flags);
  563. list_add(&entry.list, &clients);
  564. spin_unlock_irqrestore(&client_lock, flags);
  565. (*proc)(arg);
  566. mconsole_reply_len(req, "", 0, 0, 0);
  567. spin_lock_irqsave(&client_lock, flags);
  568. list_del(&entry.list);
  569. spin_unlock_irqrestore(&client_lock, flags);
  570. }
  571. #ifdef CONFIG_MAGIC_SYSRQ
  572. #include <linux/sysrq.h>
  573. static void sysrq_proc(void *arg)
  574. {
  575. char *op = arg;
  576. handle_sysrq(*op);
  577. }
  578. void mconsole_sysrq(struct mc_request *req)
  579. {
  580. char *ptr = req->request.data;
  581. ptr += strlen("sysrq");
  582. ptr = skip_spaces(ptr);
  583. /*
  584. * With 'b', the system will shut down without a chance to reply,
  585. * so in this case, we reply first.
  586. */
  587. if (*ptr == 'b')
  588. mconsole_reply(req, "", 0, 0);
  589. with_console(req, sysrq_proc, ptr);
  590. }
  591. #else
  592. void mconsole_sysrq(struct mc_request *req)
  593. {
  594. mconsole_reply(req, "Sysrq not compiled in", 1, 0);
  595. }
  596. #endif
  597. static void stack_proc(void *arg)
  598. {
  599. struct task_struct *from = current, *to = arg;
  600. to->thread.saved_task = from;
  601. switch_to(from, to, from);
  602. }
  603. /*
  604. * Mconsole stack trace
  605. * Added by Allan Graves, Jeff Dike
  606. * Dumps a stacks registers to the linux console.
  607. * Usage stack <pid>.
  608. */
  609. void mconsole_stack(struct mc_request *req)
  610. {
  611. char *ptr = req->request.data;
  612. int pid_requested= -1;
  613. struct task_struct *to = NULL;
  614. /*
  615. * Would be nice:
  616. * 1) Send showregs output to mconsole.
  617. * 2) Add a way to stack dump all pids.
  618. */
  619. ptr += strlen("stack");
  620. ptr = skip_spaces(ptr);
  621. /*
  622. * Should really check for multiple pids or reject bad args here
  623. */
  624. /* What do the arguments in mconsole_reply mean? */
  625. if (sscanf(ptr, "%d", &pid_requested) == 0) {
  626. mconsole_reply(req, "Please specify a pid", 1, 0);
  627. return;
  628. }
  629. to = find_task_by_pid_ns(pid_requested, &init_pid_ns);
  630. if ((to == NULL) || (pid_requested == 0)) {
  631. mconsole_reply(req, "Couldn't find that pid", 1, 0);
  632. return;
  633. }
  634. with_console(req, stack_proc, to);
  635. }
  636. /*
  637. * Changed by mconsole_setup, which is __setup, and called before SMP is
  638. * active.
  639. */
  640. static char *notify_socket = NULL;
  641. static int __init mconsole_init(void)
  642. {
  643. /* long to avoid size mismatch warnings from gcc */
  644. long sock;
  645. int err;
  646. char file[UNIX_PATH_MAX];
  647. if (umid_file_name("mconsole", file, sizeof(file)))
  648. return -1;
  649. snprintf(mconsole_socket_name, sizeof(file), "%s", file);
  650. sock = os_create_unix_socket(file, sizeof(file), 1);
  651. if (sock < 0) {
  652. printk(KERN_ERR "Failed to initialize management console\n");
  653. return 1;
  654. }
  655. if (os_set_fd_block(sock, 0))
  656. goto out;
  657. register_reboot_notifier(&reboot_notifier);
  658. err = um_request_irq(MCONSOLE_IRQ, sock, IRQ_READ, mconsole_interrupt,
  659. IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  660. "mconsole", (void *)sock);
  661. if (err) {
  662. printk(KERN_ERR "Failed to get IRQ for management console\n");
  663. goto out;
  664. }
  665. if (notify_socket != NULL) {
  666. notify_socket = kstrdup(notify_socket, GFP_KERNEL);
  667. if (notify_socket != NULL)
  668. mconsole_notify(notify_socket, MCONSOLE_SOCKET,
  669. mconsole_socket_name,
  670. strlen(mconsole_socket_name) + 1);
  671. else printk(KERN_ERR "mconsole_setup failed to strdup "
  672. "string\n");
  673. }
  674. printk(KERN_INFO "mconsole (version %d) initialized on %s\n",
  675. MCONSOLE_VERSION, mconsole_socket_name);
  676. return 0;
  677. out:
  678. os_close_file(sock);
  679. return 1;
  680. }
  681. __initcall(mconsole_init);
  682. static ssize_t mconsole_proc_write(struct file *file,
  683. const char __user *buffer, size_t count, loff_t *pos)
  684. {
  685. char *buf;
  686. buf = kmalloc(count + 1, GFP_KERNEL);
  687. if (buf == NULL)
  688. return -ENOMEM;
  689. if (copy_from_user(buf, buffer, count)) {
  690. count = -EFAULT;
  691. goto out;
  692. }
  693. buf[count] = '\0';
  694. mconsole_notify(notify_socket, MCONSOLE_USER_NOTIFY, buf, count);
  695. out:
  696. kfree(buf);
  697. return count;
  698. }
  699. static const struct file_operations mconsole_proc_fops = {
  700. .owner = THIS_MODULE,
  701. .write = mconsole_proc_write,
  702. .llseek = noop_llseek,
  703. };
  704. static int create_proc_mconsole(void)
  705. {
  706. struct proc_dir_entry *ent;
  707. if (notify_socket == NULL)
  708. return 0;
  709. ent = proc_create("mconsole", 0200, NULL, &mconsole_proc_fops);
  710. if (ent == NULL) {
  711. printk(KERN_INFO "create_proc_mconsole : create_proc_entry "
  712. "failed\n");
  713. return 0;
  714. }
  715. return 0;
  716. }
  717. static DEFINE_SPINLOCK(notify_spinlock);
  718. void lock_notify(void)
  719. {
  720. spin_lock(&notify_spinlock);
  721. }
  722. void unlock_notify(void)
  723. {
  724. spin_unlock(&notify_spinlock);
  725. }
  726. __initcall(create_proc_mconsole);
  727. #define NOTIFY "notify:"
  728. static int mconsole_setup(char *str)
  729. {
  730. if (!strncmp(str, NOTIFY, strlen(NOTIFY))) {
  731. str += strlen(NOTIFY);
  732. notify_socket = str;
  733. }
  734. else printk(KERN_ERR "mconsole_setup : Unknown option - '%s'\n", str);
  735. return 1;
  736. }
  737. __setup("mconsole=", mconsole_setup);
  738. __uml_help(mconsole_setup,
  739. "mconsole=notify:<socket>\n"
  740. " Requests that the mconsole driver send a message to the named Unix\n"
  741. " socket containing the name of the mconsole socket. This also serves\n"
  742. " to notify outside processes when UML has booted far enough to respond\n"
  743. " to mconsole requests.\n\n"
  744. );
  745. static int notify_panic(struct notifier_block *self, unsigned long unused1,
  746. void *ptr)
  747. {
  748. char *message = ptr;
  749. if (notify_socket == NULL)
  750. return 0;
  751. mconsole_notify(notify_socket, MCONSOLE_PANIC, message,
  752. strlen(message) + 1);
  753. return 0;
  754. }
  755. static struct notifier_block panic_exit_notifier = {
  756. .notifier_call = notify_panic,
  757. .next = NULL,
  758. .priority = 1
  759. };
  760. static int add_notifier(void)
  761. {
  762. atomic_notifier_chain_register(&panic_notifier_list,
  763. &panic_exit_notifier);
  764. return 0;
  765. }
  766. __initcall(add_notifier);
  767. char *mconsole_notify_socket(void)
  768. {
  769. return notify_socket;
  770. }
  771. EXPORT_SYMBOL(mconsole_notify_socket);