int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "Error: please provide a filename to store the program output\n"); usage(); exit(1); } char *fname = argv[1]; // do not accept directories, links, and files with ".." if (strstr(fname, "..") || is_link(fname) || is_dir(fname)) { fprintf(stderr, "Error: invalid output file. Links, directories and files with \"..\" are not allowed.\n"); exit(1); } struct stat s; if (stat(fname, &s) == 0) { // check permissions if (s.st_uid != getuid() || s.st_gid != getgid()) { fprintf(stderr, "Error: the output file needs to be owned by the current user.\n"); exit(1); } // check hard links if (s.st_nlink != 1) { fprintf(stderr, "Error: no hard links allowed.\n"); exit(1); } } // check if we can append to this file /* coverity[toctou] */ FILE *fp = fopen(fname, "a"); if (!fp) { fprintf(stderr, "Error: cannot open output file %s\n", fname); exit(1); } fclose(fp); // preserve the last log file log_rotate(fname); setvbuf (stdout, NULL, _IONBF, 0); while(1) { int n = read(0, buf, sizeof(buf)); if (n < 0 && errno == EINTR) continue; if (n <= 0) break; fwrite(buf, n, 1, stdout); log_write(buf, n, fname); } log_close(); return 0; }
int log_rotate_if_needed(AppContext * const context) { if (log_get_delay_before_next(context) == (time_t) 0 || context->logfile_ops->logfile_ftello(context) >= (off_t) context->logfile_soft_limit) { return log_rotate(context); } return 0; }
void setupLogfile(void) { if (!logfile.isEmpty()) { if (log_rotate(1) < 0) { VERBOSE(VB_IMPORTANT, LOC_WARN + "Cannot open logfile; using stdout/stderr instead"); } else signal(SIGHUP, &log_rotate_handler); } }
log_t *log_open(const char *filename, int log_flags) { log_rotate(filename, LOG_MAXBACKUPS); logfile = fopen(filename, "a"); if (logfile) { /* Get UNIX-style time and display as number and string. */ time_t ltime; time(<ime); fprintf(logfile, "===\n=== Logfile started at %s===\n", ctime(<ime)); return log_create(log_flags, logfile, log_stdio); } return NULL; }
void log_open(const char *filename) { if (logfile) { log_close(); } log_rotate(filename, LOG_MAXBACKUPS); logfile = fopen(filename, "a"); if (logfile) { /* Get UNIX-style time and display as number and string. */ time_t ltime; time(<ime); fprintf(logfile, "===\n=== Logfile started at %s===\n", ctime(<ime)); } }
static gboolean gstreamill_monitor (GstClock *clock, GstClockTime time, GstClockID id, gpointer user_data) { GstClockID nextid; GstClockReturn ret; GstClockTime now; Gstreamill *gstreamill; GSList *list; gstreamill = (Gstreamill *)user_data; g_mutex_lock (&(gstreamill->job_list_mutex)); /* remove stoped job from job list */ clean_job_list (gstreamill); /* stop? */ if (gstreamill->stop && g_slist_length (gstreamill->job_list) == 0) { GST_ERROR ("streamill stopped"); exit (0); } /* check job stat */ if (!gstreamill->stop) { list = gstreamill->job_list; g_slist_foreach (list, job_check_func, gstreamill); } /* log rotate. */ if (gstreamill->daemon) { log_rotate (gstreamill); dvr_clean (gstreamill); } g_mutex_unlock (&(gstreamill->job_list_mutex)); /* register streamill monitor */ now = gst_clock_get_time (gstreamill->system_clock); nextid = gst_clock_new_single_shot_id (gstreamill->system_clock, now + 2000 * GST_MSECOND); ret = gst_clock_id_wait_async (nextid, gstreamill_monitor, gstreamill, NULL); gst_clock_id_unref (nextid); if (ret != GST_CLOCK_OK) { GST_WARNING ("Register gstreamill monitor failure"); return FALSE; } return TRUE; }
static int log_check_rotate(LogContext *pContext) { if (pContext->log_fd == STDERR_FILENO) { if (pContext->current_size > 0) { pContext->current_size = 0; } return ENOENT; } if (pContext->rotate_immediately) { pContext->rotate_immediately = false; return log_rotate(pContext); } return 0; }
void ugh_wcb_silent(EV_P_ ev_timer *w, int tev) { if (0 != aux_terminate) { ev_timer_stop(EV_A_ w); ev_break(EV_A_ EVBREAK_ALL); return; } if (0 != aux_rotatelog) { ugh_daemon_t *d; aux_rotatelog = 0; d = aux_memberof(ugh_daemon_t, wev_silent, w); if (-1 == log_rotate(d->cfg.log_error)) { log_warn("log_rotate(%s) (%d: %s)", d->cfg.log_error, errno, aux_strerror(errno)); } } }
void r_log(int level,char* file, int line, char *Format, ...) { if(level != 3){ return; } if(level > g_cfg.log_level){ return; } #ifdef __THREAD_SAFE__ pthread_mutex_lock(&l_mutex); #endif va_list arg; time_t local; struct tm *t; int len; int sp = 0; static char buff[1024]; static char buff2[1024]; memset(buff, 0, 1024); memset(buff2, 0, 1024); time(&local); t = (struct tm*)localtime(&local); sp += sprintf(buff+sp, "[%4d-%02d-%02d %02d:%02d:%02d][%s:%d]", t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, file, line); snprintf(buff+sp, 1024-sp, ":%s",Format); va_start(arg, Format); len = vsnprintf(buff2, 1024-1, buff, arg); va_end (arg); len += sprintf(buff2 + len ,"\n"); if( (debug_log == NULL) ) open_log(); assert(debug_log); fwrite(buff2, 1, len, debug_log); fflush(debug_log); logfile_size +=len; //printf("log file size %lld \n",logfile_size); if(logfile_size > 100000000){ log_rotate(); logfile_size = 0; } #ifdef __THREAD_SAFE__ pthread_mutex_unlock(&l_mutex); #endif }
void log_write(int level, const char *fmt, ...) { struct tm tm; int pos; int end; int status; va_list ap; time_t now; int index; if (level > log_level) { return; } if (log_buffer == MAP_FAILED) { log_buffer = mmap(0, LOG_BUFFER_SIZE, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (log_buffer == MAP_FAILED) { fprintf(stderr, "mmap log buffer failed: %s\n", strerror(errno)); return; } } now = time(NULL); localtime_r(&now, &tm); pos = sprintf(log_buffer, "[%04d/%02d/%02d-%02d:%02d:%02d][%05d][%s]", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, getpid(), log_level_text[level]); va_start(ap, fmt); end = vsnprintf(log_buffer + pos, LOG_BUFFER_SIZE - pos, fmt, ap); va_end(ap); log_buffer[end + pos] = '\n'; index = log_multi ? level : 0; if ((log_files[index].fd != -1 && log_rotate(log_files[index].fd, (const char*)log_files[index].path) == 0)) { close(log_files[index].fd); log_files[index].fd = -1; } if (log_files[index].fd == -1) { log_files[index].fd = open(log_files[index].path, O_WRONLY|O_CREAT|O_APPEND, 0644); if (log_files[index].fd < 0) { fprintf(stderr, "open log file %s failed: %s\n", log_files[index].path, strerror(errno)); return; } status = fcntl(log_files[index].fd, F_GETFD, 0); status |= FD_CLOEXEC; fcntl(log_files[index].fd, F_SETFD, status); } if (write(log_files[index].fd, log_buffer, end + pos + 1) != end + pos + 1) { fprintf(stderr, "write log to file %s failed: %s\n", log_files[index].path, strerror(errno)); return; } }
void log_rotate_handler(int) { log_rotate(0); }
int idle() { static struct timeval pta = { 0, 0 }; static int process_time_count = 0; struct timeval now; if (pta.tv_sec == 0) gettimeofday(&pta, (struct timezone *) 0); int passed_pulses; if (!(passed_pulses = thecore_idle())) return 0; assert(passed_pulses > 0); DWORD t; while (passed_pulses--) { heartbeat(thecore_heart, ++thecore_heart->pulse); // To reduce the possibility of abort() in checkpointing thecore_tick(); } t = get_dword_time(); CHARACTER_MANAGER::instance().Update(thecore_heart->pulse); db_clientdesc->Update(t); s_dwProfiler[PROF_CHR_UPDATE] += (get_dword_time() - t); t = get_dword_time(); if (!io_loop(main_fdw)) return 0; s_dwProfiler[PROF_IO] += (get_dword_time() - t); log_rotate(); gettimeofday(&now, (struct timezone *) 0); ++process_time_count; if (now.tv_sec - pta.tv_sec > 0) { pt_log("[%3d] event %5d/%-5d idle %-4ld event %-4ld heartbeat %-4ld I/O %-4ld chrUpate %-4ld | WRITE: %-7d | PULSE: %d", process_time_count, num_events_called, event_count(), thecore_profiler[PF_IDLE], s_dwProfiler[PROF_EVENT], s_dwProfiler[PROF_HEARTBEAT], s_dwProfiler[PROF_IO], s_dwProfiler[PROF_CHR_UPDATE], current_bytes_written, thecore_pulse()); num_events_called = 0; current_bytes_written = 0; process_time_count = 0; gettimeofday(&pta, (struct timezone *) 0); memset(&thecore_profiler[0], 0, sizeof(thecore_profiler)); memset(&s_dwProfiler[0], 0, sizeof(s_dwProfiler)); } #ifdef _WIN32 if (_kbhit()) { int c = _getch(); switch (c) { case 0x1b: // Esc return 0; // shutdown break; default: break; } } #endif return 1; }
int main(int argc, char **argv) { bool bShowSettings = false; bool cmdline_err; MythCommandLineParser cmdline( kCLPOverrideSettingsFile | kCLPOverrideSettings | kCLPQueryVersion); for (int argpos = 0; argpos < argc; ++argpos) { if (cmdline.PreParse(argc, argv, argpos, cmdline_err)) { if (cmdline_err) return GENERIC_EXIT_INVALID_CMDLINE; if (cmdline.WantsToExit()) return GENERIC_EXIT_OK; } } QApplication a(argc, argv); QFileInfo finfo(a.argv()[0]); QString binname = finfo.baseName(); // Check command line arguments for (int argpos = 1; argpos < a.argc(); ++argpos) { if (!strcmp(a.argv()[argpos],"-v") || !strcmp(a.argv()[argpos],"--verbose")) { if (a.argc()-1 > argpos) { if (parse_verbose_arg(a.argv()[argpos+1]) == GENERIC_EXIT_INVALID_CMDLINE) return GENERIC_EXIT_INVALID_CMDLINE; ++argpos; } else { cerr << "Missing argument to -v/--verbose option\n"; return GENERIC_EXIT_INVALID_CMDLINE; } } else if (!strcmp(a.argv()[argpos],"-s") || !strcmp(a.argv()[argpos],"--setup")) { bShowSettings = true; } else if (!strcmp(a.argv()[argpos], "-l") || !strcmp(a.argv()[argpos], "--logfile")) { if (a.argc()-1 > argpos) { logfile = a.argv()[argpos+1]; if (logfile.startsWith("-")) { cerr << "Invalid or missing argument to -l/--logfile option\n"; return GENERIC_EXIT_INVALID_CMDLINE; } else { ++argpos; } } else { cerr << "Missing argument to -l/--logfile option\n"; return GENERIC_EXIT_INVALID_CMDLINE; } } else if (cmdline.Parse(a.argc(), a.argv(), argpos, cmdline_err)) { if (cmdline_err) return GENERIC_EXIT_INVALID_CMDLINE; if (cmdline.WantsToExit()) return GENERIC_EXIT_OK; } else { showUsage(cmdline); return GENERIC_EXIT_INVALID_CMDLINE; } } gContext = new MythContext(MYTH_BINARY_VERSION); if (!gContext->Init()) { VERBOSE(VB_IMPORTANT, "mythwelcome: Could not initialize MythContext. " "Exiting."); return GENERIC_EXIT_NO_MYTHCONTEXT; } gCoreContext->SetAppName(binname); if (!MSqlQuery::testDBConnection()) { VERBOSE(VB_IMPORTANT, "mythwelcome: Could not open the database. " "Exiting."); return -1; } if (!logfile.isEmpty()) { if (!log_rotate(true)) cerr << "cannot open logfile; using stdout/stderr" << endl; else signal(SIGHUP, &log_rotate_handler); } LCD::SetupLCD(); if (LCD *lcd = LCD::Get()) lcd->switchToTime(); MythTranslation::load("mythfrontend"); GetMythUI()->LoadQtConfig(); #ifdef Q_WS_MACX // Mac OS 10.4 and Qt 4.4 have window-focus problems gCoreContext->SetSetting("RunFrontendInWindow", "1"); #endif MythMainWindow *mainWindow = GetMythMainWindow(); mainWindow->Init(); initKeys(); if (bShowSettings) { MythShutdownSettings settings; settings.exec(); } else { MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack(); WelcomeDialog *welcome = new WelcomeDialog(mainStack, "mythwelcome"); if (welcome->Create()) mainStack->AddScreen(welcome, false); else return -1; do { qApp->processEvents(); usleep(5000); } while (mainStack->TotalScreens() > 0); } DestroyMythMainWindow(); delete gContext; return 0; }
static void log_rotate_handler(int) { log_rotate(false); }