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");
30 if ((fp = fopen(slicepath, "r")) == NULL) {
31 printf("Could not open %s\n", slicepath);
35 if (fscanf(fp, "%d", &xid)==0) {
36 printf("Could not read ctx file\n");
44 int verify_ownership(int pid, int arg_xid) {
45 char procpath[PATHLEN];
48 snprintf(procpath, sizeof(procpath), "/proc/%d/vinfo");
50 if ((fp = fopen(procpath, "r")) == NULL) {
51 printf("Could not open %s\n", procpath);
55 if (fscanf(fp, "XID: %d", &xid)==0) {
56 printf("Could not read ctx file\n");
61 return (arg_xid==xid);
65 int main(int argc, char *argv[]) {
70 printf("Slice name missing. Was I invoked by vsys?\n");
76 if ((xid = get_slice_xid(argv[1]))==-1) {
77 printf("Could not get xid for slice %s\n",argv[1]);
81 if (!verify_ownership(pid, xid)) {
82 printf("Does xid %d really own %d?\n",xid,pid);
86 set_space(pid, xid, 0, CLONE_NEWNET);