blob: d2b3b2973456e82a91635f718f98e41157b510ee [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>
Eric W. Biederman39732ac2007-02-14 00:33:58 -080017
Yuanhan Liucd89f462013-02-27 17:05:22 -080018#ifdef CONFIG_PROC_SYSCTL
19
Jann Horn55463c62018-06-25 18:34:10 +020020static void *get_uts(struct ctl_table *table)
Eric W. Biederman39732ac2007-02-14 00:33:58 -080021{
22 char *which = table->data;
Pavel Emelyanov32df81c2007-11-28 16:21:38 -080023 struct uts_namespace *uts_ns;
24
25 uts_ns = current->nsproxy->uts_ns;
26 which = (which - (char *)&init_uts_ns) + (char *)uts_ns;
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070027
Eric W. Biederman39732ac2007-02-14 00:33:58 -080028 return which;
29}
30
Eric W. Biederman39732ac2007-02-14 00:33:58 -080031/*
32 * Special case of dostring for the UTS structure. This has locks
33 * to observe. Should this be in kernel/sys.c ????
34 */
Joe Perches6f8fd1d2014-06-06 14:38:08 -070035static int proc_do_uts_string(struct ctl_table *table, int write,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080036 void __user *buffer, size_t *lenp, loff_t *ppos)
37{
38 struct ctl_table uts_table;
39 int r;
Jann Horn55463c62018-06-25 18:34:10 +020040 char tmp_data[__NEW_UTS_LEN + 1];
Lucas De Marchif1ecf062011-11-02 13:39:22 -070041
Jann Horn55463c62018-06-25 18:34:10 +020042 memcpy(&uts_table, table, sizeof(uts_table));
43 uts_table.data = tmp_data;
44
45 /*
46 * Buffer the value in tmp_data so that proc_dostring() can be called
47 * without holding any locks.
48 * We also need to read the original value in the write==1 case to
49 * support partial writes.
50 */
51 down_read(&uts_sem);
52 memcpy(tmp_data, get_uts(table), sizeof(tmp_data));
53 up_read(&uts_sem);
54 r = proc_dostring(&uts_table, write, buffer, lenp, ppos);
55
56 if (write) {
57 /*
58 * Write back the new value.
59 * Note that, since we dropped uts_sem, the result can
60 * theoretically be incorrect if there are two parallel writes
61 * at non-zero offsets to the same sysctl.
62 */
63 down_write(&uts_sem);
64 memcpy(get_uts(table), tmp_data, sizeof(tmp_data));
65 up_write(&uts_sem);
Lucas De Marchif1ecf062011-11-02 13:39:22 -070066 proc_sys_poll_notify(table->poll);
Jann Horn55463c62018-06-25 18:34:10 +020067 }
Lucas De Marchif1ecf062011-11-02 13:39:22 -070068
Eric W. Biederman39732ac2007-02-14 00:33:58 -080069 return r;
70}
71#else
72#define proc_do_uts_string NULL
73#endif
74
Lucas De Marchif1ecf062011-11-02 13:39:22 -070075static DEFINE_CTL_TABLE_POLL(hostname_poll);
76static DEFINE_CTL_TABLE_POLL(domainname_poll);
77
Eric W. Biederman39732ac2007-02-14 00:33:58 -080078static struct ctl_table uts_kern_table[] = {
79 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -080080 .procname = "ostype",
81 .data = init_uts_ns.name.sysname,
82 .maxlen = sizeof(init_uts_ns.name.sysname),
83 .mode = 0444,
84 .proc_handler = proc_do_uts_string,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080085 },
86 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -080087 .procname = "osrelease",
88 .data = init_uts_ns.name.release,
89 .maxlen = sizeof(init_uts_ns.name.release),
90 .mode = 0444,
91 .proc_handler = proc_do_uts_string,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080092 },
93 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -080094 .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,
Eric W. Biederman39732ac2007-02-14 00:33:58 -080099 },
100 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800101 .procname = "hostname",
102 .data = init_uts_ns.name.nodename,
103 .maxlen = sizeof(init_uts_ns.name.nodename),
104 .mode = 0644,
105 .proc_handler = proc_do_uts_string,
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700106 .poll = &hostname_poll,
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800107 },
108 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800109 .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,
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700114 .poll = &domainname_poll,
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800115 },
116 {}
117};
118
119static struct ctl_table uts_root_table[] = {
120 {
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800121 .procname = "kernel",
122 .mode = 0555,
123 .child = uts_kern_table,
124 },
125 {}
126};
127
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700128#ifdef CONFIG_PROC_SYSCTL
129/*
130 * Notify userspace about a change in a certain entry of uts_kern_table,
131 * identified by the parameter proc.
132 */
133void uts_proc_notify(enum uts_proc proc)
134{
135 struct ctl_table *table = &uts_kern_table[proc];
136
137 proc_sys_poll_notify(table->poll);
138}
139#endif
140
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800141static int __init utsname_sysctl_init(void)
142{
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800143 register_sysctl_table(uts_root_table);
Eric W. Biederman39732ac2007-02-14 00:33:58 -0800144 return 0;
145}
146
Fabian Frederick95583e42014-06-04 16:11:26 -0700147device_initcall(utsname_sysctl_init);