3 # fioc.pl: A Perl conversion of the fioc example IOCTL server program
4 # from the FUSE distribution. I've endeavored to stay pretty close
5 # structure-wise to the C version, while using Perl-specific features.
6 # I wrote this to provide a way to verify my ioctl() wrapper
7 # implementation would work properly. So far, it seems to, and it will
8 # interoperate with the C client as well.
17 local $SIG{'__WARN__'} = \&Carp::cluck;
23 my $fioc_size :shared = 0;
24 use constant FIOC_NAME => 'fioc';
25 my $fioc_buf :shared = '';
26 use constant FIOC_NONE => 0;
27 use constant FIOC_ROOT => 1;
28 use constant FIOC_FILE => 2;
31 require 'linux/ioctl.ph';
34 require 'sys/ioccom.ph';
37 our %sizeof = ('size_t' => length(pack('L!')));
38 sub FIOC_GET_SIZE { _IOR(ord 'E', 0, 'size_t'); }
39 sub FIOC_SET_SIZE { _IOW(ord 'E', 1, 'size_t'); }
40 sub TCGETS { 0x5401; }
44 print 'called ', (caller(0))[3], "\n";
45 return 0 if $size == $fioc_size;
47 if ($size < $fioc_size) {
48 $fioc_buf = substr($fioc_buf, 0, $size);
51 $fioc_buf .= "\0" x ($size - $fioc_size);
59 print 'called ', (caller(0))[3], "\n";
60 if ($size > $fioc_size) {
61 return fioc_resize($size);
68 print 'called ', (caller(0))[3], "\n";
69 return FIOC_ROOT if $path eq '/';
70 return FIOC_FILE if $path eq '/' . FIOC_NAME;
76 print 'called ', (caller(0))[3], "\n";
77 my @stbuf = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
80 $stbuf[5] = (split(/\s+/, $())[0];
81 $stbuf[8] = $stbuf[9] = time();
83 my $type = fioc_file_type($path);
84 if ($type == FIOC_ROOT) {
85 $stbuf[2] = S_IFDIR | 0755;
88 elsif ($type == FIOC_FILE) {
89 $stbuf[2] = S_IFREG | 0644;
91 $stbuf[7] = $fioc_size;
100 my ($path, $flags, $info) = @_;
101 print 'called ', (caller(0))[3], "\n";
103 return 0 if fioc_file_type($path) != FIOC_NONE;
108 my ($path, $size, $offset) = @_;
109 print 'called ', (caller(0))[3], "\n";
111 return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
112 return q{} if $offset > $fioc_size;
114 if ($size > $fioc_size - $offset) {
115 $size - $fioc_size - $offset;
118 return substr($fioc_buf, $offset, $size);
122 my ($path, $data, $offset) = @_;
123 print 'called ', (caller(0))[3], "\n";
126 return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
127 return -&ENOMEM if fioc_expand($offset + length($data));
129 substr($fioc_buf, $offset, length($data), $data);
130 return length($data);
134 my ($path, $size) = @_;
135 print 'called ', (caller(0))[3], "\n";
138 return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
140 return fioc_resize($size);
144 my ($path, $offset) = @_;
145 print 'called ', (caller(0))[3], "\n";
147 return -&EINVAL if fioc_file_type($path) != FIOC_ROOT;
149 return ('.', '..', FIOC_NAME, 0);
153 my ($path, $cmd, $flags, $data) = @_;
154 print 'called ', (caller(0))[3], "\n";
156 return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
157 return -&ENOSYS if $flags & FUSE_IOCTL_COMPAT;
159 if ($cmd == FIOC_GET_SIZE) {
160 return(0, pack('L!', $fioc_size));
162 elsif ($cmd == FIOC_SET_SIZE) {
164 fioc_resize(unpack('L!', $data));
167 elsif ($cmd == TCGETS) {
168 # perl sends TCGETS as part of calling isatty() on opening a file;
169 # this appears to be a more canonical answer
176 croak("Fuse doesn't have ioctl") unless Fuse::fuse_version() >= 2.8;
179 'mountpoint' => $ARGV[0],
180 'getattr' => 'main::fioc_getattr',
181 'readdir' => 'main::fioc_readdir',
182 'truncate' => 'main::fioc_truncate',
183 'open' => 'main::fioc_open',
184 'read' => 'main::fioc_read',
185 'write' => 'main::fioc_write',
186 'ioctl' => 'main::fioc_ioctl',