ce8f9710ac4099f43c3510842cbd3488931d144c
[linux-2.6.git] / kernel / vserver / helper.c
1 /*
2  *  linux/kernel/vserver/helper.c
3  *
4  *  Virtual Context Support
5  *
6  *  Copyright (C) 2004  Herbert Pƶtzl
7  *
8  *  V0.01  basic helper
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/errno.h>
14 #include <linux/reboot.h>
15 #include <linux/kmod.h>
16 #include <linux/sched.h>
17 #include <linux/vs_context.h>
18
19 #include <asm/uaccess.h>
20 #include <asm/unistd.h>
21
22
23 char vshelper_path[255] = "/sbin/vshelper";
24
25
26 /*
27  *      vshelper path is set via /proc/sys
28  *      invoked by vserver sys_reboot(), with
29  *      the following arguments
30  *
31  *      argv [0] = vshelper_path;
32  *      argv [1] = action: "restart", "halt", "poweroff", ...
33  *      argv [2] = context identifier
34  *      argv [3] = additional argument (restart2)
35  *
36  *      envp [*] = type-specific parameters
37  */
38
39 long vs_reboot(unsigned int cmd, void * arg)
40 {
41         char id_buf[8], cmd_buf[32];
42         char uid_buf[32], pid_buf[32];
43         char buffer[256];
44
45         char *argv[] = {vshelper_path, NULL, id_buf, NULL, 0};
46         char *envp[] = {"HOME=/", "TERM=linux",
47                         "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
48                         uid_buf, pid_buf, cmd_buf, 0};
49
50         snprintf(id_buf, sizeof(id_buf)-1, "%d", vx_current_xid());
51
52         snprintf(cmd_buf, sizeof(cmd_buf)-1, "VS_CMD=%08x", cmd);
53         snprintf(uid_buf, sizeof(uid_buf)-1, "VS_UID=%d", current->uid);
54         snprintf(pid_buf, sizeof(pid_buf)-1, "VS_PID=%d", current->pid);
55
56         switch (cmd) {
57         case LINUX_REBOOT_CMD_RESTART:
58                 argv[1] = "restart";
59                 break;
60
61         case LINUX_REBOOT_CMD_HALT:
62                 argv[1] = "halt";
63                 break;
64
65         case LINUX_REBOOT_CMD_POWER_OFF:
66                 argv[1] = "poweroff";
67                 break;
68
69         case LINUX_REBOOT_CMD_SW_SUSPEND:
70                 argv[1] = "swsusp";
71                 break;
72
73         case LINUX_REBOOT_CMD_RESTART2:
74                 if (strncpy_from_user(&buffer[0], (char *)arg, sizeof(buffer) - 1) < 0)
75                         return -EFAULT;
76                 argv[3] = buffer;
77         default:
78                 argv[1] = "restart2";
79                 break;
80         }
81
82         /* maybe we should wait ? */
83         if (call_usermodehelper(*argv, argv, envp, 0)) {
84                 printk( KERN_WARNING
85                         "vs_reboot(): failed to exec (%s %s %s %s)\n",
86                         vshelper_path, argv[1], argv[2], argv[3]);
87                 return -EPERM;
88         }
89         return 0;
90 }
91
92 long vs_context_state(unsigned int cmd)
93 {
94         char id_buf[8], cmd_buf[32];
95
96         char *argv[] = {vshelper_path, NULL, id_buf, NULL, 0};
97         char *envp[] = {"HOME=/", "TERM=linux",
98                         "PATH=/sbin:/usr/sbin:/bin:/usr/bin", cmd_buf, 0};
99
100         snprintf(id_buf, sizeof(id_buf)-1, "%d", vx_current_xid());
101         snprintf(cmd_buf, sizeof(cmd_buf)-1, "VS_CMD=%08x", cmd);
102
103         switch (cmd) {
104         case VS_CONTEXT_CREATED:
105                 argv[1] = "startup";
106                 break;
107         case VS_CONTEXT_DESTROY:
108                 argv[1] = "shutdown";
109                 break;
110         default:
111                 return 0;
112         }
113
114         if (call_usermodehelper(*argv, argv, envp, 1)) {
115                 printk( KERN_WARNING
116                         "vs_context_state(): failed to exec (%s %s %s %s)\n",
117                         vshelper_path, argv[1], argv[2], argv[3]);
118                 return 0;
119         }
120         return 0;
121 }
122