mconsole_kern.c 18 KB

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