--- src/dtm/common/osaflog_protocol.h | 13 +++ src/dtm/tools/osaflog.cc | 168 ++++++++++++++++++++++++++++++++------ src/dtm/transport/log_server.cc | 74 ++++++++--------- src/dtm/transport/log_server.h | 14 ++-- src/dtm/transport/log_writer.cc | 5 +- src/dtm/transport/log_writer.h | 3 +- src/dtm/transport/main.cc | 6 +- src/dtm/transport/transportd.conf | 8 +- 8 files changed, 211 insertions(+), 80 deletions(-)
diff --git a/src/dtm/common/osaflog_protocol.h b/src/dtm/common/osaflog_protocol.h index 61e9f6f..54551ee 100644 --- a/src/dtm/common/osaflog_protocol.h +++ b/src/dtm/common/osaflog_protocol.h @@ -24,6 +24,19 @@ namespace Osaflog { +enum cmd { FLUSH, MAXBACKUPS, MAXFILESIZE}; +enum cmdreply { RFLUSH = 101, RMAXBACKUPS, RMAXFILESIZE, FAILURE}; +struct cmd_osaflog { + char marker[4]; + enum cmd m_cmd; // Command Enum + size_t m_value; // Value based on the command +}; + + +struct cmd_osaflog_resp { + enum cmdreply m_cmdreply; // Command Enum +}; + static constexpr const char* kServerSocketPath = PKGLOCALSTATEDIR "/osaf_log.sock"; diff --git a/src/dtm/tools/osaflog.cc b/src/dtm/tools/osaflog.cc index 3ce66f4..e0d135b 100644 --- a/src/dtm/tools/osaflog.cc +++ b/src/dtm/tools/osaflog.cc @@ -14,6 +14,7 @@ */ #include <fcntl.h> +#include <getopt.h> #include <poll.h> #include <sched.h> #include <sys/socket.h> @@ -39,6 +40,8 @@ namespace { void PrintUsage(const char* program_name); bool Flush(); +bool MaxTraceFileSize(size_t maxfilesize); +bool NoOfBackupFiles(size_t nooffiles); base::UnixServerSocket* CreateSocket(); uint64_t Random64Bits(uint64_t seed); bool PrettyPrint(const std::string& log_stream); @@ -53,20 +56,82 @@ char buf[65 * 1024]; } // namespace int main(int argc, char** argv) { - bool flush_option = false; - if (argc >= 2 && strcmp(argv[1], "--flush") == 0) { - flush_option = true; - --argc; - ++argv; - } - if ((argc != 2) && (argc != 1 || flush_option == false)) { + struct option long_options[] = {{"max-file-size", required_argument, 0, 'm'}, + {"max-backups", required_argument, 0, 'b'}, + {"flush", no_argument, 0, 'f'}, + {"print", required_argument, 0, 'p'}, + {0, 0, 0, 0}}; + + size_t maxfilesize = 0; + size_t maxbackups = 0; + char *pplog = NULL; + int opt = 0; + + int long_index = 0; + bool flush_result = true; + bool print_result = true; + bool maxfilesize_result = true; + bool noof_backup_result = true; + bool flush_set = false; + bool prettyprint_set = false; + bool maxfilesize_set = false; + bool maxbackups_set = false; + + if (argc == 1) { PrintUsage(argv[0]); exit(EXIT_FAILURE); } - bool flush_result = Flush(); - bool print_result = true; - if (argc == 2) print_result = PrettyPrint(argv[1]); - exit((flush_result && print_result) ? EXIT_SUCCESS : EXIT_FAILURE); + + while ((opt = getopt_long(argc, argv, "m:b:p:f", + long_options, &long_index)) != -1) { + switch (opt) { + case 'p': + pplog = optarg; + prettyprint_set = true; + flush_set = true; + break; + case 'f': + flush_set = true; + break; + case 'm': + maxfilesize_set = true; + maxfilesize = atoi(optarg); + break; + case 'b': + maxbackups_set = true; + maxbackups = atoi(optarg); + break; + default: PrintUsage(argv[0]); + exit(EXIT_FAILURE); + } + } + + if (argc - optind == 1) { + flush_result = Flush(); + flush_set = false; + print_result = PrettyPrint(argv[optind]); + prettyprint_set = false; + } else if (argc - optind > 1) { + PrintUsage(argv[0]); + exit(EXIT_FAILURE); + } + + if (flush_set == true) { + flush_result = Flush(); + } + if (prettyprint_set == true) { + print_result = PrettyPrint(pplog); + } + if (maxbackups_set == true) { + noof_backup_result = NoOfBackupFiles(maxbackups); + } + if (maxfilesize_set == true) { + maxfilesize_result = MaxTraceFileSize(maxfilesize); + } + if (flush_result && print_result && maxfilesize_result && + noof_backup_result) + exit(EXIT_SUCCESS); + exit(EXIT_FAILURE); } namespace { @@ -75,18 +140,27 @@ void PrintUsage(const char* program_name) { fprintf(stderr, "Usage: %s [OPTION] [LOGSTREAM]\n" "\n" - "Pretty-print the messages stored on disk for the specified\n" + "print the messages stored on disk for the specified\n" "LOGSTREAM. When a LOGSTREAM argument is specified, the option\n" "--flush is implied.\n" "\n" "Opions:\n" "\n" - " --flush Flush all buffered messages in the log server to disk\n" - " even when no LOGSTREAM is specified\n", + "--flush Flush all buffered messages in the log server to\n" + " disk even when no LOGSTREAM is specified\n" + "--print print the messages stored on disk for the \n" + " 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" + "--max-backups Set the maximum number of backup files to keep\n" + " when rotating the log.\n", program_name); } -bool Flush() { + +bool cmdProcessor(struct Osaflog::cmd_osaflog cmd, + enum Osaflog::cmdreply m_cmdreply) { auto sock = std::unique_ptr<base::UnixServerSocket>(CreateSocket()); if (!sock) { @@ -97,17 +171,18 @@ bool Flush() { struct sockaddr_un osaftransportd_addr; socklen_t addrlen = base::UnixSocket::SetAddress(Osaflog::kServerSocketPath, &osaftransportd_addr); - const char flush_command[] = "?flush"; - ssize_t result = sock->SendTo(flush_command, sizeof(flush_command) - 1, - &osaftransportd_addr, addrlen); + + ssize_t result = sock->SendTo(&cmd, sizeof(cmd), + &osaftransportd_addr, addrlen); if (result < 0) { perror("Failed to send message to osaftransportd"); return false; - } else if (static_cast<size_t>(result) != (sizeof(flush_command) - 1)) { + } else if (static_cast<size_t>(result) != + (sizeof(struct Osaflog::cmd_osaflog))) { fprintf(stderr, "Failed to send message to osaftransportd\n"); return false; } - static const char expected_reply[] = "!flush"; + struct timespec end_time = base::ReadMonotonicClock() + base::kTenSeconds; for (;;) { struct pollfd fds { @@ -139,14 +214,57 @@ bool Flush() { if (result < 0) { perror("Failed to receive reply from osaftransportd"); return false; - } else if (static_cast<size_t>(result) != (sizeof(expected_reply) - 1) || - memcmp(buf, expected_reply, result) != 0) { - fprintf(stderr, "Received unexpected reply from osaftransportd\n"); - return false; + } else if (static_cast<size_t>(result) != + (sizeof(struct Osaflog::cmd_osaflog_resp) )) { + struct Osaflog::cmd_osaflog_resp cmdreply_buf; + memcpy(&cmdreply_buf, buf, result); + if (cmdreply_buf.m_cmdreply != m_cmdreply) { + fprintf(stderr, "Received unexpected reply from osaftransportd\n"); + return false; + } } return true; } +bool MaxTraceFileSize(size_t maxfilesize) { + struct Osaflog::cmd_osaflog maxfilesize_cmd; + + memset(&maxfilesize_cmd, 0, sizeof(maxfilesize_cmd)); + maxfilesize_cmd.marker[0]='?'; + maxfilesize_cmd.m_cmd = Osaflog::MAXFILESIZE; + maxfilesize_cmd.m_value = maxfilesize; + + bool result; + result = cmdProcessor(maxfilesize_cmd, Osaflog::RMAXFILESIZE); + return result; +} + +bool NoOfBackupFiles(size_t nooffiles) { + struct Osaflog::cmd_osaflog maxbackups_cmd; + + memset(&maxbackups_cmd, 0, sizeof(maxbackups_cmd)); + maxbackups_cmd.marker[0]='?'; + maxbackups_cmd.m_cmd = Osaflog::MAXBACKUPS; + maxbackups_cmd.m_value = nooffiles; + + bool result; + result = cmdProcessor(maxbackups_cmd, Osaflog::RMAXBACKUPS); + return result; +} + +bool Flush() { + struct Osaflog::cmd_osaflog flush_cmd; + + memset(&flush_cmd, 0, sizeof(flush_cmd)); + flush_cmd.marker[0]='?'; + flush_cmd.m_cmd = Osaflog::FLUSH; + flush_cmd.m_value = 0; + + bool result; + result = cmdProcessor(flush_cmd, Osaflog::RFLUSH); + return result; +} + base::UnixServerSocket* CreateSocket() { base::UnixServerSocket* sock = nullptr; Osaflog::ClientAddress addr{}; @@ -221,7 +339,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 780feb1..e29b8d4 100644 --- a/src/dtm/transport/log_server.cc +++ b/src/dtm/transport/log_server.cc @@ -17,26 +17,24 @@ */ #include "dtm/transport/log_server.h" -#include <cstring> #include <signal.h> #include <syslog.h> +#include <cstring> #include "base/osaf_poll.h" #include "base/time.h" #include "dtm/common/osaflog_protocol.h" #include "osaf/configmake.h" -#define TRANSPORTD_CONFIG_FILE PKGSYSCONFDIR "/transportd.conf" - -size_t LogServer::no_of_backups = 9; -size_t LogServer::kmax_file_size = 5000 * 1024; const Osaflog::ClientAddressConstantPrefix LogServer::address_header_{}; LogServer::LogServer(int term_fd) : term_fd_{term_fd}, + no_of_backups_{9}, + max_file_size_{5 * 1024 * 1024}, log_socket_{Osaflog::kServerSocketPath, base::UnixSocket::kNonblocking}, log_streams_{}, - current_stream_{new LogStream{"mds.log", 1, LogServer::kmax_file_size}}, + current_stream_{new LogStream{"mds.log", 1, 5 * 1024 * 1024}}, no_of_log_streams_{1} { log_streams_["mds.log"] = current_stream_; } @@ -48,11 +46,6 @@ LogServer::~LogServer() { void LogServer::Run() { struct pollfd pfd[2] = {{term_fd_, POLLIN, 0}, {log_socket_.fd(), POLLIN, 0}}; - /* Initialize a signal handler for loading new configuration from transportd.conf */ - if ((signal(SIGUSR2, usr2_sig_handler)) == SIG_ERR) { - syslog(LOG_ERR,"signal USR2 registration failed: %s", strerror(errno)); - } - do { for (int i = 0; i < 256; ++i) { char* buffer = current_stream_->current_buffer_position(); @@ -101,12 +94,6 @@ void LogServer::Run() { } while ((pfd[0].revents & POLLIN) == 0); } -void LogServer::usr2_sig_handler(int sig) { - syslog(LOG_ERR, "Recived the SIGUSR2 Signal"); - ReadConfig(TRANSPORTD_CONFIG_FILE); - signal(SIGUSR2, usr2_sig_handler); -} - LogServer::LogStream* LogServer::GetStream(const char* msg_id, size_t msg_id_size) { if (msg_id_size == current_stream_->log_name_size() && @@ -119,7 +106,7 @@ LogServer::LogStream* LogServer::GetStream(const char* msg_id, if (no_of_log_streams_ >= kMaxNoOfStreams) return nullptr; if (!ValidateLogName(msg_id, msg_id_size)) return nullptr; - LogStream* stream = new LogStream{log_name, LogServer::no_of_backups, LogServer::kmax_file_size}; + LogStream* stream = new LogStream{log_name, no_of_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); @@ -130,15 +117,14 @@ 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 maxFileSize=0; - size_t noOfBackupFiles=0; + size_t maxFileSize = 0; + size_t noOfBackupFiles = 0; int i, n, comment_line, tag_len = 0; /* Open transportd.conf config file. */ transport_conf_file = fopen(transport_config_file, "r"); if (transport_conf_file == nullptr) { - - syslog(LOG_ERR,"Not able to read transportd.conf: %s", strerror(errno)); + syslog(LOG_ERR, "Not able to read transportd.conf: %s", strerror(errno)); return false; } @@ -154,13 +140,14 @@ bool LogServer::ReadConfig(const char *transport_config_file) { n = strlen(line); comment_line = 0; for (i = 0; i < n; i++) { - if ((line[i] == ' ') || (line[i] == '\t')) + if ((line[i] == ' ') || (line[i] == '\t')) { continue; - else if (line[i] == '#') { + } else if (line[i] == '#') { comment_line = 1; break; - } else + } else { break; + } } if (comment_line) continue; line[n - 1] = 0; @@ -171,7 +158,7 @@ bool LogServer::ReadConfig(const char *transport_config_file) { maxFileSize = atoi(&line[tag_len]); if (maxFileSize > 1) { - LogServer::kmax_file_size = maxFileSize * 1024 * 1024; + max_file_size_ = maxFileSize; } } @@ -181,7 +168,7 @@ bool LogServer::ReadConfig(const char *transport_config_file) { noOfBackupFiles = atoi(&line[tag_len]); if (noOfBackupFiles > 1) { - LogServer::no_of_backups = noOfBackupFiles; + no_of_backups_ = noOfBackupFiles; } } } @@ -210,8 +197,9 @@ void LogServer::ExecuteCommand(const char* command, size_t size, const struct sockaddr_un& addr, socklen_t addrlen) { if (ValidateAddress(addr, addrlen)) { - std::string cmd_result = ExecuteCommand(std::string{command, size}); - log_socket_.SendTo(cmd_result.data(), cmd_result.size(), &addr, addrlen); + struct Osaflog::cmd_osaflog_resp cmdreply_buf; + cmdreply_buf.m_cmdreply = ExecuteCommand(command, size); + log_socket_.SendTo(&cmdreply_buf, sizeof(cmdreply_buf), &addr, addrlen); } } @@ -224,21 +212,27 @@ bool LogServer::ValidateAddress(const struct sockaddr_un& addr, } } -std::string LogServer::ExecuteCommand(const std::string& command) { - if (command == "?flush") { - for (const auto& s : log_streams_) { - LogStream* stream = s.second; - stream->Flush(); - } - return std::string{"!flush"}; - } else { - return std::string{"!not_supported"}; +enum Osaflog::cmdreply LogServer::ExecuteCommand(const char * command, + size_t size) { + struct Osaflog::cmd_osaflog cmd_buf; + memcpy(&cmd_buf, command, size); + + if (cmd_buf.m_cmd == Osaflog::MAXFILESIZE) { + max_file_size_ = cmd_buf.m_value; + return Osaflog::RMAXFILESIZE; + } else if (cmd_buf.m_cmd == Osaflog::MAXBACKUPS) { + no_of_backups_ = cmd_buf.m_value; + return Osaflog::RMAXBACKUPS; + } else if (cmd_buf.m_cmd == Osaflog::FLUSH) { + return Osaflog::RFLUSH; } + return Osaflog::FAILURE; } LogServer::LogStream::LogStream(const std::string& log_name, - size_t no_of_backups, size_t kmax_file_size) - : log_name_{log_name}, last_flush_{}, log_writer_{log_name, no_of_backups, kmax_file_size} { + 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_} { 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 95ea980..eb04fc7 100644 --- a/src/dtm/transport/log_server.h +++ b/src/dtm/transport/log_server.h @@ -33,6 +33,8 @@ class LogServer { public: static constexpr size_t kMaxNoOfStreams = 32; + static constexpr const char* kTransportdConfigFile = + PKGSYSCONFDIR "/transportd.conf"; // @a term_fd is a file descriptor that will become readable when the program // should exit because it has received the SIGTERM signal. explicit LogServer(int term_fd); @@ -43,13 +45,14 @@ class LogServer { // by making the term_fd (provided in the constructor) readable. void Run(); // To read Transportd.conf - static bool ReadConfig(const char *transport_config_file); + bool ReadConfig(const char *transport_config_file); private: class LogStream { public: static constexpr size_t kMaxLogNameSize = 32; - LogStream(const std::string& log_name, size_t no_of_backups, size_t kMaxFileSize); + LogStream(const std::string& log_name, size_t no_of_backups, + size_t kMaxFileSize); size_t log_name_size() const { return log_name_.size(); } const char* log_name_data() const { return log_name_.data(); } @@ -85,11 +88,12 @@ class LogServer { static void usr2_sig_handler(int sig); static bool ValidateAddress(const struct sockaddr_un& addr, socklen_t addrlen); - std::string ExecuteCommand(const std::string& command); +// std::string ExecuteCommand(const std::string& command); + enum Osaflog::cmdreply ExecuteCommand(const char* command, size_t size); int term_fd_; // Configuration for LogServer - static size_t no_of_backups; - static size_t kmax_file_size; + size_t no_of_backups_; + size_t max_file_size_; base::UnixServerSocket log_socket_; std::map<std::string, LogStream*> log_streams_; diff --git a/src/dtm/transport/log_writer.cc b/src/dtm/transport/log_writer.cc index 329ad77..60c0bf5 100644 --- a/src/dtm/transport/log_writer.cc +++ b/src/dtm/transport/log_writer.cc @@ -26,7 +26,8 @@ #include "base/getenv.h" #include "osaf/configmake.h" -LogWriter::LogWriter(const std::string& log_name, size_t no_of_backups, size_t kmax_file_size) +LogWriter::LogWriter(const std::string& log_name, size_t no_of_backups, + size_t kmax_file_size) : log_file_{base::GetEnv<std::string>("pkglogdir", PKGLOGDIR) + "/" + log_name}, fd_{-1}, @@ -45,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; } diff --git a/src/dtm/transport/log_writer.h b/src/dtm/transport/log_writer.h index eb38cfa..b6cf6c1 100644 --- a/src/dtm/transport/log_writer.h +++ b/src/dtm/transport/log_writer.h @@ -29,7 +29,8 @@ class LogWriter { public: constexpr static const size_t kMaxMessageSize = 2 * size_t{1024}; - LogWriter(const std::string& log_name, size_t no_of_backups, size_t kmax_file_size); + LogWriter(const std::string& log_name, size_t no_of_backups, + size_t kmax_file_size); virtual ~LogWriter(); char* current_buffer_position() { return buffer_ + current_buffer_size_; } diff --git a/src/dtm/transport/main.cc b/src/dtm/transport/main.cc index 0d1fedf..077601a 100644 --- a/src/dtm/transport/main.cc +++ b/src/dtm/transport/main.cc @@ -27,8 +27,8 @@ #include "dtm/transport/transport_monitor.h" -#define TRANSPORTD_CONFIG_FILE PKGSYSCONFDIR "/transportd.conf" - +static constexpr const char* kTransportdConfigFile = + PKGSYSCONFDIR "/transportd.conf"; constexpr static const int kDaemonStartWaitTimeInSeconds = 15; enum Termination { kExit, kDaemonExit, kReboot }; @@ -42,6 +42,7 @@ struct Result { static void* LogServerStartFunction(void* instance) { LogServer* log_server = static_cast<LogServer*>(instance); + log_server->ReadConfig(kTransportdConfigFile); log_server->Run(); return nullptr; } @@ -85,7 +86,6 @@ Result MainFunction(int term_fd) { pthread_attr_destroy(&attr); return Result{kExit, "pthread_attr_setinheritsched() failed", result}; } - LogServer::ReadConfig(TRANSPORTD_CONFIG_FILE); LogServer log_server{term_fd}; pthread_t thread_id; result = diff --git a/src/dtm/transport/transportd.conf b/src/dtm/transport/transportd.conf index 48b334f..2a410b8 100644 --- a/src/dtm/transport/transportd.conf +++ b/src/dtm/transport/transportd.conf @@ -2,12 +2,12 @@ # # TRANSPORT_MAX_LOG_FILESIZE: The maximum size of the log file. The size value should -# be in MB's i.e if you give 6 then it is treated as 6 MB. By default value will be -# 5 MB -TRANSPORT_MAX_LOG_FILESIZE=5 +# be in Bytes i.e if you want to give 5 MB please mention it as 5242880. it is treated +# as 5 MB. By default value will be 5242880 Bytes +#TRANSPORT_MAX_LOG_FILESIZE=5242880 # # TRANSPORT_NO_OF_BACKUP_LOG_FILES: Number of backup files to maintain. Log rotation will # be done based on this value. Default value will be 9 # i.e totally 10 log files will be maintain. -TRANSPORT_NO_OF_BACKUP_LOG_FILES=9 +#TRANSPORT_NO_OF_BACKUP_LOG_FILES=9 -- 1.9.1 ------------------------------------------------------------------------------ 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