Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / inet / tests / TestSetupSignallingPosix.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2013-2018 Nest Labs, Inc.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *    @file
21  *      This file sets up signalling for all POSIX CHIP Inet layer library test
22  *      applications and tools.
23  *
24  *      NOTE: These do not comprise a public part of the CHIP API and
25  *            are subject to change without notice.
26  *
27  */
28
29 #include "TestSetupSignalling.h"
30
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 static void ExitOnSIGUSR1Handler(int signum)
37 {
38     // exit() allows us a slightly better clean up (gcov data) than SIGINT's exit
39     exit(0);
40 }
41
42 // We set a hook to exit when we receive SIGUSR1, SIGTERM or SIGHUP
43 void SetSIGUSR1Handler()
44 {
45     SetSignalHandler(ExitOnSIGUSR1Handler);
46 }
47
48 void SetSignalHandler(SignalHandler handler)
49 {
50     struct sigaction sa;
51     int signals[] = { SIGUSR1 };
52     size_t i;
53
54     memset(&sa, 0, sizeof(sa));
55     sa.sa_handler = handler;
56
57     for (i = 0; i < sizeof(signals) / sizeof(signals[0]); i++)
58     {
59         if (sigaction(signals[i], &sa, nullptr) == -1)
60         {
61             perror("Can't catch signal");
62             exit(1);
63         }
64     }
65 }