]>
Commit | Line | Data |
---|---|---|
1a9d15db MH |
1 | /* |
2 | * State diagram and cleanup | |
3 | * ------------------------- | |
4 | * | |
5 | * If the program exits while a temporary file is active, we want to | |
6 | * make sure that we remove it. This is done by remembering the active | |
7 | * temporary files in a linked list, `tempfile_list`. An `atexit(3)` | |
8 | * handler and a signal handler are registered, to clean up any active | |
9 | * temporary files. | |
10 | * | |
11 | * Because the signal handler can run at any time, `tempfile_list` and | |
12 | * the `tempfile` objects that comprise it must be kept in | |
13 | * self-consistent states at all times. | |
14 | * | |
15 | * The possible states of a `tempfile` object are as follows: | |
16 | * | |
17 | * - Uninitialized. In this state the object's `on_list` field must be | |
18 | * zero but the rest of its contents need not be initialized. As | |
19 | * soon as the object is used in any way, it is irrevocably | |
20 | * registered in `tempfile_list`, and `on_list` is set. | |
21 | * | |
22 | * - Active, file open (after `create_tempfile()` or | |
23 | * `reopen_tempfile()`). In this state: | |
24 | * | |
25 | * - the temporary file exists | |
26 | * - `active` is set | |
27 | * - `filename` holds the filename of the temporary file | |
28 | * - `fd` holds a file descriptor open for writing to it | |
29 | * - `fp` holds a pointer to an open `FILE` object if and only if | |
30 | * `fdopen_tempfile()` has been called on the object | |
31 | * - `owner` holds the PID of the process that created the file | |
32 | * | |
49bd0fc2 | 33 | * - Active, file closed (after `close_tempfile_gently()`). Same |
1a9d15db MH |
34 | * as the previous state, except that the temporary file is closed, |
35 | * `fd` is -1, and `fp` is `NULL`. | |
36 | * | |
49bd0fc2 JK |
37 | * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a |
38 | * failed attempt to create a temporary file). In this state: | |
1a9d15db MH |
39 | * |
40 | * - `active` is unset | |
41 | * - `filename` is empty (usually, though there are transitory | |
42 | * states in which this condition doesn't hold). Client code should | |
43 | * *not* rely on the filename being empty in this state. | |
44 | * - `fd` is -1 and `fp` is `NULL` | |
45 | * - the object is left registered in the `tempfile_list`, and | |
46 | * `on_list` is set. | |
47 | * | |
48 | * A temporary file is owned by the process that created it. The | |
49 | * `tempfile` has an `owner` field that records the owner's PID. This | |
50 | * field is used to prevent a forked process from deleting a temporary | |
51 | * file created by its parent. | |
52 | */ | |
53 | ||
54 | #include "cache.h" | |
55 | #include "tempfile.h" | |
56 | #include "sigchain.h" | |
57 | ||
58 | static struct tempfile *volatile tempfile_list; | |
59 | ||
60 | static void remove_tempfiles(int skip_fclose) | |
61 | { | |
62 | pid_t me = getpid(); | |
63 | ||
64 | while (tempfile_list) { | |
65 | if (tempfile_list->owner == me) { | |
66 | /* fclose() is not safe to call in a signal handler */ | |
67 | if (skip_fclose) | |
68 | tempfile_list->fp = NULL; | |
69 | delete_tempfile(tempfile_list); | |
70 | } | |
71 | tempfile_list = tempfile_list->next; | |
72 | } | |
73 | } | |
74 | ||
75 | static void remove_tempfiles_on_exit(void) | |
76 | { | |
77 | remove_tempfiles(0); | |
78 | } | |
79 | ||
80 | static void remove_tempfiles_on_signal(int signo) | |
81 | { | |
82 | remove_tempfiles(1); | |
83 | sigchain_pop(signo); | |
84 | raise(signo); | |
85 | } | |
86 | ||
7eba6ce5 MH |
87 | /* |
88 | * Initialize *tempfile if necessary and add it to tempfile_list. | |
89 | */ | |
90 | static void prepare_tempfile_object(struct tempfile *tempfile) | |
1a9d15db | 91 | { |
1a9d15db MH |
92 | if (!tempfile_list) { |
93 | /* One-time initialization */ | |
94 | sigchain_push_common(remove_tempfiles_on_signal); | |
95 | atexit(remove_tempfiles_on_exit); | |
96 | } | |
97 | ||
e6fc2673 | 98 | if (is_tempfile_active(tempfile)) |
9b028aa4 | 99 | BUG("prepare_tempfile_object called for active object"); |
1a9d15db MH |
100 | if (!tempfile->on_list) { |
101 | /* Initialize *tempfile and add it to tempfile_list: */ | |
102 | tempfile->fd = -1; | |
103 | tempfile->fp = NULL; | |
104 | tempfile->active = 0; | |
105 | tempfile->owner = 0; | |
7eba6ce5 | 106 | strbuf_init(&tempfile->filename, 0); |
1a9d15db MH |
107 | tempfile->next = tempfile_list; |
108 | tempfile_list = tempfile; | |
109 | tempfile->on_list = 1; | |
110 | } else if (tempfile->filename.len) { | |
111 | /* This shouldn't happen, but better safe than sorry. */ | |
9b028aa4 | 112 | BUG("prepare_tempfile_object called for improperly-reset object"); |
1a9d15db | 113 | } |
7eba6ce5 MH |
114 | } |
115 | ||
116 | /* Make sure errno contains a meaningful value on error */ | |
117 | int create_tempfile(struct tempfile *tempfile, const char *path) | |
118 | { | |
119 | prepare_tempfile_object(tempfile); | |
1a9d15db MH |
120 | |
121 | strbuf_add_absolute_path(&tempfile->filename, path); | |
05d1ed61 BW |
122 | tempfile->fd = open(tempfile->filename.buf, |
123 | O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666); | |
124 | if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL) | |
125 | /* Try again w/o O_CLOEXEC: the kernel might not support it */ | |
126 | tempfile->fd = open(tempfile->filename.buf, | |
127 | O_RDWR | O_CREAT | O_EXCL, 0666); | |
1a9d15db MH |
128 | if (tempfile->fd < 0) { |
129 | strbuf_reset(&tempfile->filename); | |
130 | return -1; | |
131 | } | |
132 | tempfile->owner = getpid(); | |
133 | tempfile->active = 1; | |
134 | if (adjust_shared_perm(tempfile->filename.buf)) { | |
135 | int save_errno = errno; | |
136 | error("cannot fix permission bits on %s", tempfile->filename.buf); | |
137 | delete_tempfile(tempfile); | |
138 | errno = save_errno; | |
139 | return -1; | |
140 | } | |
141 | return tempfile->fd; | |
142 | } | |
143 | ||
99397152 MH |
144 | void register_tempfile(struct tempfile *tempfile, const char *path) |
145 | { | |
146 | prepare_tempfile_object(tempfile); | |
147 | strbuf_add_absolute_path(&tempfile->filename, path); | |
148 | tempfile->owner = getpid(); | |
149 | tempfile->active = 1; | |
150 | } | |
151 | ||
354ab112 MH |
152 | int mks_tempfile_sm(struct tempfile *tempfile, |
153 | const char *template, int suffixlen, int mode) | |
154 | { | |
155 | prepare_tempfile_object(tempfile); | |
156 | ||
157 | strbuf_add_absolute_path(&tempfile->filename, template); | |
158 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); | |
159 | if (tempfile->fd < 0) { | |
160 | strbuf_reset(&tempfile->filename); | |
161 | return -1; | |
162 | } | |
163 | tempfile->owner = getpid(); | |
164 | tempfile->active = 1; | |
165 | return tempfile->fd; | |
166 | } | |
167 | ||
168 | int mks_tempfile_tsm(struct tempfile *tempfile, | |
169 | const char *template, int suffixlen, int mode) | |
170 | { | |
171 | const char *tmpdir; | |
172 | ||
173 | prepare_tempfile_object(tempfile); | |
174 | ||
175 | tmpdir = getenv("TMPDIR"); | |
176 | if (!tmpdir) | |
177 | tmpdir = "/tmp"; | |
178 | ||
179 | strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template); | |
180 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); | |
181 | if (tempfile->fd < 0) { | |
182 | strbuf_reset(&tempfile->filename); | |
183 | return -1; | |
184 | } | |
185 | tempfile->owner = getpid(); | |
186 | tempfile->active = 1; | |
187 | return tempfile->fd; | |
188 | } | |
189 | ||
190 | int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode) | |
191 | { | |
192 | int fd; | |
193 | struct strbuf full_template = STRBUF_INIT; | |
194 | ||
195 | strbuf_add_absolute_path(&full_template, template); | |
196 | fd = mks_tempfile_m(tempfile, full_template.buf, mode); | |
197 | if (fd < 0) | |
198 | die_errno("Unable to create temporary file '%s'", | |
199 | full_template.buf); | |
200 | ||
201 | strbuf_release(&full_template); | |
202 | return fd; | |
203 | } | |
204 | ||
1a9d15db MH |
205 | FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) |
206 | { | |
e6fc2673 | 207 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 208 | BUG("fdopen_tempfile() called for inactive object"); |
1a9d15db | 209 | if (tempfile->fp) |
9b028aa4 | 210 | BUG("fdopen_tempfile() called for open object"); |
1a9d15db MH |
211 | |
212 | tempfile->fp = fdopen(tempfile->fd, mode); | |
213 | return tempfile->fp; | |
214 | } | |
215 | ||
216 | const char *get_tempfile_path(struct tempfile *tempfile) | |
217 | { | |
e6fc2673 | 218 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 219 | BUG("get_tempfile_path() called for inactive object"); |
1a9d15db MH |
220 | return tempfile->filename.buf; |
221 | } | |
222 | ||
223 | int get_tempfile_fd(struct tempfile *tempfile) | |
224 | { | |
e6fc2673 | 225 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 226 | BUG("get_tempfile_fd() called for inactive object"); |
1a9d15db MH |
227 | return tempfile->fd; |
228 | } | |
229 | ||
230 | FILE *get_tempfile_fp(struct tempfile *tempfile) | |
231 | { | |
e6fc2673 | 232 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 233 | BUG("get_tempfile_fp() called for inactive object"); |
1a9d15db MH |
234 | return tempfile->fp; |
235 | } | |
236 | ||
49bd0fc2 | 237 | int close_tempfile_gently(struct tempfile *tempfile) |
1a9d15db | 238 | { |
f5b4dc76 JK |
239 | int fd; |
240 | FILE *fp; | |
1a9d15db MH |
241 | int err; |
242 | ||
f5b4dc76 | 243 | if (!is_tempfile_active(tempfile) || tempfile->fd < 0) |
1a9d15db MH |
244 | return 0; |
245 | ||
f5b4dc76 JK |
246 | fd = tempfile->fd; |
247 | fp = tempfile->fp; | |
1a9d15db MH |
248 | tempfile->fd = -1; |
249 | if (fp) { | |
250 | tempfile->fp = NULL; | |
7e8c9355 JK |
251 | if (ferror(fp)) { |
252 | err = -1; | |
253 | if (!fclose(fp)) | |
254 | errno = EIO; | |
255 | } else { | |
256 | err = fclose(fp); | |
257 | } | |
1a9d15db MH |
258 | } else { |
259 | err = close(fd); | |
260 | } | |
261 | ||
49bd0fc2 | 262 | return err ? -1 : 0; |
1a9d15db MH |
263 | } |
264 | ||
265 | int reopen_tempfile(struct tempfile *tempfile) | |
266 | { | |
e6fc2673 | 267 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 268 | BUG("reopen_tempfile called for an inactive object"); |
f5b4dc76 | 269 | if (0 <= tempfile->fd) |
9b028aa4 | 270 | BUG("reopen_tempfile called for an open object"); |
1a9d15db MH |
271 | tempfile->fd = open(tempfile->filename.buf, O_WRONLY); |
272 | return tempfile->fd; | |
273 | } | |
274 | ||
275 | int rename_tempfile(struct tempfile *tempfile, const char *path) | |
276 | { | |
e6fc2673 | 277 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 278 | BUG("rename_tempfile called for inactive object"); |
1a9d15db | 279 | |
49bd0fc2 JK |
280 | if (close_tempfile_gently(tempfile)) { |
281 | delete_tempfile(tempfile); | |
1a9d15db | 282 | return -1; |
49bd0fc2 | 283 | } |
1a9d15db MH |
284 | |
285 | if (rename(tempfile->filename.buf, path)) { | |
286 | int save_errno = errno; | |
287 | delete_tempfile(tempfile); | |
288 | errno = save_errno; | |
289 | return -1; | |
290 | } | |
291 | ||
292 | tempfile->active = 0; | |
293 | strbuf_reset(&tempfile->filename); | |
294 | return 0; | |
295 | } | |
296 | ||
297 | void delete_tempfile(struct tempfile *tempfile) | |
298 | { | |
e6fc2673 | 299 | if (!is_tempfile_active(tempfile)) |
1a9d15db MH |
300 | return; |
301 | ||
49bd0fc2 JK |
302 | close_tempfile_gently(tempfile); |
303 | unlink_or_warn(tempfile->filename.buf); | |
304 | tempfile->active = 0; | |
305 | strbuf_reset(&tempfile->filename); | |
1a9d15db | 306 | } |