utsname_sysctl.c 3.4 KB

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