summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/vmSignal/vmsignal.c
blob: 2d2c947969111f0265534b80b6377920e2956b09 (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
/*********************************************************
 * Copyright (C) 1998-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/


/*
 * vmsignal.c --
 *
 *    Posix signal handling utility functions
 *
 */

#ifndef VMX86_DEVEL

#endif


#include <stdio.h>
#include <errno.h>
#include <string.h>


#include "vmsignal.h"


/*
 * Signal_SetGroupHandler --
 *
 *    Set a signal handler for a group of signals.
 *    We carefully ensure that if the handler is only used to handle the
 *    signals of the group, the handling of all the signals of the group is
 *    serialized, which means that the handler is not re-entrant.
 *
 * Return value:
 *    1 on success
 *    0 on failure (detail is displayed)
 *
 * Side effects:
 *    None
 *
 */

int
Signal_SetGroupHandler(int const *signals,          // IN
                       struct sigaction *olds,      // OUT
                       unsigned int nr,             // IN
                       void (*handler)(int signal)) // IN
{
   unsigned int i;
   struct sigaction new;

   new.sa_handler = handler;
   if (sigemptyset(&new.sa_mask)) {
      fprintf(stderr, "Unable to empty a signal set: %s.\n\n", strerror(errno));

      return 0;
   }
   for (i = 0; i < nr; i++) {
      if (sigaddset(&new.sa_mask, signals[i])) {
         fprintf(stderr, "Unable to add a signal to a signal set: %s.\n\n", strerror(errno));

         return 0;
      }
   }
   new.sa_flags = 0;

   for (i = 0; i < nr; i++) {
      if (sigaction(signals[i], &new, &olds[i])) {
         fprintf(stderr, "Unable to modify the handler of the signal %d: %s.\n\n", signals[i], strerror(errno));

         return 0;
      }
   }

   return 1;
}


/*
 * Signal_ResetGroupHandler --
 *
 *    Reset the handler of each signal of a group of signals
 *
 * Return value:
 *    1 on success
 *    0 on failure (detail is displayed)
 *
 * Side effects:
 *    None
 *
 */

int
Signal_ResetGroupHandler(int const *signals,           // IN
                         struct sigaction const *olds, // IN
                         unsigned int nr)              // IN
{
   unsigned int i;

   for (i = 0; i < nr; i++) {
      if (sigaction(signals[i], &olds[i], NULL)) {
         fprintf(stderr, "Unable to reset the handler of the signal %d: %s.\n\n", signals[i], strerror(errno));

         return 0;
      }
   }

   return 1;
}