8 #include <talloc/talloc.h>
9 #include <system/system.h>
10 #include <process/process.h>
16 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
20 struct process *process;
21 struct load_url_result *result;
23 load_url_complete async_cb;
27 const char *mount_base(void)
29 return DEVICE_MOUNT_BASE;
32 char *join_paths(void *alloc_ctx, const char *a, const char *b)
36 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
39 if (b[0] != '/' && a[strlen(a) - 1] != '/')
40 strcat(full_path, "/");
47 static char *local_name(void *ctx)
49 char *ret, tmp[] = "/tmp/pb-XXXXXX";
59 ret = talloc_strdup(ctx, tmp);
64 static void load_url_result_cleanup_local(struct load_url_result *result)
66 if (result->cleanup_local)
67 unlink(result->local);
70 static void load_url_process_exit(struct process *process)
72 struct load_task *task = process->data;
73 struct load_url_result *result;
77 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
78 process->path, process->pid, task->url->full,
79 process->exit_status);
81 result = task->result;
82 data = task->async_data;
85 if (result->status == LOAD_CANCELLED) {
86 load_url_result_cleanup_local(result);
87 } else if (process->exit_status == 0) {
88 result->status = LOAD_OK;
90 result->status = LOAD_ERROR;
91 load_url_result_cleanup_local(result);
94 /* The load callback may well free the ctx, which was the
95 * talloc parent of the task. Therefore, we want to do our cleanup
98 process_release(process);
105 static void load_process_to_local_file(struct load_task *task,
106 const char **argv, int argv_local_idx)
110 task->result->local = local_name(task->result);
111 if (!task->result->local) {
112 task->result->status = LOAD_ERROR;
115 task->result->cleanup_local = true;
118 argv[argv_local_idx] = task->result->local;
120 task->process->argv = argv;
121 task->process->path = argv[0];
124 rc = process_run_async(task->process);
126 process_release(task->process);
127 task->process = NULL;
129 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
131 rc = process_run_sync(task->process);
132 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
133 process_release(task->process);
134 task->process = NULL;
139 * pb_load_nfs - Create a mountpoint, set the local file within that
140 * mountpoint, and run the appropriate mount command
143 static void load_nfs(struct load_task *task)
145 char *mountpoint, *opts;
147 const char *argv[] = {
148 pb_system_apps.mount,
153 NULL, /* 6: mountpoint */
157 task->result->status = LOAD_ERROR;
158 mountpoint = local_name(task->result);
161 task->result->cleanup_local = true;
162 argv[6] = mountpoint;
164 rc = pb_mkdir_recursive(mountpoint);
168 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
172 opts = talloc_asprintf_append(opts, ",port=%s",
175 task->result->local = talloc_asprintf(task->result, "%s/%s",
179 task->process->path = pb_system_apps.mount;
180 task->process->argv = argv;
183 rc = process_run_async(task->process);
185 process_release(task->process);
186 task->process = NULL;
188 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
190 rc = process_run_sync(task->process);
191 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
192 process_release(task->process);
193 task->process = NULL;
199 static void load_sftp(struct load_task *task)
201 const char *argv[] = {
203 NULL, /* 1: host:path */
204 NULL, /* 2: local file */
208 argv[1] = talloc_asprintf(task, "%s:%s",
209 task->url->host, task->url->path);
210 load_process_to_local_file(task, argv, 2);
213 static enum tftp_type check_tftp_type(void *ctx)
215 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
216 struct process *process;
219 process = process_create(ctx);
220 process->path = pb_system_apps.tftp;
221 process->argv = argv;
222 process->keep_stdout = true;
223 process_run_sync(process);
225 if (!process->stdout_buf || process->stdout_len == 0) {
226 pb_log("Can't check TFTP client type!\n");
227 type = TFTP_TYPE_BROKEN;
229 } else if (memmem(process->stdout_buf, process->stdout_len,
230 "tftp-hpa", strlen("tftp-hpa"))) {
231 pb_debug("Found TFTP client type: tftp-hpa\n");
232 type = TFTP_TYPE_HPA;
234 } else if (memmem(process->stdout_buf, process->stdout_len,
235 "BusyBox", strlen("BusyBox"))) {
236 pb_debug("Found TFTP client type: BusyBox tftp\n");
237 type = TFTP_TYPE_BUSYBOX;
240 pb_log("Unknown TFTP client type!\n");
241 type = TFTP_TYPE_BROKEN;
244 process_release(process);
248 static void load_tftp(struct load_task *task)
250 const char *port = "69";
251 const char *argv[10] = {
256 port = task->url->port;
258 if (tftp_type == TFTP_TYPE_UNKNOWN)
259 tftp_type = check_tftp_type(task);
261 if (tftp_type == TFTP_TYPE_BUSYBOX) {
264 argv[3] = NULL; /* 3: local file */
266 argv[5] = task->url->path;
267 argv[6] = task->url->host;
271 load_process_to_local_file(task, argv, 3);
273 } else if (tftp_type == TFTP_TYPE_HPA) {
276 argv[3] = task->url->host;
280 argv[7] = task->url->path;
281 argv[8] = NULL; /* 8: local file */
283 load_process_to_local_file(task, argv, 8);
286 task->result->status = LOAD_ERROR;
291 wget_no_check_certificate = 1,
295 * pb_load_wget - Loads a remote file via wget and returns the local file path.
297 * Returns the local file path in a talloc'ed character string on success,
301 static void load_wget(struct load_task *task, int flags)
303 const char *argv[] = {
306 NULL, /* 2: local file */
315 argv[i++] = "--quiet";
317 if (flags & wget_no_check_certificate)
318 argv[i++] = "--no-check-certificate";
320 argv[i] = task->url->full;
322 load_process_to_local_file(task, argv, 2);
325 /* Although we don't need to load anything for a local path (we just return
326 * the path from the file:// URL), the other load helpers will error-out on
327 * non-existant files. So, do the same here with an access() check on the local
330 static void load_local(struct load_task *task)
334 rc = access(task->url->path, F_OK);
336 task->result->status = LOAD_ERROR;
338 task->result->local = talloc_strdup(task->result,
340 task->result->status = LOAD_OK;
345 * load_url - Loads a (possibly) remote URL and returns the local file
347 * @ctx: The talloc context to associate with the returned string.
348 * @url: The remote file URL.
349 * @tempfile: An optional variable pointer to be set when a temporary local
351 * @url_cb: An optional callback pointer if the caller wants to load url
354 * Returns the local file path in a talloc'ed character string on success,
358 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
359 load_url_complete async_cb, void *async_data)
361 struct load_url_result *result;
362 struct load_task *task;
367 task = talloc_zero(ctx, struct load_task);
369 task->async = async_cb != NULL;
370 task->result = talloc_zero(ctx, struct load_url_result);
371 task->result->task = task;
372 task->process = process_create(task);
374 task->async_cb = async_cb;
375 task->async_data = async_data;
376 task->process->exit_cb = load_url_process_exit;
377 task->process->data = task;
380 switch (url->scheme) {
386 load_wget(task, wget_no_check_certificate);
402 result = task->result;
403 if (result->status == LOAD_ERROR) {
404 load_url_result_cleanup_local(task->result);
416 struct load_url_result *load_url(void *ctx, struct pb_url *url)
418 return load_url_async(ctx, url, NULL, NULL);
421 void load_url_async_cancel(struct load_url_result *res)
423 struct load_task *task = res->task;
425 /* the completion callback may have already been called; this clears
430 if (res->status == LOAD_CANCELLED)
434 assert(task->process);
436 res->status = LOAD_CANCELLED;
437 process_stop_async(task->process);