]>
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` | |
422a21c6 | 45 | * - the object is removed from `tempfile_list` (but could be used again) |
1a9d15db MH |
46 | * |
47 | * A temporary file is owned by the process that created it. The | |
48 | * `tempfile` has an `owner` field that records the owner's PID. This | |
49 | * field is used to prevent a forked process from deleting a temporary | |
50 | * file created by its parent. | |
51 | */ | |
52 | ||
53 | #include "cache.h" | |
54 | #include "tempfile.h" | |
55 | #include "sigchain.h" | |
56 | ||
24d82185 | 57 | static VOLATILE_LIST_HEAD(tempfile_list); |
1a9d15db | 58 | |
6b935066 | 59 | static void remove_tempfiles(int in_signal_handler) |
1a9d15db MH |
60 | { |
61 | pid_t me = getpid(); | |
24d82185 JK |
62 | volatile struct volatile_list_head *pos; |
63 | ||
64 | list_for_each(pos, &tempfile_list) { | |
65 | struct tempfile *p = list_entry(pos, struct tempfile, list); | |
1a9d15db | 66 | |
6b935066 JK |
67 | if (!is_tempfile_active(p) || p->owner != me) |
68 | continue; | |
69 | ||
70 | if (p->fd >= 0) | |
71 | close(p->fd); | |
72 | ||
73 | if (in_signal_handler) | |
74 | unlink(p->filename.buf); | |
75 | else | |
76 | unlink_or_warn(p->filename.buf); | |
77 | ||
78 | p->active = 0; | |
1a9d15db MH |
79 | } |
80 | } | |
81 | ||
82 | static void remove_tempfiles_on_exit(void) | |
83 | { | |
84 | remove_tempfiles(0); | |
85 | } | |
86 | ||
87 | static void remove_tempfiles_on_signal(int signo) | |
88 | { | |
89 | remove_tempfiles(1); | |
90 | sigchain_pop(signo); | |
91 | raise(signo); | |
92 | } | |
93 | ||
7eba6ce5 | 94 | static void prepare_tempfile_object(struct tempfile *tempfile) |
1a9d15db | 95 | { |
422a21c6 JK |
96 | tempfile->fd = -1; |
97 | tempfile->fp = NULL; | |
98 | tempfile->active = 0; | |
99 | tempfile->owner = 0; | |
100 | INIT_LIST_HEAD(&tempfile->list); | |
101 | strbuf_init(&tempfile->filename, 0); | |
7eba6ce5 MH |
102 | } |
103 | ||
2933ebba JK |
104 | static void activate_tempfile(struct tempfile *tempfile) |
105 | { | |
422a21c6 JK |
106 | static int initialized; |
107 | ||
108 | if (is_tempfile_active(tempfile)) | |
109 | BUG("activate_tempfile called for active object"); | |
110 | ||
111 | if (!initialized) { | |
112 | sigchain_push_common(remove_tempfiles_on_signal); | |
113 | atexit(remove_tempfiles_on_exit); | |
114 | initialized = 1; | |
115 | } | |
116 | ||
117 | volatile_list_add(&tempfile->list, &tempfile_list); | |
2933ebba JK |
118 | tempfile->owner = getpid(); |
119 | tempfile->active = 1; | |
120 | } | |
121 | ||
b5f4dcb5 JK |
122 | static void deactivate_tempfile(struct tempfile *tempfile) |
123 | { | |
124 | tempfile->active = 0; | |
102cf7a6 | 125 | strbuf_release(&tempfile->filename); |
422a21c6 | 126 | volatile_list_del(&tempfile->list); |
b5f4dcb5 JK |
127 | } |
128 | ||
7eba6ce5 MH |
129 | /* Make sure errno contains a meaningful value on error */ |
130 | int create_tempfile(struct tempfile *tempfile, const char *path) | |
131 | { | |
132 | prepare_tempfile_object(tempfile); | |
1a9d15db MH |
133 | |
134 | strbuf_add_absolute_path(&tempfile->filename, path); | |
05d1ed61 BW |
135 | tempfile->fd = open(tempfile->filename.buf, |
136 | O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666); | |
137 | if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL) | |
138 | /* Try again w/o O_CLOEXEC: the kernel might not support it */ | |
139 | tempfile->fd = open(tempfile->filename.buf, | |
140 | O_RDWR | O_CREAT | O_EXCL, 0666); | |
1a9d15db | 141 | if (tempfile->fd < 0) { |
b5f4dcb5 | 142 | deactivate_tempfile(tempfile); |
1a9d15db MH |
143 | return -1; |
144 | } | |
2933ebba | 145 | activate_tempfile(tempfile); |
1a9d15db MH |
146 | if (adjust_shared_perm(tempfile->filename.buf)) { |
147 | int save_errno = errno; | |
148 | error("cannot fix permission bits on %s", tempfile->filename.buf); | |
149 | delete_tempfile(tempfile); | |
150 | errno = save_errno; | |
151 | return -1; | |
152 | } | |
153 | return tempfile->fd; | |
154 | } | |
155 | ||
99397152 MH |
156 | void register_tempfile(struct tempfile *tempfile, const char *path) |
157 | { | |
158 | prepare_tempfile_object(tempfile); | |
159 | strbuf_add_absolute_path(&tempfile->filename, path); | |
2933ebba | 160 | activate_tempfile(tempfile); |
99397152 MH |
161 | } |
162 | ||
354ab112 MH |
163 | int mks_tempfile_sm(struct tempfile *tempfile, |
164 | const char *template, int suffixlen, int mode) | |
165 | { | |
166 | prepare_tempfile_object(tempfile); | |
167 | ||
168 | strbuf_add_absolute_path(&tempfile->filename, template); | |
169 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); | |
170 | if (tempfile->fd < 0) { | |
b5f4dcb5 | 171 | deactivate_tempfile(tempfile); |
354ab112 MH |
172 | return -1; |
173 | } | |
2933ebba | 174 | activate_tempfile(tempfile); |
354ab112 MH |
175 | return tempfile->fd; |
176 | } | |
177 | ||
178 | int mks_tempfile_tsm(struct tempfile *tempfile, | |
179 | const char *template, int suffixlen, int mode) | |
180 | { | |
181 | const char *tmpdir; | |
182 | ||
183 | prepare_tempfile_object(tempfile); | |
184 | ||
185 | tmpdir = getenv("TMPDIR"); | |
186 | if (!tmpdir) | |
187 | tmpdir = "/tmp"; | |
188 | ||
189 | strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template); | |
190 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); | |
191 | if (tempfile->fd < 0) { | |
b5f4dcb5 | 192 | deactivate_tempfile(tempfile); |
354ab112 MH |
193 | return -1; |
194 | } | |
2933ebba | 195 | activate_tempfile(tempfile); |
354ab112 MH |
196 | return tempfile->fd; |
197 | } | |
198 | ||
199 | int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode) | |
200 | { | |
201 | int fd; | |
202 | struct strbuf full_template = STRBUF_INIT; | |
203 | ||
204 | strbuf_add_absolute_path(&full_template, template); | |
205 | fd = mks_tempfile_m(tempfile, full_template.buf, mode); | |
206 | if (fd < 0) | |
207 | die_errno("Unable to create temporary file '%s'", | |
208 | full_template.buf); | |
209 | ||
210 | strbuf_release(&full_template); | |
211 | return fd; | |
212 | } | |
213 | ||
1a9d15db MH |
214 | FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) |
215 | { | |
e6fc2673 | 216 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 217 | BUG("fdopen_tempfile() called for inactive object"); |
1a9d15db | 218 | if (tempfile->fp) |
9b028aa4 | 219 | BUG("fdopen_tempfile() called for open object"); |
1a9d15db MH |
220 | |
221 | tempfile->fp = fdopen(tempfile->fd, mode); | |
222 | return tempfile->fp; | |
223 | } | |
224 | ||
225 | const char *get_tempfile_path(struct tempfile *tempfile) | |
226 | { | |
e6fc2673 | 227 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 228 | BUG("get_tempfile_path() called for inactive object"); |
1a9d15db MH |
229 | return tempfile->filename.buf; |
230 | } | |
231 | ||
232 | int get_tempfile_fd(struct tempfile *tempfile) | |
233 | { | |
e6fc2673 | 234 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 235 | BUG("get_tempfile_fd() called for inactive object"); |
1a9d15db MH |
236 | return tempfile->fd; |
237 | } | |
238 | ||
239 | FILE *get_tempfile_fp(struct tempfile *tempfile) | |
240 | { | |
e6fc2673 | 241 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 242 | BUG("get_tempfile_fp() called for inactive object"); |
1a9d15db MH |
243 | return tempfile->fp; |
244 | } | |
245 | ||
49bd0fc2 | 246 | int close_tempfile_gently(struct tempfile *tempfile) |
1a9d15db | 247 | { |
f5b4dc76 JK |
248 | int fd; |
249 | FILE *fp; | |
1a9d15db MH |
250 | int err; |
251 | ||
f5b4dc76 | 252 | if (!is_tempfile_active(tempfile) || tempfile->fd < 0) |
1a9d15db MH |
253 | return 0; |
254 | ||
f5b4dc76 JK |
255 | fd = tempfile->fd; |
256 | fp = tempfile->fp; | |
1a9d15db MH |
257 | tempfile->fd = -1; |
258 | if (fp) { | |
259 | tempfile->fp = NULL; | |
7e8c9355 JK |
260 | if (ferror(fp)) { |
261 | err = -1; | |
262 | if (!fclose(fp)) | |
263 | errno = EIO; | |
264 | } else { | |
265 | err = fclose(fp); | |
266 | } | |
1a9d15db MH |
267 | } else { |
268 | err = close(fd); | |
269 | } | |
270 | ||
49bd0fc2 | 271 | return err ? -1 : 0; |
1a9d15db MH |
272 | } |
273 | ||
274 | int reopen_tempfile(struct tempfile *tempfile) | |
275 | { | |
e6fc2673 | 276 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 277 | BUG("reopen_tempfile called for an inactive object"); |
f5b4dc76 | 278 | if (0 <= tempfile->fd) |
9b028aa4 | 279 | BUG("reopen_tempfile called for an open object"); |
1a9d15db MH |
280 | tempfile->fd = open(tempfile->filename.buf, O_WRONLY); |
281 | return tempfile->fd; | |
282 | } | |
283 | ||
284 | int rename_tempfile(struct tempfile *tempfile, const char *path) | |
285 | { | |
e6fc2673 | 286 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 287 | BUG("rename_tempfile called for inactive object"); |
1a9d15db | 288 | |
49bd0fc2 JK |
289 | if (close_tempfile_gently(tempfile)) { |
290 | delete_tempfile(tempfile); | |
1a9d15db | 291 | return -1; |
49bd0fc2 | 292 | } |
1a9d15db MH |
293 | |
294 | if (rename(tempfile->filename.buf, path)) { | |
295 | int save_errno = errno; | |
296 | delete_tempfile(tempfile); | |
297 | errno = save_errno; | |
298 | return -1; | |
299 | } | |
300 | ||
b5f4dcb5 | 301 | deactivate_tempfile(tempfile); |
1a9d15db MH |
302 | return 0; |
303 | } | |
304 | ||
305 | void delete_tempfile(struct tempfile *tempfile) | |
306 | { | |
e6fc2673 | 307 | if (!is_tempfile_active(tempfile)) |
1a9d15db MH |
308 | return; |
309 | ||
49bd0fc2 JK |
310 | close_tempfile_gently(tempfile); |
311 | unlink_or_warn(tempfile->filename.buf); | |
b5f4dcb5 | 312 | deactivate_tempfile(tempfile); |
1a9d15db | 313 | } |