-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathcontrol_tools.cpp
More file actions
118 lines (104 loc) · 2.59 KB
/
control_tools.cpp
File metadata and controls
118 lines (104 loc) · 2.59 KB
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
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <map>
#include <string>
#include "control_tools.h"
#include "pclsync_lib.h"
#include "overlay_client.h"
namespace control_tools{
static const int STOP = 0;
enum command_ids_ {
STARTCRYPTO = 20,
STOPCRYPTO,
FINALIZE,
LISTSYNC,
ADDSYNC,
STOPSYNC
};
int start_crypto(const char * pass){
int ret;
char* errm=NULL;
if(SendCall(STARTCRYPTO, pass, &ret, &errm))
std::cout << "Start Crypto failed. return is " << ret << " and message is "<<errm << std::endl;
else
std::cout << "Crypto started. "<< std::endl;
if(errm)
free(errm);
}
int stop_crypto(){
int ret;
char* errm=NULL;
if (SendCall(STOPCRYPTO, "", &ret, &errm))
std::cout << "Stop Crypto failed. return is " << ret << " and message is "<< errm << std::endl;
else
std::cout << "Crypto Stopped. "<< std::endl;
if (errm)
free(errm);
}
int finalize(){
int ret;
char* errm=NULL;
if (SendCall(FINALIZE, "", &ret, &errm))
std::cout << "Finalize failed. return is " << ret<< " and message is "<< errm << std::endl;
else
std::cout << "Exiting ..."<< std::endl;
if (errm)
free(errm);
}
void process_commands()
{
std::cout<< "Supported commands are:" << std::endl << "startcrypto <crypto pass>, stopcrypto, finalize, q, quit" << std::endl;
std::cout<< "> " ;
for (std::string line; std::getline(std::cin, line);) {
if (!line.compare("finalize")){
finalize();
break;
}
else if (!line.compare("stopcrypto"))
stop_crypto();
else if (!line.compare(0,11,"startcrypto",0,11) && (line.length() > 12))
start_crypto(line.c_str() + 12);
else if (!line.compare("q") || !line.compare("quit"))
break;
std::cout<< "> " ;
}
}
int daemonize(bool do_commands) {
pid_t pid, sid;
pid = fork();
if (pid<0)
exit(EXIT_FAILURE);
if (pid>0){
std::cout << "Daemon process created. Process id is: " << pid << std::endl;
if (do_commands){
process_commands();
}
else
std::cout << "sudo kill -9 "<<pid<< std::endl<<" To stop it."<< std::endl;
exit(EXIT_SUCCESS);
}
umask(0);
/* Open any logs here */
sid=setsid();
if (sid < 0)
exit(EXIT_FAILURE);
if ((chdir("/")) < 0)
exit(EXIT_FAILURE);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (console_client::clibrary::pclsync_lib::get_lib().init())
exit(EXIT_FAILURE);
while (1){
sleep(10);
}
}
}