blob: c716b5a6db13d9552a59e7f7ec5688229ca9bf0e [file] [log] [blame]
Jeff Dikee16f5352007-06-08 13:46:54 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
Jeff Dikee16f5352007-06-08 13:46:54 -07006#include "linux/kernel.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/init.h"
8#include "linux/ctype.h"
9#include "linux/proc_fs.h"
10#include "asm/uaccess.h"
11
12/* If read and write race, the read will still atomically read a valid
13 * value.
14 */
15int uml_exitcode = 0;
16
17static int read_proc_exitcode(char *page, char **start, off_t off,
18 int count, int *eof, void *data)
19{
Jeff Dike730760e2006-09-29 01:58:50 -070020 int len, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Jeff Dike730760e2006-09-29 01:58:50 -070022 /* Save uml_exitcode in a local so that we don't need to guarantee
23 * that sprintf accesses it atomically.
24 */
25 val = uml_exitcode;
26 len = sprintf(page, "%d\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 len -= off;
Jeff Dikee16f5352007-06-08 13:46:54 -070028 if(len <= off+count)
29 *eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *start = page + off;
Jeff Dikee16f5352007-06-08 13:46:54 -070031 if(len > count)
32 len = count;
33 if(len < 0)
34 len = 0;
35 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38static int write_proc_exitcode(struct file *file, const char __user *buffer,
39 unsigned long count, void *data)
40{
41 char *end, buf[sizeof("nnnnn\0")];
42 int tmp;
43
44 if(copy_from_user(buf, buffer, count))
Jeff Dikee16f5352007-06-08 13:46:54 -070045 return -EFAULT;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 tmp = simple_strtol(buf, &end, 0);
48 if((*end != '\0') && !isspace(*end))
Jeff Dikee16f5352007-06-08 13:46:54 -070049 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 uml_exitcode = tmp;
Jeff Dikee16f5352007-06-08 13:46:54 -070052 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55static int make_proc_exitcode(void)
56{
57 struct proc_dir_entry *ent;
58
59 ent = create_proc_entry("exitcode", 0600, &proc_root);
60 if(ent == NULL){
Christophe Lucas30f417c2005-07-28 21:16:12 -070061 printk(KERN_WARNING "make_proc_exitcode : Failed to register "
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 "/proc/exitcode\n");
Jeff Dikee16f5352007-06-08 13:46:54 -070063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65
66 ent->read_proc = read_proc_exitcode;
67 ent->write_proc = write_proc_exitcode;
Jeff Dikee16f5352007-06-08 13:46:54 -070068
69 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72__initcall(make_proc_exitcode);