Class: Winevt::EventLog::Channel

Inherits:
Object
  • Object
show all
Defined in:
ext/winevt/winevt_channel.c,
ext/winevt/winevt_channel.c

Overview

Retrieve Windows EventLog channel name.

Examples:

require 'winevt'
channels = []
@channel = Winevt::EventLog::Channel.new
# If users want to retrieve all channel names that
# including type of Debug and Analytical,
# it should be set as true.
@channel.force_enumerate = false
@channel.each do |channel|
  channels << channel
end
print channels

Instance Method Summary collapse

Constructor Details

#initializeObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'ext/winevt/winevt_channel.c', line 64

static VALUE
rb_winevt_channel_initialize(VALUE self)
{
  struct WinevtChannel* winevtChannel;

  TypedData_Get_Struct(
    self, struct WinevtChannel, &rb_winevt_channel_type, winevtChannel);

  winevtChannel->force_enumerate = FALSE;

  return Qnil;
}

Instance Method Details

#eachObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'ext/winevt/winevt_channel.c', line 194

static VALUE
rb_winevt_channel_each(VALUE self)
{
  EVT_HANDLE hChannels;
  EVT_HANDLE hChannelConfig = NULL;
  struct WinevtChannel* winevtChannel;
  char errBuf[256];
  LPWSTR buffer = NULL;
  DWORD bufferSize = 0;
  DWORD bufferUsed = 0;
  DWORD status = ERROR_SUCCESS;
  VALUE utf8str;

  RETURN_ENUMERATOR(self, 0, 0);

  TypedData_Get_Struct(
    self, struct WinevtChannel, &rb_winevt_channel_type, winevtChannel);

  hChannels = EvtOpenChannelEnum(NULL, 0);

  if (hChannels) {
    winevtChannel->channels = hChannels;
  } else {
    _snprintf_s(errBuf,
                _countof(errBuf),
                _TRUNCATE,
                "Failed to enumerate channels with %lu\n",
                GetLastError());
    rb_raise(rb_eRuntimeError, errBuf);
  }

  while (1) {
    if (!EvtNextChannelPath(winevtChannel->channels, bufferSize, buffer, &bufferUsed)) {
      status = GetLastError();

      if (ERROR_NO_MORE_ITEMS == status) {
        break;
      } else if (ERROR_INSUFFICIENT_BUFFER == status) {
        bufferSize = bufferUsed;
        buffer = (LPWSTR)malloc(bufferSize * sizeof(WCHAR));
        if (buffer) {
          continue;
        } else {
          free(buffer);
          EvtClose(winevtChannel->channels);
          winevtChannel->channels = NULL;
          rb_raise(rb_eRuntimeError, "realloc failed");
        }
      } else {
        free(buffer);
        EvtClose(winevtChannel->channels);
        winevtChannel->channels = NULL;
        _snprintf_s(errBuf,
                    _countof(errBuf),
                    _TRUNCATE,
                    "EvtNextChannelPath failed with %lu.\n",
                    status);
        rb_raise(rb_eRuntimeError, errBuf);
      }
    }
    hChannelConfig = EvtOpenChannelConfig(NULL, buffer, 0);
    if (NULL == hChannelConfig) {
      status = GetLastError();

      free(buffer);
      buffer = NULL;
      bufferSize = 0;

      if (is_resource_exhaustion(status)) {
        EvtClose(winevtChannel->channels);
        winevtChannel->channels = NULL;

        rb_raise(rb_eRuntimeError, "EvtOpenChannelConfig failed with %lu\n", status);
      }

      continue;
    }

    status = is_subscribable_channel_p(hChannelConfig, winevtChannel->force_enumerate);
    EvtClose(hChannelConfig);

    if (is_resource_exhaustion(status)) {
      EvtClose(winevtChannel->channels);
      winevtChannel->channels = NULL;

      free(buffer);
      buffer = NULL;
      bufferSize = 0;

      rb_raise(rb_eRuntimeError, "Failed to read channel configuration with %lu\n", status);
    }

    // Not fatal: Security's configuration cannot be read without elevation, and
    // it is the third channel EvtOpenChannelEnum returns, so raising here
    // truncated the whole enumeration to two channels.
    if (status != ERROR_SUCCESS) {
      free(buffer);
      buffer = NULL;
      bufferSize = 0;

      continue;
    }

    utf8str = wstr_to_rb_str(CP_UTF8, buffer, -1);

    free(buffer);
    buffer = NULL;
    bufferSize = 0;

    rb_yield(utf8str);
  }

  if (winevtChannel->channels) {
    EvtClose(winevtChannel->channels);
    winevtChannel->channels = NULL;
  }

  free(buffer);

  return Qnil;
}

#force_enumerateObject



104
105
106
107
108
109
110
111
112
113
# File 'ext/winevt/winevt_channel.c', line 104

static VALUE
rb_winevt_channel_get_force_enumerate(VALUE self)
{
  struct WinevtChannel* winevtChannel;

  TypedData_Get_Struct(
    self, struct WinevtChannel, &rb_winevt_channel_type, winevtChannel);

  return winevtChannel->force_enumerate ? Qtrue : Qfalse;
}

#force_enumerate=(rb_force_enumerate_p) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/winevt/winevt_channel.c', line 84

static VALUE
rb_winevt_channel_set_force_enumerate(VALUE self, VALUE rb_force_enumerate_p)
{
  struct WinevtChannel* winevtChannel;

  TypedData_Get_Struct(
    self, struct WinevtChannel, &rb_winevt_channel_type, winevtChannel);

  winevtChannel->force_enumerate = RTEST(rb_force_enumerate_p);

  return Qnil;
}