utsname_sysctl.c 3.4 KB

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