Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 1 | #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 | |
| 22 | static char *tempdir = NULL; |
| 23 | |
| 24 | static 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 | */ |
| 54 | int make_tempfile(const char *template, char **out_tempname, int do_unlink) |
| 55 | { |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame^] | 56 | char *tempname; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 57 | int fd; |
| 58 | |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame^] | 59 | tempname = malloc(MAXPATHLEN); |
| 60 | |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 61 | find_tempdir(); |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame^] | 62 | if (template[0] != '/') |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 63 | strcpy(tempname, tempdir); |
| 64 | else |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame^] | 65 | tempname[0] = '\0'; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 66 | 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' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame^] | 71 | goto out; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 72 | } |
| 73 | if(do_unlink && (unlink(tempname) < 0)){ |
| 74 | perror("unlink"); |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame^] | 75 | goto out; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 76 | } |
| 77 | if(out_tempname){ |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame^] | 78 | *out_tempname = tempname; |
| 79 | } else { |
| 80 | free(tempname); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 81 | } |
| 82 | return(fd); |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame^] | 83 | out: |
| 84 | free(tempname); |
| 85 | return -1; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 86 | } |
| 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 Dike | ae17381 | 2005-11-07 00:58:57 -0800 | [diff] [blame] | 94 | int create_tmp_file(unsigned long long len) |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 95 | { |
| 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 Dike | ae17381 | 2005-11-07 00:58:57 -0800 | [diff] [blame] | 127 | int create_mem_file(unsigned long long len) |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 128 | { |
| 129 | int err, fd; |
| 130 | |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 131 | fd = create_tmp_file(len); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 132 | |
| 133 | err = os_set_exec_close(fd, 1); |
| 134 | if(err < 0){ |
| 135 | errno = -err; |
| 136 | perror("exec_close"); |
| 137 | } |
| 138 | return(fd); |
| 139 | } |