コード例 #1
0
ファイル: ncurse_2.c プロジェクト: rotarui/42sh
static void	aff_cmd(t_shell *shell, char *buff, int *i)
{
  if (shell->com.on == TRUE && shell->com.cursor)
    {
      put_in_buff(buff, shell->com.cursor->str, i);
      print_buff(shell, buff, i);
      aff_cmpl(shell);
    }
  else
    print_buff(shell, buff, i);
}
コード例 #2
0
/**
 *
 *  nfs3_FSALToFhandle: converts a FSAL file handle to a nfs3 file handle.
 *
 * Converts a nfs3 file handle to a FSAL file handle.
 *
 * @param pfh3 [OUT] pointer to the extracted file handle 
 * @param pfsalhandle [IN] pointer to the FSAL handle to be converted
 * @param pexport [IN] pointer to the export list entry the FH belongs to
 *
 * @return 1 if successful, 0 otherwise
 *
 */
int nfs3_FSALToFhandle(nfs_fh3 * pfh3, fsal_handle_t * pfsalhandle,
                       exportlist_t * pexport)
{
  fsal_status_t fsal_status;
  file_handle_v3_t file_handle;

  print_buff(COMPONENT_FILEHANDLE, (char *)pfsalhandle, sizeof(fsal_handle_t));

  /* zero-ification of the buffer to be used as handle */
  memset(pfh3->data.data_val, 0, NFS3_FHSIZE);
  memset((caddr_t) &file_handle, 0, sizeof(file_handle_v3_t));

  /* Fill in the fs opaque part */
  fsal_status =
      FSAL_DigestHandle(&pexport->FS_export_context, FSAL_DIGEST_NFSV3, pfsalhandle,
                        (caddr_t) & file_handle.fsopaque);
  if(FSAL_IS_ERROR(fsal_status))
    return 0;

  /* keep track of the export id */
  file_handle.exportid = pexport->id;

  /* Set the last byte */
  file_handle.xattr_pos = 0;

  /* Set the len */
  pfh3->data.data_len = sizeof(file_handle_v3_t);

  /* Set the data */
  memcpy(pfh3->data.data_val, &file_handle, sizeof(file_handle_v3_t));

  print_fhandle3(COMPONENT_FILEHANDLE, pfh3);

  return 1;
}                               /* nfs3_FSALToFhandle */
コード例 #3
0
/**
 *
 *  nfs3_FhandleToFSAL: converts a nfs3 file handle to a FSAL file handle.
 *
 * Converts a nfs3 file handle to a FSAL file handle.
 *
 * @param pfh3 [IN] pointer to the file handle to be converted
 * @param pfsalhandle [OUT] pointer to the extracted FSAL handle
 *
 * @return 1 if successful, 0 otherwise
 *
 */
int nfs3_FhandleToFSAL(nfs_fh3 * pfh3, fsal_handle_t * pfsalhandle,
                       fsal_op_context_t * pcontext)
{
  fsal_status_t fsal_status;
  file_handle_v3_t *pfile_handle;

  print_fhandle3(COMPONENT_FILEHANDLE, pfh3);

  /* Verify the len */
  if(pfh3->data.data_len != sizeof(file_handle_v3_t))
    return 0;                   /* Corrupted FH */

  /* Cast the fh as a non opaque structure */
  pfile_handle = (file_handle_v3_t *) (pfh3->data.data_val);

  /* Fill in the fs opaque part */
  fsal_status =
      FSAL_ExpandHandle(FSAL_GET_EXP_CTX(pcontext), FSAL_DIGEST_NFSV3,
                        (caddr_t) & (pfile_handle->fsopaque), pfsalhandle);
  if(FSAL_IS_ERROR(fsal_status))
    return 0;                   /* Corrupted FH */

  print_buff(COMPONENT_FILEHANDLE, (char *)pfsalhandle, sizeof(fsal_handle_t));

  return 1;
}                               /* nfs3_FhandleToFSAL */
コード例 #4
0
/**
 *
 *  nfs4_FhandleToFSAL: converts a nfs4 file handle to a FSAL file handle.
 *
 * Converts a nfs4 file handle to a FSAL file handle.
 *
 * @param pfh4 [IN] pointer to the file handle to be converted
 * @param pfsalhandle [OUT] pointer to the extracted FSAL handle
 *
 * @return 1 if successful, 0 otherwise
 *
 */
int nfs4_FhandleToFSAL(nfs_fh4 * pfh4, fsal_handle_t * pfsalhandle,
                       fsal_op_context_t * pcontext)
{
  fsal_status_t fsal_status;
  file_handle_v4_t *pfile_handle;

  print_fhandle4(COMPONENT_FILEHANDLE, pfh4);

  /* Verify the len */
  if(pfh4->nfs_fh4_len != sizeof(file_handle_v4_t))
    return 0;                   /* Corrupted FH */

  /* Cast the fh as a non opaque structure */
  pfile_handle = (file_handle_v4_t *) (pfh4->nfs_fh4_val);

  /* The filehandle should not be related to pseudo fs */
  if(pfile_handle->pseudofs_id != 0 || pfile_handle->pseudofs_flag != FALSE)
    return 0;                   /* Bad FH */

  /* Fill in the fs opaque part */
  fsal_status =
      FSAL_ExpandHandle(FSAL_GET_EXP_CTX(pcontext), FSAL_DIGEST_NFSV4,
                        (caddr_t) & (pfile_handle->fsopaque), pfsalhandle);
  if(FSAL_IS_ERROR(fsal_status))
    return 0;                   /* Corrupted (or stale) FH */

  print_buff(COMPONENT_FILEHANDLE, (char *)pfsalhandle, sizeof(fsal_handle_t));

  return 1;
}                               /* nfs4_FhandleToFSAL */
コード例 #5
0
short nlm4_FhandleToExportId(netobj * pfh3)
{
  file_handle_v3_t *pfile_handle;

  if(pfh3->n_bytes == NULL || pfh3->n_len < sizeof(file_handle_v3_t))
    return -1;                  /* Badly formed argument */

  pfile_handle = (file_handle_v3_t *) (pfh3->n_bytes);

  print_buff(COMPONENT_FILEHANDLE, pfh3->n_bytes, pfh3->n_len);

  return pfile_handle->exportid;
}
コード例 #6
0
/**
 *
 * nfs3_FhandleToExportId
 *
 * This routine extracts the export id from the file handle NFSv3
 *
 * @param pfh3 [IN] file handle to manage.
 * 
 * @return the export id.
 *
 */
short nfs3_FhandleToExportId(nfs_fh3 * pfh3)
{
  file_handle_v3_t *pfile_handle;

  pfile_handle = (file_handle_v3_t *) (pfh3->data.data_val);

  if(pfile_handle == NULL)
    return -1;                  /* Badly formed argument */

  print_buff(COMPONENT_FILEHANDLE, (char *)pfh3->data.data_val, pfh3->data.data_len);

  return pfile_handle->exportid;
}                               /* nfs3_FhandleToExportId */
コード例 #7
0
/**
 *
 *  nfs2_FhandleToFSAL: converts a nfs2 file handle to a FSAL file handle.
 *
 * Converts a nfs2 file handle to a FSAL file handle.
 *
 * @param pfh2 [IN] pointer to the file handle to be converted
 * @param pfsalhandle [OUT] pointer to the extracted FSAL handle
 *
 * @return 1 if successful, 0 otherwise
 *
 */
int nfs2_FhandleToFSAL(fhandle2 * pfh2, fsal_handle_t * pfsalhandle,
                       fsal_op_context_t * pcontext)
{
  fsal_status_t fsal_status;
  file_handle_v2_t *pfile_handle;

  /* Cast the fh as a non opaque structure */
  pfile_handle = (file_handle_v2_t *) pfh2;
  print_fhandle2(COMPONENT_FILEHANDLE, pfh2);

  /* Fill in the fs opaque part */
  fsal_status =
      FSAL_ExpandHandle(FSAL_GET_EXP_CTX(pcontext), FSAL_DIGEST_NFSV2,
                        (caddr_t) & (pfile_handle->fsopaque), pfsalhandle);
  if(FSAL_IS_ERROR(fsal_status))
    return 0;                   /* Corrupted FH */

  print_buff(COMPONENT_FILEHANDLE, (char *)pfsalhandle, sizeof(fsal_handle_t));

  return 1;
}                               /* nfs2_FhandleToFSAL */
コード例 #8
0
ファイル: gp_layer.c プロジェクト: eusafe/libgate
// send message to port
int gp_resend () {
	int nwritten;
//	int i=0;

 	if(gp_cfg.fd < 3) exit (100+gp_cfg.fd);
	
	if( debug >= 9 || (debug >= 8 && gp_cfg.polling == 0)  ) {
		print_buff("port  sending: ", send_mess.buf, send_mess.len);
/*		fprintf(stderr, "port  sending: "); 
		for(i=0;i<send_mess.len;i++) {
			fprintf(stderr, "0x%02x, ", send_mess.buf[i]);
		}
	 	fprintf(stderr, "writing  %d bytes into port\n", send_mess.len);*/
 	}
 		
 	if ( event_add(&gp_cfg.evport, &gp_cfg.timeout) < 0) syslog(LOG_ERR, "event_add.send_mess.ev_timeout setup: %m");
	nwritten = write(gp_cfg.fd, send_mess.buf, send_mess.len);
	
// 	struct timeval tv;
//  	gettimeofday(&tv,NULL); 
// 	if( debug >= 5 || (debug >= 4 && gp_cfg.polling == 0)  ) {
//  		fprintf(stderr, "wrote  %d bytes into port, time: %u.%06u, is pend %d\n", nwritten, tv.tv_sec,tv.tv_usec, 
//  			event_pending(&gp_cfg.evport,EV_READ|EV_TIMEOUT, &gp_cfg.timeout));
//  	}

	if (nwritten == 0) {
		if (errno == EPIPE) {
			syslog(LOG_ERR, "recipient closed connection");
			gp_close();
			// daemon_exit(1); // never exit on error!
		}
	} else if (nwritten < 0) {
		syslog(LOG_CRIT, "write (%d): %m", errno);
		gp_close();
		// daemon_exit(1); // never exit on error!
	}
//	if ( event_add(&gp_cfg.evport, NULL) < 0) syslog(LOG_ERR, "evport,event_add setup: %m");
	return nwritten;
}
コード例 #9
0
/**
 *
 *  nfs2_FSALToFhandle: converts a FSAL file handle to a nfs2 file handle.
 *
 * Converts a nfs2 file handle to a FSAL file handle.
 *
 * @param pfh2 [OUT] pointer to the extracted file handle 
 * @param pfsalhandle [IN] pointer to the FSAL handle to be converted
 * @param pfsalhandle [IN] pointer to the FSAL handle to be converted
 *
 * @return 1 if successful, 0 otherwise
 *
 */
int nfs2_FSALToFhandle(fhandle2 * pfh2, fsal_handle_t * pfsalhandle,
                       exportlist_t * pexport)
{
  fsal_status_t fsal_status;
  file_handle_v2_t file_handle;

  print_buff(COMPONENT_FILEHANDLE, (char *)pfsalhandle, sizeof(fsal_handle_t));

  /* zero-ification of the buffer to be used as handle */
  memset(pfh2, 0, NFS2_FHSIZE);
  memset((caddr_t) &file_handle, 0, sizeof(file_handle_v2_t));

  /* Fill in the fs opaque part */
  fsal_status =
      FSAL_DigestHandle(&pexport->FS_export_context, FSAL_DIGEST_NFSV2, pfsalhandle,
                        (caddr_t) & file_handle.fsopaque);
  if(FSAL_IS_ERROR(fsal_status))
   {
     if( fsal_status.major == ERR_FSAL_TOOSMALL )
      LogCrit( COMPONENT_FILEHANDLE, "NFSv2 file handle is too small to manage this fsal" ) ;
     else
      LogCrit( COMPONENT_FILEHANDLE, "FSAL_DigestHandle return (%u,%u) when called from %s", 
               fsal_status.major, fsal_status.minor, __func__ ) ;
    return 0;
   }
  /* keep track of the export id */
  file_handle.exportid = pexport->id;

  /* Set the last byte */
  file_handle.xattr_pos = 0;

  /* Set the data */
  memcpy((caddr_t) pfh2, &file_handle, sizeof(file_handle_v2_t));

  print_fhandle2(COMPONENT_FILEHANDLE, pfh2);

  return 1;
}                               /* nfs2_FSALToFhandle */
コード例 #10
0
ファイル: nfs4_op_lookup.c プロジェクト: eitanb/nfs-ganesha
int nfs4_op_lookup(struct nfs_argop4 *op, compound_data_t * data, struct nfs_resop4 *resp)
{
  fsal_name_t name;
  char strname[MAXNAMLEN];
#ifndef _NO_XATTRD
  char objname[MAXNAMLEN];
#endif
  unsigned int xattr_found = FALSE;
  cache_entry_t *dir_pentry = NULL;
  cache_entry_t *file_pentry = NULL;
  fsal_attrib_list_t attrlookup;
  cache_inode_status_t cache_status;

  fsal_handle_t *pfsal_handle = NULL;

  char __attribute__ ((__unused__)) funcname[] = "nfs4_op_lookup";

  resp->resop = NFS4_OP_LOOKUP;
  res_LOOKUP4.status = NFS4_OK;

  /* If there is no FH */
  if(nfs4_Is_Fh_Empty(&(data->currentFH)))
    {
      res_LOOKUP4.status = NFS4ERR_NOFILEHANDLE;
      return res_LOOKUP4.status;
    }

  /* If the filehandle is invalid */
  if(nfs4_Is_Fh_Invalid(&(data->currentFH)))
    {
      res_LOOKUP4.status = NFS4ERR_BADHANDLE;
      return res_LOOKUP4.status;
    }

  /* Tests if the Filehandle is expired (for volatile filehandle) */
  if(nfs4_Is_Fh_Expired(&(data->currentFH)))
    {
      res_LOOKUP4.status = NFS4ERR_FHEXPIRED;
      return res_LOOKUP4.status;
    }

  /* Check for empty name */
  if(op->nfs_argop4_u.oplookup.objname.utf8string_len == 0 ||
     op->nfs_argop4_u.oplookup.objname.utf8string_val == NULL)
    {
      res_LOOKUP4.status = NFS4ERR_INVAL;
      return res_LOOKUP4.status;
    }

  /* Check for name to long */
  if(op->nfs_argop4_u.oplookup.objname.utf8string_len > FSAL_MAX_NAME_LEN)
    {
      res_LOOKUP4.status = NFS4ERR_NAMETOOLONG;
      return res_LOOKUP4.status;
    }

  /* If Filehandle points to a pseudo fs entry, manage it via pseudofs specific functions */
  if(nfs4_Is_Fh_Pseudo(&(data->currentFH)))
    return nfs4_op_lookup_pseudo(op, data, resp);

#ifndef _NO_XATTRD
  /* If Filehandle points to a xattr object, manage it via the xattrs specific functions */
  if(nfs4_Is_Fh_Xattr(&(data->currentFH)))
    return nfs4_op_lookup_xattr(op, data, resp);
#endif

  /* UTF8 strings may not end with \0, but they carry their length */
  utf82str(strname, sizeof(strname), &arg_LOOKUP4.objname);

#ifndef _NO_XATTRD
  /* Is this a .xattr.d.<object> name ? */
  if(nfs_XattrD_Name(strname, objname))
    {
      strcpy(strname, objname);
      xattr_found = TRUE;
    }
#endif

  if((cache_status = cache_inode_error_convert(FSAL_str2name(strname,
                                                             MAXNAMLEN,
                                                             &name))) !=
     CACHE_INODE_SUCCESS)
    {
      res_LOOKUP4.status = nfs4_Errno(cache_status);
      return res_LOOKUP4.status;
    }

  /* No 'cd .' is allowed return NFS4ERR_BADNAME in this case */
  /* No 'cd .. is allowed, return EINVAL in this case. NFS4_OP_LOOKUPP should be use instead */
  if(!FSAL_namecmp(&name, (fsal_name_t *) & FSAL_DOT)
     || !FSAL_namecmp(&name, (fsal_name_t *) & FSAL_DOT_DOT))
    {
      res_LOOKUP4.status = NFS4ERR_BADNAME;
      return res_LOOKUP4.status;
    }

  /* Do the lookup in the HPSS Namespace */
  file_pentry = NULL;
  dir_pentry = data->current_entry;

  /* Sanity check: dir_pentry should be ACTUALLY a directory */
  if(dir_pentry->internal_md.type != DIR_BEGINNING
     && dir_pentry->internal_md.type != DIR_CONTINUE)
    {
      /* This is not a directory */
      if(dir_pentry->internal_md.type == SYMBOLIC_LINK)
        res_LOOKUP4.status = NFS4ERR_SYMLINK;
      else
        res_LOOKUP4.status = NFS4ERR_NOTDIR;

      /* Return failed status */
      return res_LOOKUP4.status;
    }

  /* BUGAZOMEU: Faire la gestion des cross junction traverse */
  if((file_pentry = cache_inode_lookup(dir_pentry,
                                       &name,
                                       &attrlookup,
                                       data->ht,
                                       data->pclient,
                                       data->pcontext, &cache_status)) != NULL)
    {
      /* Extract the fsal attributes from the cache inode pentry */
      pfsal_handle = cache_inode_get_fsal_handle(file_pentry, &cache_status);

      if(cache_status != CACHE_INODE_SUCCESS)
        {
          res_LOOKUP4.status = NFS4ERR_SERVERFAULT;
          return res_LOOKUP4.status;
        }

      /* Convert it to a file handle */
      if(!nfs4_FSALToFhandle(&data->currentFH, pfsal_handle, data))
        {
          res_LOOKUP4.status = NFS4ERR_SERVERFAULT;
          return res_LOOKUP4.status;
        }

      /* Copy this to the mounted on FH (if no junction is traversed */
      memcpy((char *)(data->mounted_on_FH.nfs_fh4_val),
             (char *)(data->currentFH.nfs_fh4_val), data->currentFH.nfs_fh4_len);
      data->mounted_on_FH.nfs_fh4_len = data->currentFH.nfs_fh4_len;

#if 0
      print_buff((char *)cache_inode_get_fsal_handle(file_pentry, &cache_status),
                 sizeof(fsal_handle_t));
      print_buff((char *)cache_inode_get_fsal_handle(dir_pentry, &cache_status),
                 sizeof(fsal_handle_t));
#endif
      if(isFullDebug(COMPONENT_NFS_V4))
        {
          LogFullDebug(COMPONENT_NFS_V4,
                       "----> nfs4_op_lookup: name=%s  dir_pentry=%p  looked up pentry=%p",
                       strname, dir_pentry, file_pentry);
          LogFullDebug(COMPONENT_NFS_V4,
                       "----> FSAL handle parent puis fils dans nfs4_op_lookup");
          print_buff(COMPONENT_NFS_V4,
                     (char *)cache_inode_get_fsal_handle(file_pentry, &cache_status),
                     sizeof(fsal_handle_t));
          print_buff(COMPONENT_NFS_V4,
                     (char *)cache_inode_get_fsal_handle(dir_pentry, &cache_status),
                     sizeof(fsal_handle_t));
        }
      LogHandleNFS4("NFS4 LOOKUP CURRENT FH: ", &data->currentFH);

      /* Keep the pointer within the compound data */
      data->current_entry = file_pentry;
      data->current_filetype = file_pentry->internal_md.type;

      /* Return successfully */
      res_LOOKUP4.status = NFS4_OK;

#ifndef _NO_XATTRD
      /* If this is a xattr ghost directory name, update the FH */
      if(xattr_found == TRUE)
        res_LOOKUP4.status = nfs4_fh_to_xattrfh(&(data->currentFH), &(data->currentFH));
#endif

      if((data->current_entry->internal_md.type == DIR_BEGINNING) &&
         (data->current_entry->object.dir_begin.referral != NULL))
        {
          if(!nfs4_Set_Fh_Referral(&(data->currentFH)))
            {
              res_LOOKUP4.status = NFS4ERR_SERVERFAULT;
              return res_LOOKUP4.status;
            }
        }

      return NFS4_OK;

    }

  /* If the part of the code is reached, then something wrong occured in the lookup process, status is not HPSS_E_NOERROR 
   * and contains the code for the error */

  res_LOOKUP4.status = nfs4_Errno(cache_status);

  return res_LOOKUP4.status;
}                               /* nfs4_op_lookup */
コード例 #11
0
ファイル: gp_layer.c プロジェクト: eusafe/libgate
int gp_send_idle() {
	static int gp_dst=0;
	int activ=0;
	
	do {
		gp_dst++;
		if( gp_dst > gp_cfg.max_dev_n ) { gp_dst=0; return 0; }
//	fprintf(stderr, "Check gp_send_idle for %d (%d)\n",gp_dst,devices[gp_dst].timeout_count );
		if(devices[gp_dst].timeout_count > gp_cfg.max_timeout_count) {
			if( devices[gp_dst].activ > 0)
//			if ( gp_dst == 5  ) 
				{
				fprintf(stderr, "Disabled dev %d (%d)\n", gp_dst, devices[gp_dst].timeout_count);
				syslog(LOG_ERR, "Disabled dev %d (%d)\n", gp_dst, devices[gp_dst].timeout_count);
				}
			devices[gp_dst].activ=activ=0;
			continue;
		}
		activ=1;
	} while( activ == 0 );
//	if ( gp_dst == 5  ) 
//	fprintf(stderr, "Run gp_send_idle for %d (%d)\n",gp_dst,devices[gp_dst].timeout_count );
	
/*	cmd_send_t z = {
		.cmd_n = 0x06,
		.set_timeout = 50,
		.polling = 1,
		.data_len = 0
	};*/
	
//return 0;
//	return ad_get_cid(AD_Q_SHORT, gp_dst, &cb_get_poll_result);
	cmd_send_t z = {
		.queue = AD_Q_SHORT,
		.ev_handler = &cb_get_poll_result,
		.target_n = AD_TARGET_GET_CID,
		.cmd_n = 0x04,
		.set_timeout = 150,
		.polling = 1,
		.data_len = 5,
		.cmd_buff = { 0x00, 0xD0, 6, 0x00, 0x0C }
	};
	
	z.dst=gp_dst;
	return gp_send(&z);
}


#pragma pack(push,1)
#pragma pack(pop)

//void gp_receiv(uint8_t* in, int nread) {
// short event=4;
int mydev=-1;

int err=0;
void gp_receiv(int fd, short event, void *arg) {
	int nread=0;
//	int last=0;
	struct timeval tv;
 	
 	gettimeofday(&tv,NULL);
 	uint32_t current_time=tv2ms(tv);
 	uint32_t expect_time=tv2ms(gp_cfg.timeout);
 	uint32_t delta = current_time -  send_mess.sent_time;
 	uint32_t delta2 = current_time -  receiv_mess.last_read;
/*	if (send_mess.dev == 1 ) 
	fprintf(stderr, "Got  event (%d): 0x%X, %u.%06u (%u ms), sent time %u ms, current timeout: %u ms, delta: %u ms (%u ms)\n",
		send_mess.dev,
		event,
		tv.tv_sec,tv.tv_usec,
		current_time,
		send_mess.sent_time,
		expect_time,
		delta, delta2
		);*/
//	char buf[PORT_READ_BUF_LENGTH];
	

//	memset(buf, 0, sizeof(buf));
				
//	if ( event_del(&send_mess.ev_timeout) < 0) syslog(LOG_ERR, "event_del (send_mess.ev_timeout): %m");
//	event_set(&evsocket, sockfd, EV_READ|EV_PERSIST, socket_read, (void*)&evsocket);
//	if ( event_del(&gp_cfg.evport) < 0) syslog(LOG_ERR, "event_del (send_mess.ev_timeout): %m");

/*	if( (event & EV_WRITE) != 0 ) {
//		gp_put_cmd();
		return;
	}*/
/*	evutil_timerclear(&gp_cfg.timeout);
	gp_cfg.timeout.tv_usec = gp_cfg.gp_timeout * 1000;
 	if ( event_add(&gp_cfg.evport, &gp_cfg.timeout) < 0) syslog(LOG_ERR, "event_add.send_mess.ev_timeout setup: %m");*/
	
	if( (event & EV_READ) == 0 ) {
		gp_reconnect(0);
// HACK - we check real timeout, not timer
//		if( current_time >= expect_time ) {	 	
//				} else if(gp_cfg.timeout.tv_sec > 0) {
/*		if ( send_mess.dev == 5 ) 
		fprintf(stderr, "dev=%d,  delta by sent = %d, delta by read = %d, count = %d\n",
			send_mess.dev,delta, delta2, devices[send_mess.dev].timeout_count );*/
	 	if( delta >=  gp_cfg.gp_timeout) {
			if( send_mess.is_expected > 0 ) {
				send_mess.is_expected=0;
				devices[send_mess.dev].timeout_count++;
//				if ( send_mess.dev == 1 ) fprintf(stderr, "inc %d dev=%d\n", devices[send_mess.dev].timeout_count, send_mess.dev);
				if( gp_cfg.polling == 0 ) {
//				if( send_mess.dev == 1 ) {
//					send_mess.target=0;
					uint32_t delta3=current_time - devices[send_mess.dev].last_read;
					fprintf(stderr, "Timeout (%d) for read for %d (cmd=%d, delta=%u)\n", devices[send_mess.dev].timeout_count,
						send_mess.dev, send_mess.target, delta3);
					syslog(LOG_ERR, "Timeout (%d) for read for %d (cmd=%d, delta=%u)",   devices[send_mess.dev].timeout_count,
						send_mess.dev, send_mess.target, delta3);
				}
			}
		}	 	
//		if( current_time >= expect_time + gp_cfg.gp_timeout) {
//	 	if( current_time - receiv_mess.last_read >  gp_cfg.gp_timeout) {
	 	if( delta >=  gp_cfg.gp_timeout) {
// /*	 		if ( send_mess.dev == 5 ) 
// 				fprintf(stderr, "Runed gp_put_cmd!\n");*/
			if( gp_put_cmd() > 0 ||  gp_send_idle() > 0) return;
	 	}
		evutil_timerclear(&gp_cfg.timeout);
		gp_cfg.timeout.tv_usec = gp_cfg.gp_timeout * 1000;
		if ( event_add(&gp_cfg.evport, &gp_cfg.timeout) < 0) syslog(LOG_ERR, "event_add.send_mess.ev_timeout setup: %m");
		return;
	}

// HACK Read (EV_READ)
	evutil_timerclear(&gp_cfg.timeout);
	gp_cfg.timeout.tv_usec = gp_cfg.gp_timeout * 1000;
 	if ( event_add(&gp_cfg.evport, &gp_cfg.timeout) < 0) syslog(LOG_ERR, "event_add.send_mess.ev_timeout setup: %m");

// 	fprintf(stderr, "Set last read %lu \n",current_time);
	devices[send_mess.dev].last_read=receiv_mess.last_read=current_time;
//	gp_cfg.last_dev=send_mess.dev;
	send_mess.is_expected=0;
// TODO after check crc
	if( gp_cfg.polling == 1 && devices[send_mess.dev].activ==0) {
		syslog(LOG_ERR,"Found dev: '%d'\n",send_mess.dev);
	}
	devices[send_mess.dev].activ=1; 
	devices[send_mess.dev].timeout_count=0;
	if (devices[send_mess.dev].is_inited == 0) {
		gp_dev_init(send_mess.dev);
	}
// HACK	
//	mydev=send_mess.dst;
// First chunk of data. 
	if( receiv_buf_p == 0 ) {
		receiv_buf_p = receiv_mess.buf;
		receiv_mess.len=0;
	}
	nread = read(fd, receiv_buf_p, GP_PORT_READ_BUF_LENGTH - 1 - receiv_mess.len);
	
	
//	if( debug >= 5 || (debug >= 4 && gp_cfg.polling == 0) ||  receiv_mess.src != 7) {
	if( debug >= 9 || (debug >= 8 && gp_cfg.polling == 0) ) {
		print_buff("port received: ", receiv_buf_p, nread);
	}
// 	dprint(DL5, "read %d bytes, ev=%x\n", nread, event);
	
	if (nread < 0) { /* EOF */
		syslog(LOG_CRIT, "read: %m");
		gp_close();
		// daemon_exit(1); // never exit on error!
	} else if (nread == 0) {
	 	zprintf(1, "port unexpectedly closed\n");
	 	ad_soft_reset(AD_Q_SHORT,send_mess.dev);
		gp_close();
//		gp_reconnect(0);
//		if(err++  > 10 ) exit(102);
		return;
	} else if (nread > 0) {
		// right trim buffer
		receiv_buf_p[nread] = '\0';
		if( receiv_mess.fl_begin !=  GP_INIT) {
			syslog(LOG_ERR, "Begin flag not found");
			gp_reconnect(0);
//			gp_close();
			return;
		}		
		uint8_t* p=memchr(receiv_buf_p, GP_END,nread);
		if ( p != 0 ) {
			receiv_buf_p=0;
//			*p='\0'; 
			int l = p - &receiv_mess.id_ctrl; // buff. length  for crc		
//	fprintf(stderr, "l = %d bytes\n", l);
//			l = memcpy_unesc(receiv_mess.cmd_buff,receiv_mess.cmd_buff,l);
			l = memcpy_unesc(&receiv_mess.id_ctrl,&receiv_mess.id_ctrl,l);
	
//	fprintf(stderr, "l = %d bytes\n", l);
			if ( crc8_xor(&receiv_mess.id_ctrl,l) > 0 ) {
				int crc=crc8_xor(&receiv_mess.id_ctrl,l-1);
				fprintf(stderr, "crc8 error for %d (cmd=%d, crc=x0%02x) \n", send_mess.dev, send_mess.target, crc);
				syslog(LOG_ERR, "crc8 error for %d (cmd=%d, crc=x0%02x) \n", send_mess.dev, send_mess.target, crc);
				if( debug < 9 ) 
					print_buff("port received (crc error): ", receiv_mess.buf, nread+receiv_mess.len);
//				exit(222);
				return;
			}
// Check error!!!
			if( send_mess.dev !=  receiv_mess.src ) {
				zprintf(2, "device error: got %d, expect %d\n", receiv_mess.src, send_mess.dev);
//				exit(222);				
				return;
			} else {
				receiv_mess.dev=receiv_mess.src;
			}
			if( receiv_mess.cmd_n == GP_REPLY ) {
//				syslog(LOG_ERR, "Got ask(0x%02X) for %d (cmd=%d)\n",receiv_mess.replay, send_mess.dev, send_mess.target);
				if( receiv_mess.replay == GP_REPLY_ACK ) {
					receiv_mess.cmd_n=receiv_mess.was_cmd_n;
//					fprintf(stderr, "Got ACK for (0x%02X) \n", receiv_mess.was_cmd_n);
					process_port_input(PROC_REPLY_ACK);
				} else if ( receiv_mess.replay == GP_REPLY_NACK ) {
					receiv_mess.cmd_n=receiv_mess.was_cmd_n;
					zprintf(2, "Got NACK for (0x%02X) \n", receiv_mess.was_cmd_n);
//					process_port_input(PROC_REPLY_NACK);
				} else {
					receiv_mess.cmd_n=receiv_mess.bad_cmd_n;
					zprintf(2, "Got  EEPROM/RTC error (%d)\n",receiv_mess.replay );
//					process_port_input(PROC_REPLY_EEPROM_ERR);
				}				
			} else {
				receiv_mess.len=l-(3+1);
				if( process_port_input(PROC_REPLY_ACK) > 0 ) return;
			}
		} else {
			receiv_mess.len +=nread;
			receiv_buf_p+=nread;
			return;
		}
//			dprint(DL2, "port recvd: '%.*s'\n", nread, buf);
/*			if(debug >= DL2) {
				char buf2[PORT_READ_BUF_LENGTH];
				char* p=strncpy(buf2,buf,PORT_READ_BUF_LENGTH);
				while( (p=strchr(p,'\r')) > 0 ) *p='$';
				p=buf2;
				while( (p=strchr(buf2,'\n')) >0 ) *p='&';
				dprint(DL2, "port recvd: '%s'\n", buf2);
			}*/
	}
//	fprintf(stderr, "Sending new cmd\n");
	gp_put_cmd();
// 	if ( gp_put_cmd() == 0 && event_add(&gp_cfg.evport, &gp_cfg.timeout) < 0) 
// 		syslog(LOG_ERR, "event_add.send_mess.ev_timeout setup: %m");

// exit(0);	f
// fprintf(stderr, "gp_receiv ok \n");
}