Exemplo n.º 1
0
untyped_handle_s handle_new_untyped(void *value)
{
    handle_index_t index = get_free_index();
    slots[index].id++;
    slots[index].value = value;

    untyped_handle_s handle;
    handle.id = slots[index].id;
    handle.index = index;

    return handle;
}
/**
 * @brief function  opens  the  file whose name is the string
 *                      pointed to by path and associates a stream with it 
 *
 * @param name name of the file to be open
 * @param flags Mode Translated flag value for file to be open
 * @param ... optional third argument
 *
 * @return Returns the Index the of the FILE structure Otherwise,
 *                      NULL is returned and errno is set to indicate the error
 */
int _open(const char *name, int flags, ...) 
{ 
#ifdef CONFIG_FILESYSTEM_SUPPORT
    int free_index = 0;
    u8 mode_val;

    if((free_index = get_free_index()) == 0) {
        errno = EMFILE;
        return(-1);
    }
	switch(flags) {
        case 0: 
            mode_val = FILE_READ;
            break;
        case 2: 
            mode_val = FILE_READ | FILE_WRITE;
            break;
        case 1537:
            mode_val = FILE_WRITE | FILE_CREATE_ALWAYS | FILE_CREATE_NEW;
            break;
        case 1538:
            mode_val = FILE_READ | FILE_WRITE | FILE_CREATE_ALWAYS | 
					   FILE_CREATE_NEW;
            break;
        default:
            errno = EINVAL;
            return(-1);
    }
	
    if((file_pointers[free_index].fd = file_open(name, mode_val)) == -1) {
        /*todo map the return code to an error number */
        return(-1);
    }
    sw_strcpy((char*)file_names[free_index],(char*)name);
    errno = 0;
    return(free_index);
#else
        sw_printk("file system configuration is not defined. \
                Enable CONFIG_FILESYSTEM_SUPPORT.\n");

        errno= EBADF;
        return (-1);
#endif
}
Exemplo n.º 3
0
int server(int *isExit, unsigned short port, int type)
{
	struct sockaddr_in   *paddrv4 = (struct sockaddr_in*)addr;
	struct sockaddr_in6  *paddrv6 = (struct sockaddr_in6*)addr;
	char dst[MAX_ADDR_LEN] = {0};
	char buff[MAX_DATA_LEN] = {0};
	int len = 0, cnt = 0, index = 0;
	int dst_len = sizeof(dst);
	int sock = 0,sockv6 = 0, tmpsock = 0;
	fd_set fdset, tmpfs;
	struct timeval tm = {1, 0};
	int clients[MAX_TCP_LINK] = {0};
	const char *lptemp = NULL;

	if(port == 0) {
		return -1;
	}

	sock = socket(AF_INET, type, 0);
	sockv6 = socket(AF_INET6, type, 0);

	if(sock < 3 && sockv6 < 3) {
		return -1;
	}

#ifdef _WINDOWS
	{
		unsigned long isNonBlocked = 1;
		ioctlsocket(sock, FIONBIO, (unsigned long*)&isNonBlocked); 
		isNonBlocked = 1;
		ioctlsocket(sockv6, FIONBIO, (unsigned long*)&isNonBlocked); 
	}
#else
	fcntl(sock,F_SETFL,fcntl(sock,F_GETFL) | O_NONBLOCK);
	fcntl(sockv6,F_SETFL,fcntl(sockv6,F_GETFL) | O_NONBLOCK);
#endif

	FD_ZERO(&fdset);

	if(sock > 0) {
		memset(addr, 0, sizeof addr);
		paddrv4->sin_family = AF_INET;
		paddrv4->sin_port = htons(port);
		
		if(bind(sock, (struct sockaddr*)addr, sizeof(struct sockaddr_in)) != 0) {
			return -1;
		}

		FD_SET(sock, &fdset);
	}

	if(sockv6 > 0) {
		memset(addr, 0, sizeof addr);
		paddrv6->sin6_family = AF_INET6;
		paddrv6->sin6_port = htons(port);
		bind(sockv6, (struct sockaddr*)addr, 0);
		FD_SET(sockv6, &fdset);
	}

	if(type == SOCK_STREAM) {
		listen(sock, 4);
		listen(sockv6, 4);
	}

	tmpfs = fdset;

	while(!(*isExit)) {
		tm.tv_sec = 1;
		tmpfs = fdset;
		cnt = select((sock>sockv6?sock:sockv6) + 1, &tmpfs, NULL, NULL, &tm);
		
		if(cnt <= 0) {
			continue;
		}

		if(FD_ISSET(sock, &tmpfs)) {
			tmpsock = sock;
		} else if(FD_ISSET(sockv6, &tmpfs)) {
			tmpsock = sockv6; 
		} else {
			for(index = 0; index < MAX_TCP_LINK; index++) {
				if(clients[index] > 0 && FD_ISSET(clients[index], &tmpfs)) {
					if((len = recv(clients[index], buff, sizeof buff, 0)) == 0) {
#ifdef _WINDOWS
						closesocket(clients[index]);
#else
						close(clients[index]);
#endif
						FD_CLR(clients[index], &fdset);
						clients[index] = 0;
						continue;
					}

					if((lptemp = get_data_pair(buff, len)) != NULL) {
						send(clients[index], lptemp+1, lptemp[0], 0);
					}
				}
			}

			continue;
		}

		if(type == SOCK_DGRAM) {
			while((len = recvfrom(tmpsock, buff, sizeof buff, 0, (struct sockaddr *)dst, &dst_len)) > 0) {
				const unsigned char * ptemp = get_data_pair(buff, len);
				if(ptemp != NULL) {
					sendto(tmpsock, ptemp+1, ptemp[0], 0, (struct sockaddr *)dst, dst_len);
				}
			}
		} else if(type == SOCK_STREAM) {
			index = get_free_index(clients, MAX_TCP_LINK);
			while(index >= 0 && (clients[index] = accept(tmpsock, (struct sockaddr *)dst, &dst_len)) > 0) {
				FD_SET(clients[index], &fdset);
				index = get_free_index(clients, MAX_TCP_LINK);
			}
		}
	}
#ifdef _WINDOWS
	closesocket(sock);
	closesocket(sockv6);
#else
	close(sock);
	close(sockv6);
#endif

	for(index = 0; index < MAX_TCP_LINK; index++) {
		if(clients[index] > 0) {
#ifdef _WINDOWS
			closesocket(clients[index]);
#else
			close(clients[index]);
#endif
		}
	}

	return 0;
}
Exemplo n.º 4
0
ftnword f77name(slabini)(char *f_name, ftnword dateo[2], ftnword *f_npas,
			ftnword *f_deet, char *f_etiket, int l1, int l2)
{                                               
 int fd, ix, i, j, taille, npas;
 char name[MAX_LEN], etiket[MAX_LEN];


 if (init == 0) init_index();
 l1 = (l1 < MAX_LEN) ? l1 : MAX_LEN-1; 
 strncpy(name,f_name,l1);
 name[l1] = '\0';

 while ((name[l1-1] == ' ') && (l1 > 1)) {
   l1--;
   name[l1] = '\0';
 }

 l2 = (l2 < 12) ? l2 : 12; 
 strncpy(etiket,f_etiket,l2);
 etiket[l2] = '\0';
   
 while ((etiket[l2-1] == ' ') && (l2 > 1)) {
   l2--;
   etiket[l2] = '\0';
 }

 if((fd = open(name, O_RDWR | O_CREAT | O_EXCL ,0744)) == ERR_NO_FILE)
    {
     fprintf(stderr,"\n***ERROR in SLABINI: error opening file %s\n",name);
     slab_exit(-3);
     }

 ix = get_free_index(fd);
 if (ix == ERR_TAB_FULL) {
     fprintf(stderr,"\n***ERROR in SLABINI(%s): slab file table is full\n",name);
     return(slab_exit(-2));
     }

 strcpy(file_table[ix].file_name,name);
 for (j=0;j < MAX_SLAB_TYPES; j++){
 file_table[ix].count[j] = 0;
 file_table[ix].nrows[j] = 0;
 file_table[ix].nio_njo[j] = 0;
 }

 if ((intBuffer = (int *) malloc(BUFSIZE * sizeof(int)))==NULL)
    {
     fprintf(stderr,"\n***ERROR in SLABINI(%s): Cannot allocate memory for intBuffer\n",name);
     return(slab_exit(-3));
     }
 
 file_table[ix].buffer = intBuffer;
   
 id_block.slb0    = 'SLB0';
 id_block.nBytes  = 32;
 id_block.deet    = (int ) *f_deet;
 id_block.npas    = (int ) *f_npas;
 strcpy(id_block.etiket,etiket);
 id_block.dateo1  = (int ) dateo[0];
 id_block.dateo2  = (int ) dateo[1];
 id_block.val15   = 1.5;

 pos=0;

 iVal = (int *) &id_block;
 taille = (sizeof(id_block) / sizeof(int));
 if (proc0) put_in_buffer(iVal,intBuffer,pos,taille);

 file_table[ix].pos = pos;
 return (ftnword) fd;
}
Exemplo n.º 5
0
void net_select()
{
	FD_ZERO(&rset);
	FD_ZERO(&wset);
	
	FD_SET(server,&rset);
	
	for(int i=0;i<MAX_CONN;i++)
	{
		if(clients[i].state == STATE_HOLD)
		{
			FD_SET(clients[i].socket,&rset);
			FD_SET(clients[i].socket,&wset);
		}
	}
	
	if( select(0, &rset, &wset, NULL, NULL) == SOCKET_ERROR)
	{
		Log::info("server socket:select return error!");
		return;
	}

	if(FD_ISSET(server,&rset))
	{
		if( (client_temp = accept(server,NULL,NULL) ) != INVALID_SOCKET )
		{
			unsigned long non_block = 1;
			if( ioctlsocket(client_temp,FIONBIO, &non_block) == SOCKET_ERROR )
			{
				Log::info("ioctlsocket() failed with error %d!",WSAGetLastError());
				return;
			}
			int i = get_free_index();
			if(i>=0)
			{
				clients[i].socket = client_temp;
				clients[i].state = STATE_HOLD;
				++num_clients;
				onClientAccept(num_clients,i);
				Log::info("client accepted, Total:%d!",num_clients);
				//onAccepted, notify business layer
			}
			else
			{
				Log::info("no free space to accept!");
			}
		}
	}
	
	for(int i=0;i<MAX_CONN;i++)
	{
		if(clients[i].state == STATE_HOLD)
		{
			if(FD_ISSET(clients[i].socket,&rset))
			{
				int len = READ_BUFFER_SIZE - clients[i].readBufAvaliable;
				char* avaliableBuffer = clients[i].readBuffer+clients[i].readBufAvaliable;
				if(len > 0)
				{
					EnterCriticalSection(&CS);//enter critical section
					int rc = recv(clients[i].socket,avaliableBuffer,len,0);
					if(rc > 0)
					{
						clients[i].readBufAvaliable += rc;
						Log::info("recv success,len:%d",rc);
					}
					else if(rc == 0)
					{
						clients[i].state = STATE_CLOSED;
						Log::info("client closed!");
					}
					else
					{
						Log::info("recv error:%d!",WSAGetLastError());
						clients[i].state = STATE_CLOSED;
					}
					LeaveCriticalSection(&CS);//leave critical section
				}
			}
			// return;
			/*
			if(FD_ISSET(clients[i].socket,&wset))
			{
				int len = clients[i].writeBufAvaliable;
				char* sendBuffer = clients[i].writeBuffer;
				
				if(len > 0)
				{
					EnterCriticalSection(&CS);//enter critical section
					int rc = send(clients[i].socket,sendBuffer,len,0);
					if(rc > 0)
					{
						int left = len - rc;
						if(left > 0)
							memcpy(clients[i].writeBuffer,clients[i].writeBuffer+rc,left);
						clients[i].writeBufAvaliable -= rc;
					}
					LeaveCriticalSection(&CS);//leave critical section
				}
			}
			*/
		}
	}
}