-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.c
116 lines (105 loc) · 2.43 KB
/
controller.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* libinstrument-functions
* Copyright © 2018 yiifburj
*
* This program 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.
*
* 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h> /* For O_* constants */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "libinstrument_functions.h"
#include <errno.h>
#include <getopt.h>
/* TODO:
* support thread time upgrade network */
int main(int argc, char *argv[])
{
int turn_on = -1;
#define input_error() do{fprintf(stderr, "input error\n");exit(1);}while(0)
while(1){
static struct option long_options[]=
{
{"on", no_argument, 0, 1},
{"off", no_argument, 0, 0},
};
int c = getopt_long(argc, argv,"", long_options, NULL);
if(c == -1)
break;
switch (c) {
case 1:
if(turn_on != -1)
input_error();
turn_on = 1;
break;
case 0:
if(turn_on != -1)
input_error();
turn_on = 0;
break;
default:
input_error();
}
}
int idx;
if((idx=get_process_idx(argv[optind])) < 0)
{
fprintf(stderr, "not controller\n");
exit(1);
}
int shm_size;
char *controller_map = get_map_addr_size(PROT_READ|PROT_WRITE, &shm_size);
int old = controller_map[idx];
if(turn_on == 1){
switch(old)
{
case TO_OFF:
fprintf(stderr, "in closing\n");
exit(1);
case ON:
break;
case OFF:
controller_map[idx] = ON;
break;
// if(!__sync_bool_compare_and_swap(controller_map+idx,OFF, ON))
// {
// fprintf(stderr, "conflict, try again\n");
// exit(1);
// }
default:
fprintf(stderr, "memory is corrupted!\n");
exit(1);
}
}
else if(turn_on == 0)
{
switch(old)
{
case TO_OFF:
case OFF:
break;
case ON:
controller_map[idx] = TO_OFF;
break;
default:
fprintf(stderr, "memory is corrupted!\n");
exit(1);
}
}
munmap(controller_map, shm_size);
return 0;
}