mconsole_kern.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
  3. * Copyright (C) 2001 - 2003 Jeff Dike (jdike@addtoit.com)
  4. * Licensed under the GPL
  5. */
  6. #include "linux/kernel.h"
  7. #include "linux/slab.h"
  8. #include "linux/init.h"
  9. #include "linux/notifier.h"
  10. #include "linux/reboot.h"
  11. #include "linux/utsname.h"
  12. #include "linux/ctype.h"
  13. #include "linux/interrupt.h"
  14. #include "linux/sysrq.h"
  15. #include "linux/workqueue.h"
  16. #include "linux/module.h"
  17. #include "linux/file.h"
  18. #include "linux/fs.h"
  19. #include "linux/namei.h"
  20. #include "linux/proc_fs.h"
  21. #include "linux/syscalls.h"
  22. #include "asm/irq.h"
  23. #include "asm/uaccess.h"
  24. #include "user_util.h"
  25. #include "kern_util.h"
  26. #include "kern.h"
  27. #include "mconsole.h"
  28. #include "mconsole_kern.h"
  29. #include "irq_user.h"
  30. #include "init.h"
  31. #include "os.h"
  32. #include "umid.h"
  33. #include "irq_kern.h"
  34. static int do_unlink_socket(struct notifier_block *notifier,
  35. unsigned long what, void *data)
  36. {
  37. return(mconsole_unlink_socket());
  38. }
  39. static struct notifier_block reboot_notifier = {
  40. .notifier_call = do_unlink_socket,
  41. .priority = 0,
  42. };
  43. /* Safe without explicit locking for now. Tasklets provide their own
  44. * locking, and the interrupt handler is safe because it can't interrupt
  45. * itself and it can only happen on CPU 0.
  46. */
  47. LIST_HEAD(mc_requests);
  48. static void mc_work_proc(void *unused)
  49. {
  50. struct mconsole_entry *req;
  51. unsigned long flags;
  52. while(!list_empty(&mc_requests)){
  53. local_save_flags(flags);
  54. req = list_entry(mc_requests.next, struct mconsole_entry,
  55. list);
  56. list_del(&req->list);
  57. local_irq_restore(flags);
  58. req->request.cmd->handler(&req->request);
  59. kfree(req);
  60. }
  61. }
  62. DECLARE_WORK(mconsole_work, mc_work_proc, NULL);
  63. static irqreturn_t mconsole_interrupt(int irq, void *dev_id,
  64. struct pt_regs *regs)
  65. {
  66. /* long to avoid size mismatch warnings from gcc */
  67. long fd;
  68. struct mconsole_entry *new;
  69. struct mc_request req;
  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_ATOMIC);
  76. if(new == NULL)
  77. mconsole_reply(&req, "Out of memory", 1, 0);
  78. else {
  79. new->request = req;
  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", system_utsname.sysname,
  93. system_utsname.nodename, system_utsname.release,
  94. system_utsname.version, system_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("%.*s", len, ptr);
  104. mconsole_reply(req, "", 0, 0);
  105. }
  106. /* This is a more convoluted version of mconsole_proc, which has some stability
  107. * problems; however, we need it fixed, because it is expected that UML users
  108. * mount HPPFS instead of procfs on /proc. And we want mconsole_proc to still
  109. * show the real procfs content, not the ones from hppfs.*/
  110. #if 0
  111. void mconsole_proc(struct mc_request *req)
  112. {
  113. struct nameidata nd;
  114. struct file_system_type *proc;
  115. struct super_block *super;
  116. struct file *file;
  117. int n, err;
  118. char *ptr = req->request.data, *buf;
  119. ptr += strlen("proc");
  120. while(isspace(*ptr)) ptr++;
  121. proc = get_fs_type("proc");
  122. if(proc == NULL){
  123. mconsole_reply(req, "procfs not registered", 1, 0);
  124. goto out;
  125. }
  126. super = (*proc->get_sb)(proc, 0, NULL, NULL);
  127. put_filesystem(proc);
  128. if(super == NULL){
  129. mconsole_reply(req, "Failed to get procfs superblock", 1, 0);
  130. goto out;
  131. }
  132. up_write(&super->s_umount);
  133. nd.dentry = super->s_root;
  134. nd.mnt = NULL;
  135. nd.flags = O_RDONLY + 1;
  136. nd.last_type = LAST_ROOT;
  137. /* START: it was experienced that the stability problems are closed
  138. * if commenting out these two calls + the below read cycle. To
  139. * make UML crash again, it was enough to readd either one.*/
  140. err = link_path_walk(ptr, &nd);
  141. if(err){
  142. mconsole_reply(req, "Failed to look up file", 1, 0);
  143. goto out_kill;
  144. }
  145. file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
  146. if(IS_ERR(file)){
  147. mconsole_reply(req, "Failed to open file", 1, 0);
  148. goto out_kill;
  149. }
  150. /*END*/
  151. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  152. if(buf == NULL){
  153. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  154. goto out_fput;
  155. }
  156. if((file->f_op != NULL) && (file->f_op->read != NULL)){
  157. do {
  158. n = (*file->f_op->read)(file, buf, PAGE_SIZE - 1,
  159. &file->f_pos);
  160. if(n >= 0){
  161. buf[n] = '\0';
  162. mconsole_reply(req, buf, 0, (n > 0));
  163. }
  164. else {
  165. mconsole_reply(req, "Read of file failed",
  166. 1, 0);
  167. goto out_free;
  168. }
  169. } while(n > 0);
  170. }
  171. else mconsole_reply(req, "", 0, 0);
  172. out_free:
  173. kfree(buf);
  174. out_fput:
  175. fput(file);
  176. out_kill:
  177. deactivate_super(super);
  178. out: ;
  179. }
  180. #endif
  181. void mconsole_proc(struct mc_request *req)
  182. {
  183. char path[64];
  184. char *buf;
  185. int len;
  186. int fd;
  187. int first_chunk = 1;
  188. char *ptr = req->request.data;
  189. ptr += strlen("proc");
  190. while(isspace(*ptr)) ptr++;
  191. snprintf(path, sizeof(path), "/proc/%s", ptr);
  192. fd = sys_open(path, 0, 0);
  193. if (fd < 0) {
  194. mconsole_reply(req, "Failed to open file", 1, 0);
  195. printk("open %s: %d\n",path,fd);
  196. goto out;
  197. }
  198. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  199. if(buf == NULL){
  200. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  201. goto out_close;
  202. }
  203. for (;;) {
  204. len = sys_read(fd, buf, PAGE_SIZE-1);
  205. if (len < 0) {
  206. mconsole_reply(req, "Read of file failed", 1, 0);
  207. goto out_free;
  208. }
  209. /*Begin the file content on his own line.*/
  210. if (first_chunk) {
  211. mconsole_reply(req, "\n", 0, 1);
  212. first_chunk = 0;
  213. }
  214. if (len == PAGE_SIZE-1) {
  215. buf[len] = '\0';
  216. mconsole_reply(req, buf, 0, 1);
  217. } else {
  218. buf[len] = '\0';
  219. mconsole_reply(req, buf, 0, 0);
  220. break;
  221. }
  222. }
  223. out_free:
  224. kfree(buf);
  225. out_close:
  226. sys_close(fd);
  227. out:
  228. /* nothing */;
  229. }
  230. #define UML_MCONSOLE_HELPTEXT \
  231. "Commands: \n\
  232. version - Get kernel version \n\
  233. help - Print this message \n\
  234. halt - Halt UML \n\
  235. reboot - Reboot UML \n\
  236. config <dev>=<config> - Add a new device to UML; \n\
  237. same syntax as command line \n\
  238. config <dev> - Query the configuration of a device \n\
  239. remove <dev> - Remove a device from UML \n\
  240. sysrq <letter> - Performs the SysRq action controlled by the letter \n\
  241. cad - invoke the Ctl-Alt-Del handler \n\
  242. stop - pause the UML; it will do nothing until it receives a 'go' \n\
  243. go - continue the UML after a 'stop' \n\
  244. log <string> - make UML enter <string> into the kernel log\n\
  245. proc <file> - returns the contents of the UML's /proc/<file>\n\
  246. "
  247. void mconsole_help(struct mc_request *req)
  248. {
  249. mconsole_reply(req, UML_MCONSOLE_HELPTEXT, 0, 0);
  250. }
  251. void mconsole_halt(struct mc_request *req)
  252. {
  253. mconsole_reply(req, "", 0, 0);
  254. machine_halt();
  255. }
  256. void mconsole_reboot(struct mc_request *req)
  257. {
  258. mconsole_reply(req, "", 0, 0);
  259. machine_restart(NULL);
  260. }
  261. extern void ctrl_alt_del(void);
  262. void mconsole_cad(struct mc_request *req)
  263. {
  264. mconsole_reply(req, "", 0, 0);
  265. ctrl_alt_del();
  266. }
  267. void mconsole_go(struct mc_request *req)
  268. {
  269. mconsole_reply(req, "Not stopped", 1, 0);
  270. }
  271. void mconsole_stop(struct mc_request *req)
  272. {
  273. deactivate_fd(req->originating_fd, MCONSOLE_IRQ);
  274. os_set_fd_block(req->originating_fd, 1);
  275. mconsole_reply(req, "", 0, 0);
  276. while(mconsole_get_request(req->originating_fd, req)){
  277. if(req->cmd->handler == mconsole_go) break;
  278. (*req->cmd->handler)(req);
  279. }
  280. os_set_fd_block(req->originating_fd, 0);
  281. reactivate_fd(req->originating_fd, MCONSOLE_IRQ);
  282. mconsole_reply(req, "", 0, 0);
  283. }
  284. /* This list is populated by __initcall routines. */
  285. LIST_HEAD(mconsole_devices);
  286. void mconsole_register_dev(struct mc_device *new)
  287. {
  288. list_add(&new->list, &mconsole_devices);
  289. }
  290. static struct mc_device *mconsole_find_dev(char *name)
  291. {
  292. struct list_head *ele;
  293. struct mc_device *dev;
  294. list_for_each(ele, &mconsole_devices){
  295. dev = list_entry(ele, struct mc_device, list);
  296. if(!strncmp(name, dev->name, strlen(dev->name)))
  297. return(dev);
  298. }
  299. return(NULL);
  300. }
  301. #define CONFIG_BUF_SIZE 64
  302. static void mconsole_get_config(int (*get_config)(char *, char *, int,
  303. char **),
  304. struct mc_request *req, char *name)
  305. {
  306. char default_buf[CONFIG_BUF_SIZE], *error, *buf;
  307. int n, size;
  308. if(get_config == NULL){
  309. mconsole_reply(req, "No get_config routine defined", 1, 0);
  310. return;
  311. }
  312. error = NULL;
  313. size = sizeof(default_buf)/sizeof(default_buf[0]);
  314. buf = default_buf;
  315. while(1){
  316. n = (*get_config)(name, buf, size, &error);
  317. if(error != NULL){
  318. mconsole_reply(req, error, 1, 0);
  319. goto out;
  320. }
  321. if(n <= size){
  322. mconsole_reply(req, buf, 0, 0);
  323. goto out;
  324. }
  325. if(buf != default_buf)
  326. kfree(buf);
  327. size = n;
  328. buf = kmalloc(size, GFP_KERNEL);
  329. if(buf == NULL){
  330. mconsole_reply(req, "Failed to allocate buffer", 1, 0);
  331. return;
  332. }
  333. }
  334. out:
  335. if(buf != default_buf)
  336. kfree(buf);
  337. }
  338. void mconsole_config(struct mc_request *req)
  339. {
  340. struct mc_device *dev;
  341. char *ptr = req->request.data, *name;
  342. int err;
  343. ptr += strlen("config");
  344. while(isspace(*ptr)) ptr++;
  345. dev = mconsole_find_dev(ptr);
  346. if(dev == NULL){
  347. mconsole_reply(req, "Bad configuration option", 1, 0);
  348. return;
  349. }
  350. name = &ptr[strlen(dev->name)];
  351. ptr = name;
  352. while((*ptr != '=') && (*ptr != '\0'))
  353. ptr++;
  354. if(*ptr == '='){
  355. err = (*dev->config)(name);
  356. mconsole_reply(req, "", err, 0);
  357. }
  358. else mconsole_get_config(dev->get_config, req, name);
  359. }
  360. void mconsole_remove(struct mc_request *req)
  361. {
  362. struct mc_device *dev;
  363. char *ptr = req->request.data;
  364. int err;
  365. ptr += strlen("remove");
  366. while(isspace(*ptr)) ptr++;
  367. dev = mconsole_find_dev(ptr);
  368. if(dev == NULL){
  369. mconsole_reply(req, "Bad remove option", 1, 0);
  370. return;
  371. }
  372. err = (*dev->remove)(&ptr[strlen(dev->name)]);
  373. mconsole_reply(req, "", err, 0);
  374. }
  375. #ifdef CONFIG_MAGIC_SYSRQ
  376. void mconsole_sysrq(struct mc_request *req)
  377. {
  378. char *ptr = req->request.data;
  379. ptr += strlen("sysrq");
  380. while(isspace(*ptr)) ptr++;
  381. mconsole_reply(req, "", 0, 0);
  382. handle_sysrq(*ptr, &current->thread.regs, NULL);
  383. }
  384. #else
  385. void mconsole_sysrq(struct mc_request *req)
  386. {
  387. mconsole_reply(req, "Sysrq not compiled in", 1, 0);
  388. }
  389. #endif
  390. /* Changed by mconsole_setup, which is __setup, and called before SMP is
  391. * active.
  392. */
  393. static char *notify_socket = NULL;
  394. int mconsole_init(void)
  395. {
  396. /* long to avoid size mismatch warnings from gcc */
  397. long sock;
  398. int err;
  399. char file[256];
  400. if(umid_file_name("mconsole", file, sizeof(file))) return(-1);
  401. snprintf(mconsole_socket_name, sizeof(file), "%s", file);
  402. sock = os_create_unix_socket(file, sizeof(file), 1);
  403. if (sock < 0){
  404. printk("Failed to initialize management console\n");
  405. return(1);
  406. }
  407. register_reboot_notifier(&reboot_notifier);
  408. err = um_request_irq(MCONSOLE_IRQ, sock, IRQ_READ, mconsole_interrupt,
  409. SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
  410. "mconsole", (void *)sock);
  411. if (err){
  412. printk("Failed to get IRQ for management console\n");
  413. return(1);
  414. }
  415. if(notify_socket != NULL){
  416. notify_socket = uml_strdup(notify_socket);
  417. if(notify_socket != NULL)
  418. mconsole_notify(notify_socket, MCONSOLE_SOCKET,
  419. mconsole_socket_name,
  420. strlen(mconsole_socket_name) + 1);
  421. else printk(KERN_ERR "mconsole_setup failed to strdup "
  422. "string\n");
  423. }
  424. printk("mconsole (version %d) initialized on %s\n",
  425. MCONSOLE_VERSION, mconsole_socket_name);
  426. return(0);
  427. }
  428. __initcall(mconsole_init);
  429. static int write_proc_mconsole(struct file *file, const char __user *buffer,
  430. unsigned long count, void *data)
  431. {
  432. char *buf;
  433. buf = kmalloc(count + 1, GFP_KERNEL);
  434. if(buf == NULL)
  435. return(-ENOMEM);
  436. if(copy_from_user(buf, buffer, count)){
  437. count = -EFAULT;
  438. goto out;
  439. }
  440. buf[count] = '\0';
  441. mconsole_notify(notify_socket, MCONSOLE_USER_NOTIFY, buf, count);
  442. out:
  443. kfree(buf);
  444. return(count);
  445. }
  446. static int create_proc_mconsole(void)
  447. {
  448. struct proc_dir_entry *ent;
  449. if(notify_socket == NULL) return(0);
  450. ent = create_proc_entry("mconsole", S_IFREG | 0200, NULL);
  451. if(ent == NULL){
  452. printk("create_proc_mconsole : create_proc_entry failed\n");
  453. return(0);
  454. }
  455. ent->read_proc = NULL;
  456. ent->write_proc = write_proc_mconsole;
  457. return(0);
  458. }
  459. static DEFINE_SPINLOCK(notify_spinlock);
  460. void lock_notify(void)
  461. {
  462. spin_lock(&notify_spinlock);
  463. }
  464. void unlock_notify(void)
  465. {
  466. spin_unlock(&notify_spinlock);
  467. }
  468. __initcall(create_proc_mconsole);
  469. #define NOTIFY "=notify:"
  470. static int mconsole_setup(char *str)
  471. {
  472. if(!strncmp(str, NOTIFY, strlen(NOTIFY))){
  473. str += strlen(NOTIFY);
  474. notify_socket = str;
  475. }
  476. else printk(KERN_ERR "mconsole_setup : Unknown option - '%s'\n", str);
  477. return(1);
  478. }
  479. __setup("mconsole", mconsole_setup);
  480. __uml_help(mconsole_setup,
  481. "mconsole=notify:<socket>\n"
  482. " Requests that the mconsole driver send a message to the named Unix\n"
  483. " socket containing the name of the mconsole socket. This also serves\n"
  484. " to notify outside processes when UML has booted far enough to respond\n"
  485. " to mconsole requests.\n\n"
  486. );
  487. static int notify_panic(struct notifier_block *self, unsigned long unused1,
  488. void *ptr)
  489. {
  490. char *message = ptr;
  491. if(notify_socket == NULL) return(0);
  492. mconsole_notify(notify_socket, MCONSOLE_PANIC, message,
  493. strlen(message) + 1);
  494. return(0);
  495. }
  496. static struct notifier_block panic_exit_notifier = {
  497. .notifier_call = notify_panic,
  498. .next = NULL,
  499. .priority = 1
  500. };
  501. static int add_notifier(void)
  502. {
  503. notifier_chain_register(&panic_notifier_list, &panic_exit_notifier);
  504. return(0);
  505. }
  506. __initcall(add_notifier);
  507. char *mconsole_notify_socket(void)
  508. {
  509. return(notify_socket);
  510. }
  511. EXPORT_SYMBOL(mconsole_notify_socket);
  512. /*
  513. * Overrides for Emacs so that we follow Linus's tabbing style.
  514. * Emacs will notice this stuff at the end of the file and automatically
  515. * adjust the settings for this buffer only. This must remain at the end
  516. * of the file.
  517. * ---------------------------------------------------------------------------
  518. * Local variables:
  519. * c-file-style: "linux"
  520. * End:
  521. */