osaf/libs/agents/saf/lga/lga_api.c | 58 ++++- osaf/libs/common/logsv/include/lgsv_defs.h | 13 - osaf/libs/common/logsv/include/lgsv_msg.h | 4 +- osaf/services/saf/logsv/lgs/Makefile.am | 14 +- osaf/services/saf/logsv/lgs/lgs.h | 12 +- osaf/services/saf/logsv/lgs/lgs_config.cc | 84 +++--- osaf/services/saf/logsv/lgs/lgs_config.h | 7 +- osaf/services/saf/logsv/lgs/lgs_evt.cc | 61 ++-- osaf/services/saf/logsv/lgs/lgs_filehdl.cc | 148 ++++--------- osaf/services/saf/logsv/lgs/lgs_filehdl.h | 35 +- osaf/services/saf/logsv/lgs/lgs_imm.cc | 178 +++++++-------- osaf/services/saf/logsv/lgs/lgs_mbcsv.cc | 32 +- osaf/services/saf/logsv/lgs/lgs_mbcsv_v2.cc | 5 +- osaf/services/saf/logsv/lgs/lgs_mds.cc | 60 ++++- osaf/services/saf/logsv/lgs/lgs_stream.cc | 261 ++++++++++------------- osaf/services/saf/logsv/lgs/lgs_stream.h | 30 +- osaf/services/saf/logsv/lgs/lgs_util.cc | 312 +++++++++++++++------------ osaf/services/saf/logsv/lgs/lgs_util.h | 29 +- 18 files changed, 674 insertions(+), 669 deletions(-)
Also replace static arrays of log file name, path name by C++ string. diff --git a/osaf/libs/agents/saf/lga/lga_api.c b/osaf/libs/agents/saf/lga/lga_api.c --- a/osaf/libs/agents/saf/lga/lga_api.c +++ b/osaf/libs/agents/saf/lga/lga_api.c @@ -27,6 +27,13 @@ (SA_DISPATCH_BLOCKING == flag) ) #define LGSV_NANOSEC_TO_LEAPTM 10000000 +/** + * Temporary maximum log file name length to avoid LOG client sends + * log file name too long (big data) to LOG service. + * + * The real limit check will be done by service side. + */ +#define LGA_FILE_LENGTH_TEMP_LIMIT 2048 /* The main controle block */ lga_cb_t lga_cb = { @@ -58,15 +65,6 @@ static void populate_open_params(lgsv_st } else { /* Server will assign a def fmt string if needed (logFileFmt==NULL) */ open_param->logFileFmt = logFileCreateAttributes->logFileFmt; - - strncpy(open_param->logFileName, logFileCreateAttributes->logFileName, NAME_MAX); - - /* A NULL pointer refers to impl defined directory */ - if (logFileCreateAttributes->logFilePathName == NULL) - strcpy(open_param->logFilePathName, "."); - else - strncpy(open_param->logFilePathName, logFileCreateAttributes->logFilePathName, PATH_MAX); - open_param->maxLogFileSize = logFileCreateAttributes->maxLogFileSize; open_param->maxLogRecordSize = logFileCreateAttributes->maxLogRecordSize; open_param->haProperty = logFileCreateAttributes->haProperty; @@ -74,6 +72,7 @@ static void populate_open_params(lgsv_st open_param->maxFilesRotated = logFileCreateAttributes->maxFilesRotated; open_param->lstr_open_flags = logStreamOpenFlags; } + TRACE_LEAVE(); } @@ -561,8 +560,8 @@ static SaAisErrorT validate_open_params( /* Check implementation specific string length */ if (NULL != logFileCreateAttributes) { len = strlen(logFileCreateAttributes->logFileName); - if ((len == 0) || (len > LOG_NAME_MAX)) { - TRACE("logFileName"); + if ((len == 0) || (len > LGA_FILE_LENGTH_TEMP_LIMIT)) { + TRACE("logFileName is too long (max = %d)", LGA_FILE_LENGTH_TEMP_LIMIT); return SA_AIS_ERR_INVALID_PARAM; } if (logFileCreateAttributes->logFilePathName != NULL) { @@ -628,9 +627,41 @@ SaAisErrorT saLogStreamOpen_2(SaLogHandl msg.info.api_info.type = LGSV_STREAM_OPEN_REQ; open_param = &msg.info.api_info.param.lstr_open_sync; + /* Make it safe for free */ + open_param->logFileName = NULL; + open_param->logFilePathName = NULL; + populate_open_params(open_param, logStreamName, - hdl_rec, (SaLogFileCreateAttributesT_2 *)logFileCreateAttributes, logStreamOpenFlags); + hdl_rec, + (SaLogFileCreateAttributesT_2 *)logFileCreateAttributes, + logStreamOpenFlags); + + if (logFileCreateAttributes != NULL) { + /* Construct the logFileName */ + open_param->logFileName = (char *) malloc(strlen(logFileCreateAttributes->logFileName) + 1); + if (open_param->logFileName == NULL) { + rc = SA_AIS_ERR_NO_MEMORY; + goto done_give_hdl; + } + strcpy(open_param->logFileName, logFileCreateAttributes->logFileName); + + /* Construct the logFilePathName */ + /* A NULL pointer refers to impl defined directory */ + size_t len = (logFileCreateAttributes->logFilePathName == NULL) ? (2) : + (strlen(logFileCreateAttributes->logFilePathName) + 1); + + open_param->logFilePathName = (char *) malloc(len); + if (open_param->logFilePathName == NULL) { + rc = SA_AIS_ERR_NO_MEMORY; + goto done_give_hdl; + } + + if (logFileCreateAttributes->logFilePathName == NULL) + strcpy(open_param->logFilePathName, "."); + else + strcpy(open_param->logFilePathName, logFileCreateAttributes->logFilePathName); + } /* Normalize the timeOut value */ timeout = (uint32_t)(timeOut / LGSV_NANOSEC_TO_LEAPTM); @@ -699,6 +730,9 @@ SaAisErrorT saLogStreamOpen_2(SaLogHandl done_give_hdl: ncshm_give_hdl(logHandle); + free(open_param->logFileName); + free(open_param->logFilePathName); + done: TRACE_LEAVE(); return rc; diff --git a/osaf/libs/common/logsv/include/lgsv_defs.h b/osaf/libs/common/logsv/include/lgsv_defs.h --- a/osaf/libs/common/logsv/include/lgsv_defs.h +++ b/osaf/libs/common/logsv/include/lgsv_defs.h @@ -25,17 +25,4 @@ // Waiting time in library for sync send, unit 10ms #define LGS_WAIT_TIME 1000 -// The length of '_yyyymmdd_hhmmss_yyyymmdd_hhmmss.log' including null-termination char -#define LOG_TAIL_MAX 37 - -/** - * The log file name format: <filename>_<createtime>_<closetime>.log - * '_<createtime>_<closetime>.log' part is appended by logsv and has above format, - * and '<filename>' part can set by user. Limit the length of <filename> from user, - * to make sure the log file name does not exceed the maximum length (NAME_MAX). - * - * This macro defines the maximum length for <filename> part. - */ -#define LOG_NAME_MAX (NAME_MAX - LOG_TAIL_MAX) - #endif /* LGSV_DEFS_H */ diff --git a/osaf/libs/common/logsv/include/lgsv_msg.h b/osaf/libs/common/logsv/include/lgsv_msg.h --- a/osaf/libs/common/logsv/include/lgsv_msg.h +++ b/osaf/libs/common/logsv/include/lgsv_msg.h @@ -76,8 +76,8 @@ typedef struct { typedef struct { uint32_t client_id; SaNameT lstr_name; - char logFileName[NAME_MAX]; - char logFilePathName[PATH_MAX]; + char *logFileName; + char *logFilePathName; SaUint64T maxLogFileSize; SaUint32T maxLogRecordSize; SaBoolT haProperty; diff --git a/osaf/services/saf/logsv/lgs/Makefile.am b/osaf/services/saf/logsv/lgs/Makefile.am --- a/osaf/services/saf/logsv/lgs/Makefile.am +++ b/osaf/services/saf/logsv/lgs/Makefile.am @@ -24,17 +24,17 @@ SUBDIRS = scripts noinst_HEADERS = \ lgs_cb.h \ lgs_fmt.h \ - lgs_mbcsv.h \ - lgs_mbcsv_v1.h \ - lgs_mbcsv_v2.h \ lgs_util.h \ lgs_evt.h \ lgs.h \ lgs_stream.h \ lgs_file.h \ lgs_filehdl.h \ + lgs_config.h \ + lgs_mbcsv.h \ + lgs_mbcsv_v1.h \ + lgs_mbcsv_v2.h \ lgs_mbcsv_v3.h \ - lgs_config.h \ lgs_mbcsv_v5.h osaf_execbindir = $(pkglibdir) @@ -53,15 +53,15 @@ osaflogd_SOURCES = \ lgs_fmt.cc \ lgs_imm.cc \ lgs_main.cc \ - lgs_mbcsv.cc \ lgs_config.cc \ - lgs_mbcsv_v1.cc \ - lgs_mbcsv_v2.cc \ lgs_mds.cc \ lgs_stream.cc \ lgs_util.cc \ lgs_file.cc \ lgs_filehdl.cc \ + lgs_mbcsv.cc \ + lgs_mbcsv_v1.cc \ + lgs_mbcsv_v2.cc \ lgs_mbcsv_v3.cc \ lgs_mbcsv_v5.cc diff --git a/osaf/services/saf/logsv/lgs/lgs.h b/osaf/services/saf/logsv/lgs/lgs.h --- a/osaf/services/saf/logsv/lgs/lgs.h +++ b/osaf/services/saf/logsv/lgs/lgs.h @@ -94,14 +94,16 @@ extern uint32_t lgs_mds_init(lgs_cb_t *c extern uint32_t lgs_mds_finalize(lgs_cb_t *cb); extern uint32_t lgs_mds_change_role(lgs_cb_t *cb); extern uint32_t lgs_mds_msg_send(lgs_cb_t *cb, - lgsv_msg_t *msg, - MDS_DEST *dest, MDS_SYNC_SND_CTXT *mds_ctxt, MDS_SEND_PRIORITY_TYPE prio); + lgsv_msg_t *msg, + MDS_DEST *dest, + MDS_SYNC_SND_CTXT *mds_ctxt, + MDS_SEND_PRIORITY_TYPE prio); extern SaAisErrorT lgs_imm_create_configStream(lgs_cb_t *cb); extern void logRootDirectory_filemove( - const char *new_logRootDirectory, - const char *old_logRootDirectory, - time_t *cur_time_in); + const std::string &new_logRootDirectory, + const std::string &old_logRootDirectory, + time_t *cur_time_in); extern void logDataGroupname_fileown(const char *new_logDataGroupname); diff --git a/osaf/services/saf/logsv/lgs/lgs_config.cc b/osaf/services/saf/logsv/lgs/lgs_config.cc --- a/osaf/services/saf/logsv/lgs/lgs_config.cc +++ b/osaf/services/saf/logsv/lgs/lgs_config.cc @@ -109,7 +109,7 @@ static struct lgs_conf_def_t { */ typedef struct _lgs_conf_t { /* --- Corresponds to IMM Class SaLogConfig --- */ - char logRootDirectory[PATH_MAX]; + std::string logRootDirectory; char logDataGroupname[UT_NAMESIZE]; char logStreamFileFormat[MAX_FIELD_SIZE]; SaUint32T logMaxLogrecsize; @@ -164,7 +164,7 @@ typedef struct _lgs_conf_t { logFileSysConfig_cnfflag = LGS_CNF_DEF; logStreamFileFormat_cnfflag = LGS_CNF_DEF; - (void) strcpy(logRootDirectory, lgs_conf_def.logRootDirectory); + logRootDirectory = lgs_conf_def.logRootDirectory; (void) strcpy(logDataGroupname, lgs_conf_def.logDataGroupname); (void) strcpy(logStreamFileFormat, lgs_conf_def.logStreamFileFormat); logMaxLogrecsize = lgs_conf_def.logMaxLogrecsize; @@ -383,8 +383,7 @@ int lgs_cfg_update(const lgs_config_chg_ /* Update config data */ if (strcmp(name_str, LOG_ROOT_DIRECTORY) == 0) { - (void) snprintf(lgs_conf.logRootDirectory, PATH_MAX, - "%s", value_str); + lgs_conf.logRootDirectory = value_str; lgs_conf.logRootDirectory_cnfflag = LGS_CNF_OBJ; } else if (strcmp(name_str, LOG_DATA_GROUPNAME) == 0) { (void) snprintf(lgs_conf.logDataGroupname, UT_NAMESIZE, @@ -461,17 +460,33 @@ int lgs_cfg_update(const lgs_config_chg_ * @param root_str_in[in] Root path to verify * @return -1 on error */ -int lgs_cfg_verify_root_dir(char *root_str_in) +int lgs_cfg_verify_root_dir(const std::string &root_str_in) { int rc = 0; - size_t n_max = PATH_MAX+1; - size_t n = strnlen(root_str_in, n_max); + log_stream_t *stream = NULL; + size_t n = root_str_in.size(); + if (n > PATH_MAX) { LOG_NO("verify_root_dir Fail. Path > PATH_MAX"); rc = -1; goto done; } + /** + * Make sure that the path /rootPath/streamPath/<fileName><tail> + * must not be larger than PATH_MAX. + */ + stream = log_stream_getnext_by_name(NULL); + while (stream != NULL) { + if (lgs_is_valid_pathlength(stream->pathName, stream->fileName, + root_str_in) == false) { + TRACE("The rootPath is invalid (%s)", root_str_in.c_str()); + rc = -1; + goto done; + } + stream = log_stream_getnext_by_name(stream->name); + } + if (lgs_path_is_writeable_dir_h(root_str_in) == false) { LOG_NO("path_is_writeable_dir... Fail"); rc = -1; @@ -658,11 +673,10 @@ static int lgs_cfg_verify_log_filesys_co * @param root_str_in[in] Root path to verify * @return -1 on error */ -static int verify_root_dir_init(char *root_str_in) +static int verify_root_dir_init(const std::string &root_str_in) { int rc = 0; - size_t n_max = PATH_MAX+1; - size_t n = strnlen(root_str_in, n_max); + size_t n = root_str_in.size(); if (n > PATH_MAX) { LOG_NO("verify_root_dir Fail. Path > PATH_MAX"); rc = -1; @@ -687,7 +701,7 @@ static int verify_all_init() TRACE_ENTER(); if (verify_root_dir_init(lgs_conf.logRootDirectory) == -1) { - strcpy(lgs_conf.logRootDirectory, lgs_conf_def.logRootDirectory); + lgs_conf.logRootDirectory = lgs_conf_def.logRootDirectory; lgs_conf.logRootDirectory_cnfflag = LGS_CNF_DEF; rc = -1; } @@ -813,15 +827,11 @@ static void read_logsv_config_obj_2() { value = attribute->attrValues[0]; if (!strcmp(attribute->attrName, LOG_ROOT_DIRECTORY)) { - n = snprintf(lgs_conf.logRootDirectory, PATH_MAX, "%s", - *((char **) value)); - if (n >= PATH_MAX) { - LOG_WA("LOG root dir read from config object is > PATH_MAX"); - lgs_conf.logRootDirectory[0] = '\0'; - } else { + lgs_conf.logRootDirectory = *(static_cast<char **>(value)); + if (lgs_conf.logRootDirectory.size() > PATH_MAX) { lgs_conf.logRootDirectory_cnfflag = LGS_CNF_OBJ; TRACE("Conf obj; logRootDirectory: %s", - lgs_conf.logRootDirectory); + lgs_conf.logRootDirectory.c_str()); } } else if (!strcmp(attribute->attrName, LOG_DATA_GROUPNAME)) { n = snprintf(lgs_conf.logDataGroupname, UT_NAMESIZE, "%s", @@ -923,12 +933,8 @@ static void read_log_config_environ_var_ if (lgs_conf.logRootDirectory_cnfflag == LGS_CNF_DEF) { /* Has not been set when reading config object */ if ((val_str = getenv("LOGSV_ROOT_DIRECTORY")) != NULL) { - n = snprintf(lgs_conf.logRootDirectory, PATH_MAX, "%s", val_str); - if (n >= PATH_MAX) { - /* Fail */ - LOG_WA("LOG root dir read from config file is > PATH_MAX"); - lgs_conf.logRootDirectory[0] = '\0'; - } else { + lgs_conf.logRootDirectory = val_str; + if (lgs_conf.logRootDirectory.size() > PATH_MAX) { lgs_conf.logRootDirectory_cnfflag = LGS_CNF_ENV; } } else { @@ -936,7 +942,8 @@ static void read_log_config_environ_var_ } } TRACE("logRootDirectory \"%s\", cnfflag '%s'", - lgs_conf.logRootDirectory, cnfflag_str(lgs_conf.logRootDirectory_cnfflag)); + lgs_conf.logRootDirectory.c_str(), + cnfflag_str(lgs_conf.logRootDirectory_cnfflag)); /* logDataGroupname * Rule: Object has precedence @@ -1184,7 +1191,7 @@ const void *lgs_cfg_get(lgs_logconfGet_t switch (param) { case LGS_IMM_LOG_ROOT_DIRECTORY: - value_ptr = lgs_conf.logRootDirectory; + value_ptr = const_cast<char *>(lgs_conf.logRootDirectory.c_str()); break; case LGS_IMM_DATA_GROUPNAME: value_ptr = lgs_conf.logDataGroupname; @@ -1241,7 +1248,7 @@ const void *lgs_cfg_get(lgs_logconfGet_t * return: true = Path is valid * false = Path is invalid */ -bool lgs_path_is_writeable_dir_h(const char *pathname) +bool lgs_path_is_writeable_dir_h(const std::string &pathname) { bool is_writeable_dir = false; @@ -1251,20 +1258,15 @@ bool lgs_path_is_writeable_dir_h(const c TRACE_ENTER(); - TRACE("%s - pathname \"%s\"",__FUNCTION__,pathname); + TRACE("%s - pathname \"%s\"", __FUNCTION__, pathname.c_str()); - size_t params_in_size = strlen(pathname)+1; - if (params_in_size > PATH_MAX) { - is_writeable_dir = false; - LOG_WA("Path > PATH_MAX"); - goto done; - } + size_t params_in_size = pathname.size() + 1; /* Allocate memory for parameter */ params_in_p = malloc(params_in_size); /* Fill in path */ - memcpy(params_in_p, pathname, params_in_size); + memcpy(params_in_p, pathname.c_str(), params_in_size); /* Fill in API structure */ apipar.req_code_in = LGSF_CHECKDIR; @@ -1286,7 +1288,6 @@ bool lgs_path_is_writeable_dir_h(const c free(params_in_p); -done: TRACE_LEAVE2("is_writeable_dir = %d",is_writeable_dir); return is_writeable_dir; } @@ -1302,15 +1303,14 @@ done: * * @param root_path_str */ -void lgs_rootpathconf_set(const char *root_path_str) +void lgs_rootpathconf_set(const std::string &root_path_str) { /* Lock mutex while config data is written */ osaf_mutex_lock_ordie(&lgs_config_data_mutex); - (void) snprintf(lgs_conf.logRootDirectory, PATH_MAX, "%s", - root_path_str); - TRACE("%s logRootDirectory updated to \"%s\"",__FUNCTION__, - lgs_conf.logRootDirectory); + lgs_conf.logRootDirectory = root_path_str; + TRACE("%s logRootDirectory updated to \"%s\"", __FUNCTION__, + lgs_conf.logRootDirectory.c_str()); osaf_mutex_unlock_ordie(&lgs_config_data_mutex); } @@ -1523,7 +1523,7 @@ void lgs_trace_config() TRACE("===== LOG Configuration Start ====="); TRACE("logRootDirectory\t\t \"%s\",\t %s", - lgs_conf.logRootDirectory, + lgs_conf.logRootDirectory.c_str(), cnfflag_str(lgs_conf.logRootDirectory_cnfflag)); TRACE("logDataGroupname\t\t \"%s\",\t %s", lgs_conf.logDataGroupname, diff --git a/osaf/services/saf/logsv/lgs/lgs_config.h b/osaf/services/saf/logsv/lgs/lgs_config.h --- a/osaf/services/saf/logsv/lgs/lgs_config.h +++ b/osaf/services/saf/logsv/lgs/lgs_config.h @@ -20,6 +20,7 @@ #include <stdbool.h> #include <stdint.h> +#include <string> #include "saImmOi.h" #include "saAmf.h" @@ -70,7 +71,7 @@ typedef struct config_chkpt { */ void lgs_cfg_init(SaImmOiHandleT immOiHandle, SaAmfHAStateT ha_state); const void *lgs_cfg_get(lgs_logconfGet_t param); -bool lgs_path_is_writeable_dir_h(const char *pathname); +bool lgs_path_is_writeable_dir_h(const std::string &pathname); void lgs_cfgupd_list_create(const char *name_str, char *value_str, lgs_config_chg_t *config_data); char *lgs_cfgupd_list_read(char **name_str, char **value_str, @@ -81,14 +82,14 @@ int lgs_cfg_update(const lgs_config_chg_ * Functions for updating some parameters. Used to support check-point before * version 5 */ -void lgs_rootpathconf_set(const char *root_path_str); +void lgs_rootpathconf_set(const std::string &root_path_str); void lgs_groupnameconf_set(const char *data_groupname_str); /* * Parameter value validation functions. Validates parameters. See function * headers for validation rules */ -int lgs_cfg_verify_root_dir(char *root_str_in); +int lgs_cfg_verify_root_dir(const std::string &root_str_in); int lgs_cfg_verify_log_data_groupname(char *group_name); int lgs_cfg_verify_log_file_format(const char* log_file_format); int lgs_cfg_verify_max_logrecsize(uint32_t max_logrecsize_in); diff --git a/osaf/services/saf/logsv/lgs/lgs_evt.cc b/osaf/services/saf/logsv/lgs/lgs_evt.cc --- a/osaf/services/saf/logsv/lgs/lgs_evt.cc +++ b/osaf/services/saf/logsv/lgs/lgs_evt.cc @@ -745,9 +745,9 @@ static uint32_t lgs_ckpt_stream_open(lgs ckpt_rec_open_ptr->clientId = open_sync_param->client_id; ckpt_rec_open_ptr->streamId = logStream->streamId; - ckpt_rec_open_ptr->logFile = logStream->fileName; - ckpt_rec_open_ptr->logPath = logStream->pathName; - ckpt_rec_open_ptr->logFileCurrent = logStream->logFileCurrent; + ckpt_rec_open_ptr->logFile = const_cast<char *>(logStream->fileName.c_str()); + ckpt_rec_open_ptr->logPath = const_cast<char *>(logStream->pathName.c_str()); + ckpt_rec_open_ptr->logFileCurrent = const_cast<char *>(logStream->logFileCurrent.c_str()); ckpt_rec_open_ptr->fileFmt = logStream->logFileFormat; ckpt_rec_open_ptr->logStreamName = logStream->name; @@ -781,7 +781,6 @@ static SaAisErrorT create_new_app_stream SaAisErrorT rc = SA_AIS_OK; log_stream_t *stream; SaBoolT twelveHourModeFlag; - uint32_t len = 0; SaUint32T logMaxLogrecsize_conf = 0; TRACE_ENTER(); @@ -827,11 +826,26 @@ static SaAisErrorT create_new_app_stream goto done; } + /* Verify if logFileName length is valid */ + if (lgs_is_valid_filelength(open_sync_param->logFileName) == false) { + TRACE("logFileName is invalid"); + rc = SA_AIS_ERR_INVALID_PARAM; + goto done; + } + + /* Verify if logFilePathName length is valid */ + if (lgs_is_valid_pathlength(open_sync_param->logFilePathName, + open_sync_param->logFileName) == false) { + TRACE("logFilePathName is invalid"); + rc = SA_AIS_ERR_INVALID_PARAM; + goto done; + } + /* Verify that path and file are unique */ stream = log_stream_getnext_by_name(NULL); while (stream != NULL) { - if ((strncmp(stream->fileName, open_sync_param->logFileName, NAME_MAX) == 0) && - (strncmp(stream->pathName, open_sync_param->logFilePathName, SA_MAX_NAME_LENGTH) == 0)) { + if ((stream->fileName == open_sync_param->logFileName) && + (stream->pathName == open_sync_param->logFilePathName)) { TRACE("pathname already exist"); rc = SA_AIS_ERR_INVALID_PARAM; goto done; @@ -856,22 +870,6 @@ static SaAisErrorT create_new_app_stream goto done; } - /* Verify if logFileName length is valid */ - len = strlen(open_sync_param->logFileName); - if ((len == 0) || (len > LOG_NAME_MAX)) { - TRACE("logFileName is invalid"); - rc = SA_AIS_ERR_INVALID_PARAM; - goto done; - } - - /* Verify if logFilePathName length is valid */ - len = strlen(open_sync_param->logFilePathName); - if ((len == 0) || (len > PATH_MAX)) { - TRACE("logFilePathName is invalid"); - rc = SA_AIS_ERR_INVALID_PARAM; - goto done; - } - stream = log_stream_new(&open_sync_param->lstr_name, open_sync_param->logFileName, open_sync_param->logFilePathName, @@ -920,13 +918,13 @@ static SaAisErrorT file_attribute_cmp(lg TRACE("logFileFullAction create params differs, new: %d, old: %d", open_sync_param->logFileFullAction, applicationStream->logFullAction); rs = SA_AIS_ERR_EXIST; - } else if (strcmp(applicationStream->fileName, open_sync_param->logFileName) != 0) { + } else if (applicationStream->fileName != open_sync_param->logFileName) { TRACE("logFileName differs, new: %s existing: %s", - open_sync_param->logFileName, applicationStream->fileName); + open_sync_param->logFileName, applicationStream->fileName.c_str()); rs = SA_AIS_ERR_EXIST; - } else if (strcmp(applicationStream->pathName, open_sync_param->logFilePathName) != 0) { + } else if (applicationStream->pathName != open_sync_param->logFilePathName) { TRACE("log file path differs, new: %s existing: %s", - open_sync_param->logFilePathName, applicationStream->pathName); + open_sync_param->logFilePathName, applicationStream->pathName.c_str()); rs = SA_AIS_ERR_EXIST; } else if ((open_sync_param->logFileFmt != NULL) && strcmp(applicationStream->logFileFormat, open_sync_param->logFileFmt) != 0) { @@ -1047,7 +1045,12 @@ static uint32_t proc_stream_open_msg(lgs if (ais_rv == SA_AIS_OK) { (void)lgs_ckpt_stream_open(cb, logStream, open_sync_param); } + + // These memories are allocated in MDS log open decode callback. free(open_sync_param->logFileFmt); + free(open_sync_param->logFilePathName); + free(open_sync_param->logFileName); + TRACE_LEAVE(); return rc; } @@ -1217,7 +1220,8 @@ static uint32_t proc_write_log_async_msg ckpt_v2.ckpt_rec.write_log.recordId = stream->logRecordId; ckpt_v2.ckpt_rec.write_log.streamId = stream->streamId; ckpt_v2.ckpt_rec.write_log.curFileSize = stream->curFileSize; - ckpt_v2.ckpt_rec.write_log.logFileCurrent = stream->logFileCurrent; + ckpt_v2.ckpt_rec.write_log.logFileCurrent = const_cast<char *>( + stream->logFileCurrent.c_str()); ckpt_v2.ckpt_rec.write_log.logRecord = logOutputString; ckpt_v2.ckpt_rec.write_log.c_file_close_time_stamp = stream->act_last_close_timestamp; ckpt_ptr = &ckpt_v2; @@ -1229,7 +1233,8 @@ static uint32_t proc_write_log_async_msg ckpt_v1.ckpt_rec.write_log.recordId = stream->logRecordId; ckpt_v1.ckpt_rec.write_log.streamId = stream->streamId; ckpt_v1.ckpt_rec.write_log.curFileSize = stream->curFileSize; - ckpt_v1.ckpt_rec.write_log.logFileCurrent = stream->logFileCurrent; + ckpt_v1.ckpt_rec.write_log.logFileCurrent = const_cast<char *>( + stream->logFileCurrent.c_str()); ckpt_ptr = &ckpt_v1; } diff --git a/osaf/services/saf/logsv/lgs/lgs_filehdl.cc b/osaf/services/saf/logsv/lgs/lgs_filehdl.cc --- a/osaf/services/saf/logsv/lgs/lgs_filehdl.cc +++ b/osaf/services/saf/logsv/lgs/lgs_filehdl.cc @@ -338,99 +338,63 @@ int make_log_dir_hdl(void *indata, void int rc = 0; int mldh_rc = 0; mld_in_t *params_in = static_cast<mld_in_t *>(indata); - char *out_path = static_cast<char *>(outdata); - char *relpath = params_in->rel_path; - char *rootpath = params_in->root_dir; - char dir_to_make[PATH_MAX]; - char mpath[PATH_MAX]; - char *spath_p; - char *epath_p; - struct stat statbuf; - int n; + std::string relpath = params_in->rel_path; + std::string rootpath = params_in->root_dir; + std::string dir_to_make; + std::string mpath; + const char *spath_p = NULL; + const char *epath_p = NULL; int path_len = 0; - char *rootpp = NULL; TRACE_ENTER(); - TRACE("rootpath \"%s\"",rootpath); - TRACE("relpath \"%s\"",relpath); + TRACE("rootpath \"%s\"", rootpath.c_str()); + TRACE("relpath \"%s\"", relpath.c_str()); osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK Critical section */ /* * Create root directory if it does not exists. - * TBD. Handle via separate ticket - * (Create the default root path regardless of what is set in the - * configuration object.) - */ - out_path[0] = '\0'; - if (lstat(rootpath, &statbuf) != 0) { -#if 0 - rootpath = PKGLOGDIR; -#endif - n = snprintf(out_path, max_outsize, "%s", rootpath); - if (n < 0 || static_cast<uint32_t>(n) >= max_outsize) { - LOG_WA("Invalid root path > max_outsize"); - mldh_rc = -1; - goto done; - } - LOG_NO("LOG Root path does not exist. Will be created"); - TRACE("%s - create rootpath \"%s\"",__FUNCTION__,rootpath); - } - - /* * Create root path without preceding '/' and ending '/' * Example: ro1/ro2/ro3 - * Check that not > PATH_MAX */ - n = snprintf(mpath, PATH_MAX, "%s", rootpath); - if (n >= PATH_MAX) { - LOG_WA("Could not create path, rootpath > PATH_MAX"); - mldh_rc = -1; - goto done; - } - rootpp = mpath; - while (*rootpp == '/') rootpp++; /* Remove preceding '/' */ - while (mpath[strlen(mpath)-1] == '/') { /* Remove trailing '/' if needed */ - mpath[strlen(mpath)-1] = '\0'; - } + /* Remove preceding '/' */ + rootpath.erase(0, rootpath.find_first_not_of('/')); + /* Remove trailing '/' */ + rootpath.erase(rootpath.find_last_not_of('/') + 1); /* * Add relative path. Shall end with '/' * Example: ro1/ro2/ro3/re1/re2/re3/ - * Check that not > PATH_MAX */ - if (relpath[strlen(relpath)-1] != '/') { - n = snprintf(dir_to_make, PATH_MAX, "/%s/%s/", rootpp, relpath); + if (relpath[relpath.size() - 1] != '/') { + dir_to_make = "/" + rootpath + "/" + relpath + "/"; } else { - n = snprintf(dir_to_make, PATH_MAX, "/%s/%s", rootpp, relpath); + dir_to_make = "/" + rootpath + "/" + relpath; } - if (n >= PATH_MAX) { - LOG_WA("Could not create path > PATH_MAX"); - mldh_rc = -1; - goto done; - } - TRACE("%s - Path to create \"%s\"",__FUNCTION__,dir_to_make); + + TRACE("%s - Path to create \"%s\"", __FUNCTION__, dir_to_make.c_str()); /* Create the path */ - spath_p = epath_p = dir_to_make; + spath_p = epath_p = dir_to_make.c_str(); while ((epath_p = strchr(epath_p, '/')) != NULL) { if (epath_p == spath_p) { epath_p++; continue; /* Don't try to create path "/" */ } + epath_p++; path_len = epath_p - spath_p; - strncpy(mpath, spath_p, path_len); - mpath[path_len] = '\0'; - rc = mkdir(mpath, S_IRWXU | S_IRWXG | S_IRWXO); + + mpath.assign(spath_p, path_len); + rc = mkdir(mpath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); if ((rc != 0) && (errno != EEXIST)) { - LOG_ER("mkdir \"%s\" Fail %s", mpath, strerror(errno)); + LOG_ER("mkdir \"%s\" Fail %s", mpath.c_str(), strerror(errno)); mldh_rc = -1; goto done; } } - TRACE("%s - Dir \"%s\" created",__FUNCTION__, mpath); + TRACE("%s - Dir \"%s\" created", __FUNCTION__, mpath.c_str()); done: osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section */ @@ -606,12 +570,16 @@ static int check_oldest(char *line, char } /* Filter function used by scandir. */ -static char file_prefix[SA_MAX_NAME_LENGTH]; +static std::string file_prefix; + static int filter_func(const struct dirent *finfo) { int ret; int filenameLen = strlen(finfo->d_name) - strlen(".log"); - ret = strncmp(file_prefix, finfo->d_name, strlen(file_prefix)) || strcmp(finfo->d_name + filenameLen, ".log"); + + ret = strncmp(file_prefix.c_str(), finfo->d_name, file_prefix.size()) + || strcmp(finfo->d_name + filenameLen, ".log"); + return !ret; } @@ -627,7 +595,7 @@ int get_number_of_log_files_hdl(void *in { struct dirent **namelist; int n, old_date = -1, old_time = -1, old_ind = -1, files, i, failed = 0; - char path[PATH_MAX]; + std::string path; gnolfh_in_t *params_in; char *oldest_file; int rc = 0; @@ -638,32 +606,20 @@ int get_number_of_log_files_hdl(void *in oldest_file = static_cast<char *>(outdata); /* Initialize the filter */ - n = snprintf(file_prefix, SA_MAX_NAME_LENGTH, "%s", params_in->file_name); - if (n >= SA_MAX_NAME_LENGTH) { - rc = -1; - LOG_WA("file_prefix > SA_MAX_NAME_LENGTH"); - goto done_exit; - } - - n = snprintf(path, PATH_MAX, "%s/%s", - params_in->logsv_root_dir, params_in->pathName); - if (n >= PATH_MAX) { - LOG_WA("path > PATH_MAX"); - rc = -1; - goto done_exit; - } + file_prefix = params_in->file_name; + path = std::string(params_in->logsv_root_dir) + "/" + params_in->pathName; osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK critical section */ - files = n = scandir(path, &namelist, filter_func, alphasort); + files = n = scandir(path.c_str(), &namelist, filter_func, alphasort); + osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section */ - osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section */ if (n == -1 && errno == ENOENT) { rc = 0; goto done_exit; } if (n < 0) { - LOG_WA("scandir:%s - %s", strerror(errno), path); + LOG_WA("scandir:%s - %s", strerror(errno), path.c_str()); rc = -1; goto done_exit; } @@ -685,7 +641,7 @@ int get_number_of_log_files_hdl(void *in if (old_ind != -1) { TRACE_1("oldest: %s", namelist[old_ind]->d_name); n = snprintf(oldest_file, max_outsize, "%s/%s", - path, namelist[old_ind]->d_name); + path.c_str(), namelist[old_ind]->d_name); if (n < 0 || static_cast<uint32_t>(n) >= max_outsize) { LOG_WA("oldest_file > max_outsize"); rc = -1; @@ -718,7 +674,8 @@ done_exit: * * @return int, 0 on success or -1 if error */ -int own_log_files_by_group_hdl(void *indata, void *outdata, size_t max_outsize) { +int own_log_files_by_group_hdl(void *indata, void *outdata, size_t max_outsize) +{ struct dirent **namelist; int n, files, i; olfbgh_t *params_in; @@ -730,16 +687,11 @@ int own_log_files_by_group_hdl(void *ind params_in = static_cast<olfbgh_t *>(indata); /* Set file prefix filter */ - n = snprintf(file_prefix, SA_MAX_NAME_LENGTH, "%s", params_in->file_name); - if (n >= SA_MAX_NAME_LENGTH) { - rc = -1; - LOG_WA("file_prefix > SA_MAX_NAME_LENGTH"); - goto done_exit; - } + file_prefix = params_in->file_name; osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK critical section */ - files = n = scandir(params_in->dir_path, &namelist, filter_func, alphasort); + osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section */ if (n == -1 && errno == ENOENT) { rc = 0; @@ -760,28 +712,22 @@ int own_log_files_by_group_hdl(void *ind gid = lgs_get_data_gid(params_in->groupname); while (n--) { - char file[PATH_MAX]; - int len = snprintf(file, PATH_MAX, "%s/%s", - params_in->dir_path, namelist[n]->d_name); - if (len >= PATH_MAX) { - LOG_WA("path > PATH_MAX"); - rc = -1; - goto done_free; - } - TRACE_3("%s", file); - if (chown(file, (uid_t) -1, gid) != 0) { + std::string file; + file = std::string(params_in->dir_path) + "/" + namelist[n]->d_name; + + TRACE_3("%s", file.c_str()); + + if (chown(file.c_str(), (uid_t) -1, gid) != 0) { LOG_WA("Failed to change the ownership of %s - %s", namelist[n]->d_name, strerror(errno)); } } -done_free: /* Free scandir allocated memory */ for (i = 0; i < files; i++) free(namelist[i]); free(namelist); done_exit: - osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section */ TRACE_LEAVE(); return rc; } diff --git a/osaf/services/saf/logsv/lgs/lgs_filehdl.h b/osaf/services/saf/logsv/lgs/lgs_filehdl.h --- a/osaf/services/saf/logsv/lgs/lgs_filehdl.h +++ b/osaf/services/saf/logsv/lgs/lgs_filehdl.h @@ -42,9 +42,9 @@ * No typedef needed */ typedef struct { - char filepath[PATH_MAX]; /* Complete path to file */ + char *filepath; /* Complete path to file */ char groupname[UT_NAMESIZE]; /* Name of owner group */ -}fopen_in_t; +} fopen_in_t; /* * fileclose_hdl(..) @@ -67,31 +67,24 @@ typedef struct { * Parameter data_out_size must be set to PATH_MAX */ typedef struct { - char root_dir[PATH_MAX]; /* Implementer defined root directory */ - char rel_path[PATH_MAX]; /* Path to create */ -}mld_in_t; + char *root_dir; /* Implementer defined root directory */ + char *rel_path; /* Path to create */ +} mld_in_t; /* * get_number_of_log_files_hdl(..) */ typedef struct { /* File name prefix (name part before time stamps) */ - char file_name[SA_MAX_NAME_LENGTH]; + char *file_name; /* logsv_root_dir + pathName makes up the path to the log files * Note: The whole path cannot be a longer string than PATH_MAX */ - char logsv_root_dir[PATH_MAX]; - char pathName[PATH_MAX]; -}gnolfh_in_t; + char *logsv_root_dir; + char *pathName; +} gnolfh_in_t; - /* parameter [out] - * char str[PATH_MAX] - * No typdef needed - * Out parameter is a string of max PATH_MAX + NAME_MAX length - * Parameter data_out_size must be set to PATH_MAX + NAME_MAX. - */ - -/* +/* * write_log_record_hdl(..) * Outpar int errno_save */ @@ -115,7 +108,7 @@ typedef struct { SaUint32T maxFilesRotated; /* File information */ size_t file_path_size; /* Size of file_path incl. '\0' */ -}ccfh_t; +} ccfh_t; /* char logFileFormat[] * char file_path[] * Strings of varying length that shall be added @@ -126,11 +119,11 @@ typedef struct { * No out parameters */ typedef struct { - char dir_path[PATH_MAX]; /* Full path to stream directory */ + char *dir_path; /* Full path to stream directory */ /* File name prefix (name part before time stamps) */ - char file_name[SA_MAX_NAME_LENGTH]; + char *file_name; char groupname[UT_NAMESIZE]; /* Name of owner group */ -}olfbgh_t; +} olfbgh_t; /* * rename_file_hdl(..) diff --git a/osaf/services/saf/logsv/lgs/lgs_imm.cc b/osaf/services/saf/logsv/lgs/lgs_imm.cc --- a/osaf/services/saf/logsv/lgs/lgs_imm.cc +++ b/osaf/services/saf/logsv/lgs/lgs_imm.cc @@ -235,8 +235,8 @@ static uint32_t ckpt_stream_config(log_s ckpt_v2.header.data_len = 1; ckpt_v2.ckpt_rec.stream_cfg.name = (char *)stream->name; - ckpt_v2.ckpt_rec.stream_cfg.fileName = stream->fileName; - ckpt_v2.ckpt_rec.stream_cfg.pathName = stream->pathName; + ckpt_v2.ckpt_rec.stream_cfg.fileName = const_cast<char *>(stream->fileName.c_str()); + ckpt_v2.ckpt_rec.stream_cfg.pathName = const_cast<char *>(stream->pathName.c_str()); ckpt_v2.ckpt_rec.stream_cfg.maxLogFileSize = stream->maxLogFileSize; ckpt_v2.ckpt_rec.stream_cfg.fixedLogRecordSize = stream->fixedLogRecordSize; ckpt_v2.ckpt_rec.stream_cfg.logFullAction = stream->logFullAction; @@ -244,7 +244,7 @@ static uint32_t ckpt_stream_config(log_s ckpt_v2.ckpt_rec.stream_cfg.maxFilesRotated = stream->maxFilesRotated; ckpt_v2.ckpt_rec.stream_cfg.logFileFormat = stream->logFileFormat; ckpt_v2.ckpt_rec.stream_cfg.severityFilter = stream->severityFilter; - ckpt_v2.ckpt_rec.stream_cfg.logFileCurrent = stream->logFileCurrent; + ckpt_v2.ckpt_rec.stream_cfg.logFileCurrent = const_cast<char *>(stream->logFileCurrent.c_str()); ckpt_v2.ckpt_rec.stream_cfg.c_file_close_time_stamp = stream->act_last_close_timestamp; ckpt_ptr = &ckpt_v2; } else { @@ -254,8 +254,8 @@ static uint32_t ckpt_stream_config(log_s ckpt_v1.header.data_len = 1; ckpt_v1.ckpt_rec.stream_cfg.name = (char *)stream->name; - ckpt_v1.ckpt_rec.stream_cfg.fileName = stream->fileName; - ckpt_v1.ckpt_rec.stream_cfg.pathName = stream->pathName; + ckpt_v1.ckpt_rec.stream_cfg.fileName = const_cast<char *>(stream->fileName.c_str()); + ckpt_v1.ckpt_rec.stream_cfg.pathName = const_cast<char *>(stream->pathName.c_str()); ckpt_v1.ckpt_rec.stream_cfg.maxLogFileSize = stream->maxLogFileSize; ckpt_v1.ckpt_rec.stream_cfg.fixedLogRecordSize = stream->fixedLogRecordSize; ckpt_v1.ckpt_rec.stream_cfg.logFullAction = stream->logFullAction; @@ -263,7 +263,7 @@ static uint32_t ckpt_stream_config(log_s ckpt_v1.ckpt_rec.stream_cfg.maxFilesRotated = stream->maxFilesRotated; ckpt_v1.ckpt_rec.stream_cfg.logFileFormat = stream->logFileFormat; ckpt_v1.ckpt_rec.stream_cfg.severityFilter = stream->severityFilter; - ckpt_v1.ckpt_rec.stream_cfg.logFileCurrent = stream->logFileCurrent; + ckpt_v1.ckpt_rec.stream_cfg.logFileCurrent = const_cast<char *>(stream->logFileCurrent.c_str()); ckpt_ptr = &ckpt_v1; } @@ -735,15 +735,15 @@ static SaAisErrorT config_ccb_completed_ if (!strcmp(attribute->attrName, LOG_ROOT_DIRECTORY)) { if (attribute->attrValuesNumber != 0) { - char *pathName = *((char **)value); - rc = lgs_cfg_verify_root_dir(pathName); - if (rc == -1) { + std::string pathName = *(static_cast<char **>(value)); + if (lgs_cfg_verify_root_dir(pathName) != 0) { report_oi_error(immOiHandle, opdata->ccbId, - "pathName: %s is NOT accepted", pathName); + "pathName: %s is NOT accepted", + pathName.c_str()); ais_rc = SA_AIS_ERR_BAD_OPERATION; goto done; } - TRACE("pathName: %s is accepted", pathName); + TRACE("pathName: %s is accepted", pathName.c_str()); } } else if (!strcmp(attribute->attrName, LOG_DATA_GROUPNAME)) { if (attribute->attrValuesNumber == 0) { @@ -1000,18 +1000,18 @@ static lgs_stream_defval_t *get_SaLogStr * @return true if exists */ bool chk_filepath_stream_exist( - char *fileName, - char *pathName, - log_stream_t *stream, - enum CcbUtilOperationType operationType) + std::string &fileName, + std::string &pathName, + log_stream_t *stream, + enum CcbUtilOperationType operationType) { log_stream_t *i_stream = NULL; - char *i_fileName = NULL; - char *i_pathName = NULL; + std::string i_fileName; + std::string i_pathName; bool rc = false; TRACE_ENTER(); - TRACE("fileName \"%s\", pathName \"%s\"", fileName, pathName); + TRACE("fileName \"%s\", pathName \"%s\"", fileName.c_str(), pathName.c_str()); /* If a stream is modified only the name may be modified. The path name * must be fetched from the stream. @@ -1022,26 +1022,26 @@ bool chk_filepath_stream_exist( /* No stream to modify. Should never happen */ osafassert(0); } - if ((fileName == NULL) && (pathName == NULL)) { + if ((fileName.empty() == true) && (pathName.empty() == true)) { /* Nothing has changed */ TRACE("Nothing has changed"); return false; } - if (fileName == NULL) { + if (fileName.empty() == true) { i_fileName = stream->fileName; - TRACE("From stream: fileName \"%s\"", i_fileName); + TRACE("From stream: fileName \"%s\"", i_fileName.c_str()); } else { i_fileName = fileName; } - if (pathName == NULL) { + if (pathName.empty() == true) { i_pathName = stream->pathName; - TRACE("From stream: pathName \"%s\"", i_pathName); + TRACE("From stream: pathName \"%s\"", i_pathName.c_str()); } else { i_pathName = pathName; } } else if (operationType == CCBUTIL_CREATE) { TRACE("CREATE"); - if ((fileName == NULL) || (pathName == NULL)) { + if ((fileName.empty() == true) || (pathName.empty() == true)) { /* Should never happen * A valid fileName and pathName is always given at create */ LOG_ER("fileName or pathName is not a string"); @@ -1060,8 +1060,8 @@ bool chk_filepath_stream_exist( i_stream = log_stream_getnext_by_name(NULL); while (i_stream != NULL) { TRACE("Check stream \"%s\"", i_stream->name); - if ((strncmp(i_stream->fileName, i_fileName, NAME_MAX) == 0) && - (strncmp(i_stream->pathName, i_pathName, SA_MAX_NAME_LENGTH) == 0)) { + if ((i_stream->fileName == i_fileName) && + (i_stream->pathName == i_pathName)) { rc = true; break; } @@ -1253,10 +1253,10 @@ static SaAisErrorT check_attr_validity(S /* Attribute values to be checked */ /* Mandatory if create. Can be modified */ - char *i_fileName = NULL; + std::string i_fileName; bool i_fileName_mod = false; /* Mandatory if create. Cannot be changed (handled in class definition) */ - char *i_pathName = NULL; + std::string i_pathName; bool i_pathName_mod = false; /* Modification flag -> true if modified */ SaUint32T i_streamFixedLogRecordSize = 0; @@ -1306,7 +1306,7 @@ static SaAisErrorT check_attr_validity(S i_pathName = stream->pathName; i_fileName = stream->fileName; TRACE("From stream: pathName \"%s\", fileName \"%s\"", - i_pathName, i_fileName); + i_pathName.c_str(), i_fileName.c_str()); } } else if (opdata->operationType == CCBUTIL_CREATE){ TRACE("Validate for CREATE"); @@ -1420,7 +1420,7 @@ static SaAisErrorT check_attr_validity(S */ if (i_pathName_mod) { TRACE("Checking saLogStreamPathName"); - if (i_pathName == NULL) { + if (i_pathName.empty() == true) { /* Must point to a string */ rc = SA_AIS_ERR_BAD_OPERATION; report_oi_error(immOiHandle, opdata->ccbId, @@ -1429,7 +1429,7 @@ static SaAisErrorT check_attr_validity(S goto done; } - if (strlen(i_pathName) > PATH_MAX) { + if (lgs_is_valid_pathlength(i_pathName, i_fileName) == false) { /* Path name is too long */ rc = SA_AIS_ERR_BAD_OPERATION; report_oi_error(immOiHandle, opdata->ccbId, @@ -1440,9 +1440,9 @@ static SaAisErrorT check_attr_validity(S if (lgs_relative_path_check_ts(i_pathName) == true) { report_oi_error(immOiHandle, opdata->ccbId, - "Path %s not valid", i_pathName); + "Path %s not valid", i_pathName.c_str()); rc = SA_AIS_ERR_BAD_OPERATION; - TRACE("Path %s not valid", i_pathName); + TRACE("Path %s not valid", i_pathName.c_str()); goto done; } } @@ -1453,7 +1453,7 @@ static SaAisErrorT check_attr_validity(S */ if (i_fileName_mod) { TRACE("Checking saLogStreamFileName"); - if (i_fileName == NULL) { + if (i_fileName.empty() == true) { /* Must point to a string */ rc = SA_AIS_ERR_BAD_OPERATION; report_oi_error(immOiHandle, opdata->ccbId, @@ -1462,20 +1462,22 @@ static SaAisErrorT check_attr_validity(S goto done; } - if (strlen(i_fileName) > LOG_NAME_MAX) { + if ((lgs_is_valid_pathlength(i_pathName, i_fileName) == false) || + (lgs_is_valid_filelength(i_fileName) == false)) { /* File name is too long */ rc = SA_AIS_ERR_BAD_OPERATION; report_oi_error(immOiHandle, opdata->ccbId, "saLogStreamFileName is so long"); - TRACE("saLogStreamFileName is so long (max: %d)", LOG_NAME_MAX); + TRACE("saLogStreamFileName is so long"); goto done; } + /* Not allow special characters exising in file name */ if (lgs_has_special_char(i_fileName) == true) { /* Report failed if has special character in file name */ rc = SA_AIS_ERR_BAD_OPERATION; report_oi_error(immOiHandle, opdata->ccbId, - "Invalid saLogStreamFileName value"); + "Invalid saLogStreamFileName value"); TRACE("\t448 Invalid saLogStreamFileName value"); goto done; } @@ -1483,10 +1485,11 @@ static SaAisErrorT check_attr_validity(S if (chk_filepath_stream_exist(i_fileName, i_pathName, stream, opdata->operationType)) { report_oi_error(immOiHandle, opdata->ccbId, - "Path/file %s/%s already exist", i_pathName, i_fileName); + "Path/file %s/%s already exist", + i_pathName.c_str(), i_fileName.c_str()); rc = SA_AIS_ERR_BAD_OPERATION; TRACE("Path/file %s/%s already exist", - i_pathName, i_fileName); + i_pathName.c_str(), i_fileName.c_str()); goto done; } } @@ -1799,26 +1802,19 @@ static SaAisErrorT ccbCompletedCallback( * @param cur_time_in */ void logRootDirectory_filemove( - const char *new_logRootDirectory, - const char *old_logRootDirectory, - time_t *cur_time_in) + const std::string &new_logRootDirectory, + const std::string &old_logRootDirectory, + time_t *cur_time_in) { TRACE_ENTER(); log_stream_t *stream; - int n = 0; - char *current_logfile; - - /* Shall never happen */ - if (strlen(new_logRootDirectory) + 1 > PATH_MAX) { - LOG_ER("%s Root path > PATH_MAX! Abort", __FUNCTION__); - osafassert(0); - } + std::string current_logfile; /* Close and rename files at current path */ stream = log_stream_getnext_by_name(NULL); while (stream != NULL) { - TRACE("Handling file %s", stream->logFileCurrent); + TRACE("Handling file %s", stream->logFileCurrent.c_str()); if (lgs_cb->ha_state == SA_AMF_HA_ACTIVE) { current_logfile = stream->logFileCurrent; @@ -1826,7 +1822,7 @@ void logRootDirectory_filemove( current_logfile = stream->stb_logFileCurrent; } - TRACE("current_logfile \"%s\"",current_logfile); + TRACE("current_logfile \"%s\"", current_logfile.c_str()); if (log_stream_config_change(!LGS_STREAM_CREATE_FILES, old_logRootDirectory, stream, current_logfile, @@ -1848,12 +1844,9 @@ void logRootDirectory_filemove( /* Create the new log file based on updated configuration */ char *current_time = lgs_get_time(cur_time_in); - n = snprintf(stream->logFileCurrent, NAME_MAX, "%s_%s", stream->fileName, - current_time); - if (n >= NAME_MAX) { - LOG_ER("New log file could not be created for stream: %s", - stream->name); - } else if ((*stream->p_fd = log_file_open(new_logRootDirectory, + stream->logFileCurrent = stream->fileName + "_" + current_time; + + if ((*stream->p_fd = log_file_open(new_logRootDirectory, stream, stream->logFileCurrent, NULL)) == -1) { LOG_ER("New log file could not be created for stream: %s", stream->name); @@ -1862,7 +1855,7 @@ void logRootDirectory_filemove( /* Also update standby current file name * Used if standby and configured for split file system */ - strcpy(stream->stb_logFileCurrent, stream->logFileCurrent); + stream->stb_logFileCurrent = stream->logFileCurrent; stream = log_stream_getnext_by_name(stream->name); } TRACE_LEAVE(); @@ -2104,7 +2097,8 @@ static SaAisErrorT stream_create_and_con *stream = NULL; int i = 0; SaNameT objectName; - int n = 0; + std::string fileName; + std::string pathName; TRACE_ENTER(); @@ -2141,19 +2135,9 @@ static SaAisErrorT stream_create_and_con SaImmAttrValueT value = ccb->param.create.attrValues[i]->attrValues[0]; if (!strcmp(ccb->param.create.attrValues[i]->attrName, "saLogStreamFileName")) { - n = snprintf((*stream)->fileName, NAME_MAX, "%s", *((char **) value)); - if (n >= NAME_MAX) { - LOG_ER("Error: saLogStreamFileName > NAME_MAX"); - osafassert(0); - } - TRACE("fileName: %s", (*stream)->fileName); + fileName = *(static_cast<char **>(value)); } else if (!strcmp(ccb->param.create.attrValues[i]->attrName, "saLogStreamPathName")) { - n = snprintf((*stream)->pathName, PATH_MAX, "%s", *((char **) value)); - if (n >= PATH_MAX) { - LOG_ER("Error: Path name size > PATH_MAX"); - osafassert(0); - } - TRACE("pathName: %s", (*stream)->pathName); + pathName = *(static_cast<char **>(value)); } else if (!strcmp(ccb->param.create.attrValues[i]->attrName, "saLogStreamMaxLogFileSize")) { (*stream)->maxLogFileSize = *((SaUint64T *) value); TRACE("maxLogFileSize: %llu", (*stream)->maxLogFileSize); @@ -2188,6 +2172,18 @@ static SaAisErrorT stream_create_and_con i++; } // while + /* Check if filename & pathName is valid */ + if (lgs_is_valid_filelength(fileName) && lgs_is_valid_pathlength(pathName, fileName)) { + (*stream)->fileName = fileName; + (*stream)->pathName = pathName; + TRACE("fileName: %s", fileName.c_str()); + TRACE("pathName: %s", pathName.c_str()); + } else { + TRACE("Problem with fileName (%s) or pathName (%s)", fileName.c_str(), pathName.c_str()); + rc = SA_AIS_ERR_NO_MEMORY; + goto done; + } + if ((*stream)->logFileFormat == NULL) { /* If passing NULL to log file format, use default value */ const char* logFileFormat = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_STREAM_FILE_FORMAT)); @@ -2226,11 +2222,10 @@ static void stream_ccb_apply_modify(cons const SaImmAttrModificationT_2 *attrMod; int i = 0; log_stream_t *stream; - char current_logfile_name[NAME_MAX]; + std::string current_logfile_name; bool new_cfg_file_needed = false; - int n = 0; struct timespec curtime_tspec; - char *fileName; + std::string fileName; bool modify = false; TRACE_ENTER2("CCB ID %llu, '%s'", opdata->ccbId, opdata->objectName.value); @@ -2238,11 +2233,7 @@ static void stream_ccb_apply_modify(cons stream = log_stream_get_by_name((char*)opdata->objectName.value); osafassert(stream); - n = snprintf(current_logfile_name, NAME_MAX, "%s", stream->logFileCurrent); - if (n >= NAME_MAX) { - LOG_ER("Error: a. File name > NAME_MAX"); - osafassert(0); - } + current_logfile_name = stream->logFileCurrent; attrMod = opdata->param.modify.attrMods[i++]; while (attrMod != NULL) { @@ -2295,7 +2286,7 @@ static void stream_ccb_apply_modify(cons osaf_clock_gettime(CLOCK_REALTIME, &curtime_tspec); time_t cur_time = curtime_tspec.tv_sec; - const char *root_path = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + std::string root_path = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); if (new_cfg_file_needed) { int rc; @@ -2316,17 +2307,14 @@ static void stream_ccb_apply_modify(cons LOG_ER("log_stream_config_change failed: %d", rc); } - n = snprintf(stream->fileName, NAME_MAX, "%s", fileName); - if (n >= NAME_MAX) { - LOG_ER("Error: File name (%zu) > NAME_MAX (%d)", strlen(fileName), (int) NAME_MAX); - osafassert(0); - } + stream->fileName = fileName; + if ((rc = lgs_create_config_file_h(root_path, stream)) != 0) { LOG_ER("lgs_create_config_file_h failed: %d", rc); } char *current_time = lgs_get_time(&cur_time); - sprintf(stream->logFileCurrent, "%s_%s", stream->fileName, current_time); + stream->logFileCurrent = stream->fileName + "_" + current_time; // Create the new log file based on updated configuration *stream->p_fd = log_file_open(root_path, @@ -2579,11 +2567,11 @@ static SaAisErrorT stream_create_and_con value = attribute->attrValues[0]; if (!strcmp(attribute->attrName, "saLogStreamFileName")) { - strcpy(stream->fileName, *((char **)value)); - TRACE("fileName: %s", stream->fileName); + stream->fileName = *(static_cast<char **>(value)); + TRACE("fileName: %s", stream->fileName.c_str()); } else if (!strcmp(attribute->attrName, "saLogStreamPathName")) { - strcpy(stream->pathName, *((char **)value)); - TRACE("pathName: %s", stream->pathName); + stream->pathName = *(static_cast<char **>(value)); + TRACE("pathName: %s", stream->pathName.c_str()); } else if (!strcmp(attribute->attrName, "saLogStreamMaxLogFileSize")) { stream->maxLogFileSize = *((SaUint64T *)value); TRACE("maxLogFileSize: %llu", stream->maxLogFileSize); @@ -2621,6 +2609,14 @@ static SaAisErrorT stream_create_and_con } } + /* Check if the fileName and pathName are valid */ + if ((lgs_is_valid_filelength(stream->fileName) == false) || + (lgs_is_valid_pathlength(stream->pathName, stream->fileName) == false)) { + LOG_ER("Problem with fileName (%s)/pathName (%s)", + stream->fileName.c_str(), stream->pathName.c_str()); + osafassert(0); + } + if (stream->logFileFormat == NULL) { if (stream->streamType == STREAM_TYPE_APPLICATION) { const char* logFileFormat = static_cast<const char *>( diff --git a/osaf/services/saf/logsv/lgs/lgs_mbcsv.cc b/osaf/services/saf/logsv/lgs/lgs_mbcsv.cc --- a/osaf/services/saf/logsv/lgs/lgs_mbcsv.cc +++ b/osaf/services/saf/logsv/lgs/lgs_mbcsv.cc @@ -291,12 +291,12 @@ uint32_t lgs_mbcsv_change_HA_state(lgs_c stream = log_stream_getnext_by_name(NULL); while (stream != NULL) { /* Iterate over all streams */ if (cb->ha_state == SA_AMF_HA_ACTIVE) { - strcpy(stream->logFileCurrent, stream->stb_logFileCurrent); + stream->logFileCurrent = stream->stb_logFileCurrent; stream->curFileSize = stream->stb_curFileSize; *stream->p_fd = -1; /* Reopen files */ } else if (cb->ha_state == SA_AMF_HA_QUIESCED) { - strcpy(stream->stb_logFileCurrent, stream->logFileCurrent); - strcpy(stream->stb_prev_actlogFileCurrent, stream->stb_logFileCurrent); + stream->stb_logFileCurrent = stream->logFileCurrent; + stream->stb_prev_actlogFileCurrent = stream->stb_logFileCurrent; stream->stb_curFileSize = stream->curFileSize; *stream->p_fd = -1; /* Reopen files */ } @@ -593,9 +593,9 @@ uint32_t lgs_ckpt_stream_open_set(log_st memset(stream_open, 0, sizeof(lgs_ckpt_stream_open_t)); stream_open->clientId = -1; /* not used in this message */ stream_open->streamId = logStream->streamId; - stream_open->logFile = logStream->fileName; - stream_open->logPath = logStream->pathName; - stream_open->logFileCurrent = logStream->logFileCurrent; + stream_open->logFile = const_cast<char *>(logStream->fileName.c_str()); + stream_open->logPath = const_cast<char *>(logStream->pathName.c_str()); + stream_open->logFileCurrent = const_cast<char *>(logStream->logFileCurrent.c_str()); stream_open->fileFmt = logStream->logFileFormat; stream_open->logStreamName = logStream->name; stream_open->maxFileSize = logStream->maxLogFileSize; @@ -1724,8 +1724,8 @@ static uint32_t ckpt_proc_log_write(lgs_ stream->logRecordId = recordId; stream->curFileSize = curFileSize; - strcpy(stream->logFileCurrent, logFileCurrent); - + stream->logFileCurrent = logFileCurrent; + /* If configured for split file system log records shall be written also if * we are standby. */ @@ -1912,10 +1912,10 @@ uint32_t ckpt_proc_open_stream(lgs_cb_t stream->numOpeners = param->numOpeners; stream->creationTimeStamp = param->creationTimeStamp; - strcpy(stream->logFileCurrent, param->logFileCurrent); stream->stb_curFileSize = 0; - strcpy(stream->stb_prev_actlogFileCurrent, param->logFileCurrent); - strcpy(stream->stb_logFileCurrent, param->logFileCurrent); + stream->logFileCurrent = param->logFileCurrent; + stream->stb_prev_actlogFileCurrent = param->logFileCurrent; + stream->stb_logFileCurrent = param->logFileCurrent; } /* If configured for split file system files shall be opened on stand by @@ -2112,7 +2112,7 @@ static uint32_t ckpt_proc_cfg_stream(lgs TRACE("config stream %s, id: %u", stream->name, stream->streamId); stream->act_last_close_timestamp = closetime; /* Not used if ver 1 */ - strcpy(stream->fileName, fileName); + stream->fileName = fileName; stream->maxLogFileSize = maxLogFileSize; stream->fixedLogRecordSize = fixedLogRecordSize; stream->logFullAction = logFullAction; @@ -2126,12 +2126,12 @@ static uint32_t ckpt_proc_cfg_stream(lgs strcpy(stream->logFileFormat, logFileFormat); stream->severityFilter = severityFilter; - strcpy(stream->logFileCurrent, logFileCurrent); + stream->logFileCurrent = logFileCurrent; /* If split file mode, update standby files */ if (lgs_is_split_file_system()) { int rc = 0; - const char *root_path = static_cast<const char *>(lgs_cfg_get( + std::string root_path = static_cast<const char *>(lgs_cfg_get( LGS_IMM_LOG_ROOT_DIRECTORY)); if ((rc = log_stream_config_change(LGS_STREAM_CREATE_FILES, root_path, stream, @@ -2142,8 +2142,8 @@ static uint32_t ckpt_proc_cfg_stream(lgs /* When modifying old files are closed and new are opened meaning that * we have a new standby current log-file */ - strcpy(stream->stb_prev_actlogFileCurrent, stream->logFileCurrent); - strcpy(stream->stb_logFileCurrent, stream->logFileCurrent); + stream->stb_prev_actlogFileCurrent = stream->logFileCurrent; + stream->stb_logFileCurrent = stream->logFileCurrent; } done: diff --git a/osaf/services/saf/logsv/lgs/lgs_mbcsv_v2.cc b/osaf/services/saf/logsv/lgs/lgs_mbcsv_v2.cc --- a/osaf/services/saf/logsv/lgs/lgs_mbcsv_v2.cc +++ b/osaf/services/saf/logsv/lgs/lgs_mbcsv_v2.cc @@ -57,8 +57,9 @@ uint32_t ckpt_proc_lgs_cfg_v2(lgs_cb_t * /* Handle log files for new directory if configured for split file system */ if (lgs_is_split_file_system()) { - const char *old_root = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); - const char *new_root = param->logRootDirectory; + const std::string old_root = static_cast<const char *>( + lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + const std::string new_root = param->logRootDirectory; /* Change root path in configuration struct */ lgs_rootpathconf_set(param->logRootDirectory); logRootDirectory_filemove(new_root, old_root, diff --git a/osaf/services/saf/logsv/lgs/lgs_mds.cc b/osaf/services/saf/logsv/lgs/lgs_mds.cc --- a/osaf/services/saf/logsv/lgs/lgs_mds.cc +++ b/osaf/services/saf/logsv/lgs/lgs_mds.cc @@ -130,6 +130,12 @@ static uint32_t dec_lstr_open_sync_msg(N lgsv_stream_open_req_t *param = &msg->info.api_info.param.lstr_open_sync; uint8_t local_data[256]; + TRACE_ENTER(); + // To make it safe when using free() + param->logFileName = NULL; + param->logFilePathName = NULL; + param->logFileFmt = NULL; + /* client_id */ p8 = ncs_dec_flatten_space(uba, local_data, 4); param->client_id = ncs_decode_32bit(&p8); @@ -154,26 +160,48 @@ static uint32_t dec_lstr_open_sync_msg(N len = ncs_decode_16bit(&p8); ncs_dec_skip_space(uba, 2); - if ((len == 0) || (len > NAME_MAX)) { - TRACE("%s - logFileName length is invalid (%d)",__FUNCTION__, len); - rc = NCSCC_RC_FAILURE; - goto done; + TRACE("enter dealing with logfilename"); + /** + * Leave the upper limit check to lgs_evt. + * Then, if the file length is invalid, LOG agent will get INVALID_PARAM + * instead of error code RC_FAILURE returned by MDS layer. + */ + if (len > 0) { + /** + * This allocated memory is freed in proc_stream_open_msg() @ lgs_evt.c + */ + param->logFileName = static_cast<char *>(malloc(len)); + if (param->logFileName == NULL) { + LOG_ER("%s - Failed to allocate memory for logFileName", __FUNCTION__); + rc = NCSCC_RC_FAILURE; + goto done_err; + } + ncs_decode_n_octets_from_uba(uba, (uint8_t *)param->logFileName, len); } - ncs_decode_n_octets_from_uba(uba, (uint8_t *)param->logFileName, len); - /* log file path name */ p8 = ncs_dec_flatten_space(uba, local_data, 2); len = ncs_decode_16bit(&p8); ncs_dec_skip_space(uba, 2); - if ((len == 0) || (len > PATH_MAX)) { - TRACE("%s - logFilePathName length is invalid (%d)",__FUNCTION__, len); + if (len > PATH_MAX) { + TRACE("%s - logFilePathName length is invalid (%d)", __FUNCTION__, len); rc = NCSCC_RC_FAILURE; - goto done; + goto done_err; } - ncs_decode_n_octets_from_uba(uba, (uint8_t *)param->logFilePathName, len); + if (len > 0) { + /** + * This allocated memory is freed in proc_stream_open_msg() @ lgs_evt.c + */ + param->logFilePathName = static_cast<char *>(malloc(len)); + if (param->logFilePathName == NULL) { + LOG_ER("%s - Failed to allocate memory for logFilePathName", __FUNCTION__); + rc = NCSCC_RC_FAILURE; + goto done_err; + } + ncs_decode_n_octets_from_uba(uba, (uint8_t *)param->logFilePathName, len); + } /* log record format length */ p8 = ncs_dec_flatten_space(uba, local_data, 24); @@ -191,7 +219,7 @@ static uint32_t dec_lstr_open_sync_msg(N if (param->logFileFmt == NULL) { LOG_WA("malloc FAILED"); rc = NCSCC_RC_FAILURE; - goto done; + goto done_err; } ncs_decode_n_octets_from_uba(uba, (uint8_t *)param->logFileFmt, len); } @@ -201,7 +229,15 @@ static uint32_t dec_lstr_open_sync_msg(N param->lstr_open_flags = ncs_decode_8bit(&p8); ncs_dec_skip_space(uba, 1); - done: + // Everything is ok. Do not free the allocated memories. + goto done; + +done_err: + free(param->logFileName); + free(param->logFilePathName); + free(param->logFileFmt); + +done: TRACE_8("LGSV_STREAM_OPEN_REQ"); return rc; } diff --git a/osaf/services/saf/logsv/lgs/lgs_stream.cc b/osaf/services/saf/logsv/lgs/lgs_stream.cc --- a/osaf/services/saf/logsv/lgs/lgs_stream.cc +++ b/osaf/services/saf/logsv/lgs/lgs_stream.cc @@ -58,31 +58,27 @@ static int get_number_of_log_files_h(log * @param errno_save[out], errno if error * @return File descriptor or -1 if error */ -static int fileopen_h(char *filepath, int *errno_save) +static int fileopen_h(const std::string &filepath, int *errno_save) { lgsf_apipar_t apipar; lgsf_retcode_t api_rc; int fd; fopen_in_t data_in; const char *groupname = NULL; - + std::string filePath; + TRACE_ENTER(); - - osafassert(filepath != NULL); - - if ((strlen(filepath)+1) > PATH_MAX) { - LOG_WA("Cannot open file, File path > PATH_MAX"); - fd = -1; - goto done; - } + + osafassert(filepath.empty() != true); groupname = static_cast<const char *>(lgs_cfg_get(LGS_IMM_DATA_GROUPNAME)); - strcpy(data_in.filepath, filepath); + filePath = filepath; + data_in.filepath = const_cast<char *>(filePath.c_str()); strcpy(data_in.groupname, groupname); - - TRACE("%s - filepath \"%s\"",__FUNCTION__,filepath); - + + TRACE("%s - filepath \"%s\"",__FUNCTION__, filepath.c_str()); + /* Fill in API structure */ apipar.req_code_in = LGSF_FILEOPEN; apipar.data_in_size = sizeof(fopen_in_t); @@ -97,8 +93,7 @@ static int fileopen_h(char *filepath, in } else { fd = apipar.hdl_ret_code_out; } - -done: + TRACE_LEAVE(); return fd; } @@ -155,7 +150,7 @@ static int fileclose_h(int fd, int *errn * be deleted * @return (-1) if error */ -static int file_unlink_h(char *filepath) +static int file_unlink_h(const std::string &filepath) { int rc = 0; lgsf_apipar_t apipar; @@ -163,17 +158,13 @@ static int file_unlink_h(char *filepath) size_t filepath_len; TRACE_ENTER(); - filepath_len = strlen(filepath)+1; /* Include terminating null character */ - if (filepath_len > PATH_MAX) { - LOG_WA("Cannot delete file, File path > PATH_MAX"); - rc = -1; - goto done; - } - + + filepath_len = filepath.size() + 1; /* Include terminating null character */ + /* Fill in API structure */ apipar.req_code_in = LGSF_DELETE_FILE; apipar.data_in_size = filepath_len; - apipar.data_in = filepath; + apipar.data_in = const_cast<char *>(filepath.c_str()); apipar.data_out_size = 0; apipar.data_out = NULL; @@ -185,7 +176,6 @@ static int file_unlink_h(char *filepath) rc = apipar.hdl_ret_code_out; } -done: TRACE_LEAVE2("rc = %d", rc); return rc; @@ -200,26 +190,19 @@ done: static int delete_config_file(log_stream_t *stream) { int rc = 0; - int n; - char pathname[PATH_MAX]; + std::string pathname; TRACE_ENTER(); /* create absolute path for config file */ - const char *logsv_root_dir = static_cast<const char *>( + std::string logsv_root_dir = static_cast<const char *>( lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); - n = snprintf(pathname, PATH_MAX, "%s/%s/%s.cfg", - logsv_root_dir, stream->pathName, stream->fileName); - if (n >= PATH_MAX) { - LOG_WA("Config file could not be deleted, path > PATH_MAX"); - rc = -1; - goto done; - } + pathname = logsv_root_dir + "/" + stream->pathName + + "/" + stream->fileName + ".cfg"; rc = file_unlink_h(pathname); - -done: + TRACE_LEAVE2("rc = %d", rc); return rc; } @@ -315,8 +298,9 @@ static uint32_t log_stream_remove(const void log_initiate_stream_files(log_stream_t *stream) { int errno_save; - const char *log_root_path = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); - + const std::string log_root_path = static_cast<const char *>( + lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + TRACE_ENTER(); /* Initiate standby stream file parameters. Only needed if we are standby @@ -324,8 +308,7 @@ void log_initiate_stream_files(log_strea */ if ((lgs_cb->ha_state == SA_AMF_HA_STANDBY) && lgs_is_split_file_system()) { stream->stb_curFileSize = 0; - strncpy(stream->stb_logFileCurrent, stream->logFileCurrent, NAME_MAX); - stream->stb_logFileCurrent[NAME_MAX-1] = 0; + stream->stb_logFileCurrent = stream->logFileCurrent; } /* Initiate stream file descriptor to flag that no valid descriptor exist */ @@ -353,8 +336,8 @@ void log_initiate_stream_files(log_strea if ((*stream->p_fd = log_file_open(log_root_path, stream, stream->logFileCurrent, &errno_save)) == -1) { - TRACE("%s - Could not open '%s' - %s",__FUNCTION__, - stream->logFileCurrent, strerror(errno_save)); + TRACE("%s - Could not open '%s' - %s", __FUNCTION__, + stream->logFileCurrent.c_str(), strerror(errno_save)); goto done; } @@ -391,8 +374,8 @@ void log_stream_print(log_stream_t *stre osafassert(stream != NULL); TRACE_2("******** Stream %s ********", stream->name); - TRACE_2(" fileName: %s", stream->fileName); - TRACE_2(" pathName: %s", stream->pathName); + TRACE_2(" fileName: %s", stream->fileName.c_str()); + TRACE_2(" pathName: %s", stream->pathName.c_str()); TRACE_2(" maxLogFileSize: %llu", stream->maxLogFileSize); TRACE_2(" fixedLogRecordSize: %u", stream->fixedLogRecordSize); if (stream->streamType == STREAM_TYPE_APPLICATION) @@ -406,7 +389,7 @@ void log_stream_print(log_stream_t *stre TRACE_2(" numOpeners: %u", stream->numOpeners); TRACE_2(" streamId: %u", stream->streamId); TRACE_2(" fd: %d", *stream->p_fd); - TRACE_2(" logFileCurrent: %s", stream->logFileCurrent); + TRACE_2(" logFileCurrent: %s", stream->logFileCurrent.c_str()); TRACE_2(" curFileSize: %u", stream->curFileSize); TRACE_2(" logRecordId: %u", stream->logRecordId); TRACE_2(" streamType: %u", stream->streamType); @@ -447,7 +430,7 @@ void log_stream_delete(log_stream_t **s) if (stream->logFileFormat != NULL) free(stream->logFileFormat); - free(stream); + delete stream; *s = NULL; TRACE_LEAVE(); } @@ -489,8 +472,8 @@ static void init_log_stream_fd(log_strea * @return log_stream_t* */ log_stream_t *log_stream_new(SaNameT *dn, - const char *filename, - const char *pathname, + const std::string &filename, + const std::string &pathname, SaUint64T maxLogFileSize, SaUint32T fixedLogRecordSize, SaLogFileFullActionT logFullAction, @@ -509,15 +492,16 @@ log_stream_t *log_stream_new(SaNameT *dn if (lgs_relative_path_check_ts(pathname)) { goto done; } - stream = static_cast<log_stream_t *>(calloc(1, sizeof(log_stream_t))); + + stream = new (std::nothrow) log_stream_t(); if (stream == NULL) { LOG_WA("log_stream_new calloc FAILED"); goto done; } memcpy(stream->name, dn->value, dn->length); stream->name[SA_MAX_NAME_LENGTH] = '\0'; - strncpy(stream->fileName, filename, sizeof(stream->fileName)); - strncpy(stream->pathName, pathname, sizeof(stream->pathName)); + stream->fileName = filename; + stream->pathName = pathname; stream->maxLogFileSize = maxLogFileSize; stream->fixedLogRecordSize = fixedLogRecordSize; stream->haProperty = SA_TRUE; @@ -582,7 +566,7 @@ log_stream_t *log_stream_new(SaNameT *dn .attrValuesNumber = 1, .attrValues = arr1 }; - char *str2 = stream->fileName; + char *str2 = const_cast<char *>(stream->fileName.c_str()); void *arr2[] = { &str2 }; const SaImmAttrValuesT_2 attr_safLogStreamFileName = { .attrName = const_cast<SaImmAttrNameT>("saLogStreamFileName"), @@ -590,7 +574,7 @@ log_stream_t *log_stream_new(SaNameT *dn .attrValuesNumber = 1, .attrValues = arr2 }; - char *str3 = stream->pathName; + char *str3 = const_cast<char *>(stream->pathName.c_str()); void *arr3[] = { &str3 }; const SaImmAttrValuesT_2 attr_safLogStreamPathName = { .attrName = const_cast<SaImmAttrNameT>("saLogStreamPathName"), @@ -707,7 +691,7 @@ log_stream_t *log_stream_new_2(SaNameT * osafassert(name != NULL); TRACE_ENTER2("%s, l: %u", name->value, (unsigned int)name->length); - stream = static_cast<log_stream_t *>(calloc(1, sizeof(log_stream_t))); + stream = new (std::nothrow) (log_stream_t)(); if (stream == NULL) { LOG_WA("calloc FAILED"); goto done; @@ -760,33 +744,26 @@ log_stream_t *log_stream_new_2(SaNameT * * @return int - the file descriptor or -1 on errors */ int log_file_open( - const char *root_path, - log_stream_t *stream, - const char* filename, - int *errno_save) + const std::string &root_path, + log_stream_t *stream, + const std::string &filename, + int *errno_save) { int fd; - char pathname[PATH_MAX]; + std::string pathname; int errno_ret; - int n; TRACE_ENTER(); - n = snprintf(pathname, PATH_MAX, "%s/%s/%s.log", - root_path, stream->pathName, filename); - if (n >= PATH_MAX) { - LOG_WA("Cannot open log file, path > PATH_MAX"); - fd = -1; - goto done; - } - - TRACE("%s - Opening file \"%s\"",__FUNCTION__,pathname); + pathname = root_path + "/" + stream->pathName + "/" + filename + ".log"; + + TRACE("%s - Opening file \"%s\"", __FUNCTION__, pathname.c_str()); + fd = fileopen_h(pathname, &errno_ret); if (errno_save != 0) { *errno_save = errno_ret; } -done: TRACE_LEAVE(); return fd; } @@ -814,7 +791,7 @@ void log_stream_open_fileinit(log_stream /* first time open? */ if (stream->numOpeners == 0) { /* Create and save current log file name */ - snprintf(stream->logFileCurrent, NAME_MAX, "%s_%s", stream->fileName, lgs_get_time(NULL)); + stream->logFileCurrent = stream->fileName + "_" + lgs_get_time(NULL); log_initiate_stream_files(stream); } @@ -844,14 +821,16 @@ void log_stream_close(log_stream_t **s, int rc = 0; int errno_ret; log_stream_t *stream = *s; - char *file_to_rename = NULL; + std::string file_to_rename; char *timeString = NULL; + std::string emptyStr = ""; uint32_t msecs_waited = 0; const unsigned int max_waiting_time = 8 * 1000; /* 8 secs */ const unsigned int sleep_delay_ms = 500; SaUint32T trace_num_openers; struct timespec closetime_tspec; - const char *root_path = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + const std::string root_path = static_cast<const char *>( + lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); osafassert(stream != NULL); TRACE_ENTER2("%s", stream->name); @@ -899,14 +878,15 @@ void log_stream_close(log_stream_t **s, /* Rename stream log file */ rc = lgs_file_rename_h(root_path, stream->pathName, file_to_rename, timeString, - LGS_LOG_FILE_EXT, NULL); + LGS_LOG_FILE_EXT, emptyStr); while ((rc == -1) && (msecs_waited < max_waiting_time)) { usleep(sleep_delay_ms * 1000); msecs_waited += sleep_delay_ms; rc = lgs_file_rename_h(root_path, stream->pathName, file_to_rename, timeString, - LGS_LOG_FILE_EXT, NULL); + LGS_LOG_FILE_EXT, emptyStr); } + if (rc == -1) { LOG_ER("Could not rename log file: %s", strerror(errno)); goto done_files; @@ -915,13 +895,13 @@ void log_stream_close(log_stream_t **s, /* Rename stream config file */ rc = lgs_file_rename_h(root_path, stream->pathName, stream->fileName, timeString, - LGS_LOG_FILE_CONFIG_EXT, NULL); + LGS_LOG_FILE_CONFIG_EXT, emptyStr); while ((rc == -1) && (msecs_waited < max_waiting_time)) { usleep(sleep_delay_ms * 1000); msecs_waited += sleep_delay_ms; rc = lgs_file_rename_h(root_path, stream->pathName, stream->fileName, timeString, - LGS_LOG_FILE_CONFIG_EXT, NULL); + LGS_LOG_FILE_CONFIG_EXT, emptyStr); } if (rc == -1) { LOG_ER("Could not rename config file: %s", strerror(errno)); @@ -977,42 +957,17 @@ static int get_number_of_log_files_h(log lgsf_apipar_t apipar; lgsf_retcode_t api_rc; gnolfh_in_t parameters_in; - size_t path_len; - int rc, n; - + int rc = 0; + TRACE_ENTER(); - const char *logsv_root_dir = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + std::string logsv_root_dir = static_cast<const char *>( + lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); - n = snprintf(parameters_in.file_name, SA_MAX_NAME_LENGTH, "%s", logStream->fileName); - if (n >= SA_MAX_NAME_LENGTH) { - rc = -1; - LOG_WA("file_name > SA_MAX_NAME_LENGTH"); - goto done; - } + parameters_in.file_name = const_cast<char *>(logStream->fileName.c_str()); + parameters_in.logsv_root_dir = const_cast<char *>(logsv_root_dir.c_str()); + parameters_in.pathName = const_cast<char *>(logStream->pathName.c_str()); - n = snprintf(parameters_in.logsv_root_dir, PATH_MAX, "%s", logsv_root_dir); - if (n >= PATH_MAX) { - rc = -1; - LOG_WA("logsv_root_dir > PATH_MAX"); - goto done; - } - n = snprintf(parameters_in.pathName, PATH_MAX, "%s", logStream->pathName); - if (n >= PATH_MAX) { - rc = -1; - LOG_WA("pathName > PATH_MAX"); - goto done; - } - - path_len = strlen(parameters_in.file_name) + - strlen(parameters_in.logsv_root_dir) + - strlen(parameters_in.pathName); - if (path_len > PATH_MAX) { - LOG_WA("Path to log files > PATH_MAX"); - rc = -1; - goto done; - } - /* Fill in API structure */ apipar.req_code_in = LGSF_GET_NUM_LOGFILES; apipar.data_in_size = sizeof(gnolfh_in_t); @@ -1028,7 +983,6 @@ static int get_number_of_log_files_h(log rc = apipar.hdl_ret_code_out; } -done: TRACE_LEAVE(); return rc; } @@ -1047,10 +1001,10 @@ static int log_rotation_stb(log_stream_t int errno_save; int errno_ret; char *current_time_str; - char new_current_log_filename[NAME_MAX]; + std::string new_current_log_filename; bool do_rotate = false; - const char *root_path = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); - + std::string root_path = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + TRACE_ENTER(); stream->stb_curFileSize += count; @@ -1069,17 +1023,17 @@ static int log_rotation_stb(log_stream_t * This means that a rotation on active when standby is running is not * guaranteed. This situation though is very unlikely. */ - if (strcmp(stream->logFileCurrent, stream->stb_prev_actlogFileCurrent) != 0) { + if (stream->logFileCurrent != stream->stb_prev_actlogFileCurrent) { TRACE("Active has rotated (follow active)"); /* Active has rotated */ - strcpy(stream->stb_prev_actlogFileCurrent, stream->logFileCurrent); + stream->stb_prev_actlogFileCurrent = stream->logFileCurrent; /* Use close time from active for renaming of closed file */ current_time_str = lgs_get_time(&stream->act_last_close_timestamp); do_rotate = true; /* Use same name as active for new current log file */ - snprintf(new_current_log_filename, NAME_MAX, "%s", stream->logFileCurrent); + new_current_log_filename = stream->logFileCurrent; } else if (stream->stb_curFileSize > stream->maxLogFileSize) { TRACE("Standby has rotated (filesize)"); /* Standby current log file has reached max limit */ @@ -1087,8 +1041,7 @@ static int log_rotation_stb(log_stream_t current_time_str = lgs_get_time(NULL); do_rotate = true; /* Create new name for current log file based on local time */ - snprintf(new_current_log_filename, NAME_MAX, "%s_%s", - stream->fileName, current_time_str); + new_current_log_filename = stream->fileName + "_" + current_time_str; } if (do_rotate) { @@ -1103,11 +1056,15 @@ static int log_rotation_stb(log_stream_t LOG_NO("close FAILED: %s", strerror(errno_ret)); goto done; } - + + std::string emptyStr = ""; /* Rename file to give it the "close timestamp" */ rc = lgs_file_rename_h(root_path, - stream->pathName, stream->stb_logFileCurrent, - current_time_str, LGS_LOG_FILE_EXT, NULL); + stream->pathName, + stream->stb_logFileCurrent, + current_time_str, + LGS_LOG_FILE_EXT, + emptyStr); if (rc == -1) goto done; @@ -1117,11 +1074,12 @@ static int log_rotation_stb(log_stream_t } /* Save new name for current log file and open it */ - snprintf(stream->stb_logFileCurrent, NAME_MAX, "%s", new_current_log_filename); + stream->stb_logFileCurrent = new_current_log_filename; if ((*stream->p_fd = log_file_open(root_path, stream, - stream->stb_logFileCurrent, &errno_save)) == -1) { - LOG_IN("Could not open '%s' - %s", stream->stb_logFileCurrent, - strerror(errno_save)); + stream->stb_logFileCurrent, + &errno_save)) == -1) { + LOG_IN("Could not open '%s' - %s", stream->stb_logFileCurrent.c_str(), + strerror(errno_save)); rc = -1; goto done; } @@ -1147,8 +1105,10 @@ static int log_rotation_act(log_stream_t int errno_save; int errno_ret; struct timespec closetime_tspec; - const char *root_path = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); - + std::string root_path = static_cast<const char *>( + lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + std::string emptyStr = ""; + /* If file size > max file size: * - Close the log file and create a new. * - If number of files > max number of files delete the oldest @@ -1170,9 +1130,12 @@ static int log_rotation_act(log_stream_t } /* Rename file to give it the "close timestamp" */ - rc = lgs_file_rename_h(root_path, stream->pathName, - stream->logFileCurrent, current_time, - LGS_LOG_FILE_EXT, NULL); + rc = lgs_file_rename_h(root_path, + stream->pathName, + stream->logFileCurrent, + current_time, + LGS_LOG_FILE_EXT, + emptyStr); if (rc == -1) goto done; @@ -1191,12 +1154,13 @@ static int log_rotation_act(log_stream_t } /* Create a new file name that includes "open time stamp" and open the file */ - snprintf(stream->logFileCurrent, NAME_MAX, "%s_%s", stream->fileName, - current_time); - if ((*stream->p_fd = log_file_open(root_path, stream, - stream->logFileCurrent, &errno_save)) == -1) { - LOG_IN("Could not open '%s' - %s", stream->logFileCurrent, - strerror(errno_save)); + stream->logFileCurrent = stream->fileName + "_" + current_time; + if ((*stream->p_fd = log_file_open(root_path, + stream, + stream->logFileCurrent, + &errno_save)) == -1) { + LOG_IN("Could not open '%s' - %s", stream->logFileCurrent.c_str(), + strerror(errno_save)); rc = -1; goto done; } @@ -1291,9 +1255,9 @@ int log_stream_write_h(log_stream_t *str */ /* Careful with log level here to avoid syslog flooding */ if (rc == -2) { - LOG_IN("write '%s' failed \"Timeout\"", stream->logFileCurrent); + LOG_IN("write '%s' failed \"Timeout\"", stream->logFileCurrent.c_str()); } else { - LOG_IN("write '%s' failed \"%s\"", stream->logFileCurrent, + LOG_IN("write '%s' failed \"%s\"", stream->logFileCurrent.c_str(), strerror(write_errno)); } @@ -1485,14 +1449,15 @@ uint32_t log_stream_init() * @return -1 on error */ int log_stream_config_change(bool create_files_f, - const char *root_path, + const std::string &root_path, log_stream_t *stream, - const char *current_logfile_name, + const std::string ¤t_logfile_name, time_t *cur_time_in) { int rc; int errno_ret; char *current_time = lgs_get_time(cur_time_in); + std::string emptyStr = ""; TRACE_ENTER2("%s", stream->name); @@ -1513,13 +1478,13 @@ int log_stream_config_change(bool create *stream->p_fd = -1; rc = lgs_file_rename_h(root_path, stream->pathName, current_logfile_name, - current_time, LGS_LOG_FILE_EXT, NULL); + current_time, LGS_LOG_FILE_EXT, emptyStr); if (rc == -1) { goto done; } rc = lgs_file_rename_h(root_path, stream->pathName, stream->fileName, - current_time, LGS_LOG_FILE_CONFIG_EXT, NULL); + current_time, LGS_LOG_FILE_CONFIG_EXT, emptyStr); if (rc == -1) { goto done; } @@ -1530,11 +1495,13 @@ int log_stream_config_change(bool create if ((rc = lgs_create_config_file_h(root_path, stream)) != 0) goto done; - sprintf(stream->logFileCurrent, "%s_%s", stream->fileName, current_time); + stream->logFileCurrent = stream->fileName + "_" + current_time; /* Create the new log file based on updated configuration */ - *stream->p_fd = log_file_open(root_path, stream, - stream->logFileCurrent,NULL); + *stream->p_fd = log_file_open(root_path, + stream, + stream->logFileCurrent, + NULL); } /* Fix bug - this function makes return (-1) when create_files_f = false */ diff --git a/osaf/services/saf/logsv/lgs/lgs_stream.h b/osaf/services/saf/logsv/lgs/lgs_stream.h --- a/osaf/services/saf/logsv/lgs/lgs_stream.h +++ b/osaf/services/saf/logsv/lgs/lgs_stream.h @@ -18,9 +18,11 @@ #ifndef __LGS_STREAM_H #define __LGS_STREAM_H +#include <string> #include <ncspatricia.h> #include <time.h> #include <limits.h> + #include "lgs_fmt.h" /** @@ -33,8 +35,8 @@ typedef struct log_stream { /* --- Corresponds to IMM Class SaLogStream/SaLogStreamConfig --- */ char name[SA_MAX_NAME_LENGTH + 1]; /* add for null termination */ - char fileName[NAME_MAX]; - char pathName[PATH_MAX]; + std::string fileName; + std::string pathName; SaUint64T maxLogFileSize; SaUint32T fixedLogRecordSize; SaBoolT haProperty; /* app log stream only */ @@ -52,7 +54,7 @@ typedef struct log_stream { int32_t fd_shared; /* Checkpointed stream file descriptor for shared fs */ int32_t fd_local; /* Local stream file descriptor for split fs */ int32_t *p_fd; /* Points to shared or local fd depending on fs config */ - char logFileCurrent[NAME_MAX]; /* Current file name */ + std::string logFileCurrent; /* Current file name */ uint32_t curFileSize; /* Bytes written to current log file */ uint32_t logRecordId; /* log record indentifier increased for each record */ SaBoolT twelveHourModeFlag; /* Not used. Can be removed? */ @@ -68,8 +70,8 @@ typedef struct log_stream { /* Not checkpointed parameters. Used by standby in split file mode */ uint32_t stb_logRecordId; /* Last written Id. For checking Id inconsistency */ - char stb_logFileCurrent[NAME_MAX]; /* Current file name used on standby */ - char stb_prev_actlogFileCurrent[NAME_MAX]; /* current file name on active when previous record was written */ + std::string stb_logFileCurrent; /* Current file name used on standby */ + std::string stb_prev_actlogFileCurrent; /* current file name on active when previous record was written */ uint32_t stb_curFileSize; /* Bytes written to current log file */ } log_stream_t; @@ -78,8 +80,8 @@ extern void log_stream_delete(log_stream #define STREAM_NEW -1 extern log_stream_t *log_stream_new(SaNameT *name, - const char *filename, - const char *pathname, + const std::string &filename, + const std::string &pathname, SaUint64T maxLogFileSize, SaUint32T fixedLogRecordSize, SaLogFileFullActionT logFullAction, @@ -102,12 +104,14 @@ extern void log_stream_id_print(); #define LGS_STREAM_CREATE_FILES true int log_stream_config_change(bool create_files_f, - const char *root_path, - log_stream_t *stream, - const char *current_logfile_name, - time_t *cur_time_in); -extern int log_file_open(const char *root_path, log_stream_t *stream, - const char* filename, int *errno_save); + const std::string &root_path, + log_stream_t *stream, + const std::string ¤t_logfile_name, + time_t *cur_time_in); +extern int log_file_open(const std::string &root_path, + log_stream_t *stream, + const std::string &filename, + int *errno_save); /* Accessor functions */ extern log_stream_t *log_stream_get_by_name(const char *name); diff --git a/osaf/services/saf/logsv/lgs/lgs_util.cc b/osaf/services/saf/logsv/lgs/lgs_util.cc --- a/osaf/services/saf/logsv/lgs/lgs_util.cc +++ b/osaf/services/saf/logsv/lgs/lgs_util.cc @@ -43,6 +43,9 @@ #define LGS_CREATE_CLOSE_TIME_LEN 16 #define START_YEAR 1900 +// The length of '_yyyymmdd_hhmmss_yyyymmdd_hhmmss.log' including null-termination char +#define LOG_TAIL_MAX (std::string("_yyyymmdd_hhmmss_yyyymmdd_hhmmss.log").size()) + /** * Create config file according to spec. * @param root_path[in] @@ -50,70 +53,55 @@ * * @return -1 on error */ -int lgs_create_config_file_h(const char *root_path, log_stream_t *stream) +int lgs_create_config_file_h(const std::string &root_path, log_stream_t *stream) { lgsf_apipar_t apipar; lgsf_retcode_t api_rc; - void *params_in; + void *params_in = NULL; ccfh_t *header_in_p; size_t params_in_size; char *logFileFormat_p; char *pathname_p; int rc; - int path_len; uint32_t n = 0; - char pathname[PATH_MAX]; + std::string pathname; size_t logFileFormat_size = 0; size_t pathname_size = 0; + TRACE_ENTER(); - TRACE_ENTER(); - /* check the existence of logsv_root_dir/pathName, * check that the path is safe. * If ok, create the path if it doesn't already exits */ - path_len = snprintf(pathname, PATH_MAX, "%s/%s", - root_path, stream->pathName); - if (path_len >= PATH_MAX) { - LOG_WA("logsv_root_dir + pathName > PATH_MAX"); - rc = -1; - goto done; - } - + pathname = root_path + "/" + stream->pathName; if (lgs_relative_path_check_ts(pathname) == true) { - LOG_WA("Directory path \"%s\" not allowed", pathname); + LOG_WA("Directory path \"%s\" not allowed", pathname.c_str()); rc = -1; goto done; } if (lgs_make_reldir_h(stream->pathName) != 0) { LOG_WA("Create directory '%s/%s' failed", - root_path, stream->pathName); + root_path.c_str(), stream->pathName.c_str()); rc = -1; goto done; } /* create absolute path for config file */ - n = snprintf(pathname, PATH_MAX, "%s/%s/%s.cfg", - root_path, stream->pathName, stream->fileName); - - if (n >= PATH_MAX) { - LOG_WA("Complete pathname > PATH_MAX"); - rc = -1; - goto done; - } - TRACE("%s - Config file path \"%s\"",__FUNCTION__, pathname); + pathname += "/" + stream->fileName + ".cfg"; - /* + TRACE("%s - Config file path \"%s\"", __FUNCTION__, pathname.c_str()); + + /* * Create the configuration file. * Open the file, write it's content and close the file */ /* Allocate memory for parameters */ params_in_size = sizeof(ccfh_t) + (strlen(stream->logFileFormat) + 1) + - (strlen(pathname) + 1); + (pathname.size() + 1); params_in = malloc(params_in_size); /* Set pointers to allocated memory */ @@ -138,13 +126,9 @@ int lgs_create_config_file_h(const char LOG_WA("Log file format string too long"); goto done_free; } - n = snprintf(pathname_p, pathname_size, "%s", pathname); - if (n >= pathname_size) { - rc = -1; - LOG_WA("Path name string too long"); - goto done_free; - } - + + (void)snprintf(pathname_p, pathname_size, "%s", pathname.c_str()); + /* Fill in API structure */ apipar.req_code_in = LGSF_CREATECFGFILE; apipar.data_in_size = params_in_size; @@ -251,20 +235,19 @@ SaTimeT lgs_get_SaTime() * @return -1 if error */ int lgs_file_rename_h( - const char *root_path, - const char *rel_path, - const char *old_name, - const char *time_stamp, - const char *suffix, - char *new_name) + const std::string &root_path, + const std::string &rel_path, + const std::string &old_name, + const std::string &time_stamp, + const std::string &suffix, + std::string &new_name) { int rc; - char oldpath[PATH_MAX]; - char newpath[PATH_MAX]; - char new_name_loc[NAME_MAX]; - size_t n; + std::string oldpath; + std::string newpath; + std::string new_name_loc; lgsf_apipar_t apipar; - void *params_in_p; + void *params_in_p = NULL; size_t params_in_size; lgsf_retcode_t api_rc; char *oldpath_in_p; @@ -275,37 +258,26 @@ int lgs_file_rename_h( TRACE_ENTER(); - n = snprintf(oldpath, PATH_MAX, "%s/%s/%s%s", - root_path, rel_path, old_name, suffix); - if (n >= PATH_MAX) { - LOG_ER("Cannot rename file, old path > PATH_MAX"); - rc = -1; - goto done; + oldpath = root_path + "/" + rel_path + "/" + old_name + suffix; + + if (time_stamp.empty() == false) { + new_name_loc = old_name + "_" + time_stamp + suffix; + } else { + new_name_loc = new_name + suffix; } - if (time_stamp != NULL) { - snprintf(new_name_loc, NAME_MAX, "%s_%s%s", old_name, time_stamp, suffix); - } else { - snprintf(new_name_loc, NAME_MAX, "%s%s", new_name, suffix); + newpath = root_path + "/" + rel_path + "/" + new_name_loc; + + if (new_name.empty() == false) { + new_name = new_name_loc; } - n = snprintf(newpath, PATH_MAX, "%s/%s/%s", - root_path, rel_path, new_name_loc); - if (n >= PATH_MAX) { - LOG_ER("Cannot rename file, new path > PATH_MAX"); - rc = -1; - goto done; - } - - if (new_name != NULL) { - strcpy(new_name, new_name_loc); - } - - TRACE_4("Rename file from %s", oldpath); - TRACE_4(" to %s", newpath); - + + TRACE_4("Rename file from %s", oldpath.c_str()); + TRACE_4(" to %s", newpath.c_str()); + /* Allocate memory for parameters */ - oldpath_size = strlen(oldpath) + 1; - newpath_size = strlen(newpath) + 1; + oldpath_size = oldpath.size() + 1; + newpath_size = newpath.size() + 1; params_in_size = sizeof(size_t) + oldpath_size + newpath_size; params_in_p = malloc(params_in_size); @@ -317,9 +289,9 @@ int lgs_file_rename_h( /* Fill in parameters */ *oldpath_str_size_p = oldpath_size; - memcpy(oldpath_in_p, oldpath, oldpath_size); - memcpy(newpath_in_p, newpath, newpath_size); - + memcpy(oldpath_in_p, oldpath.c_str(), oldpath_size); + memcpy(newpath_in_p, newpath.c_str(), newpath_size); + /* Fill in API structure */ apipar.req_code_in = LGSF_RENAME_FILE; apipar.data_in_size = params_in_size; @@ -337,7 +309,6 @@ int lgs_file_rename_h( free(params_in_p); -done: TRACE_LEAVE(); return rc; } @@ -448,12 +419,13 @@ void lgs_free_write_log(const lgsv_write * @param path * @return true if path is not allowed */ -bool lgs_relative_path_check_ts(const char* path) +bool lgs_relative_path_check_ts(const std::string &path) { bool rc = false; - int len_path = strlen(path); + int len_path = path.size(); if (len_path >= 3) { - if (strstr(path, "/../") != NULL || strstr(path, "/..") != NULL) { + if (path.find("/../") != std::string::npos || + path.find("/..") != std::string::npos) { /* /..dir/ is not allowed, however /dir../ is allowed. */ rc = true; } else if (path[0] == '.' && path[1] == '.' && path[2] == '/') { @@ -476,49 +448,30 @@ bool lgs_relative_path_check_ts(const ch * @param path, Path relative log service root directory * @return -1 on error */ -int lgs_make_reldir_h(const char* path) +int lgs_make_reldir_h(const std::string &path) { lgsf_apipar_t apipar; lgsf_retcode_t api_rc; int rc = -1; mld_in_t params_in; - char new_rootstr[PATH_MAX]; - size_t n1, n2; - - new_rootstr[0] = '\0'; /* Initiate to empty string */ - + TRACE_ENTER(); - const char *logsv_root_dir = static_cast<const char *>( + const std::string logsv_root_dir = static_cast<const char *>( lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); - - TRACE("logsv_root_dir \"%s\"",logsv_root_dir); - TRACE("path \"%s\"",path); - - n1 = snprintf(params_in.root_dir, PATH_MAX, "%s", logsv_root_dir); - if (n1 >= PATH_MAX) { - LOG_WA("logsv_root_dir > PATH_MAX"); - goto done; - } - n2 = snprintf(params_in.rel_path, PATH_MAX, "%s", path); - if (n2 >= PATH_MAX) { - LOG_WA("path > PATH_MAX"); - goto done; - } - - /* Check that the complete path is not longer than PATH_MAX */ - if ((strlen(params_in.root_dir) + strlen(params_in.rel_path)) >= PATH_MAX) { - LOG_WA("root_dir + rel_path > PATH_MAX"); - goto done; - } - + + TRACE("logsv_root_dir \"%s\"", logsv_root_dir.c_str()); + TRACE("path \"%s\"", path.c_str()); + + params_in.root_dir = const_cast<char *>(logsv_root_dir.c_str()); + params_in.rel_path = const_cast<char *>(path.c_str()); + /* Fill in API structure */ apipar.req_code_in = LGSF_MAKELOGDIR; apipar.data_in_size = sizeof(mld_in_t); apipar.data_in = ¶ms_in; - apipar.data_out_size = PATH_MAX; - apipar.data_out = &new_rootstr; - + apipar.data_out_size = 0; + apipar.data_out = NULL; api_rc = log_file_api(&apipar); if (api_rc != LGSF_SUCESS) { TRACE("%s - API error %s",__FUNCTION__,lgsf_retcode_str(api_rc)); @@ -526,16 +479,7 @@ int lgs_make_reldir_h(const char* path) } else { rc = apipar.hdl_ret_code_out; } - - /* Handle a possible change of root dir to default */ - if (new_rootstr[0] != '\0') { - //lgs_imm_rootpathconf_set(new_rootstr); - /* LLDTEST1XXX Replace with new handling - */ - TRACE("%s - new_rootstr \"%s\"",__FUNCTION__,new_rootstr); - } -done: TRACE_LEAVE2("rc = %d",rc); return rc; @@ -615,36 +559,19 @@ int lgs_own_log_files_h(log_stream_t *st { lgsf_apipar_t apipar; lgsf_retcode_t api_rc; + int rc = 0, n; + std::string dir_path; olfbgh_t *data_in = static_cast<olfbgh_t *>(malloc(sizeof(olfbgh_t))); - int rc = 0, n, path_len; TRACE_ENTER2("stream %s",stream->name); /* Set in parameter dir_path */ - const char *logsv_root_dir = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + const std::string logsv_root_dir = static_cast<const char *>(lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); /* Set in parameter file_name */ - n = snprintf(data_in->file_name, SA_MAX_NAME_LENGTH, "%s", stream->fileName); - if (n >= SA_MAX_NAME_LENGTH) { - rc = -1; - LOG_WA("file_name > SA_MAX_NAME_LENGTH"); - goto done; - } - - n = snprintf(data_in->dir_path, PATH_MAX, "%s/%s", - logsv_root_dir, stream->pathName); - if (n >= PATH_MAX) { - rc = -1; - LOG_WA("dir_path > PATH_MAX"); - goto done; - } - - path_len = strlen(data_in->file_name) + strlen(data_in->dir_path); - if (path_len > PATH_MAX) { - LOG_WA("Path to log files > PATH_MAX"); - rc = -1; - goto done; - } + data_in->file_name = const_cast<char *>(stream->fileName.c_str()); + dir_path = logsv_root_dir + "/" + stream->pathName; + data_in->dir_path = const_cast<char *>(dir_path.c_str()); /* Set in parameter groupname */ n = snprintf(data_in->groupname, UT_NAMESIZE, "%s", groupname); @@ -681,7 +608,7 @@ done: * @param: str [in] input string for checking * @return: true if there is special character, otherwise false. */ -bool lgs_has_special_char(const char *str) +bool lgs_has_special_char(const std::string &str) { int index = 0; char c; @@ -734,3 +661,102 @@ bool lgs_has_special_char(const char *st } return false; } + + +/** + * Get the maximum length of filename + * + * @param:none + * @return: the maximum length of filename + */ +static size_t lgs_pathconf() +{ + static size_t name_max = 0; + static bool invoked = false; + static std::string old_path; + + std::string dir_path = static_cast<const char *>( + lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + + /* To avoid invoking pathconf() unncessarily */ + if ((invoked == true) && (dir_path == old_path)) { + return name_max; + } + + errno = 0; + long max = pathconf(dir_path.c_str(), _PC_NAME_MAX); + + /* Get default value if the limit is not defined, or error */ + if (max == -1) { + max = 255; + invoked = false; + LOG_WA("pathconf error: %s", strerror(errno)); + } + + /* Backup the values to avoid calling pathconf() */ + name_max = max; + old_path = dir_path; + + invoked = true; + return name_max; +} + +/** + * Get the maximum length of a <filename> which is inputed from users + * exlcuded tail part. + * + * @param: none + * @return: the maximum length of <filename> + */ +size_t lgs_max_nlength() +{ + return (lgs_pathconf() - LOG_TAIL_MAX); +} + +/** + * Check if file length is valid. + * + * logsv will append the tail to fileName such as file extention, open time. + * and <fileName> is input from user (immcfg, imm.xml, log Open API), + * so once passing the checking, logsv does not need to be worry + * about the file name is over the limitation. + + * @param: fileName - file name for checking (excluse the tail part) + * @return: true if file is valid, false otherwise. + * + */ +bool lgs_is_valid_filelength(const std::string &fileName) +{ + size_t filelen = fileName.size() + 1; + size_t maxsize = lgs_pathconf(); + + return ((filelen + LOG_TAIL_MAX) < maxsize); +} + +/** + * Check if the length of the path to file is valid + * Path to file format: rootDir/pathToDir/<fileName><tail> + * + * @param: + * - path: the path to log file of the stream (excluse root dir). + * - fileName: the <fileName> of the log stream (excluse the tail part) + * @return: true if the path is valid, false otherwise. + */ +bool lgs_is_valid_pathlength(const std::string &path, + const std::string &fileName, + const std::string &rootPath) +{ + std::string rootpath; + size_t pathlen = path.size(); + size_t filelen = fileName.size(); + + if (rootPath.empty() == true) { + rootpath = static_cast<const char *>( + lgs_cfg_get(LGS_IMM_LOG_ROOT_DIRECTORY)); + } else { + rootpath = rootPath; + } + size_t rootlen = rootpath.size() + 1; + + return ((rootlen + pathlen + filelen + LOG_TAIL_MAX) < PATH_MAX); +} diff --git a/osaf/services/saf/logsv/lgs/lgs_util.h b/osaf/services/saf/logsv/lgs/lgs_util.h --- a/osaf/services/saf/logsv/lgs/lgs_util.h +++ b/osaf/services/saf/logsv/lgs/lgs_util.h @@ -51,23 +51,30 @@ */ extern char *lgs_get_time(time_t *time_in); -extern int lgs_create_config_file_h(const char *root_path, log_stream_t *stream); +extern int lgs_create_config_file_h(const std::string &root_path, log_stream_t *stream); extern void lgs_evt_destroy(lgsv_lgs_evt_t *evt); extern SaTimeT lgs_get_SaTime(); extern int lgs_file_rename_h( - const char *root_path, - const char *rel_path, - const char *old_name, - const char *time_stamp, - const char *suffix, - char *new_name); + const std::string &root_path, + const std::string &rel_path, + const std::string &old_name, + const std::string &time_stamp, + const std::string &suffix, + std::string &new_name); //extern uint32_t lgs_create_known_streams(lgs_cb_t *lgs_cb); /* Not used, no code */ extern void lgs_exit(const char *msg, SaAmfRecommendedRecoveryT rec_rcvr); -extern bool lgs_relative_path_check_ts(const char* path); -extern int lgs_make_reldir_h(const char* path); -extern int lgs_check_path_exists_h(const char *path_to_check); +extern bool lgs_relative_path_check_ts(const std::string &path); +extern int lgs_make_reldir_h(const std::string &path); +extern int lgs_check_path_exists_h(const std::string &path_to_check); extern gid_t lgs_get_data_gid(char *groupname); extern int lgs_own_log_files_h(log_stream_t *stream, const char *groupname); -extern bool lgs_has_special_char(const char *str); +extern bool lgs_has_special_char(const std::string &str); + +// Functions to check if the filename/path length is valid +size_t lgs_max_nlength(); +bool lgs_is_valid_filelength(const std::string &fileName); +bool lgs_is_valid_pathlength(const std::string &path, + const std::string &fileName, + const std::string &rootPath = ""); #endif /* ifndef __LGS_UTIL_H */ ------------------------------------------------------------------------------ Site24x7 APM Insight: Get Deep Visibility into Application Performance APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month Monitor end-to-end web transactions and take corrective actions now Troubleshoot faster and improve end-user experience. Signup Now! http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140 _______________________________________________ Opensaf-devel mailing list Opensaf-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensaf-devel