ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / drivers / mconsole_kern.c
1 /*
2  * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
3  * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.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/proc_fs.h"
19 #include "asm/irq.h"
20 #include "asm/uaccess.h"
21 #include "user_util.h"
22 #include "kern_util.h"
23 #include "kern.h"
24 #include "mconsole.h"
25 #include "mconsole_kern.h"
26 #include "irq_user.h"
27 #include "init.h"
28 #include "os.h"
29 #include "umid.h"
30
31 static int do_unlink_socket(struct notifier_block *notifier, 
32                             unsigned long what, void *data)
33 {
34         return(mconsole_unlink_socket());
35 }
36
37
38 static struct notifier_block reboot_notifier = {
39         .notifier_call          = do_unlink_socket,
40         .priority               = 0,
41 };
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
48 LIST_HEAD(mc_requests);
49
50 void mc_work_proc(void *unused)
51 {
52         struct mconsole_entry *req;
53         unsigned long flags;
54         int done;
55
56         do {
57                 local_save_flags(flags);
58                 req = list_entry(mc_requests.next, struct mconsole_entry, 
59                                  list);
60                 list_del(&req->list);
61                 done = list_empty(&mc_requests);
62                 local_irq_restore(flags);
63                 req->request.cmd->handler(&req->request);
64                 kfree(req);
65         } while(!done);
66 }
67
68 DECLARE_WORK(mconsole_work, mc_work_proc, NULL);
69
70 void mconsole_interrupt(int irq, void *dev_id, struct pt_regs *regs)
71 {
72         int fd;
73         struct mconsole_entry *new;
74         struct mc_request req;
75
76         fd = (int) dev_id;
77         while (mconsole_get_request(fd, &req)){
78                 if(req.cmd->as_interrupt) (*req.cmd->handler)(&req);
79                 else {
80                         new = kmalloc(sizeof(req), GFP_ATOMIC);
81                         if(new == NULL)
82                                 mconsole_reply(&req, "Out of memory", 1, 0);
83                         else {
84                                 new->request = req;
85                                 list_add(&new->list, &mc_requests);
86                         }
87                 }
88         }
89         if(!list_empty(&mc_requests)) schedule_work(&mconsole_work);
90         reactivate_fd(fd, MCONSOLE_IRQ);
91 }
92
93 void mconsole_version(struct mc_request *req)
94 {
95         char version[256];
96
97         sprintf(version, "%s %s %s %s %s", system_utsname.sysname, 
98                 system_utsname.nodename, system_utsname.release, 
99                 system_utsname.version, system_utsname.machine);
100         mconsole_reply(req, version, 0, 0);
101 }
102
103 #define UML_MCONSOLE_HELPTEXT \
104 "Commands:
105     version - Get kernel version
106     help - Print this message
107     halt - Halt UML
108     reboot - Reboot UML
109     config <dev>=<config> - Add a new device to UML; 
110         same syntax as command line
111     config <dev> - Query the configuration of a device
112     remove <dev> - Remove a device from UML
113     sysrq <letter> - Performs the SysRq action controlled by the letter
114     cad - invoke the Ctl-Alt-Del handler
115     stop - pause the UML; it will do nothing until it receives a 'go'
116     go - continue the UML after a 'stop'
117 "
118
119 void mconsole_help(struct mc_request *req)
120 {
121         mconsole_reply(req, UML_MCONSOLE_HELPTEXT, 0, 0);
122 }
123
124 void mconsole_halt(struct mc_request *req)
125 {
126         mconsole_reply(req, "", 0, 0);
127         machine_halt();
128 }
129
130 void mconsole_reboot(struct mc_request *req)
131 {
132         mconsole_reply(req, "", 0, 0);
133         machine_restart(NULL);
134 }
135
136 extern void ctrl_alt_del(void);
137
138 void mconsole_cad(struct mc_request *req)
139 {
140         mconsole_reply(req, "", 0, 0);
141         ctrl_alt_del();
142 }
143
144 void mconsole_go(struct mc_request *req)
145 {
146         mconsole_reply(req, "Not stopped", 1, 0);
147 }
148
149 void mconsole_stop(struct mc_request *req)
150 {
151         deactivate_fd(req->originating_fd, MCONSOLE_IRQ);
152         os_set_fd_block(req->originating_fd, 1);
153         mconsole_reply(req, "", 0, 0);
154         while(mconsole_get_request(req->originating_fd, req)){
155                 if(req->cmd->handler == mconsole_go) break;
156                 (*req->cmd->handler)(req);
157         }
158         os_set_fd_block(req->originating_fd, 0);
159         reactivate_fd(req->originating_fd, MCONSOLE_IRQ);
160         mconsole_reply(req, "", 0, 0);
161 }
162
163 /* This list is populated by __initcall routines. */
164
165 LIST_HEAD(mconsole_devices);
166
167 void mconsole_register_dev(struct mc_device *new)
168 {
169         list_add(&new->list, &mconsole_devices);
170 }
171
172 static struct mc_device *mconsole_find_dev(char *name)
173 {
174         struct list_head *ele;
175         struct mc_device *dev;
176
177         list_for_each(ele, &mconsole_devices){
178                 dev = list_entry(ele, struct mc_device, list);
179                 if(!strncmp(name, dev->name, strlen(dev->name)))
180                         return(dev);
181         }
182         return(NULL);
183 }
184
185 #define CONFIG_BUF_SIZE 64
186
187 static void mconsole_get_config(int (*get_config)(char *, char *, int, 
188                                                   char **),
189                                 struct mc_request *req, char *name)
190 {
191         char default_buf[CONFIG_BUF_SIZE], *error, *buf;
192         int n, size;
193
194         if(get_config == NULL){
195                 mconsole_reply(req, "No get_config routine defined", 1, 0);
196                 return;
197         }
198
199         error = NULL;
200         size = sizeof(default_buf)/sizeof(default_buf[0]);
201         buf = default_buf;
202
203         while(1){
204                 n = (*get_config)(name, buf, size, &error);
205                 if(error != NULL){
206                         mconsole_reply(req, error, 1, 0);
207                         goto out;
208                 }
209
210                 if(n <= size){
211                         mconsole_reply(req, buf, 0, 0);
212                         goto out;
213                 }
214
215                 if(buf != default_buf)
216                         kfree(buf);
217
218                 size = n;
219                 buf = kmalloc(size, GFP_KERNEL);
220                 if(buf == NULL){
221                         mconsole_reply(req, "Failed to allocate buffer", 1, 0);
222                         return;
223                 }
224         }
225  out:
226         if(buf != default_buf)
227                 kfree(buf);
228         
229 }
230
231 void mconsole_config(struct mc_request *req)
232 {
233         struct mc_device *dev;
234         char *ptr = req->request.data, *name;
235         int err;
236
237         ptr += strlen("config");
238         while(isspace(*ptr)) ptr++;
239         dev = mconsole_find_dev(ptr);
240         if(dev == NULL){
241                 mconsole_reply(req, "Bad configuration option", 1, 0);
242                 return;
243         }
244
245         name = &ptr[strlen(dev->name)];
246         ptr = name;
247         while((*ptr != '=') && (*ptr != '\0'))
248                 ptr++;
249
250         if(*ptr == '='){
251                 err = (*dev->config)(name);
252                 mconsole_reply(req, "", err, 0);
253         }
254         else mconsole_get_config(dev->get_config, req, name);
255 }
256
257 void mconsole_remove(struct mc_request *req)
258 {
259         struct mc_device *dev;  
260         char *ptr = req->request.data;
261         int err;
262
263         ptr += strlen("remove");
264         while(isspace(*ptr)) ptr++;
265         dev = mconsole_find_dev(ptr);
266         if(dev == NULL){
267                 mconsole_reply(req, "Bad remove option", 1, 0);
268                 return;
269         }
270         err = (*dev->remove)(&ptr[strlen(dev->name)]);
271         mconsole_reply(req, "", err, 0);
272 }
273
274 #ifdef CONFIG_MAGIC_SYSRQ
275 void mconsole_sysrq(struct mc_request *req)
276 {
277         char *ptr = req->request.data;
278
279         ptr += strlen("sysrq");
280         while(isspace(*ptr)) ptr++;
281
282         handle_sysrq(*ptr, &current->thread.regs, NULL);
283         mconsole_reply(req, "", 0, 0);
284 }
285 #else
286 void mconsole_sysrq(struct mc_request *req)
287 {
288         mconsole_reply(req, "Sysrq not compiled in", 1, 0);
289 }
290 #endif
291
292 /* Changed by mconsole_setup, which is __setup, and called before SMP is
293  * active.
294  */
295 static char *notify_socket = NULL; 
296
297 int mconsole_init(void)
298 {
299         int err, sock;
300         char file[256];
301
302         if(umid_file_name("mconsole", file, sizeof(file))) return(-1);
303         snprintf(mconsole_socket_name, sizeof(file), "%s", file);
304
305         sock = create_unix_socket(file, sizeof(file));
306         if (sock < 0){
307                 printk("Failed to initialize management console\n");
308                 return(1);
309         }
310
311         register_reboot_notifier(&reboot_notifier);
312
313         err = um_request_irq(MCONSOLE_IRQ, sock, IRQ_READ, mconsole_interrupt,
314                              SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
315                              "mconsole", (void *)sock);
316         if (err){
317                 printk("Failed to get IRQ for management console\n");
318                 return(1);
319         }
320
321         if(notify_socket != NULL){
322                 notify_socket = uml_strdup(notify_socket);
323                 if(notify_socket != NULL)
324                         mconsole_notify(notify_socket, MCONSOLE_SOCKET,
325                                         mconsole_socket_name, 
326                                         strlen(mconsole_socket_name) + 1);
327                 else printk(KERN_ERR "mconsole_setup failed to strdup "
328                             "string\n");
329         }
330
331         printk("mconsole (version %d) initialized on %s\n", 
332                MCONSOLE_VERSION, mconsole_socket_name);
333         return(0);
334 }
335
336 __initcall(mconsole_init);
337
338 static int write_proc_mconsole(struct file *file, const char *buffer,
339                                unsigned long count, void *data)
340 {
341         char *buf;
342
343         buf = kmalloc(count + 1, GFP_KERNEL);
344         if(buf == NULL) 
345                 return(-ENOMEM);
346
347         if(copy_from_user(buf, buffer, count))
348                 return(-EFAULT);
349         buf[count] = '\0';
350
351         mconsole_notify(notify_socket, MCONSOLE_USER_NOTIFY, buf, count);
352         return(count);
353 }
354
355 static int create_proc_mconsole(void)
356 {
357         struct proc_dir_entry *ent;
358
359         if(notify_socket == NULL) return(0);
360
361         ent = create_proc_entry("mconsole", S_IFREG | 0200, NULL);
362         if(ent == NULL){
363                 printk("create_proc_mconsole : create_proc_entry failed\n");
364                 return(0);
365         }
366
367         ent->read_proc = NULL;
368         ent->write_proc = write_proc_mconsole;
369         return(0);
370 }
371
372 static spinlock_t notify_spinlock = SPIN_LOCK_UNLOCKED;
373
374 void lock_notify(void)
375 {
376         spin_lock(&notify_spinlock);
377 }
378
379 void unlock_notify(void)
380 {
381         spin_unlock(&notify_spinlock);
382 }
383
384 __initcall(create_proc_mconsole);
385
386 #define NOTIFY "=notify:"
387
388 static int mconsole_setup(char *str)
389 {
390         if(!strncmp(str, NOTIFY, strlen(NOTIFY))){
391                 str += strlen(NOTIFY);
392                 notify_socket = str;
393         }
394         else printk(KERN_ERR "mconsole_setup : Unknown option - '%s'\n", str);
395         return(1);
396 }
397
398 __setup("mconsole", mconsole_setup);
399
400 __uml_help(mconsole_setup,
401 "mconsole=notify:<socket>\n"
402 "    Requests that the mconsole driver send a message to the named Unix\n"
403 "    socket containing the name of the mconsole socket.  This also serves\n"
404 "    to notify outside processes when UML has booted far enough to respond\n"
405 "    to mconsole requests.\n\n"
406 );
407
408 static int notify_panic(struct notifier_block *self, unsigned long unused1,
409                         void *ptr)
410 {
411         char *message = ptr;
412
413         if(notify_socket == NULL) return(0);
414
415         mconsole_notify(notify_socket, MCONSOLE_PANIC, message, 
416                         strlen(message) + 1);
417         return(0);
418 }
419
420 static struct notifier_block panic_exit_notifier = {
421         .notifier_call          = notify_panic,
422         .next                   = NULL,
423         .priority               = 1
424 };
425
426 static int add_notifier(void)
427 {
428         notifier_chain_register(&panic_notifier_list, &panic_exit_notifier);
429         return(0);
430 }
431
432 __initcall(add_notifier);
433
434 char *mconsole_notify_socket(void)
435 {
436         return(notify_socket);
437 }
438
439 EXPORT_SYMBOL(mconsole_notify_socket);
440
441 /*
442  * Overrides for Emacs so that we follow Linus's tabbing style.
443  * Emacs will notice this stuff at the end of the file and automatically
444  * adjust the settings for this buffer only.  This must remain at the end
445  * of the file.
446  * ---------------------------------------------------------------------------
447  * Local variables:
448  * c-file-style: "linux"
449  * End:
450  */