11 #include "kern_util.h"
13 #include "user_util.h"
18 #include "kern_constants.h"
20 #include <sys/param.h>
22 static char *tempdir = NULL;
24 static void __init find_tempdir(void)
26 char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
30 if(tempdir != NULL) return; /* We've already been called */
31 for(i = 0; dirs[i]; i++){
32 dir = getenv(dirs[i]);
33 if((dir != NULL) && (*dir != '\0'))
36 if((dir == NULL) || (*dir == '\0'))
39 tempdir = malloc(strlen(dir) + 2);
41 fprintf(stderr, "Failed to malloc tempdir, "
42 "errno = %d\n", errno);
50 * This proc still used in tt-mode
51 * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
52 * So it isn't 'static' yet.
54 int make_tempfile(const char *template, char **out_tempname, int do_unlink)
59 tempname = malloc(MAXPATHLEN);
62 if (template[0] != '/')
63 strcpy(tempname, tempdir);
66 strcat(tempname, template);
67 fd = mkstemp(tempname);
69 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
73 if(do_unlink && (unlink(tempname) < 0)){
78 *out_tempname = tempname;
88 #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
91 * This proc is used in start_up.c
92 * So it isn't 'static'.
94 int create_tmp_file(unsigned long long len)
99 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
104 err = fchmod(fd, 0777);
106 perror("os_mode_fd");
110 if (lseek64(fd, len, SEEK_SET) < 0) {
111 perror("os_seek_file");
117 err = os_write_file(fd, &zero, 1);
120 perror("os_write_file");
127 int create_mem_file(unsigned long long len)
131 fd = create_tmp_file(len);
133 err = os_set_exec_close(fd, 1);
136 perror("exec_close");