blob: 829df49dee99c6093d5586d0e2c19e34786dfa97 [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")];
43 int tmp;
44
Jeff Dikec5d4bb12008-02-04 22:31:14 -080045 if (copy_from_user(buf, buffer, count))
Jeff Dikee16f5352007-06-08 13:46:54 -070046 return -EFAULT;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 tmp = simple_strtol(buf, &end, 0);
Jeff Dikec5d4bb12008-02-04 22:31:14 -080049 if ((*end != '\0') && !isspace(*end))
Jeff Dikee16f5352007-06-08 13:46:54 -070050 return -EINVAL;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 uml_exitcode = tmp;
Jeff Dikee16f5352007-06-08 13:46:54 -070053 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080056static const struct file_operations exitcode_proc_fops = {
57 .owner = THIS_MODULE,
58 .open = exitcode_proc_open,
59 .read = seq_read,
60 .llseek = seq_lseek,
61 .release = single_release,
62 .write = exitcode_proc_write,
63};
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int make_proc_exitcode(void)
66{
67 struct proc_dir_entry *ent;
68
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080069 ent = proc_create("exitcode", 0600, NULL, &exitcode_proc_fops);
Jeff Dikec5d4bb12008-02-04 22:31:14 -080070 if (ent == NULL) {
Christophe Lucas30f417c2005-07-28 21:16:12 -070071 printk(KERN_WARNING "make_proc_exitcode : Failed to register "
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 "/proc/exitcode\n");
Jeff Dikee16f5352007-06-08 13:46:54 -070073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
Jeff Dikee16f5352007-06-08 13:46:54 -070075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78__initcall(make_proc_exitcode);