blob: e35566a3f4fb4b5fd00a68ac5ae1d4c920b98829 [file] [log] [blame]
David Carliere9cff612022-01-21 20:46:40 +00001#define _GNU_SOURCE
2#include <dac.h>
3#include <dadwcurl.h>
4#include <dadwarc.h>
5#include <getopt.h>
6#include <stdlib.h>
7#include <signal.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <sys/mman.h>
11
12#define ATLASTOKSZ PATH_MAX
13#define ATLASMAPNM "/hapdeviceatlas"
14
15const char *__pgname;
16
17static struct {
18 da_dwatlas_t o;
19 int ofd;
20 void* atlasmap;
21} global_deviceatlassch = {
22 .ofd = -1,
23 .atlasmap = NULL
24};
25
26
27void usage(void)
28{
29 fprintf(stderr, "%s -u download URL [-d hour (in H:M:S format) current hour by default] [-p path for the downloaded file, /tmp by default]\n", __pgname);
30 exit(EXIT_FAILURE);
31}
32
33static size_t jsonread(void *ctx, size_t count, char *buf)
34{
35 return fread(buf, 1, count, ctx);
36}
37
38static da_status_t jsonseek(void *ctx, off_t pos)
39{
40 return fseek(ctx, pos, SEEK_SET) != -1 ? DA_OK : DA_SYS;
41}
42
43static void dadwlog(dw_config_t cfg, const char* msg)
44{
45 time_t now = time(NULL);
46 char buf[26] = {0};
Willy Tarreaufe1b3b82023-04-27 18:12:06 +020047 ctime_r(&now, buf);
David Carliere9cff612022-01-21 20:46:40 +000048 buf[24] = 0;
49 fprintf(stderr, "%s: %s\n", buf, msg);
50}
51
52static dw_status_t dadwnot(void *a, dw_config_t *cfg)
53{
54 da_dwatlas_t *o = (da_dwatlas_t *)a;
55 if (!o)
56 return DW_ERR;
57 char *e;
58 char jsondbuf[26] = {0}, buf[26] = {0}, atlasp[ATLASTOKSZ] = {0};
59 time_t now = time(NULL);
60 time_t jsond;
61 int fd = -1;
62 (void)a;
63 jsond = da_getdatacreation(&o->atlas);
64 dwgetfinalp(o->dcfg.info, atlasp, sizeof(atlasp));
65 ctime_r(&jsond, jsondbuf);
Willy Tarreaufe1b3b82023-04-27 18:12:06 +020066 ctime_r(&now, buf);
David Carliere9cff612022-01-21 20:46:40 +000067 jsondbuf[24] = 0;
68 buf[24] = 0;
69
70 printf("%s: data file generated on `%s`\n", buf, jsondbuf);
71 int val = 1;
72 unsigned char *ptr = (unsigned char *)global_deviceatlassch.atlasmap;
73 memset(ptr, 0, sizeof(atlasp));
74 strcpy(ptr, atlasp);
75 return DW_OK;
76}
77
78static da_status_t dadwinit(void)
79{
80 if ((global_deviceatlassch.ofd = shm_open(ATLASMAPNM, O_RDWR | O_CREAT, 0660)) == -1) {
81 fprintf(stderr, "%s\n", strerror(errno));
82 return DA_SYS;
83 }
84
85 if (ftruncate(global_deviceatlassch.ofd, ATLASTOKSZ) == -1) {
86 close(global_deviceatlassch.ofd);
87 return DA_SYS;
88 }
89 lseek(global_deviceatlassch.ofd, 0, SEEK_SET);
90 global_deviceatlassch.atlasmap = mmap(0, ATLASTOKSZ, PROT_READ | PROT_WRITE, MAP_SHARED, global_deviceatlassch.ofd, 0);
91 if (global_deviceatlassch.atlasmap == MAP_FAILED) {
92 fprintf(stderr, "%s\n", strerror(errno));
93 return DA_SYS;
94 } else {
95 memset(global_deviceatlassch.atlasmap, 0, ATLASTOKSZ);
96 return DA_OK;
97 }
98}
99
100static void dadwexit(int sig __attribute__((unused)), siginfo_t *s __attribute__((unused)), void *ctx __attribute__((unused)))
101{
102 ssize_t w;
103
104 fprintf(stderr, "%s: exit\n", __pgname);
105 dw_daatlas_close(&global_deviceatlassch.o);
106 da_fini();
107 munmap(global_deviceatlassch.atlasmap, ATLASTOKSZ);
108 close(global_deviceatlassch.ofd);
109 shm_unlink(ATLASMAPNM);
110 exit(EXIT_SUCCESS);
111}
112
113int main(int argc, char **argv)
114{
115 const char *opts = "u:p:d:h";
116 bool dset = false;
117 size_t i;
118 int ch;
119
120 da_property_decl_t extraprops[1] = {
121 { 0, 0 }
122 };
123
124 __pgname = argv[0];
125
126 dw_df_dainit_fn = curldwinit;
127 dw_df_dacleanup_fn = curldwcleanup;
128
129 da_init();
130 memset(&global_deviceatlassch.o.dcfg, 0, sizeof(global_deviceatlassch.o.dcfg));
131 while ((ch = getopt(argc, argv, opts)) != -1) {
132 switch (ch) {
133 case 'u':
134 global_deviceatlassch.o.dcfg.info.url = strdup(optarg);
135 break;
136 case 'p':
137 global_deviceatlassch.o.dcfg.info.path = strdup(optarg);
138 break;
139 case 'd':
140 if (strptime(optarg, "%H:%M:%S", &global_deviceatlassch.o.dcfg.info.rtm) != NULL)
141 dset = true;
142 else
143 usage();
144 break;
145 case 'h':
146 default:
147 usage();
148 }
149 }
150
151 if (!dset) {
152 time_t now = time(NULL);
Willy Tarreaufe1b3b82023-04-27 18:12:06 +0200153 struct tm *cnow = gmtime(&now);
David Carliere9cff612022-01-21 20:46:40 +0000154 memcpy(&global_deviceatlassch.o.dcfg.info.rtm, cnow, offsetof(struct tm, tm_mday));
155 }
156
157 if (!global_deviceatlassch.o.dcfg.info.url)
158 usage();
159
160 struct sigaction sa;
161 memset(&sa, 0, sizeof(sa));
162 sa.sa_flags = SA_SIGINFO | SA_RESTART;
163 sa.sa_sigaction = dadwexit;
164
165 global_deviceatlassch.o.dcfg.info.datatm = 1;
166 global_deviceatlassch.o.dcfg.info.chksum = 1;
167 global_deviceatlassch.o.dcfg.info.reload = 1;
168 global_deviceatlassch.o.dcfg.info.tobin = 1;
169 global_deviceatlassch.o.dcfg.ep = extraprops;
170 global_deviceatlassch.o.dcfg.dwproc = curldwproc;
171 global_deviceatlassch.o.dcfg.dwextract = dadwextract;
172 global_deviceatlassch.o.dcfg.lptr = (void *)stderr;
173 global_deviceatlassch.o.dcfg.dwlog = &dadwlog;
174 global_deviceatlassch.o.dcfg.dwnotify_n = &dadwnot;
175 global_deviceatlassch.o.rfn = jsonread;
176 global_deviceatlassch.o.posfn = jsonseek;
177
178 if (dadwinit() != DA_OK) {
179 fprintf(stderr, "%s init failed\n", __pgname);
180 exit(EXIT_FAILURE);
181 }
182
183 if (da_atlas_open_schedule(&global_deviceatlassch.o) != DA_OK) {
184 fprintf(stderr, "%s scheduling failed\n", __pgname);
185 exit(EXIT_FAILURE);
186 }
187
188 sigaction(SIGINT, &sa, NULL);
189 sigaction(SIGQUIT, &sa, NULL);
190 sigaction(SIGTERM, &sa, NULL);
191
192 while (true) sleep(1);
193
194 return 0;
195}