1 #if defined(HAVE_CONFIG_H)
12 #include <talloc/talloc.h>
13 #include <system/system.h>
14 #include <process/process.h>
17 #include "i18n/i18n.h"
20 #include "device-handler.h"
23 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
26 struct list pending_network_jobs;
29 struct load_task *task;
32 struct list_item list;
37 struct process *process;
38 struct load_url_result *result;
40 load_url_complete async_cb;
44 const char *mount_base(void)
46 return DEVICE_MOUNT_BASE;
49 char *join_paths(void *alloc_ctx, const char *a, const char *b)
53 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
56 if (b[0] != '/' && a[strlen(a) - 1] != '/')
57 strcat(full_path, "/");
63 #ifndef PETITBOOT_TEST
66 static inline bool have_busybox(void) { return true; }
68 static inline bool have_busybox(void) { return false; }
71 static char *local_name(void *ctx)
73 char *ret, tmp[] = "/tmp/pb-XXXXXX";
77 oldmask = umask(0644);
86 ret = talloc_strdup(ctx, tmp);
91 static void load_url_result_cleanup_local(struct load_url_result *result)
93 if (result->cleanup_local)
94 unlink(result->local);
97 static void load_url_process_exit(struct process *process)
99 struct load_task *task = process->data;
100 struct load_url_result *result;
101 load_url_complete cb;
104 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
105 process->path, process->pid, task->url->full,
106 process->exit_status);
108 result = task->result;
109 data = task->async_data;
112 if (result->status == LOAD_CANCELLED) {
113 load_url_result_cleanup_local(result);
114 } else if (process_exit_ok(process)) {
115 result->status = LOAD_OK;
117 result->status = LOAD_ERROR;
118 load_url_result_cleanup_local(result);
121 if (result->status == LOAD_OK && process->stdout_data)
122 device_handler_status_info(process->stdout_data,
123 _("Download complete: %s"), task->url->file);
125 /* The load callback may well free the ctx, which was the
126 * talloc parent of the task. Therefore, we want to do our cleanup
129 process_release(process);
137 * Callback to retrieve progress information from Busybox utilities.
138 * Busybox utilities use a common progress bar format which progress percentage
139 * and current size can be can be parsed from.
141 static int busybox_progress_cb(void *arg)
143 const char *busybox_fmt = "%*s %u%*[%* |]%u%c %*u:%*u:%*u ETA\n";
144 struct process_info *procinfo = arg;
145 char *n, *s, suffix, *line = NULL;
146 struct device_handler *handler;
147 unsigned int percentage, size;
154 p = procinfo_get_process(procinfo);
155 handler = p->stdout_data;
157 rc = process_stdout_custom(procinfo, &line);
160 /* Unregister ourselves from progress tracking */
161 device_handler_status_download_remove(handler, procinfo);
167 rc = sscanf(line, busybox_fmt, &percentage, &size, &suffix);
170 * Many unrecognised lines are partial updates. If we see a partial
171 * line with a newline character, see if we can match a valid line
172 * at the end of stdout_buf
175 n = strchr(line, '\n');
177 for (s = n - 1; s >= p->stdout_buf; s--)
179 rc = sscanf(s + 1, busybox_fmt,
180 &percentage, &size, &suffix);
186 percentage = size = 0;
188 device_handler_status_download(handler, procinfo,
189 percentage, size, suffix);
196 static void load_process_to_local_file(struct load_task *task,
197 const char **argv, int argv_local_idx)
201 task->result->local = local_name(task->result);
202 if (!task->result->local) {
203 task->result->status = LOAD_ERROR;
206 task->result->cleanup_local = true;
209 argv[argv_local_idx] = task->result->local;
211 task->process->argv = argv;
212 task->process->path = argv[0];
215 rc = process_run_async(task->process);
217 process_release(task->process);
218 task->process = NULL;
220 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
222 rc = process_run_sync(task->process);
223 if (rc || !process_exit_ok(task->process))
224 task->result->status = LOAD_ERROR;
226 task->result->status = LOAD_OK;
227 process_release(task->process);
228 task->process = NULL;
233 * pb_load_nfs - Create a mountpoint, set the local file within that
234 * mountpoint, and run the appropriate mount command
237 static void load_nfs(struct load_task *task)
239 char *mountpoint, *opts;
241 const char *argv[] = {
242 pb_system_apps.mount,
247 NULL, /* 6: mountpoint */
251 task->result->status = LOAD_ERROR;
252 mountpoint = local_name(task->result);
255 task->result->cleanup_local = true;
256 argv[6] = mountpoint;
258 rc = pb_mkdir_recursive(mountpoint);
262 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
266 opts = talloc_asprintf_append(opts, ",port=%s",
269 task->result->local = talloc_asprintf(task->result, "%s/%s",
273 task->process->path = pb_system_apps.mount;
274 task->process->argv = argv;
277 rc = process_run_async(task->process);
279 process_release(task->process);
280 task->process = NULL;
282 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
284 rc = process_run_sync(task->process);
285 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
286 process_release(task->process);
287 task->process = NULL;
293 static void load_sftp(struct load_task *task)
295 const char *argv[] = {
297 NULL, /* 1: host:path */
298 NULL, /* 2: local file */
302 argv[1] = talloc_asprintf(task, "%s:%s",
303 task->url->host, task->url->path);
304 load_process_to_local_file(task, argv, 2);
307 static enum tftp_type check_tftp_type(void *ctx)
309 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
310 struct process *process;
314 process = process_create(ctx);
315 process->path = pb_system_apps.tftp;
316 process->argv = argv;
317 process->keep_stdout = true;
318 process->add_stderr = true;
319 rc = process_run_sync(process);
321 if (rc || !process->stdout_buf || process->stdout_len == 0) {
322 pb_log("Can't check TFTP client type!\n");
323 type = TFTP_TYPE_BROKEN;
325 } else if (memmem(process->stdout_buf, process->stdout_len,
326 "tftp-hpa", strlen("tftp-hpa"))) {
327 pb_debug("Found TFTP client type: tftp-hpa\n");
328 type = TFTP_TYPE_HPA;
330 } else if (memmem(process->stdout_buf, process->stdout_len,
331 "BusyBox", strlen("BusyBox"))) {
332 pb_debug("Found TFTP client type: BusyBox tftp\n");
333 type = TFTP_TYPE_BUSYBOX;
336 pb_log("Unknown TFTP client type!\n");
337 type = TFTP_TYPE_BROKEN;
340 process_release(process);
344 static void load_tftp(struct load_task *task)
346 const char *port = "69";
347 const char *argv[10] = {
352 port = task->url->port;
354 if (tftp_type == TFTP_TYPE_UNKNOWN)
355 tftp_type = check_tftp_type(task);
357 if (tftp_type == TFTP_TYPE_BUSYBOX) {
360 argv[3] = NULL; /* 3: local file */
362 argv[5] = task->url->path;
363 argv[6] = task->url->host;
367 load_process_to_local_file(task, argv, 3);
369 } else if (tftp_type == TFTP_TYPE_HPA) {
372 argv[3] = task->url->host;
376 argv[7] = task->url->path;
377 argv[8] = NULL; /* 8: local file */
379 load_process_to_local_file(task, argv, 8);
382 task->result->status = LOAD_ERROR;
387 wget_no_check_certificate = 0x2,
392 * pb_load_wget - Loads a remote file via wget and returns the local file path.
394 * Returns the local file path in a talloc'ed character string on success,
398 static void load_wget(struct load_task *task, int flags)
400 const char *argv[] = {
403 NULL, /* 2: local file */
404 NULL, /* 3 (optional): --quiet */
405 NULL, /* 4 (optional): --no-check-certificate */
411 if (task->process->stdout_cb)
412 flags |= wget_verbose;
416 flags |= wget_verbose;
418 if ((flags & wget_verbose) == 0)
419 argv[i++] = "--quiet";
421 if (flags & wget_no_check_certificate)
422 argv[i++] = "--no-check-certificate";
424 argv[i] = task->url->full;
426 load_process_to_local_file(task, argv, 2);
429 /* Although we don't need to load anything for a local path (we just return
430 * the path from the file:// URL), the other load helpers will error-out on
431 * non-existant files. So, do the same here with an access() check on the local
434 static void load_local(struct load_task *task)
436 struct load_url_result *result = task->result;
439 rc = access(task->url->path, F_OK);
441 result->status = LOAD_ERROR;
443 result->local = talloc_strdup(task->result, task->url->path);
444 result->status = LOAD_OK;
447 task->async_cb(task->result, task->async_data);
450 static void load_url_async_start_pending(struct load_task *task, int flags)
452 pb_log("Starting pending job for %s\n", task->url->full);
454 switch (task->url->scheme) {
457 load_wget(task, flags);
460 flags |= wget_no_check_certificate;
461 load_wget(task, flags);
473 /* Shouldn't be a need via this path but.. */
478 if (task->result->status == LOAD_ERROR) {
479 pb_log("Pending job failed for %s\n", task->url->full);
480 load_url_result_cleanup_local(task->result);
481 talloc_free(task->result);
486 void pending_network_jobs_start(void)
488 struct network_job *job, *tmp;
490 if (!pending_network_jobs.head.next)
493 list_for_each_entry_safe(&pending_network_jobs, job, tmp, list) {
494 load_url_async_start_pending(job->task, job->flags);
495 list_remove(&job->list);
499 void pending_network_jobs_cancel(void)
501 struct network_job *job, *tmp;
503 if (!pending_network_jobs.head.next)
506 list_for_each_entry_safe(&pending_network_jobs, job, tmp, list)
508 list_init(&pending_network_jobs);
511 static void pending_network_jobs_add(struct load_task *task, int flags)
513 struct network_job *job;
515 if (!pending_network_jobs.head.next)
516 list_init(&pending_network_jobs);
518 job = talloc(task, struct network_job);
520 pb_log("Failed to allocate space for pending job\n");
526 list_add_tail(&pending_network_jobs, &job->list);
531 * load_url - Loads a (possibly) remote URL and returns the local file
533 * @ctx: The talloc context to associate with the returned string.
534 * @url: The remote file URL.
535 * @tempfile: An optional variable pointer to be set when a temporary local
537 * @url_cb: An optional callback pointer if the caller wants to load url
540 * Returns the local file path in a talloc'ed character string on success,
544 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
545 load_url_complete async_cb, void *async_data,
546 waiter_cb stdout_cb, void *stdout_data)
548 struct load_url_result *result;
549 struct load_task *task;
555 task = talloc_zero(ctx, struct load_task);
557 task->async = async_cb != NULL;
558 task->result = talloc_zero(ctx, struct load_url_result);
559 task->result->task = task;
560 task->result->url = url;
561 task->process = process_create(task);
563 task->async_cb = async_cb;
564 task->async_data = async_data;
565 task->process->exit_cb = load_url_process_exit;
566 task->process->data = task;
567 task->process->stdout_cb = stdout_cb;
568 task->process->stdout_data = stdout_data;
571 if (!stdout_cb && stdout_data && have_busybox())
572 task->process->stdout_cb = busybox_progress_cb;
574 /* Make sure we save output for any task that has a custom handler */
575 if (task->process->stdout_cb) {
576 task->process->add_stderr = true;
577 task->process->keep_stdout = true;
580 /* If the url is remote but network is not yet available queue up this
582 if (!system_info_network_available() && url->scheme != pb_url_file) {
583 pb_log("load task for %s queued pending network\n", url->full);
584 pending_network_jobs_add(task, flags);
585 task->result->status = LOAD_ASYNC;
589 switch (url->scheme) {
592 load_wget(task, flags);
595 flags |= wget_no_check_certificate;
596 load_wget(task, flags);
612 result = task->result;
613 if (result->status == LOAD_ERROR) {
614 load_url_result_cleanup_local(task->result);
620 if (!task->async || result->status == LOAD_OK)
626 struct load_url_result *load_url(void *ctx, struct pb_url *url)
628 return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
631 void load_url_async_cancel(struct load_url_result *res)
633 struct load_task *task = res->task;
635 /* the completion callback may have already been called; this clears
640 if (res->status == LOAD_CANCELLED)
644 assert(task->process);
646 res->status = LOAD_CANCELLED;
647 process_stop_async(task->process);
652 static void __attribute__((unused)) load_local(
653 struct load_task *task __attribute__((unused)))
656 static void __attribute__((unused)) load_wget(
657 struct load_task *task __attribute__((unused)),
658 int flags __attribute__((unused)))
661 static void __attribute__((unused)) load_tftp(
662 struct load_task *task __attribute__((unused)))
665 static void __attribute__((unused)) load_sftp(
666 struct load_task *task __attribute__((unused)))
669 static void __attribute__((unused)) load_nfs(
670 struct load_task *task __attribute__((unused)))
673 static void __attribute__((unused)) load_url_process_exit(
674 struct process *process __attribute__((unused)))