blob: 233cd8fc691082363d6c324f1555aa1dd1f33c21 [file] [log] [blame]
Eric W. Biederman39732ac2007-02-14 00:33:58 -08001/*
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
Paul Gortmaker9984de12011-05-23 14:51:41 -040012#include <linux/export.h>
Eric W. Biederman39732ac2007-02-14 00:33:58 -080013#include <linux/uts.h>
14#include <linux/utsname.h>
Eric W. Biederman39732ac2007-02-14 00:33:58 -080015#include <linux/sysctl.h>
Lucas De Marchif1ecf062011-11-02 13:39:22 -070016#include <linux/wait.h>
Ingo Molnarcd9c5132017-02-08 18:51:58 +010017#include <linux/rwsem.h>
Eric W. Biederman39732ac2007-02-14 00:33:58 -080018
Yuanhan Liucd89f462013-02-27 17:05:22 -080019#ifdef CONFIG_PROC_SYSCTL
20
Joe Perches6f8fd1d2014-06-06 14:38:08 -070021static void *get_uts(struct ctl_table *table, int write)
Eric W. Biederman39732ac2007-02-14 00:33:58 -080022{
23 char *which = table->data;
Pavel Emelyanov32df81c2007-11-28 16:21:38 -080024 struct uts_namespace *uts_ns;
25
26 uts_ns = current->nsproxy->uts_ns;
27 which = (which - (char *)&init_uts_ns) + (char *)uts_ns;
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070028
Eric W. Biederman39732ac2007-02-14 00:33:58 -080029 if (!write)
30 down_read(&uts_sem);
31 else
32 down_write(&uts_sem);
33 return which;
34}
35
Joe Perches6f8fd1d2014-06-06 14:38:08 -070036static void put_uts(struct ctl_table *table, int write, void *which)
Eric W. Biederman39732ac2007-02-14 00:33:58 -080037{
38 if (!write)
39 up_read(&uts_sem);
40 else
41 up_write(&uts_sem);
42}
43
Eric W. Biederman39732ac2007-02-14 00:33:58 -080044/*
45 * Special case of dostring for the UTS structure. This has locks
46 * to observe. Should this be in kernel/sys.c ????
47 */
Joe Perches6f8fd1d2014-06-06 14:38:08 -070048static int proc_do_uts_string(struct ctl_table *table, int write,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080049 void __user *buffer, size_t *lenp, loff_t *ppos)
50{
51 struct ctl_table uts_table;
52 int r;
53 memcpy(&uts_table, table, sizeof(uts_table));
54 uts_table.data = get_uts(table, write);
Fabian Frederick95583e42014-06-04 16:11:26 -070055 r = proc_dostring(&uts_table, write, buffer, lenp, ppos);
Eric W. Biederman39732ac2007-02-14 00:33:58 -080056 put_uts(table, write, uts_table.data);
Lucas De Marchif1ecf062011-11-02 13:39:22 -070057
58 if (write)
59 proc_sys_poll_notify(table->poll);
60
Eric W. Biederman39732ac2007-02-14 00:33:58 -080061 return r;
62}
63#else
64#define proc_do_uts_string NULL
65#endif
66
Lucas De Marchif1ecf062011-11-02 13:39:22 -070067static DEFINE_CTL_TABLE_POLL(hostname_poll);
68static DEFINE_CTL_TABLE_POLL(domainname_poll);
69
Eric W. Biederman39732ac2007-02-14 00:33:58 -080070static struct ctl_table uts_kern_table[] = {
71 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -080072 .procname = "ostype",
73 .data = init_uts_ns.name.sysname,
74 .maxlen = sizeof(init_uts_ns.name.sysname),
75 .mode = 0444,
76 .proc_handler = proc_do_uts_string,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080077 },
78 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -080079 .procname = "osrelease",
80 .data = init_uts_ns.name.release,
81 .maxlen = sizeof(init_uts_ns.name.release),
82 .mode = 0444,
83 .proc_handler = proc_do_uts_string,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080084 },
85 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -080086 .procname = "version",
87 .data = init_uts_ns.name.version,
88 .maxlen = sizeof(init_uts_ns.name.version),
89 .mode = 0444,
90 .proc_handler = proc_do_uts_string,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080091 },
92 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -080093 .procname = "hostname",
94 .data = init_uts_ns.name.nodename,
95 .maxlen = sizeof(init_uts_ns.name.nodename),
96 .mode = 0644,
97 .proc_handler = proc_do_uts_string,
Lucas De Marchif1ecf062011-11-02 13:39:22 -070098 .poll = &hostname_poll,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080099 },
100 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800101 .procname = "domainname",
102 .data = init_uts_ns.name.domainname,
103 .maxlen = sizeof(init_uts_ns.name.domainname),
104 .mode = 0644,
105 .proc_handler = proc_do_uts_string,
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700106 .poll = &domainname_poll,
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800107 },
108 {}
109};
110
111static struct ctl_table uts_root_table[] = {
112 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800113 .procname = "kernel",
114 .mode = 0555,
115 .child = uts_kern_table,
116 },
117 {}
118};
119
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700120#ifdef CONFIG_PROC_SYSCTL
121/*
122 * Notify userspace about a change in a certain entry of uts_kern_table,
123 * identified by the parameter proc.
124 */
125void uts_proc_notify(enum uts_proc proc)
126{
127 struct ctl_table *table = &uts_kern_table[proc];
128
129 proc_sys_poll_notify(table->poll);
130}
131#endif
132
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800133static int __init utsname_sysctl_init(void)
134{
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800135 register_sysctl_table(uts_root_table);
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800136 return 0;
137}
138
Fabian Frederick95583e42014-06-04 16:11:26 -0700139device_initcall(utsname_sysctl_init);