blob: 984f80e668ca5d7c85b3f8ae549dc13fa5aabe57 [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>
9#include <linux/proc_fs.h>
10#include <linux/types.h>
11#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Jeff Dikec5d4bb12008-02-04 22:31:14 -080013/*
14 * If read and write race, the read will still atomically read a valid
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * value.
16 */
17int uml_exitcode = 0;
18
19static int read_proc_exitcode(char *page, char **start, off_t off,
20 int count, int *eof, void *data)
21{
Jeff Dike730760e2006-09-29 01:58:50 -070022 int len, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jeff Dikec5d4bb12008-02-04 22:31:14 -080024 /*
25 * Save uml_exitcode in a local so that we don't need to guarantee
Jeff Dike730760e2006-09-29 01:58:50 -070026 * that sprintf accesses it atomically.
27 */
28 val = uml_exitcode;
29 len = sprintf(page, "%d\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 len -= off;
Jeff Dikec5d4bb12008-02-04 22:31:14 -080031 if (len <= off+count)
Jeff Dikee16f5352007-06-08 13:46:54 -070032 *eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *start = page + off;
Jeff Dikec5d4bb12008-02-04 22:31:14 -080034 if (len > count)
Jeff Dikee16f5352007-06-08 13:46:54 -070035 len = count;
Jeff Dikec5d4bb12008-02-04 22:31:14 -080036 if (len < 0)
Jeff Dikee16f5352007-06-08 13:46:54 -070037 len = 0;
38 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41static 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 Dikec5d4bb12008-02-04 22:31:14 -080047 if (copy_from_user(buf, buffer, count))
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
58static int make_proc_exitcode(void)
59{
60 struct proc_dir_entry *ent;
61
62 ent = create_proc_entry("exitcode", 0600, &proc_root);
Jeff Dikec5d4bb12008-02-04 22:31:14 -080063 if (ent == NULL) {
Christophe Lucas30f417c2005-07-28 21:16:12 -070064 printk(KERN_WARNING "make_proc_exitcode : Failed to register "
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 "/proc/exitcode\n");
Jeff Dikee16f5352007-06-08 13:46:54 -070066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68
69 ent->read_proc = read_proc_exitcode;
70 ent->write_proc = write_proc_exitcode;
Jeff Dikee16f5352007-06-08 13:46:54 -070071
72 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75__initcall(make_proc_exitcode);