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"
22 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
26 struct process *process;
27 struct load_url_result *result;
29 load_url_complete async_cb;
33 static inline bool have_busybox(void)
42 const char *mount_base(void)
44 return DEVICE_MOUNT_BASE;
47 char *join_paths(void *alloc_ctx, const char *a, const char *b)
51 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
54 if (b[0] != '/' && a[strlen(a) - 1] != '/')
55 strcat(full_path, "/");
61 #ifndef PETITBOOT_TEST
63 static char *local_name(void *ctx)
65 char *ret, tmp[] = "/tmp/pb-XXXXXX";
69 oldmask = umask(0644);
78 ret = talloc_strdup(ctx, tmp);
83 static void load_url_result_cleanup_local(struct load_url_result *result)
85 if (result->cleanup_local)
86 unlink(result->local);
89 static void load_url_process_exit(struct process *process)
91 struct load_task *task = process->data;
92 struct load_url_result *result;
96 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
97 process->path, process->pid, task->url->full,
98 process->exit_status);
100 result = task->result;
101 data = task->async_data;
104 if (result->status == LOAD_CANCELLED) {
105 load_url_result_cleanup_local(result);
106 } else if (process_exit_ok(process)) {
107 result->status = LOAD_OK;
109 result->status = LOAD_ERROR;
110 load_url_result_cleanup_local(result);
113 if (result->status == LOAD_OK && process->stdout_data)
114 device_handler_status_info(process->stdout_data,
115 _("Download complete: %s"), task->url->file);
117 /* The load callback may well free the ctx, which was the
118 * talloc parent of the task. Therefore, we want to do our cleanup
121 process_release(process);
129 * Callback to retrieve progress information from Busybox utilities.
130 * Busybox utilities use a common progress bar format which progress percentage
131 * and current size can be can be parsed from.
133 static int busybox_progress_cb(void *arg)
135 const char *busybox_fmt = "%*s %u%*[%* |]%u%c %*u:%*u:%*u ETA\n";
136 struct process_info *procinfo = arg;
137 char *n, *s, suffix, *line = NULL;
138 struct device_handler *handler;
139 unsigned int percentage, size;
146 p = procinfo_get_process(procinfo);
147 handler = p->stdout_data;
149 rc = process_stdout_custom(procinfo, &line);
152 /* Unregister ourselves from progress tracking */
153 device_handler_status_download_remove(handler, procinfo);
159 rc = sscanf(line, busybox_fmt, &percentage, &size, &suffix);
162 * Many unrecognised lines are partial updates. If we see a partial
163 * line with a newline character, see if we can match a valid line
164 * at the end of stdout_buf
167 n = strchr(line, '\n');
169 for (s = n - 1; s >= p->stdout_buf; s--)
171 rc = sscanf(s + 1, busybox_fmt,
172 &percentage, &size, &suffix);
178 percentage = size = 0;
180 device_handler_status_download(handler, procinfo,
181 percentage, size, suffix);
188 static void load_process_to_local_file(struct load_task *task,
189 const char **argv, int argv_local_idx)
193 task->result->local = local_name(task->result);
194 if (!task->result->local) {
195 task->result->status = LOAD_ERROR;
198 task->result->cleanup_local = true;
201 argv[argv_local_idx] = task->result->local;
203 task->process->argv = argv;
204 task->process->path = argv[0];
207 rc = process_run_async(task->process);
209 process_release(task->process);
210 task->process = NULL;
212 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
214 rc = process_run_sync(task->process);
215 if (rc || !process_exit_ok(task->process))
216 task->result->status = LOAD_ERROR;
218 task->result->status = LOAD_OK;
219 process_release(task->process);
220 task->process = NULL;
225 * pb_load_nfs - Create a mountpoint, set the local file within that
226 * mountpoint, and run the appropriate mount command
229 static void load_nfs(struct load_task *task)
231 char *mountpoint, *opts;
233 const char *argv[] = {
234 pb_system_apps.mount,
239 NULL, /* 6: mountpoint */
243 task->result->status = LOAD_ERROR;
244 mountpoint = local_name(task->result);
247 task->result->cleanup_local = true;
248 argv[6] = mountpoint;
250 rc = pb_mkdir_recursive(mountpoint);
254 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
258 opts = talloc_asprintf_append(opts, ",port=%s",
261 task->result->local = talloc_asprintf(task->result, "%s/%s",
265 task->process->path = pb_system_apps.mount;
266 task->process->argv = argv;
269 rc = process_run_async(task->process);
271 process_release(task->process);
272 task->process = NULL;
274 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
276 rc = process_run_sync(task->process);
277 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
278 process_release(task->process);
279 task->process = NULL;
285 static void load_sftp(struct load_task *task)
287 const char *argv[] = {
289 NULL, /* 1: host:path */
290 NULL, /* 2: local file */
294 argv[1] = talloc_asprintf(task, "%s:%s",
295 task->url->host, task->url->path);
296 load_process_to_local_file(task, argv, 2);
299 static enum tftp_type check_tftp_type(void *ctx)
301 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
302 struct process *process;
306 process = process_create(ctx);
307 process->path = pb_system_apps.tftp;
308 process->argv = argv;
309 process->keep_stdout = true;
310 process->add_stderr = true;
311 rc = process_run_sync(process);
313 if (rc || !process->stdout_buf || process->stdout_len == 0) {
314 pb_log("Can't check TFTP client type!\n");
315 type = TFTP_TYPE_BROKEN;
317 } else if (memmem(process->stdout_buf, process->stdout_len,
318 "tftp-hpa", strlen("tftp-hpa"))) {
319 pb_debug("Found TFTP client type: tftp-hpa\n");
320 type = TFTP_TYPE_HPA;
322 } else if (memmem(process->stdout_buf, process->stdout_len,
323 "BusyBox", strlen("BusyBox"))) {
324 pb_debug("Found TFTP client type: BusyBox tftp\n");
325 type = TFTP_TYPE_BUSYBOX;
328 pb_log("Unknown TFTP client type!\n");
329 type = TFTP_TYPE_BROKEN;
332 process_release(process);
336 static void load_tftp(struct load_task *task)
338 const char *port = "69";
339 const char *argv[10] = {
344 port = task->url->port;
346 if (tftp_type == TFTP_TYPE_UNKNOWN)
347 tftp_type = check_tftp_type(task);
349 if (tftp_type == TFTP_TYPE_BUSYBOX) {
352 argv[3] = NULL; /* 3: local file */
354 argv[5] = task->url->path;
355 argv[6] = task->url->host;
359 load_process_to_local_file(task, argv, 3);
361 } else if (tftp_type == TFTP_TYPE_HPA) {
364 argv[3] = task->url->host;
368 argv[7] = task->url->path;
369 argv[8] = NULL; /* 8: local file */
371 load_process_to_local_file(task, argv, 8);
374 task->result->status = LOAD_ERROR;
379 wget_no_check_certificate = 0x2,
384 * pb_load_wget - Loads a remote file via wget and returns the local file path.
386 * Returns the local file path in a talloc'ed character string on success,
390 static void load_wget(struct load_task *task, int flags)
392 const char *argv[] = {
395 NULL, /* 2: local file */
396 NULL, /* 3 (optional): --quiet */
397 NULL, /* 4 (optional): --no-check-certificate */
403 if (task->process->stdout_cb)
404 flags |= wget_verbose;
408 flags |= wget_verbose;
410 if ((flags & wget_verbose) == 0)
411 argv[i++] = "--quiet";
413 if (flags & wget_no_check_certificate)
414 argv[i++] = "--no-check-certificate";
416 argv[i] = task->url->full;
418 load_process_to_local_file(task, argv, 2);
421 /* Although we don't need to load anything for a local path (we just return
422 * the path from the file:// URL), the other load helpers will error-out on
423 * non-existant files. So, do the same here with an access() check on the local
426 static void load_local(struct load_task *task)
430 rc = access(task->url->path, F_OK);
432 task->result->status = LOAD_ERROR;
434 task->result->local = talloc_strdup(task->result,
436 task->result->status = LOAD_OK;
441 * load_url - Loads a (possibly) remote URL and returns the local file
443 * @ctx: The talloc context to associate with the returned string.
444 * @url: The remote file URL.
445 * @tempfile: An optional variable pointer to be set when a temporary local
447 * @url_cb: An optional callback pointer if the caller wants to load url
450 * Returns the local file path in a talloc'ed character string on success,
454 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
455 load_url_complete async_cb, void *async_data,
456 waiter_cb stdout_cb, void *stdout_data)
458 struct load_url_result *result;
459 struct load_task *task;
465 task = talloc_zero(ctx, struct load_task);
467 task->async = async_cb != NULL;
468 task->result = talloc_zero(ctx, struct load_url_result);
469 task->result->task = task;
470 task->result->url = url;
471 task->process = process_create(task);
473 task->async_cb = async_cb;
474 task->async_data = async_data;
475 task->process->exit_cb = load_url_process_exit;
476 task->process->data = task;
477 task->process->stdout_cb = stdout_cb;
478 task->process->stdout_data = stdout_data;
481 if (!stdout_cb && stdout_data && have_busybox())
482 task->process->stdout_cb = busybox_progress_cb;
484 /* Make sure we save output for any task that has a custom handler */
485 if (task->process->stdout_cb) {
486 task->process->add_stderr = true;
487 task->process->keep_stdout = true;
490 switch (url->scheme) {
493 load_wget(task, flags);
496 flags |= wget_no_check_certificate;
497 load_wget(task, flags);
513 result = task->result;
514 if (result->status == LOAD_ERROR) {
515 load_url_result_cleanup_local(task->result);
527 struct load_url_result *load_url(void *ctx, struct pb_url *url)
529 return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
532 void load_url_async_cancel(struct load_url_result *res)
534 struct load_task *task = res->task;
536 /* the completion callback may have already been called; this clears
541 if (res->status == LOAD_CANCELLED)
545 assert(task->process);
547 res->status = LOAD_CANCELLED;
548 process_stop_async(task->process);
553 static void __attribute__((unused)) load_local(
554 struct load_task *task __attribute__((unused)))
557 static void __attribute__((unused)) load_wget(
558 struct load_task *task __attribute__((unused)),
559 int flags __attribute__((unused)))
562 static void __attribute__((unused)) load_tftp(
563 struct load_task *task __attribute__((unused)))
566 static void __attribute__((unused)) load_sftp(
567 struct load_task *task __attribute__((unused)))
570 static void __attribute__((unused)) load_nfs(
571 struct load_task *task __attribute__((unused)))
574 static void __attribute__((unused)) load_url_process_exit(
575 struct process *process __attribute__((unused)))