exitcode.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. int tmp;
  38. if (copy_from_user(buf, buffer, count))
  39. return -EFAULT;
  40. tmp = simple_strtol(buf, &end, 0);
  41. if ((*end != '\0') && !isspace(*end))
  42. return -EINVAL;
  43. uml_exitcode = tmp;
  44. return count;
  45. }
  46. static const struct file_operations exitcode_proc_fops = {
  47. .owner = THIS_MODULE,
  48. .open = exitcode_proc_open,
  49. .read = seq_read,
  50. .llseek = seq_lseek,
  51. .release = single_release,
  52. .write = exitcode_proc_write,
  53. };
  54. static int make_proc_exitcode(void)
  55. {
  56. struct proc_dir_entry *ent;
  57. ent = proc_create("exitcode", 0600, NULL, &exitcode_proc_fops);
  58. if (ent == NULL) {
  59. printk(KERN_WARNING "make_proc_exitcode : Failed to register "
  60. "/proc/exitcode\n");
  61. return 0;
  62. }
  63. return 0;
  64. }
  65. __initcall(make_proc_exitcode);