/* Take a program binary from clGetPropramInfo() and a replacement IL source as a string. Replace the .amdil section in the ELF image and return a new copy of the binary. */ unsigned char* getModifiedAMDBinary(unsigned char* bin, size_t binSize, int nStream, MWCALtargetEnum target, size_t* newBinSizeOut) { int fd = -1; int rc = 0; char tmpBinFile[128]; unsigned char* newBin = NULL; if (!bin) return NULL; getTmpBinaryName(tmpBinFile, sizeof(tmpBinFile)); /* Write binary to a temporary file since we need a file descriptor for libelf */ fd = open(tmpBinFile, openMode, openPermMode); if (fd < 0) { mwPerror("Failed to open AMD binary file '%s", tmpBinFile); return NULL; } if (write(fd, bin, binSize) <= 0) { mwPerror("Failed to write temporary binary file '%s'", tmpBinFile); return NULL; } rc = processElf(fd, nStream, target); if (rc == 0) { if (lseek(fd, 0, SEEK_SET) != 0) { mwPerror("Failed to seek temporary binary file '%s'", tmpBinFile); return NULL; } newBin = (unsigned char*) readFD(fd, newBinSizeOut); } if (close(fd) < 0) { mwPerror("Failed to close binary file '%s'", tmpBinFile); free(newBin); return NULL; } mw_remove(tmpBinFile); return newBin; }
int isVioConsole(void) { #if !defined(__powerpc__) return 0; #else int fd, i; char *buf, *start; char driver[50], device[50]; static int isviocons = -1; if (isviocons != -1) return isviocons; fd = open("/proc/tty/drivers", O_RDONLY); if (fd < 0) { fprintf(stderr, "failed to open /proc/tty/drivers!\n"); return 0; } i = readFD(fd, &buf); if (i < 1) { close(fd); fprintf(stderr, "error reading /proc/tty/drivers!\n"); return 0; } close(fd); buf[i] = '\0'; isviocons = 0; start = buf; while (start && *start) { if (sscanf(start, "%s %s", (char *) &driver, (char *) &device) == 2) { if (!strcmp(driver, "vioconsole") && !strcmp(device, "/dev/tty")) { isviocons = 1; break; } } start = strchr(start, '\n'); if (start) start++; } free(buf); return isviocons; #endif }
int doPwMount(char *dev, char *where, char *fs, char *options, char **err) { int rc, child, status, pipefd[2]; char *opts = NULL, *device; if (mkdirChain(where)) return IMOUNT_ERR_ERRNO; if (strstr(fs, "nfs")) { if (options) { if (asprintf(&opts, "%s,nolock", options) == -1) { fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__, strerror(errno)); fflush(stderr); abort(); } } else { opts = strdup("nolock"); } device = strdup(dev); } else { if ((options && strstr(options, "bind") == NULL) && strncmp(dev, "LABEL=", 6) && strncmp(dev, "UUID=", 5) && *dev != '/') { if (asprintf(&device, "/dev/%s", dev) == -1) { fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__, strerror(errno)); fflush(stderr); abort(); } } else { device = strdup(dev); } if (options) opts = strdup(options); } if (pipe(pipefd)) return IMOUNT_ERR_ERRNO; if (!(child = fork())) { int fd; close(pipefd[0]); /* Close stdin entirely, redirect stdout to tty5, and redirect stderr * to a pipe so we can put error messages into exceptions. We'll * only use these messages should mount also return an error code. */ fd = open("/dev/tty5", O_RDONLY); close(STDIN_FILENO); dup2(fd, STDIN_FILENO); close(fd); fd = open("/dev/tty5", O_WRONLY); close(STDOUT_FILENO); dup2(fd, STDOUT_FILENO); dup2(pipefd[1], STDERR_FILENO); if (opts) { fprintf(stdout, "Running... /bin/mount -n -t %s -o %s %s %s\n", fs, opts, device, where); rc = execl("/bin/mount", "/bin/mount", "-n", "-t", fs, "-o", opts, device, where, NULL); exit(1); } else { fprintf(stdout, "Running... /bin/mount -n -t %s %s %s\n", fs, device, where); rc = execl("/bin/mount", "/bin/mount", "-n", "-t", fs, device, where, NULL); exit(1); } } close(pipefd[1]); if (err != NULL) rc = readFD(pipefd[0], err); close(pipefd[0]); waitpid(child, &status, 0); free(opts); free(device); if (!WIFEXITED(status) || (WIFEXITED(status) && WEXITSTATUS(status))) return IMOUNT_ERR_OTHER; return 0; }