7ca6700d6994df766d394f599ca091e4e2234ac4
[linux-2.6.git] / kernel / vserver / legacy.c
1 /*
2  *  linux/kernel/vserver/legacy.c
3  *
4  *  Virtual Server: Legacy Funtions
5  *
6  *  Copyright (C) 2001-2003  Jacques Gelinas
7  *  Copyright (C) 2003-2005  Herbert Pƶtzl
8  *
9  *  V0.01  broken out from vcontext.c V0.05
10  *
11  */
12
13 #include <linux/config.h>
14 #include <linux/sched.h>
15 #include <linux/vs_context.h>
16 #include <linux/vs_network.h>
17 #include <linux/vserver/legacy.h>
18 #include <linux/vserver/namespace.h>
19 #include <linux/namespace.h>
20
21 #include <asm/errno.h>
22 #include <asm/uaccess.h>
23
24
25
26 static int vx_set_initpid(struct vx_info *vxi, int pid)
27 {
28         if (vxi->vx_initpid)
29                 return -EPERM;
30
31         vxi->vx_initpid = pid;
32         return 0;
33 }
34
35 int vc_new_s_context(uint32_t ctx, void __user *data)
36 {
37         int ret = -ENOMEM;
38         struct vcmd_new_s_context_v1 vc_data;
39         struct vx_info *new_vxi;
40
41         if (copy_from_user(&vc_data, data, sizeof(vc_data)))
42                 return -EFAULT;
43
44         /* legacy hack, will be removed soon */
45         if (ctx == -2) {
46                 /* assign flags and initpid */
47                 if (!current->vx_info)
48                         return -EINVAL;
49                 ret = 0;
50                 if (vc_data.flags & VX_INFO_INIT)
51                         ret = vx_set_initpid(current->vx_info, current->tgid);
52                 if (ret == 0) {
53                         /* We keep the same vx_id, but lower the capabilities */
54                         current->vx_info->vx_bcaps &= (~vc_data.remove_cap);
55                         // current->cap_bset &= (~vc_data.remove_cap);
56                         ret = vx_current_xid();
57                         current->vx_info->vx_flags |= vc_data.flags;
58                 }
59                 return ret;
60         }
61
62         if (!vx_check(0, VX_ADMIN) || !capable(CAP_SYS_ADMIN)
63                 /* might make sense in the future, or not ... */
64                 || vx_flags(VX_INFO_LOCK, 0))
65                 return -EPERM;
66
67         /* ugly hack for Spectator */
68         if (ctx == 1) {
69                 current->xid = 1;
70                 return 0;
71         }
72
73         if (((ctx > MAX_S_CONTEXT) && (ctx != VX_DYNAMIC_ID)) ||
74                 (ctx == 0))
75                 return -EINVAL;
76
77         if ((ctx == VX_DYNAMIC_ID) || (ctx < MIN_D_CONTEXT))
78                 new_vxi = locate_or_create_vx_info(ctx);
79         else
80                 new_vxi = locate_vx_info(ctx);
81
82         if (!new_vxi)
83                 return -EINVAL;
84
85         ret = -EPERM;
86         if (!vx_info_flags(new_vxi, VXF_STATE_SETUP, 0) &&
87                 vx_info_flags(new_vxi, VX_INFO_PRIVATE, 0))
88                 goto out_put;
89
90         new_vxi->vx_flags &= ~(VXF_STATE_SETUP|VXF_STATE_INIT);
91
92         ret = vx_migrate_task(current, new_vxi);
93         if (ret == 0) {
94                 current->vx_info->vx_bcaps &= (~vc_data.remove_cap);
95                 // current->cap_bset &= (~vc_data.remove_cap);
96                 new_vxi->vx_flags |= vc_data.flags;
97                 if (vc_data.flags & VX_INFO_INIT)
98                         vx_set_initpid(new_vxi, current->tgid);
99                 if (vc_data.flags & VX_INFO_NAMESPACE)
100                         vx_set_namespace(new_vxi,
101                                 current->namespace, current->fs);
102                 if (vc_data.flags & VX_INFO_NPROC)
103                         new_vxi->limit.rlim[RLIMIT_NPROC] =
104                                 current->signal->rlim[RLIMIT_NPROC].rlim_max;
105                 ret = new_vxi->vx_id;
106         }
107 out_put:
108         put_vx_info(new_vxi);
109         return ret;
110 }
111