return log_ctx->fp ? 1 : 0;
}
+static int SCLogFileWriteSocket(const char *buffer, int buffer_len,
+ LogFileCtx *ctx)
+{
+ int tries = 0;
+ int ret = 0;
+ bool reopen = false;
+
+ if (ctx->fp == NULL && ctx->is_sock) {
+ SCLogUnixSocketReconnect(ctx);
+ }
+
+tryagain:
+ ret = -1;
+ reopen = 0;
+ errno = 0;
+ if (ctx->fp != NULL) {
+ int fd = fileno(ctx->fp);
+ ssize_t size = send(fd, buffer, buffer_len, MSG_DONTWAIT);
+ if (size > -1) {
+ ret = 0;
+ } else {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ SCLogDebug("Socket would block, dropping event.");
+ } else if (errno == EINTR) {
+ if (tries++ == 0) {
+ SCLogDebug("Interrupted system call, trying again.");
+ goto tryagain;
+ }
+ SCLogDebug("Too many interrupted system calls, "
+ "dropping event.");
+ } else {
+ /* Some other error. Assume badness and reopen. */
+ SCLogDebug("Send failed: %s", strerror(errno));
+ reopen = true;
+ }
+ }
+ }
+
+ if (reopen && tries++ == 0) {
+ if (SCLogUnixSocketReconnect(ctx)) {
+ goto tryagain;
+ }
+ }
+
+ if (ret == -1) {
+ ctx->dropped++;
+ }
+
+ return ret;
+}
+
/**
* \brief Write buffer to log file.
* \retval 0 on failure; otherwise, the return value of fwrite (number of
static int SCLogFileWrite(const char *buffer, int buffer_len, LogFileCtx *log_ctx)
{
SCMutexLock(&log_ctx->fp_mutex);
+ int ret = 0;
- /* Check for rotation. */
- if (log_ctx->rotation_flag) {
- log_ctx->rotation_flag = 0;
- SCConfLogReopen(log_ctx);
- }
+ if (log_ctx->is_sock) {
+ ret = SCLogFileWriteSocket(buffer, buffer_len, log_ctx);
+ } else {
- if (log_ctx->flags & LOGFILE_ROTATE_INTERVAL) {
- time_t now = time(NULL);
- if (now >= log_ctx->rotate_time) {
+ /* Check for rotation. */
+ if (log_ctx->rotation_flag) {
+ log_ctx->rotation_flag = 0;
SCConfLogReopen(log_ctx);
- log_ctx->rotate_time = now + log_ctx->rotate_interval;
}
- }
-
- int ret = 0;
- if (log_ctx->fp == NULL && log_ctx->is_sock)
- SCLogUnixSocketReconnect(log_ctx);
-
- if (log_ctx->fp) {
- clearerr(log_ctx->fp);
- ret = fwrite(buffer, buffer_len, 1, log_ctx->fp);
- fflush(log_ctx->fp);
-
- if (ferror(log_ctx->fp) && log_ctx->is_sock) {
- /* Error on Unix socket, maybe needs reconnect */
- if (SCLogUnixSocketReconnect(log_ctx)) {
- ret = fwrite(buffer, buffer_len, 1, log_ctx->fp);
- fflush(log_ctx->fp);
+ if (log_ctx->flags & LOGFILE_ROTATE_INTERVAL) {
+ time_t now = time(NULL);
+ if (now >= log_ctx->rotate_time) {
+ SCConfLogReopen(log_ctx);
+ log_ctx->rotate_time = now + log_ctx->rotate_interval;
}
}
+
+ if (log_ctx->fp) {
+ clearerr(log_ctx->fp);
+ ret = fwrite(buffer, buffer_len, 1, log_ctx->fp);
+ fflush(log_ctx->fp);
+ }
}
SCMutexUnlock(&log_ctx->fp_mutex);