📄 log.c
字号:
voidlog_errno_routine(int e, const char *message, ...){ va_list args; char m[LOG_WIDTH]; /* longer messages will be truncated */ va_start(args, message); fmt_log(m, sizeof(m), message, args); va_end(args); if (log_to_stderr) fprintf(stderr, "ERROR: %s. Errno %d: %s\n", m, e, strerror(e)); if (log_to_syslog) syslog(LOG_ERR, "ERROR: %s. Errno %d: %s", m, e, strerror(e)); if (log_to_perpeer) { peerlog(strerror(e), m); } whack_log(RC_LOG_SERIOUS , "~ERROR: %s. Errno %d: %s", m, e, strerror(e));}voidexit_log(const char *message, ...){ va_list args; char m[LOG_WIDTH]; /* longer messages will be truncated */ va_start(args, message); fmt_log(m, sizeof(m), message, args); va_end(args); if (log_to_stderr) fprintf(stderr, "FATAL ERROR: %s\n", m); if (log_to_syslog) syslog(LOG_ERR, "FATAL ERROR: %s", m); if (log_to_perpeer) peerlog("FATAL ERROR: ", m); whack_log(RC_LOG_SERIOUS, "~FATAL ERROR: %s", m); exit_pluto(1);}voidexit_log_errno_routine(int e, const char *message, ...){ va_list args; char m[LOG_WIDTH]; /* longer messages will be truncated */ va_start(args, message); fmt_log(m, sizeof(m), message, args); va_end(args); if (log_to_stderr) fprintf(stderr, "FATAL ERROR: %s. Errno %d: %s\n", m, e, strerror(e)); if (log_to_syslog) syslog(LOG_ERR, "FATAL ERROR: %s. Errno %d: %s", m, e, strerror(e)); if (log_to_perpeer) peerlog(strerror(e), m); whack_log(RC_LOG_SERIOUS , "~FATAL ERROR: %s. Errno %d: %s", m, e, strerror(e)); exit_pluto(1);}/* emit message to whack. * form is "ddd statename text" where * - ddd is a decimal status code (RC_*) as described in whack.h * - text is a human-readable annotation */#ifdef DEBUGstatic volatile sig_atomic_t dying_breath = FALSE;#endifvoidwhack_log(int mess_no, const char *message, ...){ int wfd = whack_log_fd != NULL_FD ? whack_log_fd : cur_state != NULL ? cur_state->st_whack_sock : NULL_FD; if (wfd != NULL_FD#ifdef DEBUG || dying_breath#endif ) { va_list args; char m[LOG_WIDTH]; /* longer messages will be truncated */ int prelen = snprintf(m, sizeof(m), "%03d ", mess_no); passert(prelen >= 0); va_start(args, message); fmt_log(m+prelen, sizeof(m)-prelen, message, args); va_end(args);#if DEBUG if (dying_breath) { /* status output copied to log */ if (log_to_stderr) fprintf(stderr, "%s\n", m + prelen); if (log_to_syslog) syslog(LOG_WARNING, "%s", m + prelen); if (log_to_perpeer) peerlog("", m); }#endif if (wfd != NULL_FD) { /* write to whack socket, but suppress possible SIGPIPE */ size_t len = strlen(m);#ifdef MSG_NOSIGNAL /* depends on version of glibc??? */ m[len] = '\n'; /* don't need NUL, do need NL */ (void) send(wfd, m, len + 1, MSG_NOSIGNAL);#else /* !MSG_NOSIGNAL */ int r; struct sigaction act , oldact; m[len] = '\n'; /* don't need NUL, do need NL */ act.sa_handler = SIG_IGN; sigemptyset(&act.sa_mask); act.sa_flags = 0; /* no nothing */ r = sigaction(SIGPIPE, &act, &oldact); passert(r == 0); (void) write(wfd, m, len + 1); r = sigaction(SIGPIPE, &oldact, NULL); passert(r == 0);#endif /* !MSG_NOSIGNAL */ } }}/* Debugging message support */#ifdef DEBUGvoidswitch_fail(int n, const char *file_str, unsigned long line_no){ char buf[30]; snprintf(buf, sizeof(buf), "case %d unexpected", n); passert_fail(buf, file_str, line_no); /* NOTREACHED */}voidpassert_fail(const char *pred_str, const char *file_str, unsigned long line_no){ /* we will get a possibly unplanned prefix. Hope it works */ loglog(RC_LOG_SERIOUS, "ASSERTION FAILED at %s:%lu: %s", file_str, line_no, pred_str); if (!dying_breath) { dying_breath = TRUE; show_status(); } abort(); /* exiting correctly doesn't always work */}voidpexpect_log(const char *pred_str, const char *file_str, unsigned long line_no){ /* we will get a possibly unplanned prefix. Hope it works */ loglog(RC_LOG_SERIOUS, "EXPECTATION FAILED at %s:%lu: %s", file_str, line_no, pred_str);}lset_t base_debugging = DBG_NONE, /* default to reporting nothing */ cur_debugging = DBG_NONE;voidextra_debugging(const struct connection *c){ if(c == NULL) { reset_debugging(); return; } if (c!= NULL && c->extra_debugging != 0) { openswan_log("enabling for connection: %s" , bitnamesof(debug_bit_names, c->extra_debugging & ~cur_debugging)); set_debugging(cur_debugging | c->extra_debugging); }}voidset_debugging(lset_t deb){ cur_debugging = deb; pfkey_lib_debug = (cur_debugging&DBG_PFKEY ? PF_KEY_DEBUG_PARSE_MAX : PF_KEY_DEBUG_PARSE_NONE);}/* log a debugging message (prefixed by "| ") */voidDBG_log(const char *message, ...){ va_list args; char m[LOG_WIDTH]; /* longer messages will be truncated */ va_start(args, message); vsnprintf(m, sizeof(m), message, args); va_end(args); /* then sanitize anything else that is left. */ (void)sanitize_string(m, sizeof(m)); if (log_to_stderr) fprintf(stderr, "%c %s\n", debug_prefix, m); if (log_to_syslog) syslog(LOG_DEBUG, "%c %s", debug_prefix, m); if (log_to_perpeer) { char prefix[3]; prefix[0]=debug_prefix; prefix[1]=' '; prefix[2]='\n'; peerlog(prefix, m); }}/* dump raw bytes in hex to stderr (for lack of any better destination) */voidopenswan_DBG_dump(const char *label, const void *p, size_t len){# define DUMP_LABEL_WIDTH 20 /* arbitrary modest boundary */# define DUMP_WIDTH (4 * (1 + 4 * 3) + 1) char buf[DUMP_LABEL_WIDTH + DUMP_WIDTH]; char *bp; const unsigned char *cp = p; bp = buf; if (label != NULL && label[0] != '\0') { /* Handle the label. Care must be taken to avoid buffer overrun. */ size_t llen = strlen(label); if (llen + 1 > sizeof(buf)) { DBG_log("%s", label); } else { strcpy(buf, label); if (buf[llen-1] == '\n') { buf[llen-1] = '\0'; /* get rid of newline */ DBG_log("%s", buf); } else if (llen < DUMP_LABEL_WIDTH) { bp = buf + llen; } else { DBG_log("%s", buf); } } } do { int i, j; for (i = 0; len!=0 && i!=4; i++) { *bp++ = ' '; for (j = 0; len!=0 && j!=4; len--, j++) { static const char hexdig[] = "0123456789abcdef"; *bp++ = ' '; *bp++ = hexdig[(*cp >> 4) & 0xF]; *bp++ = hexdig[*cp & 0xF]; cp++; } } *bp = '\0'; DBG_log("%s", buf); bp = buf; } while (len != 0);# undef DUMP_LABEL_WIDTH# undef DUMP_WIDTH}#endif /* DEBUG */voidshow_status(void){ show_ifaces_status(); show_myid_status(); show_debug_status(); whack_log(RC_COMMENT, BLANK_FORMAT); /* spacer */#ifdef KERNEL_ALG kernel_alg_show_status(); whack_log(RC_COMMENT, BLANK_FORMAT); /* spacer */#endif#ifdef IKE_ALG ike_alg_show_status(); whack_log(RC_COMMENT, BLANK_FORMAT); /* spacer */#endif#ifndef NO_DB_OPS_STATS db_ops_show_status(); whack_log(RC_COMMENT, BLANK_FORMAT); /* spacer */#endif show_connections_status(); whack_log(RC_COMMENT, BLANK_FORMAT); /* spacer */ show_states_status();#ifdef KLIPS whack_log(RC_COMMENT, BLANK_FORMAT); /* spacer */ show_shunt_status();#endif}/* ip_str: a simple to use variant of addrtot. * It stores its result in a static buffer. * This means that newer calls overwrite the storage of older calls. * Note: this is not used in any of the logging functions, so their * callers may use it. */const char *ip_str(const ip_address *src){ static char buf[ADDRTOT_BUF]; addrtot(src, 0, buf, sizeof(buf)); return buf;}/* * a routine that attempts to schedule itself daily. * */voiddaily_log_reset(void){ /* now perform actions */ logged_txt_warning = FALSE; logged_myid_fqdn_txt_warning = FALSE; logged_myid_ip_txt_warning = FALSE; logged_myid_fqdn_key_warning = FALSE; logged_myid_ip_key_warning = FALSE;}voiddaily_log_event(void){ struct tm *ltime; time_t n, interval; /* attempt to schedule oneself to midnight, local time * do this by getting seconds in the day, and delaying * by 86400 - hour*3600+minutes*60+seconds. */ time(&n); ltime = localtime(&n); interval = (24 * 60 * 60) - (ltime->tm_sec + ltime->tm_min * 60 + ltime->tm_hour * 3600); event_schedule(EVENT_LOG_DAILY, interval, NULL); daily_log_reset();}/* for paths.h *//* * decode the paths */struct pluto_paths plutopaths;void verify_path_space(struct paththing *p, size_t min, const char *why){ if (min > p->path_space) { pfreeany(p->path); p->path_space = min + 10; p->path = alloc_bytes(p->path_space, why); }}void set_paths(const char *basedir){ size_t baselen = strlen(basedir) + 2; verify_path_space(&plutopaths.acerts, baselen + sizeof("acerts"), "acert path"); snprintf(plutopaths.acerts.path, plutopaths.acerts.path_space, "%s/acerts", basedir); verify_path_space(&plutopaths.cacerts, baselen + sizeof("cacerts"), "cacert path"); snprintf(plutopaths.cacerts.path, plutopaths.cacerts.path_space, "%s/cacerts", basedir); verify_path_space(&plutopaths.crls, baselen + sizeof("crls"), "crls path"); snprintf(plutopaths.crls.path, plutopaths.crls.path_space, "%s/crls", basedir); verify_path_space(&plutopaths.private, baselen + sizeof("private"), "private path"); snprintf(plutopaths.private.path, plutopaths.private.path_space, "%s/private", basedir); verify_path_space(&plutopaths.certs, baselen + sizeof("certs"), "certs path"); snprintf(plutopaths.certs.path, plutopaths.certs.path_space, "%s/certs", basedir); verify_path_space(&plutopaths.aacerts, baselen + sizeof("aacerts"), "aacerts path"); snprintf(plutopaths.aacerts.path, plutopaths.certs.path_space, "%s/aacerts", basedir); verify_path_space(&plutopaths.ocspcerts, baselen + sizeof("ocspcerts"), "ocspcerts path"); snprintf(plutopaths.ocspcerts.path, plutopaths.certs.path_space, "%s/ocspcerts", basedir);}/* * Local Variables: * c-basic-offset:4 * c-style: pluto * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -