/* * Do the commands in cmds (initialized by yyparse). */ void docmds(struct namelist *hostlist, int argc, char **argv) { struct cmd *c; char *cp; int i; (void) signal(SIGHUP, sighandler); (void) signal(SIGINT, sighandler); (void) signal(SIGQUIT, sighandler); (void) signal(SIGTERM, sighandler); if (!nflag) mysetlinebuf(stdout); /* Make output (mostly) clean */ #if defined(USE_STATDB) if (!nflag && (dostatdb || juststatdb)) { extern long reccount; message(MT_INFO, "Making stat database [%s] ... \n", gettimestr()); if (mkstatdb() < 0) error("Warning: Make stat database failed."); message(MT_INFO, "Stat database created: %d files stored [%s].\n", reccount, gettimestr()); if (juststatdb) return; } #endif /* USE_STATDB */ /* * Print errors for any command line targets we didn't find. * If any errors are found, return to main() which will then exit. */ for (i = 0; i < argc; i++) { int found; for (found = FALSE, c = cmds; c != NULL; c = c->c_next) { if (c->c_label && argv[i] && strcmp(c->c_label, argv[i]) == 0) { found = TRUE; break; } } if (!found) error("Label \"%s\" is not defined in the distfile.", argv[i]); } if (nerrs) return; /* * Main command loop. Loop through all the commands. */ for (c = cmds; c != NULL; c = c->c_next) { checkcmd(c); if (do_fork) { /* * Let the children take care of their assigned host */ if (amchild) { if (strcmp(c->c_name, currenthost) != 0) continue; } else if (c->c_flags & CMD_ASSIGNED) { /* This cmd has been previously assigned */ debugmsg(DM_MISC, "prev assigned: %s\n", c->c_name); continue; } } if (hostlist) { /* Do specific hosts as specified on command line */ struct namelist *nlptr; for (nlptr = hostlist; nlptr; nlptr = nlptr->n_next) /* * Try an exact match and then a match * without '@' (if present). */ if ((strcmp(c->c_name, nlptr->n_name) == 0) || ((cp = strchr(c->c_name, '@')) && strcmp(++cp, nlptr->n_name) == 0)) docmd(c, argc, argv); continue; } else /* Do all of the command */ docmd(c, argc, argv); } if (do_fork) { /* * We're multi-threaded, so do appropriate shutdown * actions based on whether we're the parent or a child. */ if (amchild) { if (!IS_ON(options, DO_QUIET)) message(MT_VERBOSE, "updating of %s finished", currenthost); closeconn(); cleanup(0); exit(nerrs); } /* * Wait for all remaining active children to finish */ while (activechildren > 0) { debugmsg(DM_MISC, "Waiting for %d children to finish.\n", activechildren); waitup(); } } else if (!nflag) { /* * We're single-threaded so close down current connection */ closeconn(); cleanup(0); } }
/** * @brief log handler * * @see * @note * @author auxten <*****@*****.**> * @date 2011-8-1 **/ void _gko_log(const u_char log_level, const char *fmt, ...) { if (gko.opt.to_debug || log_level < DEBUG ) { int errnum = errno; va_list args; va_start(args, fmt); char logstr[256] = {'\0'}; char oldlogpath[MAX_PATH_LEN] = {'\0'}; static FILE * lastfp = NULL; static GKO_INT64 counter = 1; snprintf(logstr, sizeof(logstr), "%s[%u]", LOG_DIC[log_level], gko_gettid()); gettimestr(logstr + strlen(logstr), TIME_FORMAT); vsnprintf(logstr + strlen(logstr), sizeof(logstr) - strlen(logstr), fmt, args); if (log_level < NOTICE) { snprintf(logstr + strlen(logstr), sizeof(logstr) - strlen(logstr), "; "); strerror_r(errnum, logstr + strlen(logstr), sizeof(logstr) - strlen(logstr)); } pthread_mutex_lock(&logcut_lock); if (gko.opt.logpath[0] == '\0') { gko.log_fp = stdout; } else { counter ++; if (counter % MAX_LOG_REOPEN_LINE == 0) { if (lastfp) { fclose(lastfp); } lastfp = gko.log_fp; if (counter % MAX_LOG_LINE == 0) { strncpy(oldlogpath, gko.opt.logpath, MAX_PATH_LEN); gettimestr(oldlogpath + strlen(oldlogpath), OLD_LOG_TIME); rename(gko.opt.logpath, oldlogpath); } gko.log_fp = fopen(gko.opt.logpath, "a+"); } if(UNLIKELY(! gko.log_fp)) { gko.log_fp = fopen(gko.opt.logpath, "a+"); if(! gko.log_fp) { perror("Cann't open log file"); exit(1); } } } fprintf(gko.log_fp, "%s\n", logstr); fflush(gko.log_fp); pthread_mutex_unlock(&logcut_lock); va_end(args); } return; }
/** * @brief log handler * * @see * @note * @author auxten <*****@*****.**> <*****@*****.**> * @date 2011-8-1 **/ void gko_log_flf(const u_int8_t log_level, const char *file, const int line, const char *func, const char *fmt, ...) { if (gko.opt.to_debug || log_level < DEBUG ) { int errnum = errno; va_list args; va_start(args, fmt); char logstr[MAX_LOG_BYTE]; char oldlogpath[MAX_PATH_LEN]; char rmOldLogs[MAX_PATH_LEN]; static FILE * lastfp = NULL; static GKO_INT64 counter = 1; struct timeval last_timeval; struct timeval time_diff; struct timeval * time_p = get_timer(); long usec_diff; int len = 0; int rm_cmd_len; memcpy(&last_timeval, time_p, sizeof(last_timeval)); len += snprintf(logstr, sizeof(logstr), "%s: [%u]", LOG_DIC[log_level], gko_gettid()); len += gettimestr(logstr + len, TIME_FORMAT, time_p); timersub(time_p, &last_timeval, &time_diff); usec_diff = time_diff.tv_sec * 1000000 + time_diff.tv_usec; if (usec_diff < 0) usec_diff = 0; len += snprintf(logstr + len, sizeof(logstr) - len, "[%s:%d @%s][%ldus]\t", file, line, func, usec_diff); len += vsnprintf(logstr + len, sizeof(logstr) - len, fmt, args); logstr[sizeof(logstr) - 3] = '\0'; len = MIN(sizeof(logstr) - 3, len); if (log_level < NOTICE) { len += snprintf(logstr + len, sizeof(logstr) - len, "; "); strerror_r(errnum, logstr + len, sizeof(logstr) - len); } pthread_mutex_lock(&g_logcut_lock); if (gko.opt.logpath[0] == '\0') { gko.log_fp = stdout; } else { counter ++; if (counter % MAX_LOG_REOPEN_LINE == 0) { if (lastfp) { fclose(lastfp); } lastfp = gko.log_fp; if (counter % MAX_LOG_LINE == 0) { strncpy(oldlogpath, gko.opt.logpath, MAX_PATH_LEN - 1); gettimestr(oldlogpath + strlen(oldlogpath), OLD_LOG_TIME, time_p); rename(gko.opt.logpath, oldlogpath); rm_cmd_len = snprintf(rmOldLogs, MAX_PATH_LEN - 1, "/bin/ls -t %s.201* | /usr/bin/tail -n +%u | /usr/bin/xargs /bin/rm -f", gko.opt.logpath, MAX_LOG_KEEPED); if (strlen(gko.opt.logpath) > 0 && rm_cmd_len < MAX_PATH_LEN && rm_cmd_len > 0) { rmOldLogs[rm_cmd_len] = '\0'; system(rmOldLogs); } } gko.log_fp = fopen(gko.opt.logpath, "a+"); } if(UNLIKELY(! gko.log_fp)) { gko.log_fp = fopen(gko.opt.logpath, "a+"); if(! gko.log_fp) { perror("Cann't open log file"); _exit(1); } } } fprintf(gko.log_fp, "%s\n", logstr); if (usec_diff >= 1000000 || (counter % MAX_LOG_FLUSH_LINE == 0)) { fflush(gko.log_fp); } pthread_mutex_unlock(&g_logcut_lock); va_end(args); } return; }