utsname_sysctl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/module.h>
  12. #include <linux/uts.h>
  13. #include <linux/utsname.h>
  14. #include <linux/sysctl.h>
  15. static void *get_uts(ctl_table *table, int write)
  16. {
  17. char *which = table->data;
  18. struct uts_namespace *uts_ns;
  19. uts_ns = current->nsproxy->uts_ns;
  20. which = (which - (char *)&init_uts_ns) + (char *)uts_ns;
  21. if (!write)
  22. down_read(&uts_sem);
  23. else
  24. down_write(&uts_sem);
  25. return which;
  26. }
  27. static void put_uts(ctl_table *table, int write, void *which)
  28. {
  29. if (!write)
  30. up_read(&uts_sem);
  31. else
  32. up_write(&uts_sem);
  33. }
  34. #ifdef CONFIG_PROC_SYSCTL
  35. /*
  36. * Special case of dostring for the UTS structure. This has locks
  37. * to observe. Should this be in kernel/sys.c ????
  38. */
  39. static int proc_do_uts_string(ctl_table *table, int write, struct file *filp,
  40. void __user *buffer, size_t *lenp, loff_t *ppos)
  41. {
  42. struct ctl_table uts_table;
  43. int r;
  44. memcpy(&uts_table, table, sizeof(uts_table));
  45. uts_table.data = get_uts(table, write);
  46. r = proc_dostring(&uts_table,write,filp,buffer,lenp, ppos);
  47. put_uts(table, write, uts_table.data);
  48. return r;
  49. }
  50. #else
  51. #define proc_do_uts_string NULL
  52. #endif
  53. #ifdef CONFIG_SYSCTL_SYSCALL
  54. /* The generic string strategy routine: */
  55. static int sysctl_uts_string(ctl_table *table,
  56. void __user *oldval, size_t __user *oldlenp,
  57. void __user *newval, size_t newlen)
  58. {
  59. struct ctl_table uts_table;
  60. int r, write;
  61. write = newval && newlen;
  62. memcpy(&uts_table, table, sizeof(uts_table));
  63. uts_table.data = get_uts(table, write);
  64. r = sysctl_string(&uts_table, oldval, oldlenp, newval, newlen);
  65. put_uts(table, write, uts_table.data);
  66. return r;
  67. }
  68. #else
  69. #define sysctl_uts_string NULL
  70. #endif
  71. static struct ctl_table uts_kern_table[] = {
  72. {
  73. .ctl_name = KERN_OSTYPE,
  74. .procname = "ostype",
  75. .data = init_uts_ns.name.sysname,
  76. .maxlen = sizeof(init_uts_ns.name.sysname),
  77. .mode = 0444,
  78. .proc_handler = proc_do_uts_string,
  79. .strategy = sysctl_uts_string,
  80. },
  81. {
  82. .ctl_name = KERN_OSRELEASE,
  83. .procname = "osrelease",
  84. .data = init_uts_ns.name.release,
  85. .maxlen = sizeof(init_uts_ns.name.release),
  86. .mode = 0444,
  87. .proc_handler = proc_do_uts_string,
  88. .strategy = sysctl_uts_string,
  89. },
  90. {
  91. .ctl_name = KERN_VERSION,
  92. .procname = "version",
  93. .data = init_uts_ns.name.version,
  94. .maxlen = sizeof(init_uts_ns.name.version),
  95. .mode = 0444,
  96. .proc_handler = proc_do_uts_string,
  97. .strategy = sysctl_uts_string,
  98. },
  99. {
  100. .ctl_name = KERN_NODENAME,
  101. .procname = "hostname",
  102. .data = init_uts_ns.name.nodename,
  103. .maxlen = sizeof(init_uts_ns.name.nodename),
  104. .mode = 0644,
  105. .proc_handler = proc_do_uts_string,
  106. .strategy = sysctl_uts_string,
  107. },
  108. {
  109. .ctl_name = KERN_DOMAINNAME,
  110. .procname = "domainname",
  111. .data = init_uts_ns.name.domainname,
  112. .maxlen = sizeof(init_uts_ns.name.domainname),
  113. .mode = 0644,
  114. .proc_handler = proc_do_uts_string,
  115. .strategy = sysctl_uts_string,
  116. },
  117. {}
  118. };
  119. static struct ctl_table uts_root_table[] = {
  120. {
  121. .ctl_name = CTL_KERN,
  122. .procname = "kernel",
  123. .mode = 0555,
  124. .child = uts_kern_table,
  125. },
  126. {}
  127. };
  128. static int __init utsname_sysctl_init(void)
  129. {
  130. register_sysctl_table(uts_root_table);
  131. return 0;
  132. }
  133. __initcall(utsname_sysctl_init);