1 /*
   2  * Copyright (c) 2015, 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/os.inline.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 #include "utilities/defaultStream.hpp"
  32 
  33 const char* LogFileOutput::FileOpenMode = "a";
  34 const char* LogFileOutput::PidFilenamePlaceholder = "%p";
  35 const char* LogFileOutput::TimestampFilenamePlaceholder = "%t";
  36 const char* LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S";
  37 const char* LogFileOutput::FileSizeOptionKey = "filesize";
  38 const char* LogFileOutput::FileCountOptionKey = "filecount";
  39 char        LogFileOutput::_pid_str[PidBufferSize];
  40 char        LogFileOutput::_vm_start_time_str[StartTimeBufferSize];
  41 
  42 LogFileOutput::LogFileOutput(const char* name)
  43     : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)),
  44       _file_name(NULL), _archive_name(NULL), _archive_name_len(0),
  45       _rotate_size(DefaultRotationFileSize), _file_count(DefaultRotationFileCount),
  46       _current_size(0), _current_file(0), _rotation_semaphore(1) {
  47   _file_name = make_file_name(name, _pid_str, _vm_start_time_str);
  48 }
  49 
  50 void LogFileOutput::set_file_name_parameters(jlong vm_start_time) {
  51   int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id());
  52   assert(res > 0, "PID buffer too small");
  53 
  54   struct tm local_time;
  55   time_t utc_time = vm_start_time / 1000;
  56   os::localtime_pd(&utc_time, &local_time);
  57   res = (int)strftime(_vm_start_time_str, sizeof(_vm_start_time_str), TimestampFormat, &local_time);
  58   assert(res > 0, "VM start time buffer too small.");
  59 }
  60 
  61 LogFileOutput::~LogFileOutput() {
  62   if (_stream != NULL) {
  63     if (fclose(_stream) != 0) {
  64       jio_fprintf(defaultStream::error_stream(), "Could not close log file '%s' (%s).\n",
  65                   _file_name, strerror(errno));
  66     }
  67   }
  68   os::free(_archive_name);
  69   os::free(_file_name);
  70   os::free(const_cast<char*>(_name));
  71 }
  72 
  73 static size_t parse_value(const char* value_str) {
  74   char* end;
  75   unsigned long long value = strtoull(value_str, &end, 10);
  76   if (!isdigit(*value_str) || end != value_str + strlen(value_str) || value >= SIZE_MAX) {
  77     return SIZE_MAX;
  78   }
  79   return value;
  80 }
  81 
  82 static bool file_exists(const char* filename) {
  83   struct stat dummy_stat;
  84   return os::stat(filename, &dummy_stat) == 0;
  85 }
  86 
  87 static uint number_of_digits(uint number) {
  88   double d = static_cast<double>(number);
  89   uint res = 1 + log10(d);
  90   return res;
  91 }
  92 
  93 static uint next_file_number(const char* filename,
  94                              uint number_of_digits,
  95                              uint filecount) {
  96   bool found = false;
  97   uint next_num = 0;
  98 
  99   // len is filename + dot + digits + null char
 100   size_t len = strlen(filename) + number_of_digits + 2;
 101   char* archive_name = NEW_C_HEAP_ARRAY(char, len, mtLogging);
 102   char* oldest_name = NEW_C_HEAP_ARRAY(char, len, mtLogging);
 103 
 104   for (uint i = 0; i < filecount; i++) {
 105     int ret = jio_snprintf(archive_name, len, "%s.%0*u",
 106                            filename, number_of_digits, i);
 107     assert(ret > 0 && static_cast<size_t>(ret) == len - 1,
 108            "incorrect buffer length calculation");
 109 
 110     // Stop looking if we find an unused file name
 111     if (!file_exists(archive_name)) {
 112       next_num = i;
 113       found = true;
 114       break;
 115     }
 116 
 117     // Keep track of oldest existing log file
 118     if (!found
 119         || os::compare_file_modified_times(oldest_name, archive_name) > 0) {
 120       strcpy(oldest_name, archive_name);
 121       next_num = i;
 122       found = true;
 123     }
 124   }
 125 
 126   FREE_C_HEAP_ARRAY(char, oldest_name);
 127   FREE_C_HEAP_ARRAY(char, archive_name);
 128   assert(found, "should always find a next number");
 129   return next_num;
 130 }
 131 
 132 bool LogFileOutput::configure(const char* options) {
 133   if (options == NULL || strlen(options) == 0) {
 134     return true;
 135   }
 136   bool success = true;
 137   char* opts = os::strdup_check_oom(options, mtLogging);
 138 
 139   char* comma_pos;
 140   char* pos = opts;
 141   do {
 142     comma_pos = strchr(pos, ',');
 143     if (comma_pos != NULL) {
 144       *comma_pos = '\0';
 145     }
 146 
 147     char* equals_pos = strchr(pos, '=');
 148     if (equals_pos == NULL) {
 149       success = false;
 150       break;
 151     }
 152     char* key = pos;
 153     char* value_str = equals_pos + 1;
 154     *equals_pos = '\0';
 155 
 156     if (strcmp(FileCountOptionKey, key) == 0) {
 157       size_t value = parse_value(value_str);
 158       if (value == SIZE_MAX || value >= UINT_MAX) {
 159         success = false;
 160         break;
 161       }
 162       _file_count = static_cast<uint>(value);
 163     } else if (strcmp(FileSizeOptionKey, key) == 0) {
 164       size_t value = parse_value(value_str);
 165       if (value == SIZE_MAX || value > SIZE_MAX / K) {
 166         success = false;
 167         break;
 168       }
 169       _rotate_size = value * K;
 170     } else {
 171       success = false;
 172       break;
 173     }
 174     pos = comma_pos + 1;
 175   } while (comma_pos != NULL);
 176 
 177   os::free(opts);
 178   return success;
 179 }
 180 
 181 bool LogFileOutput::initialize(const char* options) {
 182   if (!configure(options)) {
 183     return false;
 184   }
 185 
 186   if (_file_count > 0) {
 187     _file_count_max_digits = number_of_digits(_file_count - 1); // -1 for 0-index
 188     _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
 189     _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
 190   }
 191 
 192   log_trace(logging)("Initializing logging to file '%s' (filecount: %u"
 193                      ", filesize: " SIZE_FORMAT " KiB).",
 194                      _file_name, _file_count, _rotate_size / K);
 195 
 196   if (_file_count > 0 && file_exists(_file_name)) {
 197     _current_file = next_file_number(_file_name,
 198                                      _file_count_max_digits,
 199                                      _file_count);
 200     log_trace(logging)("Existing log file found, saving it as '%s.%0*u'.",
 201                        _file_name, _file_count_max_digits, _current_file);
 202     archive();
 203   }
 204 
 205   _stream = fopen(_file_name, FileOpenMode);
 206   if (_stream == NULL) {
 207     log_error(logging)("Could not open log file '%s' (%s).", _file_name, strerror(errno));
 208     return false;
 209   }
 210 
 211   if (_file_count == 0) {
 212     log_trace(logging)("Truncating log file");
 213     os::ftruncate(os::fileno(_stream), 0);
 214   }
 215 
 216   return true;
 217 }
 218 
 219 int LogFileOutput::write(const LogDecorations& decorations, const char* msg) {
 220   if (_stream == NULL) {
 221     // An error has occurred with this output, avoid writing to it.
 222     return 0;
 223   }
 224 
 225   _rotation_semaphore.wait();
 226   int written = LogFileStreamOutput::write(decorations, msg);
 227   _current_size += written;
 228 
 229   if (should_rotate()) {
 230     rotate();
 231   }
 232   _rotation_semaphore.signal();
 233 
 234   return written;
 235 }
 236 
 237 void LogFileOutput::archive() {
 238   assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function.");
 239   int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u",
 240                          _file_name, _file_count_max_digits, _current_file);
 241   assert(ret >= 0, "Buffer should always be large enough");
 242 
 243   // Attempt to remove possibly existing archived log file before we rename.
 244   // Don't care if it fails, we really only care about the rename that follows.
 245   remove(_archive_name);
 246 
 247   // Rename the file from ex hotspot.log to hotspot.log.2
 248   if (rename(_file_name, _archive_name) == -1) {
 249     jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n",
 250                 _file_name, _archive_name, strerror(errno));
 251   }
 252 }
 253 
 254 void LogFileOutput::force_rotate() {
 255   if (_file_count == 0) {
 256     // Rotation not possible
 257     return;
 258   }
 259   _rotation_semaphore.wait();
 260   rotate();
 261   _rotation_semaphore.signal();
 262 }
 263 
 264 void LogFileOutput::rotate() {
 265 
 266   if (fclose(_stream)) {
 267     jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",
 268                 _file_name, strerror(errno));
 269   }
 270 
 271   // Archive the current log file
 272   archive();
 273 
 274   // Open the active log file using the same stream as before
 275   _stream = fopen(_file_name, FileOpenMode);
 276   if (_stream == NULL) {
 277     jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n",
 278                 _file_name, strerror(errno));
 279     return;
 280   }
 281 
 282   // Reset accumulated size, increase current file counter, and check for file count wrap-around.
 283   _current_size = 0;
 284   _current_file++;
 285   if (_current_file == _file_count) {
 286     _current_file = 0;
 287   }
 288 }
 289 
 290 char* LogFileOutput::make_file_name(const char* file_name,
 291                                     const char* pid_string,
 292                                     const char* timestamp_string) {
 293   char* result = NULL;
 294 
 295   // Lets start finding out if we have any %d and/or %t in the name.
 296   // We will only replace the first occurrence of any placeholder
 297   const char* pid = strstr(file_name, PidFilenamePlaceholder);
 298   const char* timestamp = strstr(file_name, TimestampFilenamePlaceholder);
 299 
 300   if (pid == NULL && timestamp == NULL) {
 301     // We found no place-holders, return the simple filename
 302     return os::strdup_check_oom(file_name, mtLogging);
 303   }
 304 
 305   // At least one of the place-holders were found in the file_name
 306   const char* first = "";
 307   size_t first_pos = SIZE_MAX;
 308   size_t first_replace_len = 0;
 309 
 310   const char* second = "";
 311   size_t second_pos = SIZE_MAX;
 312   size_t second_replace_len = 0;
 313 
 314   // If we found a %p, then setup our variables accordingly
 315   if (pid != NULL) {
 316     if (timestamp == NULL || pid < timestamp) {
 317       first = pid_string;
 318       first_pos = pid - file_name;
 319       first_replace_len = strlen(PidFilenamePlaceholder);
 320     } else {
 321       second = pid_string;
 322       second_pos = pid - file_name;
 323       second_replace_len = strlen(PidFilenamePlaceholder);
 324     }
 325   }
 326 
 327   if (timestamp != NULL) {
 328     if (pid == NULL || timestamp < pid) {
 329       first = timestamp_string;
 330       first_pos = timestamp - file_name;
 331       first_replace_len = strlen(TimestampFilenamePlaceholder);
 332     } else {
 333       second = timestamp_string;
 334       second_pos = timestamp - file_name;
 335       second_replace_len = strlen(TimestampFilenamePlaceholder);
 336     }
 337   }
 338 
 339   size_t first_len = strlen(first);
 340   size_t second_len = strlen(second);
 341 
 342   // Allocate the new buffer, size it to hold all we want to put in there +1.
 343   size_t result_len =  strlen(file_name) + first_len - first_replace_len + second_len - second_replace_len;
 344   result = NEW_C_HEAP_ARRAY(char, result_len + 1, mtLogging);
 345 
 346   // Assemble the strings
 347   size_t file_name_pos = 0;
 348   size_t i = 0;
 349   while (i < result_len) {
 350     if (file_name_pos == first_pos) {
 351       // We are in the range of the first placeholder
 352       strcpy(result + i, first);
 353       // Bump output buffer position with length of replacing string
 354       i += first_len;
 355       // Bump source buffer position to skip placeholder
 356       file_name_pos += first_replace_len;
 357     } else if (file_name_pos == second_pos) {
 358       // We are in the range of the second placeholder
 359       strcpy(result + i, second);
 360       i += second_len;
 361       file_name_pos += second_replace_len;
 362     } else {
 363       // Else, copy char by char of the original file
 364       result[i] = file_name[file_name_pos++];
 365       i++;
 366     }
 367   }
 368   // Add terminating char
 369   result[result_len] = '\0';
 370   return result;
 371 }