mconsole_kern.c 20 KB

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