Class: LibSSH::Channel

Inherits:
Object
  • Object
show all
Defined in:
ext/libssh_ruby/channel.c,
ext/libssh_ruby/channel.c

Overview

Wrapper for ssh_channel struct in libssh.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Object

Initialize a channel from the session.

See Also:

Since:

  • 0.1.0



66
67
68
69
70
71
72
73
74
75
76
# File 'ext/libssh_ruby/channel.c', line 66

static VALUE m_initialize(VALUE self, VALUE session) {
  ChannelHolder *holder;
  SessionHolder *session_holder;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  session_holder = libssh_ruby_session_holder(session);
  holder->channel = ssh_channel_new(session_holder->session);
  holder->session = session;

  return self;
}

Class Method Details

.select(read_channels, write_channels, except_channels, timeout) ⇒ nil

Act like the standard select(2) on channels.

See Also:

Since:

  • 0.1.0



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'ext/libssh_ruby/channel.c', line 596

static VALUE s_select(RB_UNUSED_VAR(VALUE self), VALUE read_channels,
                      VALUE write_channels, VALUE except_channels,
                      VALUE timeout) {
  struct nogvl_select_args args;
  struct timeval tv;

  if (NIL_P(timeout)) {
    args.timeout = NULL;
  } else {
    Check_Type(timeout, T_FIXNUM);
    tv.tv_sec = FIX2INT(timeout);
    tv.tv_usec = 0;
    args.timeout = &tv;
  }
  set_select_channels(&args.read_channels, read_channels);
  set_select_channels(&args.write_channels, write_channels);
  set_select_channels(&args.except_channels, except_channels);
  rb_thread_call_without_gvl(nogvl_select, &args, RUBY_UBF_IO, NULL);
  ruby_xfree(args.read_channels);
  ruby_xfree(args.write_channels);
  ruby_xfree(args.except_channels);
  return Qnil;
}

Instance Method Details

#closenil

Close a channel.

See Also:

Since:

  • 0.1.0



96
97
98
99
100
101
102
103
104
105
106
# File 'ext/libssh_ruby/channel.c', line 96

static VALUE m_close(VALUE self) {
  ChannelHolder *holder;
  struct nogvl_channel_args args;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  args.channel = holder->channel;
  rb_thread_call_without_gvl(nogvl_close, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);

  return Qnil;
}

#closed?Boolean

Check if the channel is closed or not.

See Also:

Since:

  • 0.4.0

Since:

  • 0.1.0



381
382
383
384
385
386
# File 'ext/libssh_ruby/channel.c', line 381

static VALUE m_closed_p(VALUE self) {
  ChannelHolder *holder;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  return ssh_channel_is_closed(holder->channel) ? Qtrue : Qfalse;
}

#eof?Boolean

Check if remote has sent an EOF.

See Also:

Since:

  • 0.1.0



366
367
368
369
370
371
# File 'ext/libssh_ruby/channel.c', line 366

static VALUE m_eof_p(VALUE self) {
  ChannelHolder *holder;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  return ssh_channel_is_eof(holder->channel) ? Qtrue : Qfalse;
}

#get_exit_statusFixnum?

Get the exit status of the channel.

Since:

  • 0.1.0



477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'ext/libssh_ruby/channel.c', line 477

static VALUE m_get_exit_status(VALUE self) {
  ChannelHolder *holder;
  struct nogvl_channel_args args;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  args.channel = holder->channel;
  rb_thread_call_without_gvl(nogvl_get_exit_status, &args, RUBY_UBF_IO, NULL);
  if (args.rc == -1) {
    return Qnil;
  } else {
    return INT2FIX(args.rc);
  }
}

#open?Boolean

Check if the channel is open or not.

See Also:

Since:

  • 0.4.0

Since:

  • 0.1.0



396
397
398
399
400
401
# File 'ext/libssh_ruby/channel.c', line 396

static VALUE m_open_p(VALUE self) {
  ChannelHolder *holder;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  return ssh_channel_is_open(holder->channel) ? Qtrue : Qfalse;
}

#open_forward(remote_host, remote_port) ⇒ nil

Open a TCP/IP forwarding channel.

See Also:

Since:

  • 0.4.0

Since:

  • 0.1.0



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/libssh_ruby/channel.c', line 171

static VALUE m_open_forward(VALUE self, VALUE remote_host, VALUE remote_port) {
  ChannelHolder *holder;
  struct nogvl_open_forward_args args;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  args.remote_host = StringValueCStr(remote_host);
  Check_Type(remote_port, T_FIXNUM);
  args.remote_port = FIX2INT(remote_port);
  args.channel = holder->channel;
  rb_thread_call_without_gvl(nogvl_open_forward, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);

  if (rb_block_given_p()) {
    return rb_ensure(rb_yield, Qnil, m_close, self);
  } else {
    return Qnil;
  }
}

#open_session {|channel| ... } ⇒ Object

Open a session channel, and close it after the block.

Yield Parameters:

See Also:

Since:

  • 0.1.0



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/libssh_ruby/channel.c', line 124

static VALUE m_open_session(VALUE self) {
  ChannelHolder *holder;
  struct nogvl_channel_args args;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  /* When ssh_channel_open_session is called before ssh_connect, libssh would
   * crash :-< */
  if (!ssh_is_connected(ssh_channel_get_session(holder->channel))) {
    rb_raise(rb_eArgError, "Session isn't connected");
  }
  args.channel = holder->channel;
  rb_thread_call_without_gvl(nogvl_open_session, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);

  if (rb_block_given_p()) {
    return rb_ensure(rb_yield, Qnil, m_close, self);
  } else {
    return Qnil;
  }
}

#poll(stderr: false, timeout: -1) ⇒ Fixnum?

Poll a channel for data to read.

Since:

  • 0.1.0



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'ext/libssh_ruby/channel.c', line 428

static VALUE m_poll(int argc, VALUE *argv, VALUE self) {
  ChannelHolder *holder;
  VALUE opts;
  const ID table[] = {id_stderr, id_timeout};
  VALUE kwvals[sizeof(table) / sizeof(*table)];
  struct nogvl_poll_args args;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  rb_scan_args(argc, argv, "00:", &opts);
  rb_get_kwargs(opts, table, 0, 2, kwvals);
  if (kwvals[0] == Qundef) {
    args.is_stderr = 0;
  } else {
    args.is_stderr = RTEST(kwvals[0]) ? 1 : 0;
  }
  if (kwvals[1] == Qundef) {
    args.timeout = -1;
  } else {
    Check_Type(kwvals[1], T_FIXNUM);
    args.timeout = FIX2INT(kwvals[1]);
  }

  args.channel = holder->channel;
  rb_thread_call_without_gvl(nogvl_poll, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);

  if (args.rc == SSH_EOF) {
    return Qnil;
  } else {
    return INT2FIX(args.rc);
  }
}

#read(count, stderr: false, timeout: -1) ⇒ String

Read data from a channel.

Since:

  • 0.1.0



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
# File 'ext/libssh_ruby/channel.c', line 273

static VALUE m_read(int argc, VALUE *argv, VALUE self) {
  ChannelHolder *holder;
  VALUE count, opts;
  const ID table[] = {id_stderr, id_timeout};
  VALUE kwvals[sizeof(table) / sizeof(*table)];
  struct nogvl_read_args args;
  VALUE ret;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  rb_scan_args(argc, argv, "10:", &count, &opts);
  Check_Type(count, T_FIXNUM);
  rb_get_kwargs(opts, table, 0, 2, kwvals);
  if (kwvals[0] == Qundef) {
    args.is_stderr = 0;
  } else {
    args.is_stderr = RTEST(kwvals[0]) ? 1 : 0;
  }
  if (kwvals[1] == Qundef) {
    args.timeout = -1;
  } else {
    Check_Type(kwvals[1], T_FIXNUM);
    args.timeout = FIX2INT(kwvals[1]);
  }
  args.channel = holder->channel;
  args.count = FIX2UINT(count);
  args.buf = ALLOC_N(char, args.count);
  rb_thread_call_without_gvl(nogvl_read, &args, RUBY_UBF_IO, NULL);

  ret = rb_utf8_str_new(args.buf, args.rc);
  ruby_xfree(args.buf);
  return ret;
}

#read_nonblocking(count, is_stderr = false) ⇒ String?

Do a nonblocking read on the channel.

See Also:

Since:

  • 0.3.0

Since:

  • 0.1.0



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'ext/libssh_ruby/channel.c', line 331

static VALUE m_read_nonblocking(int argc, VALUE *argv, VALUE self) {
  ChannelHolder *holder;
  VALUE count, is_stderr;
  struct nogvl_read_nonblocking_args args;
  VALUE ret;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  args.channel = holder->channel;
  rb_scan_args(argc, argv, "11", &count, &is_stderr);
  Check_Type(count, T_FIXNUM);
  args.count = FIX2UINT(count);
  if (is_stderr == Qundef) {
    args.is_stderr = 0;
  } else {
    args.is_stderr = RTEST(is_stderr) ? 1 : 0;
  }
  args.buf = ALLOC_N(char, args.count);
  rb_thread_call_without_gvl(nogvl_read_nonblocking, &args, RUBY_UBF_IO, NULL);

  if (args.rc == SSH_EOF) {
    ret = Qnil;
  } else {
    ret = rb_utf8_str_new(args.buf, args.rc);
  }
  ruby_xfree(args.buf);
  return ret;
}

#request_exec(cmd) ⇒ nil

Run a shell command without an interactive shell.

Since:

  • 0.1.0



211
212
213
214
215
216
217
218
219
220
221
# File 'ext/libssh_ruby/channel.c', line 211

static VALUE m_request_exec(VALUE self, VALUE cmd) {
  ChannelHolder *holder;
  struct nogvl_request_exec_args args;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  args.channel = holder->channel;
  args.cmd = StringValueCStr(cmd);
  rb_thread_call_without_gvl(nogvl_request_exec, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);
  return Qnil;
}

#request_ptynil

Request a PTY.

Since:

  • 0.1.0



236
237
238
239
240
241
242
243
244
245
# File 'ext/libssh_ruby/channel.c', line 236

static VALUE m_request_pty(VALUE self) {
  ChannelHolder *holder;
  struct nogvl_channel_args args;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  args.channel = holder->channel;
  rb_thread_call_without_gvl(nogvl_request_pty, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);
  return Qnil;
}

#send_eofnil

Send EOF on the channel.

Since:

  • 0.1.0



539
540
541
542
543
544
545
546
547
548
# File 'ext/libssh_ruby/channel.c', line 539

static VALUE m_send_eof(VALUE self) {
  ChannelHolder *holder;
  struct nogvl_channel_args args;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  args.channel = holder->channel;
  rb_thread_call_without_gvl(nogvl_send_eof, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);
  return Qnil;
}

#write(data) ⇒ Fixnum

Write data on the channel.

See Also:

Since:

  • 0.1.0



512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'ext/libssh_ruby/channel.c', line 512

static VALUE m_write(VALUE self, VALUE data) {
  ChannelHolder *holder;
  struct nogvl_write_args args;

  Check_Type(data, T_STRING);
  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  args.channel = holder->channel;
  args.data = RSTRING_PTR(data);
  args.len = RSTRING_LEN(data);
  rb_thread_call_without_gvl(nogvl_write, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);
  return INT2FIX(args.rc);
}