(no commit message)
[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 "fdpass.h"
8
9 unsigned int rcvbuf = 16*1024*1024;
10 unsigned int arg_length = 128;
11 unsigned int mountflags = MS_NODEV | MS_NOSUID;
12
13 void receive_argument(int control_channel_fd, char *source) {
14     int received;
15     received=recv(control_channel_fd, source, arg_length, 0);
16     if (received<arg_length) {
17         printf("Error receiving arguments over the control buffer\n");
18         exit(1);
19     }
20 }
21
22 int get_magic_fd (char *data) {
23     char *ptr;
24     int fd;
25
26     data[arg_length-1]='\0';
27     ptr = strstr(data,"fd=");
28     if (!ptr)
29         return -1;
30
31     // Found two fd= expressions
32     if (strstr(ptr+3,"fd="))
33         return -1;
34
35     if (*(ptr+3)!='\0') {
36         sscanf(ptr+3,"%d",&fd);
37         return fd;
38     }
39     else
40         return -1;
41 }
42
43 int main(int argc, char *argv[]) {
44     int control_channel_fd, magic_fd, mount_fd;
45     char source[128],target[128],filesystemtype[128],data[128],slice_target[256];
46
47     int received;
48
49     if (argc < 3) {
50         printf("This script is called by vsys.\n");
51         exit(1);
52     }
53
54     char *slice_name = argv[1];
55     
56     sscanf(argv[2],"%d", &control_channel_fd);
57
58     if (control_channel_fd <3 || control_channel_fd > 1023) {
59         printf ("Got control_channel_fd = %d\n", control_channel_fd);
60         exit(1);
61     }
62
63     receive_argument(control_channel_fd, source);
64     receive_argument(control_channel_fd, target);
65     receive_argument(control_channel_fd, filesystemtype);
66     receive_argument(control_channel_fd, data);
67     
68     magic_fd = get_magic_fd (data);
69
70     if (magic_fd < 3) {
71         printf("Got fd %d in fusemount\n",magic_fd);
72         exit(1);
73     }
74
75     mount_fd = receive_fd (control_channel_fd);
76
77     if (mount_fd != magic_fd) {
78         printf("mount_fd (%d) != magic_fd (%d)\n", mount_fd, magic_fd);
79         exit(1);
80     }
81
82     check_source(source);
83     check_target(target);
84     check_fstype(filesystemtype);
85
86     sprintf(slice_target,"/vservers/%s/%s", target);
87
88     if (!mount(source, slice_target, filesystemtype, mountflags, data)) {
89         send_fd(control_channel_fd, magic_fd);
90     }
91
92 }