mconsole_kern.c 18 KB

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