1 #if defined(HAVE_CONFIG_H)
10 #include <sys/socket.h>
12 #include <sys/types.h>
14 #include <talloc/talloc.h>
15 #include <system/system.h>
16 #include <process/process.h>
19 #include "i18n/i18n.h"
22 #include "device-handler.h"
25 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
28 struct list pending_network_jobs;
31 struct load_task *task;
34 struct list_item list;
39 struct process *process;
40 struct load_url_result *result;
42 load_url_complete async_cb;
46 const char *mount_base(void)
48 return DEVICE_MOUNT_BASE;
51 char *join_paths(void *alloc_ctx, const char *a, const char *b)
55 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
58 if (b[0] != '/' && a[strlen(a) - 1] != '/')
59 strcat(full_path, "/");
65 #ifndef PETITBOOT_TEST
68 static inline bool have_busybox(void) { return true; }
70 static inline bool have_busybox(void) { return false; }
73 static char *local_name(void *ctx)
75 char *ret, tmp[] = "/tmp/pb-XXXXXX";
79 oldmask = umask(0644);
88 ret = talloc_strdup(ctx, tmp);
93 static void load_url_result_cleanup_local(struct load_url_result *result)
95 if (result->cleanup_local)
96 unlink(result->local);
99 static void load_url_process_exit(struct process *process)
101 struct load_task *task = process->data;
102 struct load_url_result *result;
103 load_url_complete cb;
106 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
107 process->path, process->pid, task->url->full,
108 process->exit_status);
110 result = task->result;
111 data = task->async_data;
114 if (result->status == LOAD_CANCELLED) {
115 load_url_result_cleanup_local(result);
116 } else if (process_exit_ok(process)) {
117 result->status = LOAD_OK;
119 result->status = LOAD_ERROR;
120 load_url_result_cleanup_local(result);
121 pb_debug("Download client stdout buffer:\n%s\n",
122 process->stdout_buf);
125 if (result->status == LOAD_OK && process->stdout_data)
126 device_handler_status_info(process->stdout_data,
127 _("Download complete: %s"), task->url->file);
129 /* The load callback may well free the ctx, which was the
130 * talloc parent of the task. Therefore, we want to do our cleanup
133 process_release(process);
141 * Callback to retrieve progress information from Busybox utilities.
142 * Busybox utilities use a common progress bar format which progress percentage
143 * and current size can be can be parsed from.
145 static int busybox_progress_cb(void *arg)
147 const char *busybox_fmt = "%*s %u%*[%* |]%u%c %*u:%*u:%*u ETA\n";
148 struct process_info *procinfo = arg;
149 char *n, *s, suffix, *line = NULL;
150 struct device_handler *handler;
151 unsigned int percentage, size;
158 p = procinfo_get_process(procinfo);
159 handler = p->stdout_data;
161 rc = process_stdout_custom(procinfo, &line);
164 /* Unregister ourselves from progress tracking */
165 device_handler_status_download_remove(handler, procinfo);
171 rc = sscanf(line, busybox_fmt, &percentage, &size, &suffix);
174 * Many unrecognised lines are partial updates. If we see a partial
175 * line with a newline character, see if we can match a valid line
176 * at the end of stdout_buf
179 n = strchr(line, '\n');
181 for (s = n - 1; s >= p->stdout_buf; s--)
183 rc = sscanf(s + 1, busybox_fmt,
184 &percentage, &size, &suffix);
190 percentage = size = 0;
192 device_handler_status_download(handler, procinfo,
193 percentage, size, suffix);
200 static void load_process_to_local_file(struct load_task *task,
201 const char **argv, int argv_local_idx)
205 task->result->local = local_name(task->result);
206 if (!task->result->local) {
207 task->result->status = LOAD_ERROR;
210 task->result->cleanup_local = true;
213 argv[argv_local_idx] = task->result->local;
215 task->process->argv = argv;
216 task->process->path = argv[0];
219 rc = process_run_async(task->process);
221 process_release(task->process);
222 task->process = NULL;
224 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
226 rc = process_run_sync(task->process);
227 if (rc || !process_exit_ok(task->process))
228 task->result->status = LOAD_ERROR;
230 task->result->status = LOAD_OK;
231 process_release(task->process);
232 task->process = NULL;
237 * pb_load_nfs - Create a mountpoint, set the local file within that
238 * mountpoint, and run the appropriate mount command
241 static void load_nfs(struct load_task *task)
243 char *mountpoint, *opts;
245 const char *argv[] = {
246 pb_system_apps.mount,
251 NULL, /* 6: mountpoint */
255 task->result->status = LOAD_ERROR;
256 mountpoint = local_name(task->result);
259 task->result->cleanup_local = true;
260 argv[6] = mountpoint;
262 rc = pb_mkdir_recursive(mountpoint);
266 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
270 opts = talloc_asprintf_append(opts, ",port=%s",
273 task->result->local = talloc_asprintf(task->result, "%s/%s",
277 task->process->path = pb_system_apps.mount;
278 task->process->argv = argv;
281 rc = process_run_async(task->process);
283 process_release(task->process);
284 task->process = NULL;
286 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
288 rc = process_run_sync(task->process);
289 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
290 process_release(task->process);
291 task->process = NULL;
297 static void load_sftp(struct load_task *task)
299 const char *argv[] = {
301 NULL, /* 1: host:path */
302 NULL, /* 2: local file */
306 argv[1] = talloc_asprintf(task, "%s:%s",
307 task->url->host, task->url->path);
308 load_process_to_local_file(task, argv, 2);
311 static enum tftp_type check_tftp_type(void *ctx)
313 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
314 struct process *process;
318 process = process_create(ctx);
319 process->path = pb_system_apps.tftp;
320 process->argv = argv;
321 process->keep_stdout = true;
322 process->add_stderr = true;
323 rc = process_run_sync(process);
325 if (rc || !process->stdout_buf || process->stdout_len == 0) {
326 pb_log("Can't check TFTP client type!\n");
327 type = TFTP_TYPE_BROKEN;
329 } else if (memmem(process->stdout_buf, process->stdout_len,
330 "tftp-hpa", strlen("tftp-hpa"))) {
331 pb_debug("Found TFTP client type: tftp-hpa\n");
332 type = TFTP_TYPE_HPA;
334 } else if (memmem(process->stdout_buf, process->stdout_len,
335 "BusyBox", strlen("BusyBox"))) {
336 pb_debug("Found TFTP client type: BusyBox tftp\n");
337 type = TFTP_TYPE_BUSYBOX;
340 pb_log("Unknown TFTP client type!\n");
341 type = TFTP_TYPE_BROKEN;
344 process_release(process);
348 static void load_tftp(struct load_task *task)
350 const char *port = "69";
351 const char *argv[10] = {
356 port = task->url->port;
358 if (tftp_type == TFTP_TYPE_UNKNOWN)
359 tftp_type = check_tftp_type(task);
361 if (tftp_type == TFTP_TYPE_BUSYBOX) {
364 argv[3] = NULL; /* 3: local file */
366 argv[5] = task->url->path;
367 argv[6] = task->url->host;
371 load_process_to_local_file(task, argv, 3);
373 } else if (tftp_type == TFTP_TYPE_HPA) {
376 argv[3] = task->url->host;
380 argv[7] = task->url->path;
381 argv[8] = NULL; /* 8: local file */
383 load_process_to_local_file(task, argv, 8);
386 task->result->status = LOAD_ERROR;
391 wget_no_check_certificate = 0x2,
396 * pb_load_wget - Loads a remote file via wget and returns the local file path.
398 * Returns the local file path in a talloc'ed character string on success,
402 static void load_wget(struct load_task *task, int flags)
404 const char *argv[] = {
407 NULL, /* 2: local file */
408 NULL, /* 3 (optional): --quiet */
409 NULL, /* 4 (optional): --no-check-certificate */
415 if (task->process->stdout_cb)
416 flags |= wget_verbose;
420 flags |= wget_verbose;
422 if ((flags & wget_verbose) == 0)
423 argv[i++] = "--quiet";
425 if (flags & wget_no_check_certificate)
426 argv[i++] = "--no-check-certificate";
428 argv[i] = task->url->full;
430 load_process_to_local_file(task, argv, 2);
433 /* Although we don't need to load anything for a local path (we just return
434 * the path from the file:// URL), the other load helpers will error-out on
435 * non-existant files. So, do the same here with an access() check on the local
438 static void load_local(struct load_task *task)
440 struct load_url_result *result = task->result;
443 rc = access(task->url->path, F_OK);
445 result->status = LOAD_ERROR;
447 result->local = talloc_strdup(task->result, task->url->path);
448 result->status = LOAD_OK;
451 task->async_cb(task->result, task->async_data);
454 static void load_url_async_start_pending(struct load_task *task, int flags)
456 pb_log("Starting pending job for %s\n", task->url->full);
458 switch (task->url->scheme) {
461 load_wget(task, flags);
464 flags |= wget_no_check_certificate;
465 load_wget(task, flags);
477 /* Shouldn't be a need via this path but.. */
482 if (task->result->status == LOAD_ERROR) {
483 pb_log("Pending job failed for %s\n", task->url->full);
484 load_url_result_cleanup_local(task->result);
485 talloc_free(task->result);
490 void pending_network_jobs_start(void)
492 struct network_job *job, *tmp;
494 if (!pending_network_jobs.head.next)
497 list_for_each_entry_safe(&pending_network_jobs, job, tmp, list) {
498 load_url_async_start_pending(job->task, job->flags);
499 list_remove(&job->list);
503 void pending_network_jobs_cancel(void)
505 struct network_job *job, *tmp;
507 if (!pending_network_jobs.head.next)
510 list_for_each_entry_safe(&pending_network_jobs, job, tmp, list)
512 list_init(&pending_network_jobs);
515 static void pending_network_jobs_add(struct load_task *task, int flags)
517 struct network_job *job;
519 if (!pending_network_jobs.head.next)
520 list_init(&pending_network_jobs);
522 job = talloc(task, struct network_job);
524 pb_log("Failed to allocate space for pending job\n");
530 list_add_tail(&pending_network_jobs, &job->list);
535 * load_url - Loads a (possibly) remote URL and returns the local file
537 * @ctx: The talloc context to associate with the returned string.
538 * @url: The remote file URL.
539 * @tempfile: An optional variable pointer to be set when a temporary local
541 * @url_cb: An optional callback pointer if the caller wants to load url
544 * Returns the local file path in a talloc'ed character string on success,
548 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
549 load_url_complete async_cb, void *async_data,
550 waiter_cb stdout_cb, void *stdout_data)
552 struct load_url_result *result;
553 struct load_task *task;
554 struct addrinfo *res;
560 task = talloc_zero(ctx, struct load_task);
562 task->async = async_cb != NULL;
563 task->result = talloc_zero(ctx, struct load_url_result);
564 task->result->task = task;
565 task->result->url = url;
566 task->process = process_create(task);
568 task->async_cb = async_cb;
569 task->async_data = async_data;
570 task->process->exit_cb = load_url_process_exit;
571 task->process->data = task;
572 task->process->stdout_cb = stdout_cb;
573 task->process->stdout_data = stdout_data;
576 if (!stdout_cb && stdout_data && have_busybox())
577 task->process->stdout_cb = busybox_progress_cb;
579 /* Make sure we save output for any task that has a custom handler */
580 if (task->process->stdout_cb) {
581 task->process->add_stderr = true;
582 task->process->keep_stdout = true;
585 /* If the url is remote but network is not yet available queue up this
587 if (url->scheme != pb_url_file &&
588 getaddrinfo(url->host, NULL, NULL, &res) != 0) {
589 pb_log("load task for %s queued pending network\n", url->full);
590 pending_network_jobs_add(task, flags);
591 task->result->status = LOAD_ASYNC;
595 switch (url->scheme) {
598 load_wget(task, flags);
601 flags |= wget_no_check_certificate;
602 load_wget(task, flags);
618 result = task->result;
619 if (result->status == LOAD_ERROR) {
620 load_url_result_cleanup_local(task->result);
626 if (!task->async || result->status == LOAD_OK)
632 struct load_url_result *load_url(void *ctx, struct pb_url *url)
634 return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
637 void load_url_async_cancel(struct load_url_result *res)
639 struct load_task *task = res->task;
641 /* the completion callback may have already been called; this clears
646 if (res->status == LOAD_CANCELLED)
650 assert(task->process);
652 res->status = LOAD_CANCELLED;
653 process_stop_async(task->process);
658 static void __attribute__((unused)) load_local(
659 struct load_task *task __attribute__((unused)))
662 static void __attribute__((unused)) load_wget(
663 struct load_task *task __attribute__((unused)),
664 int flags __attribute__((unused)))
667 static void __attribute__((unused)) load_tftp(
668 struct load_task *task __attribute__((unused)))
671 static void __attribute__((unused)) load_sftp(
672 struct load_task *task __attribute__((unused)))
675 static void __attribute__((unused)) load_nfs(
676 struct load_task *task __attribute__((unused)))
679 static void __attribute__((unused)) load_url_process_exit(
680 struct process *process __attribute__((unused)))