Use the function base::StrToUint64 to pass command-line option arguments. This gives us error checking, and support for the k, M and G suffixes. --- src/dtm/tools/osaflog.cc | 44 +++++++++++++++++++++++++---------------- src/dtm/transport/log_server.cc | 44 +++++++++++++++++++++++++---------------- src/dtm/transport/log_server.h | 4 ++-- src/dtm/transport/log_writer.cc | 10 +++++----- src/dtm/transport/log_writer.h | 4 ++-- 5 files changed, 63 insertions(+), 43 deletions(-)
diff --git a/src/dtm/tools/osaflog.cc b/src/dtm/tools/osaflog.cc index 8035733f2..7437885cc 100644 --- a/src/dtm/tools/osaflog.cc +++ b/src/dtm/tools/osaflog.cc @@ -31,6 +31,7 @@ #include <memory> #include <random> #include <string> +#include "base/string_parse.h" #include "base/time.h" #include "base/unix_server_socket.h" #include "dtm/common/osaflog_protocol.h" @@ -39,9 +40,10 @@ namespace { void PrintUsage(const char* program_name); +bool SendCommand(const std::string& command); +bool MaxTraceFileSize(uint64_t max_file_size); +bool NoOfBackupFiles(uint64_t number_of_backups); bool Flush(); -bool MaxTraceFileSize(size_t max_file_size); -bool NoOfBackupFiles(size_t number_of_backups); base::UnixServerSocket* CreateSocket(); uint64_t Random64Bits(uint64_t seed); bool PrettyPrint(const std::string& log_stream); @@ -62,8 +64,8 @@ int main(int argc, char** argv) { {"print", required_argument, nullptr, 'p'}, {0, 0, 0, 0}}; - size_t max_file_size = 0; - size_t max_backups = 0; + uint64_t max_file_size = 0; + uint64_t max_backups = 0; char *pretty_print_argument = NULL; int option = 0; @@ -94,12 +96,19 @@ int main(int argc, char** argv) { flush_set = true; break; case 'm': - max_file_size_set = true; - max_file_size = atoi(optarg); + max_file_size = base::StrToUint64(optarg, + &max_file_size_set); + if (!max_file_size_set || max_file_size > SIZE_MAX) { + fprintf(stderr, "Illegal max-file-size argument\n"); + exit(EXIT_FAILURE); + } break; case 'b': - max_backups_set = true; - max_backups = atoi(optarg); + max_backups = base::StrToUint64(optarg, &max_backups_set); + if (!max_backups_set || max_backups > SIZE_MAX) { + fprintf(stderr, "Illegal max-backups argument\n"); + exit(EXIT_FAILURE); + } break; default: PrintUsage(argv[0]); exit(EXIT_FAILURE); @@ -144,7 +153,7 @@ void PrintUsage(const char* program_name) { "LOGSTREAM. When a LOGSTREAM argument is specified, the option\n" "--flush is implied.\n" "\n" - "Opions:\n" + "Options:\n" "\n" "--flush Flush all buffered messages in the log server to\n" " disk even when no LOGSTREAM is specified\n" @@ -152,15 +161,16 @@ void PrintUsage(const char* program_name) { " specified LOGSTREAM.This option is default\n" " when no option is specified.\n" "--max-file-size Set the maximum size (in bytes) of the log file\n" - " before the log is rotated.\n" + " before the log is rotated. Suffixes k, M and G\n" + " can be used for kilo-, mega- and gigabytes.\n" "--max-backups Set the maximum number of backup files to keep\n" " when rotating the log.\n", program_name); } bool SendCommand(const std::string& command) { - std::string request{std::string{"?"} + command}; - std::string expected_reply{std::string{"!"} + command}; + std::string request(std::string("?") + command); + std::string expected_reply(std::string("!") + command); auto sock = std::unique_ptr<base::UnixServerSocket>(CreateSocket()); if (!sock) { @@ -222,13 +232,13 @@ bool SendCommand(const std::string& command) { return true; } -bool MaxTraceFileSize(size_t max_file_size) { - return SendCommand(std::string{"max-file-size "} + +bool MaxTraceFileSize(uint64_t max_file_size) { + return SendCommand(std::string("max-file-size ") + std::to_string(max_file_size)); } -bool NoOfBackupFiles(size_t max_backups) { - return SendCommand(std::string{"max-backups "} + std::to_string(max_backups)); +bool NoOfBackupFiles(uint64_t max_backups) { + return SendCommand(std::string("max-backups ") + std::to_string(max_backups)); } bool Flush() { @@ -309,7 +319,7 @@ std::list<int> OpenLogFiles(const std::string& log_stream) { std::string PathName(const std::string& log_stream, int suffix) { std::string path_name{PKGLOGDIR "/"}; path_name += log_stream; - if (suffix != 0) path_name += std::string{"."} + std::to_string(suffix); + if (suffix != 0) path_name += std::string(".") + std::to_string(suffix); return path_name; } diff --git a/src/dtm/transport/log_server.cc b/src/dtm/transport/log_server.cc index 76519cf35..a44d18680 100644 --- a/src/dtm/transport/log_server.cc +++ b/src/dtm/transport/log_server.cc @@ -19,9 +19,11 @@ #include "dtm/transport/log_server.h" #include <signal.h> #include <syslog.h> +#include <cstdint> #include <cstdlib> #include <cstring> #include "base/osaf_poll.h" +#include "base/string_parse.h" #include "base/time.h" #include "dtm/common/osaflog_protocol.h" #include "osaf/configmake.h" @@ -30,7 +32,7 @@ const Osaflog::ClientAddressConstantPrefix LogServer::address_header_{}; LogServer::LogServer(int term_fd) : term_fd_{term_fd}, - no_of_backups_{9}, + max_backups_{9}, max_file_size_{5 * 1024 * 1024}, log_socket_{Osaflog::kServerSocketPath, base::UnixSocket::kNonblocking}, log_streams_{}, @@ -104,7 +106,7 @@ LogServer::LogStream* LogServer::GetStream(const char* msg_id, if (iter != log_streams_.end()) return iter->second; if (no_of_log_streams_ >= kMaxNoOfStreams) return nullptr; if (!ValidateLogName(msg_id, msg_id_size)) return nullptr; - LogStream* stream = new LogStream{log_name, no_of_backups_, max_file_size_}; + LogStream* stream = new LogStream{log_name, max_backups_, max_file_size_}; auto result = log_streams_.insert( std::map<std::string, LogStream*>::value_type{log_name, stream}); if (!result.second) osaf_abort(msg_id_size); @@ -115,8 +117,6 @@ LogServer::LogStream* LogServer::GetStream(const char* msg_id, bool LogServer::ReadConfig(const char *transport_config_file) { FILE *transport_conf_file; char line[256]; - size_t max_file_size = 0; - size_t number_of_backup_files = 0; int i, n, comment_line, tag_len = 0; /* Open transportd.conf config file. */ @@ -153,20 +153,26 @@ bool LogServer::ReadConfig(const char *transport_config_file) { if (strncmp(line, "TRANSPORT_MAX_LOG_FILESIZE=", strlen("TRANSPORT_MAX_LOG_FILESIZE=")) == 0) { tag_len = strlen("TRANSPORT_MAX_LOG_FILESIZE="); - max_file_size = atoi(&line[tag_len]); - - if (max_file_size > 1) { + bool success; + uint64_t max_file_size = base::StrToUint64(&line[tag_len], &success); + if (success && max_file_size <= SIZE_MAX) { max_file_size_ = max_file_size; + } else { + syslog(LOG_ERR, "TRANSPORT_MAX_LOG_FILESIZE has illegal value '%s'", + &line[tag_len]); } } if (strncmp(line, "TRANSPORT_NO_OF_BACKUP_LOG_FILES=", strlen("TRANSPORT_NO_OF_BACKUP_LOG_FILES=")) == 0) { tag_len = strlen("TRANSPORT_NO_OF_BACKUP_LOG_FILES="); - number_of_backup_files = atoi(&line[tag_len]); - - if (number_of_backup_files > 1) { - no_of_backups_ = number_of_backup_files; + bool success; + uint64_t max_backups = base::StrToUint64(&line[tag_len], &success); + if (success && max_backups <= SIZE_MAX) { + max_backups_ = max_backups; + } else { + syslog(LOG_ERR, "TRANSPORT_NO_OF_BACKUP_LOG_FILES " + "has illegal value '%s'", &line[tag_len]); } } } @@ -221,11 +227,15 @@ bool LogServer::ValidateAddress(const struct sockaddr_un& addr, std::string LogServer::ExecuteCommand(const std::string& command, const std::string& argument) { if (command == "?max-file-size") { - max_file_size_ = atoi(argument.c_str()); + bool success; + uint64_t max_file_size = base::StrToUint64(argument.c_str(), &success); + if (success && max_file_size <= SIZE_MAX) max_file_size_ = max_file_size; return std::string{"!max-file-size " + std::to_string(max_file_size_)}; } else if (command == "?max-backups") { - no_of_backups_ = atoi(argument.c_str()); - return std::string{"!max-backups " + std::to_string(no_of_backups_)}; + bool success; + uint64_t max_backups = base::StrToUint64(argument.c_str(), &success); + if (success && max_backups <= SIZE_MAX) max_backups_ = max_backups; + return std::string{"!max-backups " + std::to_string(max_backups_)}; } else if (command == "?flush") { for (const auto& s : log_streams_) { LogStream* stream = s.second; @@ -238,9 +248,9 @@ std::string LogServer::ExecuteCommand(const std::string& command, } LogServer::LogStream::LogStream(const std::string& log_name, - size_t no_of_backups_, size_t max_file_size_) - : log_name_{log_name}, last_flush_{}, log_writer_{log_name, no_of_backups_, - max_file_size_} { + size_t max_backups, size_t max_file_size) + : log_name_{log_name}, last_flush_{}, log_writer_{log_name, max_backups, + max_file_size} { if (log_name.size() > kMaxLogNameSize) osaf_abort(log_name.size()); } diff --git a/src/dtm/transport/log_server.h b/src/dtm/transport/log_server.h index ee9a9685b..a767c5c63 100644 --- a/src/dtm/transport/log_server.h +++ b/src/dtm/transport/log_server.h @@ -51,7 +51,7 @@ class LogServer { class LogStream { public: static constexpr size_t kMaxLogNameSize = 32; - LogStream(const std::string& log_name, size_t no_of_backups, + LogStream(const std::string& log_name, size_t max_backups, size_t max_file_size); size_t log_name_size() const { return log_name_.size(); } @@ -90,7 +90,7 @@ class LogServer { const std::string& argument); int term_fd_; // Configuration for LogServer - size_t no_of_backups_; + size_t max_backups_; size_t max_file_size_; base::UnixServerSocket log_socket_; diff --git a/src/dtm/transport/log_writer.cc b/src/dtm/transport/log_writer.cc index a5de8f554..0195c2555 100644 --- a/src/dtm/transport/log_writer.cc +++ b/src/dtm/transport/log_writer.cc @@ -26,14 +26,14 @@ #include "dtm/transport/log_writer.h" #include "dtm/transport/log_server.h" -LogWriter::LogWriter(const std::string& log_name, size_t no_of_backups, +LogWriter::LogWriter(const std::string& log_name, size_t max_backups, size_t max_file_size) : log_file_{base::GetEnv<std::string>("pkglogdir", PKGLOGDIR) + "/" + log_name}, fd_{-1}, current_file_size_{0}, current_buffer_size_{0}, - no_of_backups_{no_of_backups}, + max_backups_{max_backups}, max_file_size_{max_file_size}, buffer_{new char[kBufferSize]} {} @@ -46,7 +46,7 @@ LogWriter::~LogWriter() { std::string LogWriter::log_file(size_t backup) const { std::string file_name = log_file_; if (backup != 0) { - file_name += std::string{"."} + std::to_string(backup); + file_name += std::string(".") + std::to_string(backup); } return file_name; } @@ -77,8 +77,8 @@ void LogWriter::Close() { void LogWriter::RotateLog() { Close(); - unlink(log_file(no_of_backups_).c_str()); - for (size_t i = no_of_backups_; i != 0; --i) { + unlink(log_file(max_backups_).c_str()); + for (size_t i = max_backups_; i != 0; --i) { std::string backup_name = log_file(i); std::string previous_backup = log_file(i - 1); if (rename(previous_backup.c_str(), backup_name.c_str()) != 0) { diff --git a/src/dtm/transport/log_writer.h b/src/dtm/transport/log_writer.h index 09c38f5f7..e70f9c22f 100644 --- a/src/dtm/transport/log_writer.h +++ b/src/dtm/transport/log_writer.h @@ -29,7 +29,7 @@ class LogWriter { public: constexpr static const size_t kMaxMessageSize = 2 * size_t{1024}; - LogWriter(const std::string& log_name, size_t no_of_backups, + LogWriter(const std::string& log_name, size_t max_backups, size_t max_file_size); virtual ~LogWriter(); @@ -54,7 +54,7 @@ class LogWriter { int fd_; size_t current_file_size_; size_t current_buffer_size_; - size_t no_of_backups_; + size_t max_backups_; size_t max_file_size_; char* buffer_; -- 2.13.3 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Opensaf-devel mailing list Opensaf-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensaf-devel