21 $STOP_CMD = '/usr/local/etc/rc.d/netatalk.sh stop';
22 $START_CMD = '/usr/local/etc/rc.d/netatalk.sh start';
24 # This ps command needs to output the following fields in the following order:
25 # USER,PID,PPID,COMMAND
26 # Below is the example of a BSD ps. A SYSV example is:
27 # /bin/ps -eflouid,pid,ppid,comm
28 $PS_CMD = '@PS@ -axouser,pid,ppid,command';
30 $DB_STAT = '@DB3_PATH@bin/db_stat';
31 $DB_RECOVER = '@DB3_PATH@bin/db_recover';
32 $DB_VERIFY = '@DB3_PATH@bin/db_verify';
33 $APPLE_VOLUMES_FILE = '@PKGCONFDIR@/AppleVolumes.default';
38 $LOCK_FILE = '/tmp/cnid_maint.LOCK';
51 getopts( 'hsvVl', $opts );
71 print "Beginning run of CNID DB Maintanence script at "
72 . scalar(localtime) . ".\n\n";
74 if ( -f $LOCK_FILE ) {
75 error( 1, "Lock file $LOCK_FILE exists." );
79 unless ( open( LOCK, ">" . $LOCK_FILE ) ) {
80 error( 2, "Unable to create $LOCK_FILE: $!" );
82 flock( LOCK, LOCK_EX );
85 # Check to see if the AppleVolumes.default file exists. We will use this file
86 # to get a list of database environments to recover. We will ignore users'
87 # home directories since that could be a monumental under taking.
88 if ( !-f $APPLE_VOLUMES_FILE ) {
89 error( 2, "Unable to locate $APPLE_VOLUMES_FILE" );
92 # Use ps to get a list of running afpds. We will track all afpd PIDs that are
94 unless ( open( PS, $PS_CMD . " | $GREP afpd | $GREP -v grep |" ) ) {
95 error( 2, "Unable to open a pipe to ps: $!" );
103 my ( $user, $pid, $ppid, $command ) = split (/\s+/);
104 if ( ( $user eq "root" && $ppid != 1 ) || ( $user ne "root" ) ) {
111 if ( $children > 1 ) {
113 # We have some children. We cannot run recovery.
115 "Clients are still connected. Database recovery will not be run at this time."
122 # Shutdown the running afpds.
124 error( 0, "Shutting down afpd process..." );
125 error( 2, "Failed to shutdown afpd" )
126 if system( $STOP_CMD . ">/dev/null 2>&1" );
129 # Now, we parse AppleVolumes.default to get a list of volumes to run recovery
131 unless ( open( VOLS, $APPLE_VOLUMES_FILE ) ) {
132 error( 2, "Unable to open $APPLE_VOLUMES_FILE: $!" );
141 my ( $path, @options ) = split ( /\s+/, $_ );
142 next if ( $path =~ /^~/ );
144 foreach $option (@options) {
146 # We need to check for the dbpath option on each volume. If that
147 # option is present, we should use its path instead of the actual
149 if ( $option =~ /^dbpath:/ ) {
161 foreach $path (@paths) {
162 my $dbpath = $path . "/.AppleDB";
165 "Checking database environment $dbpath for open connections..." );
166 unless ( open( STAT, $DB_STAT . " -h $dbpath -e |" ) ) {
167 error( 1, "Failed to open a pipe to $DB_STAT: $!" );
171 # Now, check each DB environment for any open connections (db_stat calls
172 # them as references). If a volume has no references, we can do
173 # recovery on it. Only check this option if the user wants to play
179 if (/References\.$/) {
187 # Print out two different skip messages. This is just for anality.
188 if ( $connections == 1 ) {
189 error( 1, "Skipping $dbpath since it has one active connection" );
193 if ( $connections > 0 ) {
195 "Skipping $dbpath since it has $connections active connections"
201 # Run the db_recover command on the environment.
202 error( 0, "Running db_recover on $dbpath" );
203 if ( system( $DB_RECOVER . " -h $dbpath >/dev/null 2>&1" ) ) {
204 error( 1, "Failed to run db_recover on $dbpath" );
209 error( 0, "Verifying $dbpath/cnid.db" );
210 if ( system( $DB_VERIFY . " -q -h $dbpath cnid.db" ) ) {
211 error( 1, "Verification of $dbpath/cnid.db failed" );
215 error( 0, "Verifying $dbpath/devino.db" );
216 if ( system( $DB_VERIFY . " -q -h $dbpath devino.db" ) ) {
217 error( 1, "Verification of $dbpath/devino.db failed" );
221 error( 0, "Verifying $dbpath/didname.db" );
222 if ( system( $DB_VERIFY . " -q -h $dbpath didname.db" ) ) {
223 error( 1, "Verification of $dbpath/didname.db failed" );
230 # Remove the log files if told to do so.
231 unless ( opendir( DIR, $dbpath ) ) {
232 error( 1, "Failed to open $dbpath for browsing: $!" );
237 while ( defined( $file = readdir(DIR) ) ) {
238 if ( $file =~ /^log\.\d+$/ ) {
239 error( 0, "Removing $dbpath/$file" );
240 unless ( unlink( $dbpath . "/" . $file ) ) {
241 error( 1, "Failed to remove $dbpath/$file: $!" );
255 my ( $code, $msg ) = @_;
263 print $err_types->{$code} . ": " . $msg . "\n";
265 end() if ( $code == 2 );
269 if ($START_NETATALK) {
270 error( 0, "Restarting Netatalk" );
271 if ( system( $START_CMD . " >/dev/null 2>&1" ) ) {
272 print "ERROR: Failed to restart Netatalk\n";
279 print "\nRun of CNID DB Maintenance script ended at "
280 . scalar(localtime) . ".\n";
285 print "cnid_maint.pl version $VERSION\n";
289 print "usage: cnid_maint.pl [-hlsvV]\n";
290 print "\t-h view this message\n";
291 print "\t-l remove transaction logs after running recovery\n";
293 "\t-s be extra safe in verifying there are no open DB connections\n";
294 print "\t-v print version and exit\n";
295 print "\t-V run a verification on all database files after recovery\n";