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