Fd passing script for fusemount
[vsys-scripts.git] / fd_fusemount.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include "fdpass.h"
7
8 unsigned int rcvbuf = 16*1024*1024;
9 unsigned int arg_length = 128;
10
11 void receive_argument(int control_channel_fd, char *source) {
12     int received;
13     received=recv(control_channel_fd, source, arg_length);
14     if (received<arg_length) {
15         printf("Error receiving arguments over the control buffer\n");
16         exit(1);
17     }
18 }
19
20 int get_magic_fd (char *data) {
21     char *ptr;
22     int fd;
23
24     data[arg_length-1]='\0';
25     ptr = strstr(data,"fd=");
26     if (!ptr)
27         return -1;
28
29     // Found two fd= expressions
30     if (strstr(ptr+3,"fd="))
31         return -1;
32
33     if (*(ptr+3)!='\0') {
34         sscanf(ptr+3,"%d",&fd);
35         return fd;
36     }
37     else
38         return -1;
39 }
40
41 int main(int argc, char *argv[]) {
42     int control_channel_fd, magic_fd;
43     char source[128],target[128],filesystemtype[128],data[129];
44
45     int received;
46
47     if (argc < 3) {
48         printf("This script is called by vsys.\n");
49         exit(1);
50     }
51
52     sscanf(argv[2],"%d", &control_channel_fd);
53
54     receive_argument(control_channel_fd, source);
55     receive_argument(control_channel_fd, target);
56     receive_argument(control_channel_fd, filesystemtype);
57     receive_argument(control_channel_fd, data);
58     
59     magic_fd = get_magic_fd (data);
60
61     if (magic_fd < 3) {
62         printf("Got fd %d in fusemount\n",magic_fd);
63         exit(1);
64     }
65
66
67
68 }