-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExecServer.cpp
730 lines (645 loc) · 19.7 KB
/
ExecServer.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#include "xmlrpcpp/XmlRpc.h"
#include "sha1.h"
#include "util.h"
#include "version.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#if defined(_WINDOWS)
#include <direct.h>
#include <io.h>
#define F_OK 0
#define W_OK 2
#define R_OK 4
#define mkdir(__path,__mode) _mkdir(__path)
#pragma warning (disable:4996)
#define DIR_SEPARATOR '\\'
#else
#define DIR_SEPARATOR '/'
#endif
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
using namespace XmlRpc;
using namespace std;
void throw_on_os_error(const char* desc /*=NULL*/) {
const char* errtext;
int e=get_os_error(&errtext);
if(e) {
stringstream ss;
if(desc)
ss << desc << ": ";
ss << "Error " << e;
if(errtext)
ss << ": " << errtext;
throw XmlRpcException(ss.str(), e);
}
}
/* converts string to char*, or NULL if string is empty */
const char* str(const string& s) {
if(s.empty())
return NULL;
return s.c_str();
}
/* converts vector<string> to NULL-terminated char** */
class strlist {
char** data;
public:
strlist(const vector<string>& vs): data(NULL) {
size_t vsize=vs.size();
if(!vsize)
return;
data=(char**)malloc((vsize+1)*sizeof(char*));
for(size_t i=0; i<vsize; ++i) {
data[i]=strdup(vs[i].c_str());
}
data[vsize]=NULL;
}
~strlist() {
if(data) {
for(char** pp=data; *pp; ++pp)
free(*pp);
free(data);
}
}
operator char**() {
return data;
}
};
class Path {
char* data;
static size_t path_max() {
#if defined PATH_MAX
return PATH_MAX;
#elif defined _MAX_PATH
return _MAX_PATH;
#elif defined _PC_PATH_MAX
return pathconf("/", _PC_PATH_MAX);
#else
#error "cannot find PATH_MAX"
#endif
}
public:
Path(const char* value=NULL): data(new char[path_max()]) {
if(value) strncpy(data, value, size());
}
~Path() { delete[] data; }
size_t size() const { return path_max(); }
operator char*() { return data; }
char* get() { return data; }
const char* get() const { return data; }
};
class FileHolder {
FILE* f;
public:
FileHolder(FILE* f_): f(f_) {}
~FileHolder() {
if(f)
fclose(f);
}
operator FILE*() {
return f;
}
};
class M_dir_tmpname: public XmlRpcServerMethod {
public:
M_dir_tmpname(XmlRpcServer* server = 0):
XmlRpcServerMethod("dir.tmpname", server) {}
std::string help() {
return "dir.tmpname(): return a unique temporary file name in the current directory";
}
void execute(XmlRpcValue& /*params*/, XmlRpcValue& result) {
int attempts=256;
Path cwd;
clear_error();
getcwd(cwd, cwd.size());
throw_on_os_error("getcwd");
if(access(cwd, W_OK)<0) {
throw XmlRpcException("Current directory is not writeable");
}
Path fname;
for(int i=(rand() << 8) ^ (int)(time(NULL)); attempts; ++i,--attempts) {
snprintf(fname, fname.size(), "%s%cEX%06x", cwd.get(), DIR_SEPARATOR, i&0xFFFFFF);
clear_error();
if(access(fname, F_OK)<0) {
if(errno == ENOENT) {
/* great! this file doesn't exist */
clear_error();
result=fname;
return;
}
}
}
throw XmlRpcException("Cannot create temporary file name");
}
};
class M_dir_chdir: public XmlRpcServerMethod {
public:
M_dir_chdir(XmlRpcServer* server = 0):
XmlRpcServerMethod("dir.chdir", server) {}
std::string help() {
return "dir.chdir([dir]): change current directory\n"
" If <dir> is empty, go to the start directory\n"
"Return value: current directory";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
Path buf;
string dir;
try {
if(params.getType()!=XmlRpcValue::TypeInvalid)
dir=string(params[0]);
} catch(...) {
throw XmlRpcException("parameters error");
}
if(dir.empty()) dir=cfg()->start_dir;
clear_error();
chdir(dir.c_str());
throw_on_os_error("chdir");
getcwd(buf, buf.size());
throw_on_os_error("getcwd");
result=string(buf);
}
};
class M_dir_mkdir: public XmlRpcServerMethod {
public:
M_dir_mkdir(XmlRpcServer* server = 0):
XmlRpcServerMethod("dir.mkdir", server) {}
std::string help() {
return "dir.mkdir(dir): create directory";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
string dir;
try {
if(params.getType()!=XmlRpcValue::TypeInvalid)
dir=string(params[0]);
} catch(...) {
throw XmlRpcException("parameters error");
}
if(dir.empty()) throw XmlRpcException("directory name is empty");
clear_error();
mkdir(dir.c_str(),0777);
throw_on_os_error("mkdir");
(void)result;
}
};
class M_dir_rmdir: public XmlRpcServerMethod {
public:
M_dir_rmdir(XmlRpcServer* server = 0):
XmlRpcServerMethod("dir.rmdir", server) {}
std::string help() {
return "dir.rmdir(dir, [recursive=False]): remove directory\n"
"\tif recursive==True, delete non-empty directory and all its contents";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
string dir;
bool recursive=false;
try {
if(params.getType()!=XmlRpcValue::TypeInvalid)
dir=string(params[0]);
if(params.size()>1) {
recursive=bool(params[1]);
}
} catch(...) {
throw XmlRpcException("parameters error");
}
if(dir.empty()) throw XmlRpcException("directory name is empty");
clear_error();
if(recursive)
rmdir_recursive(dir.c_str());
else
rmdir(dir.c_str());
throw_on_os_error("rmdir");
(void)result;
}
};
class M_file_put: public XmlRpcServerMethod {
public:
M_file_put(XmlRpcServer* server = 0):
XmlRpcServerMethod("file.put", server) {}
std::string help() {
return "file.put(filename, data, [append=False]): write to file <filename> the string <data> (or a base64 encoded <data>)\n"
"\tIf append==True, append to the end of file\n"
"Return value: number of bytes written";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
string fname, data;
bool binary=false;
bool append=false;
try {
fname=string(params[0]);
switch(params[1].getType()) {
case XmlRpcValue::TypeString:
data=string(params[1]);
binary=false;
break;
case XmlRpcValue::TypeBase64: {
XmlRpcValue::BinaryData& b(params[1]);
data.assign(b.begin(), b.end());
binary=true;
}
break;
default:
throw XmlRpcException("parameters error");
}
if(params.size()>2) append=bool(params[2]);
} catch(...) {
throw XmlRpcException("parameters error");
}
clear_error();
FileHolder fo=fopen(fname.c_str(),
binary?
(append? "ab":"wb"):
(append? "a":"w"));
if(!fo)
throw_on_os_error("fopen");
clear_error();
size_t nw=fwrite(data.c_str(), 1, data.size(), fo);
throw_on_os_error("fwrite");
result=(int)nw;
}
};
class M_file_get: public XmlRpcServerMethod {
public:
M_file_get(XmlRpcServer* server = 0):
XmlRpcServerMethod("file.get", server) {}
std::string help() {
return "file.get(filename, [binary=False], [pos=0], [maxbytes=ALL]): receive a file from the host filesystem\n"
"Arguments:\n"
" filename: name of file to receive\n"
" binary: boolean value, if set to TRUE, the contents will be sent as Base64, otherwise as String\n"
" pos: initial position in file\n"
" maxbytes: maximum number of bytes to send, file will be truncated\n"
"Return value:\n"
" the contents of the file (string or base64 on demand)\n";
}
enum { BUFSZ = 1024*64 };
void execute(XmlRpcValue& params, XmlRpcValue& result) {
string fname;
bool binary=false;
int pos=0, maxbytes=-1;
try {
fname=string(params[0]);
if(params.size()>1) {
binary=bool(params[1]);
}
if(params.size()>2) {
pos=int(params[2]);
}
if(params.size()>3) {
maxbytes=int(params[3]);
}
} catch(...) {
throw XmlRpcException("parameters error");
}
clear_error();
FileHolder fi=fopen(fname.c_str(), binary? "rb":"r");
if(!fi)
throw_on_os_error("fopen");
if(pos) {
fseek(fi, pos, SEEK_SET);
throw_on_os_error("fseek");
}
string res;
char buf[BUFSZ];
while(maxbytes) {
size_t nr=maxbytes<0? BUFSZ: maxbytes;
if(nr>BUFSZ)
nr=BUFSZ;
if(!nr)
break;
nr=fread(buf,1,nr,fi);
if(!nr) {
throw_on_os_error("fread");
break;
}
res.append(buf, buf+nr);
if(maxbytes>=0)
maxbytes-=nr;
}
if(binary) {
result=XmlRpcValue((void*)res.c_str(), res.size());
} else {
result=res;
}
}
};
class M_file_sha1: public XmlRpcServerMethod {
public:
M_file_sha1(XmlRpcServer* server = 0):
XmlRpcServerMethod("file.sha1", server) {}
std::string help() {
return "file.sha1(filename): return SHA1 hexdigest of a file";
}
enum { BUFSZ = 1024*64 };
void execute(XmlRpcValue& params, XmlRpcValue& result) {
string fname;
try {
fname=string(params[0]);
} catch(...) {
throw XmlRpcException("parameters error");
}
clear_error();
FileHolder fi=fopen(fname.c_str(), "rb");
if(!fi)
throw_on_os_error("fopen");
SHA1 sha1;
char buf[BUFSZ];
while(1) {
size_t nr=BUFSZ;
if(nr>BUFSZ)
nr=BUFSZ;
if(!nr)
break;
nr=fread(buf,1,nr,fi);
if(!nr) {
throw_on_os_error("fread");
break;
}
sha1.Input(buf, nr);
}
unsigned rd[5];
if(!sha1.Result(rd)) throw XmlRpcException("SHA message corrupted");
stringstream ss;
for(int i=0; i<5; ++i) ss << hex << setw(8) << setfill('0') << rd[i];
result=ss.str();
}
};
class M_file_remove: public XmlRpcServerMethod {
public:
M_file_remove(XmlRpcServer* server = 0):
XmlRpcServerMethod("file.remove", server) {}
std::string help() {
return "file.remove(filename): remove file from the host filesystem\n";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
string fname;
try {
fname=string(params[0]);
} catch(...) {
throw XmlRpcException("parameters error");
}
clear_error();
unlink(fname.c_str());
throw_on_os_error("unlink");
(void)result;
}
};
class M_process_spawn: public XmlRpcServerMethod {
public:
M_process_spawn(XmlRpcServer * server = 0):
XmlRpcServerMethod("process.spawn", server) {}
std::string help() {
return "process.spawn(args, timeout, options): spawn a subprocess\n"
"Arguments:\n"
" args: list of parameters (first is the program name)\n"
" timeout: if zero, the process is started asynchronously, otherwise, it's a maximum execution time\n"
" options: an optional struct with the following keys:\n"
" cwd: the process will chdir there before execution\n"
" stdin: name of a file to be fed to the subprocess's stdin\n"
" stdout: name of a file where the suprocess's stdout will be written\n"
" stderr: name of a file where the suprocess's stderr will be written\n"
" env: dict of environment variables to set\n"
"Return value:\n"
" for asynchronous requests:\n"
" pid (integer)\n"
" for synchronous requests:\n"
" return value (integer)\n"
" on timeout:\n"
" kill process and raise Fault";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
vector<string> args, envs;
string argstr;
string cwd, fin, fout, ferr;
int timeout=0;
try {
XmlRpcValue& vargs=params[0];
switch(vargs.getType()) {
case XmlRpcValue::TypeArray:
for(int i=0; i<vargs.size(); ++i) {
args.push_back(string(vargs[i]));
}
break;
case XmlRpcValue::TypeString:
args.push_back(string(vargs));
break;
default:
throw XmlRpcException("parameters error");
}
if(params.size()>1) {
timeout=int(params[1]);
}
if(params.size()>2) {
XmlRpcValue& vopts=params[2];
if(vopts.hasMember("cwd"))
cwd=string(vopts["cwd"]);
if(vopts.hasMember("stdin"))
fin=string(vopts["stdin"]);
if(vopts.hasMember("stdout"))
fout=string(vopts["stdout"]);
if(vopts.hasMember("stderr"))
ferr=string(vopts["stderr"]);
if(vopts.hasMember("env")) {
XmlRpcValue& venv=vopts["env"];
vector<string> keys=venv.keys();
for(vector<string>::const_iterator p=keys.begin();
p!=keys.end(); ++p) {
envs.push_back(*p+string("=")+string(venv[*p]));
}
}
}
} catch(...) {
throw XmlRpcException("parameters error");
}
clear_error();
int pid=pspawn(strlist(args), strlist(envs), str(cwd),
str(fin), str(fout), str(ferr));
if(pid<0)
throw_on_os_error("exec");
if(timeout==0) {
result=pid;
} else {
int res=pwait(pid, timeout);
if(res<0) {
pkill(pid);
throw_on_os_error("kill");
throw XmlRpcException("Process killed on timeout");
}
result=res;
}
}
};
class M_process_wait: public XmlRpcServerMethod {
public:
M_process_wait(XmlRpcServer* server = 0):
XmlRpcServerMethod("process.wait", server) {}
std::string help() {
return "process.wait(pid, timeout): wait for completion of <pid> or for <timeout> seconds";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
int pid, timeout=0;
try {
pid=params[0];
timeout=params[1];
} catch(...) {
throw XmlRpcException("parameters error");
}
clear_error();
int res=pwait(pid, timeout);
throw_on_os_error("wait");
result=res;
}
};
class M_process_kill: public XmlRpcServerMethod {
public:
M_process_kill(XmlRpcServer* server = 0):
XmlRpcServerMethod("process.kill", server) {}
std::string help() {
return "process.kill(pid): kill the process <pid>";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
int pid;
try {
pid=params[0];
} catch(...) {
throw XmlRpcException("parameters error");
}
clear_error();
int res=pkill(pid);
throw_on_os_error("kill");
result=res;
}
};
class M_system_version: public XmlRpcServerMethod {
public:
M_system_version(XmlRpcServer* server = 0): XmlRpcServerMethod("system.version", server) {}
std::string help() {
return "system.version(): return ExecServer version";
}
void execute(XmlRpcValue& /*params*/, XmlRpcValue& result) {
result = _VERSION_;
}
};
class M_system_uname: public XmlRpcServerMethod {
public:
M_system_uname(XmlRpcServer* server = 0): XmlRpcServerMethod("system.uname", server) {}
std::string help() {
return "system.uname(): return machine info";
}
void execute(XmlRpcValue& /*params*/, XmlRpcValue& result) {
struct utsname un;
uname(&un);
result["sysname"]=un.sysname;
result["nodename"]=un.nodename;
result["release"]=un.release;
result["version"]=un.version;
result["machine"]=un.machine;
}
};
class M_system_getenv: public XmlRpcServerMethod {
public:
M_system_getenv(XmlRpcServer* server = 0): XmlRpcServerMethod("system.getenv", server) {}
std::string help() {
return "system.getenv(): return environment variable";
}
void execute(XmlRpcValue& params, XmlRpcValue& result) {
std::string var;
try {
var=string(params[0]);
} catch(...) {
throw XmlRpcException("parameters error");
}
char* res=getenv(var.c_str());
result=res ? string(res): string("");
}
};
class ExecServer: public XmlRpcServer {
int port_;
volatile bool stop_flag_;
public:
ExecServer(int port): XmlRpcServer(), port_(port), stop_flag_(false) {
addMethod(new M_dir_tmpname(this));
addMethod(new M_dir_chdir(this));
addMethod(new M_dir_mkdir(this));
addMethod(new M_dir_rmdir(this));
addMethod(new M_file_put(this));
addMethod(new M_file_get(this));
addMethod(new M_file_sha1(this));
addMethod(new M_file_remove(this));
addMethod(new M_process_spawn(this));
addMethod(new M_process_wait(this));
addMethod(new M_process_kill(this));
addMethod(new M_system_getenv(this));
addMethod(new M_system_version(this));
addMethod(new M_system_uname(this));
}
bool start() {
if(!bindAndListen(port_)) return false;
enableIntrospection();
while(!stop_flag_) work(0.5);
shutdown();
return true;
}
void stop() {
stop_flag_=true;
}
};
ExecServer* srv=NULL;
int main0(void) {
if(!cfg()) {
// cannot load configuration
return 2;
}
srand((unsigned)time(NULL));
chdir(cfg()->start_dir);
srv=new ExecServer(cfg()->listen_port);
srv->start();
delete srv;
srv=NULL;
return 0;
}
void stop_all() {
if(srv) {
srv->stop();
}
}
#ifdef _WINDOWS
#include "ServiceModule.h"
static BOOL WINAPI ctrl_handler(DWORD /*dwCtrlType*/) {
stop_all();
return TRUE;
}
int main(int argc, char** argv) {
if(argc>1) {
if(strcmp(argv[1],"--install") == 0) {
return CServiceModule::Install();
} else if(strcmp(argv[1],"--uninstall") == 0) {
return CServiceModule::Uninstall();
} else if(strcmp(argv[1],"--start") == 0) {
return CServiceModule::Start();
} else if(strcmp(argv[1],"--no-detach") == 0) {
SetConsoleCtrlHandler(ctrl_handler, TRUE);
XmlRpc::setVerbosity(2);
return main0();
}
}
return CServiceModule::Start();
}
#else // !_WINDOWS
int main(int argc, char** argv) {
bool detach=true;
if(argc>1) {
if(strcmp(argv[1],"--no-detach") == 0) {
XmlRpc::setVerbosity(2);
detach=false;
}
}
if(detach) daemon(0,0);
return main0();
}
#endif