exitcode.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/proc_fs.h>
  9. #include <linux/types.h>
  10. #include <asm/uaccess.h>
  11. /*
  12. * If read and write race, the read will still atomically read a valid
  13. * value.
  14. */
  15. int uml_exitcode = 0;
  16. static int read_proc_exitcode(char *page, char **start, off_t off,
  17. int count, int *eof, void *data)
  18. {
  19. int len, val;
  20. /*
  21. * Save uml_exitcode in a local so that we don't need to guarantee
  22. * that sprintf accesses it atomically.
  23. */
  24. val = uml_exitcode;
  25. len = sprintf(page, "%d\n", val);
  26. len -= off;
  27. if (len <= off+count)
  28. *eof = 1;
  29. *start = page + off;
  30. if (len > count)
  31. len = count;
  32. if (len < 0)
  33. len = 0;
  34. return len;
  35. }
  36. static int write_proc_exitcode(struct file *file, const char __user *buffer,
  37. unsigned long count, void *data)
  38. {
  39. char *end, buf[sizeof("nnnnn\0")];
  40. int tmp;
  41. if (copy_from_user(buf, buffer, count))
  42. return -EFAULT;
  43. tmp = simple_strtol(buf, &end, 0);
  44. if ((*end != '\0') && !isspace(*end))
  45. return -EINVAL;
  46. uml_exitcode = tmp;
  47. return count;
  48. }
  49. static int make_proc_exitcode(void)
  50. {
  51. struct proc_dir_entry *ent;
  52. ent = create_proc_entry("exitcode", 0600, NULL);
  53. if (ent == NULL) {
  54. printk(KERN_WARNING "make_proc_exitcode : Failed to register "
  55. "/proc/exitcode\n");
  56. return 0;
  57. }
  58. ent->read_proc = read_proc_exitcode;
  59. ent->write_proc = write_proc_exitcode;
  60. return 0;
  61. }
  62. __initcall(make_proc_exitcode);