| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 1 | /* | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL | 
|  | 4 | */ | 
|  | 5 |  | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 6 | #include <linux/ctype.h> | 
|  | 7 | #include <linux/init.h> | 
|  | 8 | #include <linux/kernel.h> | 
|  | 9 | #include <linux/proc_fs.h> | 
|  | 10 | #include <linux/types.h> | 
|  | 11 | #include <asm/uaccess.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 |  | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 13 | /* | 
|  | 14 | * If read and write race, the read will still atomically read a valid | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * value. | 
|  | 16 | */ | 
|  | 17 | int uml_exitcode = 0; | 
|  | 18 |  | 
|  | 19 | static int read_proc_exitcode(char *page, char **start, off_t off, | 
|  | 20 | int count, int *eof, void *data) | 
|  | 21 | { | 
| Jeff Dike | 730760e | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 22 | int len, val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 |  | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 24 | /* | 
|  | 25 | * Save uml_exitcode in a local so that we don't need to guarantee | 
| Jeff Dike | 730760e | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 26 | * that sprintf accesses it atomically. | 
|  | 27 | */ | 
|  | 28 | val = uml_exitcode; | 
|  | 29 | len = sprintf(page, "%d\n", val); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | len -= off; | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 31 | if (len <= off+count) | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 32 | *eof = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | *start = page + off; | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 34 | if (len > count) | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 35 | len = count; | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 36 | if (len < 0) | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 37 | len = 0; | 
|  | 38 | return len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } | 
|  | 40 |  | 
|  | 41 | static int write_proc_exitcode(struct file *file, const char __user *buffer, | 
|  | 42 | unsigned long count, void *data) | 
|  | 43 | { | 
|  | 44 | char *end, buf[sizeof("nnnnn\0")]; | 
|  | 45 | int tmp; | 
|  | 46 |  | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 47 | if (copy_from_user(buf, buffer, count)) | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 48 | return -EFAULT; | 
|  | 49 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | tmp = simple_strtol(buf, &end, 0); | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 51 | if ((*end != '\0') && !isspace(*end)) | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 52 | return -EINVAL; | 
|  | 53 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | uml_exitcode = tmp; | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 55 | return count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
|  | 58 | static int make_proc_exitcode(void) | 
|  | 59 | { | 
|  | 60 | struct proc_dir_entry *ent; | 
|  | 61 |  | 
| Alexey Dobriyan | c74c120 | 2008-04-29 01:01:44 -0700 | [diff] [blame] | 62 | ent = create_proc_entry("exitcode", 0600, NULL); | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 63 | if (ent == NULL) { | 
| Christophe Lucas | 30f417c | 2005-07-28 21:16:12 -0700 | [diff] [blame] | 64 | printk(KERN_WARNING "make_proc_exitcode : Failed to register " | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | "/proc/exitcode\n"); | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 66 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
|  | 69 | ent->read_proc = read_proc_exitcode; | 
|  | 70 | ent->write_proc = write_proc_exitcode; | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
|  | 75 | __initcall(make_proc_exitcode); |