This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
/
expire
222 lines (190 loc) · 5.67 KB
/
expire
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/perl
#
# MiniVend session expiration
#
# $Id: expire,v 1.17 1999/08/13 01:36:08 mike Exp $
#
# This program is largely based on Vend 0.2
# Copyright 1995 by Andrew M. Wilcox <awilcox@world.std.com>
#
# Portions from Vend 0.3
# Copyright 1995 by Andrew M. Wilcox <awilcox@world.std.com>
#
# Enhancements made by and
# Copyright 1996-1999 by Michael J. Heins <mikeh@minivend.com>
#
# See the file 'Changes' for information.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
BEGIN {
eval {
require 5.004;
require FindBin;
1 and $Global::VendRoot = "$FindBin::RealBin";
1 and $Global::VendRoot =~ s/.bin$//;
};
($Global::VendRoot = $ENV{MINIVEND_ROOT})
if defined $ENV{MINIVEND_ROOT};
$Global::VendRoot = $Global::VendRoot || '/home/minivend';
$ENV{MINIVEND_STORABLE} = 1
if -f "$Global::VendRoot/_session_storable";
$ENV{MINIVEND_STORABLE_DB} = 1
if -f "$Global::VendRoot/_db_storable";
}
### END CONFIGURABLE VARIABLES
sub dontwarn { $FindBin::RealBin; }
$Global::ConfigFile = 'minivend.cfg';
$Global::HammerLock = 20;
$Global::ErrorFile = 'error.log';
use lib $Global::VendRoot;
use lib "$Global::VendRoot/lib";
use strict;
use Fcntl;
my $DEBUG = 0;
$Vend::SessionName = 'utility';
$Global::SendMailLocation = 'none';
#select a DBM
BEGIN {
$Global::GDBM = $Global::DB_File = 0;
AUTO: {
last AUTO if
(defined $ENV{MINIVEND_DBFILE} and $Global::DB_File = 1);
last AUTO if
(defined $ENV{MINIVEND_NODBM});
eval {require GDBM_File and $Global::GDBM = 1} ||
eval {require DB_File and $Global::DB_File = 1};
}
if($Global::GDBM) {
require Vend::Table::GDBM;
import GDBM_File;
$Global::GDBM = 1;
}
elsif($Global::DB_File) {
require Vend::Table::DB_File;
import DB_File;
$Global::DB_File = 1;
}
else {
die "Can't use expire without DBM sessions.\n";
}
}
use Vend::Session;
use Vend::Config qw(get_catalog_default parse_time);
use vars qw/$opt_c $opt_r $opt_e/;
my $USAGE = <<EOF;
usage: expire [-r] -c catalog [-e 'SessionExpire']
or
expire [-r] [-e 'SessionExpire'] sessionfile [sessionfile.lock]
OPTIONS
-c catalog specify a catalog from minivend.cfg
-e time vary expire time from default in catalog.cfg.
time is a string like '4 hours' or '2 days'
-r reorganize database and recover lost disk space
EOF
my $catalog;
my $expiretime;
my $reorg;
$Vend::Cfg = { FileCreationMask => 0600 };
use Getopt::Std;
getopts('c:e:r') or die "$@\n$USAGE\n";
$catalog = $opt_c || undef;
$expiretime = $opt_e || undef;
$reorg = $opt_r;
GETOPT: {
last GETOPT if $catalog;
$Vend::Cfg->{SessionDatabase} = shift
|| die $USAGE;
$Vend::Cfg->{SessionDatabase} =~ s/\.(gdbm|db)$//;
$Vend::Cfg->{SessionLockFile} = shift;
if (defined $Vend::Cfg->{SessionLockFile}) {
die <<EOF unless -f $Vend::Cfg->{SessionLockFile};
Session lock file '$Vend::Cfg->{SessionLockFile}' doesn't exist.
Create one if you are sure the MiniVend server is down, then try
again.
EOF
}
elsif (-f "$Vend::Cfg->{SessionDatabase}.lock") {
$Vend::Cfg->{SessionLockFile} =
"$Vend::Cfg->{SessionDatabase}.lock";
}
else {
my $dir = $Vend::Cfg->{SessionDatabase};
$dir = s:/?([^/]+)$::;
my $file = $1 || die $USAGE;
die "Aborting, no lock files found!\n"
unless -f "$dir/$file.lock";
}
} # END GETOPT
die "too many args, aborting.\n"
if @ARGV;
my (%wanted) = qw( sessionexpire SessionExpire
sessiondatabase SessionDatabase
sessionlockfile SessionLockFile
scratchdir ScratchDir
errorfile ErrorFile);
if(defined $catalog) {
my($name,$dir,$param);
chdir $Global::VendRoot;
open(GLOBAL, $Global::ConfigFile) or
die "No global configuration file? Aborting.\n";
while(<GLOBAL>) {
next unless /^\s*catalog\s+$catalog\s+/i;
chomp;
s/^\s+//;
(undef,$name,$dir,$param) = split /\s+/, $_, 4;
last;
}
close GLOBAL;
die "Catalog $catalog not found in $Global::ConfigFile.\n"
unless defined $name;
chdir $dir or die "chdir to $dir: $!\n";
open(CATALOG, 'catalog.cfg') or
die "No catalog configuration file? Aborting.\n";
my (%seen);
while(<CATALOG>) {
next unless /\S/;
next if /^\s*#/;
chomp;
s/^\s+//;
s/\s+$//;
($name,$param) = split /\s+/, $_, 2;
next unless defined $wanted{lc $name};
$seen{$wanted{lc $name}} = $param;
}
close CATALOG;
for(values %wanted) {
next if defined $seen{$_};
$seen{$_} = get_catalog_default($_);
}
for(keys %seen) {
$Vend::Cfg->{$_} = $seen{$_};
warn "$_: $Vend::Cfg->{$_}\n"
if $DEBUG;
}
$expiretime = $Vend::Cfg->{SessionExpire} unless defined $expiretime;
}
else {
$Vend::Cfg->{ScratchDir} = '/tmp';
$Vend::Cfg->{ErrorFile} = $Global::ErrorFile;
$expiretime = '1 day' unless defined $expiretime;
}
$Vend::Cfg->{SessionExpire} = parse_time('SessionExpire', $expiretime);
die $USAGE unless defined $Vend::Cfg->{SessionLockFile};
my $db_reorg = $Global::DB_File ? $reorg : undef;
undef $reorg unless $Global::GDBM;
get_session() unless $reorg;
expire_sessions($reorg);
release_session() unless $reorg;
system "compact ." if $db_reorg;