cf91effb82d12c776cf0d0b0b1055561dbb4d171
[util-vserver-pl.git] / src / netns.c
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include "vserver.h"
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <sys/types.h>
11 #include <pwd.h>
12 #include <unistd.h>
13
14 #define     SPACE_FILE      "/spaces/net"
15 #define     VSERVERCONF     "/etc/vservers/"
16
17 int
18 pl_unshare_netns(xid_t xid) {
19     char *ctx_space_file, *space_name;
20     struct passwd *slice_user;
21     int res = 0;
22     char buf[100];
23     FILE *fb;
24
25     slice_user = getpwuid(xid);
26
27     if (!slice_user)
28       return 0;
29
30     ctx_space_file=(char *) malloc(sizeof(VSERVERCONF SPACE_FILE "Z")+strlen(slice_user->pw_name));
31     if (!ctx_space_file)
32       return 0;
33
34     sprintf(ctx_space_file,VSERVERCONF "%s" SPACE_FILE, slice_user->pw_name);
35
36     if ((fb = fopen(ctx_space_file, "r")) == NULL)
37       return 0;
38
39     if (fgets(buf, sizeof(buf), fb) != NULL) {
40       res = atoi(buf);
41     }
42
43     fclose(fb);
44     free(ctx_space_file);
45     return res;
46 }