-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharmsd.c
2563 lines (2198 loc) · 56 KB
/
armsd.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
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
/*
* Copyright (c) 2012, Internet Initiative Japan, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <dirent.h>
#include <err.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libarms.h"
#include "armsd.h"
#define ARMSDIRPERM 0755 /* 0755 for debug, 0700 for prod */
#define HEARTBEAT_MAX 5
#define PATHLEN 256
#define PATHMARMSDCONF "/etc/armsd/armsd.conf"
#define PATHICONFIG "/etc/armsd/initial-config"
#define PATHSTATECACHE "/var/cache/armsd/state"
#define PATHPIDFILE "/var/run/armsd.pid"
const char *opt_basedir = NULL;
int opt_debug = 0;
int opt_hbdebug = 0;
const char *opt_distid = NULL;
const char *opt_logfile = NULL;
const char armsd_progname[] = "armsd";
const char armsd_version[] = "0.8.0";
const char arms_root_ca_certificate[] =
"-----BEGIN CERTIFICATE-----\n"
"MIICxjCCAa6gAwIBAgIBADANBgkqhkiG9w0BAQUFADAXMRUwEwYDVQQDEwxBUk1T\n"
"IFJvb3QgQ0EwHhcNMDcwNTIzMDQ0MjEyWhcNMzcwNjE0MDQ0MjEyWjAXMRUwEwYD\n"
"VQQDEwxBUk1TIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB\n"
"AQDJRayC6SYg897wpwf56UffftVpGGtiYOi8LO4/L9lnPaMw7b9P8r30UbXy0AfX\n"
"rHzQHSBpdHTVopFONT6dMhd2kIyvFAw3r8H/lRK0VmIDV+dE3Y/rRFdB1P3TYQoC\n"
"rpUEsDJeQ/3bc0f/UwyCgm9laigMD6sGqEuFBFm7ovFfp/y2RtN1QKgCBGGKbz6z\n"
"dSgfqG0BwISICjqVEBM2ltOr93pBHrlI9btER1OP7CwCvahHhwUhgsUj7US3mPrd\n"
"AHTlWb9iyd3+EdstxsSZZRZa4iJB+f2OIVcb02o+Q9baHb8IZWwnY0Ei+rgZQZDN\n"
"WLOMXLLsEuPGlj6DHgRTOXR5AgMBAAGjHTAbMAwGA1UdEwQFMAMBAf8wCwYDVR0P\n"
"BAQDAgEGMA0GCSqGSIb3DQEBBQUAA4IBAQBPeMgeVIIiipIMypT7aPcfyaDuAJDy\n"
"MFrAEBGUCQDVwt1g6OtlJb5At/N3lXc8U149P11Uu7+lMescESassXnUVV2DNOrX\n"
"3uv6Jw2H3fhqaVvnPO3hlgJqjjqE1bVZJBA7I7H8Z0k2BvFAu3EvmT9dWabyWtqW\n"
"3UW/fhXV5byJ//0JEcGoWv1sxjG3/jNiZ3YhX0ZxzITAmWbRIpaljEq/mgzgAj7G\n"
"EIBaFnH6rpYsISaEAsFQilIWOfZIGHz5DVRjqQ70IAglxDRER7bdx91312chamxK\n"
"o+dGih8xsWZ7ntkKAEwZzsmgn+ZCDOJQK5t2pAe+8kdl4oVMpOMb78hM\n"
"-----END CERTIFICATE-----\n";
distribution_id_t distid;
arms_context_t *arms_context;
int arms_callback_udata;
int arms_listen_port;
FILE *log_fp = NULL;
const char *pidfile = NULL;
int pidfilecreated = 0;
pid_t script_pid = -1;
int script_exitcode = -1;
const char *configfile = PATHMARMSDCONF;
char *dconf_text = NULL;
struct dconf {
const char *name;
const char *def;
char *val;
} dconf_list[] = {
{ "app-event-interval", "300", NULL },
{ "binary", "0", NULL },
{ "distribution-id", NULL, NULL },
{ "hb-disk-usage", "/", NULL },
{ "hb-dummy", "0", NULL },
{ "hb-traffic-if0", "eth0", NULL },
{ "hb-traffic-if1", "eth1", NULL },
{ "hb-traffic-if2", "eth2", NULL },
{ "https-proxy-url", NULL, NULL },
{ "https-proxy-url-ls", NULL, NULL },
{ "https-proxy-url-rs", NULL, NULL },
{ "ls-sa-key", "", NULL },
{ "path-iconfig", NULL, NULL },
{ "path-root-ca", NULL, NULL },
{ "path-pid", PATHPIDFILE, NULL },
{ "path-state-cache", NULL, NULL },
{ "script-clear", NULL, NULL },
{ "script-command", NULL, NULL },
{ "script-debug", NULL, NULL },
{ "script-reconfig", NULL, NULL },
{ "script-start", NULL, NULL },
{ "script-status", NULL, NULL },
{ "script-stop", NULL, NULL },
{ "script-reboot", NULL, NULL },
{ "script-line-ctrl", NULL, NULL },
{ "script-state-changed", NULL, NULL },
{ "timeout", "30", NULL },
{ NULL, NULL, NULL }
};
arms_line_conf_dhcp_t line_dhcp = { 0 };
arms_line_conf_ra_t line_ra = { 0 };
arms_line_desc_t lines[] = {
{ ARMS_LINE_DHCP, &line_dhcp },
{ ARMS_LINE_RA, &line_ra },
{ ARMS_LINE_NONE, NULL }
};
struct callback_state {
FILE *in;
FILE *out;
char *infile;
char *outfile;
int nul;
};
struct module {
unsigned long id;
char *idstr;
char *version;
char *infostring;
struct config_state {
FILE *fp;
char *tmpfile;
} cstate;
struct callback_state clear_state;
struct callback_state get_status_state;
struct callback_state read_config_state;
struct callback_state md_command_state;
struct module *next; /* linked list */
};
struct module *module_list = NULL;
static struct callback_state state_dump_debug;
struct scriptlog {
unsigned int bufsize;
unsigned int readptr;
unsigned int writeptr;
char buf[1000];
};
static struct scriptlog scriptlog;
struct timeout {
struct timespec ts;
unsigned int sec;
};
static struct armsdir {
/* directory */
char *basedir;
char *backupdir;
char *candidatedir;
char *runningdir;
char *startupdir;
char *tmpdir;
/* file */
const char *tmpfile;
/* temporary file counter */
unsigned int tmpnum;
/* we created basedir? */
int created;
} armsdir = { NULL, };
static int ihcw; /* i hate (useless) compiler warnings */
static const char *arms_config_string(int);
static const char *arms_error_string(int);
static const char *arms_state_string(int);
static int callback_app_event(void *);
static int callback_clear_status(uint32_t, const char *, size_t, char *,
size_t, int *, void *);
static int callback_command(uint32_t, int, const char *, size_t, char *,
size_t, int *, void *);
static int callback_config(uint32_t, const char *, const char *, int,
const char *, size_t, int, void *);
static int callback_dump_debug(char *, size_t, int *, void *);
static int callback_heartbeat(arms_context_t *, void *);
static int callback_generic(uint32_t, const char *, size_t, char *, size_t,
int *, void *, const char *,
struct callback_state *, int);
static int callback_get_status(uint32_t, const char *, size_t, char *, size_t,
int *, void *);
static int callback_line_ctrl(int, int, void *, void *);
static int callback_log(int, const char *, void *);
static int callback_md_command(uint32_t, const char *, size_t, char *,
size_t, int *, void *);
static int callback_ping(const arms_ping_arg_t *, char *, size_t, int *,
void *);
static int callback_read_config(uint32_t, int, char *, size_t, int *, void *);
static int callback_state(int, int, void *);
static void callback_state_set_proxy(const char *);
static int callback_traceroute(const arms_traceroute_arg_t *, char *, size_t,
int *, void *);
static int armsdir_create(void);
static int armsdir_create_sub(const char *, char **);
static int armsdir_remove(void);
static void armsdir_remove_tmpfiles(void);
static int armsdir_set_base(void);
static struct module *module_new(unsigned long, const char *, const char *);
static void module_delete(struct module *);
static void module_all_remove_files(void);
static int module_config_exist(const struct module *, int);
static const char *module_config_path(const struct module *, int);
static int module_config_pathcpy(const struct module *, int, char *,
unsigned int);
static void module_copy_backup2running(unsigned long);
static void module_copy_candidate2running(unsigned long);
static void module_copy_candidate(unsigned long, const char *);
static void module_copy_running(unsigned long, const char *);
static void module_copy_running2backup(unsigned long);
static void module_copy_running2startup(unsigned long);
static void module_copy_sub_ln(unsigned long, const char *, const char *);
static void module_copy_sub_mv(unsigned long, const char *, const char *);
static struct module *module_find(unsigned long);
static int module_list_append(struct module *);
static struct module *module_list_iter_first(void);
static struct module *module_list_iter_next(struct module *);
static int module_list_remove(struct module *);
static void module_remove_candidate(struct module *);
static void module_remove_files(const struct module *);
static void heartbeat_prepare_dummy(arms_context_t *);
static int dconf_load(void);
static int dconf_parse(char *);
static char *dconf_trim(char *);
static int log_open_file(const char *);
static const char *log_priority_string(int);
static void log_close(void);
static void pidfile_create(void);
static void pidfile_remove(void);
void script_clearlog(void);
void script_errlog(const char *, int);
void script_initenv(void);
void script_logcpy(char *, const char *, unsigned int);
struct timeout *timeout_new(unsigned int);
void timeout_free(struct timeout *);
int timeout_remaining(struct timeout *);
void sighandler_init(void);
void sighandler_child(int);
void sighandler_exit(int);
void sighandler_reload(int);
static void show_version(void);
static const char *make_version_log(void);
static void usage(void);
static void cleanup(void);
static int str2distid(const char *, distribution_id_t *);
static int distid2str(const distribution_id_t *, char *, unsigned int);
static int load_from_file(const char *, char **);
static void load_state_cache(void);
static void save_state_cache(void);
static void sa_stop_modules(void);
static void sa_reboot(void);
static arms_callback_tbl_t arms_callback_table = {
ARMS_LIBPULL_VERSION,
callback_config,
callback_line_ctrl,
callback_state,
callback_log,
callback_read_config,
callback_get_status,
callback_command,
callback_app_event,
callback_heartbeat,
};
static const char *
arms_config_string(int type)
{
switch (type) {
case ARMS_CONFIG_BACKUP: return "backup-backup";
case ARMS_CONFIG_CANDIDATE: return "candidate-config";
case ARMS_CONFIG_RUNNING: return "running-config";
default: return "(unknown-config)";
}
}
static const char *
arms_error_string(int e)
{
static char ebuf[80];
switch (e) {
default:
snprintf(ebuf, sizeof(ebuf), "(unknown error code %d)", e);
return ebuf;
}
}
static const char *
arms_state_string(int state)
{
const char *s;
switch (state) {
case ARMS_ST_INITIAL: s = "INITIAL"; break;
case ARMS_ST_LSPULL: s = "LSPULL"; break;
case ARMS_ST_RSPULL: s = "RSPULL"; break;
case ARMS_ST_PULLDONE: s = "PULLDONE"; break;
case ARMS_ST_BOOT_FAIL: s = "BOOT_FAIL"; break;
case ARMS_ST_PUSH_INITIAL: s = "PUSH_INITIAL"; break;
case ARMS_ST_PUSH_SENDREADY: s = "PUSH_SENDREADY"; break;
case ARMS_ST_PUSH_WAIT: s = "PUSH_WAIT"; break;
case ARMS_ST_PUSH_REBOOT: s = "PUSH_REBOOT"; break;
default: s = "(unknown)"; break;
}
return s;
};
static int
armsdir_create(void)
{
int e = 0;
/* create base directory */
if (mkdir(armsdir.basedir, ARMSDIRPERM) == -1) {
logit(LOG_ERR, "mkdir(\"%s\") failed: %s",
armsdir.basedir, strerror(errno));
return -1;
}
e += armsdir_create_sub("backup-config", &armsdir.backupdir);
e += armsdir_create_sub("candidate-config", &armsdir.candidatedir);
e += armsdir_create_sub("running-config", &armsdir.runningdir);
e += armsdir_create_sub("startup-config", &armsdir.startupdir);
e += armsdir_create_sub("tmp", &armsdir.tmpdir);
armsdir.tmpnum = 0;
return e;
}
/* NOTE: returns 0 for success, 1 for error. */
static int
armsdir_create_sub(const char *name, char **ptr)
{
unsigned int n;
char *p;
n = strlen(armsdir.basedir) + 1 + strlen(name) + 1;
p = malloc(n);
if (p == NULL) {
logit(LOG_EMERG, "malloc(3) failed");
return 1;
}
snprintf(p, n, "%s/%s", armsdir.basedir, name);
if (mkdir(p, ARMSDIRPERM) == -1) {
logit(LOG_ERR, "mkdir(\"%s\") failed: %s",
name, strerror(errno));
free(p);
return 1;
}
*ptr = p;
return 0;
}
FILE *
armsdir_create_tmpfile(char **ptr)
{
FILE *fp;
int fd;
char *p;
char buf[128];
snprintf(buf, sizeof(buf), "%s/%u", armsdir.tmpdir, armsdir.tmpnum);
armsdir.tmpnum++;
fd = open(buf, O_RDWR|O_CREAT|O_EXCL, 0600);
if (fd == -1)
err(1, "cannot create tmpfile: %s", buf);
if (ptr != NULL) {
p = strdup(buf);
if (p == NULL)
err(1, "strdup() failed");
*ptr = p;
}
fp = fdopen(fd, "r+");
if (fp == NULL)
err(1, "fopen failed: %s", buf);
return fp;
}
static int
armsdir_remove(void)
{
if (armsdir.basedir == NULL)
return 0;
module_all_remove_files();
armsdir_remove_tmpfiles();
if (rmdir(armsdir.backupdir) != 0)
warn("cannot remove %s", armsdir.backupdir);
if (rmdir(armsdir.candidatedir) != 0)
warn("cannot remove %s", armsdir.candidatedir);
if (rmdir(armsdir.runningdir) != 0)
warn("cannot remove %s", armsdir.runningdir);
if (rmdir(armsdir.startupdir) != 0)
warn("cannot remove %s", armsdir.startupdir);
if (rmdir(armsdir.tmpdir) != 0)
warn("cannot remove %s", armsdir.tmpdir);
if (rmdir(armsdir.basedir) != 0)
warn("cannot remove %s", armsdir.basedir);
return 0;
}
static void
armsdir_remove_tmpfiles(void)
{
DIR *dirp;
struct dirent *dp;
char buf[256];
dirp = opendir(armsdir.tmpdir);
if (dirp == NULL) {
logit(LOG_ERR, "cannot opendir %s: %s",
armsdir.tmpdir, strerror(errno));
return;
}
dirp = opendir(armsdir.tmpdir);
while ((dp = readdir(dirp)) != NULL) {
/* will not remove ".", ".." and ".hoge" */
if (dp->d_name[0] == '.')
continue;
snprintf(buf, sizeof(buf),
"%s/%s", armsdir.tmpdir, dp->d_name);
(void)unlink(buf);
}
closedir(dirp);
}
static int
armsdir_set_base(void)
{
struct stat st;
unsigned int n;
char buf[256], *p;
if (opt_basedir != NULL) {
if (*opt_basedir == '/') {
n = strlen(opt_basedir);
p = strdup(opt_basedir);
} else {
/* convert relative path to absolute path */
if (getcwd(buf, sizeof(buf)) == NULL) {
logit(LOG_ERR, "getcwd(3) failed...");
return -1;
}
n = strlen(buf) + 1 + strlen(opt_basedir) + 1;
p = malloc(n);
if (p != NULL)
snprintf(p, n, "%s/%s", buf, opt_basedir);
}
} else {
snprintf(buf, sizeof(buf), "/tmp/armsd.%lu",
(unsigned long)getpid());
n = strlen(buf);
p = strdup(buf);
}
if (p == NULL) { /* really? */
logit(LOG_ERR, "no memory (cannot allocate %u bytes)", n);
return -1;
}
if (stat(p, &st) == 0) {
logit(LOG_ERR, "base directory already exists: %s", p);
free(p);
return -1;
}
armsdir.basedir = p;
return 0;
}
static struct module *
module_new(unsigned long id, const char *version, const char *infostring)
{
struct module *mod;
char idbuf[16];
mod = malloc(sizeof(struct module));
if (mod == NULL)
return NULL;
mod->id = id;
snprintf(idbuf, sizeof(idbuf), "%lu", id);
mod->idstr = strdup(idbuf);
mod->version = strdup((version != NULL) ? version : "-");
mod->infostring = strdup((infostring != NULL) ? infostring : "-");
if (mod->idstr == NULL ||
mod->version == NULL ||
mod->infostring == NULL) {
logit(LOG_ERR, "memory exausted");
free(mod->idstr);
free(mod->version);
free(mod->infostring);
free(mod);
return NULL;
}
memset(&mod->cstate, 0, sizeof(mod->cstate));
memset(&mod->clear_state, 0, sizeof(mod->clear_state));
memset(&mod->get_status_state, 0, sizeof(mod->get_status_state));
memset(&mod->read_config_state, 0, sizeof(mod->read_config_state));
memset(&mod->md_command_state, 0, sizeof(mod->md_command_state));
mod->next = NULL;
module_list_append(mod);
return mod;
}
static void
module_delete(struct module *mod)
{
module_list_remove(mod);
module_remove_candidate(mod);
module_remove_files(mod);
free(mod->idstr);
free(mod->version);
free(mod->infostring);
free(mod);
}
static void
module_all_remove_files(void)
{
struct module *m;
for (m = module_list; m != NULL; m = m->next)
module_remove_files(m);
}
static int
module_config_exist(const struct module *mod, int type)
{
char buf[128];
if (module_config_pathcpy(mod, type, buf, sizeof(buf)) > 0)
return (access(buf, F_OK) == 0);
else
return 0;
}
/* return a pointer to a static varaible - use with care */
static const char *
module_config_path(const struct module *mod, int type)
{
static char buf[256];
if (module_config_pathcpy(mod, type, buf, sizeof(buf)) != -1)
return buf;
else
return NULL;
}
static int
module_config_pathcpy(const struct module *mod, int type, char *buf,
unsigned int len)
{
const char *dir;
switch (type) {
case ARMS_CONFIG_BACKUP: dir = armsdir.backupdir; break;
case ARMS_CONFIG_CANDIDATE: dir = armsdir.candidatedir; break;
case ARMS_CONFIG_RUNNING: dir = armsdir.runningdir; break;
default:
return -1;
}
return snprintf(buf, len, "%s/%lu", dir, mod->id);
}
static void
module_copy_backup2running(unsigned long mid)
{
char buf[128];
snprintf(buf, sizeof(buf), "%s/%lu", armsdir.backupdir, mid);
module_copy_sub_ln(mid, buf, armsdir.runningdir);
}
static void
module_copy_candidate2running(unsigned long mid)
{
char buf[128];
snprintf(buf, sizeof(buf), "%s/%lu", armsdir.candidatedir, mid);
module_copy_sub_ln(mid, buf, armsdir.runningdir);
}
static void
module_copy_candidate(unsigned long mid, const char *filename)
{
module_copy_sub_mv(mid, filename, armsdir.candidatedir);
}
static void
module_copy_running2backup(unsigned long mid)
{
char buf[128];
snprintf(buf, sizeof(buf), "%s/%lu", armsdir.runningdir, mid);
module_copy_sub_ln(mid, buf, armsdir.backupdir);
}
static void
module_copy_running2startup(unsigned long mid)
{
const struct module *mod;
char buf[128];
if ((mod = module_find(mid)) == NULL)
return;
if (module_config_pathcpy(mod, ARMS_CONFIG_RUNNING,
buf, sizeof(buf)) == -1)
return;
module_copy_sub_ln(mid, buf, armsdir.startupdir);
}
static void
module_copy_running(unsigned long mid, const char *filename)
{
module_copy_sub_mv(mid, filename, armsdir.runningdir);
}
static void
module_copy_sub_ln(unsigned long mid, const char *filename, const char *dir)
{
char buf[128];
snprintf(buf, sizeof(buf), "%s/%lu", dir, mid);
if (unlink(buf) == -1 && errno != ENOENT)
err(1, "unlink %s", buf);
if (link(filename, buf) == -1)
err(1, "link %s -> %s", filename, buf);
}
static void
module_copy_sub_mv(unsigned long mid, const char *filename, const char *dir)
{
module_copy_sub_ln(mid, filename, dir);
if (unlink(filename) == -1)
err(1, "unlink %s", filename);
}
static struct module *
module_find(unsigned long mid)
{
struct module *m;
for (m = module_list; m != NULL; m = m->next) {
if (m->id == mid)
return m;
}
return NULL;
}
static int
module_list_append(struct module *mod)
{
struct module *m;
if (module_list == NULL)
module_list = mod;
else {
for (m = module_list; m->next != NULL; m = m->next)
;
m->next = mod;
}
mod->next = NULL;
return 0;
}
static struct module *
module_list_iter_first(void)
{
return module_list;
}
static struct module *
module_list_iter_next(struct module *mod)
{
return mod->next;
}
static int
module_list_remove(struct module *mod)
{
struct module *m;
if (module_list == NULL)
return -1;
if (module_list == mod)
module_list = mod->next;
else {
for (m = module_list; m != NULL; m = m->next)
if (m->next == mod) {
m->next = mod->next;
break;
}
if (m == NULL)
return -1;
}
return 0;
}
static void
module_remove_candidate(struct module *mod)
{
char buf[128];
module_config_pathcpy(mod, ARMS_CONFIG_CANDIDATE, buf, sizeof(buf));
unlink(buf);
}
static void
module_remove_files(const struct module *mod)
{
char buf[128];
snprintf(buf, sizeof(buf), "%s/%lu", armsdir.backupdir, mod->id);
unlink(buf);
snprintf(buf, sizeof(buf), "%s/%lu", armsdir.candidatedir, mod->id);
unlink(buf);
snprintf(buf, sizeof(buf), "%s/%lu", armsdir.runningdir, mod->id);
unlink(buf);
snprintf(buf, sizeof(buf), "%s/%lu", armsdir.startupdir, mod->id);
unlink(buf);
}
static void
heartbeat_prepare_dummy(arms_context_t *ctx)
{
uint64_t q0, q1, q2, q3, q4, q5, qq;
uint8_t c0, c1, c2, c3, c4, cq;
int i, j;
static uint64_t t[6][3];
static const uint64_t tmax[3] = {
1000 * 1000 * 1000, /* if#0 is 1000Base-T */
10 * 1000 * 1000, /* if#1 is 10Base-T */
64 * 1024 /* if#2 is ISDN :D */
};
/* cpu: 4-core cpu */
for (i = 0; i < 4; i++) {
c0 = (uint8_t)random() % 101; /* idle */
cq = 100 - c0;
arms_hb_set_cpu_usage(ctx, i, cq);
c1 = c2 = c3 = 0;
if (cq > 0) {
c1 = (uint8_t)random() % (cq + 1);
cq -= c1;
}
if (cq > 0) {
c2 = (uint8_t)random() % (cq + 1);
cq -= c2;
}
if (cq > 0) {
c3 = (uint8_t)random() % (cq + 1);
cq -= c3;
}
c4 = cq;
arms_hb_set_cpu_detail_usage(ctx, i, c0, c1, c2, c3, c4);
}
/* memory: #0 is 256MB */
qq = 256 * 1024 * 1024;
q0 = (uint64_t)random() % qq;
q1 = qq - q0;
arms_hb_set_mem_usage(ctx, 0, q0, q1);
/* memory: #1 is 3GB */
qq = 3ULL * 1024 * 1024 * 1024;
q0 = (uint64_t)random() * 3 % qq;
q1 = qq - q0;
arms_hb_set_mem_usage(ctx, 1, q0, q1);
/* disk: #0 is 32GB SSD */
qq = 32ULL * 1024 * 1024 * 1024;
q0 = (uint64_t)random() * 16 % qq;
q1 = qq - q0;
arms_hb_set_disk_usage(ctx, 0, q0, q1);
/* disk: #1 is 4TB SATA HDD */
qq = 4ULL * 1024 * 1024 * 1024 * 1024;
q0 = (uint64_t)random() * 2048 % qq;
q1 = qq - q0;
arms_hb_set_disk_usage(ctx, 1, q0, q1);
/* traffic */
for (i = 0; i < 3; i++) {
qq = tmax[i] / 8; /* bit-per-sec -> byte-per-sec */
q0 = (uint64_t)random() % qq;
q1 = (uint64_t)random() % qq;
q2 = q0 / (random() % 1458 + 64);
q3 = q1 / (random() % 1458 + 64);
q4 = q2 / 5;
q5 = q3 / 5;
arms_hb_set_traffic_rate(ctx, i, q0, q1, q2, q3, q4, q5);
if (random() % 36 == 0) {
for (j = 0; j < 6; j++)
t[j][i] = 0; /* reset! */
}
t[0][i] += q0;
t[1][i] += q1;
t[2][i] += q2;
t[3][i] += q3;
t[4][i] += q4;
t[5][i] += q5;
arms_hb_set_traffic_count(ctx, i, t[0][i], t[1][i], t[2][i],
t[3][i], t[4][i], t[5][i]);
}
/* radiowave */
c0 = random() % 101;
c1 = random() % 101;
c2 = random() % 101;
c3 = random() % 101;
arms_hb_set_radiowave(ctx, 0, c0, c1, c2, c3);
}
static void
dconf_clear(void)
{
int i;
for (i = 0; dconf_list[i].name != NULL; i++) {
free(dconf_list[i].val);
dconf_list[i].val = NULL;
}
}
int
dconf_get_int(const char *name)
{
int i;
const char *v;
for (i = 0; dconf_list[i].name != NULL; i++) {
if (strcmp(name, dconf_list[i].name) == 0) {
v = dconf_list[i].val;
if (v == NULL)
v = dconf_list[i].def;
/* XXX: strtol? */
return atoi(v);
}
}
return -1;
}
const char *
dconf_get_string(const char *name)
{
int i;
for (i = 0; dconf_list[i].name != NULL; i++) {
if (strcmp(name, dconf_list[i].name) == 0) {
if (dconf_list[i].val == NULL)
return dconf_list[i].def;
else
return dconf_list[i].val;
}
}
return NULL;
}
static int
dconf_load(void)
{
FILE *fp;
int errs;
char linebuf[1000];
dconf_clear();
fp = fopen(configfile, "r");
if (fp == NULL)
err(1, "no config file %s", configfile);
errs = 0;
while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
errs += dconf_parse(linebuf);
fclose(fp);
if (errs > 0)
return -1;
return 0;
}
static int
dconf_parse(char *line)
{
int i;
char *p, *q;
/* NOTE: line is not "const". */
p = dconf_trim(line);
if (*p == '\0') /* empty line */
return 0;
if (*p == '#') /* comment */
return 0;
q = strchr(p, ':');
if (q == NULL) {
/* no separator */
return 1;
}
*q++ = '\0';
p = dconf_trim(p);
q = dconf_trim(q);
for (i = 0; dconf_list[i].name != NULL; i++) {
if (strcmp(p, dconf_list[i].name) == 0) {
q = strdup(q);
if (q == NULL)
return 1;
dconf_list[i].val = q;
return 0;
}
}
fprintf(stderr, "warning: unknown configuration parameter: %s=%s\n",
p, q);
return 1;
}
static char *
dconf_trim(char *p)
{
char *q;
while (*p != '\0' && isspace(*p))
p++;
q = p + strlen(p);