utsname_sysctl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007
  4. *
  5. * Author: Eric Biederman <ebiederm@xmision.com>
  6. */
  7. #include <linux/export.h>
  8. #include <linux/uts.h>
  9. #include <linux/utsname.h>
  10. #include <linux/sysctl.h>
  11. #include <linux/wait.h>
  12. #include <linux/rwsem.h>
  13. #ifdef CONFIG_PROC_SYSCTL
  14. static void *get_uts(struct ctl_table *table)
  15. {
  16. char *which = table->data;
  17. struct uts_namespace *uts_ns;
  18. uts_ns = current->nsproxy->uts_ns;
  19. which = (which - (char *)&init_uts_ns) + (char *)uts_ns;
  20. return which;
  21. }
  22. /*
  23. * Special case of dostring for the UTS structure. This has locks
  24. * to observe. Should this be in kernel/sys.c ????
  25. */
  26. static int proc_do_uts_string(struct ctl_table *table, int write,
  27. void *buffer, size_t *lenp, loff_t *ppos)
  28. {
  29. struct ctl_table uts_table;
  30. int r;
  31. char tmp_data[__NEW_UTS_LEN + 1];
  32. memcpy(&uts_table, table, sizeof(uts_table));
  33. uts_table.data = tmp_data;
  34. /*
  35. * Buffer the value in tmp_data so that proc_dostring() can be called
  36. * without holding any locks.
  37. * We also need to read the original value in the write==1 case to
  38. * support partial writes.
  39. */
  40. down_read(&uts_sem);
  41. memcpy(tmp_data, get_uts(table), sizeof(tmp_data));
  42. up_read(&uts_sem);
  43. r = proc_dostring(&uts_table, write, buffer, lenp, ppos);
  44. if (write) {
  45. /*
  46. * Write back the new value.
  47. * Note that, since we dropped uts_sem, the result can
  48. * theoretically be incorrect if there are two parallel writes
  49. * at non-zero offsets to the same sysctl.
  50. */
  51. down_write(&uts_sem);
  52. memcpy(get_uts(table), tmp_data, sizeof(tmp_data));
  53. up_write(&uts_sem);
  54. proc_sys_poll_notify(table->poll);
  55. }
  56. return r;
  57. }
  58. #else
  59. #define proc_do_uts_string NULL
  60. #endif
  61. static DEFINE_CTL_TABLE_POLL(hostname_poll);
  62. static DEFINE_CTL_TABLE_POLL(domainname_poll);
  63. static struct ctl_table uts_kern_table[] = {
  64. {
  65. .procname = "ostype",
  66. .data = init_uts_ns.name.sysname,
  67. .maxlen = sizeof(init_uts_ns.name.sysname),
  68. .mode = 0444,
  69. .proc_handler = proc_do_uts_string,
  70. },
  71. {
  72. .procname = "osrelease",
  73. .data = init_uts_ns.name.release,
  74. .maxlen = sizeof(init_uts_ns.name.release),
  75. .mode = 0444,
  76. .proc_handler = proc_do_uts_string,
  77. },
  78. {
  79. .procname = "version",
  80. .data = init_uts_ns.name.version,
  81. .maxlen = sizeof(init_uts_ns.name.version),
  82. .mode = 0444,
  83. .proc_handler = proc_do_uts_string,
  84. },
  85. {
  86. .procname = "hostname",
  87. .data = init_uts_ns.name.nodename,
  88. .maxlen = sizeof(init_uts_ns.name.nodename),
  89. .mode = 0644,
  90. .proc_handler = proc_do_uts_string,
  91. .poll = &hostname_poll,
  92. },
  93. {
  94. .procname = "domainname",
  95. .data = init_uts_ns.name.domainname,
  96. .maxlen = sizeof(init_uts_ns.name.domainname),
  97. .mode = 0644,
  98. .proc_handler = proc_do_uts_string,
  99. .poll = &domainname_poll,
  100. },
  101. {}
  102. };
  103. static struct ctl_table uts_root_table[] = {
  104. {
  105. .procname = "kernel",
  106. .mode = 0555,
  107. .child = uts_kern_table,
  108. },
  109. {}
  110. };
  111. #ifdef CONFIG_PROC_SYSCTL
  112. /*
  113. * Notify userspace about a change in a certain entry of uts_kern_table,
  114. * identified by the parameter proc.
  115. */
  116. void uts_proc_notify(enum uts_proc proc)
  117. {
  118. struct ctl_table *table = &uts_kern_table[proc];
  119. proc_sys_poll_notify(table->poll);
  120. }
  121. #endif
  122. static int __init utsname_sysctl_init(void)
  123. {
  124. register_sysctl_table(uts_root_table);
  125. return 0;
  126. }
  127. device_initcall(utsname_sysctl_init);