< prev index next >

src/hotspot/share/logging/logFileOutput.cpp

8176717: GC log file handle leaked to child processes
8176809: UL Log file handles leaked to child processes

227   if (_file_count > 0 && file_exists(_file_name)) {                                                                        
228     if (!is_regular_file(_file_name)) {                                                                                    
229       errstream->print_cr("Unable to log to file %s with log file rotation: "                                              
230                           "%s is not a regular file",                                                                      
231                           _file_name, _file_name);                                                                         
232       return false;                                                                                                        
233     }                                                                                                                      
234     _current_file = next_file_number(_file_name,                                                                           
235                                      _file_count_max_digits,                                                               
236                                      _file_count,                                                                          
237                                      errstream);                                                                           
238     if (_current_file == UINT_MAX) {                                                                                       
239       return false;                                                                                                        
240     }                                                                                                                      
241     log_trace(logging)("Existing log file found, saving it as '%s.%0*u'",                                                  
242                        _file_name, _file_count_max_digits, _current_file);                                                 
243     archive();                                                                                                             
244     increment_file_count();                                                                                                
245   }                                                                                                                        
246 
247   _stream = fopen(_file_name, FileOpenMode);                                                                               
248   if (_stream == NULL) {                                                                                                   
249     errstream->print_cr("Error opening log file '%s': %s",                                                                 
250                         _file_name, strerror(errno));                                                                      
251     return false;                                                                                                          
252   }                                                                                                                        
253 
254   if (_file_count == 0 && is_regular_file(_file_name)) {                                                                   
255     log_trace(logging)("Truncating log file");                                                                             
256     os::ftruncate(os::get_fileno(_stream), 0);                                                                             
257   }                                                                                                                        
258 
259   return true;                                                                                                             
260 }                                                                                                                          
261 
262 int LogFileOutput::write(const LogDecorations& decorations, const char* msg) {                                             
263   if (_stream == NULL) {                                                                                                   
264     // An error has occurred with this output, avoid writing to it.                                                        
265     return 0;                                                                                                              
266   }                                                                                                                        

227   if (_file_count > 0 && file_exists(_file_name)) {
228     if (!is_regular_file(_file_name)) {
229       errstream->print_cr("Unable to log to file %s with log file rotation: "
230                           "%s is not a regular file",
231                           _file_name, _file_name);
232       return false;
233     }
234     _current_file = next_file_number(_file_name,
235                                      _file_count_max_digits,
236                                      _file_count,
237                                      errstream);
238     if (_current_file == UINT_MAX) {
239       return false;
240     }
241     log_trace(logging)("Existing log file found, saving it as '%s.%0*u'",
242                        _file_name, _file_count_max_digits, _current_file);
243     archive();
244     increment_file_count();
245   }
246 
247   _stream = os::fopen_retain(_file_name, FileOpenMode);
248   if (_stream == NULL) {
249     errstream->print_cr("Error opening log file '%s': %s",
250                         _file_name, strerror(errno));
251     return false;
252   }
253 
254   if (_file_count == 0 && is_regular_file(_file_name)) {
255     log_trace(logging)("Truncating log file");
256     os::ftruncate(os::get_fileno(_stream), 0);
257   }
258 
259   return true;
260 }
261 
262 int LogFileOutput::write(const LogDecorations& decorations, const char* msg) {
263   if (_stream == NULL) {
264     // An error has occurred with this output, avoid writing to it.
265     return 0;
266   }

316   if (_file_count == 0) {                                                                                                  
317     // Rotation not possible                                                                                               
318     return;                                                                                                                
319   }                                                                                                                        
320   _rotation_semaphore.wait();                                                                                              
321   rotate();                                                                                                                
322   _rotation_semaphore.signal();                                                                                            
323 }                                                                                                                          
324 
325 void LogFileOutput::rotate() {                                                                                             
326 
327   if (fclose(_stream)) {                                                                                                   
328     jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",                      
329                 _file_name, os::strerror(errno));                                                                          
330   }                                                                                                                        
331 
332   // Archive the current log file                                                                                          
333   archive();                                                                                                               
334 
335   // Open the active log file using the same stream as before                                                              
336   _stream = fopen(_file_name, FileOpenMode);                                                                               
337   if (_stream == NULL) {                                                                                                   
338     jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n",                   
339                 _file_name, os::strerror(errno));                                                                          
340     return;                                                                                                                
341   }                                                                                                                        
342 
343   // Reset accumulated size, increase current file counter, and check for file count wrap-around.                          
344   _current_size = 0;                                                                                                       
345   increment_file_count();                                                                                                  
346 }                                                                                                                          
347 
348 char* LogFileOutput::make_file_name(const char* file_name,                                                                 
349                                     const char* pid_string,                                                                
350                                     const char* timestamp_string) {                                                        
351   char* result = NULL;                                                                                                     
352 
353   // Lets start finding out if we have any %d and/or %t in the name.                                                       
354   // We will only replace the first occurrence of any placeholder                                                          
355   const char* pid = strstr(file_name, PidFilenamePlaceholder);                                                             

316   if (_file_count == 0) {
317     // Rotation not possible
318     return;
319   }
320   _rotation_semaphore.wait();
321   rotate();
322   _rotation_semaphore.signal();
323 }
324 
325 void LogFileOutput::rotate() {
326 
327   if (fclose(_stream)) {
328     jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",
329                 _file_name, os::strerror(errno));
330   }
331 
332   // Archive the current log file
333   archive();
334 
335   // Open the active log file using the same stream as before
336   _stream = os::fopen_retain(_file_name, FileOpenMode);
337   if (_stream == NULL) {
338     jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n",
339                 _file_name, os::strerror(errno));
340     return;
341   }
342 
343   // Reset accumulated size, increase current file counter, and check for file count wrap-around.
344   _current_size = 0;
345   increment_file_count();
346 }
347 
348 char* LogFileOutput::make_file_name(const char* file_name,
349                                     const char* pid_string,
350                                     const char* timestamp_string) {
351   char* result = NULL;
352 
353   // Lets start finding out if we have any %d and/or %t in the name.
354   // We will only replace the first occurrence of any placeholder
355   const char* pid = strstr(file_name, PidFilenamePlaceholder);
< prev index next >