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)
56 char tempname[MAXPATHLEN];
61 strcpy(tempname, tempdir);
64 strcat(tempname, template);
65 fd = mkstemp(tempname);
67 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
71 if(do_unlink && (unlink(tempname) < 0)){
76 *out_tempname = strdup(tempname);
77 if(*out_tempname == NULL){
85 #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
88 * This proc is used in start_up.c
89 * So it isn't 'static'.
91 int create_tmp_file(unsigned long len)
96 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
101 err = fchmod(fd, 0777);
103 perror("os_mode_fd");
107 if (lseek64(fd, len, SEEK_SET) < 0) {
108 perror("os_seek_file");
114 err = os_write_file(fd, &zero, 1);
117 perror("os_write_file");
124 static int create_anon_file(unsigned long len)
129 fd = open("/dev/anon", O_RDWR);
131 perror("opening /dev/anon");
135 addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
136 if(addr == MAP_FAILED){
137 perror("mapping physmem file");
145 extern int have_devanon;
147 int create_mem_file(unsigned long len)
152 fd = create_anon_file(len);
153 else fd = create_tmp_file(len);
155 err = os_set_exec_close(fd, 1);
158 perror("exec_close");