exitcode.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/ctype.h>
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/types.h>
  12. #include <asm/uaccess.h>
  13. /*
  14. * If read and write race, the read will still atomically read a valid
  15. * value.
  16. */
  17. int uml_exitcode = 0;
  18. static int exitcode_proc_show(struct seq_file *m, void *v)
  19. {
  20. int val;
  21. /*
  22. * 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. seq_printf(m, "%d\n", val);
  27. return 0;
  28. }
  29. static int exitcode_proc_open(struct inode *inode, struct file *file)
  30. {
  31. return single_open(file, exitcode_proc_show, NULL);
  32. }
  33. static ssize_t exitcode_proc_write(struct file *file,
  34. const char __user *buffer, size_t count, loff_t *pos)
  35. {
  36. char *end, buf[sizeof("nnnnn\0")];
  37. size_t size;
  38. int tmp;
  39. size = min(count, sizeof(buf));
  40. if (copy_from_user(buf, buffer, size))
  41. return -EFAULT;
  42. tmp = simple_strtol(buf, &end, 0);
  43. if ((*end != '\0') && !isspace(*end))
  44. return -EINVAL;
  45. uml_exitcode = tmp;
  46. return count;
  47. }
  48. static const struct file_operations exitcode_proc_fops = {
  49. .owner = THIS_MODULE,
  50. .open = exitcode_proc_open,
  51. .read = seq_read,
  52. .llseek = seq_lseek,
  53. .release = single_release,
  54. .write = exitcode_proc_write,
  55. };
  56. static int make_proc_exitcode(void)
  57. {
  58. struct proc_dir_entry *ent;
  59. ent = proc_create("exitcode", 0600, NULL, &exitcode_proc_fops);
  60. if (ent == NULL) {
  61. printk(KERN_WARNING "make_proc_exitcode : Failed to register "
  62. "/proc/exitcode\n");
  63. return 0;
  64. }
  65. return 0;
  66. }
  67. __initcall(make_proc_exitcode);