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/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_FS
  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, int __user *name, int nlen,
  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, name, nlen,
  65. oldval, oldlenp, newval, newlen);
  66. put_uts(table, write, uts_table.data);
  67. return r;
  68. }
  69. #else
  70. #define sysctl_uts_string NULL
  71. #endif
  72. static struct ctl_table uts_kern_table[] = {
  73. {
  74. .ctl_name = KERN_OSTYPE,
  75. .procname = "ostype",
  76. .data = init_uts_ns.name.sysname,
  77. .maxlen = sizeof(init_uts_ns.name.sysname),
  78. .mode = 0444,
  79. .proc_handler = proc_do_uts_string,
  80. .strategy = sysctl_uts_string,
  81. },
  82. {
  83. .ctl_name = KERN_OSRELEASE,
  84. .procname = "osrelease",
  85. .data = init_uts_ns.name.release,
  86. .maxlen = sizeof(init_uts_ns.name.release),
  87. .mode = 0444,
  88. .proc_handler = proc_do_uts_string,
  89. .strategy = sysctl_uts_string,
  90. },
  91. {
  92. .ctl_name = KERN_VERSION,
  93. .procname = "version",
  94. .data = init_uts_ns.name.version,
  95. .maxlen = sizeof(init_uts_ns.name.version),
  96. .mode = 0444,
  97. .proc_handler = proc_do_uts_string,
  98. .strategy = sysctl_uts_string,
  99. },
  100. {
  101. .ctl_name = KERN_NODENAME,
  102. .procname = "hostname",
  103. .data = init_uts_ns.name.nodename,
  104. .maxlen = sizeof(init_uts_ns.name.nodename),
  105. .mode = 0644,
  106. .proc_handler = proc_do_uts_string,
  107. .strategy = sysctl_uts_string,
  108. },
  109. {
  110. .ctl_name = KERN_DOMAINNAME,
  111. .procname = "domainname",
  112. .data = init_uts_ns.name.domainname,
  113. .maxlen = sizeof(init_uts_ns.name.domainname),
  114. .mode = 0644,
  115. .proc_handler = proc_do_uts_string,
  116. .strategy = sysctl_uts_string,
  117. },
  118. {}
  119. };
  120. static struct ctl_table uts_root_table[] = {
  121. {
  122. .ctl_name = CTL_KERN,
  123. .procname = "kernel",
  124. .mode = 0555,
  125. .child = uts_kern_table,
  126. },
  127. {}
  128. };
  129. static int __init utsname_sysctl_init(void)
  130. {
  131. register_sysctl_table(uts_root_table);
  132. return 0;
  133. }
  134. __initcall(utsname_sysctl_init);