| 1 | # ***** BEGIN LICENSE BLOCK ***** | ||
| 2 | # Zimbra Collaboration Suite Server | ||
| 3 | # Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Zimbra, Inc. | ||
| 4 | # | ||
| 5 | # The contents of this file are subject to the Zimbra Public License | ||
| 6 | # Version 1.3 ("License"); you may not use this file except in | ||
| 7 | # compliance with the License. You may obtain a copy of the License at | ||
| 8 | # http://www.zimbra.com/license. | ||
| 9 | # | ||
| 10 | # Software distributed under the License is distributed on an "AS IS" | ||
| 11 | # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. | ||
| 12 | # ***** END LICENSE BLOCK ***** | ||
| 13 | |||
| 14 | package ZCS::LocalConfig::Command; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use IO::File (); | ||
| 19 | use IPC::Open3 qw(open3); | ||
| 20 | use Symbol qw(gensym); | ||
| 21 | use base qw(ZCS::LocalConfig::_base); | ||
| 22 | |||
| 23 | our $VERSION = 1.0; | ||
| 24 | |||
| 25 | { | ||
| 26 | my $Command; | ||
| 27 | |||
| 28 | sub command { | ||
| 29 | my ( $self, $value ) = @_; | ||
| 30 | if ( ref($self) ) { | ||
| 31 | $self->{command} = $value if $value; | ||
| 32 | return $self->{command} if $self->{command}; | ||
| 33 | } | ||
| 34 | $Command = $value if $value; | ||
| 35 | return $Command || "/opt/zimbra/bin/zmlocalconfig"; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | sub load { | ||
| 40 | my ( $self, @keys ) = ( shift, @_ ); | ||
| 41 | |||
| 42 | my $args = { opts => ["--show"] }; | ||
| 43 | $args = shift(@keys) if ( @keys and ref( $keys[0] ) eq "HASH" ); | ||
| 44 | |||
| 45 | my @opts = ( $args->{opts} ? @{ $args->{opts} } : () ); | ||
| 46 | my @cmd = ( $self->command, @opts, @keys ); | ||
| 47 | |||
| 48 | local ($!); | ||
| 49 | my $tout = IO::File->new | ||
| 50 | or return $self->_set_err( 1, "load: open OUT file failed: $!" ); | ||
| 51 | my $terr = IO::File->new_tmpfile | ||
| 52 | or return $self->_set_err( 1, "load: open ERR file failed: $!" ); | ||
| 53 | |||
| 54 | local ($@); | ||
| 55 | my $pid = open3( gensym(), $tout, $terr, @cmd ); | ||
| 56 | return $self->_set_err( 1, "load: command '@cmd' failed: $@" ) if $@; | ||
| 57 | return $self->_set_err( 1, "load: fork '@cmd' failed" ) if !$pid; | ||
| 58 | |||
| 59 | my %lc; | ||
| 60 | while ( my $line = <$tout> ) { | ||
| 61 | chomp($line); | ||
| 62 | my ( $key, $val ) = split( /\s+=\s+/, $line, 2 ); | ||
| 63 | $lc{$key} = $val; | ||
| 64 | } | ||
| 65 | close($tout); | ||
| 66 | |||
| 67 | waitpid( $pid, 0 ); | ||
| 68 | seek( $terr, 0, 0 ); | ||
| 69 | |||
| 70 | my @err; | ||
| 71 | while ( my $line = <$terr> ) { | ||
| 72 | chomp($line); | ||
| 73 | push( @err, $line ); | ||
| 74 | } | ||
| 75 | close($terr); | ||
| 76 | |||
| 77 | return $self->_set_err( 1, "load: " . join( "; ", @err ) ) | ||
| 78 | if (@err); | ||
| 79 | |||
| 80 | $self->_set_err( 0, "load: no data returned" ) unless (%lc); | ||
| 81 | $self->_set_conf( \%lc ); | ||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; |