utsname_sysctl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, int write)
  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. if (!write)
  24. down_read(&uts_sem);
  25. else
  26. down_write(&uts_sem);
  27. return which;
  28. }
  29. static void put_uts(struct ctl_table *table, int write, void *which)
  30. {
  31. if (!write)
  32. up_read(&uts_sem);
  33. else
  34. up_write(&uts_sem);
  35. }
  36. /*
  37. * Special case of dostring for the UTS structure. This has locks
  38. * to observe. Should this be in kernel/sys.c ????
  39. */
  40. static int proc_do_uts_string(struct ctl_table *table, int write,
  41. void __user *buffer, size_t *lenp, loff_t *ppos)
  42. {
  43. struct ctl_table uts_table;
  44. int r;
  45. memcpy(&uts_table, table, sizeof(uts_table));
  46. uts_table.data = get_uts(table, write);
  47. r = proc_dostring(&uts_table, write, buffer, lenp, ppos);
  48. put_uts(table, write, uts_table.data);
  49. if (write)
  50. proc_sys_poll_notify(table->poll);
  51. return r;
  52. }
  53. #else
  54. #define proc_do_uts_string NULL
  55. #endif
  56. static DEFINE_CTL_TABLE_POLL(hostname_poll);
  57. static DEFINE_CTL_TABLE_POLL(domainname_poll);
  58. static struct ctl_table uts_kern_table[] = {
  59. {
  60. .procname = "ostype",
  61. .data = init_uts_ns.name.sysname,
  62. .maxlen = sizeof(init_uts_ns.name.sysname),
  63. .mode = 0444,
  64. .proc_handler = proc_do_uts_string,
  65. },
  66. {
  67. .procname = "osrelease",
  68. .data = init_uts_ns.name.release,
  69. .maxlen = sizeof(init_uts_ns.name.release),
  70. .mode = 0444,
  71. .proc_handler = proc_do_uts_string,
  72. },
  73. {
  74. .procname = "version",
  75. .data = init_uts_ns.name.version,
  76. .maxlen = sizeof(init_uts_ns.name.version),
  77. .mode = 0444,
  78. .proc_handler = proc_do_uts_string,
  79. },
  80. {
  81. .procname = "hostname",
  82. .data = init_uts_ns.name.nodename,
  83. .maxlen = sizeof(init_uts_ns.name.nodename),
  84. .mode = 0644,
  85. .proc_handler = proc_do_uts_string,
  86. .poll = &hostname_poll,
  87. },
  88. {
  89. .procname = "domainname",
  90. .data = init_uts_ns.name.domainname,
  91. .maxlen = sizeof(init_uts_ns.name.domainname),
  92. .mode = 0644,
  93. .proc_handler = proc_do_uts_string,
  94. .poll = &domainname_poll,
  95. },
  96. {}
  97. };
  98. static struct ctl_table uts_root_table[] = {
  99. {
  100. .procname = "kernel",
  101. .mode = 0555,
  102. .child = uts_kern_table,
  103. },
  104. {}
  105. };
  106. #ifdef CONFIG_PROC_SYSCTL
  107. /*
  108. * Notify userspace about a change in a certain entry of uts_kern_table,
  109. * identified by the parameter proc.
  110. */
  111. void uts_proc_notify(enum uts_proc proc)
  112. {
  113. struct ctl_table *table = &uts_kern_table[proc];
  114. proc_sys_poll_notify(table->poll);
  115. }
  116. #endif
  117. static int __init utsname_sysctl_init(void)
  118. {
  119. register_sysctl_table(uts_root_table);
  120. return 0;
  121. }
  122. device_initcall(utsname_sysctl_init);