utsname_sysctl.c 3.4 KB

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