Search code examples
csegmentation-faultmemsetinvalid-argument

aio_write and memset invalid argument and Segmentation Fault (core dumped)


I'm trying to fill my file with 'a' characters and I need tu use aio_write. Here's my writing function

int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
   int rv = 0;
  // memset( (void *)aiorp, 'a', sizeof( struct aiocb ) ); // <--- I get Invalid argument (Error da_aio_write)

  // memset(&aiorp, 'a', sizeof( struct aiocb )); // <--- I get Segmentation Fault (core dumped)
   aiorp->aio_fildes = d;
   aiorp->aio_buf = buf;
   aiorp->aio_nbytes = count;
   aiorp->aio_offset = 0;

   rv = aio_write( aiorp );

   if( rv == -1) {
       perror("Error da_aio_write\n");
       exit(1);
       return rv;
   }
   return rv;
}

When I use memset( (void *)aiorp, 0, sizeof( struct aiocb ) ); // <--- I get Invalid argument (Error da_aio_write) so my rv == -1 and I get perror printed And when I use memset(&aiorp, 'a', sizeof( struct aiocb )); // <--- I get Segmentation Fault (core dumped) Why my both memset's not working? Also I add my full code

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <string.h>
#include <aio.h>
 #include <errno.h>

#define MB 1024

int da_open(const char *name);
int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count);
int da_test_wait( struct aiocb *aiorp );
int da_close(int fd);

int da_open(const char *name){
   int dskr;
   int dskr2;
   dskr = open( name, O_RDWR );
   if( dskr == -1 ){
       printf("File created\n");
       dskr2 = open( name, O_WRONLY | O_CREAT, 0644);
   }else{
       printf("End job!\n");
       exit(1);
   }
   printf( "dskr1 = %d\n", dskr2 );
   return dskr2;
}

int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
   int rv = 0;
   //memset( (void *)aiorp, 'a', sizeof( struct aiocb ) );  // <--- I get Invalid argument (Error da_aio_write)
   //memset(&aiorp, 'a', sizeof( struct aiocb )); / <--- I get Segmentation Fault (core dumped)
   aiorp->aio_fildes = d;
   aiorp->aio_buf = buf;
   aiorp->aio_nbytes = count;
   aiorp->aio_offset = 0;

   rv = aio_write( aiorp );

   if( rv == -1) {
       perror("Error da_aio_write\n");
       exit(1);
       return rv;
   }
   return rv;
}

int da_test_wait( struct aiocb *aiorp ){
   const struct aiocb *aioptr[1];
   int rv;
   aioptr[0] = aiorp;
   rv = aio_suspend( aioptr, 1, NULL );
   if( rv != 0 ){
      perror( "aio_suspend failed" );
      abort();
   }
   rv = aio_return( aiorp );
   printf( "AIO complete, %d bytes write.\n", rv );
   return 1;
}

int da_close(int fd){
   int rv;
   rv = close( fd );
   if( rv != 0 ) perror ( "close() failed" );
   else puts( "closed" );
   return rv;
}

int main(int argc, char *argv[] ){
        int sk;
        int d;
        struct aiocb aior;
        if(argc == 3){
                sk = atoi(argv[2]);
                char buffer[MB * MB * sk];
                int size; 
                size = MB * MB * sk;
                memset( buffer, '\0', size);
                //memset(&aior, '\0', sizeof( struct aiocb ));
                d = da_open(argv[1]);
                da_aio_write( d, &aior, buffer, sizeof(buffer) );
                da_test_wait( &aior );
                da_close( d );
        }
        return 0;
}

Solution

  • If you want to set each byte of aiorp to the ASCII value of 'a', your first varaiant:

    memset((void *) aiorp, 'a', sizeof(struct aiocb));
    

    is good. (You don't need the cast to void *, though, and could rewrite the size to sizeof(*aiorp) to follow a common pattern.)

    But why do you want to do that? That's the control, struct, which now contains a lot of (nonsensical) data made up of 'a' bytes, except the values that you explicitly overwrite afterwards.

    No wonder you get an EINVAL, for which "One or more of aio_offset, aio_reqprio, aio_nbytes are invalid", according to the manpage: aiorp->aio_erqprio is 0x61616161.

    You want to set the data buffer to contain 'a's:

    memset(buf, 'a', count);
    

    (But given that your aiocb structure is allocated in local storage in main and thus uninitialised, you might consider memsetting it to all zeros with the above call.)