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