39 #include "safec_lib.h"
62 if (NULL == (fp = fopen(
"/proc/loadavg",
"r"))) {
63 LOG(
"Error in opening /proc/loadavg file");
66 if (fread(str, 1, LEN, fp) != LEN) {
67 LOG(
"Error in reading loadavg");
87 fseek(fp, 0L, SEEK_END);
91 if(fseek(fp, prev, SEEK_SET) != 0){
92 LOG(
"Cannot set the file position indicator for the stream pointed to by stream\n");
104 free(PERSISTENT_PATH);
119 char buf[EC_BUF_LEN] = {0};
122 ec_fp = fopen(EXEC_COUNTER_FILENAME,
"r");
128 rval = fgets(buf, EC_BUF_LEN, ec_fp);
135 CUR_EXEC_COUNT = atoi(buf);
137 if (CUR_EXEC_COUNT < 0)
143 void saveExecCounter()
146 ec_fp = fopen(EXEC_COUNTER_FILENAME,
"w");
151 fprintf(ec_fp,
"%d" , CUR_EXEC_COUNT);
166 if (skipInterval == 0 || CUR_EXEC_COUNT == 0) {
170 if (CUR_EXEC_COUNT < skipInterval) {
173 if ((CUR_EXEC_COUNT % skipInterval) == 0) {
192 static FILE *LOG_FP = NULL;
193 static int is_rotated_log = 0;
197 char * rotatedLog = NULL;
198 size_t curLog_len = 0;
199 size_t rotatedLog_len = 0;
200 char *fileExtn =
".1";
202 if ((NULL == PERSISTENT_PATH) || (NULL == LOG_PATH) || (NULL == name)) {
203 LOG(
"Path variables are empty");
207 curLog_len = strlen(LOG_PATH) + strlen(name) + 1;
208 rotatedLog_len = strlen(LOG_PATH) + strlen(name) + strlen(fileExtn) + 1;
209 curLog = malloc(curLog_len);
210 rotatedLog = malloc(rotatedLog_len);
211 if((curLog == NULL) || (rotatedLog == NULL))
219 rc = sprintf_s(curLog,curLog_len,
"%s%s", LOG_PATH,name);
228 if(CUR_EXEC_COUNT == 0){
229 rc = sprintf_s(rotatedLog,rotatedLog_len,
"%s%s%s", LOG_PATH,name,fileExtn);
235 LOG_FP = fopen(rotatedLog,
"rb");
237 LOG(
"Error in opening %s\n",rotatedLog);
238 LOG_FP = fopen(curLog,
"rb");
247 LOG_FP = fopen(curLog,
"rb");
258 LOG_FP = fopen(curLog,
"rb");
266 fileSize =
fsize(LOG_FP);
268 if (seek_value <= fileSize)
270 if(fseek(LOG_FP, seek_value, 0) != 0){
271 LOG(
"Cannot set the file position indicator for the stream pointed to by stream\n");
279 if(NULL != DEVICE_TYPE)
281 rc = strcmp_s(
"broadband", strlen(
"broadband"), DEVICE_TYPE , &ind);
283 if((!ind) && (rc == EOK))
291 LOG(
"Telemetry file pointer corrupted");
292 if(fseek(LOG_FP, 0, 0) != 0){
293 LOG(
"Cannot set the file position indicator for the stream pointed to by stream\n");
298 if (NULL != rotatedLog) {
299 rc = sprintf_s(rotatedLog,rotatedLog_len,
"%s%s%s", LOG_PATH,name,fileExtn);
311 LOG_FP = fopen(rotatedLog,
"rb");
315 LOG(
"Error in opening file %s", rotatedLog);
321 if(fseek(LOG_FP, seek_value, 0) != 0){
322 LOG(
"Cannot set the file position indicator for the stream pointed to by stream\n");
336 rval = fgets(buf, buflen, LOG_FP);
339 long seek_value = ftell(LOG_FP);
340 LAST_SEEK_VALUE = seek_value;
345 if (is_rotated_log == 1)
350 rc = sprintf_s(curLog,curLog_len,
"%s%s", LOG_PATH,name);
357 LOG_FP = fopen(curLog,
"rb");
361 LOG(
"Error in opening file %s", curLog);
367 rval = fgets(buf, buflen, LOG_FP);
370 seek_value = ftell(LOG_FP);
371 LAST_SEEK_VALUE = seek_value;
400 int p_path_len = -1, log_path_len = -1;
401 FILE *file = fopen( INCLUDE_PROPERTIES,
"r");
404 char props[255] = {
""};
405 p_path_len = strlen(
"PERSISTENT_PATH=");
406 log_path_len = strlen(
"LOG_PATH=");
407 while(fscanf(file,
"%255s", props) != EOF )
409 char *
property = NULL;
410 if ((property = strstr( props,
"PERSISTENT_PATH="))) {
411 property =
property + p_path_len;
412 PERSISTENT_PATH = malloc(strlen(property) + 1);
413 if (NULL != PERSISTENT_PATH) {
414 rc = strcpy_s(PERSISTENT_PATH,strlen(property) + 1, property);
418 free(PERSISTENT_PATH);
419 PERSISTENT_PATH = NULL;
424 }
else if ((property = strstr( props,
"LOG_PATH="))) {
425 if ( 0 == strncmp(props,
"LOG_PATH=", log_path_len) ) {
426 property =
property + log_path_len;
427 LOG_PATH = malloc(strlen(property) + 1);
428 if (NULL != LOG_PATH) {
429 rc = strcpy_s(LOG_PATH,strlen(property) + 1, property);
445 if ((NULL != logpath) && (logpath[0] !=
'\0'))
448 tmp = realloc(LOG_PATH, strlen(logpath) + 1 );
451 rc = strcpy_s(LOG_PATH,strlen(logpath) + 1, logpath);
464 if ((NULL != perspath) && (perspath[0] !=
'\0'))
467 tmp = realloc(PERSISTENT_PATH,strlen(perspath) + 1 );
469 PERSISTENT_PATH = tmp;
470 rc = strcpy_s(PERSISTENT_PATH,strlen(perspath) + 1, perspath);
474 free(PERSISTENT_PATH);
475 PERSISTENT_PATH = NULL;
479 free(PERSISTENT_PATH);
480 PERSISTENT_PATH = NULL;
496 int device_type_len = strlen(
"DEVICE_TYPE=");
499 file = fopen( DEVICE_PROPERTIES,
"r");
502 char props[255] = {
""};
503 while(fscanf(file,
"%255s", props) != EOF )
505 char *
property = NULL;
507 if((property = strstr( props,
"DEVICE_TYPE=")))
509 property =
property + device_type_len;
510 length = strlen(property) + 1;
511 DEVICE_TYPE = malloc(length);
512 if (NULL != DEVICE_TYPE) {
513 rc = strcpy_s(DEVICE_TYPE,length, property);
531 if (NULL != DEVICE_TYPE && NULL != PERSISTENT_PATH && NULL != LOG_PATH) {
532 rc = strcmp_s(
"broadband", strlen(
"broadband"), DEVICE_TYPE , &ind);
534 if((!ind) && (rc == EOK)) {
535 char *tmp_seek_file =
"/.telemetry/tmp/rtl_";
536 char *tmp_log_file =
"/";
539 if (NULL == perspath || perspath[0] ==
'\0') {
540 length = strlen(PERSISTENT_PATH) + strlen(tmp_seek_file) + 1 ;
541 tmp = realloc(PERSISTENT_PATH,length );
543 PERSISTENT_PATH = tmp;
544 rc = strcat_s(PERSISTENT_PATH,length, tmp_seek_file);
548 free(PERSISTENT_PATH);
549 PERSISTENT_PATH = NULL;
553 free(PERSISTENT_PATH);
554 PERSISTENT_PATH = NULL;
558 if (NULL == logpath || logpath[0] ==
'\0') {
559 length = strlen(LOG_PATH) + strlen(tmp_log_file) + 1 ;
560 tmp = realloc(LOG_PATH, length );
563 rc = strcat_s(LOG_PATH,length, tmp_log_file);
578 char *tmp_seek_file = DEFAULT_SEEK_PREFIX;
579 char *tmp_log_file = DEFAULT_LOG_PATH;
584 if (NULL == perspath || perspath[0] ==
'\0') {
585 length = strlen(tmp_seek_file) + 1;
586 tmp = realloc(PERSISTENT_PATH, length );
588 PERSISTENT_PATH = tmp;
589 rc = strcpy_s(PERSISTENT_PATH,length, tmp_seek_file);
593 free(PERSISTENT_PATH);
594 PERSISTENT_PATH = NULL;
598 free(PERSISTENT_PATH);
599 PERSISTENT_PATH = NULL;
603 if (NULL == logpath || logpath[0] ==
'\0') {
604 length = strlen(tmp_log_file) + 1 ;
605 tmp = realloc(LOG_PATH, length );
608 rc = strcpy_s(LOG_PATH,length, tmp_log_file);
640 if (NULL != name && NULL != PERSISTENT_PATH) {
641 char *seekfile = NULL;
642 length = strlen(PERSISTENT_PATH) + strlen(name) + 1;
643 seekfile = malloc(length);
644 if (NULL != seekfile) {
646 ret = sprintf_s(seekfile,length,
"%s%s", PERSISTENT_PATH,name);
654 if (NULL != (fp = fopen(seekfile,
"r"))) {
656 if( fscanf(fp,
"%ld", seek_value) != 1) {
657 LOG(
"Error in fscanf()\n");
682 if (NULL != name && NULL != PERSISTENT_PATH) {
683 char *seekfile = NULL;
684 length = strlen(PERSISTENT_PATH) + strlen(name) + 1;
685 seekfile = malloc(length);
686 if (NULL != seekfile) {
688 rc = sprintf_s(seekfile,length,
"%s%s", PERSISTENT_PATH,name);
695 if (NULL != (fp = fopen(seekfile,
"w"))) {
696 fprintf(fp,
"%ld", seek_value);