@@ -71,7 +71,7 @@ static void md_sqlite_copy_db(struct md_writer_sqlite *mws, uint8_t from_timeout
7171 (mws -> session_id_file && !mws -> session_id ))
7272 {
7373 META_PRINT_SYSLOG (mws -> parent , LOG_ERR , "Can't export DB. # node_id %d "
74- "# valid_timestamp %u # session_id_file %s # session_id %d \n" ,
74+ "# valid_timestamp %u # session_id_file %s # session_id %lu \n" ,
7575 mws -> node_id ,
7676 mws -> valid_timestamp ,
7777 mws -> session_id_file ? mws -> session_id_file : "EMPTY" ,
@@ -163,7 +163,7 @@ static uint8_t md_sqlite_update_nodeid_db(struct md_writer_sqlite *mws, const ch
163163}
164164
165165static uint8_t md_sqlite_update_timestamp_db (struct md_writer_sqlite * mws ,
166- const char * sql_str , uint64_t tstamp_offset )
166+ const char * sql_str , uint64_t orig_boot_time , uint64_t real_boot_time )
167167{
168168 int32_t retval ;
169169 sqlite3_stmt * update_timestamp ;
@@ -174,12 +174,17 @@ static uint8_t md_sqlite_update_timestamp_db(struct md_writer_sqlite *mws,
174174 return RETVAL_FAILURE ;
175175 }
176176
177- if ((retval = sqlite3_bind_int64 (update_timestamp , 1 , tstamp_offset ))) {
177+ if ((retval = sqlite3_bind_int64 (update_timestamp , 1 , orig_boot_time ))) {
178178 META_PRINT_SYSLOG (mws -> parent , LOG_ERR , "Bind failed %s\n" , sqlite3_errstr (retval ));
179179 return RETVAL_FAILURE ;
180180 }
181181
182- if ((retval = sqlite3_bind_int64 (update_timestamp , 2 , FIRST_VALID_TIMESTAMP ))) {
182+ if ((retval = sqlite3_bind_int64 (update_timestamp , 2 , real_boot_time ))) {
183+ META_PRINT_SYSLOG (mws -> parent , LOG_ERR , "Bind failed %s\n" , sqlite3_errstr (retval ));
184+ return RETVAL_FAILURE ;
185+ }
186+
187+ if ((retval = sqlite3_bind_int64 (update_timestamp , 3 , real_boot_time ))) {
183188 META_PRINT_SYSLOG (mws -> parent , LOG_ERR , "Bind failed %s\n" , sqlite3_errstr (retval ));
184189 return RETVAL_FAILURE ;
185190 }
@@ -291,11 +296,32 @@ static sqlite3* md_sqlite_configure_db(struct md_writer_sqlite *mws, const char
291296 return db_handle ;
292297}
293298
299+ static int md_sqlite_read_boot_time (struct md_writer_sqlite * mws , uint64_t * boot_time )
300+ {
301+ struct timeval tv ;
302+ uint64_t uptime ;
303+
304+ //read uptime
305+ if (system_helpers_read_uint64_from_file ("/proc/uptime" , & uptime )) {
306+ return RETVAL_FAILURE ;
307+ }
308+
309+ gettimeofday (& tv , NULL );
310+
311+ * boot_time = tv .tv_sec - uptime ;
312+
313+ if (* boot_time < mws -> orig_boot_time ) {
314+ return RETVAL_FAILURE ;
315+ }
316+
317+ return RETVAL_SUCCESS ;
318+ }
319+
294320static int md_sqlite_configure (struct md_writer_sqlite * mws ,
295321 const char * db_filename , uint32_t node_id , uint32_t db_interval ,
296322 uint32_t db_events , const char * meta_prefix , const char * gps_prefix ,
297323 const char * monitor_prefix , const char * usage_prefix ,
298- const char * system_prefix )
324+ const char * system_prefix , const char * ntp_fix_file )
299325{
300326 sqlite3 * db_handle = md_sqlite_configure_db (mws , db_filename );
301327 const char * dump_events , * dump_updates , * dump_gps , * dump_monitor ,
@@ -439,6 +465,11 @@ static int md_sqlite_configure(struct md_writer_sqlite *mws,
439465 mws -> system_prefix_len = strlen (system_prefix );
440466 }
441467
468+ if (ntp_fix_file ) {
469+ memset (mws -> ntp_fix_file , 0 , sizeof (mws -> ntp_fix_file ));
470+ memcpy (mws -> ntp_fix_file , ntp_fix_file , strlen (ntp_fix_file ));
471+ }
472+
442473 if (mws -> node_id && (md_sqlite_update_nodeid_db (mws , UPDATE_EVENT_ID ) ||
443474 md_sqlite_update_nodeid_db (mws , UPDATE_UPDATES_ID ) ||
444475 md_sqlite_update_nodeid_db (mws , UPDATE_SYSTEM_ID ))) {
@@ -454,7 +485,7 @@ static int md_sqlite_configure(struct md_writer_sqlite *mws,
454485 system_helpers_read_uint64_from_file (mws -> last_conn_tstamp_path ,
455486 & (mws -> dump_tstamp ));
456487
457- return RETVAL_SUCCESS ;
488+ return md_sqlite_read_boot_time ( mws , & ( mws -> orig_boot_time )) ;
458489}
459490
460491void md_sqlite_usage ()
@@ -474,6 +505,7 @@ void md_sqlite_usage()
474505 fprintf (stderr , " \"api_version\":\tbackend API version (default: 1)\n" );
475506 fprintf (stderr , " \"last_conn_tstamp_path\":\toptional path to file where we read/store timestamp of last conn dump\n" );
476507 fprintf (stderr , " \"output_format\":\tJSON/SQL (default SQL)\n" );
508+ fprintf (stderr , " \"ntp_fix_file\":\tFile to check for NTP fix\n" );
477509 fprintf (stderr , "}\n" );
478510}
479511
@@ -483,7 +515,7 @@ int32_t md_sqlite_init(void *ptr, json_object* config)
483515 uint32_t node_id = 0 , interval = DEFAULT_TIMEOUT , num_events = EVENT_LIMIT ;
484516 const char * db_filename = NULL , * meta_prefix = NULL , * gps_prefix = NULL ,
485517 * monitor_prefix = NULL , * usage_prefix = NULL ,
486- * output_format = NULL , * system_prefix = NULL ;
518+ * output_format = NULL , * system_prefix = NULL , * ntp_fix_file = NULL ;
487519
488520 json_object * subconfig ;
489521 if (json_object_object_get_ex (config , "sqlite" , & subconfig )) {
@@ -515,7 +547,9 @@ int32_t md_sqlite_init(void *ptr, json_object* config)
515547 else if (!strcmp (key , "last_conn_tstamp_path" ))
516548 mws -> last_conn_tstamp_path = strdup (json_object_get_string (val ));
517549 else if (!strcmp (key , "output_format" ))
518- output_format = json_object_get_string (val );
550+ output_format = json_object_get_string (val );
551+ else if (!strcmp (key , "ntp_fix_file" ))
552+ ntp_fix_file = json_object_get_string (val );
519553 }
520554 }
521555
@@ -528,7 +562,8 @@ int32_t md_sqlite_init(void *ptr, json_object* config)
528562 (gps_prefix && strlen (gps_prefix ) > 117 ) ||
529563 (monitor_prefix && strlen (monitor_prefix ) > 117 ) ||
530564 (usage_prefix && strlen (usage_prefix ) > 117 ) ||
531- (system_prefix && strlen (system_prefix ) > 117 )) {
565+ (system_prefix && strlen (system_prefix ) > 117 ) ||
566+ (ntp_fix_file && strlen (ntp_fix_file ) > 127 )) {
532567 META_PRINT_SYSLOG (mws -> parent , LOG_ERR , "SQLite temp file prefix too long\n" );
533568 return RETVAL_FAILURE ;
534569 }
@@ -563,31 +598,31 @@ int32_t md_sqlite_init(void *ptr, json_object* config)
563598
564599 return md_sqlite_configure (mws , db_filename , node_id , interval ,
565600 num_events , meta_prefix , gps_prefix , monitor_prefix , usage_prefix ,
566- system_prefix );
601+ system_prefix , ntp_fix_file );
567602}
568603
569604static uint8_t md_sqlite_check_valid_tstamp (struct md_writer_sqlite * mws )
570605{
571606 struct timeval tv ;
572- uint64_t real_boot_time , uptime ;
607+ uint64_t real_boot_time ;
608+
573609 gettimeofday (& tv , NULL );
574610
575- //We have yet to get proper timestamp, so do not export any events
576- if (tv .tv_sec < FIRST_VALID_TIMESTAMP )
611+ if (mws -> ntp_fix_file [0 ] && access (mws -> ntp_fix_file , F_OK ))
577612 return RETVAL_FAILURE ;
578613
579- //read uptime
580- if (system_helpers_read_uint64_from_file ("/proc/uptime" , & uptime ))
614+ if (md_sqlite_read_boot_time (mws , & real_boot_time )) {
581615 return RETVAL_FAILURE ;
616+ }
582617
583- real_boot_time = tv . tv_sec - uptime ;
618+ META_PRINT_SYSLOG ( mws -> parent , LOG_INFO , "Real boot %" PRIu64 " orig boot %" PRIu64 "\n" , real_boot_time , mws -> orig_boot_time ) ;
584619
585620 if (md_sqlite_update_timestamp_db (mws , UPDATE_EVENT_TSTAMP ,
586- real_boot_time ) ||
621+ mws -> orig_boot_time , real_boot_time ) ||
587622 md_sqlite_update_timestamp_db (mws , UPDATE_UPDATES_TSTAMP ,
588- real_boot_time ) ||
623+ mws -> orig_boot_time , real_boot_time ) ||
589624 md_sqlite_update_timestamp_db (mws , UPDATE_SYSTEM_TSTAMP ,
590- real_boot_time )) {
625+ mws -> orig_boot_time , real_boot_time )) {
591626 META_PRINT_SYSLOG (mws -> parent , LOG_ERR , "Could not update tstamp in database\n" );
592627 return RETVAL_FAILURE ;
593628 }
@@ -680,7 +715,7 @@ static void md_sqlite_handle(struct md_writer *writer, struct md_event *event)
680715
681716 //We have received an indication that a valid timestamp is present, so
682717 //check and update
683- if (!mws -> valid_timestamp && event -> tstamp > FIRST_VALID_TIMESTAMP ) {
718+ if (!mws -> valid_timestamp ) {
684719 if (md_sqlite_check_valid_tstamp (mws )) {
685720 printf ("Invalid timestamp\n" );
686721 return ;
0 commit comments