exitcode.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/kernel.h"
  6. #include "linux/init.h"
  7. #include "linux/ctype.h"
  8. #include "linux/proc_fs.h"
  9. #include "asm/uaccess.h"
  10. /* If read and write race, the read will still atomically read a valid
  11. * value.
  12. */
  13. int uml_exitcode = 0;
  14. static int read_proc_exitcode(char *page, char **start, off_t off,
  15. int count, int *eof, void *data)
  16. {
  17. int len, val;
  18. /* Save uml_exitcode in a local so that we don't need to guarantee
  19. * that sprintf accesses it atomically.
  20. */
  21. val = uml_exitcode;
  22. len = sprintf(page, "%d\n", val);
  23. len -= off;
  24. if(len <= off+count)
  25. *eof = 1;
  26. *start = page + off;
  27. if(len > count)
  28. len = count;
  29. if(len < 0)
  30. len = 0;
  31. return len;
  32. }
  33. static int write_proc_exitcode(struct file *file, const char __user *buffer,
  34. unsigned long count, void *data)
  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 int make_proc_exitcode(void)
  47. {
  48. struct proc_dir_entry *ent;
  49. ent = create_proc_entry("exitcode", 0600, &proc_root);
  50. if(ent == NULL){
  51. printk(KERN_WARNING "make_proc_exitcode : Failed to register "
  52. "/proc/exitcode\n");
  53. return 0;
  54. }
  55. ent->read_proc = read_proc_exitcode;
  56. ent->write_proc = write_proc_exitcode;
  57. return 0;
  58. }
  59. __initcall(make_proc_exitcode);