Ejemplo n.º 1
0
static int lluv_poll_start(lua_State *L){
  static const lluv_uv_const_t FLAGS[] = {
    { UV_READABLE,   "readable"   },
    { UV_WRITABLE,   "writable"   },
#if LLUV_UV_VER_GE(1,9,0)
    { UV_DISCONNECT, "disconnect" },
#endif

    { 0, NULL }
  };

  lluv_handle_t *handle = lluv_check_poll(L, 1, LLUV_FLAG_OPEN);
  int events = UV_READABLE;
  int err;

  if(!lua_isfunction(L, 2))
    events = lluv_opt_flags_ui(L, 2, UV_READABLE, FLAGS);

  lluv_check_args_with_cb(L, 3);
  LLUV_START_CB(handle) = luaL_ref(L, LLUV_LUA_REGISTRY);

  err = uv_poll_start(LLUV_H(handle, uv_poll_t), events, lluv_on_poll_start);

  if(err >= 0) lluv_handle_lock(L, handle, LLUV_LOCK_START);

  return lluv_return(L, handle, LLUV_START_CB(handle), err);
}
Ejemplo n.º 2
0
static int lluv_fill_process_options_(lua_State *L){
  static const lluv_uv_const_t FLAGS[] = {
    { UV_PROCESS_SETUID,                     "setuid"   },
    { UV_PROCESS_SETGID,                     "setgid"   },
    { UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS, "verbatim" },
    { UV_PROCESS_DETACHED,                   "detached" },
    { UV_PROCESS_WINDOWS_HIDE,               "hide"     },

    { 0, NULL }
  };

  uv_process_options_t *opt = (uv_process_options_t *)lua_touserdata(L, 2);
  unsigned int flags = opt->flags;

  lua_settop(L, 1);

  opt->exit_cb = lluv_on_process_exit;

  if(lua_isstring(L, 1)){
    opt->file = (char*) lua_tostring(L, 1);
    return 1;
  }

  luaL_checktype(L, 1, LUA_TTABLE);

  opt->file  =           opt_get_string(L, 1, "file",   1, "file option required and must be a string");
  opt->cwd   =           opt_get_string(L, 1, "cwd",    0, "cwd option must be a string");
  opt->args  =           opt_get_sarray(L, 1, "args",   0, (char*)opt->file, "args option must be an array");
  opt->env   =           opt_get_sarray(L, 1, "env",    0, NULL, "env option must be an array");
  opt->uid   = (uv_uid_t)opt_get_int64 (L, 1, "uid",    0, "uid option must be a number"   );
  opt->gid   = (uv_gid_t)opt_get_int64 (L, 1, "gid",    0, "gid option must be a number"   );

  if(opt_exists(L, 1, "uid")) flags |= UV_PROCESS_SETUID;
  if(opt_exists(L, 1, "gid")) flags |= UV_PROCESS_SETGID;

  rawgets(L, 1, "flags");
  opt->flags = flags | lluv_opt_flags_ui(L, -1, 0, FLAGS);
  lua_pop(L, 1);

  opt_get_stdio(L, 1, opt);

  lua_settop(L, 1);
  return 1;
}
Ejemplo n.º 3
0
static int lluv_tcp_bind(lua_State *L){
  static const lluv_uv_const_t FLAGS[] = {
    { UV_TCP_IPV6ONLY ,   "ipv6only"   },

    { 0, NULL }
  };

  lluv_handle_t  *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  struct sockaddr_storage sa; int err = lluv_check_addr(L, 2, &sa);
  unsigned int flags = 0;
  int top = lua_gettop(L);
  if(top > 5)lua_settop(L, top = 5);

  if((top > 4) || (!lua_isfunction(L, 4))){
    flags = lluv_opt_flags_ui(L, 4, flags, FLAGS);
  }

  if(err < 0){
    lua_checkstack(L, 3);

    lua_pushvalue(L, 2); lua_pushliteral(L, ":"); lua_pushvalue(L, 3); lua_concat(L, 3);

    if(!lua_isfunction(L, top)){
      return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, lua_tostring(L, -1));
    }

    lluv_error_create(L, LLUV_ERR_UV, err, lua_tostring(L, -1));
    lua_remove(L, -2);
    lua_pushvalue(L, 1);
    lua_insert(L, -2);
    lluv_loop_defer_call(L, lluv_loop_by_handle(&handle->handle), 2);
    lua_settop(L, 1);
    return 1;
  }

  err = uv_tcp_bind(LLUV_H(handle, uv_tcp_t), (struct sockaddr *)&sa, flags);
  if(err < 0){
    lua_checkstack(L, 3);

    lua_pushvalue(L, 2); lua_pushliteral(L, ":"); lua_pushvalue(L, 3); lua_concat(L, 3);

    if(!lua_isfunction(L, top)){
      return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, lua_tostring(L, -1));
    }

    lluv_error_create(L, LLUV_ERR_UV, err, lua_tostring(L, -1));
    lua_remove(L, -2);
    lua_pushvalue(L, 1);
    lua_insert(L, -2);
    lluv_loop_defer_call(L, lluv_loop_by_handle(&handle->handle), 2);
    lua_settop(L, 1);
    return 1;
  }

  if(lua_isfunction(L, top)){
    lua_pushvalue(L, 1);
    lua_pushnil(L);
    lluv_loop_defer_call(L,
      lluv_loop_by_handle(&handle->handle),
      lluv_push_addr(L, &sa) + 2
    );
  }

  lua_settop(L, 1);
  return 1;
}
Ejemplo n.º 4
0
static void opt_get_stdio(lua_State *L, int idx, uv_process_options_t *opt){
  static const lluv_uv_const_t FLAGS[] = {
    { UV_IGNORE,         "ignore"         },
    { UV_CREATE_PIPE,    "create_pipe"    },
    { UV_INHERIT_FD,     "inherit_fd"     },
    { UV_INHERIT_STREAM, "inherit_stream" },
    { UV_READABLE_PIPE,  "readable_pipe"  },
    { UV_WRITABLE_PIPE,  "writable_pipe"  },

    { 0, NULL }
  };

  size_t i, n;

  rawgets(L, idx, "stdio");
  if(lua_isnil(L, -1)){
    lua_settop(L, 1);
    return;
  }

  if(!lua_istable(L, -1)){
    lua_pop(L, 1);
    lua_pushstring(L, "stdio option must be an array");
    lua_error(L);
    return;
  }

  n = lua_objlen(L, -1);

  if(n == 0){
    lua_pop(L, 1);
    return;
  }

  opt->stdio = lluv_alloc(L, n * sizeof(*opt->stdio));
  opt->stdio_count = n;

  for(i = 0; i < n; ++i){
    lua_rawgeti(L, -1, i + 1);

    if(lua_istable(L, -1)){
      uv_stdio_flags flags = 0;

      if(opt_exists(L, -1, "fd")){
        opt->stdio[i].data.fd = (int)opt_get_int64 (L, -1, "fd",  0, "stdio.fd option must be a number" );
        flags = UV_INHERIT_FD;
      }
      else if(opt_exists(L, -1, "stream")){
        lluv_handle_t *handle;
        rawgets(L, -1, "stream");
        handle = lluv_check_stream(L, -1, LLUV_FLAG_OPEN);
        lua_pop(L, 1);
        opt->stdio[i].data.stream = LLUV_H(handle, uv_stream_t);
        flags = UV_INHERIT_STREAM;
      }
      else{
        opt->stdio[i].data.fd = 0;
        flags = UV_IGNORE;
      }

      if(opt_exists(L, -1, "flags")){
        rawgets(L, -1, "flags");
        flags = lluv_opt_flags_ui(L, -1, 0, FLAGS);
        lua_pop(L, 1);
        // flags = opt_get_int64 (L, -1, "flags",  0, "stdio.flags option must be a number" );
      }

      opt->stdio[i].flags = flags;
    }
    else if(lua_isnumber(L, -1)){
      opt->stdio[i].data.fd = (int)lutil_checkint64(L, -1);
      opt->stdio[i].flags = UV_INHERIT_FD;
    }
    else if(lua_isuserdata(L, -1)){
      lluv_handle_t *handle = lluv_check_stream(L, -1, LLUV_FLAG_OPEN);
      opt->stdio[i].data.stream = LLUV_H(handle, uv_stream_t);
      opt->stdio[i].flags = UV_INHERIT_STREAM;
    }
    else{
      lua_pushstring(L, "stdio element must be table, stream or number");
      lua_error(L);
      return;
    }

    lua_pop(L, 1);
  }
  lua_pop(L, 1);
}