4 # cnid_maint: A script to maintain the consistency of CNID databases.
6 # $Id: cnid_maint.in,v 1.4.2.3 2002-02-11 21:21:30 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 );
78 die "You must be root to run this script.\n";
81 print "Beginning run of CNID DB Maintanence script at "
82 . scalar(localtime) . ".\n\n";
84 if ( -f $LOCK_FILE ) {
85 error( 1, "Lock file $LOCK_FILE exists." );
89 unless ( open( LOCK, ">" . $LOCK_FILE ) ) {
90 error( 2, "Unable to create $LOCK_FILE: $!" );
92 flock( LOCK, LOCK_EX );
95 # Check to see if the AppleVolumes.default file exists. We will use this file
96 # to get a list of database environments to recover. We will ignore users'
97 # home directories since that could be a monumental under taking.
98 if ( !-f $APPLE_VOLUMES_FILE ) {
99 error( 2, "Unable to locate $APPLE_VOLUMES_FILE" );
102 # Use ps to get a list of running afpds. We will track all afpd PIDs that are
104 unless ( open( PS, $PS_CMD . " | $GREP afpd | $GREP -v grep |" ) ) {
105 error( 2, "Unable to open a pipe to ps: $!" );
113 my ( $user, $pid, $ppid, $command ) = split (/\s+/);
114 if ( ( $user eq "root" && $ppid != 1 ) || ( $user ne "root" ) ) {
121 if ( $children > 1 ) {
123 # We have some children. We cannot run recovery.
125 "Clients are still connected. Database recovery will not be run at this time."
132 # Shutdown the running afpds.
134 error( 0, "Shutting down afpd process..." );
135 error( 2, "Failed to shutdown afpd" )
136 if system( $STOP_CMD . ">/dev/null 2>&1" );
139 # Now, we parse AppleVolumes.default to get a list of volumes to run recovery
141 unless ( open( VOLS, $APPLE_VOLUMES_FILE ) ) {
142 error( 2, "Unable to open $APPLE_VOLUMES_FILE: $!" );
151 my ( $path, @options ) = split ( /\s+/, $_ );
152 next if ( $path =~ /^~/ );
154 foreach $option (@options) {
156 # We need to check for the dbpath option on each volume. If that
157 # option is present, we should use its path instead of the actual
159 if ( $option =~ /^dbpath:/ ) {
171 foreach $path (@paths) {
172 my $dbpath = $path . "/.AppleDB";
174 error( 1, "Database environment $dbpath does not exist" );
179 "Checking database environment $dbpath for open connections..." );
180 unless ( open( STAT, $DB_STAT . " -h $dbpath -e |" ) ) {
181 error( 1, "Failed to open a pipe to $DB_STAT: $!" );
185 # Now, check each DB environment for any open connections (db_stat calls
186 # them as references). If a volume has no references, we can do
187 # recovery on it. Only check this option if the user wants to play
193 if (/References\.$/) {
201 # Print out two different skip messages. This is just for anality.
202 if ( $connections == 1 ) {
203 error( 1, "Skipping $dbpath since it has one active connection" );
207 if ( $connections > 0 ) {
209 "Skipping $dbpath since it has $connections active connections"
215 # Run the db_recover command on the environment.
216 error( 0, "Running db_recover on $dbpath" );
217 if ( system( $DB_RECOVER . " -h $dbpath >/dev/null 2>&1" ) ) {
218 error( 1, "Failed to run db_recover on $dbpath" );
223 error( 0, "Verifying $dbpath/cnid.db" );
224 if ( system( $DB_VERIFY . " -q -h $dbpath cnid.db" ) ) {
225 error( 1, "Verification of $dbpath/cnid.db failed" );
229 error( 0, "Verifying $dbpath/devino.db" );
230 if ( system( $DB_VERIFY . " -q -h $dbpath devino.db" ) ) {
231 error( 1, "Verification of $dbpath/devino.db failed" );
235 error( 0, "Verifying $dbpath/didname.db" );
236 if ( system( $DB_VERIFY . " -q -h $dbpath didname.db" ) ) {
237 error( 1, "Verification of $dbpath/didname.db failed" );
244 # Remove the log files if told to do so.
245 unless ( opendir( DIR, $dbpath ) ) {
246 error( 1, "Failed to open $dbpath for browsing: $!" );
251 while ( defined( $file = readdir(DIR) ) ) {
252 if ( $file =~ /^log\.\d+$/ ) {
253 error( 0, "Removing $dbpath/$file" );
254 unless ( unlink( $dbpath . "/" . $file ) ) {
255 error( 1, "Failed to remove $dbpath/$file: $!" );
271 foreach ( $ENV{TMPDIR}, "/tmp" ) {
272 next unless defined && -d && -w _;
276 $tmpdir = '' unless defined $tmpdir;
281 my ( $code, $msg ) = @_;
289 print $err_types->{$code} . ": " . $msg . "\n";
291 end() if ( $code == 2 );
295 if ($START_NETATALK) {
296 error( 0, "Restarting Netatalk" );
297 if ( system( $START_CMD . " >/dev/null 2>&1" ) ) {
298 print "ERROR: Failed to restart Netatalk\n";
305 print "\nRun of CNID DB Maintenance script ended at "
306 . scalar(localtime) . ".\n";
311 print "$0 version $VERSION\n";
315 print "usage: $0 [-hlsvV]\n";
316 print "\t-h view this message\n";
317 print "\t-l remove transaction logs after running recovery\n";
319 "\t-s be extra safe in verifying there are no open DB connections\n";
320 print "\t-v print version and exit\n";
321 print "\t-V run a verification on all database files after recovery\n";