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 static inline bool have_busybox(void)
53 const char *mount_base(void)
55 return DEVICE_MOUNT_BASE;
58 char *join_paths(void *alloc_ctx, const char *a, const char *b)
62 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
65 if (b[0] != '/' && a[strlen(a) - 1] != '/')
66 strcat(full_path, "/");
72 #ifndef PETITBOOT_TEST
74 static char *local_name(void *ctx)
76 char *ret, tmp[] = "/tmp/pb-XXXXXX";
80 oldmask = umask(0644);
89 ret = talloc_strdup(ctx, tmp);
94 static void load_url_result_cleanup_local(struct load_url_result *result)
96 if (result->cleanup_local)
97 unlink(result->local);
100 static void load_url_process_exit(struct process *process)
102 struct load_task *task = process->data;
103 struct load_url_result *result;
104 load_url_complete cb;
107 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
108 process->path, process->pid, task->url->full,
109 process->exit_status);
111 result = task->result;
112 data = task->async_data;
115 if (result->status == LOAD_CANCELLED) {
116 load_url_result_cleanup_local(result);
117 } else if (process_exit_ok(process)) {
118 result->status = LOAD_OK;
120 result->status = LOAD_ERROR;
121 load_url_result_cleanup_local(result);
124 if (result->status == LOAD_OK && process->stdout_data)
125 device_handler_status_info(process->stdout_data,
126 _("Download complete: %s"), task->url->file);
128 /* The load callback may well free the ctx, which was the
129 * talloc parent of the task. Therefore, we want to do our cleanup
132 process_release(process);
140 * Callback to retrieve progress information from Busybox utilities.
141 * Busybox utilities use a common progress bar format which progress percentage
142 * and current size can be can be parsed from.
144 static int busybox_progress_cb(void *arg)
146 const char *busybox_fmt = "%*s %u%*[%* |]%u%c %*u:%*u:%*u ETA\n";
147 struct process_info *procinfo = arg;
148 char *n, *s, suffix, *line = NULL;
149 struct device_handler *handler;
150 unsigned int percentage, size;
157 p = procinfo_get_process(procinfo);
158 handler = p->stdout_data;
160 rc = process_stdout_custom(procinfo, &line);
163 /* Unregister ourselves from progress tracking */
164 device_handler_status_download_remove(handler, procinfo);
170 rc = sscanf(line, busybox_fmt, &percentage, &size, &suffix);
173 * Many unrecognised lines are partial updates. If we see a partial
174 * line with a newline character, see if we can match a valid line
175 * at the end of stdout_buf
178 n = strchr(line, '\n');
180 for (s = n - 1; s >= p->stdout_buf; s--)
182 rc = sscanf(s + 1, busybox_fmt,
183 &percentage, &size, &suffix);
189 percentage = size = 0;
191 device_handler_status_download(handler, procinfo,
192 percentage, size, suffix);
199 static void load_process_to_local_file(struct load_task *task,
200 const char **argv, int argv_local_idx)
204 task->result->local = local_name(task->result);
205 if (!task->result->local) {
206 task->result->status = LOAD_ERROR;
209 task->result->cleanup_local = true;
212 argv[argv_local_idx] = task->result->local;
214 task->process->argv = argv;
215 task->process->path = argv[0];
218 rc = process_run_async(task->process);
220 process_release(task->process);
221 task->process = NULL;
223 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
225 rc = process_run_sync(task->process);
226 if (rc || !process_exit_ok(task->process))
227 task->result->status = LOAD_ERROR;
229 task->result->status = LOAD_OK;
230 process_release(task->process);
231 task->process = NULL;
236 * pb_load_nfs - Create a mountpoint, set the local file within that
237 * mountpoint, and run the appropriate mount command
240 static void load_nfs(struct load_task *task)
242 char *mountpoint, *opts;
244 const char *argv[] = {
245 pb_system_apps.mount,
250 NULL, /* 6: mountpoint */
254 task->result->status = LOAD_ERROR;
255 mountpoint = local_name(task->result);
258 task->result->cleanup_local = true;
259 argv[6] = mountpoint;
261 rc = pb_mkdir_recursive(mountpoint);
265 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
269 opts = talloc_asprintf_append(opts, ",port=%s",
272 task->result->local = talloc_asprintf(task->result, "%s/%s",
276 task->process->path = pb_system_apps.mount;
277 task->process->argv = argv;
280 rc = process_run_async(task->process);
282 process_release(task->process);
283 task->process = NULL;
285 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
287 rc = process_run_sync(task->process);
288 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
289 process_release(task->process);
290 task->process = NULL;
296 static void load_sftp(struct load_task *task)
298 const char *argv[] = {
300 NULL, /* 1: host:path */
301 NULL, /* 2: local file */
305 argv[1] = talloc_asprintf(task, "%s:%s",
306 task->url->host, task->url->path);
307 load_process_to_local_file(task, argv, 2);
310 static enum tftp_type check_tftp_type(void *ctx)
312 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
313 struct process *process;
317 process = process_create(ctx);
318 process->path = pb_system_apps.tftp;
319 process->argv = argv;
320 process->keep_stdout = true;
321 process->add_stderr = true;
322 rc = process_run_sync(process);
324 if (rc || !process->stdout_buf || process->stdout_len == 0) {
325 pb_log("Can't check TFTP client type!\n");
326 type = TFTP_TYPE_BROKEN;
328 } else if (memmem(process->stdout_buf, process->stdout_len,
329 "tftp-hpa", strlen("tftp-hpa"))) {
330 pb_debug("Found TFTP client type: tftp-hpa\n");
331 type = TFTP_TYPE_HPA;
333 } else if (memmem(process->stdout_buf, process->stdout_len,
334 "BusyBox", strlen("BusyBox"))) {
335 pb_debug("Found TFTP client type: BusyBox tftp\n");
336 type = TFTP_TYPE_BUSYBOX;
339 pb_log("Unknown TFTP client type!\n");
340 type = TFTP_TYPE_BROKEN;
343 process_release(process);
347 static void load_tftp(struct load_task *task)
349 const char *port = "69";
350 const char *argv[10] = {
355 port = task->url->port;
357 if (tftp_type == TFTP_TYPE_UNKNOWN)
358 tftp_type = check_tftp_type(task);
360 if (tftp_type == TFTP_TYPE_BUSYBOX) {
363 argv[3] = NULL; /* 3: local file */
365 argv[5] = task->url->path;
366 argv[6] = task->url->host;
370 load_process_to_local_file(task, argv, 3);
372 } else if (tftp_type == TFTP_TYPE_HPA) {
375 argv[3] = task->url->host;
379 argv[7] = task->url->path;
380 argv[8] = NULL; /* 8: local file */
382 load_process_to_local_file(task, argv, 8);
385 task->result->status = LOAD_ERROR;
390 wget_no_check_certificate = 0x2,
395 * pb_load_wget - Loads a remote file via wget and returns the local file path.
397 * Returns the local file path in a talloc'ed character string on success,
401 static void load_wget(struct load_task *task, int flags)
403 const char *argv[] = {
406 NULL, /* 2: local file */
407 NULL, /* 3 (optional): --quiet */
408 NULL, /* 4 (optional): --no-check-certificate */
414 if (task->process->stdout_cb)
415 flags |= wget_verbose;
419 flags |= wget_verbose;
421 if ((flags & wget_verbose) == 0)
422 argv[i++] = "--quiet";
424 if (flags & wget_no_check_certificate)
425 argv[i++] = "--no-check-certificate";
427 argv[i] = task->url->full;
429 load_process_to_local_file(task, argv, 2);
432 /* Although we don't need to load anything for a local path (we just return
433 * the path from the file:// URL), the other load helpers will error-out on
434 * non-existant files. So, do the same here with an access() check on the local
437 static void load_local(struct load_task *task)
439 struct load_url_result *result = task->result;
442 rc = access(task->url->path, F_OK);
444 result->status = LOAD_ERROR;
446 result->local = talloc_strdup(task->result, task->url->path);
447 result->status = LOAD_OK;
450 task->async_cb(task->result, task->async_data);
453 static void load_url_async_start_pending(struct load_task *task, int flags)
455 pb_log("Starting pending job for %s\n", task->url->full);
457 switch (task->url->scheme) {
460 load_wget(task, flags);
463 flags |= wget_no_check_certificate;
464 load_wget(task, flags);
476 /* Shouldn't be a need via this path but.. */
481 if (task->result->status == LOAD_ERROR) {
482 pb_log("Pending job failed for %s\n", task->url->full);
483 load_url_result_cleanup_local(task->result);
484 talloc_free(task->result);
489 void pending_network_jobs_start(void)
491 struct network_job *job, *tmp;
493 if (!pending_network_jobs.head.next)
496 list_for_each_entry_safe(&pending_network_jobs, job, tmp, list) {
497 load_url_async_start_pending(job->task, job->flags);
498 list_remove(&job->list);
502 void pending_network_jobs_cancel(void)
504 struct network_job *job, *tmp;
506 if (!pending_network_jobs.head.next)
509 list_for_each_entry_safe(&pending_network_jobs, job, tmp, list)
511 list_init(&pending_network_jobs);
514 static void pending_network_jobs_add(struct load_task *task, int flags)
516 struct network_job *job;
518 if (!pending_network_jobs.head.next)
519 list_init(&pending_network_jobs);
521 job = talloc(task, struct network_job);
523 pb_log("Failed to allocate space for pending job\n");
529 list_add_tail(&pending_network_jobs, &job->list);
534 * load_url - Loads a (possibly) remote URL and returns the local file
536 * @ctx: The talloc context to associate with the returned string.
537 * @url: The remote file URL.
538 * @tempfile: An optional variable pointer to be set when a temporary local
540 * @url_cb: An optional callback pointer if the caller wants to load url
543 * Returns the local file path in a talloc'ed character string on success,
547 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
548 load_url_complete async_cb, void *async_data,
549 waiter_cb stdout_cb, void *stdout_data)
551 struct load_url_result *result;
552 struct load_task *task;
558 task = talloc_zero(ctx, struct load_task);
560 task->async = async_cb != NULL;
561 task->result = talloc_zero(ctx, struct load_url_result);
562 task->result->task = task;
563 task->result->url = url;
564 task->process = process_create(task);
566 task->async_cb = async_cb;
567 task->async_data = async_data;
568 task->process->exit_cb = load_url_process_exit;
569 task->process->data = task;
570 task->process->stdout_cb = stdout_cb;
571 task->process->stdout_data = stdout_data;
574 if (!stdout_cb && stdout_data && have_busybox())
575 task->process->stdout_cb = busybox_progress_cb;
577 /* Make sure we save output for any task that has a custom handler */
578 if (task->process->stdout_cb) {
579 task->process->add_stderr = true;
580 task->process->keep_stdout = true;
583 /* If the url is remote but network is not yet available queue up this
585 if (!system_info_network_available() && url->scheme != pb_url_file) {
586 pb_log("load task for %s queued pending network\n", url->full);
587 pending_network_jobs_add(task, flags);
588 task->result->status = LOAD_ASYNC;
592 switch (url->scheme) {
595 load_wget(task, flags);
598 flags |= wget_no_check_certificate;
599 load_wget(task, flags);
615 result = task->result;
616 if (result->status == LOAD_ERROR) {
617 load_url_result_cleanup_local(task->result);
623 if (!task->async || result->status == LOAD_OK)
629 struct load_url_result *load_url(void *ctx, struct pb_url *url)
631 return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
634 void load_url_async_cancel(struct load_url_result *res)
636 struct load_task *task = res->task;
638 /* the completion callback may have already been called; this clears
643 if (res->status == LOAD_CANCELLED)
647 assert(task->process);
649 res->status = LOAD_CANCELLED;
650 process_stop_async(task->process);
655 static void __attribute__((unused)) load_local(
656 struct load_task *task __attribute__((unused)))
659 static void __attribute__((unused)) load_wget(
660 struct load_task *task __attribute__((unused)),
661 int flags __attribute__((unused)))
664 static void __attribute__((unused)) load_tftp(
665 struct load_task *task __attribute__((unused)))
668 static void __attribute__((unused)) load_sftp(
669 struct load_task *task __attribute__((unused)))
672 static void __attribute__((unused)) load_nfs(
673 struct load_task *task __attribute__((unused)))
676 static void __attribute__((unused)) load_url_process_exit(
677 struct process *process __attribute__((unused)))