1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 #include "precompiled.hpp"
  25 #include "logging/log.hpp"
  26 #include "logging/logConfiguration.hpp"
  27 #include "logging/logFileOutput.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "runtime/arguments.hpp"
  30 #include "runtime/os.inline.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 #include "utilities/defaultStream.hpp"
  33 
  34 const char* LogFileOutput::FileOpenMode = "a";
  35 const char* LogFileOutput::PidFilenamePlaceholder = "%p";
  36 const char* LogFileOutput::TimestampFilenamePlaceholder = "%t";
  37 const char* LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S";
  38 const char* LogFileOutput::FileSizeOptionKey = "filesize";
  39 const char* LogFileOutput::FileCountOptionKey = "filecount";
  40 char        LogFileOutput::_pid_str[PidBufferSize];
  41 char        LogFileOutput::_vm_start_time_str[StartTimeBufferSize];
  42 
  43 LogFileOutput::LogFileOutput(const char* name)
  44     : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)),
  45       _file_name(NULL), _archive_name(NULL), _archive_name_len(0),
  46       _rotate_size(DefaultFileSize), _file_count(DefaultFileCount),
  47       _current_size(0), _current_file(0), _rotation_semaphore(1) {
  48   assert(strstr(name, "file=") == name, "invalid output name '%s': missing prefix", name);
  49   _file_name = make_file_name(name + strlen("file="), _pid_str, _vm_start_time_str);
  50 }
  51 
  52 void LogFileOutput::set_file_name_parameters(jlong vm_start_time) {
  53   int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id());
  54   assert(res > 0, "PID buffer too small");
  55 
  56   struct tm local_time;
  57   time_t utc_time = vm_start_time / 1000;
  58   os::localtime_pd(&utc_time, &local_time);
  59   res = (int)strftime(_vm_start_time_str, sizeof(_vm_start_time_str), TimestampFormat, &local_time);
  60   assert(res > 0, "VM start time buffer too small.");
  61 }
  62 
  63 LogFileOutput::~LogFileOutput() {
  64   if (_stream != NULL) {
  65     if (fclose(_stream) != 0) {
  66       jio_fprintf(defaultStream::error_stream(), "Could not close log file '%s' (%s).\n",
  67                   _file_name, os::strerror(errno));
  68     }
  69   }
  70   os::free(_archive_name);
  71   os::free(_file_name);
  72   os::free(const_cast<char*>(_name));
  73 }
  74 
  75 static size_t parse_value(const char* value_str) {
  76   char* end;
  77   unsigned long long value = strtoull(value_str, &end, 10);
  78   if (!isdigit(*value_str) || end != value_str + strlen(value_str) || value >= SIZE_MAX) {
  79     return SIZE_MAX;
  80   }
  81   return value;
  82 }
  83 
  84 static bool file_exists(const char* filename) {
  85   struct stat dummy_stat;
  86   return os::stat(filename, &dummy_stat) == 0;
  87 }
  88 
  89 static uint number_of_digits(uint number) {
  90   return number < 10 ? 1 : (number < 100 ? 2 : 3);
  91 }
  92 
  93 static bool is_regular_file(const char* filename) {
  94   struct stat st;
  95   int ret = os::stat(filename, &st);
  96   if (ret != 0) {
  97     return false;
  98   }
  99 #ifdef _WINDOWS
 100   return (st.st_mode & S_IFMT) == _S_IFREG;
 101 #else
 102   return S_ISREG(st.st_mode);
 103 #endif
 104 }
 105 
 106 // Try to find the next number that should be used for file rotation.
 107 // Return UINT_MAX on error.
 108 static uint next_file_number(const char* filename,
 109                              uint number_of_digits,
 110                              uint filecount,
 111                              outputStream* errstream) {
 112   bool found = false;
 113   uint next_num = 0;
 114 
 115   // len is filename + dot + digits + null char
 116   size_t len = strlen(filename) + number_of_digits + 2;
 117   char* archive_name = NEW_C_HEAP_ARRAY(char, len, mtLogging);
 118   char* oldest_name = NEW_C_HEAP_ARRAY(char, len, mtLogging);
 119 
 120   for (uint i = 0; i < filecount; i++) {
 121     int ret = jio_snprintf(archive_name, len, "%s.%0*u",
 122                            filename, number_of_digits, i);
 123     assert(ret > 0 && static_cast<size_t>(ret) == len - 1,
 124            "incorrect buffer length calculation");
 125 
 126     if (file_exists(archive_name) && !is_regular_file(archive_name)) {
 127       // We've encountered something that's not a regular file among the
 128       // possible file rotation targets. Fail immediately to prevent
 129       // problems later.
 130       errstream->print_cr("Possible rotation target file '%s' already exists "
 131                           "but is not a regular file.", archive_name);
 132       next_num = UINT_MAX;
 133       break;
 134     }
 135 
 136     // Stop looking if we find an unused file name
 137     if (!file_exists(archive_name)) {
 138       next_num = i;
 139       found = true;
 140       break;
 141     }
 142 
 143     // Keep track of oldest existing log file
 144     if (!found
 145         || os::compare_file_modified_times(oldest_name, archive_name) > 0) {
 146       strcpy(oldest_name, archive_name);
 147       next_num = i;
 148       found = true;
 149     }
 150   }
 151 
 152   FREE_C_HEAP_ARRAY(char, oldest_name);
 153   FREE_C_HEAP_ARRAY(char, archive_name);
 154   return next_num;
 155 }
 156 
 157 bool LogFileOutput::parse_options(const char* options, outputStream* errstream) {
 158   if (options == NULL || strlen(options) == 0) {
 159     return true;
 160   }
 161   bool success = true;
 162   char* opts = os::strdup_check_oom(options, mtLogging);
 163 
 164   char* comma_pos;
 165   char* pos = opts;
 166   do {
 167     comma_pos = strchr(pos, ',');
 168     if (comma_pos != NULL) {
 169       *comma_pos = '\0';
 170     }
 171 
 172     char* equals_pos = strchr(pos, '=');
 173     if (equals_pos == NULL) {
 174       success = false;
 175       break;
 176     }
 177     char* key = pos;
 178     char* value_str = equals_pos + 1;
 179     *equals_pos = '\0';
 180 
 181     if (strcmp(FileCountOptionKey, key) == 0) {
 182       size_t value = parse_value(value_str);
 183       if (value > MaxRotationFileCount) {
 184         errstream->print_cr("Invalid option: %s must be in range [0, %u]",
 185                             FileCountOptionKey,
 186                             MaxRotationFileCount);
 187         success = false;
 188         break;
 189       }
 190       _file_count = static_cast<uint>(value);
 191     } else if (strcmp(FileSizeOptionKey, key) == 0) {
 192       julong value;
 193       success = Arguments::atojulong(value_str, &value);
 194       if (!success || (value > SIZE_MAX)) {
 195         errstream->print_cr("Invalid option: %s must be in range [0, "
 196                             SIZE_FORMAT "]", FileSizeOptionKey, SIZE_MAX);
 197         success = false;
 198         break;
 199       }
 200       _rotate_size = static_cast<size_t>(value);
 201     } else {
 202       errstream->print_cr("Invalid option '%s' for log file output.", key);
 203       success = false;
 204       break;
 205     }
 206     pos = comma_pos + 1;
 207   } while (comma_pos != NULL);
 208 
 209   os::free(opts);
 210   return success;
 211 }
 212 
 213 bool LogFileOutput::initialize(const char* options, outputStream* errstream) {
 214   if (!parse_options(options, errstream)) {
 215     return false;
 216   }
 217 
 218   if (_file_count > 0) {
 219     // compute digits with filecount - 1 since numbers will start from 0
 220     _file_count_max_digits = number_of_digits(_file_count - 1);
 221     _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
 222     _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
 223   }
 224 
 225   log_trace(logging)("Initializing logging to file '%s' (filecount: %u"
 226                      ", filesize: " SIZE_FORMAT " KiB).",
 227                      _file_name, _file_count, _rotate_size / K);
 228 
 229   if (_file_count > 0 && file_exists(_file_name)) {
 230     if (!is_regular_file(_file_name)) {
 231       errstream->print_cr("Unable to log to file %s with log file rotation: "
 232                           "%s is not a regular file",
 233                           _file_name, _file_name);
 234       return false;
 235     }
 236     _current_file = next_file_number(_file_name,
 237                                      _file_count_max_digits,
 238                                      _file_count,
 239                                      errstream);
 240     if (_current_file == UINT_MAX) {
 241       return false;
 242     }
 243     log_trace(logging)("Existing log file found, saving it as '%s.%0*u'",
 244                        _file_name, _file_count_max_digits, _current_file);
 245     archive();
 246     increment_file_count();
 247   }
 248 
 249   _stream = fopen(_file_name, FileOpenMode);
 250   if (_stream == NULL) {
 251     errstream->print_cr("Error opening log file '%s': %s",
 252                         _file_name, strerror(errno));
 253     return false;
 254   }
 255 
 256   if (_file_count == 0 && is_regular_file(_file_name)) {
 257     log_trace(logging)("Truncating log file");
 258     os::ftruncate(os::get_fileno(_stream), 0);
 259   }
 260 
 261   return true;
 262 }
 263 
 264 int LogFileOutput::write(const LogDecorations& decorations, const char* msg) {
 265   if (_stream == NULL) {
 266     // An error has occurred with this output, avoid writing to it.
 267     return 0;
 268   }
 269 
 270   _rotation_semaphore.wait();
 271   int written = LogFileStreamOutput::write(decorations, msg);
 272   _current_size += written;
 273 
 274   if (should_rotate()) {
 275     rotate();
 276   }
 277   _rotation_semaphore.signal();
 278 
 279   return written;
 280 }
 281 
 282 int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
 283   if (_stream == NULL) {
 284     // An error has occurred with this output, avoid writing to it.
 285     return 0;
 286   }
 287 
 288   _rotation_semaphore.wait();
 289   int written = LogFileStreamOutput::write(msg_iterator);
 290   _current_size += written;
 291 
 292   if (should_rotate()) {
 293     rotate();
 294   }
 295   _rotation_semaphore.signal();
 296 
 297   return written;
 298 }
 299 
 300 void LogFileOutput::archive() {
 301   assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function.");
 302   int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u",
 303                          _file_name, _file_count_max_digits, _current_file);
 304   assert(ret >= 0, "Buffer should always be large enough");
 305 
 306   // Attempt to remove possibly existing archived log file before we rename.
 307   // Don't care if it fails, we really only care about the rename that follows.
 308   remove(_archive_name);
 309 
 310   // Rename the file from ex hotspot.log to hotspot.log.2
 311   if (rename(_file_name, _archive_name) == -1) {
 312     jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n",
 313                 _file_name, _archive_name, os::strerror(errno));
 314   }
 315 }
 316 
 317 void LogFileOutput::force_rotate() {
 318   if (_file_count == 0) {
 319     // Rotation not possible
 320     return;
 321   }
 322   _rotation_semaphore.wait();
 323   rotate();
 324   _rotation_semaphore.signal();
 325 }
 326 
 327 void LogFileOutput::rotate() {
 328 
 329   if (fclose(_stream)) {
 330     jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",
 331                 _file_name, os::strerror(errno));
 332   }
 333 
 334   // Archive the current log file
 335   archive();
 336 
 337   // Open the active log file using the same stream as before
 338   _stream = fopen(_file_name, FileOpenMode);
 339   if (_stream == NULL) {
 340     jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n",
 341                 _file_name, os::strerror(errno));
 342     return;
 343   }
 344 
 345   // Reset accumulated size, increase current file counter, and check for file count wrap-around.
 346   _current_size = 0;
 347   increment_file_count();
 348 }
 349 
 350 char* LogFileOutput::make_file_name(const char* file_name,
 351                                     const char* pid_string,
 352                                     const char* timestamp_string) {
 353   char* result = NULL;
 354 
 355   // Lets start finding out if we have any %d and/or %t in the name.
 356   // We will only replace the first occurrence of any placeholder
 357   const char* pid = strstr(file_name, PidFilenamePlaceholder);
 358   const char* timestamp = strstr(file_name, TimestampFilenamePlaceholder);
 359 
 360   if (pid == NULL && timestamp == NULL) {
 361     // We found no place-holders, return the simple filename
 362     return os::strdup_check_oom(file_name, mtLogging);
 363   }
 364 
 365   // At least one of the place-holders were found in the file_name
 366   const char* first = "";
 367   size_t first_pos = SIZE_MAX;
 368   size_t first_replace_len = 0;
 369 
 370   const char* second = "";
 371   size_t second_pos = SIZE_MAX;
 372   size_t second_replace_len = 0;
 373 
 374   // If we found a %p, then setup our variables accordingly
 375   if (pid != NULL) {
 376     if (timestamp == NULL || pid < timestamp) {
 377       first = pid_string;
 378       first_pos = pid - file_name;
 379       first_replace_len = strlen(PidFilenamePlaceholder);
 380     } else {
 381       second = pid_string;
 382       second_pos = pid - file_name;
 383       second_replace_len = strlen(PidFilenamePlaceholder);
 384     }
 385   }
 386 
 387   if (timestamp != NULL) {
 388     if (pid == NULL || timestamp < pid) {
 389       first = timestamp_string;
 390       first_pos = timestamp - file_name;
 391       first_replace_len = strlen(TimestampFilenamePlaceholder);
 392     } else {
 393       second = timestamp_string;
 394       second_pos = timestamp - file_name;
 395       second_replace_len = strlen(TimestampFilenamePlaceholder);
 396     }
 397   }
 398 
 399   size_t first_len = strlen(first);
 400   size_t second_len = strlen(second);
 401 
 402   // Allocate the new buffer, size it to hold all we want to put in there +1.
 403   size_t result_len =  strlen(file_name) + first_len - first_replace_len + second_len - second_replace_len;
 404   result = NEW_C_HEAP_ARRAY(char, result_len + 1, mtLogging);
 405 
 406   // Assemble the strings
 407   size_t file_name_pos = 0;
 408   size_t i = 0;
 409   while (i < result_len) {
 410     if (file_name_pos == first_pos) {
 411       // We are in the range of the first placeholder
 412       strcpy(result + i, first);
 413       // Bump output buffer position with length of replacing string
 414       i += first_len;
 415       // Bump source buffer position to skip placeholder
 416       file_name_pos += first_replace_len;
 417     } else if (file_name_pos == second_pos) {
 418       // We are in the range of the second placeholder
 419       strcpy(result + i, second);
 420       i += second_len;
 421       file_name_pos += second_replace_len;
 422     } else {
 423       // Else, copy char by char of the original file
 424       result[i] = file_name[file_name_pos++];
 425       i++;
 426     }
 427   }
 428   // Add terminating char
 429   result[result_len] = '\0';
 430   return result;
 431 }
 432 
 433 void LogFileOutput::describe(outputStream *out) {
 434   LogOutput::describe(out);
 435   out->print(" ");
 436 
 437   out->print("filecount=%u,filesize=" SIZE_FORMAT "%s", _file_count,
 438              byte_size_in_proper_unit(_rotate_size),
 439              proper_unit_for_byte_size(_rotate_size));
 440 }
 441