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)
441 rc = access(task->url->path, F_OK);
443 task->result->status = LOAD_ERROR;
445 task->result->local = talloc_strdup(task->result,
447 task->result->status = LOAD_OK;
451 static void load_url_async_start_pending(struct load_task *task, int flags)
453 pb_log("Starting pending job for %s\n", task->url->full);
455 switch (task->url->scheme) {
458 load_wget(task, flags);
461 flags |= wget_no_check_certificate;
462 load_wget(task, flags);
474 /* Shouldn't be a need via this path but.. */
479 if (task->result->status == LOAD_ERROR) {
480 pb_log("Pending job failed for %s\n", task->url->full);
481 load_url_result_cleanup_local(task->result);
482 talloc_free(task->result);
487 void pending_network_jobs_start(void)
489 struct network_job *job, *tmp;
491 if (!pending_network_jobs.head.next)
494 list_for_each_entry_safe(&pending_network_jobs, job, tmp, list) {
495 load_url_async_start_pending(job->task, job->flags);
496 list_remove(&job->list);
500 void pending_network_jobs_cancel(void)
502 struct network_job *job, *tmp;
504 if (!pending_network_jobs.head.next)
507 list_for_each_entry_safe(&pending_network_jobs, job, tmp, list)
509 list_init(&pending_network_jobs);
512 static void pending_network_jobs_add(struct load_task *task, int flags)
514 struct network_job *job;
516 if (!pending_network_jobs.head.next)
517 list_init(&pending_network_jobs);
519 job = talloc(task, struct network_job);
521 pb_log("Failed to allocate space for pending job\n");
527 list_add_tail(&pending_network_jobs, &job->list);
532 * load_url - Loads a (possibly) remote URL and returns the local file
534 * @ctx: The talloc context to associate with the returned string.
535 * @url: The remote file URL.
536 * @tempfile: An optional variable pointer to be set when a temporary local
538 * @url_cb: An optional callback pointer if the caller wants to load url
541 * Returns the local file path in a talloc'ed character string on success,
545 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
546 load_url_complete async_cb, void *async_data,
547 waiter_cb stdout_cb, void *stdout_data)
549 struct load_url_result *result;
550 struct load_task *task;
556 task = talloc_zero(ctx, struct load_task);
558 task->async = async_cb != NULL;
559 task->result = talloc_zero(ctx, struct load_url_result);
560 task->result->task = task;
561 task->result->url = url;
562 task->process = process_create(task);
564 task->async_cb = async_cb;
565 task->async_data = async_data;
566 task->process->exit_cb = load_url_process_exit;
567 task->process->data = task;
568 task->process->stdout_cb = stdout_cb;
569 task->process->stdout_data = stdout_data;
572 if (!stdout_cb && stdout_data && have_busybox())
573 task->process->stdout_cb = busybox_progress_cb;
575 /* Make sure we save output for any task that has a custom handler */
576 if (task->process->stdout_cb) {
577 task->process->add_stderr = true;
578 task->process->keep_stdout = true;
581 /* If the url is remote but network is not yet available queue up this
583 if (!system_info_network_available() && url->scheme != pb_url_file) {
584 pb_log("load task for %s queued pending network\n", url->full);
585 pending_network_jobs_add(task, flags);
586 task->result->status = LOAD_ASYNC;
590 switch (url->scheme) {
593 load_wget(task, flags);
596 flags |= wget_no_check_certificate;
597 load_wget(task, flags);
613 result = task->result;
614 if (result->status == LOAD_ERROR) {
615 load_url_result_cleanup_local(task->result);
627 struct load_url_result *load_url(void *ctx, struct pb_url *url)
629 return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
632 void load_url_async_cancel(struct load_url_result *res)
634 struct load_task *task = res->task;
636 /* the completion callback may have already been called; this clears
641 if (res->status == LOAD_CANCELLED)
645 assert(task->process);
647 res->status = LOAD_CANCELLED;
648 process_stop_async(task->process);
653 static void __attribute__((unused)) load_local(
654 struct load_task *task __attribute__((unused)))
657 static void __attribute__((unused)) load_wget(
658 struct load_task *task __attribute__((unused)),
659 int flags __attribute__((unused)))
662 static void __attribute__((unused)) load_tftp(
663 struct load_task *task __attribute__((unused)))
666 static void __attribute__((unused)) load_sftp(
667 struct load_task *task __attribute__((unused)))
670 static void __attribute__((unused)) load_nfs(
671 struct load_task *task __attribute__((unused)))
674 static void __attribute__((unused)) load_url_process_exit(
675 struct process *process __attribute__((unused)))