blob: 41ebbfebb3332f2bc7269a754ff96b377e17212b [file] [log] [blame]
Jeff Dikee16f5352007-06-08 13:46:54 -07001/*
Jeff Dikec5d4bb12008-02-04 22:31:14 -08002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dikec5d4bb12008-02-04 22:31:14 -08006#include <linux/ctype.h>
7#include <linux/init.h>
8#include <linux/kernel.h>
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -08009#include <linux/module.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080010#include <linux/proc_fs.h>
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080011#include <linux/seq_file.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080012#include <linux/types.h>
13#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Jeff Dikec5d4bb12008-02-04 22:31:14 -080015/*
16 * If read and write race, the read will still atomically read a valid
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * value.
18 */
19int uml_exitcode = 0;
20
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080021static int exitcode_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080023 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Jeff Dikec5d4bb12008-02-04 22:31:14 -080025 /*
26 * Save uml_exitcode in a local so that we don't need to guarantee
Jeff Dike730760e2006-09-29 01:58:50 -070027 * that sprintf accesses it atomically.
28 */
29 val = uml_exitcode;
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080030 seq_printf(m, "%d\n", val);
31 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080034static int exitcode_proc_open(struct inode *inode, struct file *file)
35{
36 return single_open(file, exitcode_proc_show, NULL);
37}
38
39static ssize_t exitcode_proc_write(struct file *file,
40 const char __user *buffer, size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 char *end, buf[sizeof("nnnnn\0")];
Dan Carpenter201f99f2013-10-29 22:06:04 +030043 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int tmp;
45
Dan Carpenter201f99f2013-10-29 22:06:04 +030046 size = min(count, sizeof(buf));
47 if (copy_from_user(buf, buffer, size))
Jeff Dikee16f5352007-06-08 13:46:54 -070048 return -EFAULT;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 tmp = simple_strtol(buf, &end, 0);
Jeff Dikec5d4bb12008-02-04 22:31:14 -080051 if ((*end != '\0') && !isspace(*end))
Jeff Dikee16f5352007-06-08 13:46:54 -070052 return -EINVAL;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 uml_exitcode = tmp;
Jeff Dikee16f5352007-06-08 13:46:54 -070055 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080058static 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 Torvalds1da177e2005-04-16 15:20:36 -070067static int make_proc_exitcode(void)
68{
69 struct proc_dir_entry *ent;
70
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080071 ent = proc_create("exitcode", 0600, NULL, &exitcode_proc_fops);
Jeff Dikec5d4bb12008-02-04 22:31:14 -080072 if (ent == NULL) {
Christophe Lucas30f417c2005-07-28 21:16:12 -070073 printk(KERN_WARNING "make_proc_exitcode : Failed to register "
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 "/proc/exitcode\n");
Jeff Dikee16f5352007-06-08 13:46:54 -070075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
Jeff Dikee16f5352007-06-08 13:46:54 -070077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80__initcall(make_proc_exitcode);