4 # cnid_maint: A script to maintain the consistency of CNID databases.
6 # $Id: cnid_maint.in,v 1.4.2.1 2002-02-08 16:32:40 jmarcus Exp $
27 $STOP_CMD = '/usr/local/etc/rc.d/netatalk.sh stop';
28 $START_CMD = '/usr/local/etc/rc.d/netatalk.sh start';
30 # This ps command needs to output the following fields in the following order:
31 # USER,PID,PPID,COMMAND
32 # Below is the example of a BSD ps. A SYSV example is:
33 # /bin/ps -eflouid,pid,ppid,comm
34 $PS_CMD = '@PS@ -axouser,pid,ppid,command';
35 $DB_STAT = '@DB3_PATH@bin/db_stat';
36 $DB_RECOVER = '@DB3_PATH@bin/db_recover';
37 $DB_VERIFY = '@DB3_PATH@bin/db_verify';
38 $APPLE_VOLUMES_FILE = '@PKGCONFDIR@/AppleVolumes.default';
44 $LOCK_FILE = tmpdir() . 'cnid_maint.LOCK';
57 getopts( 'hsvVl', $opts );
77 print "Beginning run of CNID DB Maintanence script at "
78 . scalar(localtime) . ".\n\n";
80 if ( -f $LOCK_FILE ) {
81 error( 1, "Lock file $LOCK_FILE exists." );
85 unless ( open( LOCK, ">" . $LOCK_FILE ) ) {
86 error( 2, "Unable to create $LOCK_FILE: $!" );
88 flock( LOCK, LOCK_EX );
91 # Check to see if the AppleVolumes.default file exists. We will use this file
92 # to get a list of database environments to recover. We will ignore users'
93 # home directories since that could be a monumental under taking.
94 if ( !-f $APPLE_VOLUMES_FILE ) {
95 error( 2, "Unable to locate $APPLE_VOLUMES_FILE" );
98 # Use ps to get a list of running afpds. We will track all afpd PIDs that are
100 unless ( open( PS, $PS_CMD . " | $GREP afpd | $GREP -v grep |" ) ) {
101 error( 2, "Unable to open a pipe to ps: $!" );
109 my ( $user, $pid, $ppid, $command ) = split (/\s+/);
110 if ( ( $user eq "root" && $ppid != 1 ) || ( $user ne "root" ) ) {
117 if ( $children > 1 ) {
119 # We have some children. We cannot run recovery.
121 "Clients are still connected. Database recovery will not be run at this time."
128 # Shutdown the running afpds.
130 error( 0, "Shutting down afpd process..." );
131 error( 2, "Failed to shutdown afpd" )
132 if system( $STOP_CMD . ">/dev/null 2>&1" );
135 # Now, we parse AppleVolumes.default to get a list of volumes to run recovery
137 unless ( open( VOLS, $APPLE_VOLUMES_FILE ) ) {
138 error( 2, "Unable to open $APPLE_VOLUMES_FILE: $!" );
147 my ( $path, @options ) = split ( /\s+/, $_ );
148 next if ( $path =~ /^~/ );
150 foreach $option (@options) {
152 # We need to check for the dbpath option on each volume. If that
153 # option is present, we should use its path instead of the actual
155 if ( $option =~ /^dbpath:/ ) {
167 foreach $path (@paths) {
168 my $dbpath = $path . "/.AppleDB";
171 "Checking database environment $dbpath for open connections..." );
172 unless ( open( STAT, $DB_STAT . " -h $dbpath -e |" ) ) {
173 error( 1, "Failed to open a pipe to $DB_STAT: $!" );
177 # Now, check each DB environment for any open connections (db_stat calls
178 # them as references). If a volume has no references, we can do
179 # recovery on it. Only check this option if the user wants to play
185 if (/References\.$/) {
193 # Print out two different skip messages. This is just for anality.
194 if ( $connections == 1 ) {
195 error( 1, "Skipping $dbpath since it has one active connection" );
199 if ( $connections > 0 ) {
201 "Skipping $dbpath since it has $connections active connections"
207 # Run the db_recover command on the environment.
208 error( 0, "Running db_recover on $dbpath" );
209 if ( system( $DB_RECOVER . " -h $dbpath >/dev/null 2>&1" ) ) {
210 error( 1, "Failed to run db_recover on $dbpath" );
215 error( 0, "Verifying $dbpath/cnid.db" );
216 if ( system( $DB_VERIFY . " -q -h $dbpath cnid.db" ) ) {
217 error( 1, "Verification of $dbpath/cnid.db failed" );
221 error( 0, "Verifying $dbpath/devino.db" );
222 if ( system( $DB_VERIFY . " -q -h $dbpath devino.db" ) ) {
223 error( 1, "Verification of $dbpath/devino.db failed" );
227 error( 0, "Verifying $dbpath/didname.db" );
228 if ( system( $DB_VERIFY . " -q -h $dbpath didname.db" ) ) {
229 error( 1, "Verification of $dbpath/didname.db failed" );
236 # Remove the log files if told to do so.
237 unless ( opendir( DIR, $dbpath ) ) {
238 error( 1, "Failed to open $dbpath for browsing: $!" );
243 while ( defined( $file = readdir(DIR) ) ) {
244 if ( $file =~ /^log\.\d+$/ ) {
245 error( 0, "Removing $dbpath/$file" );
246 unless ( unlink( $dbpath . "/" . $file ) ) {
247 error( 1, "Failed to remove $dbpath/$file: $!" );
263 foreach ( $ENV{TMPDIR}, "/tmp" ) {
264 next unless defined && -d && -w _;
268 $tmpdir = '' unless defined $tmpdir;
273 my ( $code, $msg ) = @_;
281 print $err_types->{$code} . ": " . $msg . "\n";
283 end() if ( $code == 2 );
287 if ($START_NETATALK) {
288 error( 0, "Restarting Netatalk" );
289 if ( system( $START_CMD . " >/dev/null 2>&1" ) ) {
290 print "ERROR: Failed to restart Netatalk\n";
297 print "\nRun of CNID DB Maintenance script ended at "
298 . scalar(localtime) . ".\n";
303 print "cnid_maint.pl version $VERSION\n";
307 print "usage: cnid_maint.pl [-hlsvV]\n";
308 print "\t-h view this message\n";
309 print "\t-l remove transaction logs after running recovery\n";
311 "\t-s be extra safe in verifying there are no open DB connections\n";
312 print "\t-v print version and exit\n";
313 print "\t-V run a verification on all database files after recovery\n";