1 #if defined(HAVE_CONFIG_H)
12 #include <talloc/talloc.h>
13 #include <system/system.h>
14 #include <process/process.h>
20 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
24 struct process *process;
25 struct load_url_result *result;
27 load_url_complete async_cb;
31 const char *mount_base(void)
33 return DEVICE_MOUNT_BASE;
36 char *join_paths(void *alloc_ctx, const char *a, const char *b)
40 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
43 if (b[0] != '/' && a[strlen(a) - 1] != '/')
44 strcat(full_path, "/");
50 #ifndef PETITBOOT_TEST
52 static char *local_name(void *ctx)
54 char *ret, tmp[] = "/tmp/pb-XXXXXX";
58 oldmask = umask(0644);
67 ret = talloc_strdup(ctx, tmp);
72 static void load_url_result_cleanup_local(struct load_url_result *result)
74 if (result->cleanup_local)
75 unlink(result->local);
78 static void load_url_process_exit(struct process *process)
80 struct load_task *task = process->data;
81 struct load_url_result *result;
85 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
86 process->path, process->pid, task->url->full,
87 process->exit_status);
89 result = task->result;
90 data = task->async_data;
93 if (result->status == LOAD_CANCELLED) {
94 load_url_result_cleanup_local(result);
95 } else if (process_exit_ok(process)) {
96 result->status = LOAD_OK;
98 result->status = LOAD_ERROR;
99 load_url_result_cleanup_local(result);
102 /* The load callback may well free the ctx, which was the
103 * talloc parent of the task. Therefore, we want to do our cleanup
106 process_release(process);
113 static void load_process_to_local_file(struct load_task *task,
114 const char **argv, int argv_local_idx)
118 task->result->local = local_name(task->result);
119 if (!task->result->local) {
120 task->result->status = LOAD_ERROR;
123 task->result->cleanup_local = true;
126 argv[argv_local_idx] = task->result->local;
128 task->process->argv = argv;
129 task->process->path = argv[0];
132 rc = process_run_async(task->process);
134 process_release(task->process);
135 task->process = NULL;
137 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
139 rc = process_run_sync(task->process);
140 if (rc || !process_exit_ok(task->process))
141 task->result->status = LOAD_ERROR;
143 task->result->status = LOAD_OK;
144 process_release(task->process);
145 task->process = NULL;
150 * pb_load_nfs - Create a mountpoint, set the local file within that
151 * mountpoint, and run the appropriate mount command
154 static void load_nfs(struct load_task *task)
156 char *mountpoint, *opts;
158 const char *argv[] = {
159 pb_system_apps.mount,
164 NULL, /* 6: mountpoint */
168 task->result->status = LOAD_ERROR;
169 mountpoint = local_name(task->result);
172 task->result->cleanup_local = true;
173 argv[6] = mountpoint;
175 rc = pb_mkdir_recursive(mountpoint);
179 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
183 opts = talloc_asprintf_append(opts, ",port=%s",
186 task->result->local = talloc_asprintf(task->result, "%s/%s",
190 task->process->path = pb_system_apps.mount;
191 task->process->argv = argv;
194 rc = process_run_async(task->process);
196 process_release(task->process);
197 task->process = NULL;
199 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
201 rc = process_run_sync(task->process);
202 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
203 process_release(task->process);
204 task->process = NULL;
210 static void load_sftp(struct load_task *task)
212 const char *argv[] = {
214 NULL, /* 1: host:path */
215 NULL, /* 2: local file */
219 argv[1] = talloc_asprintf(task, "%s:%s",
220 task->url->host, task->url->path);
221 load_process_to_local_file(task, argv, 2);
224 static enum tftp_type check_tftp_type(void *ctx)
226 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
227 struct process *process;
231 process = process_create(ctx);
232 process->path = pb_system_apps.tftp;
233 process->argv = argv;
234 process->keep_stdout = true;
235 process->add_stderr = true;
236 rc = process_run_sync(process);
238 if (rc || !process->stdout_buf || process->stdout_len == 0) {
239 pb_log("Can't check TFTP client type!\n");
240 type = TFTP_TYPE_BROKEN;
242 } else if (memmem(process->stdout_buf, process->stdout_len,
243 "tftp-hpa", strlen("tftp-hpa"))) {
244 pb_debug("Found TFTP client type: tftp-hpa\n");
245 type = TFTP_TYPE_HPA;
247 } else if (memmem(process->stdout_buf, process->stdout_len,
248 "BusyBox", strlen("BusyBox"))) {
249 pb_debug("Found TFTP client type: BusyBox tftp\n");
250 type = TFTP_TYPE_BUSYBOX;
253 pb_log("Unknown TFTP client type!\n");
254 type = TFTP_TYPE_BROKEN;
257 process_release(process);
261 static void load_tftp(struct load_task *task)
263 const char *port = "69";
264 const char *argv[10] = {
269 port = task->url->port;
271 if (tftp_type == TFTP_TYPE_UNKNOWN)
272 tftp_type = check_tftp_type(task);
274 if (tftp_type == TFTP_TYPE_BUSYBOX) {
277 argv[3] = NULL; /* 3: local file */
279 argv[5] = task->url->path;
280 argv[6] = task->url->host;
284 load_process_to_local_file(task, argv, 3);
286 } else if (tftp_type == TFTP_TYPE_HPA) {
289 argv[3] = task->url->host;
293 argv[7] = task->url->path;
294 argv[8] = NULL; /* 8: local file */
296 load_process_to_local_file(task, argv, 8);
299 task->result->status = LOAD_ERROR;
304 wget_no_check_certificate = 1,
308 * pb_load_wget - Loads a remote file via wget and returns the local file path.
310 * Returns the local file path in a talloc'ed character string on success,
314 static void load_wget(struct load_task *task, int flags)
316 const char *argv[] = {
319 NULL, /* 2: local file */
320 NULL, /* 3 (optional): --quiet */
321 NULL, /* 4 (optional): --no-check-certificate */
329 argv[i++] = "--quiet";
331 if (flags & wget_no_check_certificate)
332 argv[i++] = "--no-check-certificate";
334 argv[i] = task->url->full;
336 load_process_to_local_file(task, argv, 2);
339 /* Although we don't need to load anything for a local path (we just return
340 * the path from the file:// URL), the other load helpers will error-out on
341 * non-existant files. So, do the same here with an access() check on the local
344 static void load_local(struct load_task *task)
348 rc = access(task->url->path, F_OK);
350 task->result->status = LOAD_ERROR;
352 task->result->local = talloc_strdup(task->result,
354 task->result->status = LOAD_OK;
359 * load_url - Loads a (possibly) remote URL and returns the local file
361 * @ctx: The talloc context to associate with the returned string.
362 * @url: The remote file URL.
363 * @tempfile: An optional variable pointer to be set when a temporary local
365 * @url_cb: An optional callback pointer if the caller wants to load url
368 * Returns the local file path in a talloc'ed character string on success,
372 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
373 load_url_complete async_cb, void *async_data)
375 struct load_url_result *result;
376 struct load_task *task;
381 task = talloc_zero(ctx, struct load_task);
383 task->async = async_cb != NULL;
384 task->result = talloc_zero(ctx, struct load_url_result);
385 task->result->task = task;
386 task->process = process_create(task);
388 task->async_cb = async_cb;
389 task->async_data = async_data;
390 task->process->exit_cb = load_url_process_exit;
391 task->process->data = task;
394 switch (url->scheme) {
400 load_wget(task, wget_no_check_certificate);
416 result = task->result;
417 if (result->status == LOAD_ERROR) {
418 load_url_result_cleanup_local(task->result);
430 struct load_url_result *load_url(void *ctx, struct pb_url *url)
432 return load_url_async(ctx, url, NULL, NULL);
435 void load_url_async_cancel(struct load_url_result *res)
437 struct load_task *task = res->task;
439 /* the completion callback may have already been called; this clears
444 if (res->status == LOAD_CANCELLED)
448 assert(task->process);
450 res->status = LOAD_CANCELLED;
451 process_stop_async(task->process);
456 static void __attribute__((unused)) load_local(
457 struct load_task *task __attribute__((unused)))
460 static void __attribute__((unused)) load_wget(
461 struct load_task *task __attribute__((unused)),
462 int flags __attribute__((unused)))
465 static void __attribute__((unused)) load_tftp(
466 struct load_task *task __attribute__((unused)))
469 static void __attribute__((unused)) load_sftp(
470 struct load_task *task __attribute__((unused)))
473 static void __attribute__((unused)) load_nfs(
474 struct load_task *task __attribute__((unused)))
477 static void __attribute__((unused)) load_url_process_exit(
478 struct process *process __attribute__((unused)))