Subversion Repositories DevTools

Rev

Rev 2571 | Rev 6776 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2571 Rev 5398
Line 1... Line 1...
1
package Utils;
1
package Utils;
2
use File::Basename;
2
use File::Basename;
3
use Data::Dumper;
3
use Data::Dumper;
4
 
4
 
5
sub trunc ($) {
5
sub trunc ($) {
6
	my $file = shift;
6
    my $file = shift;
7
	open my $fh, ">$file" or die "Can't write $file: $!\n";
7
    open my $fh, ">$file" or die "Can't write $file: $!\n";
8
	print $fh '';
8
    print $fh '';
9
	close $fh;
9
    close $fh;
10
}
10
}
11
 
11
 
12
sub checkpid ($) {
12
sub checkpid ($) {
13
	my $conf = shift;
13
    my $conf = shift;
14
	my $pidfile = $conf->{'pidfile'};
14
    my $pidfile = $conf->{'pidfile'};
15
	my $logger = $conf->{logger};
15
    my $logger = $conf->{logger};
16
 
16
 
17
	trunc $pidfile unless -f $pidfile;
17
    trunc $pidfile unless -f $pidfile;
18
 
18
 
19
	open my $fh, $pidfile or $logger->err("Can't read pidfile $pidfile: $!");
19
    open my $fh, $pidfile or $logger->err("Can't read pidfile $pidfile: $!");
20
	my ($pid) = <$fh>;
20
    my ($pid) = <$fh>;
21
	close $fh;
21
    close $fh;
22
 
22
 
23
	if (defined $pid) {
23
    if (defined $pid) {
24
		chomp $pid;
24
        chomp $pid;
25
		$logger->err("Process with pid $pid already running") if 0 < int $pid && kill 0, $pid;
25
        $logger->err("Process with pid $pid already running") if 0 < int $pid && kill 0, $pid;
26
	}
26
    }
27
}
27
}
28
 
28
 
29
sub writepid ($) {
29
sub writepid ($) {
30
	my $conf = shift;
30
    my $conf = shift;
31
	my $logger = $conf->{logger};
31
    my $logger = $conf->{logger};
32
	my $pidfile = $conf->{'pidfile'};
32
    my $pidfile = $conf->{'pidfile'};
33
 
33
 
34
	open my $fh, ">$pidfile" or $logger->err("Can't write pidfile: $!");
34
    open my $fh, ">$pidfile" or $logger->err("Can't write pidfile: $!");
35
	print $fh "$$\n";
35
    print $fh "$$\n";
36
	close $fh;
36
    close $fh;
37
}
37
}
38
 
38
 
39
#-------------------------------------------------------------------------------
39
#-------------------------------------------------------------------------------
40
# Function        : trimstr
40
# Function        : trimstr
41
#
41
#
Line 44... Line 44...
44
# Inputs          : Array of strings
44
# Inputs          : Array of strings
45
#
45
#
46
# Returns         : Array of trimmed strings
46
# Returns         : Array of trimmed strings
47
#
47
#
48
sub trimstr (@) {
48
sub trimstr (@) {
49
	my @str = @_;
49
    my @str = @_;
50
 
50
 
51
	for (@str) {
51
    for (@str) {
52
		chomp;
52
        chomp;
53
		s/^[\t\s]+//;
53
        s/^[\t\s]+//;
54
		s/[\t\s]+$//;
54
        s/[\t\s]+$//;
55
	}
55
    }
56
 
56
 
57
	return @str;
57
    return @str;
58
}
58
}
59
 
59
 
60
#-------------------------------------------------------------------------------
60
#-------------------------------------------------------------------------------
61
# Function        : readconf
61
# Function        : readconf
62
#
62
#
Line 88... Line 88...
88
 
88
 
89
sub readconf
89
sub readconf
90
{
90
{
91
    my ($conffile, $pdata) = @_;
91
    my ($conffile, $pdata) = @_;
92
    my @errors;
92
    my @errors;
93
	my $conf;
93
    my $conf;
94
    my $ignored;
94
    my $ignored;
95
 
95
 
96
    if ( open my $fh, $conffile )
96
    if ( open my $fh, $conffile )
97
    {
97
    {
98
	    while (<$fh>) {
98
        while (<$fh>) {
99
		    next if /^[\t\w]+#/;
99
            next if /^[\t\w]+#/;
100
		    s/#.*//;
100
            s/#.*//;
101
 
101
 
102
		    my ($key, $val) = trimstr split '=', $_, 2;
102
            my ($key, $val) = trimstr split '=', $_, 2;
103
		    next unless defined $val;
103
            next unless defined $val;
104
 
104
 
105
            #
105
            #
106
            #   Create hash of key : value
106
            #   Create hash of key : value
107
            #   Special handling of int_list. Multiple definitions are allowed
107
            #   Special handling of int_list. Multiple definitions are allowed
108
            #
108
            #
Line 113... Line 113...
113
                    push @errors, "Multiple configuration of: $key";
113
                    push @errors, "Multiple configuration of: $key";
114
                }
114
                }
115
            } else {
115
            } else {
116
                $conf->{$key} = $val;
116
                $conf->{$key} = $val;
117
            }
117
            }
118
	    }
118
        }
119
	    close $fh;
119
        close $fh;
120
 
120
 
121
        #
121
        #
122
        #   Validate mandatory entries
122
        #   Validate mandatory entries
123
        #   Insert defaults that are not present
123
        #   Insert defaults that are not present
124
        #
124
        #