5 #include <sys/socket.h>
9 char *socket_name = "/vsys/fd_fusemount.control";
10 unsigned int arg_length = 128;
12 void send_argument(int control_channel_fd, const char *source) {
14 sent=send(control_channel_fd, source, arg_length, 0);
15 if (sent<arg_length) {
16 printf("Error receiving arguments over the control buffer\n");
20 int connect_socket() {
21 int fd = socket( AF_UNIX, SOCK_STREAM, 0 );
22 struct sockaddr_un addr;
23 addr.sun_family = AF_UNIX;
24 strcpy( addr.sun_path, socket_name );
25 int len = strlen(socket_name) + sizeof(addr.sun_family);
26 assert( connect( fd, (struct sockaddr *) &addr, len ) == 0 );
30 void do_umount( char *const argv[], int n, int fd ) {
34 sprintf( buf, "%08x\n", n );
35 write( fd, buf, strlen(buf) );
39 for( i = 0; i < n; i++ ) {
40 assert( strlen(argv[i]) < 1024 );
41 sprintf( buf, "%s\n", argv[i] );
42 write( fd, buf, strlen(buf) );
46 int n2 = read( fd, inbuf, 10 );
53 int umount2( const char *mnt, int flags ) {
55 int fd = connect_socket();
58 argv[0] = "fusermount";
62 do_umount( (char **const) argv, 3, fd );
68 int get_magic_fd (char *data) {
72 data[arg_length-1]='\0';
73 ptr = strstr(data,"fd=");
77 // Found two fd= expressions
78 if (strstr(ptr+3,"fd="))
82 sscanf(ptr+3,"%d",&fd);
88 int mount(const char *source, const char *target, const char *filesystemtype,
89 unsigned long mountflags, const void *data) {
90 int fd = connect_socket();
91 int old_fuse_fd, new_fuse_fd;
95 send_argument(fd, source);
96 send_argument(fd, target );
97 send_argument(fd, filesystemtype );
98 send_argument(fd, data );
100 old_fuse_fd = get_magic_fd (data);
103 if (old_fuse_fd == -1) {
104 printf ("Reroutemount: Could not identify FUSE fd: %d\n", old_fuse_fd);
108 send_fd(fd, old_fuse_fd);
109 new_fuse_fd=receive_fd(fd);
111 if (new_fuse_fd == -1) {
112 printf ("Reroutemount: Fusemount returned bad fd: %d\n", new_fuse_fd);
116 if( dup2(new_fuse_fd, old_fuse_fd ) != new_fuse_fd ) {
117 printf ("Could not duplicate returned file descriptor\n");
126 int execv( const char *path, char *const argv[] ) {
129 if( strstr( path, "fusermount" ) == NULL ) {
130 return execv( path, argv );
133 // also make sure this is an unmount . . .
137 while( arg != NULL ) {
138 if( strcmp( arg, "-u" ) == 0 ) {
146 return execv( path, argv );
149 // Have root do any fusermounts we need done
150 fd = connect_socket();
152 do_umount( argv, n, fd );