| @@ -0,0 +1,164 @@ |
| | 1 | #include <stdio.h> |
| | 2 | #include <stdlib.h> |
| | 3 | #include <string.h> |
| | 4 | #include <fcntl.h> |
| | 5 | #include <unistd.h> |
| | 6 | #include <sys/stat.h> |
| | 7 | #include <sys/time.h> |
| | 8 | #include <sys/wait.h> |
| | 9 | #include <libgen.h> |
| | 10 | #include <openssl/sha.h> |
| | 11 | |
| | 12 | #define nil NULL |
| | 13 | |
| | 14 | int |
| | 15 | main(int argc, char **argv) |
| | 16 | { |
| | 17 | char *path, *e, *origexe, *s, exe[4096], *argv0, *in, *out, *dirout, dhh[SHA256_DIGEST_LENGTH*2+1]; |
| | 18 | uint8_t tmp[65536], dh[SHA256_DIGEST_LENGTH]; |
| | 19 | int d, f, i, argout; |
| | 20 | SHA256_CTX shactx; |
| | 21 | struct stat st; |
| | 22 | ssize_t n; |
| | 23 | pid_t pid; |
| | 24 | |
| | 25 | if((dirout = getenv("LLCACHE_DIR")) == nil){ |
| | 26 | fprintf(stderr, "LLCACHE_DIR not set\n"); |
| | 27 | goto err; |
| | 28 | } |
| | 29 | if((argv0 = basename(argv[0])) == nil){ |
| | 30 | fprintf(stderr, "argv[0] is not a proper path\n"); |
| | 31 | goto err; |
| | 32 | } |
| | 33 | |
| | 34 | if((n = readlink("/proc/self/exe", exe, sizeof(exe))) >= (ssize_t)sizeof(exe)){ |
| | 35 | exesmall: |
| | 36 | fprintf(stderr, "exe buffer too small\n"); |
| | 37 | goto err; |
| | 38 | }else if(n < 0){ |
| | 39 | perror("exe buffer"); |
| | 40 | goto err; |
| | 41 | } |
| | 42 | exe[n] = 0; |
| | 43 | origexe = realpath(exe, nil); |
| | 44 | |
| | 45 | path = strdup(getenv("PATH")); |
| | 46 | for(s = path; s != nil; s = e ? e + 1 : nil){ |
| | 47 | if((e = strchr(s, ':')) != nil) |
| | 48 | *e = 0; |
| | 49 | if((d = open(s, O_DIRECTORY|O_RDONLY)) < 0) |
| | 50 | continue; |
| | 51 | n = readlinkat(d, argv0, exe, sizeof(exe)); |
| | 52 | if(n >= (ssize_t)sizeof(exe)) |
| | 53 | goto exesmall; |
| | 54 | if(n < 0){ |
| | 55 | if((n = fstatat(d, argv0, &st, 0)) == 0) |
| | 56 | exe[0] = 0; |
| | 57 | } |
| | 58 | close(d); |
| | 59 | if(n < 0 || strcmp(origexe, exe) == 0) |
| | 60 | continue; |
| | 61 | if(snprintf(exe, sizeof(exe), "%s/%s", s, argv0) >= (int)sizeof(exe)) |
| | 62 | goto exesmall; |
| | 63 | if((s = realpath(exe, nil)) == nil || strcmp(origexe, s) == 0){ |
| | 64 | free(s); |
| | 65 | continue; |
| | 66 | } |
| | 67 | |
| | 68 | break; |
| | 69 | } |
| | 70 | if(s == nil){ |
| | 71 | fprintf(stderr, "%s: not found\n", argv0); |
| | 72 | goto err; |
| | 73 | } |
| | 74 | |
| | 75 | SHA256_Init(&shactx); |
| | 76 | SHA256_Update(&shactx, s, strlen(s)+1); |
| | 77 | |
| | 78 | in = nil; |
| | 79 | out = nil; |
| | 80 | argout = -1; |
| | 81 | for(i = 1; i < argc; i++){ |
| | 82 | if(argv[i][0] == '/'){ |
| | 83 | if(strcmp(argv[i-1], "-o") == 0){ |
| | 84 | out = argv[i]; |
| | 85 | argout = i; |
| | 86 | }else{ |
| | 87 | in = argv[i]; |
| | 88 | } |
| | 89 | }else{ |
| | 90 | SHA256_Update(&shactx, argv[i], strlen(argv[i])+1); |
| | 91 | } |
| | 92 | } |
| | 93 | if(in == nil || out == nil) |
| | 94 | goto run; |
| | 95 | |
| | 96 | if((f = open(in, O_RDONLY)) < 0){ |
| | 97 | inerr: |
| | 98 | perror("input"); |
| | 99 | goto err; |
| | 100 | } |
| | 101 | for(;;){ |
| | 102 | if((n = read(f, tmp, sizeof(tmp))) == 0) |
| | 103 | break; |
| | 104 | if(n < 0) |
| | 105 | goto inerr; |
| | 106 | SHA256_Update(&shactx, tmp, n); |
| | 107 | } |
| | 108 | close(f); |
| | 109 | |
| | 110 | SHA256_Final(dh, &shactx); |
| | 111 | for(i = 0; i < (int)sizeof(dh); i++) |
| | 112 | sprintf(&dhh[i*2], "%02x", dh[i]); |
| | 113 | |
| | 114 | pid = 0; |
| | 115 | copy: |
| | 116 | if((d = open(dirout, O_DIRECTORY)) >= 0 && (f = openat(d, dhh, O_RDONLY)) >= 0 && fstat(f, &st) == 0){ |
| | 117 | close(d); |
| | 118 | if((d = creat(out, st.st_mode)) < 0){ |
| | 119 | perror("creat"); |
| | 120 | goto err; |
| | 121 | } |
| | 122 | for(;;){ |
| | 123 | if((n = read(f, tmp, sizeof(tmp))) == 0) |
| | 124 | break; |
| | 125 | if(n < 0) |
| | 126 | goto inerr; |
| | 127 | if(write(d, tmp, n) != n){ |
| | 128 | perror("output"); |
| | 129 | goto err; |
| | 130 | } |
| | 131 | } |
| | 132 | close(f); |
| | 133 | close(d); |
| | 134 | return 0; |
| | 135 | } |
| | 136 | |
| | 137 | if(pid != 0){ |
| | 138 | fprintf(stderr, "endless loop?\n"); |
| | 139 | goto err; |
| | 140 | } |
| | 141 | |
| | 142 | run: |
| | 143 | if((pid = fork()) < 0){ |
| | 144 | perror("fork"); |
| | 145 | goto err; |
| | 146 | }else if(pid == 0){ |
| | 147 | if(argout > 0){ |
| | 148 | snprintf((char*)tmp, sizeof(tmp), "%s/%s", dirout, dhh); |
| | 149 | argv[argout] = (char*)tmp; |
| | 150 | } |
| | 151 | execvp(s, argv); |
| | 152 | perror("exec"); |
| | 153 | goto err; |
| | 154 | } |
| | 155 | |
| | 156 | if(waitpid(pid, &i, 0) < 0){ |
| | 157 | perror("waitpid"); |
| | 158 | goto err; |
| | 159 | } |
| | 160 | if(i == 0) |
| | 161 | goto copy; |
| | 162 | err: |
| | 163 | return 1; |
| | 164 | } |