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