Esempio n. 1
0
static ERL_NIF_TERM
dirty_sleeper(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifPid pid;
    ErlNifEnv* msg_env = NULL;

    assert(ERL_NIF_THR_DIRTY_CPU_SCHEDULER == enif_thread_type()
	   || ERL_NIF_THR_DIRTY_IO_SCHEDULER == enif_thread_type());

    /* If we get a pid argument, it indicates a process involved in the
       test wants a message from us. Prior to the sleep we send a 'ready'
       message, and then after the sleep, send a 'done' message. */
    if (argc == 1 && enif_get_local_pid(env, argv[0], &pid))
        enif_send(env, &pid, NULL, enif_make_atom(env, "ready"));

#ifdef __WIN32__
    Sleep(2000);
#else
    sleep(2);
#endif

    if (argc == 1)
        enif_send(env, &pid, NULL, enif_make_atom(env, "done"));

    return enif_make_atom(env, "ok");
}
Esempio n. 2
0
static ERL_NIF_TERM send_list_seq(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifPid to;
    ERL_NIF_TERM msg;
    ErlNifEnv* msg_env;
    int i, res;
    
    if (!enif_get_int(env, argv[0], &i)) {
	return enif_make_badarg(env);
    }
    if (argv[1] == atom_self) {
	enif_self(env, &to);
    }
    else if (!enif_get_local_pid(env, argv[1], &to)) {
	return enif_make_badarg(env);
    }
    msg_env = enif_alloc_env();
    msg = enif_make_list(msg_env,0);
    for ( ; i>0 ; i--) {
	msg = enif_make_list_cell(msg_env, enif_make_int(msg_env, i), msg);
    }
    res = enif_send(env, &to, msg_env, msg);
    enif_free_env(msg_env);
    return enif_make_tuple2(env, atom_ok, enif_make_int(env,res));
}
Esempio n. 3
0
static nif_term_t
salt_random_bytes(nif_heap_t *hp, int argc, const nif_term_t argv[])
{
	/* salt_random_bytes(Pcb, From_pid, From_ref, Cnt) -> enqueued | congested | exiting. */
	struct salt_pcb 	*sc;
	nif_pid_t 		pid;
	nif_term_t 		ref;
	uint_t 			cnt;

	if (argc != 4)
		return (BADARG);

	/* Unpack arguments, check types. */
	if (! enif_get_resource(hp, argv[0], salt_pcb_type, (void **)&sc))
		return (BADARG);

	if (! enif_get_local_pid(hp, argv[1], &pid))
		return (BADARG);

	if (! enif_is_ref(hp, argv[2]))
		return (BADARG);
	ref = argv[2];

	/* Get requested size, make sure it's in bounds. */
	if (! enif_get_uint(hp, argv[3], &cnt))
		return (BADARG);
	if (cnt < 1 || cnt > SALT_MAX_MESSAGE_SIZE)
		return (BADARG);

	return (salt_enqueue_req(hp, sc, pid, ref, SALT_MSG_RANDOMBYTES_REQ, cnt));
}
Esempio n. 4
0
static ERL_NIF_TERM send_blob_thread(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    union { void* vp; struct make_term_info* p; }mti;
    ERL_NIF_TERM copy;
    if (!enif_get_resource(env, argv[0], msgenv_resource_type, &mti.vp)
	|| !enif_get_local_pid(env,argv[1], &mti.p->to_pid)) {
	return enif_make_badarg(env);
    }
    copy = enif_make_copy(env, mti.p->blob);

    mti.p->send_it = enif_is_identical(argv[2],atom_join);
    if (enif_thread_create("nif_SUITE:send_from_thread", &mti.p->tid,
			   threaded_sender, mti.p, NULL) != 0) {
	return enif_make_badarg(env);
    }
    if (enif_is_identical(argv[2],atom_join)) {
	int err = enif_thread_join(mti.p->tid, NULL);
	assert(err == 0);
	return enif_make_tuple3(env, atom_ok, enif_make_int(env, mti.p->send_res), copy);
    }
    else {
	enif_keep_resource(mti.vp);
	return enif_make_tuple2(env, atom_ok, copy);
    }
}
Esempio n. 5
0
File: elua.c Progetto: indie21/elua
static ERL_NIF_TERM
elua_newstate_async(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    msg_t *msg;
    ErlNifPid pid;

    if(argc != 2) {
        return enif_make_badarg(env);
    }

    // ref
    if(!enif_is_ref(env, argv[0])){
        return make_error_tuple(env, "invalid_ref");
    }

    // dest pid
    if(!enif_get_local_pid(env, argv[1], &pid)) {
        return make_error_tuple(env, "invalid_pid");
    }

    msg = msg_create();
    if(!msg) {
        return make_error_tuple(env, "command_create_failed");
    }

    msg->type = msg_newstate;
    msg->ref = enif_make_copy(msg->env, argv[0]);
    msg->pid = pid;
    msg->hold_env = env;
    msg->res=NULL;

    return push_command(env, NULL, msg);
}
Esempio n. 6
0
static ERL_NIF_TERM
_listener (ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  CAN_handle* handle;
  ErlNifPid pid = { 0 }; // NOTE: breaking opaque type!
  enif_get_resource(env, argv[0], CAN_handle_type, (void**) &handle);
  if (handle->threaded) // there is a thread already and some pid!
  {
    pid = handle->receiver;
  }
  if (!enif_get_local_pid(env, argv[1], &handle->receiver)) // NOTE: use lock if pid type is structural!
    {
      handle->threaded = 0;
      return enif_make_badarg(env);
    }
  else
    {
      enif_get_uint(env, argv[2], &handle->chunk_size);
      enif_get_long(env, argv[3], &handle->timeout);
      if (!handle->threaded) // a thread was not created already
        {
          if (enif_thread_create("can_reading_thread",
              &handle->tid,
              _reading_thread,
              handle, 0))
            {
              handle->threaded = 0;
              return enif_make_int(env, -1004);
            }
        }
    }
  return pid.pid ? enif_make_pid(env, &pid) : enif_make_int(env, 0);
}
Esempio n. 7
0
/* 
 * Open the database
 */
static ERL_NIF_TERM
esqlite_open(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    esqlite_connection *db;
    esqlite_command *cmd = NULL;
    ErlNifPid pid;
     
    if(argc != 4) 
	    return enif_make_badarg(env);     
    if(!enif_get_resource(env, argv[0], esqlite_connection_type, (void **) &db))
	    return enif_make_badarg(env);
    if(!enif_is_ref(env, argv[1])) 
	    return make_error_tuple(env, "invalid_ref");
    if(!enif_get_local_pid(env, argv[2], &pid)) 
	    return make_error_tuple(env, "invalid_pid");

    /* Note, no check is made for the type of the argument */
    cmd = command_create();
    if(!cmd) 
	    return make_error_tuple(env, "command_create_failed");

    cmd->type = cmd_open;
    cmd->ref = enif_make_copy(cmd->env, argv[1]);
    cmd->pid = pid;
    cmd->arg = enif_make_copy(cmd->env, argv[3]);

    return push_command(env, db, cmd);
}
Esempio n. 8
0
/*
 * Step to a prepared statement
 */
static ERL_NIF_TERM 
esqlite_column_names(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    esqlite_statement *stmt;
    esqlite_command *cmd = NULL;
    ErlNifPid pid;

    if(argc != 3) 
	    return enif_make_badarg(env);
    if(!enif_get_resource(env, argv[0], esqlite_statement_type, (void **) &stmt))
	    return enif_make_badarg(env);
    if(!enif_is_ref(env, argv[1])) 
	    return make_error_tuple(env, "invalid_ref");
    if(!enif_get_local_pid(env, argv[2], &pid)) 
	    return make_error_tuple(env, "invalid_pid"); 
    if(!stmt->statement) 
	    return make_error_tuple(env, "no_prepared_statement");

    cmd = command_create();
    if(!cmd) 
	    return make_error_tuple(env, "command_create_failed");

    cmd->type = cmd_column_names;
    cmd->ref = enif_make_copy(cmd->env, argv[1]);
    cmd->pid = pid;
    cmd->stmt = stmt->statement;

    if(!stmt->connection) 
	    return make_error_tuple(env, "no_connection");
    if(!stmt->connection->commands)
	    return make_error_tuple(env, "no_command_queue");

    return push_command(env, stmt->connection, cmd);
}
Esempio n. 9
0
/*
 * Prepare the sql statement
 */
static ERL_NIF_TERM 
esqlite_prepare(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    esqlite_connection *conn;
    esqlite_command *cmd = NULL;
    ErlNifPid pid;

    if(argc != 4) 
	    return enif_make_badarg(env);
    if(!enif_get_resource(env, argv[0], esqlite_connection_type, (void **) &conn))
	    return enif_make_badarg(env);
    if(!enif_is_ref(env, argv[1])) 
	    return make_error_tuple(env, "invalid_ref");
    if(!enif_get_local_pid(env, argv[2], &pid)) 
	    return make_error_tuple(env, "invalid_pid"); 

    cmd = command_create();
    if(!cmd) 
	    return make_error_tuple(env, "command_create_failed");

    cmd->type = cmd_prepare;
    cmd->ref = enif_make_copy(cmd->env, argv[1]);
    cmd->pid = pid;
    cmd->arg = enif_make_copy(cmd->env, argv[3]);

    return push_command(env, conn, cmd);
}
Esempio n. 10
0
static ERL_NIF_TERM SetPid(ErlNifEnv *env,
    int argc,
    const ERL_NIF_TERM argv[]) {
  TRACE("SetPid\n");
  if(enif_get_local_pid(env, argv[0], &server)) {
    return enif_make_atom(env, "ok");
  } else {
    return enif_make_badarg(env);
  }
}
Esempio n. 11
0
static int load(ErlNifEnv *env, void** priv_data, ERL_NIF_TERM prim_file_pid)
{
    ErlNifResourceTypeInit callbacks;

    if(!enif_get_local_pid(env, prim_file_pid, &erts_prim_file_pid)) {
        ASSERT(!"bad pid passed to prim_file_nif");
    }

    am_close = enif_make_atom(env, "close");

    am_ok = enif_make_atom(env, "ok");
    am_error = enif_make_atom(env, "error");
    am_continue = enif_make_atom(env, "continue");

    am_read = enif_make_atom(env, "read");
    am_write = enif_make_atom(env, "write");
    am_exclusive = enif_make_atom(env, "exclusive");
    am_append = enif_make_atom(env, "append");
    am_sync = enif_make_atom(env, "sync");
    am_skip_type_check = enif_make_atom(env, "skip_type_check");

    am_read_write = enif_make_atom(env, "read_write");
    am_none = enif_make_atom(env, "none");

    am_normal = enif_make_atom(env, "normal");
    am_random = enif_make_atom(env, "random");
    am_sequential = enif_make_atom(env, "sequential");
    am_will_need = enif_make_atom(env, "will_need");
    am_dont_need = enif_make_atom(env, "dont_need");
    am_no_reuse = enif_make_atom(env, "no_reuse");

    am_device = enif_make_atom(env, "device");
    am_directory = enif_make_atom(env, "directory");
    am_regular = enif_make_atom(env, "regular");
    am_symlink = enif_make_atom(env, "symlink");
    am_other = enif_make_atom(env, "other");

    am_file_info = enif_make_atom(env, "file_info");

    am_bof = enif_make_atom(env, "bof");
    am_cur = enif_make_atom(env, "cur");
    am_eof = enif_make_atom(env, "eof");

    callbacks.down = owner_death_callback;
    callbacks.dtor = NULL;
    callbacks.stop = NULL;

    efile_resource_type = enif_open_resource_type_x(env, "efile", &callbacks,
        ERL_NIF_RT_CREATE, NULL);

    *priv_data = NULL;

    return 0;
}
Esempio n. 12
0
static ERL_NIF_TERM
do_set_update_hook(ErlNifEnv *env, esqlite_connection *db, const ERL_NIF_TERM arg)
{
    if(!enif_get_local_pid(env, arg, &db->notification_pid))
	    return make_error_tuple(env, "invalid_pid");

    sqlite3_update_hook(db->db, NULL, NULL);
    if(sqlite3_update_hook(db->db, update_callback, db) != SQLITE_OK)
        return make_error_tuple(env, "sqlite3_update_hook_fail");

    return make_atom(env, "ok");
}
Esempio n. 13
0
static ERL_NIF_TERM send_term(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifEnv* menv;
    ErlNifPid pid;
    int ret;
    if (!enif_get_local_pid(env, argv[0], &pid)) {
	return enif_make_badarg(env);
    }
    menv = enif_alloc_env();
    ret = enif_send(env, &pid, menv, enif_make_copy(menv, argv[1]));
    enif_free_env(menv);
    return enif_make_int(env, ret);
}
Esempio n. 14
0
static ERL_NIF_TERM send_blob(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    union { void* vp; struct make_term_info* p; }mti;
    ErlNifPid to;
    ERL_NIF_TERM copy;
    int res;
    if (!enif_get_resource(env, argv[0], msgenv_resource_type, &mti.vp)
	|| !enif_get_local_pid(env, argv[1], &to)) {
	return enif_make_badarg(env);
    }
    copy = enif_make_copy(env, mti.p->blob);
    res = enif_send(env, &to, mti.p->dst_env, mti.p->blob);
    return enif_make_tuple3(env, atom_ok, enif_make_int(env,res), copy);
}
static ERL_NIF_TERM
send_to_pid(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    state_t* state = (state_t*) enif_priv_data(env);
    ErlNifPid* pid = (ErlNifPid*) enif_alloc(sizeof(ErlNifPid));

    if(!enif_get_local_pid(env, argv[0], pid))
    {
        return enif_make_badarg(env);
    }

    queue_push(state->queue, pid);

    return state->atom_ok;
}
Esempio n. 16
0
static void push_nif_pid(lua_State* lua, ERL_NIF_TERM message, ErlNifEnv* env)
{
	const int top = lua_gettop(lua);

	luaL_checkstack(lua, 2, ERROR_STACK_MESSAGE);

	ErlNifPid *pid = (ErlNifPid *)lua_newuserdata(lua, sizeof(ErlNifPid));
	if(enif_get_local_pid(env, message, pid))
	{
		luaL_getmetatable(lua, TYPE_ERL_PID);
		lua_setmetatable(lua, -2);
	}

	assert(lua_gettop(lua) == top+1);
}
Esempio n. 17
0
static ERL_NIF_TERM send_from_dirty_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ERL_NIF_TERM result;
    ErlNifPid pid;
    int res;

    if (!enif_get_local_pid(env, argv[0], &pid))
	return enif_make_badarg(env);
    result = enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_pid(env, &pid));
    res = enif_send(env, &pid, NULL, result);
    if (!res)
	return enif_make_badarg(env);
    else
	return result;
}
Esempio n. 18
0
ERL_NIF_TERM
snappy_decompress_impl(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    ctx_t  *ctx;
    task_t *task;

    ErlNifPid pid;

    if (argc != 4) {
        return enif_make_badarg(env);
    }

    if (!enif_get_resource(env, argv[0], res_type,
            reinterpret_cast<void**>(&ctx))) {
        return enif_make_badarg(env);
    }

    if (!enif_is_ref(env, argv[1])) {
        return enif_make_tuple2(env, atom_error,
            enif_make_string(env, "Second arg. is not a reference",
                ERL_NIF_LATIN1));
    }

    if (!enif_get_local_pid(env, argv[2], &pid)) {
        return enif_make_tuple2(env, atom_error,
            enif_make_string(env, "Third arg. is not a pid of local process",
                ERL_NIF_LATIN1));
    }

    if (!enif_is_binary(env, argv[3])) {
        return enif_make_tuple2(env, atom_error,
            enif_make_string(env, "Forth arg. is not a binary",
                ERL_NIF_LATIN1));
    }

    task = init_task(DECOMPRESS, argv[1], pid, argv[3]);

    if (!task) {
        return enif_make_tuple2(env, atom_error,
            enif_make_string(env, "Failed to create a task",
                ERL_NIF_LATIN1));
    }

    async_queue_push(ctx->queue, static_cast<void*>(task));

    return atom_ok;
}
Esempio n. 19
0
static ERL_NIF_TERM elibart_prefix_search(ErlNifEnv* env, int argc,
                                          const ERL_NIF_TERM argv[])
{
    art_tree* t;
    ErlNifBinary key;
    callback_data cb_data;

    // extract arguments atr_tree, key
    if (argc != 4)
        return enif_make_badarg(env);
    if(!enif_get_resource(env, argv[0], elibart_RESOURCE, (void**) &t))
        return enif_make_badarg(env);
    if (!enif_inspect_binary(env, argv[1], &key))
        return enif_make_badarg(env);

    cb_data.env = env;
    if(!enif_is_pid(env, argv[3]))
        return mk_error(env, "not_a_pid");

    if(!enif_get_local_pid(env, argv[3], &cb_data.pid))
        return mk_error(env, "not_a_local_pid");

    cb_data.caller_ref = argv[2];
   
    // TODO this should be a worker thread since it's a long opearation (?)
    if (art_iter_prefix(t, key.data, key.size, prefix_cb, &cb_data))
        return mk_error(env, "art_prefix_search");

    ErlNifEnv *msg_env = enif_alloc_env();

    if(msg_env == NULL)
        return mk_error(env, "env_alloc_error");;

    ERL_NIF_TERM caller_ref = enif_make_copy(msg_env, argv[2]);
    ERL_NIF_TERM res = enif_make_tuple2(msg_env, caller_ref, mk_atom(msg_env, "ok"));

    if (!enif_send(env, &cb_data.pid, msg_env, res))
    {
        enif_free(msg_env);

        return mk_error(env, "art_prefix_search");
    }

    enif_free(msg_env);

    return mk_atom(env, "ok");
}
Esempio n. 20
0
static ERL_NIF_TERM send_new_blob(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifPid to;
    ERL_NIF_TERM msg, copy;
    ErlNifEnv* msg_env;
    int res;
    
    if (!enif_get_local_pid(env, argv[0], &to)) {
	return enif_make_badarg(env);
    }
    msg_env = enif_alloc_env();
    msg = make_blob(env,msg_env, argv[1]);
    copy = make_blob(env,env, argv[1]);
    res = enif_send(env, &to, msg_env, msg);
    enif_free_env(msg_env);
    return enif_make_tuple3(env, atom_ok, enif_make_int(env,res), copy);
}
Esempio n. 21
0
File: elua.c Progetto: indie21/elua
static ERL_NIF_TERM
elua_gencall_async(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    elua_t *res;
    msg_t *msg;
    ErlNifPid pid;

    if(argc != 6) {
        return enif_make_badarg(env);
    }

    // first arg: ref
    if(!enif_get_resource(env, argv[0], RES_SYNC, (void**) &res)) {
        return enif_make_badarg(env);
    }

    // ref
    if(!enif_is_ref(env, argv[1])) {
        return make_error_tuple(env, "invalid_ref");
    }

    // dest pid
    if(!enif_get_local_pid(env, argv[2], &pid)) {
        return make_error_tuple(env, "invalid_pid");
    }

    // fourth arg: list of input args
    if(!enif_is_list(env, argv[5])) {
        return enif_make_badarg(env);
    }

    msg = msg_create();
    if(!msg) {
        return make_error_tuple(env, "command_create_failed");
    }

    msg->type = msg_gencall;
    msg->ref = enif_make_copy(msg->env, argv[1]);
    msg->pid = pid;
    msg->arg1 = enif_make_copy(msg->env, argv[3]);
    msg->arg2 = enif_make_copy(msg->env, argv[4]);
    msg->arg3 = enif_make_copy(msg->env, argv[5]);
    msg->res = res;

    return push_command(env, res, msg);
}
Esempio n. 22
0
    static ERL_NIF_TERM cqueue_deposit(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
        cqueue_handle* handle = NULL;
        int list_id = get_list_id(enif_thread_self());
        ErlNifPid pid;

        if (list_id == -1) return enif_make_badarg(env);

        if (enif_get_resource(env, argv[0], cqueue_RESOURCE, (void**)&handle) == 0) {
            return enif_make_badarg(env);
        }

        if (enif_get_local_pid(env, argv[1], &pid) == 0) {
            return enif_make_badarg(env);
        }

        handle->queue->Deposit(pid, list_id);

        return enif_make_atom(env, "ok");
    }
Esempio n. 23
0
/*
 * Multi step to a prepared statement
 */
static ERL_NIF_TERM
esqlite_multi_step(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    esqlite_connection *conn;
    esqlite_statement *stmt;
    esqlite_command *cmd = NULL;
    ErlNifPid pid;
    int chunk_size = 0;

    if(argc != 5)
        return enif_make_badarg(env);

    if(!enif_get_resource(env, argv[0], esqlite_connection_type, (void **) &conn))
        return enif_make_badarg(env);

    if(!enif_get_resource(env, argv[1], esqlite_statement_type, (void **) &stmt))
        return enif_make_badarg(env);

    if(!enif_get_int(env, argv[2], &chunk_size))
        return make_error_tuple(env, "invalid_chunk_size");

    if(!enif_is_ref(env, argv[3]))
        return make_error_tuple(env, "invalid_ref");

    if(!enif_get_local_pid(env, argv[4], &pid))
        return make_error_tuple(env, "invalid_pid");

    if(!stmt->statement)
        return make_error_tuple(env, "no_prepared_statement");

    cmd = command_create();
    if(!cmd)
        return make_error_tuple(env, "command_create_failed");

    cmd->type = cmd_multi_step;
    cmd->ref = enif_make_copy(cmd->env, argv[3]);
    cmd->pid = pid;
    cmd->stmt = enif_make_copy(cmd->env, argv[1]);
    cmd->arg = enif_make_copy(cmd->env, argv[2]);

    return push_command(env, conn, cmd);
}
Esempio n. 24
0
static ERL_NIF_TERM trace(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int state_arity;
    ErlNifPid self, to;
    ERL_NIF_TERM *tuple, msg;
    const ERL_NIF_TERM *state_tuple;
    ASSERT(argc == 5);

    enif_get_tuple(env, argv[1], &state_arity, &state_tuple);

    tuple = enif_alloc(sizeof(ERL_NIF_TERM)*(argc));
    memcpy(tuple,argv,sizeof(ERL_NIF_TERM)*argc);

    msg = enif_make_tuple_from_array(env, tuple, argc);
    enif_get_local_pid(env, state_tuple[1], &to);
    enif_send(env, &to, NULL, msg);
    enif_free(tuple);

    return atom_ok;
}
Esempio n. 25
0
    static ERL_NIF_TERM cpool_join(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
        cpool_handle* handle = NULL;
        cpool_node_handle* node;
        ErlNifPid pid;
        ERL_NIF_TERM result;

        if (enif_get_resource(env, argv[0], cpool_RESOURCE, (void**)&handle) == 0) {
            return enif_make_badarg(env);
        }

        if (enif_get_local_pid(env, argv[1], &pid) == 0) {
            return enif_make_badarg(env);
        }

        node = (cpool_node_handle*)enif_alloc_resource(cpool_node_RESOURCE, sizeof(cpool_node_handle));
        node->node = handle->pool->Join(pid);
        result  = enif_make_resource(env, (void*)node);
        enif_release_resource((void*)node);

        return result;
    }
Esempio n. 26
0
static ERL_NIF_TERM dirty_call_while_terminated_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifPid self;
    ERL_NIF_TERM result, self_term;
    ErlNifPid to;
    ErlNifEnv* menv;
    int res;

    if (!enif_get_local_pid(env, argv[0], &to))
	return enif_make_badarg(env);

    if (!enif_self(env, &self))
	return enif_make_badarg(env);

    self_term = enif_make_pid(env, &self);

    menv = enif_alloc_env();
    result = enif_make_tuple2(menv, enif_make_atom(menv, "dirty_alive"), self_term);
    res = enif_send(env, &to, menv, result);
    enif_free_env(menv);
    if (!res)
	return enif_make_badarg(env);

    /* Wait until we have been killed */
    while (enif_is_process_alive(env, &self))
	;

    result = enif_make_tuple2(env, enif_make_atom(env, "dirty_dead"), self_term);
    res = enif_send(env, &to, NULL, result);

#ifdef __WIN32__
    Sleep(1000);
#else
    sleep(1);
#endif

    return enif_make_atom(env, "ok");
}
Esempio n. 27
0
static ERL_NIF_TERM
send(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifEnv* cp_env = enif_alloc_env();
    ErlNifPid dst;
    ERL_NIF_TERM msg;
    ERL_NIF_TERM ret;
    
    if(argc == 1) {
        if(!enif_self(env, &dst)) {
            ret = enif_make_badarg(env);
            goto done;
        }
        msg = argv[0];
    } else if(argc == 2) {
        if(!enif_get_local_pid(env, argv[0], &dst)) {
            ret = enif_make_badarg(env);
            goto done;
        }
        msg = argv[1];
    } else {
        ret = enif_make_badarg(env);
        goto done;
    }

    msg = enif_make_copy(cp_env, msg);

    if(!enif_send(env, &dst, cp_env, msg)) {
        ret = enif_make_badarg(env);
        goto done;
    }
    
    ret = enif_make_atom(env, "ok");

done:
    enif_free_env(cp_env);
    return ret;
}
Esempio n. 28
0
static ERL_NIF_TERM trace(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int state_arity;
    ErlNifPid self, to;
    ERL_NIF_TERM *tuple, msg;
    ASSERT(argc == 5);

    tuple = enif_alloc(sizeof(ERL_NIF_TERM)*(argc+1));
    memcpy(tuple+1,argv,sizeof(ERL_NIF_TERM)*argc);

    if (enif_self(env, &self)) {
        tuple[0] = enif_make_pid(env, &self);
    } else {
        tuple[0] = enif_make_atom(env, "undefined");
    }

    msg = enif_make_tuple_from_array(env, tuple, argc + 1);
    enif_get_local_pid(env, argv[1], &to);
    enif_send(env, &to, NULL, msg);
    enif_free(tuple);

    return atom_ok;
}
Esempio n. 29
0
static nif_term_t
salt_sign_keypair(nif_heap_t *hp, int argc, const nif_term_t argv[])
{
	/* salt_sign_keypair(Pcb, From_pid, From_ref) -> enqueued | congested | exiting. */
	struct salt_pcb 	*sc;
	nif_pid_t 		pid;
	nif_term_t 		ref;

	if (argc != 3)
		return (BADARG);

	/* Unpack arguments, check types. */
	if (! enif_get_resource(hp, argv[0], salt_pcb_type, (void **)&sc))
		return (BADARG);

	if (! enif_get_local_pid(hp, argv[1], &pid))
		return (BADARG);

	if (! enif_is_ref(hp, argv[2]))
		return (BADARG);
	ref = argv[2];

	return (salt_enqueue_req(hp, sc, pid, ref, SALT_MSG_SIGNKEYPAIR_REQ, 0));
}
Esempio n. 30
0
static ERL_NIF_TERM
nif_scheduler_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary frameworkInfo_binary;
    ErlNifBinary credentials_binary;
    char masterUrl[MAXBUFLEN];

    state_ptr state = (state_ptr) enif_priv_data(env);
    
    if(state->initilised == 1) 
    {
        return enif_make_tuple2(env, 
            enif_make_atom(env, "state_error"), 
            enif_make_atom(env, "scheduler_already_inited"));
    }

    ErlNifPid* pid = (ErlNifPid*) enif_alloc(sizeof(ErlNifPid));

    if(!enif_get_local_pid(env, argv[0], pid))
    {
        return enif_make_tuple3(env, 
                                enif_make_atom(env, "argument_error"), 
                                enif_make_atom(env, "invalid_or_corrupted_parameter"),
                                enif_make_atom(env, "pid"));
    }

    if (!enif_inspect_binary(env, argv[1], &frameworkInfo_binary)) 
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "framework_info"));
    }

    if(!enif_get_string(env, argv[2], masterUrl , MAXBUFLEN, ERL_NIF_LATIN1 ))
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "master_info"));
    }

    if(argc == 4 )
    {
        if(!enif_inspect_binary(env,argv[3], &credentials_binary))
        {           
            return enif_make_tuple3(env, 
                        enif_make_atom(env, "argument_error"), 
                        enif_make_atom(env, "invalid_or_corrupted_parameter"),
                        enif_make_atom(env, "credential"));
            
        }
        state->scheduler_state = scheduler_init(pid, &frameworkInfo_binary, masterUrl, 1, &credentials_binary);
    }
    else
    {
        state->scheduler_state = scheduler_init(pid, &frameworkInfo_binary, masterUrl, 0, &credentials_binary);
    }
    state->initilised = 1;
    return enif_make_atom(env, "ok");
}