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, "/");
51 static char *local_name(void *ctx)
53 char *ret, tmp[] = "/tmp/pb-XXXXXX";
57 oldmask = umask(0644);
66 ret = talloc_strdup(ctx, tmp);
71 static void load_url_result_cleanup_local(struct load_url_result *result)
73 if (result->cleanup_local)
74 unlink(result->local);
77 static void load_url_process_exit(struct process *process)
79 struct load_task *task = process->data;
80 struct load_url_result *result;
84 pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
85 process->path, process->pid, task->url->full,
86 process->exit_status);
88 result = task->result;
89 data = task->async_data;
92 if (result->status == LOAD_CANCELLED) {
93 load_url_result_cleanup_local(result);
94 } else if (process_exit_ok(process)) {
95 result->status = LOAD_OK;
97 result->status = LOAD_ERROR;
98 load_url_result_cleanup_local(result);
101 /* The load callback may well free the ctx, which was the
102 * talloc parent of the task. Therefore, we want to do our cleanup
105 process_release(process);
112 static void load_process_to_local_file(struct load_task *task,
113 const char **argv, int argv_local_idx)
117 task->result->local = local_name(task->result);
118 if (!task->result->local) {
119 task->result->status = LOAD_ERROR;
122 task->result->cleanup_local = true;
125 argv[argv_local_idx] = task->result->local;
127 task->process->argv = argv;
128 task->process->path = argv[0];
131 rc = process_run_async(task->process);
133 process_release(task->process);
134 task->process = NULL;
136 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
138 rc = process_run_sync(task->process);
139 if (rc || !process_exit_ok(task->process))
140 task->result->status = LOAD_ERROR;
142 task->result->status = LOAD_OK;
143 process_release(task->process);
144 task->process = NULL;
149 * pb_load_nfs - Create a mountpoint, set the local file within that
150 * mountpoint, and run the appropriate mount command
153 static void load_nfs(struct load_task *task)
155 char *mountpoint, *opts;
157 const char *argv[] = {
158 pb_system_apps.mount,
163 NULL, /* 6: mountpoint */
167 task->result->status = LOAD_ERROR;
168 mountpoint = local_name(task->result);
171 task->result->cleanup_local = true;
172 argv[6] = mountpoint;
174 rc = pb_mkdir_recursive(mountpoint);
178 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
182 opts = talloc_asprintf_append(opts, ",port=%s",
185 task->result->local = talloc_asprintf(task->result, "%s/%s",
189 task->process->path = pb_system_apps.mount;
190 task->process->argv = argv;
193 rc = process_run_async(task->process);
195 process_release(task->process);
196 task->process = NULL;
198 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
200 rc = process_run_sync(task->process);
201 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
202 process_release(task->process);
203 task->process = NULL;
209 static void load_sftp(struct load_task *task)
211 const char *argv[] = {
213 NULL, /* 1: host:path */
214 NULL, /* 2: local file */
218 argv[1] = talloc_asprintf(task, "%s:%s",
219 task->url->host, task->url->path);
220 load_process_to_local_file(task, argv, 2);
223 static enum tftp_type check_tftp_type(void *ctx)
225 const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
226 struct process *process;
230 process = process_create(ctx);
231 process->path = pb_system_apps.tftp;
232 process->argv = argv;
233 process->keep_stdout = true;
234 process->add_stderr = true;
235 rc = process_run_sync(process);
237 if (rc || !process->stdout_buf || process->stdout_len == 0) {
238 pb_log("Can't check TFTP client type!\n");
239 type = TFTP_TYPE_BROKEN;
241 } else if (memmem(process->stdout_buf, process->stdout_len,
242 "tftp-hpa", strlen("tftp-hpa"))) {
243 pb_debug("Found TFTP client type: tftp-hpa\n");
244 type = TFTP_TYPE_HPA;
246 } else if (memmem(process->stdout_buf, process->stdout_len,
247 "BusyBox", strlen("BusyBox"))) {
248 pb_debug("Found TFTP client type: BusyBox tftp\n");
249 type = TFTP_TYPE_BUSYBOX;
252 pb_log("Unknown TFTP client type!\n");
253 type = TFTP_TYPE_BROKEN;
256 process_release(process);
260 static void load_tftp(struct load_task *task)
262 const char *port = "69";
263 const char *argv[10] = {
268 port = task->url->port;
270 if (tftp_type == TFTP_TYPE_UNKNOWN)
271 tftp_type = check_tftp_type(task);
273 if (tftp_type == TFTP_TYPE_BUSYBOX) {
276 argv[3] = NULL; /* 3: local file */
278 argv[5] = task->url->path;
279 argv[6] = task->url->host;
283 load_process_to_local_file(task, argv, 3);
285 } else if (tftp_type == TFTP_TYPE_HPA) {
288 argv[3] = task->url->host;
292 argv[7] = task->url->path;
293 argv[8] = NULL; /* 8: local file */
295 load_process_to_local_file(task, argv, 8);
298 task->result->status = LOAD_ERROR;
303 wget_no_check_certificate = 1,
307 * pb_load_wget - Loads a remote file via wget and returns the local file path.
309 * Returns the local file path in a talloc'ed character string on success,
313 static void load_wget(struct load_task *task, int flags)
315 const char *argv[] = {
318 NULL, /* 2: local file */
319 NULL, /* 3 (optional): --quiet */
320 NULL, /* 4 (optional): --no-check-certificate */
328 argv[i++] = "--quiet";
330 if (flags & wget_no_check_certificate)
331 argv[i++] = "--no-check-certificate";
333 argv[i] = task->url->full;
335 load_process_to_local_file(task, argv, 2);
338 /* Although we don't need to load anything for a local path (we just return
339 * the path from the file:// URL), the other load helpers will error-out on
340 * non-existant files. So, do the same here with an access() check on the local
343 static void load_local(struct load_task *task)
347 rc = access(task->url->path, F_OK);
349 task->result->status = LOAD_ERROR;
351 task->result->local = talloc_strdup(task->result,
353 task->result->status = LOAD_OK;
358 * load_url - Loads a (possibly) remote URL and returns the local file
360 * @ctx: The talloc context to associate with the returned string.
361 * @url: The remote file URL.
362 * @tempfile: An optional variable pointer to be set when a temporary local
364 * @url_cb: An optional callback pointer if the caller wants to load url
367 * Returns the local file path in a talloc'ed character string on success,
371 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
372 load_url_complete async_cb, void *async_data)
374 struct load_url_result *result;
375 struct load_task *task;
380 task = talloc_zero(ctx, struct load_task);
382 task->async = async_cb != NULL;
383 task->result = talloc_zero(ctx, struct load_url_result);
384 task->result->task = task;
385 task->process = process_create(task);
387 task->async_cb = async_cb;
388 task->async_data = async_data;
389 task->process->exit_cb = load_url_process_exit;
390 task->process->data = task;
393 switch (url->scheme) {
399 load_wget(task, wget_no_check_certificate);
415 result = task->result;
416 if (result->status == LOAD_ERROR) {
417 load_url_result_cleanup_local(task->result);
429 struct load_url_result *load_url(void *ctx, struct pb_url *url)
431 return load_url_async(ctx, url, NULL, NULL);
434 void load_url_async_cancel(struct load_url_result *res)
436 struct load_task *task = res->task;
438 /* the completion callback may have already been called; this clears
443 if (res->status == LOAD_CANCELLED)
447 assert(task->process);
449 res->status = LOAD_CANCELLED;
450 process_stop_async(task->process);