utsname_sysctl.c 3.3 KB

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