title: Is this usage of mlockall correct?
body: <p>The program below XORs 2 files to create an output file using one time pad encryption. I have attempted to use <code>mlockall</code> in order to avoid any traces of the keyfile being left on the hard drive when getting the keyfile from external memory sources.</p>
  
  <p>From the mlockall man page:</p>
  
  <p><code>mlock() and mlockall() respectively lock part or all of the calling process's
         virtual address space into RAM, preventing that memory from being paged to the
         swap area.</code></p>
  
  <p>How I do check if it is working and have I used <code>mlockall</code> correctly?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/mman.h&gt;
  
  int main(int argc, char **argv)
  {
  struct stat statbuf;
  struct stat keybuf;
  
  char buffer [20];
  int key;
  int data;
  int output;
  int count;
  char ans;
  int * buf;
  FILE * keyfile;
  FILE * sourcefile;
  FILE * destfile;
  
  if(geteuid() !=0)
  {
  printf("Root access is required to run this program\n\n");
  exit(0);
  }
  
  if(argc&lt;4)
  {
  printf("OTP-Bunny 1.0\n");
  printf("USAGE: OTP &lt;source file&gt; &lt;output file&gt; &lt;keyfile&gt;\n");
  return (0);
  }
  
  /* Check number of arguments. */
  if(argc&gt;4)
  {
  printf("Too many arguments.\n");
  printf("USAGE: OTP &lt;source file&gt; &lt;output file&gt; &lt;keyfile&gt;\n");
  exit(1);
  }
  
  /* Allocate memory required by processes */
  buf = (int*) malloc (sizeof(int));
  if (buf == NULL)
  {
  perror("Error");
  exit(1);
  }
  
  /* Lock down pages mapped to processes */
  printf("Locking down processes\n");
  if(mlockall (MCL_CURRENT | MCL_FUTURE) &lt; 0)
  {
  perror("mlockall");
  exit (1);
  }
  
  
  /* Check if sourcefile can be opened. */
  if((sourcefile = fopen(argv[1], "rb"))== NULL)
  {
  printf("Can't open source file\n");
  perror("Error");
  printf("USAGE: OTP &lt;source file&gt; &lt;output file&gt; &lt;keyfile&gt;\n");
  exit (1);
  }
  
  /* Get size of sourcefile */
  fstat(fileno(sourcefile), &amp;statbuf); 
  
  /* Check if keyfile can be opened. */
  if((keyfile = fopen(argv[3], "rb"))== NULL)
  {
  printf("Can't open keyfile.\n");
  perror("Error");
  printf("USAGE: OTP &lt;source file&gt; &lt;output file&gt; &lt;keyfile&gt;\n");
  exit(1);
  }                               
  
  /* Get size of keyfile */
  fstat(fileno(keyfile), &amp;keybuf);
  
  /* Check if keyfile is the same size as, or bigger than the sourcefile */
  if((keybuf.st_size) &lt; (statbuf.st_size))
  {
  printf("Source file is larger than keyfile.\n");
  printf("This significantly reduces cryptographic strength.\n");
  printf("Do you wish to continue? (Y/N)\n");
  fgets(buffer, 20, stdin);
  sscanf(buffer, "%c", &amp;ans);
  if(ans == 'n' || ans == 'N')
  {
  exit (1);
  }
  if(ans == 'y' || ans == 'Y')
  {
      printf("Proceeding with Encryption/Decryption.\n");
      }
  else
  {
  printf("No option selected. Exiting...\n");
  exit (1);
  }
  }   
  
  /* Check if destfile can be opened. */
  if((destfile = fopen(argv[2], "wb"))== NULL)
  {
  printf("Can't open output file.\n");
  perror("Error");
  exit(1);                    
  }    
  
  /* Encrypt/Decrypt and write to output file. */
  while(count &lt; (statbuf.st_size))
  {
  key=fgetc(keyfile);
  data=fgetc(sourcefile);
  
  output=(key^data);
  
  fputc(output,destfile);
  count++;
  }
  
  /* Close files. */
  fclose(keyfile);
  fclose(sourcefile);
  fclose(destfile);
  
  printf("Encryption/Decryption Complete.\n\n");
  
  /* delete keyfile option. */
  printf("Do you wish to delete the keyfile? (Y/N)\n");
  fgets(buffer, 20, stdin);
  sscanf(buffer, "%c", &amp;ans);
  if(ans == 'y' || ans == 'Y')
  {
      if ( remove(argv[3]) == 0)
      {
      printf("File deleted successfully.\n");
      }
      else
      {
      printf("Unable to delete the file.\n");
      perror("Error");
      exit(1);
      }
  }
  
  /* cleanup */
  printf("Releasing memory\n");
  free (buf);
  return(0);
  }
  </code></pre>