void CWatchdogMngr::OnQuit() { CSimpleTimer t ; Ping(); //to make sure that prepare_for_reboot.sh has time to run systemf_to( ON_QUIT_DELAY, NIVIS_FIRMWARE "prepare_for_reboot.sh&" ); LOG("Waiting[%u seconds] for prepare_for_reboot.sh",ON_QUIT_DELAY) ; int nTimeLeftToPing = ON_QUIT_DELAY; if (FileIsExecutable(FTP_SCRIPT)) { nTimeLeftToPing = ON_QUIT_DELAY_FTP_LOGS; } t.SetTimer( nTimeLeftToPing) ; while( ! t.IsSignaling() ) { Ping() ; Sleep() ; } Ping() ; systemf_to( 20, "log2flash 'WTD: Finished running quit procedure.'&"); }
static bool log2flash_fa(const char *fname, const char *fmt, va_list arglist) { bool retcode=false; // assume operation failed. int fd; time_t cur_time; time_t duration=0; struct tm *ptm; // structure to obtain date information char printbuf[MAX_WRITE_CHAR+2]; // size + '\n' + '\0' int bytes_to_write; int lock_to; // time out for flock() DPRINT( "log2flash_fa() called\n" ); fd = open(fname, O_RDWR|O_CREAT|O_NONBLOCK, 00644); if (fd == -1) { LOG_ERR("log2flash ERROR: Unable to open file."); goto EXIT; } lock_to = FLOCK_TIMEOUT; while( flock(fd, LOCK_EX | LOCK_NB) != 0 ) { sleep(1); lock_to--; if (lock_to < 0) { LOG_ERR("log2flash ERROR: lock timeout"); goto CLOSE_EXIT; } } if ( lseek(fd, 0, SEEK_END) == -1 ) { LOG_ERR("log2flash ERROR: unable to seek to the end of file. errno=%d", errno); goto CLOSE_EXIT; } cur_time=time(NULL); ptm=gmtime(&cur_time); // tm_year from 1900, tm_mon [0-11] // it takes 20 characters spaces. sprintf(printbuf, "%d-%02d-%02d %02d:%02d:%02d ", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); // generate the buffer string. vsnprintf(printbuf+20, MAX_WRITE_CHAR-20, fmt, arglist); bytes_to_write = strlen(printbuf); printbuf[bytes_to_write] = '\n'; printbuf[++bytes_to_write] = 0; DPRINT( printbuf ); // gather statistic struct stat f_stat; if ( fstat(fd, &f_stat) == -1 ) { LOG_ERR("log2flash ERROR: unable to gatter statistic for %s. errno=%d", fname, errno); goto CLOSE_EXIT; } // if write failed, goto CLOSE_EXIT if (loop_write(fd, printbuf, bytes_to_write) != true) goto CLOSE_EXIT; if ( f_stat.st_size + strlen(printbuf) > LOG_LIMIT) { // starting to rotate DPRINT( "File Size exceeded LOG_LIMIT - Perform rotation\n" ); char from[PATH_MAX]; char to[PATH_MAX]; for( int i=ROTATE_MAX; i>0; --i) { sprintf( from, "%s.%d", fname, i-1); sprintf( to, "%s.%d", fname, i); rename( from, to); } duration = time(NULL); Copy( fname, from, LOG_LIMIT ); unlink( fname ); duration -= time(NULL); if (FileIsExecutable(FTP_SCRIPT)) { char sBaseFile[PATH_MAX]; char sFileName[PATH_MAX]; sprintf(sBaseFile,NIVIS_TMP"activity.log"); AttachDatetime(sBaseFile,sFileName); Copy(from,sFileName); systemf_to(5, FTP_SCRIPT" %s &", sFileName); } } retcode=true; CLOSE_EXIT: flock(fd, LOCK_UN); close(fd); if (duration < -10) { //it really is negative :) we only convert it when we need to show it, below log2flash("Logfile rotation took quite long: %d seconds", -duration); } EXIT: return retcode; }