VALUE kernel_spec_rb_syserr_fail(VALUE self, VALUE err, VALUE msg) { if(msg == Qnil) { rb_syserr_fail(NUM2INT(err), NULL); } else { rb_syserr_fail(NUM2INT(err), StringValuePtr(msg)); } return Qnil; }
/* * call-seq: * io.winsize = [rows, columns] * * Tries to set console size. The effect depends on the platform and * the running environment. * * You must require 'io/console' to use this method. */ static VALUE console_set_winsize(VALUE io, VALUE size) { rb_io_t *fptr; rb_console_size_t ws; #if defined _WIN32 HANDLE wh; int newrow, newcol; #endif VALUE row, col, xpixel, ypixel; const VALUE *sz; int fd; GetOpenFile(io, fptr); size = rb_Array(size); rb_check_arity(RARRAY_LENINT(size), 2, 4); sz = RARRAY_CONST_PTR(size); row = sz[0], col = sz[1], xpixel = sz[2], ypixel = sz[3]; fd = GetWriteFD(fptr); #if defined TIOCSWINSZ ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0; #define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) SET(row); SET(col); SET(xpixel); SET(ypixel); #undef SET if (!setwinsize(fd, &ws)) rb_sys_fail(0); #elif defined _WIN32 wh = (HANDLE)rb_w32_get_osfhandle(fd); #define SET(m) new##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) SET(row); SET(col); #undef SET if (!NIL_P(xpixel)) (void)NUM2UINT(xpixel); if (!NIL_P(ypixel)) (void)NUM2UINT(ypixel); if (!GetConsoleScreenBufferInfo(wh, &ws)) { rb_syserr_fail(LAST_ERROR, "GetConsoleScreenBufferInfo"); } if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) || (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) { if (!SetConsoleScreenBufferSize(wh, ws.dwSize)) { rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo"); } } ws.srWindow.Left = 0; ws.srWindow.Top = 0; ws.srWindow.Right = newcol; ws.srWindow.Bottom = newrow; if (!SetConsoleWindowInfo(wh, FALSE, &ws.srWindow)) { rb_syserr_fail(LAST_ERROR, "SetConsoleWindowInfo"); } #endif return io; }
BIO * ossl_obj2bio(VALUE obj) { BIO *bio; if (RB_TYPE_P(obj, T_FILE)) { rb_io_t *fptr; FILE *fp; int fd; GetOpenFile(obj, fptr); rb_io_check_readable(fptr); if ((fd = rb_cloexec_dup(FPTR_TO_FD(fptr))) < 0){ rb_sys_fail(0); } rb_update_max_fd(fd); if (!(fp = fdopen(fd, "r"))){ int e = errno; close(fd); rb_syserr_fail(e, 0); } if (!(bio = BIO_new_fp(fp, BIO_CLOSE))){ fclose(fp); ossl_raise(eOSSLError, NULL); } } else { StringValue(obj); bio = BIO_new_mem_buf(RSTRING_PTR(obj), RSTRING_LENINT(obj)); if (!bio) ossl_raise(eOSSLError, NULL); } return bio; }
/* * call-seq: attach(instance_id, create=true) -> self * * Attaches to the status buffer of Hashpipe * instance given by +instance_id+ * (Integer). It is an error to call attach if already attached. If +create+ * is false, an exception will be raised if the specified statsu buffer does * not exist. */ VALUE rb_hps_attach(int argc, VALUE *argv, VALUE self) { VALUE vid, vcreate; int id; VALUE vrc; hashpipe_status_t tmp, *s; rb_scan_args(argc, argv, "11", &vid, &vcreate); id = NUM2INT(vid); // Raise exception if vcreate is given and is false and specified buffer does // not exist. if(argc == 2 && !RTEST(vcreate) && !hashpipe_status_exists(id)) { rb_syserr_fail(ENOENT, "status buffer does not exist for given instance"); } Data_Get_HPStruct_Ensure_Detached(self, s); // Ensure that instance_id field is set tmp.instance_id = id; vrc = (VALUE)rb_thread_blocking_region( rb_hps_attach_blocking_func, &tmp, RUBY_UBF_PROCESS, NULL); if(RTEST(vrc)) rb_raise(rb_eRuntimeError, "could not attach to instance id %d", id); memcpy(s, &tmp, sizeof(hashpipe_status_t)); return self; }
static VALUE ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void *arg) { rb_io_t *fptr; int status = -1; int error = 0; int fd[FD_PER_IO]; conmode t[FD_PER_IO]; VALUE result = Qnil; GetOpenFile(io, fptr); fd[0] = GetReadFD(fptr); if (fd[0] != -1) { if (set_ttymode(fd[0], t+0, setter, arg)) { status = 0; } else { error = errno; fd[0] = -1; } } fd[1] = GetWriteFD(fptr); if (fd[1] != -1 && fd[1] != fd[0]) { if (set_ttymode(fd[1], t+1, setter, arg)) { status = 0; } else { error = errno; fd[1] = -1; } } if (status == 0) { result = rb_protect(func, io, &status); } GetOpenFile(io, fptr); if (fd[0] != -1 && fd[0] == GetReadFD(fptr)) { if (!setattr(fd[0], t+0)) { error = errno; status = -1; } } if (fd[1] != -1 && fd[1] != fd[0] && fd[1] == GetWriteFD(fptr)) { if (!setattr(fd[1], t+1)) { error = errno; status = -1; } } if (status) { if (status == -1) { rb_syserr_fail(error, 0); } rb_jump_tag(status); } return result; }
static VALUE console_cursor_pos(VALUE io) { rb_io_t *fptr; int fd; rb_console_size_t ws; GetOpenFile(io, fptr); fd = GetWriteFD(fptr); if (!GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), &ws)) { rb_syserr_fail(LAST_ERROR, 0); } return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.X), UINT2NUM(ws.dwCursorPosition.Y)); }
static VALUE console_info(VALUE io) { int fd = NUM2INT(rb_funcallv(io, rb_intern("fileno"), 0, 0)); HANDLE h = (HANDLE)rb_w32_get_osfhandle(fd); CONSOLE_SCREEN_BUFFER_INFO csbi; if (h == (HANDLE)-1) rb_raise(rb_eIOError, "invalid io"); if (!GetConsoleScreenBufferInfo(h, &csbi)) rb_syserr_fail(rb_w32_map_errno(GetLastError()), "not console"); return rb_struct_new(rb_cConsoleScreenBufferInfo, INT2FIX(csbi.dwSize.X), INT2FIX(csbi.dwSize.Y), INT2FIX(csbi.dwCursorPosition.X), INT2FIX(csbi.dwCursorPosition.Y), INT2FIX(csbi.wAttributes)); }
static VALUE console_goto(VALUE io, VALUE x, VALUE y) { rb_io_t *fptr; int fd; COORD pos; GetOpenFile(io, fptr); fd = GetWriteFD(fptr); pos.X = NUM2UINT(x); pos.Y = NUM2UINT(y); if (!SetConsoleCursorPosition((HANDLE)rb_w32_get_osfhandle(fd), pos)) { rb_syserr_fail(LAST_ERROR, 0); } return io; }
static VALUE init_inetsock_internal(struct inetsock_arg *arg) { int error = 0; int type = arg->type; struct addrinfo *res, *lres; int fd, status = 0, local = 0; const char *syscall = 0; arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv, SOCK_STREAM, (type == INET_SERVER) ? AI_PASSIVE : 0); /* * Maybe also accept a local address */ if (type != INET_SERVER && (!NIL_P(arg->local.host) || !NIL_P(arg->local.serv))) { arg->local.res = rsock_addrinfo(arg->local.host, arg->local.serv, SOCK_STREAM, 0); } arg->fd = fd = -1; for (res = arg->remote.res->ai; res; res = res->ai_next) { #if !defined(INET6) && defined(AF_INET6) if (res->ai_family == AF_INET6) continue; #endif lres = NULL; if (arg->local.res) { for (lres = arg->local.res->ai; lres; lres = lres->ai_next) { if (lres->ai_family == res->ai_family) break; } if (!lres) { if (res->ai_next || status < 0) continue; /* Use a different family local address if no choice, this * will cause EAFNOSUPPORT. */ lres = arg->local.res->ai; } } status = rsock_socket(res->ai_family,res->ai_socktype,res->ai_protocol); syscall = "socket(2)"; fd = status; if (fd < 0) { error = errno; continue; } arg->fd = fd; if (type == INET_SERVER) { #if !defined(_WIN32) && !defined(__CYGWIN__) status = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&status, (socklen_t)sizeof(status)); #endif status = bind(fd, res->ai_addr, res->ai_addrlen); syscall = "bind(2)"; } else { if (lres) { status = bind(fd, lres->ai_addr, lres->ai_addrlen); local = status; syscall = "bind(2)"; } if (status >= 0) { status = rsock_connect(fd, res->ai_addr, res->ai_addrlen, (type == INET_SOCKS)); syscall = "connect(2)"; } } if (status < 0) { error = errno; close(fd); arg->fd = fd = -1; continue; } else break; } if (status < 0) { VALUE host, port; if (local < 0) { host = arg->local.host; port = arg->local.serv; } else { host = arg->remote.host; port = arg->remote.serv; } rsock_syserr_fail_host_port(error, syscall, host, port); } arg->fd = -1; if (type == INET_SERVER) { status = listen(fd, SOMAXCONN); if (status < 0) { error = errno; close(fd); rb_syserr_fail(error, "listen(2)"); } } /* create new instance */ return rsock_init_sock(arg->sock, fd); }