static void process_udp_child(int sfd, const char *host, const char *port, int cpu, int page_size) { struct sockaddr_storage peer_addr; socklen_t peer_addr_len; char buf[page_size]; char *tempfile; int cfd; int fd; int n; int once = 0; signal_setup(SIGUSR1, finish); tempfile = get_temp_file(host, port, cpu); fd = open(tempfile, O_WRONLY | O_TRUNC | O_CREAT, 0644); if (fd < 0) pdie("creating %s", tempfile); if (use_tcp) { if (listen(sfd, backlog) < 0) pdie("listen"); peer_addr_len = sizeof(peer_addr); cfd = accept(sfd, (struct sockaddr *)&peer_addr, &peer_addr_len); if (cfd < 0 && errno == EINTR) goto done; if (cfd < 0) pdie("accept"); close(sfd); sfd = cfd; } do { /* TODO, make this copyless! */ n = read(sfd, buf, page_size); if (n < 0) { if (errno == EINTR) continue; pdie("reading client"); } if (!n) break; /* UDP requires that we get the full size in one go */ if (!use_tcp && n < page_size && !once) { once = 1; warning("read %d bytes, expected %d", n, page_size); } write(fd, buf, n); } while (!done); done: put_temp_file(tempfile); exit(0); }
static gchar* get_temp_file_maybe (MuMsg *msg, MuMsgPart *part, MuMsgOptions opts) { opts |= MU_MSG_OPTION_USE_EXISTING; if (!(opts & MU_MSG_OPTION_EXTRACT_IMAGES) || g_ascii_strcasecmp (part->type, "image") != 0) return NULL; return get_temp_file (msg, opts, part->index); }
static char *insert_temp_file(char *pattern, int where, int skip) { char *fname, *pat = pattern; int i, l, rgb = mem_img_bpp == 3, fform = FT_NONE; while (TRUE) { /* Skip initial whitespace if any */ pat += strspn(pat, " \t"); /* Finish if not a transform request */ if (*pat != '>') break; l = strcspn(++pat, "> \t"); if (!strncasecmp("RGB", pat, l)) rgb = TRUE; else { for (i = FT_NONE + 1; i < NUM_FTYPES; i++) { fformat *ff = file_formats + i; if ((ff->flags & FF_IMAGE) && !(ff->flags & FF_NOSAVE) && (!strncasecmp(ff->name, pat, l) || !strncasecmp(ff->ext, pat, l) || (ff->ext2[0] && !strncasecmp(ff->ext2, pat, l)))) fform = i; } } pat += l; } where -= pat - pattern; if (where < 0) return (NULL); // Syntax error if (fform != FT_NONE) { unsigned int flags = file_formats[fform].flags; if (rgb && !(flags & FF_RGB)) fform = FT_NONE; // No way else if (flags & FF_SAVE_MASK); // Is OK else if (flags & FF_RGB) rgb = TRUE; // Fallback else fform = FT_NONE; // Give up } fname = get_temp_file(fform, rgb); if (!fname) return (NULL); /* Temp save failed */ return (wjstrcat(NULL, 0, pat, where, "\"", fname, "\"", pat + where + skip, NULL)); }
static int winhttp_stream_write_buffered( git_smart_subtransport_stream *stream, const char *buffer, size_t len) { winhttp_stream *s = (winhttp_stream *)stream; winhttp_subtransport *t = OWNING_SUBTRANSPORT(s); DWORD bytes_written; if (!s->request && winhttp_stream_connect(s) < 0) return -1; /* Buffer the payload, using a temporary file so we delegate * memory management of the data to the operating system. */ if (!s->post_body) { wchar_t temp_path[MAX_PATH + 1]; if (get_temp_file(temp_path, MAX_PATH + 1) < 0) return -1; s->post_body = CreateFileW(temp_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE, NULL, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN, NULL); if (INVALID_HANDLE_VALUE == s->post_body) { s->post_body = NULL; giterr_set(GITERR_OS, "Failed to create temporary file"); return -1; } } if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) { giterr_set(GITERR_OS, "Failed to write to temporary file"); return -1; } assert((DWORD)len == bytes_written); s->post_body_len += bytes_written; return 0; }
static const char* compile_to_object(struct options* opts, const char* file, const char* lang) { struct options copts; char* base_name; /* make a copy so we don't change any of the initial stuff */ /* a shallow copy is exactly what we want in this case */ base_name = get_basename(file); copts = *opts; copts.output_name = get_temp_file(base_name, ".o"); copts.compile_only = 1; copts.files = strarray_alloc(); strarray_add(copts.files, file); compile(&copts, lang); strarray_free(copts.files); free(base_name); return copts.output_name; }
static void build(struct options* opts) { static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" }; strarray *lib_dirs, *files; strarray *spec_args, *link_args; char *output_file; const char *spec_o_name; const char *output_name, *spec_file, *lang; const char* winebuild = getenv("WINEBUILD"); int generate_app_loader = 1; unsigned int j; /* NOTE: for the files array we'll use the following convention: * -axxx: xxx is an archive (.a) * -dxxx: xxx is a DLL (.def) * -lxxx: xxx is an unsorted library * -oxxx: xxx is an object (.o) * -rxxx: xxx is a resource (.res) * -sxxx: xxx is a shared lib (.so) * -xlll: lll is the language (c, c++, etc.) */ if (!winebuild) winebuild = "winebuild"; output_file = strdup( opts->output_name ? opts->output_name : "a.out" ); /* 'winegcc -o app xxx.exe.so' only creates the load script */ if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so")) { create_file(output_file, 0755, app_loader_template, opts->files->base[0]); return; } /* generate app loader only for .exe */ if (opts->shared || strendswith(output_file, ".exe.so")) generate_app_loader = 0; /* normalize the filename a bit: strip .so, ensure it has proper ext */ if (strendswith(output_file, ".so")) output_file[strlen(output_file) - 3] = 0; if (opts->shared) { if ((output_name = strrchr(output_file, '/'))) output_name++; else output_name = output_file; if (!strchr(output_name, '.')) output_file = strmake("%s.dll", output_file); } else if (!strendswith(output_file, ".exe")) output_file = strmake("%s.exe", output_file); /* get the filename from the path */ if ((output_name = strrchr(output_file, '/'))) output_name++; else output_name = output_file; /* prepare the linking path */ if (!opts->wine_objdir) { lib_dirs = strarray_dup(opts->lib_dirs); for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ ) strarray_add(lib_dirs, stdlibpath[j]); } else { lib_dirs = strarray_alloc(); strarray_add(lib_dirs, strmake("%s/dlls", opts->wine_objdir)); strarray_add(lib_dirs, strmake("%s/libs/wine", opts->wine_objdir)); strarray_addall(lib_dirs, opts->lib_dirs); } /* mark the files with their appropriate type */ spec_file = lang = 0; files = strarray_alloc(); for ( j = 0; j < opts->files->size; j++ ) { const char* file = opts->files->base[j]; if (file[0] != '-') { switch(get_file_type(file)) { case file_def: case file_spec: if (spec_file) error("Only one spec file can be specified\n"); spec_file = file; break; case file_rc: /* FIXME: invoke wrc to build it */ error("Can't compile .rc file at the moment: %s\n", file); break; case file_res: strarray_add(files, strmake("-r%s", file)); break; case file_obj: strarray_add(files, strmake("-o%s", file)); break; case file_arh: strarray_add(files, strmake("-a%s", file)); break; case file_so: strarray_add(files, strmake("-s%s", file)); break; case file_na: error("File does not exist: %s\n", file); break; default: file = compile_to_object(opts, file, lang); strarray_add(files, strmake("-o%s", file)); break; } } else if (file[1] == 'l') add_library( lib_dirs, files, file + 2 ); else if (file[1] == 'x') lang = file; } if (opts->shared && !spec_file) error("A spec file is currently needed in shared mode\n"); /* add the default libraries, if needed */ if (!opts->nostdlib && opts->use_msvcrt) add_library(lib_dirs, files, "msvcrt"); if (!opts->wine_objdir && !opts->nodefaultlibs) { if (opts->gui_app) { add_library(lib_dirs, files, "shell32"); add_library(lib_dirs, files, "comdlg32"); add_library(lib_dirs, files, "gdi32"); } add_library(lib_dirs, files, "advapi32"); add_library(lib_dirs, files, "user32"); add_library(lib_dirs, files, "kernel32"); } if (!opts->nostartfiles) add_library(lib_dirs, files, "winecrt0"); if (!opts->nostdlib) add_library(lib_dirs, files, "wine"); /* run winebuild to generate the .spec.o file */ spec_args = strarray_alloc(); spec_o_name = get_temp_file(output_name, ".spec.o"); strarray_add(spec_args, winebuild); if (verbose) strarray_add(spec_args, "-v"); if (keep_generated) strarray_add(spec_args, "--save-temps"); strarray_add(spec_args, "--as-cmd"); strarray_add(spec_args, AS); strarray_add(spec_args, "--ld-cmd"); strarray_add(spec_args, LD); strarray_addall(spec_args, strarray_fromstring(DLLFLAGS, " ")); strarray_add(spec_args, opts->shared ? "--dll" : "--exe"); strarray_add(spec_args, "-o"); strarray_add(spec_args, spec_o_name); if (spec_file) { strarray_add(spec_args, "-E"); strarray_add(spec_args, spec_file); } if (!opts->shared) { strarray_add(spec_args, "-F"); strarray_add(spec_args, output_name); strarray_add(spec_args, "--subsystem"); strarray_add(spec_args, opts->gui_app ? "windows" : "console"); if (opts->unicode_app) { strarray_add(spec_args, "--entry"); strarray_add(spec_args, "__wine_spec_exe_wentry"); } } for ( j = 0; j < lib_dirs->size; j++ ) strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j])); for ( j = 0 ; j < opts->winebuild_args->size ; j++ ) strarray_add(spec_args, opts->winebuild_args->base[j]); for ( j = 0; j < files->size; j++ ) { const char* name = files->base[j] + 2; switch(files->base[j][1]) { case 'r': strarray_add(spec_args, files->base[j]); break; case 'd': case 'a': case 'o': strarray_add(spec_args, name); break; } } spawn(opts->prefix, spec_args, 0); /* link everything together now */ link_args = strarray_alloc(); strarray_addall(link_args, get_translator(opts->processor)); strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " ")); strarray_add(link_args, "-o"); strarray_add(link_args, strmake("%s.so", output_file)); for ( j = 0 ; j < opts->linker_args->size ; j++ ) strarray_add(link_args, opts->linker_args->base[j]); #ifdef __APPLE__ if (opts->image_base) { strarray_add(link_args, "-image_base"); strarray_add(link_args, opts->image_base); } #endif for ( j = 0; j < lib_dirs->size; j++ ) strarray_add(link_args, strmake("-L%s", lib_dirs->base[j])); strarray_add(link_args, spec_o_name); for ( j = 0; j < files->size; j++ ) { const char* name = files->base[j] + 2; switch(files->base[j][1]) { case 'l': case 's': strarray_add(link_args, strmake("-l%s", name)); break; case 'a': case 'o': strarray_add(link_args, name); break; } } if (!opts->nostdlib) { strarray_add(link_args, "-lm"); strarray_add(link_args, "-lc"); } spawn(opts->prefix, link_args, 0); /* set the base address */ if (opts->image_base) { const char *prelink = PRELINK; if (prelink[0] && strcmp(prelink,"false")) { strarray *prelink_args = strarray_alloc(); strarray_add(prelink_args, prelink); strarray_add(prelink_args, "--reloc-only"); strarray_add(prelink_args, opts->image_base); strarray_add(prelink_args, strmake("%s.so", output_file)); spawn(opts->prefix, prelink_args, 1); strarray_free(prelink_args); } } /* create the loader script */ if (generate_app_loader) { if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0; create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name)); } }
static void process_client(const char *node, const char *port, int fd) { char **temp_files; char buf[BUFSIZ]; char *option; int *port_array; int *pid_array; int pagesize; int start_port; int udp_port; int options; int size; int cpus; int cpu; int pid; int ofd; int n, s, t, i; /* Let the client know what we are */ write(fd, "tracecmd", 8); /* read back the CPU count */ n = read_string(fd, buf, BUFSIZ); if (n == BUFSIZ) /** ERROR **/ return; cpus = atoi(buf); plog("cpus=%d\n", cpus); if (cpus < 0) return; /* next read the page size */ n = read_string(fd, buf, BUFSIZ); if (n == BUFSIZ) /** ERROR **/ return; pagesize = atoi(buf); plog("pagesize=%d\n", pagesize); if (pagesize <= 0) return; /* Now the number of options */ n = read_string(fd, buf, BUFSIZ); if (n == BUFSIZ) /** ERROR **/ return; options = atoi(buf); for (i = 0; i < options; i++) { /* next is the size of the options */ n = read_string(fd, buf, BUFSIZ); if (n == BUFSIZ) /** ERROR **/ return; size = atoi(buf); /* prevent a client from killing us */ if (size > MAX_OPTION_SIZE) return; option = malloc_or_die(size); do { t = size; s = 0; s = read(fd, option+s, t); if (s <= 0) return; t -= s; s = size - t; } while (t); s = process_option(option); free(option); /* do we understand this option? */ if (!s) return; } if (use_tcp) plog("Using TCP for live connection\n"); /* Create the client file */ snprintf(buf, BUFSIZ, "%s.%s:%s.dat", output_file, node, port); ofd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0644); if (ofd < 0) pdie("Can not create file %s", buf); port_array = malloc_or_die(sizeof(int) * cpus); pid_array = malloc_or_die(sizeof(int) * cpus); memset(pid_array, 0, sizeof(int) * cpus); start_port = START_PORT_SEARCH; /* Now create a UDP port for each CPU */ for (cpu = 0; cpu < cpus; cpu++) { udp_port = open_udp(node, port, &pid, cpu, pagesize, start_port); if (udp_port < 0) goto out_free; port_array[cpu] = udp_port; pid_array[cpu] = pid; /* due to some bugging finding ports, force search after last port */ start_port = udp_port+1; } /* send the client a comma deliminated set of port numbers */ for (cpu = 0; cpu < cpus; cpu++) { snprintf(buf, BUFSIZ, "%s%d", cpu ? "," : "", port_array[cpu]); write(fd, buf, strlen(buf)); } /* end with null terminator */ write(fd, "\0", 1); /* Now we are ready to start reading data from the client */ do { n = read(fd, buf, BUFSIZ); if (n < 0) { if (errno == EINTR) continue; pdie("reading client"); } t = n; s = 0; do { s = write(ofd, buf+s, t); if (s < 0) { if (errno == EINTR) break; pdie("writing to file"); } t -= s; s = n - t; } while (t); } while (n > 0 && !done); /* wait a little to let our readers finish reading */ sleep(1); /* stop our readers */ for (cpu = 0; cpu < cpus; cpu++) { if (pid_array[cpu] > 0) kill(pid_array[cpu], SIGUSR1); } /* wait a little to have the readers clean up */ sleep(1); /* Now put together the file */ temp_files = malloc_or_die(sizeof(*temp_files) * cpus); for (cpu = 0; cpu < cpus; cpu++) temp_files[cpu] = get_temp_file(node, port, cpu); tracecmd_attach_cpu_data_fd(ofd, cpus, temp_files); out_free: for (cpu = 0; cpu < cpus; cpu++) { if (pid_array[cpu] > 0) { kill(pid_array[cpu], SIGKILL); delete_temp_file(node, port, cpu); pid_array[cpu] = 0; } } }