1 #if defined(HAVE_CONFIG_H)
12 #include <talloc/talloc.h>
13 #include <system/system.h>
14 #include <process/process.h>
19 #include "device-handler.h"
21 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
25 struct process *process;
26 struct load_url_result *result;
28 load_url_complete async_cb;
32 static inline bool have_busybox(void)
41 const char *mount_base(void)
43 return DEVICE_MOUNT_BASE;
46 char *join_paths(void *alloc_ctx, const char *a, const char *b)
50 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
53 if (b[0] != '/' && a[strlen(a) - 1] != '/')
54 strcat(full_path, "/");
60 #ifndef PETITBOOT_TEST
62 static char *local_name(void *ctx)
64 char *ret, tmp[] = "/tmp/pb-XXXXXX";
68 oldmask = umask(0644);
77 ret = talloc_strdup(ctx, tmp);
82 static void load_url_result_cleanup_local(struct load_url_result *result)
84 if (result->cleanup_local)
85 unlink(result->local);
88 static void load_url_process_exit(struct process *process)
90 struct load_task *task = process->data;
91 struct load_url_result *result;
95 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
96 process->path, process->pid, task->url->full,
97 process->exit_status);
99 result = task->result;
100 data = task->async_data;
103 if (result->status == LOAD_CANCELLED) {
104 load_url_result_cleanup_local(result);
105 } else if (process_exit_ok(process)) {
106 result->status = LOAD_OK;
108 result->status = LOAD_ERROR;
109 load_url_result_cleanup_local(result);
112 /* The load callback may well free the ctx, which was the
113 * talloc parent of the task. Therefore, we want to do our cleanup
116 process_release(process);
124 * Callback to retrieve progress information from Busybox utilities.
125 * Busybox utilities use a common progress bar format which progress percentage
126 * and current size can be can be parsed from.
128 static int busybox_progress_cb(void *arg)
130 const char *busybox_fmt = "%*s %u%*[%* |]%u%c %*u:%*u:%*u ETA\n";
131 struct process_info *procinfo = arg;
132 char *n, *s, suffix, *line = NULL;
133 struct device_handler *handler;
134 unsigned int percentage, size;
141 p = procinfo_get_process(procinfo);
142 handler = p->stdout_data;
144 rc = process_stdout_custom(procinfo, &line);
147 /* Unregister ourselves from progress tracking */
148 device_handler_status_download_remove(handler, procinfo);
154 rc = sscanf(line, busybox_fmt, &percentage, &size, &suffix);
157 * Many unrecognised lines are partial updates. If we see a partial
158 * line with a newline character, see if we can match a valid line
159 * at the end of stdout_buf
162 n = strchr(line, '\n');
164 for (s = n - 1; s >= p->stdout_buf; s--)
166 rc = sscanf(s + 1, busybox_fmt,
167 &percentage, &size, &suffix);
173 percentage = size = 0;
175 device_handler_status_download(handler, procinfo,
176 percentage, size, suffix);
183 static void load_process_to_local_file(struct load_task *task,
184 const char **argv, int argv_local_idx)
188 task->result->local = local_name(task->result);
189 if (!task->result->local) {
190 task->result->status = LOAD_ERROR;
193 task->result->cleanup_local = true;
196 argv[argv_local_idx] = task->result->local;
198 task->process->argv = argv;
199 task->process->path = argv[0];
202 rc = process_run_async(task->process);
204 process_release(task->process);
205 task->process = NULL;
207 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
209 rc = process_run_sync(task->process);
210 if (rc || !process_exit_ok(task->process))
211 task->result->status = LOAD_ERROR;
213 task->result->status = LOAD_OK;
214 process_release(task->process);
215 task->process = NULL;
220 * pb_load_nfs - Create a mountpoint, set the local file within that
221 * mountpoint, and run the appropriate mount command
224 static void load_nfs(struct load_task *task)
226 char *mountpoint, *opts;
228 const char *argv[] = {
229 pb_system_apps.mount,
234 NULL, /* 6: mountpoint */
238 task->result->status = LOAD_ERROR;
239 mountpoint = local_name(task->result);
242 task->result->cleanup_local = true;
243 argv[6] = mountpoint;
245 rc = pb_mkdir_recursive(mountpoint);
249 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
253 opts = talloc_asprintf_append(opts, ",port=%s",
256 task->result->local = talloc_asprintf(task->result, "%s/%s",
260 task->process->path = pb_system_apps.mount;
261 task->process->argv = argv;
264 rc = process_run_async(task->process);
266 process_release(task->process);
267 task->process = NULL;
269 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
271 rc = process_run_sync(task->process);
272 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
273 process_release(task->process);
274 task->process = NULL;
280 static void load_sftp(struct load_task *task)
282 const char *argv[] = {
284 NULL, /* 1: host:path */
285 NULL, /* 2: local file */
289 argv[1] = talloc_asprintf(task, "%s:%s",
290 task->url->host, task->url->path);
291 load_process_to_local_file(task, argv, 2);
294 static enum tftp_type check_tftp_type(void *ctx)
296 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
297 struct process *process;
301 process = process_create(ctx);
302 process->path = pb_system_apps.tftp;
303 process->argv = argv;
304 process->keep_stdout = true;
305 process->add_stderr = true;
306 rc = process_run_sync(process);
308 if (rc || !process->stdout_buf || process->stdout_len == 0) {
309 pb_log("Can't check TFTP client type!\n");
310 type = TFTP_TYPE_BROKEN;
312 } else if (memmem(process->stdout_buf, process->stdout_len,
313 "tftp-hpa", strlen("tftp-hpa"))) {
314 pb_debug("Found TFTP client type: tftp-hpa\n");
315 type = TFTP_TYPE_HPA;
317 } else if (memmem(process->stdout_buf, process->stdout_len,
318 "BusyBox", strlen("BusyBox"))) {
319 pb_debug("Found TFTP client type: BusyBox tftp\n");
320 type = TFTP_TYPE_BUSYBOX;
323 pb_log("Unknown TFTP client type!\n");
324 type = TFTP_TYPE_BROKEN;
327 process_release(process);
331 static void load_tftp(struct load_task *task)
333 const char *port = "69";
334 const char *argv[10] = {
339 port = task->url->port;
341 if (tftp_type == TFTP_TYPE_UNKNOWN)
342 tftp_type = check_tftp_type(task);
344 if (tftp_type == TFTP_TYPE_BUSYBOX) {
347 argv[3] = NULL; /* 3: local file */
349 argv[5] = task->url->path;
350 argv[6] = task->url->host;
354 load_process_to_local_file(task, argv, 3);
356 } else if (tftp_type == TFTP_TYPE_HPA) {
359 argv[3] = task->url->host;
363 argv[7] = task->url->path;
364 argv[8] = NULL; /* 8: local file */
366 load_process_to_local_file(task, argv, 8);
369 task->result->status = LOAD_ERROR;
374 wget_no_check_certificate = 0x2,
379 * pb_load_wget - Loads a remote file via wget and returns the local file path.
381 * Returns the local file path in a talloc'ed character string on success,
385 static void load_wget(struct load_task *task, int flags)
387 const char *argv[] = {
390 NULL, /* 2: local file */
391 NULL, /* 3 (optional): --quiet */
392 NULL, /* 4 (optional): --no-check-certificate */
398 if (task->process->stdout_cb)
399 flags |= wget_verbose;
403 flags |= wget_verbose;
405 if ((flags & wget_verbose) == 0)
406 argv[i++] = "--quiet";
408 if (flags & wget_no_check_certificate)
409 argv[i++] = "--no-check-certificate";
411 argv[i] = task->url->full;
413 load_process_to_local_file(task, argv, 2);
416 /* Although we don't need to load anything for a local path (we just return
417 * the path from the file:// URL), the other load helpers will error-out on
418 * non-existant files. So, do the same here with an access() check on the local
421 static void load_local(struct load_task *task)
425 rc = access(task->url->path, F_OK);
427 task->result->status = LOAD_ERROR;
429 task->result->local = talloc_strdup(task->result,
431 task->result->status = LOAD_OK;
436 * load_url - Loads a (possibly) remote URL and returns the local file
438 * @ctx: The talloc context to associate with the returned string.
439 * @url: The remote file URL.
440 * @tempfile: An optional variable pointer to be set when a temporary local
442 * @url_cb: An optional callback pointer if the caller wants to load url
445 * Returns the local file path in a talloc'ed character string on success,
449 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
450 load_url_complete async_cb, void *async_data,
451 waiter_cb stdout_cb, void *stdout_data)
453 struct load_url_result *result;
454 struct load_task *task;
460 task = talloc_zero(ctx, struct load_task);
462 task->async = async_cb != NULL;
463 task->result = talloc_zero(ctx, struct load_url_result);
464 task->result->task = task;
465 task->result->url = url;
466 task->process = process_create(task);
468 task->async_cb = async_cb;
469 task->async_data = async_data;
470 task->process->exit_cb = load_url_process_exit;
471 task->process->data = task;
472 task->process->stdout_cb = stdout_cb;
473 task->process->stdout_data = stdout_data;
476 if (!stdout_cb && stdout_data && have_busybox())
477 task->process->stdout_cb = busybox_progress_cb;
479 /* Make sure we save output for any task that has a custom handler */
480 if (task->process->stdout_cb) {
481 task->process->add_stderr = true;
482 task->process->keep_stdout = true;
485 switch (url->scheme) {
488 load_wget(task, flags);
491 flags |= wget_no_check_certificate;
492 load_wget(task, flags);
508 result = task->result;
509 if (result->status == LOAD_ERROR) {
510 load_url_result_cleanup_local(task->result);
522 struct load_url_result *load_url(void *ctx, struct pb_url *url)
524 return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
527 void load_url_async_cancel(struct load_url_result *res)
529 struct load_task *task = res->task;
531 /* the completion callback may have already been called; this clears
536 if (res->status == LOAD_CANCELLED)
540 assert(task->process);
542 res->status = LOAD_CANCELLED;
543 process_stop_async(task->process);
548 static void __attribute__((unused)) load_local(
549 struct load_task *task __attribute__((unused)))
552 static void __attribute__((unused)) load_wget(
553 struct load_task *task __attribute__((unused)),
554 int flags __attribute__((unused)))
557 static void __attribute__((unused)) load_tftp(
558 struct load_task *task __attribute__((unused)))
561 static void __attribute__((unused)) load_sftp(
562 struct load_task *task __attribute__((unused)))
565 static void __attribute__((unused)) load_nfs(
566 struct load_task *task __attribute__((unused)))
569 static void __attribute__((unused)) load_url_process_exit(
570 struct process *process __attribute__((unused)))