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