1 /* enter_admin.c Vsys script to switch a vserver into admin mode in which it has access
2 * to the Internet. Install in /vsys and invoke as echo $$ > /vsys/enter_admin.in
3 * from within the slice.
4 * 3/21/2008 Sapan Bhatia
7 #include <sys/syscall.h>
14 #define CLONE_NEWNET 0x40000000 /* New network namespace (lo, device, names sockets, etc) */
17 #define __NR_set_space 327
20 int set_space(int pid, int id, int toggle, unsigned long unshare_flags) {
21 return syscall(__NR_set_space, pid, id, toggle, unshare_flags);
24 int get_slice_xid(char *slice_name) {
25 char slicepath[PATHLEN];
28 snprintf(slicepath, sizeof(slicepath), "/etc/vservers/%s/context",
31 if ((fp = fopen(slicepath, "r")) == NULL) {
32 printf("Could not open %s\n", slicepath);
36 if (fscanf(fp, "%d", &xid)==0) {
37 printf("Could not read ctx file\n");
45 int verify_ownership(int pid, int arg_xid) {
46 char procpath[PATHLEN];
49 snprintf(procpath, sizeof(procpath), "/proc/%d/vinfo", pid);
51 if ((fp = fopen(procpath, "r")) == NULL) {
52 printf("Could not open %s\n", procpath);
56 if (fscanf(fp, "XID: %d", &xid)==0) {
57 printf("Could not read ctx file\n");
62 return (arg_xid==xid);
66 int main(int argc, char *argv[]) {
71 printf("Slice name missing. Was I invoked by vsys?\n");
77 if ((xid = get_slice_xid(argv[1]))==-1) {
78 printf("Could not get xid for slice %s\n",argv[1]);
82 if (!verify_ownership(pid, xid)) {
83 printf("Does xid %d really own %d?\n",xid,pid);
87 set_space(pid, xid, 0, CLONE_NEWNET);