Beispiel #1
0
Val   _lib7_Sock_frominetaddr   (Task* task,  Val arg)   {
    //=======================
    //
    // Mythryl type:   Internet_Address -> (Internet_Address, Int)
    //
    // Given a INET-domain socket address, return the INET address and port number.
    //
    // This fn gets bound as   from_inet_addr   in:
    //
    //     src/lib/std/src/socket/internet-socket.pkg

										ENTER_MYTHRYL_CALLABLE_C_FN("_lib7_Sock_frominetaddr");

    struct sockaddr_in*	addr
        =
        GET_VECTOR_DATACHUNK_AS( struct sockaddr_in*, arg );			// Last use of 'arg'.

    ASSERT( addr->sin_family == AF_INET );

    Val data   =  make_biwordslots_vector_sized_in_bytes__may_heapclean( task, &(addr->sin_addr), sizeof(struct in_addr), NULL );
    Val inAddr =  make_vector_header( task,  UNT8_RO_VECTOR_TAGWORD, data,  sizeof(struct in_addr) );

    return  make_two_slot_record(task, inAddr, TAGGED_INT_FROM_C_INT(ntohs(addr->sin_port)) );
}
Beispiel #2
0
Val   _lib7_P_ProcEnv_getpgrp   (Task* task,  Val arg)   {
    //=======================
    //
    // Mythryl type:   Void -> Int
    //
    // Return process group.
    //
    // This fn gets bound as   get_process_group   in:
    //
    //     src/lib/std/src/psx/posix-id.pkg

									    ENTER_MYTHRYL_CALLABLE_C_FN(__func__);

    RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, NULL );
	//
	int pgrp = getpgrp();
	//
    RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );

    Val result = TAGGED_INT_FROM_C_INT( pgrp );

									    EXIT_MYTHRYL_CALLABLE_C_FN(__func__);
    return result;
}
Beispiel #3
0
Val   _lib7_P_ProcEnv_setgid   (Task* task,  Val arg)   {
    //======================
    //
    // Mythryl type:  Unt -> Void
    //
    // Set group id.
    //
    // This fn gets bound as   set_group_id   in:
    //
    //     src/lib/std/src/psx/posix-id.pkg

									    ENTER_MYTHRYL_CALLABLE_C_FN(__func__);

    RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, &arg );
	//
	int status =  setgid( WORD_LIB7toC( arg ));
	//
    RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );

    Val result =  RETURN_VOID_EXCEPT_RAISE_SYSERR_ON_NEGATIVE_STATUS__MAY_HEAPCLEAN(task, status, NULL);

									    EXIT_MYTHRYL_CALLABLE_C_FN(__func__);
    return result;
}
Beispiel #4
0
Val   _lib7_netdb_get_host_by_address   (Task* task,  Val arg)   {
    //===============================
    //
    // Mythryl type:   Internet_Address -> Null_Or(  (String, List(String), Raw_Address_Family, List(Internet_Address))  )
    //
    // This fn gets bound as   get_host_by_addr'   in:
    //
    //     src/lib/std/src/socket/dns-host-lookup.pkg

															ENTER_MYTHRYL_CALLABLE_C_FN("_lib7_netdb_get_host_by_address");

    ASSERT (sizeof(struct in_addr) == GET_VECTOR_LENGTH( arg ));

    struct in_addr*  heap_arg =  (struct in_addr*) HEAP_STRING_AS_C_STRING( arg );					// Last use of 'arg'.
    struct in_addr      c_arg = *heap_arg;

    RELEASE_MYTHRYL_HEAP( task->pthread, "_lib7_netdb_get_host_by_address", NULL );
	//
	struct hostent* result = gethostbyaddr (&c_arg, sizeof(struct in_addr), AF_INET);
	//
    RECOVER_MYTHRYL_HEAP( task->pthread, "_lib7_netdb_get_host_by_address" );

    return  _util_NetDB_mkhostent ( task, result );									// _util_NetDB_mkhostent	def in    src/c/lib/socket/util-mkhostent.c
}
Beispiel #5
0
Val   _lib7_Sock_recvbuffrom   (Task* task,  Val arg)   {
    //======================
    //
    // Mythryl type:   (Socket, rw_vector_of_one_byte_unts::Rw_Vector, Int, Int, Bool, Bool) -> (Int, Addr)
    //
    // The arguments are:
    //     socket,
    //     data buffer,
    //     start position,
    //     number of bytes,
    //     OOB flag
    //     peek flag.
    //
    // The result is:
    //     number of bytes read
    //     source address.
    //
    // This fn gets bound as   recv_from_a   in:
    //
    //     src/lib/std/src/socket/socket-guts.pkg

										ENTER_MYTHRYL_CALLABLE_C_FN(__func__);

    char       address_buf[  MAX_SOCK_ADDR_BYTESIZE ];
    socklen_t  address_len = MAX_SOCK_ADDR_BYTESIZE;

    int	  socket  = GET_TUPLE_SLOT_AS_INT( arg, 0);
//  Val   buf     = GET_TUPLE_SLOT_AS_VAL( arg, 1);				// Mythryl buffer to read bytes into.	// We'll fetch this after the call, since it may move around during the call.
    int   offset  = GET_TUPLE_SLOT_AS_INT( arg, 2);				// Offset within buf to read bytes into.
    int	  nbytes  = GET_TUPLE_SLOT_AS_INT( arg, 3);				// Number of bytes to read.

    int	  flag = 0;

    int	   n;

    if (GET_TUPLE_SLOT_AS_VAL(arg, 4) == HEAP_TRUE) flag |= MSG_OOB;
    if (GET_TUPLE_SLOT_AS_VAL(arg, 5) == HEAP_TRUE) flag |= MSG_PEEK;

    // We cannot reference anything on the Mythryl heap
    // between RELEASE_MYTHRYL_HEAP and RECOVER_MYTHRYL_HEAP
    // because garbage collection might be moving
    // it around, so allocate a C-side read buffer:
    //
    Mythryl_Heap_Value_Buffer  readbuf_buf;
    //
    {   char* c_readbuf =  buffer_mythryl_heap_nonvalue( &readbuf_buf, nbytes );

	RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, &arg );		// 'arg' is still live here!
	    //
	    do {
		//
		n = recvfrom( socket,
			      c_readbuf,
			      nbytes,
			      flag,
			      (struct sockaddr *)address_buf,
			      &address_len
			    );
	    } while (n < 0 && errno == EINTR);					// Restart if interrupted by a SIGALRM or SIGCHLD or whatever.
	    //
	RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );

	if (n < 0) {
	    unbuffer_mythryl_heap_value( &readbuf_buf );
	    return RAISE_SYSERR__MAY_HEAPCLEAN(task, status, NULL);
	}

	Val   buf      =  GET_TUPLE_SLOT_AS_VAL( arg, 1);			// Mythryl buffer to read bytes into.
	char* bufstart =  HEAP_STRING_AS_C_STRING(buf) + offset;

	memcpy( bufstart, c_readbuf, n);

	unbuffer_mythryl_heap_value( &readbuf_buf );
    }

    Val	data    =  make_biwordslots_vector_sized_in_bytes__may_heapclean(   task, address_buf,                   address_len, NULL );
    Val address =  make_vector_header(					    task,  UNT8_RO_VECTOR_TAGWORD, data, address_len);

    Val result  =  make_two_slot_record(task,  TAGGED_INT_FROM_C_INT(n), address);

										EXIT_MYTHRYL_CALLABLE_C_FN(__func__);
    return result;
}
Beispiel #6
0
Val   _lib7_P_FileSys_utime   (Task* task,  Val arg)   {
    //=====================
    //
    // Mythryl type: (String, one_word_int::Int, one_word_int::Int) -> Void
    //                name    actime      modtime
    //
    // Sets file access and modification times.
    // If actime = -1, then set both to current time.
    //
    // This fn gets bound as   utime'   in:
    //
    //     src/lib/std/src/psx/posix-file.pkg
    //     src/lib/std/src/psx/posix-file-system-64.pkg

									    ENTER_MYTHRYL_CALLABLE_C_FN(__func__);

    int status;

    Val	    path    =  GET_TUPLE_SLOT_AS_VAL( arg, 0);
    time_t  actime  =  TUPLE_GET_INT1(        arg, 1);
    time_t  modtime =  TUPLE_GET_INT1(        arg, 2);

    char* heap_path =  HEAP_STRING_AS_C_STRING( path );

    // We cannot reference anything on the Mythryl
    // heap between RELEASE_MYTHRYL_HEAP and RECOVER_MYTHRYL_HEAP
    // because garbage collection might be moving
    // it around, so copy heap_path into C storage: 
    //
    Mythryl_Heap_Value_Buffer  path_buf;
    //
    {	char* c_path
	    = 
	    buffer_mythryl_heap_value( &path_buf, (void*) heap_path, strlen( heap_path ) +1 );		// '+1' for terminal NUL on string.


	if (actime == -1) {

	    RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, NULL );
		//
		status = utime( c_path, NULL );
		//
	    RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );

	} else {

	    struct utimbuf tb;

	    tb.actime = actime;
	    tb.modtime = modtime;

	    RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, NULL );
		//
		status = utime( c_path, &tb );
		//
	    RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );
	}

	unbuffer_mythryl_heap_value( &path_buf );
    }

    Val result = RETURN_VOID_EXCEPT_RAISE_SYSERR_ON_NEGATIVE_STATUS__MAY_HEAPCLEAN(task, status, NULL);

									    EXIT_MYTHRYL_CALLABLE_C_FN(__func__);
    return result;
}
Val   get_or_set_socket_linger_option   (Task* task,  Val arg)   {
    //===============================
    //
    // Mythryl type: (Socket_Fd, Null_Or(Null_Or(Int))) -> Null_Or(Int)
    //
    // Set/get the SO_LINGER option as follows:
    //   NULL		=> get current setting
    //   THE(NULL)	=> disable linger
    //   THE(THE t)	=> enable linger with timeout t.
    //
    // This function gets bound as   ctl_linger   in:
    //
    //     src/lib/std/src/socket/socket-guts.pkg
    //

													ENTER_MYTHRYL_CALLABLE_C_FN(__func__);

    int  socket = GET_TUPLE_SLOT_AS_INT( arg, 0 );
    Val	    ctl = GET_TUPLE_SLOT_AS_VAL( arg, 1 );							// Last use of 'arg'.

    struct linger   optVal;
    int		    status;

    if (ctl == OPTION_NULL) {
        //
	socklen_t  optSz =  sizeof( struct linger );

	RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, NULL );
	    //
	    status =  getsockopt( socket, SOL_SOCKET, SO_LINGER, (sockoptval_t)&optVal, &optSz );
	    //
	RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );

	ASSERT( status < 0  ||  optSz == sizeof( struct linger ));
	//
    } else {
	//
	ctl = OPTION_GET(ctl);

	if (ctl == OPTION_NULL) {
	    optVal.l_onoff = 0;	    // Argument is THE(NULL); disable linger.
	} else {
	    optVal.l_onoff = 1;	    // argument is THE t; enable linger.
	    optVal.l_linger = TAGGED_INT_TO_C_INT(OPTION_GET(ctl));
	}

	RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, NULL );
	    //
	    status = setsockopt (socket, SOL_SOCKET, SO_LINGER, (sockoptval_t)&optVal, sizeof(struct linger));
	    //
	RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );
    }

    if (status < 0)  		return RAISE_SYSERR__MAY_HEAPCLEAN(task, status, NULL);
    if (optVal.l_onoff == 0)    return OPTION_NULL;

    Val result =   OPTION_THE(  task,  TAGGED_INT_FROM_C_INT( optVal.l_linger )  );

									    EXIT_MYTHRYL_CALLABLE_C_FN(__func__);
    return result;
}
Beispiel #8
0
Val   _lib7_P_FileSys_readlink   (Task* task,  Val arg)   {
    //========================
    //
    // Mythryl type:  String -> String
    //
    // Read the value of a symbolic link.
    //
    // The following implementation assumes that the system readlink
    // fills the given buffer as much as possible, without nul-termination,
    // and returns the number of bytes copied. If the buffer is not large
    // enough, the return value will be at least the buffer size. In that
    // case, we find out how big the link really is, allocate a buffer to
    // hold it, and redo the readlink.
    //
    // Note that the above semantics are not those of POSIX, which requires
    // null-termination on success, and only fills the buffer up to at most 
    // the penultimate byte even on failure.
    //
    // Should this be written to avoid the extra copy, using heap memory?
    //
    // This fn gets bound as   readlink   in:
    //
    //     src/lib/std/src/posix-1003.1b/posix-file.pkg
    //     src/lib/std/src/posix-1003.1b/posix-file-system-64.pkg

									    ENTER_MYTHRYL_CALLABLE_C_FN("_lib7_P_FileSys_readlink");

    struct stat  sbuf;
    int          len;
    int          result;

    char* heap_path = HEAP_STRING_AS_C_STRING( arg );

    char  buf[MAXPATHLEN];

    // We cannot reference anything on the Mythryl
    // heap between RELEASE_MYTHRYL_HEAP and RECOVER_MYTHRYL_HEAP
    // because garbage collection might be moving
    // it around, so copy heap_path into C storage: 
    //
    Mythryl_Heap_Value_Buffer  path_buf;
    //
    {   char* c_path
	    = 
	    buffer_mythryl_heap_value( &path_buf, (void*) heap_path, strlen( heap_path ) +1 );		// '+1' for terminal NUL on string.

	RELEASE_MYTHRYL_HEAP( task->pthread, "_lib7_P_FileSys_readlink", NULL );
	    //
	    len = readlink(c_path, buf, MAXPATHLEN);
	    //
	RECOVER_MYTHRYL_HEAP( task->pthread, "_lib7_P_FileSys_readlink" );

	unbuffer_mythryl_heap_value( &path_buf );
    }

    if (len < 0)   return RAISE_SYSERR__MAY_HEAPCLEAN(task, len, NULL);

    if (len < MAXPATHLEN) {
        //
	buf[len] = '\0';
	return make_ascii_string_from_c_string__may_heapclean (task, buf, NULL);
    }


    // Buffer not big enough.

    // Determine how big the link text is and allocate a buffer.

    {   char* c_path
	    = 
	    buffer_mythryl_heap_value( &path_buf, (void*) heap_path, strlen( heap_path ) +1 );		// '+1' for terminal NUL on string.

	RELEASE_MYTHRYL_HEAP( task->pthread, "_lib7_P_FileSys_readlink", NULL );
	    //
	    result = lstat (c_path, &sbuf);
	    //
	RECOVER_MYTHRYL_HEAP( task->pthread, "_lib7_P_FileSys_readlink" );

	unbuffer_mythryl_heap_value( &path_buf );
    }

    if (result < 0)   return RAISE_SYSERR__MAY_HEAPCLEAN(task, result, NULL);

    int nlen = sbuf.st_size + 1;

    char* nbuf = MALLOC(nlen);

    if (nbuf == 0)   return RAISE_ERROR__MAY_HEAPCLEAN(task, "out of malloc memory", NULL);

    // Try the readlink again. Give up on error or if len is still bigger
    // than the buffer size.
    //
    {   char* c_path
	    = 
	    buffer_mythryl_heap_value( &path_buf, (void*) heap_path, strlen( heap_path ) +1 );		// '+1' for terminal NUL on string.

	RELEASE_MYTHRYL_HEAP( task->pthread, "_lib7_P_FileSys_readlink", NULL );
	    //
	    len = readlink(c_path, buf, len);
	    //
	RECOVER_MYTHRYL_HEAP( task->pthread, "_lib7_P_FileSys_readlink" );

	unbuffer_mythryl_heap_value( &path_buf );
    }

    if (len < 0)		return RAISE_SYSERR__MAY_HEAPCLEAN(task, len, NULL);
    if (len >= nlen)		return RAISE_ERROR__MAY_HEAPCLEAN(task, "readlink failure", NULL);

    nbuf[len] = '\0';
    Val chunk = make_ascii_string_from_c_string__may_heapclean (task, nbuf, NULL);
    FREE (nbuf);
    //
    return chunk;

}
Beispiel #9
0
Val   _lib7_Sock_sendbuf   (Task* task,  Val arg)   {
    //==================
    //
    // Mythryl type:
    //     ( Int,	    # socket fd
    //       Wy8Vector,     # byte vector
    //       Int,           # start offset
    //       Int,           # vector length (end offset)
    //       Bool,          # don't-route flag
    //       Bool           # default-oob flag
    //     )
    //     ->
    //     Int
    //
    // Send data from the buffer; bytes is either a rw_vector_of_one_byte_unts.Rw_Vector, or
    // a vector_of_one_byte_unts.vector.  The arguments are: socket, data buffer, start
    // position, number of bytes, OOB flag, and don't_route flag.
    //
    // This fn gets bound as   send_v, send_a   in:
    //
    //     src/lib/std/src/socket/socket-guts.pkg
    ENTER_MYTHRYL_CALLABLE_C_FN(__func__);
    int   socket    = GET_TUPLE_SLOT_AS_INT( arg, 0);
    Val   buf       = GET_TUPLE_SLOT_AS_VAL( arg, 1);
    int   offset    = GET_TUPLE_SLOT_AS_INT( arg, 2);
    int   nbytes    = GET_TUPLE_SLOT_AS_INT( arg, 3);
    Val   oob       = GET_TUPLE_SLOT_AS_VAL( arg, 4);
    Val   dontroute = GET_TUPLE_SLOT_AS_VAL( arg, 5);									// Last use of 'arg'.

    char* heap_data = HEAP_STRING_AS_C_STRING(buf) + offset;

    // Compute flags parameter:
    //
    int                         flgs  = 0;
    if (oob       == HEAP_TRUE) flgs |= MSG_OOB;
    if (dontroute == HEAP_TRUE) flgs |= MSG_DONTROUTE;

//															log_if( "sendbuf.c/top: socket d=%d nbytes d=%d OOB=%s DONTROUTE=%s\n",
//																socket, nbytes, (oob == HEAP_TRUE) ? "TRUE" : "FALSE", (dontroute == HEAP_TRUE) ? "TRUE" : "FALSE"
//															);
//															hexdump_if( "sendbuf.c/top: Data to send: ", (unsigned char*)heap_data, nbytes );

    errno = 0;

    int n;

    // We cannot reference anything on the Mythryl heap
    // between RELEASE_MYTHRYL_HEAP and RECOVER_MYTHRYL_HEAP
    // because garbage collection might be moving
    // it around, so copy 'heap_data' into C storage:
    //
    Mythryl_Heap_Value_Buffer  data_buf;
    //
    {   char* c_data =  buffer_mythryl_heap_value( &data_buf, (void*) heap_data, nbytes );

        RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, NULL );
        //
        do {
            //
            n = send (socket, c_data, nbytes, flgs);
            //
        } while (n < 0 && errno == EINTR);										// Restart if interrupted by a SIGALRM or SIGCHLD or whatever.
        //
        RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );
//															log_if( "sendbuf.c/bot: n d=%d errno d=%d\n", n, errno );
        unbuffer_mythryl_heap_value( &data_buf );
    }

    Val result =  RETURN_STATUS_EXCEPT_RAISE_SYSERR_ON_NEGATIVE_STATUS__MAY_HEAPCLEAN(task, n, NULL);
    EXIT_MYTHRYL_CALLABLE_C_FN(__func__);
    return result;
}
Beispiel #10
0
Val   _lib7_P_IO_copy   (Task* task,  Val arg)   {
    //===============
    //
    // Mythryl type:   (String, String) -> Int
    //
    // Copy a file  and return its length.
    //
    // This fn gets bound as   copy   in:
    //
    //     src/lib/std/src/psx/posix-io.pkg
    //     src/lib/std/src/psx/posix-io-64.pkg				# Actually, I haven't gotten around to this yet.

    ENTER_MYTHRYL_CALLABLE_C_FN(__func__);

    Val	existing =  GET_TUPLE_SLOT_AS_VAL(arg, 0);
    Val	new_name =  GET_TUPLE_SLOT_AS_VAL(arg, 1);

    char* heap_existing =  HEAP_STRING_AS_C_STRING( existing );
    char* heap_new_name =  HEAP_STRING_AS_C_STRING( new_name );


    // We cannot reference anything on the Mythryl
    // heap between RELEASE_MYTHRYL_HEAP and RECOVER_MYTHRYL_HEAP
    // because garbage collection might be moving
    // it around, so copy heap_path into C storage:
    //
    Mythryl_Heap_Value_Buffer  existing_buf;
    Mythryl_Heap_Value_Buffer  new_name_buf;

    int ok = TRUE;

    ssize_t total_bytes_written  = 0;

    {   char* c_existing =  buffer_mythryl_heap_value( &existing_buf, (void*) heap_existing, strlen( heap_existing ) +1 );		// '+1' for terminal NUL on string.
        char* c_new_name =  buffer_mythryl_heap_value( &new_name_buf, (void*) heap_new_name, strlen( heap_new_name ) +1 );		// '+1' for terminal NUL on string.

        RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, NULL );
        //
        struct stat statbuf;
        int fd_out;
        int fd_in = open(c_existing, O_RDONLY);								// Open the input file.
        if (fd_in >= 0) {
            if (0 <= fstat(fd_in, &statbuf)) {								// Get the mode of the input file so that we can ...
                fd_out = creat( c_new_name, statbuf.st_mode );						// ... open the output file with same mode as input file.
                if (0 <= fd_out) {
                    char buffer[ 4096 ];
                    ssize_t bytes_read;
                    int ok = TRUE;
                    while (ok) {										// Read up to one buffer[]-load from fd_in.
                        do {
                            bytes_read = read( fd_in, buffer, 4096 );
                        } while (bytes_read <  0 && errno == EINTR);					// Retry if interrupted by SIGALRM or such.
                        if      (bytes_read <  0) {
                            ok = FALSE;
                            break;
                        }
                        if      (bytes_read == 0) {
                            break;
                        }
                        ssize_t buffer_bytes_written  = 0;
                        while (ok  &&  (buffer_bytes_written < bytes_read)) {				// Write buffer[] contents to fd_out. Usually one write() will do it, but this is not guaranteed.
                            ssize_t bytes_to_write = bytes_read - buffer_bytes_written;
                            ssize_t bytes_written;
                            do {
                                bytes_written  = write( fd_out, buffer+buffer_bytes_written, bytes_to_write );
                            } while (bytes_written < 0 && errno == EINTR);					// Retry if interrupted by SIGALRM or such.
                            ok = ok && (bytes_written > 0);
                            buffer_bytes_written += bytes_written;
                            total_bytes_written += bytes_written;
                        }
                    }
                    close(fd_out);
                } else {
                    ok = FALSE;
                }
                close(fd_in);
            } else {
                ok = FALSE;
            }
        } else {
            ok = FALSE;
        }
        //
        RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );

        unbuffer_mythryl_heap_value( &existing_buf );
        unbuffer_mythryl_heap_value( &new_name_buf );
    }

    Val        result;

    if (!ok)   result = RAISE_SYSERR__MAY_HEAPCLEAN(task, -1, NULL);						// XXX SUCKO FIXME I'm being totally sloppy about accurate diagnostics here.  Feel free to submit a patch improving this.
    else       result = TAGGED_INT_FROM_C_INT( total_bytes_written );


    EXIT_MYTHRYL_CALLABLE_C_FN(__func__);
    return result;
}
Beispiel #11
0
Val   _lib7_Sock_recv   (Task* task,  Val arg)   {
    //===============
    //
    // Mythryl type: (Socket, Int, Bool, Bool) -> vector_of_one_byte_unts::Vector
    //
    // The arguments are: socket, number of bytes, OOB flag and peek flag.
    // The result is the vector of bytes received.
    //
    // This fn gets bound as   recv_v'   in:
    //
    //     src/lib/std/src/socket/socket-guts.pkg

									    ENTER_MYTHRYL_CALLABLE_C_FN(__func__);

    Val vec;
    ssize_t n;

    int	socket = GET_TUPLE_SLOT_AS_INT( arg, 0 );
    int	nbytes = GET_TUPLE_SLOT_AS_INT( arg, 1 );
    Val	oob    = GET_TUPLE_SLOT_AS_VAL( arg, 2 );
    Val	peek   = GET_TUPLE_SLOT_AS_VAL( arg, 3 );

    int		           flag  = 0;
    if (oob  == HEAP_TRUE) flag |= MSG_OOB;
    if (peek == HEAP_TRUE) flag |= MSG_PEEK;

    // We cannot reference anything on the Mythryl
    // heap between RELEASE_MYTHRYL_HEAP and RECOVER_MYTHRYL_HEAP
    // because garbage collection might be moving
    // it around, so allocate a C-world buffer
    // to read the bytes into:
    //
    Mythryl_Heap_Value_Buffer  read_buf;
    //
    {   unsigned char* c_read =  buffer_mythryl_heap_nonvalue( &read_buf, nbytes );
//										log_if("recv.c/before: socket d=%d nbytes d=%d oob=%s peek=%s\n",socket,nbytes,(oob == HEAP_TRUE)?"TRUE":"FALSE",(peek == HEAP_TRUE)?"TRUE":"FALSE");
	errno = 0;

	RELEASE_MYTHRYL_HEAP( task->hostthread, __func__, NULL );
	    //
            do {								// Backed out 2010-02-26 CrT: See discussion at bottom of src/c/lib/socket/connect.c
		//								// Restored   2012-08-07 CrT
		n = recv (socket, c_read, nbytes, flag);
		//
            } while (n < 0 && errno == EINTR);					// Restart if interrupted by a SIGALRM or SIGCHLD or whatever.
	    //
	RECOVER_MYTHRYL_HEAP( task->hostthread, __func__ );

	if (n <= 0) {
	    unbuffer_mythryl_heap_value( &read_buf );
	    if (n <  0)   return RAISE_SYSERR__MAY_HEAPCLEAN(task, status, NULL);
	    if (n == 0)   return ZERO_LENGTH_STRING__GLOBAL;
	}

	// Allocate result vector to hold the bytes read.
	// NB: This might cause a heapcleaning, moving things around:
	//
	vec = allocate_nonempty_wordslots_vector__may_heapclean( task, BYTES_TO_WORDS(n), NULL );

	// Copy bytes read into result vector:
	//
        memcpy( PTR_CAST(char*, vec), c_read, n);
//										log_if(   "recv.c/after: n d=%d errno d=%d (%s)\n", n, errno, errno ? strerror(errno) : "");
//										hexdump_if( "recv.c/after: Received data: ", PTR_CAST(unsigned char*, vec), n );
	unbuffer_mythryl_heap_value( &read_buf );
    }

    Val result = make_vector_header( task,  STRING_TAGWORD, vec, n );

									    EXIT_MYTHRYL_CALLABLE_C_FN(__func__);
    return result;
}
static Val   set__time_profiling_is_running__to   (Task* task,  Val arg)   {
    //       ==================================
    //
    // Mythryl type:   Bool -> Void
    //
    // This dis/ables sending of SIGVTALRM signals to the process,
    // vs  set_time_profiling_rw_vector() above which
    // dis/ables handling of SIGVTALRM signals by the process.
    //
    // This fn gets bound to   set__time_profiling_is_running__to   in:
    //
    //     src/lib/std/src/nj/runtime-profiling-control.pkg

									    ENTER_MYTHRYL_CALLABLE_C_FN("set__time_profiling_is_running__to");

    #ifndef HAS_SETITIMER
	//
	return RAISE_ERROR__MAY_HEAPCLEAN(task, "time profiling not supported", NULL);
	//
    #else
	//     "The system provides each process with three interval timers,
	//      each decrementing in a distinct time domain. When any timer
	//      expires, a signal is sent to the process, and the timer
	//      (potentially) restarts.
	//
	//          ITIMER_REAL	   Decrements in real time, and delivers SIGALRM upon expiration.
	//          ITIMER_VIRTUAL Decrements only when the process is executing, and delivers SIGVTALRM upon expiration.
	//          ITIMER_PROF	   Decrements both when the process executes and when the system is executing on behalf of the process.
	//                         Coupled with ITIMER_VIRTUAL, this timer is usually used to profile the time spent by the application
	//			   in user and kernel space. SIGPROF is delivered upon expiration.
	//
	//            -- http://linux.about.com/library/cmd/blcmdl2_setitimer.htm
	//
	struct itimerval   new_itv;

	if (arg == HEAP_FALSE) {
	    //
	    new_itv.it_interval.tv_sec	=
	    new_itv.it_value.tv_sec	=
	    new_itv.it_interval.tv_usec	=
	    new_itv.it_value.tv_usec	= 0;

	} else if (time_profiling_rw_vector__global == HEAP_VOID) {
	    //
	    return RAISE_ERROR__MAY_HEAPCLEAN(task, "no time_profiling_rw_vector set", NULL);

	} else {
	    //
	    new_itv.it_interval.tv_sec	=
	        new_itv.it_value.tv_sec	= 0;

	    new_itv.it_interval.tv_usec	=
	       new_itv.it_value.tv_usec	= MICROSECONDS_PER_SIGVTALRM;		// From   src/c/h/profiler-call-counts.h
	}

	int status = setitimer (ITIMER_VIRTUAL, &new_itv, NULL);

	RETURN_VOID_EXCEPT_RAISE_SYSERR_ON_NEGATIVE_STATUS__MAY_HEAPCLEAN(task, status, NULL);

    #endif
}