blob: 71bb90a7606d0ebf8dab332fd874aa0e0f6d707b [file] [log] [blame]
Jeff Dike0f80bc82005-09-16 19:27:50 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <stddef.h>
4#include <stdarg.h>
5#include <unistd.h>
6#include <errno.h>
7#include <string.h>
8#include <fcntl.h>
9#include <sys/types.h>
10#include <sys/mman.h>
11#include "kern_util.h"
12#include "user.h"
13#include "user_util.h"
14#include "mem_user.h"
15#include "init.h"
16#include "os.h"
17#include "tempfile.h"
18#include "kern_constants.h"
19
20#include <sys/param.h>
21
22static char *tempdir = NULL;
23
24static void __init find_tempdir(void)
25{
26 char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
27 int i;
28 char *dir = NULL;
29
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'))
34 break;
35 }
36 if((dir == NULL) || (*dir == '\0'))
37 dir = "/tmp";
38
39 tempdir = malloc(strlen(dir) + 2);
40 if(tempdir == NULL){
41 fprintf(stderr, "Failed to malloc tempdir, "
42 "errno = %d\n", errno);
43 return;
44 }
45 strcpy(tempdir, dir);
46 strcat(tempdir, "/");
47}
48
49/*
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.
53 */
54int make_tempfile(const char *template, char **out_tempname, int do_unlink)
55{
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -070056 char *tempname;
Jeff Dike0f80bc82005-09-16 19:27:50 -070057 int fd;
58
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -070059 tempname = malloc(MAXPATHLEN);
60
Jeff Dike0f80bc82005-09-16 19:27:50 -070061 find_tempdir();
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -070062 if (template[0] != '/')
Jeff Dike0f80bc82005-09-16 19:27:50 -070063 strcpy(tempname, tempdir);
64 else
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -070065 tempname[0] = '\0';
Jeff Dike0f80bc82005-09-16 19:27:50 -070066 strcat(tempname, template);
67 fd = mkstemp(tempname);
68 if(fd < 0){
69 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
70 strerror(errno));
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -070071 goto out;
Jeff Dike0f80bc82005-09-16 19:27:50 -070072 }
73 if(do_unlink && (unlink(tempname) < 0)){
74 perror("unlink");
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -070075 goto out;
Jeff Dike0f80bc82005-09-16 19:27:50 -070076 }
77 if(out_tempname){
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -070078 *out_tempname = tempname;
79 } else {
80 free(tempname);
Jeff Dike0f80bc82005-09-16 19:27:50 -070081 }
82 return(fd);
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -070083out:
84 free(tempname);
85 return -1;
Jeff Dike0f80bc82005-09-16 19:27:50 -070086}
87
88#define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
89
90/*
91 * This proc is used in start_up.c
92 * So it isn't 'static'.
93 */
Jeff Dikeae173812005-11-07 00:58:57 -080094int create_tmp_file(unsigned long long len)
Jeff Dike0f80bc82005-09-16 19:27:50 -070095{
96 int fd, err;
97 char zero;
98
99 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
100 if(fd < 0) {
101 exit(1);
102 }
103
104 err = fchmod(fd, 0777);
105 if(err < 0){
106 perror("os_mode_fd");
107 exit(1);
108 }
109
110 if (lseek64(fd, len, SEEK_SET) < 0) {
111 perror("os_seek_file");
112 exit(1);
113 }
114
115 zero = 0;
116
117 err = os_write_file(fd, &zero, 1);
118 if(err != 1){
119 errno = -err;
120 perror("os_write_file");
121 exit(1);
122 }
123
124 return(fd);
125}
126
Jeff Dikeae173812005-11-07 00:58:57 -0800127int create_mem_file(unsigned long long len)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700128{
129 int err, fd;
130
Jeff Dike02dea082006-03-31 02:30:08 -0800131 fd = create_tmp_file(len);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700132
133 err = os_set_exec_close(fd, 1);
134 if(err < 0){
135 errno = -err;
136 perror("exec_close");
137 }
138 return(fd);
139}