示例#1
0
文件: load.c 项目: scorpion007/ruby
static char *
load_lock(const char *ftptr)
{
    st_data_t data;
    st_table *loading_tbl = get_loading_table();

    if (!st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
	/* partial state */
	ftptr = ruby_strdup(ftptr);
	data = (st_data_t)rb_thread_shield_new();
	st_insert(loading_tbl, (st_data_t)ftptr, data);
	return (char *)ftptr;
    }
    else if (RB_TYPE_P((VALUE)data, T_IMEMO) && imemo_type((VALUE)data) == imemo_memo) {
	struct MEMO *memo = MEMO_CAST(data);
	void (*init)(void) = (void (*)(void))memo->u3.func;
	data = (st_data_t)rb_thread_shield_new();
	st_insert(loading_tbl, (st_data_t)ftptr, data);
	(*init)();
	return (char *)"";
    }
    if (RTEST(ruby_verbose)) {
	rb_warning("loading in progress, circular require considered harmful - %s", ftptr);
	rb_backtrace_print_to(rb_stderr);
    }
    switch (rb_thread_shield_wait((VALUE)data)) {
      case Qfalse:
	data = (st_data_t)ftptr;
	st_insert(loading_tbl, data, (st_data_t)rb_thread_shield_new());
	return 0;
      case Qnil:
	return 0;
    }
    return (char *)ftptr;
}
示例#2
0
static VALUE rb_smbdir_initialize(VALUE self, VALUE smb_obj, VALUE url_obj)
{
  RB_SMBFILE_DATA_FROM_OBJ(self, data);
  RB_SMB_DATA_FROM_OBJ(smb_obj, smb_data);
  smbc_opendir_fn fn;
  const char *url = StringValueCStr(url_obj);

  fn = smbc_getFunctionOpendir(smb_data->smbcctx);
  data->smbcfile = (*fn)(smb_data->smbcctx, url);
  if (data->smbcfile == NULL) {
    rb_sys_fail_str(url_obj);
  }

  /* FIXME: Take encoding from argument */
  /* FIXME: Read unix charset (?) from smb.conf for default encoding */
  data->enc = rb_enc_find("UTF-8");

  data->smb_obj = smb_obj;
  data->smb_data = smb_data;
  data->smbcctx = smb_data->smbcctx;
  data->url = ruby_strdup(url);

  RB_SMB_DEBUG("smbcctx=%p smbcfile=%p\n", data->smbcctx, data->smbcfile);

  if (rb_block_given_p()) {
    return rb_ensure(rb_yield, self, rb_smbdir_close, self);
  }

  return self;
}
示例#3
0
文件: load.c 项目: scorpion007/ruby
static void
features_index_add_single(VALUE short_feature, VALUE offset)
{
    struct st_table *features_index;
    VALUE this_feature_index = Qnil;
    char *short_feature_cstr;

    Check_Type(offset, T_FIXNUM);
    Check_Type(short_feature, T_STRING);
    short_feature_cstr = StringValueCStr(short_feature);

    features_index = get_loaded_features_index_raw();
    st_lookup(features_index, (st_data_t)short_feature_cstr, (st_data_t *)&this_feature_index);

    if (NIL_P(this_feature_index)) {
	st_insert(features_index, (st_data_t)ruby_strdup(short_feature_cstr), (st_data_t)offset);
    }
    else if (RB_TYPE_P(this_feature_index, T_FIXNUM)) {
	VALUE feature_indexes[2];
	feature_indexes[0] = this_feature_index;
	feature_indexes[1] = offset;
	this_feature_index = (VALUE)xcalloc(1, sizeof(struct RArray));
	RBASIC(this_feature_index)->flags = T_ARRAY; /* fake VALUE, do not mark/sweep */
	rb_ary_cat(this_feature_index, feature_indexes, numberof(feature_indexes));
	st_insert(features_index, (st_data_t)short_feature_cstr, (st_data_t)this_feature_index);
    }
    else {
	Check_Type(this_feature_index, T_ARRAY);
	rb_ary_push(this_feature_index, offset);
    }
}
void
compat_init_setproctitle(int argc, char *argv[])
{
#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
	extern char **environ;
	char *lastargv = NULL;
	char *lastenvp = NULL;
	char **envp = environ;
	int i;

	/*
	 * NB: This assumes that argv has already been copied out of the
	 * way. This is true for sshd, but may not be true for other
	 * programs. Beware.
	 */

	if (argc == 0 || argv[0] == NULL)
		return;

	/* Fail if we can't allocate room for the new environment */
	for (i = 0; envp[i] != NULL; i++)
		;
	if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
		environ = envp;	/* put it back */
		return;
	}

	/*
	 * Find the last argv string or environment variable within
	 * our process memory area.
	 */
	for (i = 0; i < argc; i++) {
		if (lastargv == NULL || lastargv + 1 == argv[i])
			lastargv = argv[i] + strlen(argv[i]);
	}
	lastenvp = lastargv;
	for (i = 0; envp[i] != NULL; i++) {
		if (lastenvp + 1 == envp[i])
			lastenvp = envp[i] + strlen(envp[i]);
	}

	argv[1] = NULL;
	argv_start = argv[0];
	argv_len = lastargv - argv[0];
	argv_env_len = lastenvp - argv[0];

	for (i = 0; envp[i] != NULL; i++)
		environ[i] = ruby_strdup(envp[i]);
	environ[i] = NULL;
#endif /* SPT_REUSEARGV */
}
示例#5
0
文件: load.c 项目: scorpion007/ruby
static int
register_init_ext(st_data_t *key, st_data_t *value, st_data_t init, int existing)
{
    const char *name = (char *)*key;
    if (existing) {
	/* already registered */
	rb_warn("%s is already registered", name);
    }
    else {
	*value = (st_data_t)MEMO_NEW(0, 0, init);
	*key = (st_data_t)ruby_strdup(name);
    }
    return ST_CONTINUE;
}
示例#6
0
char *__crypt_gensalt_ra(__CONST char *prefix, unsigned long count,
	__CONST char *input, int size)
{
	char output[CRYPT_GENSALT_OUTPUT_SIZE];
	char *retval;

	retval = __crypt_gensalt_rn(prefix, count,
		input, size, output, sizeof(output));

	if (retval) {
		retval = ruby_strdup(retval);
#ifndef __GLIBC__
		/* strdup(3) on glibc sets errno, so we don't need to bother */
		if (!retval)
			__set_errno(ENOMEM);
#endif
	}

	return retval;
}
示例#7
0
static char *
load_lock(const char *ftptr)
{
    st_data_t data;
    st_table *loading_tbl = get_loading_table();

    if (!loading_tbl || !st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
	/* loading ruby library should be serialized. */
	if (!loading_tbl) {
	    GET_VM()->loading_table = loading_tbl = st_init_strtable();
	}
	/* partial state */
	ftptr = ruby_strdup(ftptr);
	data = (st_data_t)rb_thread_shield_new();
	st_insert(loading_tbl, (st_data_t)ftptr, data);
	return (char *)ftptr;
    }
    else if (RB_TYPE_P((VALUE)data, T_NODE) && nd_type((VALUE)data) == NODE_MEMO) {
	NODE *memo = RNODE(data);
	void (*init)(void) = (void (*)(void))memo->nd_cfnc;
	data = (st_data_t)rb_thread_shield_new();
	st_insert(loading_tbl, (st_data_t)ftptr, data);
	(*init)();
	return (char *)"";
    }
    if (RTEST(ruby_verbose)) {
	rb_warning("loading in progress, circular require considered harmful - %s", ftptr);
	rb_backtrace_print_to(rb_stderr);
    }
    switch (rb_thread_shield_wait((VALUE)data)) {
      case Qfalse:
	data = (st_data_t)ftptr;
	st_insert(loading_tbl, data, (st_data_t)rb_thread_shield_new());
	return 0;
      case Qnil:
	return 0;
    }
    return (char *)ftptr;
}
示例#8
0
文件: load.c 项目: Subv/Ruby-Impl
static char *
load_lock(const char *ftptr)
{
    st_data_t data;
    st_table *loading_tbl = get_loading_table();

    if (!loading_tbl || !st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
	/* loading ruby library should be serialized. */
	if (!loading_tbl) {
	    GET_VM()->loading_table = loading_tbl = st_init_strtable();
	}
	/* partial state */
	ftptr = ruby_strdup(ftptr);
	data = (st_data_t)rb_barrier_new();
	st_insert(loading_tbl, (st_data_t)ftptr, data);
	return (char *)ftptr;
    }
    if (RTEST(ruby_verbose)) {
	rb_warning("loading in progress, circular require considered harmful - %s", ftptr);
	rb_backtrace();
    }
    return RTEST(rb_barrier_wait((VALUE)data)) ? (char *)ftptr : 0;
}
示例#9
0
文件: console.c 项目: takuto-h/ruby
/*
 * 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;
}
示例#10
0
static void rb_smbfile_read_by_data(RB_SMBFILE_DATA *data)
{
  smbc_read_fn fn;
  ssize_t read_size;
  char *buffer = data->buffer + data->buffer_used_size;
  size_t buffer_size = data->buffer_size - data->buffer_used_size;

  if (buffer_size == 0) {
    /* Buffer is full */
    if (data->buffer_pos < data->buffer_used_size) {
      /* But remained data exists */
      return;
    }

    /* Rewind */
    data->buffer_used_size = 0;
    data->buffer_pos = 0;
    buffer = data->buffer;
    buffer_size = data->buffer_size;
  }

  fn = smbc_getFunctionRead(data->smbcctx);

try:
  read_size = (*fn)(data->smbcctx, data->smbcfile, buffer, buffer_size);
  if (read_size < 0) {
    if (errno != EBADF) {
      rb_sys_fail("Bad SMBCFILE");
    }
    else {
      rb_smbfile_reopen_by_data(data);
      goto try;
    }
  }

  data->buffer_used_size += read_size;
  data->eof = (read_size == 0);
}

static VALUE rb_smbfile_close(VALUE self);

static VALUE rb_smbfile_initialize(int argc, VALUE *argv, VALUE self)
{
  RB_SMBFILE_DATA_FROM_OBJ(self, data);
  VALUE smb_obj, url_obj, mode_obj;

  rb_scan_args(argc, argv, "21", &smb_obj, &url_obj, &mode_obj);
  RB_SMB_DATA_FROM_OBJ(smb_obj, smb_data);

  if (NIL_P(mode_obj)) {
    data->fmode = FMODE_READABLE;
    // FIXME data->fmode = FMODE_READABLE | DEFAULT_TEXTMODE;
    data->oflags = O_RDONLY;
  }
  else if (FIXNUM_P(mode_obj)) {
    rb_raise(rb_eArgError, "FIXME");
    data->fmode = 0;
    data->oflags = NUM2INT(mode_obj);
  }
  else {
    const char *mode_str = StringValueCStr(mode_obj);
    data->fmode = rb_io_modestr_fmode(mode_str);
    data->oflags = rb_io_modestr_oflags(mode_str);
  }

  data->smb_obj = smb_obj;
  data->smb_data = smb_data;
  data->smbcctx = smb_data->smbcctx;
  data->url = ruby_strdup(RSTRING_PTR(url_obj));

  data->buffer = ruby_xmalloc(RB_SMBFILE_BUFFER_SIZE);
  data->buffer_size = RB_SMBFILE_BUFFER_SIZE;

  rb_smbfile_open_by_data(data);

  if (rb_block_given_p()) {
    return rb_ensure(rb_yield, self, rb_smbfile_close, self);
  }

  return self;
}