コード例 #1
0
ファイル: code_commands.c プロジェクト: Zabrane/smalltable
/*
   Request:
      MUST NOT have extras.
      MUST have key.
      MUST have value.
*/
ST_RES *cmd_code_load(CONN *conn, ST_REQ *req, ST_RES *res) {
	if(req->extras_sz || !req->key_sz || !req->value_sz)
		return(set_error_code(res, MEMCACHE_STATUS_INVALID_ARGUMENTS, NULL));
	
	if(CONFIG(conn)->vx32_disabled) {
		return(set_error_code(res, MEMCACHE_STATUS_UNKNOWN_COMMAND, "Command disabled by configuration"));
	}
	
	if(NULL != find_process(req->key, req->key_sz))
		return(set_error_code(res, MEMCACHE_STATUS_KEY_EXISTS, NULL));	

	int r;
	char c_file[1024];
	char elf_file[1024];
	char keyprefix[32];
	key_escape(keyprefix, sizeof(keyprefix), req->key, req->key_sz);
	keyprefix[MIN(req->key_sz, sizeof(keyprefix)-1)] = '\0'; // make it reasonably short
	snprintf(c_file, sizeof(c_file), "%s/plugin-%s.c", CONFIG(conn)->tmpdir, keyprefix);
	snprintf(elf_file, sizeof(elf_file), "%s/plugin-%s.elf", CONFIG(conn)->tmpdir, keyprefix);
	
	res->value = res->buf;
	
	r = write_file(c_file, req->value, req->value_sz);
	if(0 != r) { // never
		res->status = MEMCACHE_STATUS_ITEM_NOT_STORED;
		res->value_sz += snprintf(&res->value[res->value_sz], res->buf_sz - res->value_sz, "Error while saving file: %s", strerror(errno));
		return(res);
	}
	
	if(0 != process_compile(CONFIG(conn), c_file, elf_file, res->value, res->buf_sz, (int*)&res->value_sz)) {
		res->status = MEMCACHE_STATUS_ITEM_NOT_STORED;
		return(res);
	}
	res->value_sz = MIN(res->value_sz, 4096); // no more than 4k logs
	res->value[res->value_sz++] = '\n';
	res->value[res->value_sz++] = '\n';
	
	struct process *process = process_new(conn, req->key, req->key_sz);
	log_warn("#%p: loaded code", process);
	r = process_load(process, elf_file);
	if(0 != r) { // never
		process_free(process);
		res->value_sz += snprintf(&res->value[res->value_sz], res->buf_sz - res->value_sz, "Error while loading the binary");
		res->status = MEMCACHE_STATUS_ITEM_NOT_STORED;
		return(res);
	}
	if(0 != process_run(conn, process)) {
		process_free(process);
		res->value_sz += snprintf(&res->value[res->value_sz], res->buf_sz - res->value_sz, "Error while running the binary");
		res->status = MEMCACHE_STATUS_ITEM_NOT_STORED;
		return(res);
	}
	res->status = MEMCACHE_STATUS_OK;
	return res;
}
コード例 #2
0
ファイル: code_commands.c プロジェクト: Zabrane/smalltable
/*
   Request:
      MUST NOT have extras.
      MUST have key.
      MUST NOT have value.
*/
ST_RES *cmd_code_check(CONN *conn, ST_REQ *req, ST_RES *res) {
	if(req->extras_sz || !req->key_sz || req->value_sz)
		return(set_error_code(res, MEMCACHE_STATUS_INVALID_ARGUMENTS, NULL));
	
	if(CONFIG(conn)->vx32_disabled) {
		return(set_error_code(res, MEMCACHE_STATUS_UNKNOWN_COMMAND, "Command disabled by configuration"));
	}
	
	struct process *process = find_process(req->key, req->key_sz);
	if(NULL == process)
		return(set_error_code(res, MEMCACHE_STATUS_KEY_NOT_FOUND, NULL));
	
	return(set_error_code(res, MEMCACHE_STATUS_KEY_EXISTS, NULL));
}
コード例 #3
0
void
Ndb_move_data::set_error_code(const NdbError& ndberror)
{
  Error& e = m_error;
  set_error_code(ndberror.code, "%s", ndberror.message);
  e.ndberror = ndberror;
}
コード例 #4
0
ファイル: task.cpp プロジェクト: zjc95/rDSN
void rpc_response_task::enqueue(error_code err, message_ex* reply)
{
    set_error_code(err);
    _response = reply;

    if (nullptr != reply)
    {
        dassert(err == ERR_OK, "error code must be success when reply is present");
        reply->add_ref(); // released in dctor
    }

    if (spec().on_rpc_response_enqueue.execute(this, true))
    {
        rpc_response_task::enqueue();
    }

    // release the task when necessary
    else
    {   
        // because (1) initially, the ref count is zero
        //         (2) upper apps may call add_ref already
        this->add_ref();
        this->release_ref();
    }
}
コード例 #5
0
ファイル: task.cpp プロジェクト: goksyli/rDSN
rpc_response_task::rpc_response_task(
    message_ex* request, 
    dsn_rpc_response_handler_t cb,
    void* context, 
    dsn_task_cancelled_handler_t on_cancel,
    int hash, 
    service_node* node
    )
    : task(task_spec::get(request->local_rpc_code)->rpc_paired_code, context, on_cancel,
           hash == 0 ? request->header->client.hash : hash, node)
{
    _cb = cb;
    _is_null = (_cb == nullptr);

    set_error_code(ERR_IO_PENDING);

    dbg_dassert (TASK_TYPE_RPC_RESPONSE == spec().type, 
        "task must be of RPC_RESPONSE type, please use DEFINE_TASK_CODE_RPC to define the request task code");

    _request = request;
    _response = nullptr;

    _caller_pool = task::get_current_worker() ? 
        task::get_current_worker()->pool() : nullptr;

    _request->add_ref(); // released in dctor
}
コード例 #6
0
ファイル: task.cpp プロジェクト: shengofsun/rDSN
rpc_response_task::rpc_response_task(message_ex *request,
                                     rpc_response_handler &&cb,
                                     int hash,
                                     service_node *node)
    : task(task_spec::get(request->local_rpc_code)->rpc_paired_code,
           hash == 0 ? request->header->client.thread_hash : hash,
           node),
      _cb(std::move(cb))
{
    _is_null = (_cb == nullptr);

    set_error_code(ERR_IO_PENDING);

    dbg_dassert(TASK_TYPE_RPC_RESPONSE == spec().type,
                "%s is not of RPC_RESPONSE type, please use DEFINE_TASK_CODE_RPC to define the "
                "request task code",
                spec().name.c_str());

    _request = request;
    _response = nullptr;

    _caller_pool = get_current_worker() ? get_current_worker()->pool() : nullptr;

    _request->add_ref(); // released in dctor
}
コード例 #7
0
ファイル: exception.cpp プロジェクト: glycerine/yield
Exception::Exception(uint32_t error_code, const char* error_message)
  : error_message(NULL) {
  set_error_code(error_code);
  if (error_message != NULL) {
    set_error_message(error_message);
  }
}
コード例 #8
0
ファイル: plugin_qlist.c プロジェクト: majek/ziutek
/*
   Request:
      MUST NOT have extras.
      MUST have key.
      MUST have value.
   Ignore CAS.
*/
ST_RES *cmd_qlist_add(ST_REQ *req, ST_RES *res) {
	if(req->extras_sz || !req->key_sz || !req->value_sz) {
		return(set_error_code(res, MEMCACHE_STATUS_INVALID_ARGUMENTS));
	}
	MC_METADATA md;
	memset(&md, 0, sizeof(md));

	uint8_t *qla = buf_a;
	int qla_sz = sizeof(buf_a);
	uint8_t *qlb = (uint8_t *)req->value;
	uint8_t *qlc = buf_b;
	int qlc_sz = sizeof(buf_b);
	
	int r = storage_get(&md, (char*)qla, qla_sz, req->key, req->key_sz);
	if(r < 0){
		r = qlist_pack(qla, qla_sz, NULL, 0);
		if(r < 0) {
			set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
			goto exit;
		}
		md.flags = FLAG_QLIST;
		md.cas = ++unique_number || ++unique_number;
	}

	if( (md.flags & FLAG_QLIST) == 0) {
		set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
		goto exit;
	}
	
	r = qlist_or(qlc, qlc_sz, qla, qlb);
	if(r < 0) {
		set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
		goto exit;
	}
	
	md.cas = (md.cas+1) || (md.cas+2);
	r = storage_set(&md, (char*)qlc, r, req->key, req->key_sz);
	if(r < 0) {
		set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
		goto exit;
	}

	res->status = MEMCACHE_STATUS_OK;
	res->cas = md.cas;
exit:
	return(res);
}
コード例 #9
0
ファイル: task.cpp プロジェクト: SunnyGyb/rDSN
aio_task::aio_task(task_code code, int hash) 
    : task(code, hash)
{
    dassert (TASK_TYPE_AIO == spec().type, "task must be of AIO type, please use DEFINE_TASK_CODE_AIO to define the task code");
    set_error_code(ERR_IO_PENDING);

    _aio = node()->disk()->prepare_aio_context(this);
}
コード例 #10
0
ファイル: task.cpp プロジェクト: shengofsun/rDSN
void aio_task::enqueue(error_code err, size_t transferred_size)
{
    set_error_code(err);
    _transferred_size = transferred_size;

    spec().on_aio_enqueue.execute(this);

    task::enqueue(node()->computation()->get_pool(spec().pool_code));
}
コード例 #11
0
ファイル: task.cpp プロジェクト: SunnyGyb/rDSN
void rpc_response_task::enqueue(error_code err, message_ptr& reply)
{
    set_error_code(err);
    _response = (err == ERR_SUCCESS ? reply : nullptr);

    if (spec().on_rpc_response_enqueue.execute(this, true))
    {
        task::enqueue(_caller_pool);
    }
}
コード例 #12
0
void
Ndb_move_data::invoke_error_insert()
{
  NdbError ndberror;
  ndberror.code = 9999;
  ndberror.status = NdbError::TemporaryError;
  ndberror.message = "Error insert";
  set_error_line(0);
  set_error_code(ndberror);
  m_error_insert = false;
}
コード例 #13
0
ファイル: plugin_qlist.c プロジェクト: majek/ziutek
/*
   Request:
      MUST NOT have extras.
      MUST have key.
      MUST have value.
   Ignore CAS.
*/
ST_RES *cmd_qlist_del(ST_REQ *req, ST_RES *res) {
	if(req->extras_sz || !req->key_sz || !req->value_sz)
		return(set_error_code(res, MEMCACHE_STATUS_INVALID_ARGUMENTS));
	
	MC_METADATA md;
	memset(&md, 0, sizeof(md));

	uint8_t *qla = buf_a;
	int qla_sz = sizeof(buf_a);
	uint8_t *qlb = (uint8_t *)req->value;
	uint8_t *qlc = buf_b;
	int qlc_sz = sizeof(buf_b);

	int r = storage_get(&md, (char*)qla, qla_sz, req->key, req->key_sz);
	if(r < 0)
		goto exit_ok;
		
	if( (md.flags & FLAG_QLIST) == 0) {
		set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
		goto exit;
	}
	
	r = qlist_andnot(qlc, qlc_sz, qla, qlb);
	
	if(r == EMPTY_QLIST_SIZE) {
		storage_delete(req->key, req->key_sz);
	}else{
		md.cas = (md.cas+1) || (md.cas+2);
		r = storage_set(&md, (char*)qlc, r, req->key, req->key_sz);
		if(r < 0) {
			set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
			goto exit;
		}
	}

exit_ok:
	res->status = MEMCACHE_STATUS_OK;
	res->cas = md.cas;
exit:
	return(res);
}
コード例 #14
0
ファイル: task.cpp プロジェクト: am11/rDSN
void rpc_response_task::enqueue(error_code err, message_ex* reply)
{
    set_error_code(err);
    _response = reply;

    if (nullptr != reply)
    {
        reply->add_ref(); // released in dctor
    }

    rpc_response_task::enqueue();
}
コード例 #15
0
ファイル: task.cpp プロジェクト: SunnyGyb/rDSN
rpc_response_task::rpc_response_task(message_ptr& request, int hash)
    : task(task_spec::get(request->header().local_rpc_code)->rpc_paired_code, 
           hash == 0 ? request->header().client.hash : hash)
{
    set_error_code(ERR_IO_PENDING);

    dbg_dassert (TASK_TYPE_RPC_RESPONSE == spec().type, "task must be of RPC_RESPONSE type, please use DEFINE_TASK_CODE_RPC to define the request task code");

    _request = request;
    _caller_pool = task::get_current_worker() ? 
        task::get_current_worker()->pool() : nullptr;
}
コード例 #16
0
ファイル: api_context.cpp プロジェクト: kayceesrk/Z3
 void context::handle_exception(z3_exception & ex) {
     if (ex.has_error_code()) {
         switch(ex.error_code()) {
         case ERR_MEMOUT: 
             set_error_code(Z3_MEMOUT_FAIL);
         break;
         case ERR_PARSER: 
             set_error_code(Z3_PARSER_ERROR);
             break;
         case ERR_INI_FILE: 
             set_error_code(Z3_INVALID_ARG);
             break;
         case ERR_OPEN_FILE:
             set_error_code(Z3_FILE_ACCESS_ERROR);
             break;
         default:
             set_error_code(Z3_INTERNAL_FATAL);
             break;
         }
     }
     else {
         m_exception_msg = ex.msg();
         set_error_code(Z3_EXCEPTION); 
     }
 }
コード例 #17
0
ファイル: task.cpp プロジェクト: shengofsun/rDSN
aio_task::aio_task(dsn::task_code code, aio_handler &&cb, int hash, service_node *node)
    : task(code, hash, node), _cb(std::move(cb))
{
    _is_null = (_cb == nullptr);

    dassert(TASK_TYPE_AIO == spec().type,
            "%s is not of AIO type, please use DEFINE_TASK_CODE_AIO to define the task code",
            spec().name.c_str());
    set_error_code(ERR_IO_PENDING);

    auto disk = get_current_disk();
    _aio = disk->prepare_aio_context(this);
}
コード例 #18
0
ファイル: task.cpp プロジェクト: shengofsun/rDSN
bool rpc_response_task::enqueue(error_code err, message_ex *reply)
{
    set_error_code(err);

    if (_response != nullptr)
        _response->release_ref(); // added in previous enqueue

    _response = reply;

    if (nullptr != reply) {
        reply->add_ref(); // released in dctor
    }

    bool ret = true;
    if (!spec().on_rpc_response_enqueue.execute(this, true)) {
        set_error_code(ERR_NETWORK_FAILURE);
        ret = false;
    }

    rpc_response_task::enqueue();
    return ret;
}
コード例 #19
0
ファイル: task.cpp プロジェクト: goksyli/rDSN
void rpc_response_task::enqueue(error_code err, message_ex* reply)
{
    set_error_code(err);
    _response = reply;

    if (nullptr != reply)
    {
        dassert(err == ERR_OK, "error code must be success when reply is present");
        reply->add_ref(); // released in dctor
    }

    rpc_response_task::enqueue();
}
コード例 #20
0
ファイル: task.cpp プロジェクト: zjc95/rDSN
aio_task::aio_task(dsn_task_code_t code, dsn_aio_handler_t cb, void* param, int hash, service_node* node)
    : task(code, hash, node)
{
    _cb = cb;
    _param = param;
    _is_null = (_cb == nullptr);

    dassert (TASK_TYPE_AIO == spec().type, "task must be of AIO type, please use DEFINE_TASK_CODE_AIO to define the task code");
    set_error_code(ERR_IO_PENDING);

    auto disk = task::get_current_disk();
    if (!disk) disk = node->node_disk();
    _aio = disk->prepare_aio_context(this);
}
コード例 #21
0
ファイル: task.cpp プロジェクト: SunnyGyb/rDSN
void aio_task::enqueue(error_code err, uint32_t transferred_size, service_node* node)
{
    set_error_code(err);
    _transferred_size = transferred_size;

    spec().on_aio_enqueue.execute(this);

    if (node != nullptr)
    {
        task::enqueue(node->computation()->get_pool(spec().pool_code));
    }
    else
    {
        task::enqueue();
    }
}
コード例 #22
0
ファイル: task.cpp プロジェクト: am11/rDSN
aio_task::aio_task(
    dsn_task_code_t code, 
    dsn_aio_handler_t cb, 
    void* context, 
    dsn_task_cancelled_handler_t on_cancel,
    int hash,
    service_node* node
    )
    : task(code, context, on_cancel, hash, node)
{
    _cb = cb;
    _is_null = (_cb == nullptr);

    dassert (TASK_TYPE_AIO == spec().type, 
        "%s is not of AIO type, please use DEFINE_TASK_CODE_AIO to define the task code",
        spec().name.c_str()
        );
    set_error_code(ERR_IO_PENDING);

    auto disk = get_current_disk();
    if (!disk) disk = node->node_disk();
    _aio = disk->prepare_aio_context(this);
}
コード例 #23
0
ファイル: api_context.cpp プロジェクト: kayceesrk/Z3
 void context::check_sorts(ast * n) {
     if (!m().check_sorts(n)) {
         switch(n->get_kind()) {
         case AST_APP: {
             std::ostringstream buffer;
             app * a = to_app(n);
             buffer << mk_pp(a->get_decl(), m()) << " applied to: ";
             if (a->get_num_args() > 1) buffer << "\n";
             for (unsigned i = 0; i < a->get_num_args(); ++i) {
                 buffer << mk_bounded_pp(a->get_arg(i), m(), 3) << " of sort ";
                 buffer << mk_pp(m().get_sort(a->get_arg(i)), m()) << "\n";
             }
             warning_msg("%s",buffer.str().c_str());
             break;
         }
         case AST_VAR:
         case AST_QUANTIFIER:
         case AST_SORT:
         case AST_FUNC_DECL:
             break;
         }
         set_error_code(Z3_SORT_ERROR);
     }
 }
コード例 #24
0
ファイル: exception.cpp プロジェクト: glycerine/yield
Exception::Exception(uint32_t error_code)
  : error_message(NULL) {
  set_error_code(error_code);
}
コード例 #25
0
ファイル: exception.cpp プロジェクト: glycerine/yield
Exception::Exception()
  : error_message(NULL) {
  set_error_code(get_last_error_code());
}
コード例 #26
0
ファイル: socket_xsb.c プロジェクト: KULeuven-KRR/IDP
/* in order to save builtin numbers, create a single socket function with
 * options socket_request(SockOperation,....)  */
xsbBool xsb_socket_request(CTXTdecl)
{
  int ecode = 0;  /* error code for socket ops */
  int timeout_flag;
  SOCKET sock_handle;
  int domain, portnum;
  SOCKADDR_IN socket_addr;
  struct linger sock_linger_opt;
  int rc;
  char *message_buffer = NULL; /* initialized to keep compiler happy */
  UInteger msg_len = 0;	  /* initialized to keep compiler happy */
  char char_read;

  switch (ptoc_int(CTXTc 1)) {
  case SOCKET_ROOT: /* this is the socket() request */
    /* socket_request(SOCKET_ROOT,+domain,-socket_fd,-Error,_,_,_) 
       Currently only AF_INET domain */
    domain = (int)ptoc_int(CTXTc 2); 
    if (!translate_domain(domain, &domain)) {
      return FALSE;
    }
    
    sock_handle = socket(domain, SOCK_STREAM, IPPROTO_TCP);
	
    /* error handling */
    if (BAD_SOCKET(sock_handle)) {
      ecode = XSB_SOCKET_ERRORCODE;
      perror("SOCKET_REQUEST");
    } else {
      ecode = SOCK_OK;
    }

    ctop_int(CTXTc 3, (SOCKET) sock_handle);
	
    return set_error_code(CTXTc ecode, 4, "SOCKET_REQUEST");

  case SOCKET_BIND:
    /* socket_request(SOCKET_BIND,+domain,+sock_handle,+port,-Error,_,_) 
       Currently only supports AF_INET */
    sock_handle = (SOCKET) ptoc_int(CTXTc 3);
    portnum = (int)ptoc_int(CTXTc 4);
    domain = (int)ptoc_int(CTXTc 2);

    if (!translate_domain(domain, &domain)) {
      return FALSE;
    }
    
    /* Bind server to the agreed upon port number.
    ** See commdef.h for the actual port number. */
    FillWithZeros(socket_addr);
    socket_addr.sin_port = htons((unsigned short)portnum);
    socket_addr.sin_family = AF_INET;
#ifndef WIN_NT
    socket_addr.sin_addr.s_addr = htonl(INADDR_ANY);
#endif
    
    rc = bind(sock_handle, (PSOCKADDR) &socket_addr, sizeof(socket_addr));
	
    /* error handling */
    if (SOCKET_OP_FAILED(rc)) {
      ecode = XSB_SOCKET_ERRORCODE;
      perror("SOCKET_BIND");
    } else
      ecode = SOCK_OK;

    return set_error_code(CTXTc ecode, 5, "SOCKET_BIND");

  case SOCKET_LISTEN: 
    /* socket_request(SOCKET_LISTEN,+sock_handle,+length,-Error,_,_,_) */
    sock_handle = (SOCKET) ptoc_int(CTXTc 2);
    rc = listen(sock_handle, (int)ptoc_int(CTXTc 3));

    /* error handling */
    if (SOCKET_OP_FAILED(rc)) {
      ecode = XSB_SOCKET_ERRORCODE;
      perror("SOCKET_LISTEN");
    } else
      ecode = SOCK_OK;

    return set_error_code(CTXTc ecode, 4, "SOCKET_LISTEN");

  case SOCKET_ACCEPT:
    timeout_flag = socket_accept(CTXTc (SOCKET *)&rc, (int)pflags[SYS_TIMER]);
	  
    if (timeout_flag == TIMED_OUT) {
      return set_error_code(CTXTc TIMEOUT_ERR, 4, "SOCKET_SEND");
    } else {
      /* error handling */ 
      if (BAD_SOCKET(rc)) {
	ecode = XSB_SOCKET_ERRORCODE;
	perror("SOCKET_ACCEPT");
	sock_handle = rc; /* shut up warning */
      } else {
	sock_handle = rc; /* accept() returns sock_out */
	ecode = SOCK_OK;
      }
	       
      ctop_int(CTXTc 3, (SOCKET) sock_handle);
	       
      return set_error_code(CTXTc ecode,  4,  "SOCKET_ACCEPT");	  
    }
  case SOCKET_CONNECT: {
    /* socket_request(SOCKET_CONNECT,+domain,+sock_handle,+port,
       +hostname,-Error) */
    timeout_flag = socket_connect(CTXTc &rc, (int)pflags[SYS_TIMER]);

    if (timeout_flag == TIMED_OUT) {
      return set_error_code(CTXTc TIMEOUT_ERR, 6, "SOCKET_CONNECT");
    } else if (timeout_flag == TIMER_SETUP_ERR) {
      return set_error_code(CTXTc TIMER_SETUP_ERR, 6, "SOCKET_CONNECT");
    } else {
      /* error handling */
      if (SOCKET_OP_FAILED(rc)) {
	ecode = XSB_SOCKET_ERRORCODE;
	perror("SOCKET_CONNECT");
	/* close, because if connect() fails then socket becomes unusable */
	closesocket(ptoc_int(CTXTc 3));
      } else {
	ecode = SOCK_OK;
      }
      return set_error_code(CTXTc ecode,  6,  "SOCKET_CONNECT");
    }
  }

  case SOCKET_CLOSE: 
    /* socket_request(SOCKET_CLOSE,+sock_handle,-Error,_,_,_,_) */
    
    sock_handle = (SOCKET)ptoc_int(CTXTc 2);
    
    /* error handling */
    rc = closesocket(sock_handle);
    if (SOCKET_OP_FAILED(rc)) {
      ecode = XSB_SOCKET_ERRORCODE;
      perror("SOCKET_CLOSE");
    } else
      ecode = SOCK_OK;
    
    return set_error_code(CTXTc ecode, 3, "SOCKET_CLOSE");
    
  case SOCKET_RECV:
    /* socket_request(SOCKET_RECV,+Sockfd, -Msg, -Error,_,_,_) */
    // TODO: consider adding protection against interrupts, EINTR, like
    //       in socket_get0.
    timeout_flag = socket_recv(CTXTc &rc, &message_buffer, &msg_len, (int)pflags[SYS_TIMER]);
	  
    if (timeout_flag == TIMED_OUT) {
      return set_error_code(CTXTc TIMEOUT_ERR, 4, "SOCKET_SEND");
    } else {
      /* error handling */
      switch (rc) {
      case SOCK_OK:
	ecode = SOCK_OK;
	break;
      case SOCK_READMSG_FAILED:
	ecode = XSB_SOCKET_ERRORCODE;
	perror("SOCKET_RECV");
	break;
      case SOCK_READMSG_EOF:
	ecode = SOCK_EOF;
	break;
      case SOCK_HEADER_LEN_MISMATCH:
	ecode = XSB_SOCKET_ERRORCODE;
	break;
      default:
	xsb_abort("XSB bug: [SOCKET_RECV] invalid return code from readmsg");
      }
	       
      if (message_buffer != NULL) {
	/* use message_buffer+XSB_MSG_HEADER_LENGTH because the first
	   XSB_MSG_HEADER_LENGTH bytes are for the message length header */
	ctop_string(CTXTc 3, (char*)message_buffer+XSB_MSG_HEADER_LENGTH);
	mem_dealloc(message_buffer,msg_len,OTHER_SPACE);
      } else {  /* this happens at the end of a file */
	ctop_string(CTXTc 3, (char*)"");
      }
	       
      return set_error_code(CTXTc ecode, 4, "SOCKET_RECV");  
    }
	       
  case SOCKET_SEND:
    /* socket_request(SOCKET_SEND,+Sockfd, +Msg, -Error,_,_,_) */
    timeout_flag = socket_send(CTXTc &rc, (int)pflags[SYS_TIMER]);
    
    if (timeout_flag == TIMED_OUT) {
      return set_error_code(CTXTc TIMEOUT_ERR, 4, "SOCKET_SEND");
    } else {
      /* error handling */
      if (SOCKET_OP_FAILED(rc)) {
	ecode = XSB_SOCKET_ERRORCODE;
	perror("SOCKET_SEND");
      } else {
	ecode = SOCK_OK;
      }
      return set_error_code(CTXTc ecode,  4,  "SOCKET_SEND"); 
    }

  case SOCKET_GET0:
    /* socket_request(SOCKET_GET0,+Sockfd,-C,-Error,_,_,_) */
    message_buffer = &char_read;
    timeout_flag = socket_get0(CTXTc &rc, message_buffer, (int)pflags[SYS_TIMER]);
	  
    if (timeout_flag == TIMED_OUT) {
      return set_error_code(CTXTc TIMEOUT_ERR, 4, "SOCKET_SEND");
    } else {
      /*error handling */ 
      switch (rc) {
      case 1:
	ctop_int(CTXTc 3,(unsigned char)message_buffer[0]);
	ecode = SOCK_OK;
	break;
      case 0:
	ecode = SOCK_EOF;
	break;
      default:
	ctop_int(CTXTc 3,-1);
	perror("SOCKET_GET0");
	ecode = XSB_SOCKET_ERRORCODE;
      }
	       
      return set_error_code(CTXTc ecode,  4,  "SOCKET_GET0");
    }    
  case SOCKET_PUT:
    /* socket_request(SOCKET_PUT,+Sockfd,+C,-Error_,_,_) */
    timeout_flag = socket_put(CTXTc &rc, (int)pflags[SYS_TIMER]);
	       
    if (timeout_flag == TIMED_OUT) {
      return set_error_code(CTXTc TIMEOUT_ERR, 4, "SOCKET_SEND");
    } else {
      /* error handling */
      if (rc == 1) {
	ecode = SOCK_OK;
      } else if (SOCKET_OP_FAILED(rc)) {
	ecode = XSB_SOCKET_ERRORCODE;
	perror("SOCKET_PUT");
      }
	       
      return set_error_code(CTXTc ecode,  4,  "SOCKET_PUT");
    }
  case SOCKET_SET_OPTION: {
    /* socket_request(SOCKET_SET_OPTION,+Sockfd,+OptionName,+Value,_,_,_) */
    
    char *option_name = ptoc_string(CTXTc 3);
    
    sock_handle = (SOCKET)ptoc_int(CTXTc 2);

    /* Set the "linger" parameter to a small number of seconds */
    if (0==strcmp(option_name,"linger")) {
      int  linger_time=(int)ptoc_int(CTXTc 4);
      
      if (linger_time < 0) {
	sock_linger_opt.l_onoff = FALSE;
	sock_linger_opt.l_linger = 0;
      } else {
	sock_linger_opt.l_onoff = TRUE;
	sock_linger_opt.l_linger = linger_time;
      }
      
      if (SETSOCKOPT(sock_handle, SOL_SOCKET, SO_LINGER,
		     &sock_linger_opt, sizeof(sock_linger_opt))
	  < 0) {
	xsb_warn(CTXTc "[SOCKET_SET_OPTION] Cannot set socket linger time");
	return FALSE;
      } 
    }else {
      xsb_warn(CTXTc "[SOCKET_SET_OPTION] Invalid option, `%s'", option_name);
      return FALSE;
    }
    
    return TRUE;
  }

  case SOCKET_SET_SELECT:  {  
    /*socket_request(SOCKET_SET_SELECT,+connection_name,
      +R_sockfd,+W_sockfd,+E_sockfd) */
    prolog_term R_sockfd, W_sockfd, E_sockfd;
    int i, connection_count;
    int rmax_fd=0, wmax_fd=0, emax_fd=0; 
    char *connection_name = ptoc_string(CTXTc 2);
    
    /* bind fds to input arguments */
    R_sockfd = reg_term(CTXTc 3);
    W_sockfd = reg_term(CTXTc 4);
    E_sockfd = reg_term(CTXTc 5);	
    
    /* initialize the array of connect_t structure for select call */	
    init_connections(CTXT); 
    
    SYS_MUTEX_LOCK(MUTEX_SOCKETS);
    /* check whether the same connection name exists */
    for (i=0;i<MAXCONNECT;i++) {
      if ((connections[i].empty_flag==FALSE) &&
	  (strcmp(connection_name,connections[i].connection_name)==0)) 	
	xsb_abort("[SOCKET_SET_SELECT] Connection `%s' already exists!",
		  connection_name);
    }
    
    /* check whether there is empty slot left for connection */	
    if ((connection_count=checkslot())<MAXCONNECT) {
      if (connections[connection_count].connection_name == NULL) {
	connections[connection_count].connection_name = connection_name;
	connections[connection_count].empty_flag = FALSE;
	
	/* call the utility function separately to take the fds in */
	list_sockfd(R_sockfd, &connections[connection_count].readset,
		    &rmax_fd, &connections[connection_count].read_fds,
		    &connections[connection_count].sizer);
	list_sockfd(W_sockfd, &connections[connection_count].writeset,
		    &wmax_fd, &connections[connection_count].write_fds,
		    &connections[connection_count].sizew);
	list_sockfd(E_sockfd, &connections[connection_count].exceptionset, 
		    &emax_fd,&connections[connection_count].exception_fds,
		    &connections[connection_count].sizee);
	
	connections[connection_count].maximum_fd =
	  xsb_max(xsb_max(rmax_fd,wmax_fd), emax_fd);
      } else 
	/* if this one is reached, it is probably a bug */
	xsb_abort("[SOCKET_SET_SELECT] All connections are busy!");
    } else
      xsb_abort("[SOCKET_SET_SELECT] Max number of collections exceeded!");
    SYS_MUTEX_UNLOCK(MUTEX_SOCKETS);
    
    return TRUE;
  }
  
  case SOCKET_SELECT: {
    /* socket_request(SOCKET_SELECT,+connection_name, +timeout
       -avail_rsockfds,-avail_wsockfds,
       -avail_esockfds,-ecode)
       Returns 3 prolog_terms for available socket fds */

    prolog_term Avail_rsockfds, Avail_wsockfds, Avail_esockfds;
    prolog_term Avail_rsockfds_tail, Avail_wsockfds_tail, Avail_esockfds_tail;

    int maxfd;
    int i;       /* index for connection_count */
    char *connection_name = ptoc_string(CTXTc 2);
    struct timeval *tv;
    prolog_term timeout_term;
    int timeout =0;
    int connectname_found = FALSE;
    int count=0;			

    SYS_MUTEX_LOCK(MUTEX_SOCKETS);
    /* specify the time out */
    timeout_term = reg_term(CTXTc 3);
    if (isointeger(timeout_term)) {
      timeout = (int)oint_val(timeout_term);
      /* initialize tv */
      tv = (struct timeval *)mem_alloc(sizeof(struct timeval),LEAK_SPACE);
      tv->tv_sec = timeout;
      tv->tv_usec = 0;
    } else
      tv = NULL; /* no timeouts */

    /* initialize the prolog term */ 
    Avail_rsockfds = p2p_new(CTXT);
    Avail_wsockfds = p2p_new(CTXT);
    Avail_esockfds = p2p_new(CTXT); 

    /* bind to output arguments */
    Avail_rsockfds = reg_term(CTXTc 4);
    Avail_wsockfds = reg_term(CTXTc 5);
    Avail_esockfds = reg_term(CTXTc 6);

    Avail_rsockfds_tail = Avail_rsockfds;
    Avail_wsockfds_tail = Avail_wsockfds;
    Avail_esockfds_tail = Avail_esockfds;

    /*
      // This was wrong. Lists are now made inside test_ready()
      c2p_list(CTXTc Avail_rsockfds_tail);
      c2p_list(CTXTc Avail_wsockfds_tail);	
      c2p_list(CTXTc Avail_esockfds_tail); 
    */
    
    for (i=0; i < MAXCONNECT; i++) {
      /* find the matching connection_name to select */
      if(connections[i].empty_flag==FALSE) {
	if (strcmp(connection_name, connections[i].connection_name) == 0) {
	  connectname_found = TRUE;
	  count = i;
	  break;
	} 
      }
    }
    if( i >= MAXCONNECT )  /* if no matching connection_name */
      xsb_abort("[SOCKET_SELECT] connection `%s' doesn't exist",
		connection_name); 
    
    /* compute maxfd for select call */
    maxfd = connections[count].maximum_fd + 1;

    /* FD_SET all sockets */
    set_sockfd( CTXTc count );

    /* test whether the socket fd is available */
    rc = select(maxfd, &connections[count].readset, 
		&connections[count].writeset,
		&connections[count].exceptionset, tv);
    
    /* error handling */	
    if (rc == 0)     /* timed out */
      ecode = TIMEOUT_ERR;
    else if (SOCKET_OP_FAILED(rc)) {
      perror("SOCKET_SELECT");
      ecode = XSB_SOCKET_ERRORCODE;
    } else {      /* no error */
      ecode = SOCK_OK;
	 
      /* call the utility function to return the available socket fds */
      test_ready(CTXTc &Avail_rsockfds_tail, &connections[count].readset,
		 connections[count].read_fds,connections[count].sizer);

      test_ready(CTXTc &Avail_wsockfds_tail, &connections[count].writeset,
		 connections[count].write_fds,connections[count].sizew);

      test_ready(CTXTc &Avail_esockfds_tail,&connections[count].exceptionset,
		 connections[count].exception_fds,connections[count].sizee);
    }
    SYS_MUTEX_UNLOCK(MUTEX_SOCKETS);

    if (tv) mem_dealloc((struct timeval *)tv,sizeof(struct timeval),LEAK_SPACE);
    SQUASH_LINUX_COMPILER_WARN(connectname_found) ; 
    return set_error_code(CTXTc ecode, 7, "SOCKET_SELECT");
  }

  case SOCKET_SELECT_DESTROY:  { 
    /*socket_request(SOCKET_SELECT_DESTROY, +connection_name) */
    char *connection_name = ptoc_string(CTXTc 2);
    select_destroy(CTXTc connection_name);
    return TRUE;
  }

  default:
    xsb_warn(CTXTc "[SOCKET_REQUEST] Invalid socket request %d", (int) ptoc_int(CTXTc 1));
    return FALSE;
  }

  /* This trick would report a bug, if a newly added case
     doesn't have a return clause */
  xsb_bug("SOCKET_REQUEST case %d has no return clause", ptoc_int(CTXTc 1));
}
コード例 #27
0
ファイル: api_context.cpp プロジェクト: kayceesrk/Z3
 void context::check_searching() {
     if (m_searching) { 
         set_error_code(Z3_INVALID_USAGE); // TBD: error code could be fixed.
     } 
 }
コード例 #28
0
ファイル: exception.cpp プロジェクト: glycerine/yield
Exception::Exception(uint32_t error_code, const string& error_message)
  : error_message(NULL) {
  set_error_code(error_code);
  set_error_message(error_message);
}