Skip to content

Commit 57b235a

Browse files
peffgitster
authored andcommitted
refactor signal handling for cleanup functions
The current code is very inconsistent about which signals are caught for doing cleanup of temporary files and lock files. Some callsites checked only SIGINT, while others checked a variety of death-dealing signals. This patch factors out those signals to a single function, and then calls it everywhere. For some sites, that means this is a simple clean up. For others, it is an improvement in that they will now properly clean themselves up after a larger variety of signals. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4a16d07 commit 57b235a

File tree

8 files changed

+17
-13
lines changed

8 files changed

+17
-13
lines changed

builtin-clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
439439
}
440440
junk_git_dir = git_dir;
441441
atexit(remove_junk);
442-
sigchain_push(SIGINT, remove_junk_on_signal);
442+
sigchain_push_common(remove_junk_on_signal);
443443

444444
setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
445445

builtin-fetch--tool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static int fetch_native_store(FILE *fp,
246246
char buffer[1024];
247247
int err = 0;
248248

249-
sigchain_push(SIGINT, remove_keep_on_signal);
249+
sigchain_push_common(remove_keep_on_signal);
250250
atexit(remove_keep);
251251

252252
while (fgets(buffer, sizeof(buffer), stdin)) {

builtin-fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
673673
ref_nr = j;
674674
}
675675

676-
sigchain_push(SIGINT, unlock_pack_on_signal);
676+
sigchain_push_common(unlock_pack_on_signal);
677677
atexit(unlock_pack);
678678
exit_code = do_fetch(transport,
679679
parse_fetch_refspec(ref_nr, refs), ref_nr);

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
19031903

19041904
if (!remove_tempfile_installed) {
19051905
atexit(remove_tempfile);
1906-
sigchain_push(SIGINT, remove_tempfile_on_signal);
1906+
sigchain_push_common(remove_tempfile_on_signal);
19071907
remove_tempfile_installed = 1;
19081908
}
19091909

http-push.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,10 +2262,7 @@ int main(int argc, char **argv)
22622262
goto cleanup;
22632263
}
22642264

2265-
sigchain_push(SIGINT, remove_locks_on_signal);
2266-
sigchain_push(SIGHUP, remove_locks_on_signal);
2267-
sigchain_push(SIGQUIT, remove_locks_on_signal);
2268-
sigchain_push(SIGTERM, remove_locks_on_signal);
2265+
sigchain_push_common(remove_locks_on_signal);
22692266

22702267
/* Check whether the remote has server info files */
22712268
remote->can_update_info_refs = 0;

lockfile.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
137137
lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
138138
if (0 <= lk->fd) {
139139
if (!lock_file_list) {
140-
sigchain_push(SIGINT, remove_lock_file_on_signal);
141-
sigchain_push(SIGHUP, remove_lock_file_on_signal);
142-
sigchain_push(SIGTERM, remove_lock_file_on_signal);
143-
sigchain_push(SIGQUIT, remove_lock_file_on_signal);
144-
sigchain_push(SIGPIPE, remove_lock_file_on_signal);
140+
sigchain_push_common(remove_lock_file_on_signal);
145141
atexit(remove_lock_file);
146142
}
147143
lk->owner = getpid();

sigchain.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ int sigchain_pop(int sig)
4141
s->n--;
4242
return 0;
4343
}
44+
45+
void sigchain_push_common(sigchain_fun f)
46+
{
47+
sigchain_push(SIGINT, f);
48+
sigchain_push(SIGHUP, f);
49+
sigchain_push(SIGTERM, f);
50+
sigchain_push(SIGQUIT, f);
51+
sigchain_push(SIGPIPE, f);
52+
}

sigchain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ typedef void (*sigchain_fun)(int);
66
int sigchain_push(int sig, sigchain_fun f);
77
int sigchain_pop(int sig);
88

9+
void sigchain_push_common(sigchain_fun f);
10+
911
#endif /* SIGCHAIN_H */

0 commit comments

Comments
 (0)