static VALUE compile(char *filename) { VALUE compiler; VALUE code; VALUE f; VALUE vfilename = rb_str_new2(filename); compile_arg_t carg; int status; if (strcmp(filename, "-") == 0) { f = rb_stdin; } else { f = rb_protect(file_open, (VALUE) filename, &status); if (status) { error(status, Qnil); } } compiler = eruby_compiler_new(); eruby_compiler_set_sourcefile(compiler, vfilename); carg.compiler = compiler; carg.input = f; code = rb_protect(eruby_compile_file, (VALUE) &carg, &status); if (status) { error(status, Qnil); } if (f != rb_stdin) rb_io_close(f); return code; }
static void rb_source_close_handler(void* sourceptr) { assert(sourceptr != NULL); rb_source_t *src = RSource(sourceptr); rb_io_close(src->handle); // Call rb_io_close directly since rb_vm_call aborts inside block // rb_vm_call(io, selClose, 0, NULL, false); }
static int MQ_IO_CLOSE(struct posix_mq *mq) { if (NIL_P(mq->io)) return 0; /* not safe during GC */ rb_io_close(mq->io); mq->io = Qnil; return 1; }
static VALUE control_block_close(VALUE cb) { rb_aiocb_t *cbs = GetCBStruct(cb); if NIL_P(cbs->io) return Qfalse; aio_return(&cbs->cb); rb_io_close(cbs->io); cbs->io = Qnil; cbs->rcb = Qnil; return Qtrue; }
/* * Closes the SOCKS connection. * */ static VALUE socks_s_close(VALUE sock) { rb_io_t *fptr; if (rb_safe_level() >= 4 && !OBJ_TAINTED(sock)) { rb_raise(rb_eSecurityError, "Insecure: can't close socket"); } GetOpenFile(sock, fptr); shutdown(fptr->fd, 2); return rb_io_close(sock); }
/* * call-seq: * basicsocket.close_read => nil * * Disallows further read using shutdown system call. * * s1, s2 = UNIXSocket.pair * s1.close_read * s2.puts #=> Broken pipe (Errno::EPIPE) */ static VALUE bsock_close_read(VALUE sock) { rb_io_t *fptr; GetOpenFile(sock, fptr); shutdown(fptr->fd, 0); if (!(fptr->mode & FMODE_WRITABLE)) { return rb_io_close(sock); } fptr->mode &= ~FMODE_READABLE; return Qnil; }
/* * call-seq: * basicsocket.close_read => nil * * Disallows further read using shutdown system call. * * s1, s2 = UNIXSocket.pair * s1.close_read * s2.puts #=> Broken pipe (Errno::EPIPE) */ static VALUE bsock_close_read(VALUE sock) { rb_io_t *fptr; if (rb_safe_level() >= 4 && !OBJ_TAINTED(sock)) { rb_raise(rb_eSecurityError, "Insecure: can't close socket"); } GetOpenFile(sock, fptr); shutdown(fptr->fd, 0); if (!(fptr->mode & FMODE_WRITABLE)) { return rb_io_close(sock); } fptr->mode &= ~FMODE_READABLE; return Qnil; }
/* * call-seq: * IO.console -> #<File:/dev/tty> * * Returns an File instance opened console. * * You must require 'io/console' to use this method. */ static VALUE console_dev(VALUE klass) { VALUE con = 0; rb_io_t *fptr; if (klass == rb_cIO) klass = rb_cFile; if (rb_const_defined(klass, id_console)) { con = rb_const_get(klass, id_console); if (RB_TYPE_P(con, T_FILE)) { if ((fptr = RFILE(con)->fptr) && GetReadFD(fptr) != -1) return con; } rb_mod_remove_const(klass, ID2SYM(id_console)); } { VALUE args[2]; #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H # define CONSOLE_DEVICE "/dev/tty" #elif defined _WIN32 # define CONSOLE_DEVICE "con$" # define CONSOLE_DEVICE_FOR_READING "conin$" # define CONSOLE_DEVICE_FOR_WRITING "conout$" #endif #ifndef CONSOLE_DEVICE_FOR_READING # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE #endif #ifdef CONSOLE_DEVICE_FOR_WRITING VALUE out; rb_io_t *ofptr; #endif int fd; #ifdef CONSOLE_DEVICE_FOR_WRITING fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_WRONLY, 0); if (fd < 0) return Qnil; rb_update_max_fd(fd); args[1] = INT2FIX(O_WRONLY); args[0] = INT2NUM(fd); out = rb_class_new_instance(2, args, klass); #endif fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0); if (fd < 0) { #ifdef CONSOLE_DEVICE_FOR_WRITING rb_io_close(out); #endif return Qnil; } rb_update_max_fd(fd); args[1] = INT2FIX(O_RDWR); args[0] = INT2NUM(fd); con = rb_class_new_instance(2, args, klass); GetOpenFile(con, fptr); #ifdef HAVE_RUBY_IO_H fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE)); #else fptr->path = ruby_strdup(CONSOLE_DEVICE); #endif #ifdef CONSOLE_DEVICE_FOR_WRITING GetOpenFile(out, ofptr); # ifdef HAVE_RB_IO_GET_WRITE_IO ofptr->pathv = fptr->pathv; fptr->tied_io_for_writing = out; # else fptr->f2 = ofptr->f; ofptr->f = 0; # endif ofptr->mode |= FMODE_SYNC; #endif fptr->mode |= FMODE_SYNC; rb_const_set(klass, id_console, con); } return con; }
/* * call-seq: * Raindrops::Linux.tcp_listener_stats([addrs[, sock]]) => hash * * If specified, +addr+ may be a string or array of strings representing * listen addresses to filter for. Returns a hash with given addresses as * keys and ListenStats objects as the values or a hash of all addresses. * * addrs = %w(0.0.0.0:80 127.0.0.1:8080) * * If +addr+ is nil or not specified, all (IPv4) addresses are returned. * If +sock+ is specified, it should be a Raindrops::InetDiagSock object. */ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self) { VALUE rv = rb_hash_new(); struct nogvl_args args; VALUE addrs, sock; rb_scan_args(argc, argv, "02", &addrs, &sock); /* * allocating page_size instead of OP_LEN since we'll reuse the * buffer for recvmsg() later, we already checked for * OPLEN <= page_size at initialization */ args.iov[2].iov_len = OPLEN; args.iov[2].iov_base = alloca(page_size); args.table = NULL; if (NIL_P(sock)) sock = rb_funcall(cIDSock, id_new, 0); args.fd = my_fileno(sock); switch (TYPE(addrs)) { case T_STRING: rb_hash_aset(rv, addrs, tcp_stats(&args, addrs)); return rv; case T_ARRAY: { long i; long len = RARRAY_LEN(addrs); VALUE cur; if (len == 1) { cur = rb_ary_entry(addrs, 0); rb_hash_aset(rv, cur, tcp_stats(&args, cur)); return rv; } for (i = 0; i < len; i++) { union any_addr check; VALUE cur = rb_ary_entry(addrs, i); parse_addr(&check, cur); rb_hash_aset(rv, cur, Qtrue); } /* fall through */ } case T_NIL: args.table = st_init_strtable(); gen_bytecode_all(&args.iov[2]); break; default: rb_raise(rb_eArgError, "addr must be an array of strings, a string, or nil"); } nl_errcheck(rb_thread_io_blocking_region(diag, &args, args.fd)); st_foreach(args.table, NIL_P(addrs) ? st_to_hash : st_AND_hash, rv); st_free_table(args.table); /* let GC deal with corner cases */ if (argc < 2) rb_io_close(sock); return rv; }
VALUE io_spec_rb_io_close(VALUE self, VALUE io) { return rb_io_close(io); }
/* * call-seq: * IO.console -> #<File:/dev/tty> * IO.console(sym, *args) * * Returns an File instance opened console. * * If +sym+ is given, it will be sent to the opened console with * +args+ and the result will be returned instead of the console IO * itself. * * You must require 'io/console' to use this method. */ static VALUE console_dev(int argc, VALUE *argv, VALUE klass) { VALUE con = 0; rb_io_t *fptr; VALUE sym = 0; rb_check_arity(argc, 0, UNLIMITED_ARGUMENTS); if (argc) { Check_Type(sym = argv[0], T_SYMBOL); } if (klass == rb_cIO) klass = rb_cFile; if (rb_const_defined(klass, id_console)) { con = rb_const_get(klass, id_console); if (!RB_TYPE_P(con, T_FILE) || (!(fptr = RFILE(con)->fptr) || GetReadFD(fptr) == -1)) { rb_const_remove(klass, id_console); con = 0; } } if (sym) { if (sym == ID2SYM(id_close) && argc == 1) { if (con) { rb_io_close(con); rb_const_remove(klass, id_console); con = 0; } return Qnil; } } if (!con) { VALUE args[2]; #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H # define CONSOLE_DEVICE "/dev/tty" #elif defined _WIN32 # define CONSOLE_DEVICE "con$" # define CONSOLE_DEVICE_FOR_READING "conin$" # define CONSOLE_DEVICE_FOR_WRITING "conout$" #endif #ifndef CONSOLE_DEVICE_FOR_READING # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE #endif #ifdef CONSOLE_DEVICE_FOR_WRITING VALUE out; rb_io_t *ofptr; #endif int fd; #ifdef CONSOLE_DEVICE_FOR_WRITING fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_RDWR, 0); if (fd < 0) return Qnil; rb_update_max_fd(fd); args[1] = INT2FIX(O_WRONLY); args[0] = INT2NUM(fd); out = rb_class_new_instance(2, args, klass); #endif fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0); if (fd < 0) { #ifdef CONSOLE_DEVICE_FOR_WRITING rb_io_close(out); #endif return Qnil; } rb_update_max_fd(fd); args[1] = INT2FIX(O_RDWR); args[0] = INT2NUM(fd); con = rb_class_new_instance(2, args, klass); GetOpenFile(con, fptr); fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE)); #ifdef CONSOLE_DEVICE_FOR_WRITING GetOpenFile(out, ofptr); ofptr->pathv = fptr->pathv; fptr->tied_io_for_writing = out; ofptr->mode |= FMODE_SYNC; #endif fptr->mode |= FMODE_SYNC; rb_const_set(klass, id_console, con); } if (sym) { return rb_f_send(argc, argv, con); } return con; }