utsname_sysctl.c 3.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 *  Copyright (C) 2007
 *
 *  Author: Eric Biederman <ebiederm@xmision.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation, version 2 of the
 *  License.
 */

12
#include <linux/export.h>
13 14 15
#include <linux/uts.h>
#include <linux/utsname.h>
#include <linux/sysctl.h>
16
#include <linux/wait.h>
17

18 19
#ifdef CONFIG_PROC_SYSCTL

20
static void *get_uts(struct ctl_table *table, int write)
21 22
{
	char *which = table->data;
23 24 25 26
	struct uts_namespace *uts_ns;

	uts_ns = current->nsproxy->uts_ns;
	which = (which - (char *)&init_uts_ns) + (char *)uts_ns;
27

28 29 30 31 32 33 34
	if (!write)
		down_read(&uts_sem);
	else
		down_write(&uts_sem);
	return which;
}

35
static void put_uts(struct ctl_table *table, int write, void *which)
36 37 38 39 40 41 42 43 44 45 46
{
	if (!write)
		up_read(&uts_sem);
	else
		up_write(&uts_sem);
}

/*
 *	Special case of dostring for the UTS structure. This has locks
 *	to observe. Should this be in kernel/sys.c ????
 */
47
static int proc_do_uts_string(struct ctl_table *table, int write,
48 49 50 51 52 53
		  void __user *buffer, size_t *lenp, loff_t *ppos)
{
	struct ctl_table uts_table;
	int r;
	memcpy(&uts_table, table, sizeof(uts_table));
	uts_table.data = get_uts(table, write);
54
	r = proc_dostring(&uts_table, write, buffer, lenp, ppos);
55
	put_uts(table, write, uts_table.data);
56 57 58 59

	if (write)
		proc_sys_poll_notify(table->poll);

60 61 62 63 64 65
	return r;
}
#else
#define proc_do_uts_string NULL
#endif

66 67 68
static DEFINE_CTL_TABLE_POLL(hostname_poll);
static DEFINE_CTL_TABLE_POLL(domainname_poll);

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static struct ctl_table uts_kern_table[] = {
	{
		.procname	= "ostype",
		.data		= init_uts_ns.name.sysname,
		.maxlen		= sizeof(init_uts_ns.name.sysname),
		.mode		= 0444,
		.proc_handler	= proc_do_uts_string,
	},
	{
		.procname	= "osrelease",
		.data		= init_uts_ns.name.release,
		.maxlen		= sizeof(init_uts_ns.name.release),
		.mode		= 0444,
		.proc_handler	= proc_do_uts_string,
	},
	{
		.procname	= "version",
		.data		= init_uts_ns.name.version,
		.maxlen		= sizeof(init_uts_ns.name.version),
		.mode		= 0444,
		.proc_handler	= proc_do_uts_string,
	},
	{
		.procname	= "hostname",
		.data		= init_uts_ns.name.nodename,
		.maxlen		= sizeof(init_uts_ns.name.nodename),
		.mode		= 0644,
		.proc_handler	= proc_do_uts_string,
97
		.poll		= &hostname_poll,
98 99 100 101 102 103 104
	},
	{
		.procname	= "domainname",
		.data		= init_uts_ns.name.domainname,
		.maxlen		= sizeof(init_uts_ns.name.domainname),
		.mode		= 0644,
		.proc_handler	= proc_do_uts_string,
105
		.poll		= &domainname_poll,
106 107 108 109 110 111 112 113 114 115 116 117 118
	},
	{}
};

static struct ctl_table uts_root_table[] = {
	{
		.procname	= "kernel",
		.mode		= 0555,
		.child		= uts_kern_table,
	},
	{}
};

119 120 121 122 123 124 125 126 127 128 129 130 131
#ifdef CONFIG_PROC_SYSCTL
/*
 * Notify userspace about a change in a certain entry of uts_kern_table,
 * identified by the parameter proc.
 */
void uts_proc_notify(enum uts_proc proc)
{
	struct ctl_table *table = &uts_kern_table[proc];

	proc_sys_poll_notify(table->poll);
}
#endif

132 133
static int __init utsname_sysctl_init(void)
{
134
	register_sysctl_table(uts_root_table);
135 136 137
	return 0;
}

138
device_initcall(utsname_sysctl_init);