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