Skip to content

Commit fa5bc8a

Browse files
committed
Merge branch 'jk/signal-cleanup'
* jk/signal-cleanup: t0005: use SIGTERM for sigchain test pager: do wait_for_pager on signal death refactor signal handling for cleanup functions chain kill signals for cleanup functions diff: refactor tempfile cleanup handling Windows: Fix signal numbers
2 parents 2edefe3 + 0ea8039 commit fa5bc8a

File tree

14 files changed

+197
-75
lines changed

14 files changed

+197
-75
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ test-match-trees
153153
test-parse-options
154154
test-path-utils
155155
test-sha1
156+
test-sigchain
156157
common-cmds.h
157158
*.tar.gz
158159
*.dsc

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ LIB_H += revision.h
391391
LIB_H += run-command.h
392392
LIB_H += sha1-lookup.h
393393
LIB_H += sideband.h
394+
LIB_H += sigchain.h
394395
LIB_H += strbuf.h
395396
LIB_H += tag.h
396397
LIB_H += transport.h
@@ -484,6 +485,7 @@ LIB_OBJS += sha1-lookup.o
484485
LIB_OBJS += sha1_name.o
485486
LIB_OBJS += shallow.o
486487
LIB_OBJS += sideband.o
488+
LIB_OBJS += sigchain.o
487489
LIB_OBJS += strbuf.o
488490
LIB_OBJS += symlinks.o
489491
LIB_OBJS += tag.o
@@ -1374,6 +1376,7 @@ TEST_PROGRAMS += test-match-trees$X
13741376
TEST_PROGRAMS += test-parse-options$X
13751377
TEST_PROGRAMS += test-path-utils$X
13761378
TEST_PROGRAMS += test-sha1$X
1379+
TEST_PROGRAMS += test-sigchain$X
13771380

13781381
all:: $(TEST_PROGRAMS)
13791382

builtin-clone.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "strbuf.h"
2020
#include "dir.h"
2121
#include "pack-refs.h"
22+
#include "sigchain.h"
2223

2324
/*
2425
* Overall FIXMEs:
@@ -288,7 +289,7 @@ static void remove_junk(void)
288289
static void remove_junk_on_signal(int signo)
289290
{
290291
remove_junk();
291-
signal(SIGINT, SIG_DFL);
292+
sigchain_pop(signo);
292293
raise(signo);
293294
}
294295

@@ -441,7 +442,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
441442
}
442443
junk_git_dir = git_dir;
443444
atexit(remove_junk);
444-
signal(SIGINT, remove_junk_on_signal);
445+
sigchain_push_common(remove_junk_on_signal);
445446

446447
setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
447448

builtin-fetch--tool.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "cache.h"
33
#include "refs.h"
44
#include "commit.h"
5+
#include "sigchain.h"
56

67
static char *get_stdin(void)
78
{
@@ -186,7 +187,7 @@ static void remove_keep(void)
186187
static void remove_keep_on_signal(int signo)
187188
{
188189
remove_keep();
189-
signal(SIGINT, SIG_DFL);
190+
sigchain_pop(signo);
190191
raise(signo);
191192
}
192193

@@ -245,7 +246,7 @@ static int fetch_native_store(FILE *fp,
245246
char buffer[1024];
246247
int err = 0;
247248

248-
signal(SIGINT, remove_keep_on_signal);
249+
sigchain_push_common(remove_keep_on_signal);
249250
atexit(remove_keep);
250251

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

builtin-fetch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "transport.h"
1111
#include "run-command.h"
1212
#include "parse-options.h"
13+
#include "sigchain.h"
1314

1415
static const char * const builtin_fetch_usage[] = {
1516
"git fetch [options] [<repository> <refspec>...]",
@@ -58,7 +59,7 @@ static void unlock_pack(void)
5859
static void unlock_pack_on_signal(int signo)
5960
{
6061
unlock_pack();
61-
signal(SIGINT, SIG_DFL);
62+
sigchain_pop(signo);
6263
raise(signo);
6364
}
6465

@@ -672,7 +673,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
672673
ref_nr = j;
673674
}
674675

675-
signal(SIGINT, unlock_pack_on_signal);
676+
sigchain_push_common(unlock_pack_on_signal);
676677
atexit(unlock_pack);
677678
exit_code = do_fetch(transport,
678679
parse_fetch_refspec(ref_nr, refs), ref_nr);

compat/mingw.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ typedef int pid_t;
2121
#define WEXITSTATUS(x) ((x) & 0xff)
2222
#define WIFSIGNALED(x) ((unsigned)(x) > 259)
2323

24-
#define SIGKILL 0
25-
#define SIGCHLD 0
26-
#define SIGPIPE 0
27-
#define SIGHUP 0
28-
#define SIGQUIT 0
29-
#define SIGALRM 100
24+
#define SIGHUP 1
25+
#define SIGQUIT 3
26+
#define SIGKILL 9
27+
#define SIGPIPE 13
28+
#define SIGALRM 14
29+
#define SIGCHLD 17
3030

3131
#define F_GETFD 1
3232
#define F_SETFD 2

diff.c

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "run-command.h"
1313
#include "utf8.h"
1414
#include "userdiff.h"
15+
#include "sigchain.h"
1516

1617
#ifdef NO_FAST_WORKING_DIRECTORY
1718
#define FAST_WORKING_DIRECTORY 0
@@ -170,6 +171,33 @@ static struct diff_tempfile {
170171
char tmp_path[PATH_MAX];
171172
} diff_temp[2];
172173

174+
static struct diff_tempfile *claim_diff_tempfile(void) {
175+
int i;
176+
for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
177+
if (!diff_temp[i].name)
178+
return diff_temp + i;
179+
die("BUG: diff is failing to clean up its tempfiles");
180+
}
181+
182+
static int remove_tempfile_installed;
183+
184+
static void remove_tempfile(void)
185+
{
186+
int i;
187+
for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
188+
if (diff_temp[i].name == diff_temp[i].tmp_path) {
189+
unlink(diff_temp[i].name);
190+
diff_temp[i].name = NULL;
191+
}
192+
}
193+
194+
static void remove_tempfile_on_signal(int signo)
195+
{
196+
remove_tempfile();
197+
sigchain_pop(signo);
198+
raise(signo);
199+
}
200+
173201
static int count_lines(const char *data, int size)
174202
{
175203
int count, ch, completely_empty = 1, nl_just_seen = 0;
@@ -1940,10 +1968,11 @@ static void prep_temp_blob(struct diff_tempfile *temp,
19401968
sprintf(temp->mode, "%06o", mode);
19411969
}
19421970

1943-
static void prepare_temp_file(const char *name,
1944-
struct diff_tempfile *temp,
1945-
struct diff_filespec *one)
1971+
static struct diff_tempfile *prepare_temp_file(const char *name,
1972+
struct diff_filespec *one)
19461973
{
1974+
struct diff_tempfile *temp = claim_diff_tempfile();
1975+
19471976
if (!DIFF_FILE_VALID(one)) {
19481977
not_a_valid_file:
19491978
/* A '-' entry produces this for file-2, and
@@ -1952,7 +1981,13 @@ static void prepare_temp_file(const char *name,
19521981
temp->name = "/dev/null";
19531982
strcpy(temp->hex, ".");
19541983
strcpy(temp->mode, ".");
1955-
return;
1984+
return temp;
1985+
}
1986+
1987+
if (!remove_tempfile_installed) {
1988+
atexit(remove_tempfile);
1989+
sigchain_push_common(remove_tempfile_on_signal);
1990+
remove_tempfile_installed = 1;
19561991
}
19571992

19581993
if (!one->sha1_valid ||
@@ -1992,32 +2027,15 @@ static void prepare_temp_file(const char *name,
19922027
*/
19932028
sprintf(temp->mode, "%06o", one->mode);
19942029
}
1995-
return;
2030+
return temp;
19962031
}
19972032
else {
19982033
if (diff_populate_filespec(one, 0))
19992034
die("cannot read data blob for %s", one->path);
20002035
prep_temp_blob(temp, one->data, one->size,
20012036
one->sha1, one->mode);
20022037
}
2003-
}
2004-
2005-
static void remove_tempfile(void)
2006-
{
2007-
int i;
2008-
2009-
for (i = 0; i < 2; i++)
2010-
if (diff_temp[i].name == diff_temp[i].tmp_path) {
2011-
unlink(diff_temp[i].name);
2012-
diff_temp[i].name = NULL;
2013-
}
2014-
}
2015-
2016-
static void remove_tempfile_on_signal(int signo)
2017-
{
2018-
remove_tempfile();
2019-
signal(SIGINT, SIG_DFL);
2020-
raise(signo);
2038+
return temp;
20212039
}
20222040

20232041
/* An external diff command takes:
@@ -2035,34 +2053,22 @@ static void run_external_diff(const char *pgm,
20352053
int complete_rewrite)
20362054
{
20372055
const char *spawn_arg[10];
2038-
struct diff_tempfile *temp = diff_temp;
20392056
int retval;
2040-
static int atexit_asked = 0;
2041-
const char *othername;
20422057
const char **arg = &spawn_arg[0];
20432058

2044-
othername = (other? other : name);
2045-
if (one && two) {
2046-
prepare_temp_file(name, &temp[0], one);
2047-
prepare_temp_file(othername, &temp[1], two);
2048-
if (! atexit_asked &&
2049-
(temp[0].name == temp[0].tmp_path ||
2050-
temp[1].name == temp[1].tmp_path)) {
2051-
atexit_asked = 1;
2052-
atexit(remove_tempfile);
2053-
}
2054-
signal(SIGINT, remove_tempfile_on_signal);
2055-
}
2056-
20572059
if (one && two) {
2060+
struct diff_tempfile *temp_one, *temp_two;
2061+
const char *othername = (other ? other : name);
2062+
temp_one = prepare_temp_file(name, one);
2063+
temp_two = prepare_temp_file(othername, two);
20582064
*arg++ = pgm;
20592065
*arg++ = name;
2060-
*arg++ = temp[0].name;
2061-
*arg++ = temp[0].hex;
2062-
*arg++ = temp[0].mode;
2063-
*arg++ = temp[1].name;
2064-
*arg++ = temp[1].hex;
2065-
*arg++ = temp[1].mode;
2066+
*arg++ = temp_one->name;
2067+
*arg++ = temp_one->hex;
2068+
*arg++ = temp_one->mode;
2069+
*arg++ = temp_two->name;
2070+
*arg++ = temp_two->hex;
2071+
*arg++ = temp_two->mode;
20662072
if (other) {
20672073
*arg++ = other;
20682074
*arg++ = xfrm_msg;
@@ -3537,15 +3543,15 @@ void diff_unmerge(struct diff_options *options,
35373543
static char *run_textconv(const char *pgm, struct diff_filespec *spec,
35383544
size_t *outsize)
35393545
{
3540-
struct diff_tempfile temp;
3546+
struct diff_tempfile *temp;
35413547
const char *argv[3];
35423548
const char **arg = argv;
35433549
struct child_process child;
35443550
struct strbuf buf = STRBUF_INIT;
35453551

3546-
prepare_temp_file(spec->path, &temp, spec);
3552+
temp = prepare_temp_file(spec->path, spec);
35473553
*arg++ = pgm;
3548-
*arg++ = temp.name;
3554+
*arg++ = temp->name;
35493555
*arg = NULL;
35503556

35513557
memset(&child, 0, sizeof(child));
@@ -3554,13 +3560,11 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
35543560
if (start_command(&child) != 0 ||
35553561
strbuf_read(&buf, child.out, 0) < 0 ||
35563562
finish_command(&child) != 0) {
3557-
if (temp.name == temp.tmp_path)
3558-
unlink(temp.name);
3563+
remove_tempfile();
35593564
error("error running textconv command '%s'", pgm);
35603565
return NULL;
35613566
}
3562-
if (temp.name == temp.tmp_path)
3563-
unlink(temp.name);
3567+
remove_tempfile();
35643568

35653569
return strbuf_detach(&buf, outsize);
35663570
}

http-push.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "exec_cmd.h"
1111
#include "remote.h"
1212
#include "list-objects.h"
13+
#include "sigchain.h"
1314

1415
#include <expat.h>
1516

@@ -1384,7 +1385,7 @@ static void remove_locks(void)
13841385
static void remove_locks_on_signal(int signo)
13851386
{
13861387
remove_locks();
1387-
signal(signo, SIG_DFL);
1388+
sigchain_pop(signo);
13881389
raise(signo);
13891390
}
13901391

@@ -2277,10 +2278,7 @@ int main(int argc, char **argv)
22772278
goto cleanup;
22782279
}
22792280

2280-
signal(SIGINT, remove_locks_on_signal);
2281-
signal(SIGHUP, remove_locks_on_signal);
2282-
signal(SIGQUIT, remove_locks_on_signal);
2283-
signal(SIGTERM, remove_locks_on_signal);
2281+
sigchain_push_common(remove_locks_on_signal);
22842282

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

lockfile.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) 2005, Junio C Hamano
33
*/
44
#include "cache.h"
5+
#include "sigchain.h"
56

67
static struct lock_file *lock_file_list;
78
static const char *alternate_index_output;
@@ -24,7 +25,7 @@ static void remove_lock_file(void)
2425
static void remove_lock_file_on_signal(int signo)
2526
{
2627
remove_lock_file();
27-
signal(signo, SIG_DFL);
28+
sigchain_pop(signo);
2829
raise(signo);
2930
}
3031

@@ -136,11 +137,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
136137
lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
137138
if (0 <= lk->fd) {
138139
if (!lock_file_list) {
139-
signal(SIGINT, remove_lock_file_on_signal);
140-
signal(SIGHUP, remove_lock_file_on_signal);
141-
signal(SIGTERM, remove_lock_file_on_signal);
142-
signal(SIGQUIT, remove_lock_file_on_signal);
143-
signal(SIGPIPE, remove_lock_file_on_signal);
140+
sigchain_push_common(remove_lock_file_on_signal);
144141
atexit(remove_lock_file);
145142
}
146143
lk->owner = getpid();

0 commit comments

Comments
 (0)