Added a log file.
[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 FILE *logfd;
15
16 void receive_argument(int control_channel_fd, char *source) {
17     int received;
18     received=recv(control_channel_fd, source, arg_length, 0);
19     if (received<arg_length) {
20         fprintf(logfd,"Error receiving arguments over the control buffer\n");
21         close(logfd);
22         exit(1);
23     }
24 }
25
26 int set_magic_fd (char *data, int new_fd) {
27     char *ptr;
28     int fd;
29
30     data[arg_length-1]='\0';
31     ptr = strstr(data,"fd=");
32     if (!ptr)
33         return -1;
34
35     // Found two fd= expressions
36     if (strstr(ptr+3,"fd="))
37         return -1;
38
39     if (*(ptr+3)!='\0') {
40         char *new_data = (char *) malloc(arg_length);
41         char *head = (char *) malloc(arg_length);
42         char *tail = (char *) malloc(arg_length);
43         sscanf(ptr+3,"%d",&fd);
44         strncpy(head, data, ptr - data);
45         tail = strchr(ptr+3,',');
46         sprintf(new_data,"%sfd=%d,%s",head,new_fd,tail);
47         strcpy(data,new_data);
48         free(new_data);
49         free(head);
50         free(tail);
51         return fd;
52     }
53     else
54         return -1;
55 }
56
57 void check_source(char *source) {
58     source[arg_length-1]='\0';
59     if (strchr(source,'/') || strstr(source,"..")) {
60         fprintf(logfd, "Tried mounting with source = %s\n", source);
61         close(logfd);
62         exit(1);
63     }
64 }
65
66 void check_target(char *target) {
67     target[arg_length-1]='\0';
68     if (strstr(target,"..")) {
69         fprintf(logfd,"Tried mounting with target = %s\n", target);
70         close(logfd);
71         exit(1);
72     }
73 }
74
75 void check_fstype(char *filesystemtype) {
76     if (strncmp(filesystemtype,"fuse",4)) {
77         fprintf(logfd,"Tried mounting filesystem type %s\n", filesystemtype);
78         close(logfd);
79         exit(1);
80     }
81 }
82
83 int main(int argc, char *argv[]) {
84     int control_channel_fd, magic_fd, mount_fd;
85     char source[128],target[128],filesystemtype[128],data[128],slice_target[1024];
86
87     int received;
88
89     if (argc < 3) {
90         printf("This script is called by vsys.\n");
91         exit(1);
92     }
93
94     char *slice_name = argv[1];
95
96     logfd=fopen("/tmp/fuselog","w");
97     if (!logfd) {logfd = stderr;}
98     
99     sscanf(argv[2],"%d", &control_channel_fd);
100
101     if (control_channel_fd <3 || control_channel_fd > 1023) {
102         printf ("Got control_channel_fd = %d\n", control_channel_fd);
103         exit(1);
104     }
105
106     receive_argument(control_channel_fd, source);
107     receive_argument(control_channel_fd, target);
108     receive_argument(control_channel_fd, filesystemtype);
109     receive_argument(control_channel_fd, data);
110
111     mount_fd = receive_fd (control_channel_fd);
112
113     if (mount_fd < 2) {
114         printf("mount_fd = %d\n", mount_fd);
115         exit(1);
116     }
117
118     set_magic_fd(data, mount_fd);
119
120
121     check_source(source);
122     check_target(target);
123     check_fstype(filesystemtype);
124
125     sprintf(slice_target,"/vservers/%s/%s", slice_name, target);
126
127     fprintf(logfd, "Mount fd: %d Source: %s slice_target: %s fstype: %s mountflags: %d data: %s\n", mount_fd, source, slice_target, filesystemtype, mountflags, data);
128
129     if (!mount(source, slice_target, filesystemtype, mountflags, data)) {
130         send_fd(control_channel_fd, mount_fd);
131     }
132     else {
133         printf ("Error executing mount\n");
134         exit(1);
135     }
136
137     close(logfd);
138     return 0;
139 }