exitcode.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  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 <linux/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 proc_ops exitcode_proc_ops = {
  49. .proc_open = exitcode_proc_open,
  50. .proc_read = seq_read,
  51. .proc_lseek = seq_lseek,
  52. .proc_release = single_release,
  53. .proc_write = exitcode_proc_write,
  54. };
  55. static int make_proc_exitcode(void)
  56. {
  57. struct proc_dir_entry *ent;
  58. ent = proc_create("exitcode", 0600, NULL, &exitcode_proc_ops);
  59. if (ent == NULL) {
  60. printk(KERN_WARNING "make_proc_exitcode : Failed to register "
  61. "/proc/exitcode\n");
  62. return 0;
  63. }
  64. return 0;
  65. }
  66. __initcall(make_proc_exitcode);