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> |
Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 9 | #include <linux/module.h> |
Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 10 | #include <linux/proc_fs.h> |
Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 11 | #include <linux/seq_file.h> |
Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 12 | #include <linux/types.h> |
| 13 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 15 | /* |
| 16 | * 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] | 17 | * value. |
| 18 | */ |
| 19 | int uml_exitcode = 0; |
| 20 | |
Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 21 | static int exitcode_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 23 | int val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 25 | /* |
| 26 | * 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] | 27 | * that sprintf accesses it atomically. |
| 28 | */ |
| 29 | val = uml_exitcode; |
Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 30 | seq_printf(m, "%d\n", val); |
| 31 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 34 | static int exitcode_proc_open(struct inode *inode, struct file *file) |
| 35 | { |
| 36 | return single_open(file, exitcode_proc_show, NULL); |
| 37 | } |
| 38 | |
| 39 | static ssize_t exitcode_proc_write(struct file *file, |
| 40 | const char __user *buffer, size_t count, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | char *end, buf[sizeof("nnnnn\0")]; |
Dan Carpenter | 201f99f | 2013-10-29 22:06:04 +0300 | [diff] [blame] | 43 | size_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | int tmp; |
| 45 | |
Dan Carpenter | 201f99f | 2013-10-29 22:06:04 +0300 | [diff] [blame] | 46 | size = min(count, sizeof(buf)); |
| 47 | if (copy_from_user(buf, buffer, size)) |
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 | |
Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 58 | static const struct file_operations exitcode_proc_fops = { |
| 59 | .owner = THIS_MODULE, |
| 60 | .open = exitcode_proc_open, |
| 61 | .read = seq_read, |
| 62 | .llseek = seq_lseek, |
| 63 | .release = single_release, |
| 64 | .write = exitcode_proc_write, |
| 65 | }; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static int make_proc_exitcode(void) |
| 68 | { |
| 69 | struct proc_dir_entry *ent; |
| 70 | |
Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 71 | ent = proc_create("exitcode", 0600, NULL, &exitcode_proc_fops); |
Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 72 | if (ent == NULL) { |
Christophe Lucas | 30f417c | 2005-07-28 21:16:12 -0700 | [diff] [blame] | 73 | printk(KERN_WARNING "make_proc_exitcode : Failed to register " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | "/proc/exitcode\n"); |
Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 75 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 77 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | __initcall(make_proc_exitcode); |