1 #if defined(HAVE_CONFIG_H)
10 #include <talloc/talloc.h>
11 #include <system/system.h>
12 #include <process/process.h>
18 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
22 struct process *process;
23 struct load_url_result *result;
25 load_url_complete async_cb;
29 const char *mount_base(void)
31 return DEVICE_MOUNT_BASE;
34 char *join_paths(void *alloc_ctx, const char *a, const char *b)
38 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
41 if (b[0] != '/' && a[strlen(a) - 1] != '/')
42 strcat(full_path, "/");
49 static char *local_name(void *ctx)
51 char *ret, tmp[] = "/tmp/pb-XXXXXX";
61 ret = talloc_strdup(ctx, tmp);
66 static void load_url_result_cleanup_local(struct load_url_result *result)
68 if (result->cleanup_local)
69 unlink(result->local);
72 static void load_url_process_exit(struct process *process)
74 struct load_task *task = process->data;
75 struct load_url_result *result;
79 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
80 process->path, process->pid, task->url->full,
81 process->exit_status);
83 result = task->result;
84 data = task->async_data;
87 if (result->status == LOAD_CANCELLED) {
88 load_url_result_cleanup_local(result);
89 } else if (process_exit_ok(process)) {
90 result->status = LOAD_OK;
92 result->status = LOAD_ERROR;
93 load_url_result_cleanup_local(result);
96 /* The load callback may well free the ctx, which was the
97 * talloc parent of the task. Therefore, we want to do our cleanup
100 process_release(process);
107 static void load_process_to_local_file(struct load_task *task,
108 const char **argv, int argv_local_idx)
112 task->result->local = local_name(task->result);
113 if (!task->result->local) {
114 task->result->status = LOAD_ERROR;
117 task->result->cleanup_local = true;
120 argv[argv_local_idx] = task->result->local;
122 task->process->argv = argv;
123 task->process->path = argv[0];
126 rc = process_run_async(task->process);
128 process_release(task->process);
129 task->process = NULL;
131 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
133 rc = process_run_sync(task->process);
134 if (rc || !process_exit_ok(task->process))
135 task->result->status = LOAD_ERROR;
137 task->result->status = LOAD_OK;
138 process_release(task->process);
139 task->process = NULL;
144 * pb_load_nfs - Create a mountpoint, set the local file within that
145 * mountpoint, and run the appropriate mount command
148 static void load_nfs(struct load_task *task)
150 char *mountpoint, *opts;
152 const char *argv[] = {
153 pb_system_apps.mount,
158 NULL, /* 6: mountpoint */
162 task->result->status = LOAD_ERROR;
163 mountpoint = local_name(task->result);
166 task->result->cleanup_local = true;
167 argv[6] = mountpoint;
169 rc = pb_mkdir_recursive(mountpoint);
173 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
177 opts = talloc_asprintf_append(opts, ",port=%s",
180 task->result->local = talloc_asprintf(task->result, "%s/%s",
184 task->process->path = pb_system_apps.mount;
185 task->process->argv = argv;
188 rc = process_run_async(task->process);
190 process_release(task->process);
191 task->process = NULL;
193 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
195 rc = process_run_sync(task->process);
196 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
197 process_release(task->process);
198 task->process = NULL;
204 static void load_sftp(struct load_task *task)
206 const char *argv[] = {
208 NULL, /* 1: host:path */
209 NULL, /* 2: local file */
213 argv[1] = talloc_asprintf(task, "%s:%s",
214 task->url->host, task->url->path);
215 load_process_to_local_file(task, argv, 2);
218 static enum tftp_type check_tftp_type(void *ctx)
220 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
221 struct process *process;
224 process = process_create(ctx);
225 process->path = pb_system_apps.tftp;
226 process->argv = argv;
227 process->keep_stdout = true;
228 process->add_stderr = true;
229 process_run_sync(process);
231 if (!process->stdout_buf || process->stdout_len == 0) {
232 pb_log("Can't check TFTP client type!\n");
233 type = TFTP_TYPE_BROKEN;
235 } else if (memmem(process->stdout_buf, process->stdout_len,
236 "tftp-hpa", strlen("tftp-hpa"))) {
237 pb_debug("Found TFTP client type: tftp-hpa\n");
238 type = TFTP_TYPE_HPA;
240 } else if (memmem(process->stdout_buf, process->stdout_len,
241 "BusyBox", strlen("BusyBox"))) {
242 pb_debug("Found TFTP client type: BusyBox tftp\n");
243 type = TFTP_TYPE_BUSYBOX;
246 pb_log("Unknown TFTP client type!\n");
247 type = TFTP_TYPE_BROKEN;
250 process_release(process);
254 static void load_tftp(struct load_task *task)
256 const char *port = "69";
257 const char *argv[10] = {
262 port = task->url->port;
264 if (tftp_type == TFTP_TYPE_UNKNOWN)
265 tftp_type = check_tftp_type(task);
267 if (tftp_type == TFTP_TYPE_BUSYBOX) {
270 argv[3] = NULL; /* 3: local file */
272 argv[5] = task->url->path;
273 argv[6] = task->url->host;
277 load_process_to_local_file(task, argv, 3);
279 } else if (tftp_type == TFTP_TYPE_HPA) {
282 argv[3] = task->url->host;
286 argv[7] = task->url->path;
287 argv[8] = NULL; /* 8: local file */
289 load_process_to_local_file(task, argv, 8);
292 task->result->status = LOAD_ERROR;
297 wget_no_check_certificate = 1,
301 * pb_load_wget - Loads a remote file via wget and returns the local file path.
303 * Returns the local file path in a talloc'ed character string on success,
307 static void load_wget(struct load_task *task, int flags)
309 const char *argv[] = {
312 NULL, /* 2: local file */
321 argv[i++] = "--quiet";
323 if (flags & wget_no_check_certificate)
324 argv[i++] = "--no-check-certificate";
326 argv[i] = task->url->full;
328 load_process_to_local_file(task, argv, 2);
331 /* Although we don't need to load anything for a local path (we just return
332 * the path from the file:// URL), the other load helpers will error-out on
333 * non-existant files. So, do the same here with an access() check on the local
336 static void load_local(struct load_task *task)
340 rc = access(task->url->path, F_OK);
342 task->result->status = LOAD_ERROR;
344 task->result->local = talloc_strdup(task->result,
346 task->result->status = LOAD_OK;
351 * load_url - Loads a (possibly) remote URL and returns the local file
353 * @ctx: The talloc context to associate with the returned string.
354 * @url: The remote file URL.
355 * @tempfile: An optional variable pointer to be set when a temporary local
357 * @url_cb: An optional callback pointer if the caller wants to load url
360 * Returns the local file path in a talloc'ed character string on success,
364 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
365 load_url_complete async_cb, void *async_data)
367 struct load_url_result *result;
368 struct load_task *task;
373 task = talloc_zero(ctx, struct load_task);
375 task->async = async_cb != NULL;
376 task->result = talloc_zero(ctx, struct load_url_result);
377 task->result->task = task;
378 task->process = process_create(task);
380 task->async_cb = async_cb;
381 task->async_data = async_data;
382 task->process->exit_cb = load_url_process_exit;
383 task->process->data = task;
386 switch (url->scheme) {
392 load_wget(task, wget_no_check_certificate);
408 result = task->result;
409 if (result->status == LOAD_ERROR) {
410 load_url_result_cleanup_local(task->result);
422 struct load_url_result *load_url(void *ctx, struct pb_url *url)
424 return load_url_async(ctx, url, NULL, NULL);
427 void load_url_async_cancel(struct load_url_result *res)
429 struct load_task *task = res->task;
431 /* the completion callback may have already been called; this clears
436 if (res->status == LOAD_CANCELLED)
440 assert(task->process);
442 res->status = LOAD_CANCELLED;
443 process_stop_async(task->process);