forked from sbabic/swupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress_thread.c
287 lines (259 loc) · 7.05 KB
/
progress_thread.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
/*
* (C) Copyright 2016
* Stefano Babic, DENX Software Engineering, [email protected].
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include "swupdate.h"
#include <handler.h>
#include "util.h"
#include "pctl.h"
#include "network_ipc.h"
#include "network_interface.h"
#include <progress.h>
#include "generated/autoconf.h"
#ifdef CONFIG_SYSTEMD
#include <systemd/sd-daemon.h>
#endif
struct progress_conn {
SIMPLEQ_ENTRY(progress_conn) next;
int sockfd;
};
SIMPLEQ_HEAD(connections, progress_conn);
/*
* Structure contains data regarding
* current installation
*/
struct swupdate_progress {
struct progress_msg msg;
char *current_image;
const handler *curhnd;
struct connections conns;
pthread_mutex_t lock;
bool step_running;
};
static struct swupdate_progress progress;
/*
* This must be called after acquiring the mutex
* for the progress structure
*/
static void send_progress_msg(void)
{
struct progress_conn *conn, *tmp;
struct swupdate_progress *pprog = &progress;
void *buf;
size_t count;
ssize_t n;
SIMPLEQ_FOREACH_SAFE(conn, &pprog->conns, next, tmp) {
buf = &pprog->msg;
count = sizeof(pprog->msg);
while (count > 0) {
n = send(conn->sockfd, buf, count, MSG_NOSIGNAL);
if (n <= 0) {
TRACE("A progress client disappeared, removing it.");
close(conn->sockfd);
SIMPLEQ_REMOVE(&pprog->conns, conn,
progress_conn, next);
free(conn);
break;
}
count -= (size_t)n;
buf = (char*)buf + n;
}
}
}
static void _swupdate_download_update(unsigned int perc, unsigned long long totalbytes)
{
/*
* TODO: totalbytes should be forwarded correctly
* after adding it to the progress message
*/
struct swupdate_progress *pprog = &progress;
pthread_mutex_lock(&pprog->lock);
if (perc != pprog->msg.dwl_percent) {
pprog->msg.status = DOWNLOAD;
pprog->msg.dwl_percent = perc;
pprog->msg.dwl_bytes = totalbytes;
send_progress_msg();
}
pthread_mutex_unlock(&pprog->lock);
}
void swupdate_progress_init(unsigned int nsteps) {
struct swupdate_progress *pprog = &progress;
pthread_mutex_lock(&pprog->lock);
pprog->msg.nsteps = nsteps;
pprog->msg.cur_step = 0;
pprog->msg.status = START;
pprog->msg.cur_percent = 0;
pprog->msg.infolen = get_install_info(&pprog->msg.source, pprog->msg.info,
sizeof(pprog->msg.info));
send_progress_msg();
/* Info is just an event, reset it after sending */
pprog->msg.infolen = 0;
pthread_mutex_unlock(&pprog->lock);
}
void swupdate_progress_update(unsigned int perc)
{
struct swupdate_progress *pprog = &progress;
pthread_mutex_lock(&pprog->lock);
if (perc != pprog->msg.cur_percent && pprog->step_running) {
pprog->msg.status = PROGRESS;
pprog->msg.cur_percent = perc;
send_progress_msg();
}
pthread_mutex_unlock(&pprog->lock);
}
void swupdate_download_update(unsigned int perc, unsigned long long totalbytes)
{
char info[PRINFOSIZE]; /* info */
/*
* Not called by main process, for example by suricatta or Webserver
*/
if (pid == getpid()) {
/*
* Notify can just receive a string as message
* so it is necessary to encode further information as string
* and decode them in the notifier, in this case
* the progress_notifier
*/
snprintf(info, sizeof(info) - 1, "%d-%llu", perc, totalbytes);
notify(PROGRESS, RECOVERY_DWL, TRACELEVEL, info);
return;
}
/* Called by main process, emit a progress message */
_swupdate_download_update(perc, totalbytes);
}
void swupdate_progress_inc_step(char *image, char *handler_name)
{
struct swupdate_progress *pprog = &progress;
pthread_mutex_lock(&pprog->lock);
pprog->msg.cur_step++;
pprog->msg.cur_percent = 0;
strlcpy(pprog->msg.cur_image, image, sizeof(pprog->msg.cur_image));
strlcpy(pprog->msg.hnd_name, handler_name, sizeof(pprog->msg.hnd_name));
pprog->step_running = true;
pprog->msg.status = RUN;
send_progress_msg();
pthread_mutex_unlock(&pprog->lock);
}
void swupdate_progress_step_completed(void)
{
struct swupdate_progress *pprog = &progress;
pthread_mutex_lock(&pprog->lock);
pprog->step_running = false;
pprog->msg.status = IDLE;
pthread_mutex_unlock(&pprog->lock);
}
void swupdate_progress_end(RECOVERY_STATUS status)
{
struct swupdate_progress *pprog = &progress;
pthread_mutex_lock(&pprog->lock);
pprog->step_running = false;
pprog->msg.status = status;
send_progress_msg();
pprog->msg.nsteps = 0;
pprog->msg.cur_step = 0;
pprog->msg.cur_percent = 0;
pprog->msg.dwl_percent = 0;
pprog->msg.dwl_bytes = 0;
pthread_mutex_unlock(&pprog->lock);
}
void swupdate_progress_info(RECOVERY_STATUS status, int cause, const char *info)
{
struct swupdate_progress *pprog = &progress;
pthread_mutex_lock(&pprog->lock);
snprintf(pprog->msg.info, sizeof(pprog->msg.info), "{\"%d\": %s}",
cause, info);
pprog->msg.infolen = strlen(pprog->msg.info);
pprog->msg.status = status;
send_progress_msg();
/* Info is just an event, reset it after sending */
pprog->msg.infolen = 0;
pthread_mutex_unlock(&pprog->lock);
}
void swupdate_progress_done(const char *info)
{
struct swupdate_progress *pprog = &progress;
pthread_mutex_lock(&pprog->lock);
if (info != NULL) {
snprintf(pprog->msg.info, sizeof(pprog->msg.info), "%s", info);
pprog->msg.infolen = strlen(pprog->msg.info);
}
pprog->step_running = false;
pprog->msg.status = DONE;
send_progress_msg();
pprog->msg.infolen = 0;
pthread_mutex_unlock(&pprog->lock);
}
static void unlink_socket(void)
{
#ifdef CONFIG_SYSTEMD
if (sd_booted() && sd_listen_fds(0) > 0) {
/*
* There were socket fds handed-over by systemd,
* so don't delete the socket file.
*/
return;
}
#endif
unlink(get_prog_socket());
}
void *progress_bar_thread (void __attribute__ ((__unused__)) *data)
{
int listen, connfd;
socklen_t clilen;
struct sockaddr_un cliaddr;
struct swupdate_progress *pprog = &progress;
struct progress_conn *conn;
pthread_mutex_init(&pprog->lock, NULL);
SIMPLEQ_INIT(&pprog->conns);
/* Initialize and bind to UDS */
listen = listener_create(get_prog_socket(), SOCK_STREAM);
if (listen < 0 ) {
ERROR("Error creating IPC socket %s, exiting.", get_prog_socket());
exit(2);
}
if (atexit(unlink_socket) != 0) {
TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.",
get_prog_socket());
}
do {
clilen = sizeof(cliaddr);
if ( (connfd = accept(listen, (struct sockaddr *) &cliaddr, &clilen)) < 0) {
if (errno == EINTR)
continue;
else {
TRACE("Accept returns: %s", strerror(errno));
continue;
}
}
/*
* Save the new connection to be handled by the progress thread
*/
conn = (struct progress_conn *)calloc(1, sizeof(*conn));
if (!conn) {
ERROR("Out of memory, skipping...");
continue;
}
conn->sockfd = connfd;
pthread_mutex_lock(&pprog->lock);
SIMPLEQ_INSERT_TAIL(&pprog->conns, conn, next);
pthread_mutex_unlock(&pprog->lock);
} while(1);
}