-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.c
98 lines (91 loc) · 2.8 KB
/
control.c
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
/* cSploit - a simple penetration testing suite
* Copyright (C) 2014 Massimo Dragano aka tux_mind <tux_mind@csploit.org>
*
* cSploit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cSploit 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cSploit. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include "control.h"
#include "logger.h"
/* part of this code has been taken from:
* Gentoo Linux Documentation - POSIX threads explained
*/
/**
* @brief initialize a ::data_control
* @param mycontrol the ::data_control to initilize
* @returns 0 on success, -1 on error
*/
int control_init(data_control *mycontrol) {
if (pthread_mutex_init(&(mycontrol->mutex),NULL)) {
print( ERROR, "pthread_mutex_init: %s", strerror(errno) );
return -1;
}
if (pthread_cond_init(&(mycontrol->cond),NULL)) {
print( ERROR, "pthread_cond_init: %s", strerror(errno) );
return -1;
}
mycontrol->active=1;
return 0;
}
/**
* @brief destroy a ::data_control
* @param mycontrol the ::data_control to destroy
* @returns 0 on success, -1 on error
*/
int control_destroy(data_control *mycontrol) {
if (pthread_cond_destroy(&(mycontrol->cond))) {
print( ERROR, "pthread_cond_destroy: %s", strerror(errno) );
return -1;
}
if (pthread_mutex_destroy(&(mycontrol->mutex))) {
print( ERROR, "pthread_mutex_destroy: %s", strerror(errno) );
return -1;
}
mycontrol->active=0;
return 0;
}
/**
* @brief set ::data_control.active to 1
* @param mycontrol the ::data_control to activate
* @returns 0 on success, -1 on error
*/
int control_activate(data_control *mycontrol) {
if (pthread_mutex_lock(&(mycontrol->mutex))) {
print( ERROR, "pthread_mutex_lock: %s", strerror(errno) );
return -1;
}
mycontrol->active=1;
pthread_mutex_unlock(&(mycontrol->mutex));
pthread_cond_broadcast(&(mycontrol->cond));
return 0;
}
/**
* @brief set ::data_control.active to 0
* @param mycontrol the ::data_control to deactivate
* @returns 0 on success, -1 on error.
*/
int control_deactivate(data_control *mycontrol) {
if (pthread_mutex_lock(&(mycontrol->mutex))) {
print( ERROR, "pthread_mutex_lock: %s", strerror(errno) );
return -1;
}
mycontrol->active=0;
pthread_mutex_unlock(&(mycontrol->mutex));
pthread_cond_broadcast(&(mycontrol->cond));
return 0;
}