1/*
2 * Copyright (c) 2007 Hyperic, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "sigar.h"
18#include "sigar_private.h"
19#include "sigar_util.h"
20
21#ifdef WIN32
22#include <windows.h>
23#endif
24
25#include <signal.h>
26#include <errno.h>
27
28SIGAR_DECLARE(int) sigar_proc_kill(sigar_pid_t pid, int signum)
29{
30#ifdef WIN32
31    int status = -1;
32    HANDLE proc =
33        OpenProcess(PROCESS_ALL_ACCESS,
34                    TRUE, (DWORD)pid);
35
36    if (proc) {
37        switch (signum) {
38          case 0:
39            status = SIGAR_OK;
40            break;
41          default:
42            if (TerminateProcess(proc, signum)) {
43                status = SIGAR_OK;
44            }
45            break;
46        }
47
48        CloseHandle(proc);
49
50        if (status == SIGAR_OK) {
51            return SIGAR_OK;
52        }
53    }
54    return GetLastError();
55#else
56    if (kill(pid, signum) == -1) {
57        return errno;
58    }
59    return SIGAR_OK;
60#endif
61}
62
63SIGAR_DECLARE(int) sigar_signum_get(char *name)
64{
65    if (strnEQ(name, "SIG", 3)) {
66        name += 3;
67    }
68
69    switch (*name) {
70      case 'A':
71#ifdef SIGABRT
72        if (strEQ(name, "ABRT")) return SIGABRT;
73#endif
74#ifdef SIGALRM
75        if (strEQ(name, "ALRM")) return SIGALRM;
76#endif
77        break;
78      case 'B':
79#ifdef SIGBUS
80        if (strEQ(name, "BUS")) return SIGBUS;
81#endif
82        break;
83      case 'C':
84#ifdef SIGCONT
85        if (strEQ(name, "CONT")) return SIGCONT;
86#endif
87#ifdef SIGCHLD
88        if (strEQ(name, "CHLD")) return SIGCHLD;
89#endif
90#ifdef SIGCLD
91        if (strEQ(name, "CLD")) return SIGCLD;
92#endif
93        break;
94      case 'E':
95#ifdef SIGEMT
96        if (strEQ(name, "EMT")) return SIGEMT;
97#endif
98        break;
99      case 'F':
100#ifdef SIGFPE
101        if (strEQ(name, "FPE")) return SIGFPE;
102#endif
103        break;
104      case 'H':
105#ifdef SIGHUP
106        if (strEQ(name, "HUP")) return SIGHUP;
107#endif
108        break;
109      case 'I':
110#ifdef SIGINT
111        if (strEQ(name, "INT")) return SIGINT;
112#endif
113#ifdef SIGILL
114        if (strEQ(name, "ILL")) return SIGILL;
115#endif
116#ifdef SIGIOT
117        if (strEQ(name, "IOT")) return SIGIOT;
118#endif
119#ifdef SIGIO
120        if (strEQ(name, "IO")) return SIGIO;
121#endif
122#ifdef SIGINFO
123        if (strEQ(name, "INFO")) return SIGINFO;
124#endif
125        break;
126      case 'K':
127#ifdef SIGKILL
128        if (strEQ(name, "KILL")) return SIGKILL;
129#endif
130        break;
131      case 'P':
132#ifdef SIGPOLL
133        if (strEQ(name, "POLL")) return SIGPOLL;
134#endif
135#ifdef SIGPIPE
136        if (strEQ(name, "PIPE")) return SIGPIPE;
137#endif
138#ifdef SIGPROF
139        if (strEQ(name, "PROF")) return SIGPROF;
140#endif
141#ifdef SIGPWR
142        if (strEQ(name, "PWR")) return SIGPWR;
143#endif
144        break;
145      case 'Q':
146#ifdef SIGQUIT
147        if (strEQ(name, "QUIT")) return SIGQUIT;
148#endif
149        break;
150      case 'S':
151#ifdef SIGSEGV
152        if (strEQ(name, "SEGV")) return SIGSEGV;
153#endif
154#ifdef SIGSYS
155        if (strEQ(name, "SYS")) return SIGSYS;
156#endif
157#ifdef SIGSTOP
158        if (strEQ(name, "STOP")) return SIGSTOP;
159#endif
160#ifdef SIGSTKFLT
161        if (strEQ(name, "STKFLT")) return SIGSTKFLT;
162#endif
163        break;
164      case 'T':
165#ifdef SIGTRAP
166        if (strEQ(name, "TRAP")) return SIGTRAP;
167#endif
168#ifdef SIGTERM
169        if (strEQ(name, "TERM")) return SIGTERM;
170#endif
171#ifdef SIGTSTP
172        if (strEQ(name, "TSTP")) return SIGTSTP;
173#endif
174#ifdef SIGTTIN
175        if (strEQ(name, "TTIN")) return SIGTTIN;
176#endif
177#ifdef SIGTTOU
178        if (strEQ(name, "TTOU")) return SIGTTOU;
179#endif
180        break;
181      case 'U':
182#ifdef SIGURG
183        if (strEQ(name, "URG")) return SIGURG;
184#endif
185#ifdef SIGUSR1
186        if (strEQ(name, "USR1")) return SIGUSR1;
187#endif
188#ifdef SIGUSR2
189        if (strEQ(name, "USR2")) return SIGUSR2;
190#endif
191        break;
192      case 'V':
193#ifdef SIGVTALRM
194        if (strEQ(name, "VTALRM")) return SIGVTALRM;
195#endif
196        break;
197      case 'W':
198#ifdef SIGWINCH
199        if (strEQ(name, "WINCH")) return SIGWINCH;
200#endif
201        break;
202      case 'X':
203#ifdef SIGXCPU
204        if (strEQ(name, "XCPU")) return SIGXCPU;
205#endif
206#ifdef SIGXFSZ
207        if (strEQ(name, "XFSZ")) return SIGXFSZ;
208#endif
209        break;
210      default:
211        break;
212    }
213
214    return -1;
215}
216
217