Advertisement
wowonline

Untitled

Dec 12th, 2021
743
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5.  
  6. enum { NAME_SIZE = 23 };
  7.  
  8. int
  9. main(int argc, char *argv[])
  10. {
  11.     int fd_r = open("/dev/urandom", O_RDONLY);
  12.     if (fd_r == -1) {
  13.         return 1;
  14.     }
  15.     unsigned long long num_r;
  16.     read(fd_r, &num_r, sizeof(num_r));
  17.     char filename[NAME_SIZE];
  18.     snprintf(filename, NAME_SIZE, "%llu%s", num_r, ".c");
  19.  
  20.     pid_t pid;
  21.     char input[NAME_SIZE];
  22.     int fd = open(filename, O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
  23.     FILE *f = fdopen(fd, "w");
  24.     if (f == NULL) {
  25.         return 1;
  26.     }
  27.     char *prog_beg =
  28.     "#include <stdio.h>\n"
  29.     "#include <stdlib.h>\n"
  30.     "int\n"
  31.     "main(int argc, char *argv[])\n"
  32.     "{\n"
  33.     "\tint x;\n"
  34.     "\tchar *summon = \"summon\";\n"
  35.     "\tchar *reject = \"reject\";\n"
  36.     "\tchar *disqualify = \"disqualify\";\n"
  37.     "\twhile (scanf(\"%d\", &x) == 1) {\n"
  38.     "\t\tprintf(\"%s\\n\", ";
  39.     char *prog_end =
  40.     ");\n"
  41.     "\t}\n"
  42.     "\treturn 0;\n"
  43.     "}";
  44.  
  45.     fprintf(f, "%s", prog_beg);
  46.     fprintf(f, "%s", argv[1]);
  47.     fprintf(f, "%s", prog_end);
  48.  
  49.     char filename_exec[NAME_SIZE+4];
  50.     read(fd_r, &num_r, sizeof(num_r));
  51.     close(fd_r);
  52.     snprintf(filename_exec, sizeof(filename_exec), "./%llu.o", num_r);
  53.  
  54.     pid = fork();
  55.     if (pid == -1) {
  56.         return 1;
  57.     } else if (!pid) {
  58.         execlp("gcc", "gcc", filename, "-o", filename_exec, NULL);
  59.         _exit(1);
  60.     }
  61.     wait(NULL);
  62.  
  63.     pid = fork();
  64.     if (pid == -1) {
  65.         return 1;
  66.     } else if (!pid) {
  67.         execlp(filename_exec, filename_exec, input, NULL);
  68.         _exit(1);
  69.     }
  70.     wait(NULL);
  71.  
  72.  
  73.     return 0;
  74. }
Advertisement
RAW Paste Data Copied
Advertisement