Identify trellis kernels.
[linux-2.6.git] / enter_topo.c
1 /* enter_topo.c         Vsys script to switch a vserver into experiment mode in which it has access 
2  *                      to the Topology. Install in /vsys and invoke as echo $$ > /vsys/enter_topo.in
3  *                      from within the slice.
4  *                      3/21/2008       Sapan Bhatia
5  */
6
7 #include <sys/syscall.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <string.h>
12
13 #ifndef CLONE_NEWNET
14 #define CLONE_NEWNET    0x40000000      /* New network namespace (lo, device, names sockets, etc) */
15 #endif
16
17 #define         __NR_set_space          327
18 #define         PATHLEN                 1024
19
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);
22 }
23
24 int get_slice_xid(char *slice_name) {
25         char slicepath[PATHLEN];
26         FILE *fp;
27         int xid;
28         snprintf(slicepath, sizeof(slicepath), "/etc/vservers/%s/context",
29                  slice_name);
30
31         if ((fp = fopen(slicepath, "r")) == NULL) {
32                 printf("Could not open %s\n", slicepath);       
33                 return -1;
34         }
35
36         if (fscanf(fp, "%d", &xid)==0) {
37                 printf("Could not read ctx file\n");
38                 return -1;
39         }
40
41         fclose (fp);
42         return xid;
43 }
44
45 int verify_ownership(int pid, int arg_xid) {
46         char procpath[PATHLEN];
47         FILE *fp;
48         int xid;
49         snprintf(procpath, sizeof(procpath), "/proc/%d/vinfo", pid);
50
51         if ((fp = fopen(procpath, "r")) == NULL) {
52                 printf("Could not open %s\n", procpath);        
53                 return -1;
54         }
55
56         if (fscanf(fp, "XID: %d", &xid)==0) {
57                 printf("Could not read ctx file\n");
58                 return -1;
59         }
60
61         fclose (fp);
62         return (arg_xid==xid);
63
64 }
65
66 int main(int argc, char *argv[]) {
67         int xid;
68         int pid;
69
70         if (argc < 1) {
71                 printf("Slice name missing. Was I invoked by vsys?\n");
72                 exit(1);
73         }
74
75         scanf("%d",&pid);
76
77         if ((xid = get_slice_xid(argv[1]))==-1) {
78                 printf("Could not get xid for slice %s\n",argv[1]);
79                 exit(1);
80         }
81
82         if (!verify_ownership(pid, xid)) {
83                 printf("Does xid %d really own %d?\n",xid,pid);
84                 exit(1);
85         }
86
87         set_space(pid, xid, 1, CLONE_NEWNET);
88 }