]>
git.proxmox.com Git - librados2-perl.git/blob - PVE/RADOS.pm
11 use PVE
::RPCEnvironment
;
15 my $rados_default_timeout = 5;
18 our @ISA = qw(Exporter);
20 # Items to export into callers namespace by default. Note: do not export
21 # names by default without a very good reason. Use EXPORT_OK instead.
22 # Do not simply export all your public functions/methods/constants.
24 # This allows declaration use PVE::RADOS ':all';
25 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
27 our %EXPORT_TAGS = ( 'all' => [ qw(
31 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
40 XSLoader
::load
('PVE::RADOS', $VERSION);
43 my ($fh, $cmd, $data) = @_;
45 local $SIG{PIPE
} = 'IGNORE';
47 my $bin = pack "a L/a*", $cmd, $data || '';
48 my $res = syswrite $fh, $bin;
50 die "write data failed - $!\n" if !defined($res);
58 local $SIG{PIPE
} = 'IGNORE';
60 while (length($head) < 5) {
61 last if !sysread $fh, $head, 5 - length($head), length($head);
63 die "partial read\n" if length($head) < 5;
65 my ($cmd, $len) = unpack "a L", $head;
68 while (length($data) < $len) {
69 last if !sysread $fh, $data, $len - length($data), length($data);
71 die "partial data read\n" if length($data) < $len;
73 return wantarray ?
($cmd, $data) : $data;
76 my $kill_worker = sub {
79 return if !$self->{cpid
};
80 return if $self->{__already_killed
};
82 $self->{__already_killed
} = 1;
84 close($self->{child
}) if defined($self->{child
});
86 # only kill if we created the process
87 return if $self->{pid
} != $$;
89 kill(9, $self->{cpid
});
90 waitpid($self->{cpid
}, 0);
94 my ($self, $cmd, $data, $expect_tag) = @_;
96 $expect_tag = '>' if !$expect_tag;
98 die "detected forked connection" if $self->{pid
} != $$;
102 &$writedata($self->{child
}, $cmd, $data) if $expect_tag ne 'S';
103 ($restag, $raw) = &$readdata($self->{child
});
105 eval { PVE
::Tools
::run_with_timeout
($self->{timeout
}, $code); };
107 &$kill_worker($self);
110 if ($restag eq 'E') {
112 die "unknown error\n";
115 die "got unexpected result\n" if $restag ne $expect_tag;
121 my ($class, %params) = @_;
123 my $rpcenv = PVE
::RPCEnvironment
::get
();
125 socketpair(my $child, my $parent, AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
)
126 || die "socketpair: $!";
130 die "unable to fork - $!\n" if !defined($cpid);
134 my $timeout = delete $params{timeout
} || $rados_default_timeout;
136 $self->{timeout
} = $timeout;
139 if ($cpid) { # parent
142 $self->{cpid
} = $cpid;
143 $self->{child
} = $child;
145 &$sendcmd($self, undef, undef, 'S'); # wait for sync
150 PVE
::INotify
::inotify_close
();
152 if (my $atfork = $rpcenv->{atfork
}) {
162 $conn = pve_rados_create
() ||
163 die "unable to create RADOS object\n";
165 pve_rados_conf_set
($conn, 'client_mount_timeout', $timeout);
167 foreach my $k (keys %params) {
168 pve_rados_conf_set
($conn, $k, $params{$k});
171 pve_rados_connect
($conn);
174 &$writedata($parent, 'E', $err);
177 &$writedata($parent, 'S');
179 $self->{conn
} = $conn;
182 my ($cmd, $data) = &$readdata($parent);
188 if ($cmd eq 'M') { # rados monitor commands
189 $res = pve_rados_mon_command
($self->{conn
}, [ $data ]);
190 } elsif ($cmd eq 'C') { # class methods
191 my $aref = decode_json
($data);
192 my $method = shift @$aref;
193 $res = encode_json
($self->$method(@$aref));
195 die "invalid command\n";
199 &$writedata($parent, 'E', $err);
202 &$writedata($parent, '>', $res);
215 #print "$$: DESTROY WAIT0\n";
216 &$kill_worker($self);
217 #print "$$: DESTROY WAIT\n";
219 #print "$$: DESTROY SHUTDOWN\n";
220 pve_rados_shutdown
($self->{conn
}) if $self->{conn
};
225 my ($self, @args) = @_;
228 my $data = encode_json
(['cluster_stat', @args]);
229 my $raw = &$sendcmd($self, 'C', $data);
230 return decode_json
($raw);
232 return pve_rados_cluster_stat
($self->{conn
});
236 # example1: { prefix => 'get_command_descriptions'})
237 # example2: { prefix => 'mon dump', format => 'json' }
239 my ($self, $cmd) = @_;
241 $cmd->{format
} = 'json' if !$cmd->{format
};
243 my $json = encode_json
($cmd);
245 my $raw = &$sendcmd($self, 'M', $json);
247 if ($cmd->{format
} && $cmd->{format
} eq 'json') {
248 return length($raw) ? decode_json
($raw) : undef;
259 PVE::RADOS - Perl bindings for librados
265 my $rados = PVE::RADOS::new();
266 my $stat = $rados->cluster_stat();
267 my $res = $rados->mon_command({ prefix => 'mon dump', format => 'json' });
271 Perl bindings for librados.
279 Dietmar Maurer, E<lt>dietmar@proxmox.com<gt>