2 * linux/kernel/vserver/helper.c
4 * Virtual Context Support
6 * Copyright (C) 2004-2007 Herbert Pƶtzl
12 #include <linux/errno.h>
13 #include <linux/kmod.h>
14 #include <linux/sched.h>
15 #include <linux/reboot.h>
16 #include <linux/vs_context.h>
17 #include <linux/vs_network.h>
18 #include <linux/vserver/signal.h>
20 #include <asm/uaccess.h>
21 #include <asm/unistd.h>
24 char vshelper_path[255] = "/sbin/vshelper";
27 static int do_vshelper(char *name, char *argv[], char *envp[], int sync)
31 if ((ret = call_usermodehelper(name, argv, envp, sync))) {
33 "%s: (%s %s) returned %s with %d\n",
34 name, argv[1], argv[2],
35 sync?"sync":"async", ret);
37 vxdprintk(VXD_CBIT(switch, 4),
38 "%s: (%s %s) returned %s with %d",
39 name, argv[1], argv[2], sync?"sync":"async", ret);
44 * vshelper path is set via /proc/sys
45 * invoked by vserver sys_reboot(), with
46 * the following arguments
48 * argv [0] = vshelper_path;
49 * argv [1] = action: "restart", "halt", "poweroff", ...
50 * argv [2] = context identifier
52 * envp [*] = type-specific parameters
55 long vs_reboot_helper(struct vx_info *vxi, int cmd, void __user *arg)
57 char id_buf[8], cmd_buf[16];
58 char uid_buf[16], pid_buf[16];
61 char *argv[] = {vshelper_path, NULL, id_buf, 0};
62 char *envp[] = {"HOME=/", "TERM=linux",
63 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
64 uid_buf, pid_buf, cmd_buf, 0};
66 if (vx_info_state(vxi, VXS_HELPER))
68 vxi->vx_state |= VXS_HELPER;
70 snprintf(id_buf, sizeof(id_buf)-1, "%d", vxi->vx_id);
72 snprintf(cmd_buf, sizeof(cmd_buf)-1, "VS_CMD=%08x", cmd);
73 snprintf(uid_buf, sizeof(uid_buf)-1, "VS_UID=%d", current->uid);
74 snprintf(pid_buf, sizeof(pid_buf)-1, "VS_PID=%d", current->pid);
77 case LINUX_REBOOT_CMD_RESTART:
81 case LINUX_REBOOT_CMD_HALT:
85 case LINUX_REBOOT_CMD_POWER_OFF:
89 case LINUX_REBOOT_CMD_SW_SUSPEND:
94 vxi->vx_state &= ~VXS_HELPER;
98 #ifndef CONFIG_VSERVER_LEGACY
99 ret = do_vshelper(vshelper_path, argv, envp, 1);
101 ret = do_vshelper(vshelper_path, argv, envp, 0);
103 vxi->vx_state &= ~VXS_HELPER;
104 __wakeup_vx_info(vxi);
105 return (ret) ? -EPERM : 0;
109 long vs_reboot(unsigned int cmd, void __user * arg)
111 struct vx_info *vxi = current->vx_info;
114 vxdprintk(VXD_CBIT(misc, 5),
115 "vs_reboot(%p[#%d],%d)",
116 vxi, vxi?vxi->vx_id:0, cmd);
118 ret = vs_reboot_helper(vxi, cmd, arg);
122 vxi->reboot_cmd = cmd;
123 if (vx_info_flags(vxi, VXF_REBOOT_KILL, 0)) {
125 case LINUX_REBOOT_CMD_RESTART:
126 case LINUX_REBOOT_CMD_HALT:
127 case LINUX_REBOOT_CMD_POWER_OFF:
128 vx_info_kill(vxi, 0, SIGKILL);
129 vx_info_kill(vxi, 1, SIGKILL);
139 * argv [0] = vshelper_path;
140 * argv [1] = action: "startup", "shutdown"
141 * argv [2] = context identifier
143 * envp [*] = type-specific parameters
146 long vs_state_change(struct vx_info *vxi, unsigned int cmd)
148 char id_buf[8], cmd_buf[16];
149 char *argv[] = {vshelper_path, NULL, id_buf, 0};
150 char *envp[] = {"HOME=/", "TERM=linux",
151 "PATH=/sbin:/usr/sbin:/bin:/usr/bin", cmd_buf, 0};
153 if (!vx_info_flags(vxi, VXF_SC_HELPER, 0))
156 snprintf(id_buf, sizeof(id_buf)-1, "%d", vxi->vx_id);
157 snprintf(cmd_buf, sizeof(cmd_buf)-1, "VS_CMD=%08x", cmd);
164 argv[1] = "shutdown";
170 return do_vshelper(vshelper_path, argv, envp, 1);
175 * argv [0] = vshelper_path;
176 * argv [1] = action: "netup", "netdown"
177 * argv [2] = context identifier
179 * envp [*] = type-specific parameters
182 long vs_net_change(struct nx_info *nxi, unsigned int cmd)
184 char id_buf[8], cmd_buf[16];
185 char *argv[] = {vshelper_path, NULL, id_buf, 0};
186 char *envp[] = {"HOME=/", "TERM=linux",
187 "PATH=/sbin:/usr/sbin:/bin:/usr/bin", cmd_buf, 0};
189 if (!nx_info_flags(nxi, NXF_SC_HELPER, 0))
192 snprintf(id_buf, sizeof(id_buf)-1, "%d", nxi->nx_id);
193 snprintf(cmd_buf, sizeof(cmd_buf)-1, "VS_CMD=%08x", cmd);
206 return do_vshelper(vshelper_path, argv, envp, 1);