[LON-CAPA-cvs] cvs: loncom / lond

raeburn raeburn at source.lon-capa.org
Thu Nov 20 10:52:26 EST 2025


raeburn		Thu Nov 20 15:52:26 2025 EDT

  Modified files:              
    /loncom	lond 
  Log:
  - Check result of IO::File->new() before using it, and close after use.
  
  
Index: loncom/lond
diff -u loncom/lond:1.585 loncom/lond:1.586
--- loncom/lond:1.585	Thu Nov 20 15:46:17 2025
+++ loncom/lond	Thu Nov 20 15:52:26 2025
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.585 2025/11/20 15:46:17 raeburn Exp $
+# $Id: lond,v 1.586 2025/11/20 15:52:26 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -65,7 +65,7 @@
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.585 $'; #' stupid emacs
+my $VERSION='$Revision: 1.586 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -1391,11 +1391,13 @@
    # the allowed load limit as set by the perl global variable lonLoadLim
 
     my $loadavg;
-    my $loadfile=IO::File->new('/proc/loadavg');
-   
-    $loadavg=<$loadfile>;
-    $loadavg =~ s/\s.*//g;                      # Extract the first field only.
-   
+    my $loadfile;
+    if ($loadfile=IO::File->new('/proc/loadavg')) {
+        $loadavg=<$loadfile>;
+        $loadavg =~ s/\s.*//g;                      # Extract the first field only.
+        $loadfile->close; 
+    }
+
     my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
 
     &Reply( $replyfd, \$loadpercent, "$cmd:$tail");
@@ -3356,6 +3358,7 @@
     my $hfh;
     if ($hfh=IO::File->new(">>$proname/activity.log")) { 
 	print $hfh "$now:$clientname:$what\n";
+        $hfh->close;
 	&Reply( $client, "ok\n", $userinput); 
     } else {
 	&Failure($client, "error: ".($!+0)." IO::File->new Failed "
@@ -4414,9 +4417,11 @@
 	$reply=~s/\&/\n/g;
 	print $store $reply;
 	close $store;
-	my $store2=IO::File->new(">$execdir/tmp/$id.end");
-	print $store2 "done\n";
-	close $store2;
+	my $store2;
+	if ($store2=IO::File->new(">$execdir/tmp/$id.end")) {
+	    print $store2 "done\n";
+	    close $store2;
+	}
 	&Reply($client, "ok\n", $userinput);
     } else {
 	&Failure($client, "error: ".($!+0)
@@ -7653,10 +7658,13 @@
 my $pidfile="$perlvar{'lonDaemons'}/logs/lond.pid";
 
 if (-e $pidfile) {
-   my $lfh=IO::File->new("$pidfile");
-   my $pide=<$lfh>;
-   chomp($pide);
-   if (kill 0 => $pide) { die "already running"; }
+   my $lfh;
+   if (IO::File->new("$pidfile")) {
+       my $pide=<$lfh>;
+       chomp($pide);
+       $lfh->close;
+       if (kill 0 => $pide) { die "already running"; }
+   }
 }
 
 # ------------------------------------------------------------- Read hosts file
@@ -7872,11 +7880,14 @@
 sub logthis {
     my $message=shift;
     my $execdir=$perlvar{'lonDaemons'};
-    my $fh=IO::File->new(">>$execdir/logs/lond.log");
-    my $now=time;
-    my $local=localtime($now);
-    $lastlog=$local.': '.$message;
-    print $fh "$local ($$): $message\n";
+    my $logfh;
+    if ($logfh=IO::File->new(">>$execdir/logs/lond.log")) {
+        my $now=time;
+        my $local=localtime($now);
+        $lastlog=$local.': '.$message;
+        print $logfh "$local ($$): $message\n";
+        $logfh->close;
+    }
 }
 
 # ------------------------- Conditional log if $DEBUG true.
@@ -7941,33 +7952,40 @@
     &status("Doing logging");
     my $docdir=$perlvar{'lonDocRoot'};
     {
-	my $fh=IO::File->new(">$docdir/lon-status/londchld/$$.txt");
-        print $fh $status."\n".$lastlog."\n".time."\n$keymode";
-        $fh->close();
+	my $fh;
+	if ($fh=IO::File->new(">$docdir/lon-status/londchld/$$.txt")) {
+	    print $fh $status."\n".$lastlog."\n".time."\n$keymode";
+	    $fh->close();
+	}
     }
     &status("Finished $$.txt");
     {
-	open(LOG,">>$docdir/lon-status/londstatus.txt");
-	flock(LOG,LOCK_EX);
-	print LOG $$."\t".$clientname."\t".$currenthostid."\t"
-	    .$status."\t".$lastlog."\t $keymode\n";
-	flock(LOG,LOCK_UN);
-	close(LOG);
+	if (open(LOG,">>$docdir/lon-status/londstatus.txt")) {
+	    flock(LOG,LOCK_EX);
+	    print LOG $$."\t".$clientname."\t".$currenthostid."\t"
+	        .$status."\t".$lastlog."\t $keymode\n";
+	    flock(LOG,LOCK_UN);
+	    close(LOG);
+        }
     }
     &status("Finished logging");
 }
 
 sub initnewstatus {
     my $docdir=$perlvar{'lonDocRoot'};
-    my $fh=IO::File->new(">$docdir/lon-status/londstatus.txt");
-    my $now=time();
-    my $local=localtime($now);
-    print $fh "LOND status $local - parent $$\n\n";
-    opendir(DIR,"$docdir/lon-status/londchld");
-    while (my $filename=readdir(DIR)) {
-        unlink("$docdir/lon-status/londchld/$filename");
+    my $fh;
+    if ($fh=IO::File->new(">$docdir/lon-status/londstatus.txt")) {
+        my $now=time();
+        my $local=localtime($now);
+        print $fh "LOND status $local - parent $$\n\n";
+        if (opendir(DIR,"$docdir/lon-status/londchld")) {
+            while (my $filename=readdir(DIR)) {
+               unlink("$docdir/lon-status/londchld/$filename");
+            }
+            closedir(DIR);
+        }
+        $fh->close;
     }
-    closedir(DIR);
 }
 
 # -------------------------------------------------------------- Status setting
@@ -8561,6 +8579,7 @@
 	my $pf = IO::File->new(">$file");
 	if($pf) {
 	    print $pf "$contents\n";
+            $pf->close;
 	    return 1;
 	} else {
 	    return 0;
@@ -8583,13 +8602,19 @@
     my $proname    = &propath($domain, $user); 
     my $passwdfile = "$proname/passwd";
     if( -e $passwdfile ) {
-	my $pf = IO::File->new($passwdfile);
-	my $realpassword = <$pf>;
-	chomp($realpassword);
-	Debug("Password info = $realpassword\n");
-	my ($authtype, $contentpwd) = split(/:/, $realpassword);
-	Debug("Authtype = $authtype, content = $contentpwd\n");
-	return "$authtype:$contentpwd";     
+	my $pf;
+	if ($pf = IO::File->new($passwdfile)) {
+	    my $realpassword = <$pf>;
+	    chomp($realpassword);
+	    Debug("Password info = $realpassword\n");
+	    my ($authtype, $contentpwd) = split(/:/, $realpassword);
+	    Debug("Authtype = $authtype, content = $contentpwd\n");
+	    $pf->close; 
+	    return "$authtype:$contentpwd";
+	} else {
+	    Debug("Opening passwdfile failed");
+	    return;
+	}
     } else {
 	Debug("Returning nouser");
 	return "nouser";
@@ -8860,10 +8885,11 @@
 	}
 	$sh->close();
     }
-    $sh=IO::File->new(">$fname.subscription");
-    if ($contents) { print $sh $contents; }
-    if ($newline) { print $sh $newline; }
-    $sh->close();
+    if ($sh=IO::File->new(">$fname.subscription")) {
+        if ($contents) { print $sh $contents; }
+        if ($newline) { print $sh $newline; }
+        $sh->close();
+    }
     return $found;
 }
 
@@ -9113,6 +9139,7 @@
 	    my $pf = IO::File->new(">$passfilename");
 	    if ($pf) {
 		print $pf "$umode:$npass\n";
+                $pf->close;
                 &update_passwd_history($uname,$udom,$umode,$action);
 	    } else {
 		$result = "pass_file_failed_error";
@@ -9125,6 +9152,7 @@
 	    my $pf = IO::File->new(">$passfilename");
 	    if($pf) {
 		print $pf "internal:$ncpass\n";
+                $pf->close;
                 &update_passwd_history($uname,$udom,$umode,$action); 
 	    } else {
 		$result = "pass_file_failed_error";
@@ -9135,6 +9163,7 @@
 	    my $pf = IO::File->new(">$passfilename");
 	    if($pf) {
 		print $pf "localauth:$npass\n";
+                $pf->close;
                 &update_passwd_history($uname,$udom,$umode,$action);
 	    } else {
 		$result = "pass_file_failed_error";
@@ -9148,6 +9177,7 @@
 	    my $pf = IO::File->new("> $passfilename");
 	    if($pf) {
 		print $pf "none:\n";
+                $pf->close;
 	    } else {
 		$result = "pass_file_failed_error";
 	    }
@@ -9156,6 +9186,7 @@
         my $pf = IO::File->new(">$passfilename");
         if($pf) {
             print $pf "lti:\n";
+            $pf->close; 
             &update_passwd_history($uname,$udom,$umode,$action);
         } else {
             $result = "pass_file_failed_error";




More information about the LON-CAPA-cvs mailing list