Win32 alarm compat

This commit is contained in:
Martin Pulec
2019-11-19 21:26:56 +01:00
parent dcdeb276ab
commit fa7ecd4048
5 changed files with 122 additions and 0 deletions

View File

@@ -103,6 +103,7 @@ OBJS = @OBJS@ \
src/capture_filter/mirror.o \
src/capture_filter/none.o \
src/capture_filter/split.o \
src/compat/alarm.o \
src/compat/drand48.o \
src/compat/gettimeofday.o \
src/compat/platform_pipe.o \

64
src/compat/alarm.c Normal file
View File

@@ -0,0 +1,64 @@
/**
* @file compat/alarm.c
* @copydoc compat/alarm.h
*/
/*
* Copyright (c) 2019 CESNET, z. s. p. o.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#include "config_unix.h"
#include "config_win32.h"
#endif // defined HAVE_CONFIG_H
#ifdef WIN32
#include "compat/alarm.h"
static void *abort_worker(void *arg) {
unsigned int *sec = arg;
sleep(*sec);
free(sec);
abort();
}
void alarm(unsigned int sec)
{
pthread_t id;
unsigned int *val = malloc(sizeof(unsigned int));
*val = sec;
pthread_create(&id, NULL, abort_worker, val);
pthread_detach(id);
}
#endif // WIN32

50
src/compat/alarm.h Normal file
View File

@@ -0,0 +1,50 @@
/**
* @file compat/alarm.h
* @author Martin Pulec <martin.pulec@cesnet.cz>
*
* This emulates alarm on Windows without custom signal handler (i. e.
* using default SIGALRM action which is terminate).
*/
/*
* Copyright (c) 2019 CESNET, z. s. p. o.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef WIN32
#ifndef COMPAT_ALARM_H
#define COMPAT_ALARM_H
EXTERN_C void alarm(unsigned int sec);
#endif // defined COMPAT_ALARM_H
#endif /* WIN32 */

View File

@@ -114,6 +114,7 @@ typedef unsigned int in_addr_t;
#define NEED_INET_ATON
#include <time.h> /* For clock_t */
#include "compat/alarm.h"
#include "compat/usleep.h"
#include "crypto/random.h"

View File

@@ -35,6 +35,12 @@
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#include "config_unix.h"
#include "config_win32.h"
#endif // defined HAVE_CONFIG_H
#include "utils/packet_counter.h"
#include <assert.h>
#include <stdio.h>