示例#1
0
文件: agent.c 项目: misterlihao/MBA
/// Execute a guest command without expecting the output
/// Return AGENT_RET_SUCCESS if succeed or AGENT_RET_EFAIL if fail
static MBA_AGENT_RETURN _MOCKABLE(execute_guest_cmd_noreturn)( void ) {

    char cmd_emit[SZ_MAX_COMMAND];

    int n_wbytes;
    
    const char* cmdline = ac->act.cmdline;

    // construct the final command for agent server
    bzero( cmd_emit, SZ_MAX_COMMAND );
    snprintf( cmd_emit, SZ_MAX_COMMAND, "invo %s", cmdline );

    // emit command
    n_wbytes = as_write( ac->sock, cmd_emit, SZ_MAX_COMMAND );
    if( n_wbytes != SZ_MAX_COMMAND ) {
        agent_printf( "Failed to emit command '%s'\n", cmd_emit );
        return AGENT_RET_EFAIL;
    }

    if ( !ecm_read() ) {
        agent_printf("Error occurs when server creating process.\n");
        return AGENT_RET_EFAIL;
    }

    return AGENT_RET_SUCCESS;
}
示例#2
0
void test_write() {
	asm_p as = &(asm_t){ 0 };
	as_new(as);
	
	as_write(as, "0000 1111");
	st_check( code_cmp(as, (uint8_t[]){ 0x0f }, 1) );
	as_clear(as);
	
	as_write(as, "0000 1111 : 1000 1111");
	st_check( code_cmp(as, (uint8_t[]){ 0x0f, 0x8f }, 2) );
	as_clear(as);
	
	as_write(as, "0100 WRXB", 0, 1, 0, 1);
	st_check( code_cmp(as, (uint8_t[]){ 0b01000101 }, 1) );
	as_clear(as);
	
	as_write(as, "mm rrr bbb", 0b00, 0b111, 0b110);
	st_check( code_cmp(as, (uint8_t[]){ 0b00111110 }, 1) );
	as_clear(as);
	
	as_write(as, "xxxx xxxx", 0b10110001);
	st_check( code_cmp(as, (uint8_t[]){ 0b10110001 }, 1) );
	as_clear(as);
	
	as_write(as, "0000 1111 : xxxx 1111", 0b1010);
	st_check( code_cmp(as, (uint8_t[]){ 0x0f, 0b10101111 }, 2) );
	as_clear(as);
	
	as_write(as, "1000 0001 : %32d", 0xaabbccdd);
	st_check( code_cmp(as, (uint8_t[]){ 0b10000001, 0xdd, 0xcc, 0xbb, 0xaa }, 5) );
	as_clear(as);
	
	as_destroy(as);
}
示例#3
0
void test_write() {
	asm_p as = &(asm_t){ 0 };
	
	as_write(as, "0000 1111");
	st_check( code_cmp(as, (uint8_t[]){ 0x0f }, 1) );
	as_free(as);
	
	as_write(as, "0000 1111 : 1000 1111");
	st_check( code_cmp(as, (uint8_t[]){ 0x0f, 0x8f }, 2) );
	as_free(as);
	
	as_write(as, "0100 WRXB", 0, 1, 0, 1);
	st_check( code_cmp(as, (uint8_t[]){ 0b01000101 }, 1) );
	as_free(as);
	
	as_write(as, "mm rrr bbb", 0b00, 0b111, 0b110);
	st_check( code_cmp(as, (uint8_t[]){ 0b00111110 }, 1) );
	as_free(as);
	
	as_write(as, "xxxx xxxx", 0b10110001);
	st_check( code_cmp(as, (uint8_t[]){ 0b10110001 }, 1) );
	as_free(as);
	
	as_write(as, "0000 1111 : xxxx 1111", 0b1010);
	st_check( code_cmp(as, (uint8_t[]){ 0x0f, 0b10101111 }, 2) );
	as_free(as);
	
	as_write(as, "1000 0001 : %8d", 0xab);
	st_check( code_cmp(as, (uint8_t[]){ 0b10000001, 0xab }, 2) );
	as_free(as);
	
	// Remember, integers are little-endian in memory
	as_write(as, "1000 0001 : %16d", 0xaabb);
	st_check( code_cmp(as, (uint8_t[]){ 0b10000001, 0xbb, 0xaa }, 3) );
	as_free(as);
	
	as_write(as, "1000 0001 : %32d", 0xaabbccdd);
	st_check( code_cmp(as, (uint8_t[]){ 0b10000001, 0xdd, 0xcc, 0xbb, 0xaa }, 5) );
	as_free(as);
	
	as_write(as, "1000 0001 : %64d", 0xaabbccdd11223344);
	st_check( code_cmp(as, (uint8_t[]){ 0b10000001, 0x44, 0x33, 0x22, 0x11, 0xdd, 0xcc, 0xbb, 0xaa }, 9) );
	as_free(as);
}
示例#4
0
文件: agent.c 项目: misterlihao/MBA
/// (E)rror (C)heck (M)essage write
/// A wrapper of sending error check messages.
/// 
///     \param suc_or_fail  Determinator for error check message, TRUE for MSG_REC_SUCCESS and FALSE for MSG_REC_FAIL
///
/// Return none
static void _MOCKABLE(ecm_write)( bool suc_or_fail ) {
    
    char errorbuf[sizeof(MSG_REC_SUCCESS)];

    if ( suc_or_fail ) {
        // --------construct the result to the server-------- //
        bzero( errorbuf, sizeof(MSG_REC_SUCCESS) );
        snprintf( errorbuf, sizeof(MSG_REC_SUCCESS), MSG_REC_SUCCESS );
        // --------send the result to the server-------- //
        as_write( ac->sock, errorbuf, sizeof(MSG_REC_SUCCESS) );
    }
    else {
        // --------construct the result to the server-------- //
        bzero( errorbuf, sizeof(MSG_REC_FAIL) );
        snprintf( errorbuf, sizeof(MSG_REC_FAIL), MSG_REC_FAIL );
        // --------send the result to the server-------- //
        as_write( ac->sock, errorbuf, sizeof(MSG_REC_FAIL) );
    }
}
示例#5
0
文件: agent.c 项目: misterlihao/MBA
void agent_handle_exec_command( const char* cmdline ) {

    char cmd_emit[SZ_MAX_COMMAND];

    if( !agent_is_ready() || !agent_is_exec() )
        return;

    bzero( cmd_emit, SZ_MAX_COMMAND );
    strncpy( cmd_emit, cmdline, SZ_MAX_COMMAND - 1 );

    // append the newline character
    strcat( cmd_emit, "\n" );

    // forward execute command to agent server
    as_write( ac->sock, cmd_emit, SZ_MAX_COMMAND );
}    
示例#6
0
void test_write_elf() {
	asm_p as = &(asm_t){ 0 };
	size_t code_vaddr = 4 * 1024 * 1024;
	size_t data_vaddr = 512 * 1024 * 1024;
	
	const char* text_ptr = "Hello World!\n";
	size_t text_len = strlen(text_ptr);
	size_t text_offset = as_data(as, text_ptr, text_len);
	
	// mov RAX, 1   // write syscall
	as_write(as, "0100 000B : 1011 wrrr :  %8d", 0, 0, 0b000, 1);
	// mov RDI, 1   // stdout,             RDI = R7 = 0b0111
	as_write(as, "0100 000B : 1011 wrrr :  %8d", 0, 0, 0b111, 1);
	// mov RSI, text_vaddr   // text_ptr,  RSI = R6 = 0b0110
	as_write(as, "0100 000B : 1011 wrrr : %32d", 0, 1, 0b110, data_vaddr + text_offset);
	// mov RDX, text_len     // text len,  RDX = R2 = 0b0010
	as_write(as, "0100 000B : 1011 wrrr : %32d", 0, 1, 0b010, text_len);
	// syscall
	as_write(as, "0000 1111 : 0000 0101");
	
	// mov RAX, 60  // exit syscall
	as_write(as, "0100 000B : 1011 wrrr :  %8d", 0, 0, 0b000, 60);
	// mov RDI, 1   // exit code,          RDI = R7 = 0b0111
	as_write(as, "0100 000B : 1011 wrrr :  %8d", 0, 0, 0b111, 1);
	// syscall
	as_write(as, "0000 1111 : 0000 0101");
	
	as_save_elf(as, code_vaddr, data_vaddr, "test_write_elf.elf");
	as_free(as);
	
	char* output = NULL;
	int status_code = run_and_delete("test_write_elf.elf", "./test_write_elf.elf", &output);
	
	st_check_int(status_code, 1);
	st_check_str(output, text_ptr);
	
	free(output);
}
示例#7
0
文件: agent.c 项目: misterlihao/MBA
/// Flush the information in cache into disk.
/// Return AGENT_RET_SUCCESS if succeed or AGENT_RET_EFAIL if fail
static MBA_AGENT_RETURN _MOCKABLE(sync_cache)( void ) {

    char cmd_emit[SZ_MAX_COMMAND] = "sync" ;

    int n_wbytes;
    
    // emit command
    n_wbytes = as_write( ac->sock, cmd_emit, SZ_MAX_COMMAND );
    if( n_wbytes != SZ_MAX_COMMAND ) {
        agent_printf( "Failed to emit command '%s'\n", cmd_emit );
        goto sync_fail;
    }

    // --------Check if server is able to open file-------- //
    if ( !ecm_read() ) {
        agent_printf("Error occurs when server flushing file buffer.\n");
        goto sync_fail;
    }
    
    return AGENT_RET_SUCCESS;

sync_fail:
    return AGENT_RET_EFAIL;
}
示例#8
0
int main (int argc, char* argv[])
{                               /* --- main function */
  int    i, k = 0;              /* loop variables, buffer */
  char   *s;                    /* to traverse options */
  char   **optarg = NULL;       /* option argument */
  char   *fn_bc   = NULL;       /* name of classifier file */
  char   *fn_out  = NULL;       /* name of output file */
  char   *blank   = NULL;       /* blank */
  char   *fldsep  = NULL;       /* field  separator */
  char   *recsep  = NULL;       /* record separator */
  int    flags    = AS_ATT;     /* table file write flags */
  double lcorr    = -DBL_MAX;   /* Laplace correction value */
  int    dwnull   = 0;          /* distribute weight of null values */
  int    maxllh   = 0;          /* max. likelihood est. of variance */
  int    tplcnt   = 1000;       /* number of tuples to generate */
  long   seed;                  /* seed for random number generator */
  int    mode;                  /* classifier setup mode */

  prgname = argv[0];            /* get program name for error msgs. */
  seed    = (long)time(NULL);   /* and get a default seed value */

  /* --- print startup/usage message --- */
  if (argc > 1) {               /* if arguments are given */
    fprintf(stderr, "%s - %s\n", argv[0], DESCRIPTION);
    fprintf(stderr, VERSION); } /* print a startup message */
  else {                        /* if no argument given */
    printf("usage: %s [options] bcfile "
                     "[-d|-h hdrfile] tabfile\n", argv[0]);
    printf("%s\n", DESCRIPTION);
    printf("%s\n", VERSION);
    printf("-n#      number of tuples to generate "
                    "(default: %d)\n", tplcnt);
    printf("-s#      seed for random number generator "
                    "(default: time)\n");
    printf("-L#      Laplace correction "
                    "(default: as specified in classifier)\n");
    printf("-v/V     (do not) distribute tuple weight "
                    "for null values\n");
    printf("-m/M     (do not) use maximum likelihood estimate "
                    "for the variance\n");
    printf("-a       align fields (default: do not align)\n");
    printf("-w       do not write field names to the output file\n");
    printf("-b/f/r#  blank character, field and record separator\n"
           "         (default: \" \", \" \", \"\\n\")\n");
    printf("bcfile   file containing classifier description\n");
    printf("tabfile  table file to write\n");
    return 0;                   /* print a usage message */
  }                             /* and abort the program */

  /* --- evaluate arguments --- */
  for (i = 1; i < argc; i++) {  /* traverse arguments */
    s = argv[i];                /* get option argument */
    if (optarg) { *optarg = s; optarg = NULL; continue; }
    if ((*s == '-') && *++s) {  /* -- if argument is an option */
      while (*s) {              /* traverse options */
        switch (*s++) {         /* evaluate option */
          case 'n': tplcnt  = (int)strtol(s, &s, 0); break;
          case 's': seed    =      strtol(s, &s, 0); break;
          case 'L': lcorr   = strtod(s, &s);         break;
          case 'v': dwnull  = NBC_ALL;               break;
          case 'V': dwnull |= NBC_DWNULL|NBC_ALL;    break;
          case 'm': maxllh  = NBC_ALL;               break;
          case 'M': maxllh |= NBC_MAXLLH|NBC_ALL;    break;
          case 'a': flags  |= AS_ALIGN;              break;
          case 'w': flags  &= ~AS_ATT;               break;
          case 'b': optarg  = &blank;                break;
          case 'f': optarg  = &fldsep;               break;
          case 'r': optarg  = &recsep;               break;
          default : error(E_OPTION, *--s);           break;
        }                       /* set option variables */
        if (!*s) break;         /* if at end of string, abort loop */
        if (optarg) { *optarg = s; optarg = NULL; break; }
      } }                       /* get option argument */
    else {                      /* if argument is no option */
      switch (k++) {            /* evaluate non-option */
        case  0: fn_bc  = s;      break;
        case  1: fn_out = s;      break;
        default: error(E_ARGCNT); break;
      }                         /* note filenames */
    }
  }
  if (optarg) error(E_OPTARG);  /* check the option argument */
  if (k != 2) error(E_ARGCNT);  /* and the number of arguments */
  if ((lcorr < 0) && (lcorr > -DBL_MAX))
    error(E_NEGLC);             /* check the Laplace correction */
  if ((flags & AS_ATT) && (flags & AS_ALIGN))
    flags |= AS_ALNHDR;         /* set align to header flag */

  /* --- read Bayes classifier --- */
  scan = sc_create(fn_bc);      /* create a scanner */
  if (!scan) error((!fn_bc || !*fn_bc) ? E_NOMEM : E_FOPEN, fn_bc);
  attset = as_create("domains", att_delete);
  if (!attset) error(E_NOMEM);  /* create an attribute set */
  fprintf(stderr, "\nreading %s ... ", sc_fname(scan));
  if ((sc_nexter(scan)   <  0)  /* start scanning (get first token) */
  ||  (as_parse(attset, scan, AT_ALL) != 0)
  ||  (as_attcnt(attset) <= 0)) /* parse attribute set */
    error(E_PARSE, sc_fname(scan));
  if ((sc_token(scan) == T_ID)  /* determine classifier type */
  &&  (strcmp(sc_value(scan), "fbc") == 0))
       fbc = fbc_parse(attset, scan);
  else nbc = nbc_parse(attset, scan);
  if ((!fbc && !nbc)            /* parse the Bayes classifier */
  ||   !sc_eof(scan))           /* and check for end of file */
    error(E_PARSE, sc_fname(scan));
  sc_delete(scan); scan = NULL; /* delete the scanner */
  fprintf(stderr, "[%d attribute(s)] done.\n", as_attcnt(attset));
  if ((lcorr >= 0) || dwnull || maxllh) {
    if (lcorr < 0)              /* get the classifier's parameters */
      lcorr = (fbc) ? fbc_lcorr(fbc) : nbc_lcorr(nbc);
    mode    = (fbc) ? fbc_mode(fbc)  : nbc_mode(nbc);
    if (dwnull) mode = (mode & ~NBC_DWNULL) | dwnull;
    if (maxllh) mode = (mode & ~NBC_MAXLLH) | maxllh;
                                /* adapt the estimation parameters */
    if (fbc) fbc_setup(fbc, mode, lcorr);
    else     nbc_setup(nbc, mode, lcorr);
  }                             /* set up the classifier anew */

  /* --- generate database --- */
  if (fn_out && *fn_out)        /* if an output file name is given, */
    out = fopen(fn_out, "w");   /* open output file for writing */
  else {                        /* if no output file name is given, */
    out = stdout; fn_out = "<stdout>"; }    /* write to std. output */
  fprintf(stderr, "writing %s ... ", fn_out);
  if (!out) error(E_FOPEN, fn_out);
  if ((flags & AS_ATT)          /* if to write a table header */
  &&  (as_write(attset, out, flags) != 0))
    error(E_FWRITE, fn_out);    /* write the attributes names */
  flags = AS_INST | (flags & ~AS_ATT);
  dseed(seed);                  /* init. random number generator */
  for (i = tplcnt; --i >= 0;) { /* generate random tuples */
    if (fbc) fbc_rand(fbc, drand);   /* instantiate the */
    else     nbc_rand(nbc, drand);   /* attribute set */
    if (as_write(attset, out, flags) != 0)
      error(E_FWRITE,fn_out);   /* write the generated tuple */
  }                             /* to the output file */
  if (out != stdout) {          /* if not written to stdout */
    i = fclose(out); out = NULL;/* close the output file */
    if (i != 0) error(E_FWRITE, fn_out);
  }                             /* print a success message */
  fprintf(stderr, "[%d tuple(s)] done.\n", tplcnt);

  /* --- clean up --- */
  #ifndef NDEBUG
  if (fbc) fbc_delete(fbc, 1);  /* delete full  Bayes classifier */
  if (nbc) nbc_delete(nbc, 1);  /* or     naive Bayes classifier */
  #endif                        /* and underlying attribute set */
  #ifdef STORAGE
  showmem("at end of program"); /* check memory usage */
  #endif
  return 0;                     /* return 'ok' */
}  /* main() */
示例#9
0
文件: agent.c 项目: misterlihao/MBA
/// Export the agent server log to host
/// Return AGENT_RET_SUCCESS if succeed or AGENT_RET_EFAIL if fail
static MBA_AGENT_RETURN _MOCKABLE(export_agent_log)( void ) {

    char cmd_emit[SZ_MAX_COMMAND];

    uint64_t fsize;

    char  fbuf[SZ_MAX_FILECHUNK];
    char* fptr;

    int fd = -1;

    int n_rbytes;
    int n_wbytes;

    const char* dst_path = ac->act.dst_path;

    // construct the final command for agent server
    bzero( cmd_emit, SZ_MAX_COMMAND );
    snprintf( cmd_emit, SZ_MAX_COMMAND, "logf" );

    // emit command
    n_wbytes = as_write( ac->sock, cmd_emit, SZ_MAX_COMMAND );
    if( n_wbytes != SZ_MAX_COMMAND ) {
        agent_printf( "Failed to emit command '%s'\n", cmd_emit );
        return AGENT_RET_EFAIL;
    }

    // --------Check if server is able to duplicate log handle, set file pointer and read filesize-------- //
    if ( !ecm_read() ) {
        agent_printf("Error occurs when server duplicate log handle, set file pointer or read filesize.\n");
        return AGENT_RET_EFAIL;
    }

    // receive log file size
    n_rbytes = as_read( ac->sock, &fsize, sizeof(uint64_t) );
    if( n_rbytes != sizeof(uint64_t) ) {
        agent_printf( "Failed to receive log file size\n" );
        return AGENT_RET_EFAIL;
    }

    // open destination file to store the log
    fd = open( dst_path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
    if( fd == -1 ) {
        agent_printf( "Failed to open '%s' for agent log export\n", dst_path );
        ecm_write( FALSE );
        return AGENT_RET_EFAIL;
    }
    ecm_write( TRUE );

    // receive & store file content
    while( fsize ) {

        // measure the maximum bytes should be read
        n_rbytes = (fsize < SZ_MAX_FILECHUNK) ? fsize : SZ_MAX_FILECHUNK;

        // --------Check if server is able to read file-------- //
        if ( !ecm_read() ) {
            agent_printf("Error occurs when server reading file.\n");
            close(fd);
            return AGENT_RET_EFAIL;
        }
    
        // receive file content from agent server
        n_rbytes = as_read( ac->sock, fbuf, n_rbytes );
        if( n_rbytes <= 0 ) {
            agent_printf( "Failed to receive file content\n" );
            close(fd);
            return AGENT_RET_EFAIL;
        }

        fsize -= n_rbytes;

        // store to local file
        fptr = fbuf;
        while( n_rbytes ) {
            n_wbytes = write( fd, fptr, n_rbytes );
            if( n_wbytes == -1 ) {
                agent_printf( "Failed to store file content\n" );
                ecm_write( FALSE );
                close(fd);
                return AGENT_RET_EFAIL;
            }
            ecm_write( TRUE );

            n_rbytes -= n_wbytes;
            fptr     += n_wbytes;
       }
    }

    close( fd );
    return AGENT_RET_SUCCESS;
}
示例#10
0
文件: agent.c 项目: misterlihao/MBA
/// Execute a guest command and perform interactive stdin/stdout
/// Return AGENT_RET_SUCCESS if succeed or AGENT_RET_EFAIL if fail
static MBA_AGENT_RETURN _MOCKABLE(execute_guest_cmd_return)( void ) {
    
    char cmd_emit[SZ_MAX_COMMAND];

    uint32_t msize;
    char     msg_chunk[4096];

    int n_rbytes;
    int n_wbytes;

    const char* cmdline = ac->act.cmdline;

    int i;

    // construct the final command for agent server
    bzero( cmd_emit, SZ_MAX_COMMAND );
    snprintf( cmd_emit, SZ_MAX_COMMAND, "exec %s", cmdline );

    // emit command
    n_wbytes = as_write( ac->sock, cmd_emit, SZ_MAX_COMMAND );
    if( n_wbytes != SZ_MAX_COMMAND ) {
        agent_printf( "Failed to emit command '%s'\n", cmd_emit );
        return AGENT_RET_EFAIL;
    }

    // Check if server has finished all the preparations 
    if ( !ecm_read() ) {
        agent_printf("Error occurs when server does preparations for w_exec.\n");
        return AGENT_RET_EFAIL;
    }

    // infinite loop to receive guest output
    msize = 0;
    while( true ) {

        // get output message size
        n_rbytes = as_read( ac->sock, &msize, sizeof(uint32_t) );
        if( n_rbytes != sizeof(uint32_t) ) 
            return AGENT_RET_EFAIL;

        if( msize == 0 )
            break;
    
        // get & print message(may not be null-terminated) content
        while( msize ) {

            // measure the maximum bytes should be read
            n_rbytes = (msize < sizeof(msg_chunk)) ? msize : sizeof(msg_chunk); 

            // read output message
            n_rbytes = as_read( ac->sock, msg_chunk, n_rbytes );
            if( n_rbytes <= 0 ) {
                agent_printf( "Failed to revceive agent exec output\n" );
                return AGENT_RET_EFAIL;
            }

            for( i = 0; i < n_rbytes; ++i )
                agent_printf( "%c", msg_chunk[i] );

            msize -= n_rbytes;
        }
    }

    if ( !ecm_read() ) {
        agent_printf("Agent Server do not finish closing handles after w_exec");
        return AGENT_RET_EFAIL;
    }
   
    return AGENT_RET_SUCCESS;
}
示例#11
0
文件: agent.c 项目: misterlihao/MBA
/// Import a host file into guest
///
/// Return AGENT_RET_SUCCESS if succeed or AGENT_RET_EFAIL if fail
static MBA_AGENT_RETURN _MOCKABLE(import_host_file)( void ) {

    int fd = -1;

    struct stat fstat;
    uint64_t    fsize;
    char        fbuf[SZ_MAX_FILECHUNK];
    char*       fptr;
    
    char cmd_emit[SZ_MAX_COMMAND];

    int n_rbytes;
    int n_wbytes;

    const char* dst_path = ac->act.dst_path;
    const char* src_path = ac->act.src_path;

    // Open the target file
    fd = open( src_path, O_RDONLY );
    if( fd == -1 ) {
        agent_printf( "Failed to open '%s' for file import\n", src_path );
        return AGENT_RET_EFAIL;
    }

    // get file size
    if( stat(src_path, &fstat) == -1 ) {
        agent_printf( "Failed to get the file status of '%s'\n", src_path );
        close( fd );
        return AGENT_RET_EFAIL;
    }
    fsize = fstat.st_size;

    // construct the final command for agent server
    bzero( cmd_emit, SZ_MAX_COMMAND );
    snprintf( cmd_emit, SZ_MAX_COMMAND, "impo %s", dst_path );
    // emit command
    n_wbytes = as_write( ac->sock, cmd_emit, SZ_MAX_COMMAND );
    if( n_wbytes != SZ_MAX_COMMAND ) {
        agent_printf( "Failed to emit command '%s'\n", cmd_emit );
        close( fd );
        return AGENT_RET_EFAIL;
    }

    // send file size
    n_wbytes = as_write( ac->sock, &fsize, sizeof(uint64_t) );
    if( n_wbytes != sizeof(uint64_t) ) {
        agent_printf( "Failed to send file size: %u\n", fsize );
        close( fd );
        return AGENT_RET_EFAIL;
    }

    // --------Check if server is able to open file-------- //
    if ( !ecm_read() ) {
        agent_printf("Error occurs when server opens file.\n");
        close( fd );
        return AGENT_RET_EFAIL;
    }
    
    // read & send file content to server
    while( fsize ) {

        // read local file
        n_rbytes = read( fd, fbuf, SZ_MAX_FILECHUNK );
        if( n_rbytes == -1 ) {
            agent_printf( "Failed to read content of '%s'\n", src_path );
            ecm_write( FALSE );
            close( fd );
            return AGENT_RET_EFAIL;
        }
        ecm_write( TRUE );
        
        fsize -= n_rbytes;

        // send to agent server
        fptr = fbuf;
        while( n_rbytes ) {
            n_wbytes = as_write( ac->sock, fptr, n_rbytes );
            if( n_wbytes <= 0 ) {
                agent_printf( "Failed to send file content\n" );
                close( fd );
                return AGENT_RET_EFAIL;
            }

            // --------Check if server can write file successfully-------- //
            if ( !ecm_read() ) {
                agent_printf("Error occurs when server writes file.\n");
                close( fd );
                return AGENT_RET_EFAIL;
            }
    
            if ( n_rbytes < SZ_MAX_FILECHUNK )
                // --------Check if server set file pointer successfully-------- //
                if ( !ecm_read() ) {
                    agent_printf("Error occurs when server sets file pointer.\n");
                    close( fd );
                    return AGENT_RET_EFAIL;
                }

             n_rbytes -= n_wbytes;
             fptr     += n_wbytes;
        }
    
    }

    close( fd );
    return AGENT_RET_SUCCESS;
}
示例#12
0
文件: 01.c 项目: fenollp/kaneton
void			test_core_as_copy_01(void)
{
  i_task		task1;
  i_task		task2;
  i_as			as1;
  i_as			as2;
  i_segment		seg1;
  i_segment		seg2;
  i_segment		seg3;
  i_segment		seg4;
  i_segment		seg5;
  i_segment		seg6;
  i_segment		useless;
  i_region		reg;
  t_uint32		i;
  t_uint8		buff[4 * ___kaneton$pagesz];

  TEST_ENTER();

  /*
   * first address space
   */

  if (task_reserve(TASK_CLASS_GUEST,
		   TASK_BEHAVIOUR_INTERACTIVE,
		   TASK_PRIORITY_INTERACTIVE,
		   &task1) != STATUS_OK)
    TEST_ERROR("[task_reserve] error");

  if (as_reserve(task1, &as1) != STATUS_OK)
    TEST_ERROR("[as_reserve] error");

  if (segment_reserve(as1,
		      2 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg1) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as1,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &useless) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as1,
		      4 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg2) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as1,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &useless) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as1,
		      2 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg3) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (region_reserve(as1,
		     seg1,
		     ___kaneton$pagesz,
		     REGION_OPTION_FORCE,
		     0x20000000,
		     ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  if (region_reserve(as1,
		     seg2,
		     ___kaneton$pagesz,
		     REGION_OPTION_FORCE,
		     0x20001000,
		     2 * ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  if (region_reserve(as1,
		     seg3,
		     0,
		     REGION_OPTION_FORCE,
		     0x20003000,
		     ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  /*
   * second address space
   */

  if (task_reserve(TASK_CLASS_GUEST,
		   TASK_BEHAVIOUR_INTERACTIVE,
		   TASK_PRIORITY_INTERACTIVE,
		   &task2) != STATUS_OK)
    TEST_ERROR("[task_reserve] error");

  if (as_reserve(task2, &as2) != STATUS_OK)
    TEST_ERROR("[as_reserve] error");

  if (segment_reserve(as2,
		      2 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg4) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as2,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &useless) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as2,
		      4 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg5) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as2,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &useless) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as2,
		      2 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg6) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (region_reserve(as2,
		     seg4,
		     0,
		     REGION_OPTION_FORCE,
		     0x40000000,
		     ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  if (region_reserve(as2,
		     seg5,
		     2 * ___kaneton$pagesz,
		     REGION_OPTION_FORCE,
		     0x40001000,
		     ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  if (region_reserve(as2,
		     seg6,
		     0,
		     REGION_OPTION_FORCE,
		     0x40002000,
		     2 * ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  /*
   * operations
   */

  for (i = 0; i < 4 * ___kaneton$pagesz; i++)
    buff[i] = (i * 2 + 4) % 256;

  if (as_write(as1, buff, 4 * ___kaneton$pagesz, 0x20000000) != STATUS_OK)
    TEST_ERROR("[as_write] error");

  for (i = 0; i < 4 * ___kaneton$pagesz; i++)
    buff[i] = 0;

  if (as_copy(as1, 0x20000000, as2, 0x40000000, 4 * ___kaneton$pagesz) != STATUS_OK)
    TEST_ERROR("[as_copy] error");

  if (as_read(as2, 0x40000000, 4 * ___kaneton$pagesz, buff) != STATUS_OK)
    TEST_ERROR("[as_read] error");

  for (i = 0; i < 4 * ___kaneton$pagesz; i++)
    if (buff[i] != (i * 2 + 4) % 256)
      TEST_ERROR("the data appears invalid once read from the "
		 "address space\n");

  TEST_SIGNATURE(rr3fiw3w20aafi9gre9g);

  TEST_LEAVE();
}