Beispiel #1
0
void
log_reader_reopen(LogPipe *s, LogProto *proto, LogPipe *control, LogReaderOptions *options, gint stats_level, gint stats_source, const gchar *stats_id, const gchar *stats_instance, gboolean immediate_check)
{
  LogReader *self = (LogReader *) s;
  gpointer args[] = { s, proto };
  log_source_deinit(s);

  main_loop_call((MainLoopTaskFunc) log_reader_reopen_deferred, args, TRUE);

  if (!main_loop_is_main_thread())
    {
      g_static_mutex_lock(&self->pending_proto_lock);
      while (self->pending_proto_present)
        {
          g_cond_wait(self->pending_proto_cond, g_static_mutex_get_mutex(&self->pending_proto_lock));
        }
      g_static_mutex_unlock(&self->pending_proto_lock);
    }
  if (immediate_check)
    {
      log_reader_set_immediate_check(&self->super.super);
    }
  log_reader_set_options(s, control, options, stats_level, stats_source, stats_id, stats_instance);
  log_reader_set_follow_filename(s, stats_instance);
  log_source_init(s);
}
Beispiel #2
0
static gboolean
afsocket_sc_init(LogPipe *s)
{
  AFSocketSourceConnection *self = (AFSocketSourceConnection *) s;
  LogTransport *transport;
  LogProtoServer *proto;

  if (!self->reader)
    {
      transport = afsocket_sd_construct_transport(self->owner, self->sock);
      proto = log_proto_server_factory_construct(self->owner->proto_factory, transport, &self->owner->reader_options.proto_options.super);
      self->reader = log_reader_new(proto);
    }
  log_reader_set_options(self->reader, s,
                         &self->owner->reader_options,
                         STATS_LEVEL1,
                         self->owner->transport_mapper->stats_source,
                         self->owner->super.super.id,
                         afsocket_sc_stats_instance(self));
  log_reader_set_peer_addr(self->reader, self->peer_addr);
  log_pipe_append((LogPipe *) self->reader, s);
  if (log_pipe_init((LogPipe *) self->reader, NULL))
    {
      return TRUE;
    }
  else
    {
      log_pipe_unref((LogPipe *) self->reader);
      self->reader = NULL;
    }
  return FALSE;
}
Beispiel #3
0
/* NOTE: runs in the main thread */
static void
affile_sd_notify(LogPipe *s, gint notify_code, gpointer user_data)
{
  AFFileSourceDriver *self = (AFFileSourceDriver *) s;
  GlobalConfig *cfg = log_pipe_get_config(s);
  gint fd;
  
  switch (notify_code)
    {
    case NC_FILE_MOVED:
      { 
        msg_verbose("Follow-mode file source moved, tracking of the new file is started",
                    evt_tag_str("filename", self->filename->str),
                    NULL);
        
        log_pipe_deinit((LogPipe *) self->reader);
        log_pipe_unref((LogPipe *) self->reader);
        self->reader = NULL;
        
        if (affile_sd_open_file(self, self->filename->str, &fd))
          {
            LogProtoServer *proto;
            PollEvents *poll_events;
            
            poll_events = affile_sd_construct_poll_events(self, fd);
            if (!poll_events)
              break;

            proto = affile_sd_construct_proto(self, fd);

            self->reader = log_reader_new(self->super.super.super.cfg);
            log_reader_reopen(self->reader, proto, poll_events);

            log_reader_set_options(self->reader,
                                   s,
                                   &self->reader_options,
                                   STATS_LEVEL1,
                                   SCS_FILE,
                                   self->super.super.id,
                                   self->filename->str);
            log_reader_set_immediate_check(self->reader);

            log_pipe_append((LogPipe *) self->reader, s);
            if (!log_pipe_init((LogPipe *) self->reader))
              {
                msg_error("Error initializing log_reader, closing fd",
                          evt_tag_int("fd", fd),
                          NULL);
                log_pipe_unref((LogPipe *) self->reader);
                self->reader = NULL;
                close(fd);
              }
            affile_sd_recover_state(s, cfg, proto);
          }
        break;
      }
    default:
      break;
    }
}
Beispiel #4
0
/* NOTE: runs in the main thread */
static void
affile_sd_notify(LogPipe *s, LogPipe *sender, gint notify_code, gpointer user_data)
{
  AFFileSourceDriver *self = (AFFileSourceDriver *) s;
  GlobalConfig *cfg = log_pipe_get_config(s);
  gint fd;
  
  switch (notify_code)
    {
    case NC_FILE_MOVED:
      { 
        msg_verbose("Follow-mode file source moved, tracking of the new file is started",
                    evt_tag_str("filename", self->filename->str),
                    NULL);
        
        log_pipe_deinit(self->reader);
        log_pipe_unref(self->reader);
        
        if (affile_sd_open_file(self, self->filename->str, &fd))
          {
            LogTransport *transport;
            LogProto *proto;
            
            transport = log_transport_plain_new(fd, 0);
            transport->timeout = 10;

            proto = affile_sd_construct_proto(self, transport);

            self->reader = log_reader_new(proto);

            log_reader_set_options(self->reader, s, &self->reader_options, 1, SCS_FILE, self->super.super.id, self->filename->str);
            log_reader_set_follow_filename(self->reader, self->filename->str);
            log_reader_set_immediate_check(self->reader);

            log_pipe_append(self->reader, s);
            if (!log_pipe_init(self->reader, cfg))
              {
                msg_error("Error initializing log_reader, closing fd",
                          evt_tag_int("fd", fd),
                          NULL);
                log_pipe_unref(self->reader);
                self->reader = NULL;
                close(fd);
              }
            affile_sd_recover_state(s, cfg, proto);
          }
        else
          {
            self->reader = NULL;
          }
        break;
      }
    default:
      break;
    }
}
Beispiel #5
0
static gboolean
afprogram_sd_init(LogPipe *s)
{
    AFProgramSourceDriver *self = (AFProgramSourceDriver *) s;
    GlobalConfig *cfg = log_pipe_get_config(s);
    gint fd;

    if (!log_src_driver_init_method(s))
        return FALSE;

    if (cfg)
        log_reader_options_init(&self->reader_options, cfg, self->super.super.group);

    msg_verbose("Starting source program",
                evt_tag_str("cmdline", self->process_info.cmdline->str));

    if (!afprogram_popen(&self->process_info, G_IO_IN, &fd))
        return FALSE;

    /* parent */
    child_manager_register(self->process_info.pid, afprogram_sd_exit, log_pipe_ref(&self->super.super.super),
                           (GDestroyNotify) log_pipe_unref);

    g_fd_set_nonblock(fd, TRUE);
    g_fd_set_cloexec(fd, TRUE);
    if (!self->reader)
    {
        LogTransport *transport;

        transport = log_transport_pipe_new(fd);
        self->reader = log_reader_new(s->cfg);
        log_reader_reopen(self->reader, log_proto_text_server_new(transport, &self->reader_options.proto_options.super),
                          poll_fd_events_new(fd));
        log_reader_set_options(self->reader,
                               s,
                               &self->reader_options,
                               STATS_LEVEL0,
                               SCS_PROGRAM,
                               self->super.super.id,
                               self->process_info.cmdline->str);
    }
    log_pipe_append((LogPipe *) self->reader, &self->super.super.super);
    if (!log_pipe_init((LogPipe *) self->reader))
    {
        msg_error("Error initializing program source, closing fd",
                  evt_tag_int("fd", fd));
        log_pipe_unref((LogPipe *) self->reader);
        self->reader = NULL;
        close(fd);
        return FALSE;
    }
    return TRUE;
}
Beispiel #6
0
static void
afsocket_sc_set_owner(AFSocketSourceConnection *self, AFSocketSourceDriver *owner)
{
  if (self->reader)
    log_reader_set_options(self->reader, &self->super, &owner->reader_options, 1, afsocket_sc_stats_source(self), owner->super.id, afsocket_sc_stats_instance(self));
  log_drv_unref(&self->owner->super);
  log_drv_ref(&owner->super);
  self->owner = owner;
  
  log_pipe_append(&self->super, &owner->super.super);

}
Beispiel #7
0
static gboolean
afstreams_sd_init(LogPipe *s)
{
  AFStreamsSourceDriver *self = (AFStreamsSourceDriver *) s;
  GlobalConfig *cfg = log_pipe_get_config(s);
  gint fd;

  if (!log_src_driver_init_method(s))
    return FALSE;

  log_reader_options_init(&self->reader_options, cfg, self->super.super.group);

  fd = open(self->dev_filename->str, O_RDONLY | O_NOCTTY | O_NONBLOCK);
  if (fd != -1)
    {
      struct strioctl ioc;

      g_fd_set_cloexec(fd, TRUE);
      memset(&ioc, 0, sizeof(ioc));
      ioc.ic_cmd = I_CONSLOG;
      if (ioctl(fd, I_STR, &ioc) < 0)
        {
          msg_error("Error in ioctl(I_STR, I_CONSLOG)",
                    evt_tag_str(EVT_TAG_FILENAME, self->dev_filename->str),
                    evt_tag_errno(EVT_TAG_OSERROR, errno));
          close(fd);
          return FALSE;
        }
      g_fd_set_nonblock(fd, TRUE);
      self->reader = log_reader_new(cfg);
      log_reader_reopen(self->reader, log_proto_dgram_server_new(log_transport_streams_new(fd), &self->reader_options.proto_options.super), poll_fd_events_new(fd));
      log_reader_set_options(self->reader,
                             s,
                             &self->reader_options,
                             STATS_LEVEL1,
                             SCS_SUN_STREAMS,
                             self->super.super.id,
                             self->dev_filename->str);
      log_pipe_append((LogPipe *) self->reader, s);

      if (self->door_filename)
        {

          /* door creation is deferred, because it creates threads which is
           * not inherited through forks, and syslog-ng forks during
           * startup, but _after_ the configuration was initialized */

          register_application_hook(AH_POST_DAEMONIZED, afstreams_init_door, self);
        }
      if (!log_pipe_init((LogPipe *) self->reader))
        {
          msg_error("Error initializing log_reader, closing fd",
                    evt_tag_int("fd", fd));
          log_pipe_unref((LogPipe *) self->reader);
          self->reader = NULL;
          close(fd);
          return FALSE;
        }

    }
  else
    {
      msg_error("Error opening syslog device",
                evt_tag_str(EVT_TAG_FILENAME, self->dev_filename->str),
                evt_tag_errno(EVT_TAG_OSERROR, errno));
      return FALSE;
    }
  return TRUE;
}
Beispiel #8
0
static gboolean
affile_sd_init(LogPipe *s)
{
  AFFileSourceDriver *self = (AFFileSourceDriver *) s;
  GlobalConfig *cfg = log_pipe_get_config(s);
  gint fd;
  gboolean file_opened, open_deferred = FALSE;

  if (!log_src_driver_init_method(s))
    return FALSE;

  log_reader_options_init(&self->reader_options, cfg, self->super.super.group);

  if ((self->multi_line_mode != MLM_PREFIX_GARBAGE && self->multi_line_mode != MLM_PREFIX_SUFFIX ) && (self->multi_line_prefix || self->multi_line_garbage))
    {
      msg_error("multi-line-prefix() and/or multi-line-garbage() specified but multi-line-mode() is not regexp based (prefix-garbage or prefix-suffix), please set multi-line-mode() properly", NULL);
      return FALSE;
    }

  file_opened = affile_sd_open_file(self, self->filename->str, &fd);
  if (!file_opened && self->follow_freq > 0)
    {
      msg_info("Follow-mode file source not found, deferring open",
               evt_tag_str("filename", self->filename->str),
               NULL);
      open_deferred = TRUE;
      fd = -1;
    }

  if (file_opened || open_deferred)
    {
      LogProtoServer *proto;
      PollEvents *poll_events;

      poll_events = affile_sd_construct_poll_events(self, fd);
      if (!poll_events)
        {
          close(fd);
          return FALSE;
        }

      proto = affile_sd_construct_proto(self, fd);
      self->reader = log_reader_new(self->super.super.super.cfg);
      log_reader_reopen(self->reader, proto, poll_events);

      log_reader_set_options(self->reader,
                             s,
                             &self->reader_options,
                             STATS_LEVEL1,
                             SCS_FILE,
                             self->super.super.id,
                             self->filename->str);
      /* NOTE: if the file could not be opened, we ignore the last
       * remembered file position, if the file is created in the future
       * we're going to read from the start. */
      
      log_pipe_append((LogPipe *) self->reader, s);
      if (!log_pipe_init((LogPipe *) self->reader))
        {
          msg_error("Error initializing log_reader, closing fd",
                    evt_tag_int("fd", fd),
                    NULL);
          log_pipe_unref((LogPipe *) self->reader);
          self->reader = NULL;
          close(fd);
          return FALSE;
        }
      affile_sd_recover_state(s, cfg, proto);
    }
  else
    {
      msg_error("Error opening file for reading",
                evt_tag_str("filename", self->filename->str),
                evt_tag_errno(EVT_TAG_OSERROR, errno),
                NULL);
      return self->super.super.optional;
    }
  return TRUE;

}
  static gboolean
affile_sd_init(LogPipe *s)
{
  AFFileSourceDriver *self = (AFFileSourceDriver *) s;
  GlobalConfig *cfg = log_pipe_get_config(s);
  gint fd;
  gboolean file_opened, open_deferred = FALSE;

  log_reader_options_init(&self->reader_options, cfg, self->super.group);

  file_opened = affile_sd_open_file(self, self->filename->str, &fd);
  if (!file_opened && self->reader_options.follow_freq > 0)
  {
    msg_info("Follow-mode file source not found, deferring open",
        evt_tag_str("filename", self->filename->str),
        NULL);
    open_deferred = TRUE;
    fd = -1;
  }

  if (file_opened || open_deferred)
  {
    LogTransport *transport;

    transport = log_transport_plain_new(fd, self->filename, NULL, 0);
    transport->timeout = 10;

    /* FIXME: we shouldn't use reader_options to store log protocol parameters */
    self->reader = log_reader_new(
        log_proto_plain_new_server(transport, self->reader_options.padding,
          self->reader_options.msg_size,
          ((self->reader_options.follow_freq > 0)
           ? LPPF_IGNORE_EOF
           : LPPF_NOMREAD)
          ),
        LR_LOCAL);
    log_reader_set_options(self->reader, s, &self->reader_options, 1, SCS_FILE, self->super.id, self->filename->str);

    log_reader_set_follow_filename(self->reader, self->filename->str);

    /* NOTE: if the file could not be opened, we ignore the last
     * remembered file position, if the file is created in the future
     * we're going to read from the start. */

    affile_sd_load_pos(s, cfg);
    log_pipe_append(self->reader, s);

    if (!log_pipe_init(self->reader, NULL))
    {
      msg_error("Error initializing log_reader, closing fd",
          evt_tag_int("fd", fd),
          NULL);
      log_pipe_unref(self->reader);
      self->reader = NULL;
      close(fd);
      return FALSE;
    }
  }
  else
  {
    msg_error("Error opening file for reading",
        evt_tag_str("filename", self->filename->str),
        evt_tag_errno(EVT_TAG_OSERROR, errno),
        NULL);
    return self->super.optional;
  }
  return TRUE;

}
Beispiel #10
0
static gboolean
afsocket_sc_init(LogPipe *s)
{
  AFSocketSourceConnection *self = (AFSocketSourceConnection *) s;
  gint read_flags;
  LogTransport *transport;
  LogProto *proto;

  read_flags = ((self->owner->flags & AFSOCKET_DGRAM) ? LTF_RECV : 0);
  if (!self->reader)
    {
#if ENABLE_SSL
      if (self->owner->tls_context)
        {
          TLSSession *tls_session = tls_context_setup_session(self->owner->tls_context);
          if (!tls_session)
            return FALSE;
          transport = log_transport_tls_new(tls_session, self->sock, read_flags);
        }
      else
#endif
        transport = log_transport_plain_new(self->sock, read_flags);

      if ((self->owner->flags & AFSOCKET_SYSLOG_PROTOCOL) == 0)
        {
          /* plain protocol */

          if (self->owner->flags & AFSOCKET_DGRAM)
            proto = log_proto_dgram_server_new(transport, self->owner->reader_options.msg_size, 0);
          else if (self->owner->reader_options.padding)
            proto = log_proto_record_server_new(transport, self->owner->reader_options.padding, 0);
          else
            proto = log_proto_text_server_new(transport, self->owner->reader_options.msg_size, 0);
        }
      else
        {
          if (self->owner->flags & AFSOCKET_DGRAM)
            {
              /* plain protocol */
              proto = log_proto_dgram_server_new(transport, self->owner->reader_options.msg_size, 0);
            }
          else
            {
              /* framed protocol */
              proto = log_proto_framed_server_new(transport, self->owner->reader_options.msg_size);
            }
        }

      self->reader = log_reader_new(proto);
    }
  log_reader_set_options(self->reader, s, &self->owner->reader_options, 1, afsocket_sc_stats_source(self), self->owner->super.super.id, afsocket_sc_stats_instance(self));
  log_reader_set_peer_addr(self->reader, self->peer_addr);
  log_pipe_append(self->reader, s);
  if (log_pipe_init(self->reader, NULL))
    {
      return TRUE;
    }
  else
    {
      log_pipe_unref(self->reader);
      self->reader = NULL;
    }
  return FALSE;
}
Beispiel #11
0
static gboolean
afsocket_sc_init(LogPipe *s)
{
  AFSocketSourceConnection *self = (AFSocketSourceConnection *) s;
  gint read_flags;
  LogTransport *transport;
  LogProto *proto;

  read_flags = ((self->owner->flags & AFSOCKET_DGRAM) ? LTF_RECV : 0);

#if ENABLE_SSL
  if (self->owner->tls_context)
    {
      TLSSession *tls_session = tls_context_setup_session(self->owner->tls_context);
      
      if (!tls_session)
        return FALSE;
      transport = log_transport_tls_new(tls_session, self->sock, read_flags);
    }
  else
#endif

  transport = log_transport_plain_new(self->sock, read_flags);

  if ((self->owner->flags & AFSOCKET_SYSLOG_PROTOCOL) == 0)
    {
      /* plain protocol */
      proto = log_proto_plain_new_server(transport, self->owner->reader_options.padding,
                   self->owner->reader_options.msg_size,
                   (self->owner->flags & AFSOCKET_DGRAM) ? (LPPF_PKTTERM + LPPF_IGNORE_EOF) : 0);
    }
  else
    {
      if (self->owner->flags & AFSOCKET_DGRAM)
        {
          /* plain protocol */
          proto = log_proto_plain_new_server(transport, self->owner->reader_options.padding,
                       self->owner->reader_options.msg_size,
                      (self->owner->flags & AFSOCKET_DGRAM) ? (LPPF_PKTTERM + LPPF_IGNORE_EOF) : 0);
        }
      else
        {
          /* framed protocol */
          proto = log_proto_framed_new_server(transport, self->owner->reader_options.msg_size);
        }
    }

  self->reader = log_reader_new(proto,
                                ((self->owner->flags & AFSOCKET_LOCAL) ? LR_LOCAL : 0) |
                                ((self->owner->flags & AFSOCKET_SYSLOG_PROTOCOL) ? LR_SYSLOG_PROTOCOL : 0));
  log_reader_set_options(self->reader, s, &self->owner->reader_options, 1, afsocket_sc_stats_source(self), self->owner->super.id, afsocket_sc_stats_instance(self));
  log_reader_set_peer_addr(self->reader, self->peer_addr);
  log_pipe_append(self->reader, s);
  if (log_pipe_init(self->reader, NULL))
    {
      self->owner->connections = g_list_prepend(self->owner->connections, self);
      return TRUE;
    }
  else
    {
      log_pipe_unref(self->reader);
      self->reader = NULL;
    }
  return FALSE;
}