[End of patch series]
[vsys-scripts.git] / fd_fusemount.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <sys/mount.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include "fdpass.h"
9
10 unsigned int rcvbuf = 16*1024*1024;
11 unsigned int arg_length = 128;
12 unsigned int mountflags = MS_NODEV | MS_NOSUID;
13
14 void receive_argument(int control_channel_fd, char *source) {
15     int received;
16     received=recv(control_channel_fd, source, arg_length, 0);
17     if (received<arg_length) {
18         printf("Error receiving arguments over the control buffer\n");
19         exit(1);
20     }
21 }
22
23 int get_magic_fd (char *data) {
24     char *ptr;
25     int fd;
26
27     data[arg_length-1]='\0';
28     ptr = strstr(data,"fd=");
29     if (!ptr)
30         return -1;
31
32     // Found two fd= expressions
33     if (strstr(ptr+3,"fd="))
34         return -1;
35
36     if (*(ptr+3)!='\0') {
37         sscanf(ptr+3,"%d",&fd);
38         return fd;
39     }
40     else
41         return -1;
42 }
43
44 void check_source(char *source) {
45     source[arg_length-1]='\0';
46     if (strchr(source,'/') || strstr(source,"..")) {
47         printf("Tried mounting with source = %s\n", source);
48         exit(1);
49     }
50 }
51
52 void check_target(char *target) {
53     target[arg_length-1]='\0';
54     if (strstr(target,"..")) {
55         printf("Tried mounting with target = %s\n", target);
56         exit(1);
57     }
58 }
59
60 void check_fstype(char *filesystemtype) {
61     if (strncmp(filesystemtype,"fuse",4)) {
62         printf("Tried mounting filesystem type %s\n", filesystemtype);
63     }
64 }
65
66 int main(int argc, char *argv[]) {
67     int control_channel_fd, magic_fd, mount_fd;
68     char source[128],target[128],filesystemtype[128],data[128],slice_target[256];
69
70     int received;
71
72     if (argc < 3) {
73         printf("This script is called by vsys.\n");
74         exit(1);
75     }
76
77     char *slice_name = argv[1];
78     
79     sscanf(argv[2],"%d", &control_channel_fd);
80
81     if (control_channel_fd <3 || control_channel_fd > 1023) {
82         printf ("Got control_channel_fd = %d\n", control_channel_fd);
83         exit(1);
84     }
85
86     receive_argument(control_channel_fd, source);
87     receive_argument(control_channel_fd, target);
88     receive_argument(control_channel_fd, filesystemtype);
89     receive_argument(control_channel_fd, data);
90     
91     magic_fd = get_magic_fd (data);
92
93     if (magic_fd < 3) {
94         printf("Got fd %d in fusemount\n",magic_fd);
95         exit(1);
96     }
97
98     mount_fd = receive_fd (control_channel_fd);
99
100     if (mount_fd != magic_fd) {
101         printf("mount_fd (%d) != magic_fd (%d)\n", mount_fd, magic_fd);
102         exit(1);
103     }
104
105     check_source(source);
106     check_target(target);
107     check_fstype(filesystemtype);
108
109     sprintf(slice_target,"/vservers/%s/%s", target);
110
111     if (!mount(source, slice_target, filesystemtype, mountflags, data)) {
112         send_fd(control_channel_fd, magic_fd);
113     }
114
115 }