Skip to content

Commit 7babe76

Browse files
committed
cli: introduce signal handler
Provide a mechanism to add a signal handler for Unix or Win32.
1 parent 48506f2 commit 7babe76

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

src/cli/cli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
#include "error.h"
1616
#include "opt.h"
1717
#include "opt_usage.h"
18+
#include "sighandler.h"
1819

1920
#endif /* CLI_cli_h__ */

src/cli/sighandler.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef CLI_sighandler_h__
9+
#define CLI_sighandler_h__
10+
11+
/**
12+
* Sets up a signal handler that will run when the process is interrupted
13+
* (via SIGINT on POSIX or Control-C or Control-Break on Windows).
14+
*
15+
* @param handler The function to run on interrupt
16+
* @return 0 on success, -1 on failure
17+
*/
18+
int cli_sighandler_set_interrupt(void (*handler)(void));
19+
20+
#endif /* CLI_sighandler_h__ */

src/cli/unix/sighandler.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include <stdint.h>
9+
#include <signal.h>
10+
#include "git2_util.h"
11+
#include "cli.h"
12+
13+
static void (*interrupt_handler)(void) = NULL;
14+
15+
static void interrupt_proxy(int signal)
16+
{
17+
GIT_UNUSED(signal);
18+
interrupt_handler();
19+
}
20+
21+
int cli_sighandler_set_interrupt(void (*handler)(void))
22+
{
23+
void (*result)(int);
24+
25+
if ((interrupt_handler = handler) != NULL)
26+
result = signal(SIGINT, interrupt_proxy);
27+
else
28+
result = signal(SIGINT, SIG_DFL);
29+
30+
if (result == SIG_ERR) {
31+
git_error_set(GIT_ERROR_OS, "could not set signal handler");
32+
return -1;
33+
}
34+
35+
return 0;
36+
}

src/cli/win32/sighandler.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "git2_util.h"
9+
#include <windows.h>
10+
11+
#include "cli.h"
12+
13+
static void (*interrupt_handler)(void) = NULL;
14+
15+
static BOOL WINAPI interrupt_proxy(DWORD signal)
16+
{
17+
GIT_UNUSED(signal);
18+
interrupt_handler();
19+
return TRUE;
20+
}
21+
22+
int cli_sighandler_set_interrupt(void (*handler)(void))
23+
{
24+
BOOL result;
25+
26+
if ((interrupt_handler = handler) != NULL)
27+
result = SetConsoleCtrlHandler(interrupt_proxy, FALSE);
28+
else
29+
result = SetConsoleCtrlHandler(NULL, FALSE);
30+
31+
if (!result) {
32+
git_error_set(GIT_ERROR_OS, "could not set control control handler");
33+
return -1;
34+
}
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)