Class: LibSSH::Channel
- Inherits:
-
Object
- Object
- LibSSH::Channel
- Defined in:
- ext/libssh_ruby/channel.c,
ext/libssh_ruby/channel.c
Overview
Wrapper for ssh_channel struct in libssh.
Instance Method Summary collapse
-
#close ⇒ nil
Close a channel.
-
#eof? ⇒ Boolean
Check if remote ha sent an EOF.
-
#get_exit_status ⇒ Fixnum?
Get the exit status of the channel.
-
#initialize(session) ⇒ Object
constructor
Initialize a channel from the session.
-
#open_session {|channel| ... } ⇒ Object
Open a session channel, and close it after the block.
-
#poll(is_stderr: false, timeout: -1) ⇒ Fixnum?
Poll a channel for data to read.
-
#read(count, is_stderr: false, timeout: -1) ⇒ String
Read data from a channel.
-
#read_nonblocking(count, is_stderr = false) ⇒ String?
Do a nonblocking read on the channel.
-
#request_exec(cmd) ⇒ nil
Run a shell command without an interactive shell.
-
#request_pty ⇒ nil
Request a PTY.
-
#send_eof ⇒ nil
Send EOF on the channel.
-
#write(data) ⇒ Fixnum
Write data on the channel.
Constructor Details
#initialize(session) ⇒ Object
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;
}
|
Instance Method Details
#close ⇒ nil
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;
}
|
#eof? ⇒ Boolean
341 342 343 344 345 346 |
# File 'ext/libssh_ruby/channel.c', line 341
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_status ⇒ Fixnum?
432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'ext/libssh_ruby/channel.c', line 432
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_session {|channel| ... } ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'ext/libssh_ruby/channel.c', line 144
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(is_stderr: false, timeout: -1) ⇒ Fixnum?
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'ext/libssh_ruby/channel.c', line 373
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, is_stderr: false, timeout: -1) ⇒ String
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 |
# File 'ext/libssh_ruby/channel.c', line 248
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?
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'ext/libssh_ruby/channel.c', line 306
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
186 187 188 189 190 191 192 193 194 195 196 |
# File 'ext/libssh_ruby/channel.c', line 186
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_pty ⇒ nil
211 212 213 214 215 216 217 218 219 220 |
# File 'ext/libssh_ruby/channel.c', line 211
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_eof ⇒ nil
494 495 496 497 498 499 500 501 502 503 |
# File 'ext/libssh_ruby/channel.c', line 494
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
467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'ext/libssh_ruby/channel.c', line 467
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);
}
|