summaryrefslogtreecommitdiff
path: root/usual/signal.c
blob: 8d5f3b78da75f82ee7cb0263104ab2083906f1ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Signal compat.
 *
 * Copyright (c) 2009  Marko Kreen
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <usual/signal.h>

/*
 * alarm() for win32
 */

#ifdef WIN32

struct AlarmCtx {
	struct sigaction sa;
	HANDLE event;
	HANDLE thread;
	int secs;

};
static volatile struct AlarmCtx actx;

static DWORD WINAPI w32_alarm_thread(LPVOID arg)
{
	DWORD wres;
	unsigned msecs;

loop:
	if (actx.secs > 0) {
		msecs = actx.secs * 1000;
	} else {
		msecs = INFINITE;
	}

	wres = WaitForSingleObject(actx.event, msecs);
	if (wres == WAIT_OBJECT_0) {
		goto loop;
	} else if (wres == WAIT_TIMEOUT) {
		actx.secs = 0;
		if (actx.sa.sa_handler)
			actx.sa.sa_handler(SIGALRM);
		goto loop;
	} else {
		Sleep(1000);
		goto loop;
	}
	return 0;
}

unsigned int alarm(unsigned int secs)
{
	actx.secs = secs;

	/* create event */
	if (!actx.event) {
		actx.event = CreateEvent(NULL, FALSE, FALSE, NULL);
		if (!actx.event)
			return 0;
	}

	/* create or notify thread */
	if (!actx.thread) {
		actx.thread = CreateThread(NULL, 0, w32_alarm_thread, NULL, 0, NULL);
	} else {
		SetEvent(actx.event);
	}
	return 0;
}

#endif

#ifndef HAVE_SIGACTION
int sigaction(int sig, const struct sigaction *sa, struct sigaction *old)
{
#ifdef WIN32
	if (sig == SIGALRM) {
		if (old)
			*old = actx.sa;
		if (sa)
			actx.sa = *sa;
		else
			actx.sa.sa_handler = NULL;
		return 0;
	}
#endif
	old->sa_handler = signal(sig, sa->sa_handler);
	if (old->sa_handler == SIG_ERR)
		return -1;
	return 0;
}
#endif

#ifdef WIN32
/* Only sig=0 is supported, to detect if process is running (ESRCH->not) */
int kill(int pid, int sig)
{
	HANDLE hProcess;
	DWORD exitCode;
	int ret = 0;

	/* handle only sig == 0 */
	if (sig != 0) {
		errno = EINVAL;
		return -1;
	}

	hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
	if (hProcess == NULL) {
		if (GetLastError() == ERROR_INVALID_PARAMETER)
			ret = ESRCH;
		else
			ret = EPERM;
	} else {
		/* OpenProcess may succed for exited processes */
		if (GetExitCodeProcess(hProcess, &exitCode)) {
			if (exitCode != STILL_ACTIVE)
				ret = ESRCH;
		}
		CloseHandle(hProcess);
	}

	if (ret) {
		errno = ret;
		return -1;
	} else
		return  0;
}
#endif