-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathacq400_knobs.cpp
More file actions
1027 lines (889 loc) · 22.6 KB
/
acq400_knobs.cpp
File metadata and controls
1027 lines (889 loc) · 22.6 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
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ------------------------------------------------------------------------- *
* acq400_knobs.cpp
* ------------------------------------------------------------------------- *
* Copyright (C) 2013 Peter Milne, D-TACQ Solutions Ltd
* <peter dot milne at D hyphen TACQ dot com>
* www.d-tacq.com
* Created on: 30 Dec 2013
* Author: pgm
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of Version 2 of the GNU General Public License *
* as published by the Free Software Foundation; *
* *
* 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, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* ------------------------------------------------------------------------- */
/*
What does acq400_knobs do?.
- r : readable
- w : writable
- x : executes it
- wildcard query
- help
- help2
- ranges wbn too.
- load the directory at start. precompute all status.
- .ranges file?.
*/
/* fixes error stat: Value too large for defined data type */
#define _FILE_OFFSET_BITS 64
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm> // std::find
#include <vector>
#include <string>
#include <sstream>
#include <glob.h>
#include "popt.h"
#include "tcp_server.h"
#include "Env.h"
const char* pattern = "";
using namespace std;
#define VERID "acq400_knobs B1006"
bool err;
char* host = 0; /* server allows connect from host (any) */
char* port = 0;
int site;
int verbose = 0;
#define MAXOUTBUF 16384
#define VPRINTF if (verbose) printf
class Knob;
vector<Knob*> KNOBS;
typedef vector<Knob*>::iterator VKI;
char* chomp(char *str) {
char* cursor = str + strlen(str)-1;
while (std::isspace(*cursor) && cursor >= str){
*cursor-- = '\0';
}
return str;
}
inline std::vector<std::string> glob(const std::string& pat){
using namespace std;
glob_t glob_result;
int rc = glob(pat.c_str(), 0, NULL, &glob_result);
if (rc != 0){
printf("ERROR: cwd %s glob %s returns %d\n",
get_current_dir_name(), pat.c_str(), rc);
}
vector<string> ret;
for(unsigned int i=0; i < glob_result.gl_pathc; ++i){
ret.push_back(string(glob_result.gl_pathv[i]));
}
globfree(&glob_result);
return ret;
}
class File {
public:
FILE *fp;
File(const char* name, const char* mode = "r") {
fp = fopen(name, mode);
}
virtual ~File() {
if (fp) fclose(fp);
}
};
class Pipe {
public:
FILE* fp;
Pipe(const char* name, const char* mode = "r") {
fp = popen(name, mode);
}
int close() {
int rc = 0;
if (fp) {
rc = pclose(fp);
fp = 0;
}
return rc;
}
virtual ~Pipe() {
if (close() == -1){
;
//perror("pclose");
}
}
};
class Validator {
public:
virtual ~Validator() {}
virtual bool isValid(char* buf, int maxbuf, const char* args) = 0;
};
class NullValidator : public Validator
/* singleton */
{
NullValidator() {}
public:
virtual bool isValid(char* buf, int maxbuf, const char* args) {
return true;
}
static Validator* instance() {
static Validator* _instance;
if (!_instance){
return _instance = new NullValidator;
}else{
return _instance;
}
}
};
class NumericValidator : public Validator {
int rmin, rmax;
NumericValidator(int _rmin, int _rmax) : rmin(_rmin), rmax(_rmax) {
}
public:
virtual bool isValid(char* buf, int maxbuf, const char* args) {
int tv;
if (sscanf(args, "%d", &tv) != 1 ){
snprintf(buf, maxbuf,
"ERROR: NumericValidator %s not numeric", args);
return false;
}else{
bool ok = tv >= rmin && tv <= rmax;
if (!ok){
snprintf(buf, maxbuf,
"ERROR: NumericValidator %d not in range %d,%d",
tv, rmin, rmax);
}
return ok;
}
}
static Validator* create(const char* def){
const char* ndef;
if ((ndef = strstr(def, "numeric=")) != 0){
int _rmin, _rmax;
if (sscanf(ndef, "numeric=%d,%d", &_rmin, &_rmax) == 2){
return new NumericValidator(_rmin, _rmax);
}
}
return 0;
}
};
class Knob {
protected:
Knob(const char* _name) {
name = new char[strlen(_name)+1];
strcpy(name, _name);
validator = NullValidator::instance();
}
char* name;
Validator *validator;
void cprint(const char* ktype) {
printf("%8s %s\n", ktype, name);
}
bool isValid(char* buf, int maxbuf, const char* args){
return validator->isValid(buf, maxbuf, args);
}
virtual int _set(char* buf, int maxbuf, const char* args) = 0;
public:
virtual ~Knob() {
delete [] name;
}
vector<Knob*> peers;
char* getName() { return name; }
virtual const char* getAttr() {
return "";
}
/* return >0 on success, <0 on fail */
virtual int set(char* buf, int maxbuf, const char* args) {
vector<Knob*>::iterator it;
for (it = peers.begin(); it != peers.end(); ++it){
(*it)->_set(buf, maxbuf, args);
}
return _set(buf, maxbuf, args);
}
virtual int get(char* buf, int maxbuf) = 0;
virtual void print(void) { cprint("Knob"); }
static Knob* create(const string _name, mode_t mode);
static int match(const char* name, const char* key) {
if (strcmp(name, key) == 0){
return 1;
}else if (fnmatch(key, name, 0) == 0){
return -1;
}else{
return 0;
}
}
};
class KnobRO : public Knob {
protected:
virtual int _set(char* buf, int maxbuf, const char* args) {
return -snprintf(buf, maxbuf, "ERROR: \"%s\" is read-only", name);
}
public:
KnobRO(const char* _name) : Knob(_name) {}
virtual int get(char* buf, int maxbuf) {
File knob(name, "r");
if (knob.fp == NULL) {
return snprintf(buf, maxbuf,
"ERROR: %d \"%s\" failed to open \"%s\"\n",
errno, strerror(errno), name);
}else{
return fgets(buf, maxbuf, knob.fp) != NULL;
}
}
virtual void print(void) { cprint("KnobRO"); }
virtual const char* getAttr() {
return "r";
}
};
class KnobRW : public KnobRO {
protected:
virtual int _set(char* buf, int maxbuf, const char* args) {
if (!isValid(buf, maxbuf, args)){
return -1;
}
File knob(name, "w");
if (knob.fp == NULL){
return -snprintf(buf, maxbuf, "ERROR: failed to open \"%s\"\n", name);
}else if (fputs(args, knob.fp) < 0){
return -snprintf(buf, maxbuf, "ERROR:");
}else{
return snprintf(buf, maxbuf, "\n");
}
}
public:
KnobRW(const char* _name) : KnobRO(_name) {
}
virtual void print(void) { cprint ("KnobRW"); }
virtual const char* getAttr() {
return "rw";
}
};
class KnobX : public Knob {
int runcmd(const char* cmd, char* buf, int maxbuf);
char attr[4];
const int site;
protected:
virtual int _set(char* buf, int maxbuf, const char* args) {
if (!isValid(buf, maxbuf, args)){
return -1;
}
int maxlen = strlen(name) + strlen(args) + 1;
if (maxlen < 128){
char cmd[128];
VPRINTF("%s SITE:%s cmd:%s\n", __FUNCTION__, getenv("SITE"), cmd);
snprintf(cmd, 128, "%s %s", name, args);
return runcmd(cmd, buf, maxbuf);
}else{
char* cmd = new char[max(4096, maxlen)];
snprintf(cmd, maxlen, "%s %s", name, args);
int rc = runcmd(cmd, buf, maxbuf);
delete [] cmd;
return rc;
}
}
static int get_site(const char* _name) {
char _nn[128];
strncpy(_nn, _name, 128);
char* ss = basename(dirname(_nn));
if (strlen(ss) > 0 && strlen(ss) <= 2){
for (char* pc = ss; *pc; ++pc){
if (!isalnum(*pc)){
return 0;
}
}
return atoi(ss);
}
return 0;
}
public:
KnobX(const char* _name) : Knob(_name), site(get_site(_name)) {
struct stat sb;
int ic = 0; attr[ic] = '\0';
if (stat(name, &sb) != -1){
if (sb.st_mode&(S_IRUSR|S_IRGRP|S_IROTH)) attr[ic++] = 'r';
if (sb.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH)) attr[ic++] = 'w';
if (sb.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH)){
attr[ic++] = 'x';
}else{
fprintf(stderr, "ERROR: KnobX \"%s\" is not executable\n", _name);
}
attr[ic] = '\0';
VPRINTF("%s %s\n", __FUNCTION__, name);
}else{
fprintf(stderr, "ERROR: KnobX \"%s\" does not exist\n", _name);
}
}
virtual int get(char* buf, int maxbuf) {
char cmd[128];
snprintf(cmd, 128, "%s ", name); // @@todo no trailing space, no work
return runcmd(cmd, buf, maxbuf);
}
virtual void print(void) { cprint("KnobX"); }
virtual const char* getAttr() {
return attr;
}
};
int KnobX::runcmd(const char* cmd, char* buf, int maxbuf){
char cmd2[128];
if (site){
snprintf(cmd2, 128, "SITE=%d %s", site, cmd);
cmd = cmd2;
VPRINTF("%s cmd %s\n", __FUNCTION__, cmd);
}
Pipe knob(cmd, "r");
if (knob.fp == NULL) {
return -snprintf(buf, maxbuf,
"ERROR: failed to open \"%s\"\n", name);
}
char* cursor = buf;
for (; (cursor = fgets(cursor, maxbuf-(cursor-buf), knob.fp)) != NULL; ){
int ll = strlen(cursor);
if (ll == 1 && *cursor == '\n'){
continue;
}
cursor += ll;
}
return cursor-buf;
}
#define HASX(mode) (((mode)&(S_IXUSR|S_IXGRP|S_IXOTH)) != 0)
#define HASW(mode) (((mode)&(S_IWUSR|S_IWGRP|S_IWOTH)) != 0)
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
class PeerFinder {
vector<int> peers;
vector<string> knobs;
static PeerFinder *_instance;
PeerFinder() {
}
void fillPeerNames(vector<string>* peer_names, string knob) {
vector<int>::iterator it;
for (it = peers.begin(); it != peers.end(); ++it){
if (*it != site){
char name[80];
sprintf(name, "../%d/%s", *it, knob.c_str());
struct stat sb;
if (stat(name, &sb) != -1){
peer_names->push_back(*new string(name));
}
}
}
}
void initKnobs(vector<string> kdef) {
knobs = kdef;
}
void initPeers(vector<string> pdef) {
vector<string>::iterator it;
for(it = pdef.begin(); it != pdef.end(); ++it){
peers.push_back(atoi((*it).c_str()));
}
}
public:
static PeerFinder* instance() {
if (_instance == 0){
_instance = new PeerFinder;
}
return _instance;
}
static PeerFinder* create(string def_file);
bool hasPeers(string knob){
return peers.size() > 0 &&
std::find(knobs.begin(), knobs.end(), knob) != knobs.end();
}
vector<string>* getPeernames(string knob){
// peer_names belongs to client on return
vector<string>* peer_names = new vector<string>();
if (hasPeers(knob)){
fillPeerNames(peer_names, knob);
VPRINTF("fillPeerNames() %s %d\n", knob.c_str(), peer_names->size());
}
return peer_names;
}
};
PeerFinder* PeerFinder::_instance;
#define PKEY "PEERS="
#define KKEY "KNOBS="
#define KEYLEN 6
PeerFinder* PeerFinder::create(string def_file)
{
_instance = new PeerFinder();
FILE *fp = fopen(def_file.c_str(), "r");
if(fp){
char defline[128];
while(fgets(defline, sizeof(defline), fp)){
chomp(defline);
if (defline[0] == '#' || strlen(defline) < 2){
continue;
}else{
std::vector<std::string> elems;
if (strncmp(PKEY, defline, KEYLEN) == 0){
_instance->initPeers(split(defline+KEYLEN, ',', elems));
}
if (strncmp(KKEY, defline, KEYLEN) == 0){
_instance->initKnobs(split(defline+KEYLEN, ',', elems));
}
}
}
}
return _instance;
}
Knob* Knob::create(const string _name, mode_t mode)
{
const char* name = _name.c_str();
Knob* knob;
int verbosek = 0;
if (strncmp(name, "streamtonowhered", 10) == 0){
VPRINTF("we have it\n");
verbosek = 1;
}
if (HASX(mode)){
knob = new KnobX(name);
}else if (HASW(mode)){
static int limit_check = -2;
Knob* knob = new KnobRW(name);
if (limit_check == -2){
limit_check = access("/usr/share/doc/numerics", F_OK);
}
if (limit_check == 0){
char cmd[128];
char reply[128];
sprintf(cmd, "grep %s /usr/share/doc/numerics", name);
Pipe pn(cmd, "r");
if (fgets(reply, 128, pn.fp)){
Validator* v = NumericValidator::create(reply);
if (v){
knob->validator = v;
}
}
}
return knob;
}else{
knob = new KnobRO(name);
}
if (verbosek){
knob->print();
VPRINTF("::create done\n");
}
return knob;
}
class GroupKnob : public Knob {
GroupKnob(string name) : Knob(name.c_str()) {}
virtual int _set(char* buf, int maxbuf, const char* args)
/* the peers do ALL the work */
{
return -1;
}
public:
virtual int get(char* buf, int maxbuf) {
return peers[0]->get(buf, maxbuf);
}
static Knob* create(string name, string def);
};
#define GROUP_MODE (S_IWUSR|S_IWGRP|S_IWOTH)
Knob* GroupKnob::create(string name, string def)
{
Knob* knob = new GroupKnob(name);
/* first collect all the peers in the same site .. they already exist */
/* the double iteration isn't efficient, but 666MIPS .. who cares .. */
vector<string> peer_names = glob(def);
vector<string>::iterator its;
for (its = peer_names.begin(); its < peer_names.end(); ++its){
for (VKI itk = KNOBS.begin(); itk != KNOBS.end(); ++itk){
if (Knob::match((*itk)->getName(), (*its).c_str()) == 1){
knob->peers.push_back(*itk);
}
}
}
/* then collect peers for other sites. we have to make them */
if (PeerFinder::instance()->hasPeers(name)){
vector<string>* peer_names = PeerFinder::instance()->getPeernames(name);
vector<string>::iterator it;
for (it = peer_names->begin(); it != peer_names->end(); ++it){
VPRINTF("create Knob %s\n", (*it).c_str());
knob->peers.push_back(Knob::create(*it, GROUP_MODE));
}
VPRINTF("knob %s has %d peers\n", name.c_str(), knob->peers.size());
}
return knob;
}
class Grouper {
static void create(vector<string> &elems);
public:
static void create(string group);
};
/*
group file
group_name group_def
group_def is classically a glob pattern
in the future, it could be a comma sep list
*/
void Grouper::create(vector<string> &elems) {
KNOBS.push_back(GroupKnob::create(elems[0], elems[1]));
}
void Grouper::create(string def_file)
{
FILE *fp = fopen(def_file.c_str(), "r");
if(fp){
char defline[128];
while(fgets(defline, sizeof(defline), fp)){
chomp(defline);
if (defline[0] == '#' || strlen(defline) < 2){
continue;
}else{
std::vector<std::string> elems;
split(defline, '=', elems);
if (elems.size() < 2){
fprintf(stderr, "ERROR: group def must be group=glob\n");
}
create(elems);
}
}
}
fclose(fp);
}
class Helper: public Knob
{
public:
Helper(const char* _name) : Knob(_name) {}
};
class Prompt: public Helper
/* singleton */
{
static bool enabled;
Prompt(): Helper("prompt") {}
/* return >0 on success, <0 on fail */
virtual int _set(char* buf, int maxbuf, const char* args) {
if (strcmp(args, "on") == 0){
enabled = 1;
}else if (strcmp(args, "off") == 0){
enabled = 0;
}
return 1;
}
public:
char* getName() { return name; }
virtual int get(char* buf, int maxbuf) {
return snprintf(buf, maxbuf, "%s", enabled? "on": "off");
}
virtual void print(void) { cprint("Knob"); }
void prompt(FILE* fout) {
if (enabled){
fprintf(fout, "acq400.%d %d >", site, err);
}else if (err){
;
}
fflush(fout);
}
static Prompt* instance() {
static Prompt* _instance;
if (!_instance){
return _instance = new Prompt;
}else{
return _instance;
}
}
};
bool Prompt::enabled;
/*
for file in $(ls -1)
do
echo $file:
HTEXT="$(grep -m1 ^$file $HROOT/acq400_help* | cut -f2-)"
if [ $? -eq 0 ]; then
echo " $HTEXT";
else
echo $file;
fi
done
*/
#define HROOT "/usr/share/doc"
class Help: public Helper {
protected:
virtual int query(Knob* knob, char* buf, int buflen){
snprintf(buf, buflen, "%s\n", knob->getName());
return 1;
}
virtual int _set(char* buf, int maxbuf, const char* args) {
char* cursor = buf;
for (VKI it = KNOBS.begin(); it != KNOBS.end(); ++it){
if (Knob::match((*it)->getName(), args)){
query(*it, cursor, MAXOUTBUF - (cursor-buf));
cursor += strlen(cursor);
}
}
return 1;
}
public:
Help() : Helper("help") {}
Help(const char* _key) : Helper(_key) {}
virtual int get(char* buf, int maxbuf) {
char* cursor = buf;
for (VKI it = KNOBS.begin(); it != KNOBS.end(); ++it){
query(*it, cursor, MAXOUTBUF - (cursor-buf));
cursor += strlen(cursor);
}
return 1;
}
};
class Help2: public Help {
protected:
virtual int query(Knob* knob, char* buf, int buflen){
char cmd[128];
char reply[128];
sprintf(cmd, "grep -m1 ^%s %s/acq400_help* | cut -f2 -",
knob->getName(), HROOT);
Pipe grep(cmd, "r");
if (fgets(reply, 128, grep.fp)){
snprintf(buf, buflen, "%-20s : %s\n\t%s",
knob->getName(), knob->getAttr(), reply);
}
return 1;
}
public:
Help2() : Help("help2") {}
};
class HelpA: public Help {
protected:
virtual int query(Knob* knob, char* buf, int buflen){
snprintf(buf, buflen, "%-20s : %s\n",
knob->getName(), knob->getAttr());
return 1;
}
public:
HelpA() : Help("helpA") {}
};
int filter(const struct dirent *dir)
{
return fnmatch(pattern, dir->d_name, 0);
}
void announce() {
char fname[80];
sprintf(fname, "/var/run/acq400_knobs.%d.pid", ::site);
FILE* fp = fopen(fname, "w");
fprintf(fp, "%d", getpid());
fclose(fp);
}
int do_scan()
{
struct dirent **namelist;
int nn = scandir(".", &namelist, filter, versionsort);
if (nn < 0){
perror("scandir");
exit(1);
}
VPRINTF("scandir returned %d\n", nn);
for (int ii = 0; ii < nn; ++ii){
if (strcmp(namelist[ii]->d_name, "peers") == 0){
PeerFinder::create("peers");
break;
}
}
for (int ii = 0; ii < nn; ++ii){
char* alias = namelist[ii]->d_name;
struct stat sb;
if (stat(alias, &sb) == -1){
VPRINTF("ERROR: rejecting %s\n", alias);
perror("stat");
}else{
if (!S_ISREG(sb.st_mode)){
VPRINTF("not a regular file:%s", alias);
}else{
Knob* knob = Knob::create(alias, sb.st_mode);
if (PeerFinder::instance()->hasPeers(alias)){
vector<string>* peer_names = PeerFinder::instance()->getPeernames(alias);
vector<string>::iterator it;
for (it = peer_names->begin(); it != peer_names->end(); ++it){
knob->peers.push_back(Knob::create(*it, sb.st_mode));
}
}
KNOBS.push_back(knob);
}
}
}
KNOBS.push_back(Prompt::instance());
KNOBS.push_back(new Help);
KNOBS.push_back(new Help2);
KNOBS.push_back(new HelpA);
for (int ii = 0; ii < nn; ++ii){
if (strcmp(namelist[ii]->d_name, "groups") == 0){
Grouper::create("groups");
break;
}
}
free(namelist);
char newpath[1024];
snprintf(newpath, 1023, "%s:%s", get_current_dir_name(), getenv("PATH"));
setenv("PATH", newpath, 1);
announce();
return 0;
}
bool is_tcp_server;
struct poptOption opt_table[] = {
{ "server", 's', POPT_ARG_NONE, 0, 's', "tcp server" },
{ "host", 'h', POPT_ARG_STRING, &host, 0, "restrict to host (any)" },
{ "port", 'p', POPT_ARG_STRING, &port, 0, "use this port (4220+site)"},
POPT_AUTOHELP
POPT_TABLEEND
};
void cli(int argc, const char** argv)
{
const char *dir;
char dbuf[128];
char sname[32];
const char* arg1;
const char* arg2;
poptContext opt_context =
poptGetContext(argv[0], argc, argv, opt_table, 0);
int rc;
while ( (rc = poptGetNextOpt( opt_context )) >= 0 ){
switch(rc){
case 's':
is_tcp_server = true;
break;
}
}
if (getenv("VERBOSE")){
verbose = atoi(getenv("VERBOSE"));
}
VPRINTF("%s verbose set %d\n", VERID, verbose);
arg1 = poptGetArg(opt_context);
if (!arg1){
return;
}
arg2 = poptGetArg(opt_context);
if (arg2){
dir = arg2;
}else{
site = atoi(arg1);
sprintf(dbuf, "/etc/acq400/%d", site);
dir = dbuf;
sprintf(sname, "%d", site);
}
setenv("SITE", sname, 0);
chdir(dir);
if (is_tcp_server){
if (port == 0){
port = new char[16];
sprintf(port, "%d", 4220+site);
}
}
}
int interpret_phrase(char* phrase, char* buf_out, FILE* fout)
{
char *args = 0;
char *key = 0;
bool is_query = false;
bool found = false;
chomp(phrase);
if (strlen(phrase) == 0){
return 0;
}
unsigned isep = strcspn(phrase, "= ");
if (isep != strlen(phrase)){
args = phrase + isep + strspn(phrase+isep, "= ");
phrase[isep] = '\0';
}else{
is_query = true;
}
key = phrase;
for (VKI it = KNOBS.begin(); it != KNOBS.end(); ++it){
Knob* knob = *it;
err = false;
int rc;
bool is_glob = true;
buf_out[0] = '\0';
switch(Knob::match(knob->getName(), key)){
case 0:
continue;
case 1:
is_glob = false;
it = KNOBS.end() - 1; // fall thru, drop out
case -1:
if (is_query){
rc = knob->get(buf_out, 4096);
if (is_glob){
if (dynamic_cast<Helper*>(knob) != 0){
/* don't query Helper */
continue;
}
if (!strstr(buf_out, knob->getName())){
fprintf(fout, "%s ", knob->getName());
}
}
}else{
rc = knob->set(buf_out, 4096, args);
}
if (rc){
fprintf(fout, "%s\n", chomp(buf_out));
fflush(fout);
}
err = rc < 0;
found = true;
}
}
if (!found){
fprintf(fout, "ERROR:\%s\" not found\n", key);
}
return 0;
}
int interpreter(FILE* fin, FILE* fout)
{
char* buf_in = new char[4096];
char* buf_out = new char[MAXOUTBUF];
for (; fgets(buf_in, 4096, fin); Prompt::instance()->prompt(fout)){
char* phrase = buf_in;
char* phrase99;
while ((phrase99 = strchr(phrase, ';'))){
char* phrase2 = phrase99 + strspn(phrase99, "; \t");
*phrase99 = '\0';
interpret_phrase(phrase, buf_out, fout);
phrase = phrase2;
}
interpret_phrase(phrase, buf_out, fout);
}
return 0;
}
#define INET_ADDRSTRLEN 20
#include <netinet/in.h>
#include <arpa/inet.h>
class ServerInfoVerbose: public ServerInfo {
const char* ip_addr(struct sockaddr_in& s_in){
static char buffer[INET_ADDRSTRLEN];
return inet_ntop( AF_INET, &s_in.sin_addr, buffer, sizeof( buffer ));
}