]> git.ozlabs.org Git - petitboot/blob - discover/paths.c
c0e2b0638a2e59f8c1d84616620f0ae167741842
[petitboot] / discover / paths.c
1 #if defined(HAVE_CONFIG_H)
2 #include "config.h"
3 #endif
4
5 #include <assert.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 #include <talloc/talloc.h>
13 #include <system/system.h>
14 #include <process/process.h>
15 #include <url/url.h>
16 #include <log/log.h>
17 #include "i18n/i18n.h"
18
19 #include "paths.h"
20 #include "device-handler.h"
21 #include "sysinfo.h"
22
23 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
24
25
26 struct list     pending_network_jobs;
27
28 struct network_job {
29         struct load_task        *task;
30         int                     flags;
31
32         struct list_item        list;
33 };
34
35 struct load_task {
36         struct pb_url           *url;
37         struct process          *process;
38         struct load_url_result  *result;
39         bool                    async;
40         load_url_complete       async_cb;
41         void                    *async_data;
42 };
43
44 const char *mount_base(void)
45 {
46         return DEVICE_MOUNT_BASE;
47 }
48
49 char *join_paths(void *alloc_ctx, const char *a, const char *b)
50 {
51         char *full_path;
52
53         full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
54
55         strcpy(full_path, a);
56         if (b[0] != '/' && a[strlen(a) - 1] != '/')
57                 strcat(full_path, "/");
58         strcat(full_path, b);
59
60         return full_path;
61 }
62
63 #ifndef PETITBOOT_TEST
64
65 #ifdef WITH_BUSYBOX
66 static inline bool have_busybox(void) { return true; }
67 #else
68 static inline bool have_busybox(void) { return false; }
69 #endif
70
71 static char *local_name(void *ctx)
72 {
73         char *ret, tmp[] = "/tmp/pb-XXXXXX";
74         mode_t oldmask;
75         int fd;
76
77         oldmask = umask(0644);
78         fd = mkstemp(tmp);
79         umask(oldmask);
80
81         if (fd < 0)
82                 return NULL;
83
84         close(fd);
85
86         ret = talloc_strdup(ctx, tmp);
87
88         return ret;
89 }
90
91 static void load_url_result_cleanup_local(struct load_url_result *result)
92 {
93         if (result->cleanup_local)
94                 unlink(result->local);
95 }
96
97 static void load_url_process_exit(struct process *process)
98 {
99         struct load_task *task = process->data;
100         struct load_url_result *result;
101         load_url_complete cb;
102         void *data;
103
104         pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
105                         process->path, process->pid, task->url->full,
106                         process->exit_status);
107
108         result = task->result;
109         data = task->async_data;
110         cb = task->async_cb;
111
112         if (result->status == LOAD_CANCELLED) {
113                 load_url_result_cleanup_local(result);
114         } else if (process_exit_ok(process)) {
115                 result->status = LOAD_OK;
116         } else {
117                 result->status = LOAD_ERROR;
118                 load_url_result_cleanup_local(result);
119                 pb_debug("Download client stdout buffer:\n%s\n",
120                                 process->stdout_buf);
121         }
122
123         if (result->status == LOAD_OK && process->stdout_data)
124                 device_handler_status_info(process->stdout_data,
125                                 _("Download complete: %s"), task->url->file);
126
127         /* The load callback may well free the ctx, which was the
128          * talloc parent of the task. Therefore, we want to do our cleanup
129          * before invoking it
130          */
131         process_release(process);
132         talloc_free(task);
133         result->task = NULL;
134
135         cb(result, data);
136 }
137
138 /*
139  * Callback to retrieve progress information from Busybox utilities.
140  * Busybox utilities use a common progress bar format which progress percentage
141  * and current size can be can be parsed from.
142  */
143 static int busybox_progress_cb(void *arg)
144 {
145         const char *busybox_fmt = "%*s %u%*[%* |]%u%c %*u:%*u:%*u ETA\n";
146         struct process_info *procinfo = arg;
147         char *n, *s, suffix, *line = NULL;
148         struct device_handler *handler;
149         unsigned int percentage, size;
150         struct process *p;
151         int rc;
152
153         if (!arg)
154                 return -1;
155
156         p = procinfo_get_process(procinfo);
157         handler = p->stdout_data;
158
159         rc = process_stdout_custom(procinfo, &line);
160
161         if (rc) {
162                 /* Unregister ourselves from progress tracking */
163                 device_handler_status_download_remove(handler, procinfo);
164         }
165
166         if (rc || !line)
167                 return rc;
168
169         rc = sscanf(line, busybox_fmt, &percentage, &size, &suffix);
170
171         /*
172          * Many unrecognised lines are partial updates. If we see a partial
173          * line with a newline character, see if we can match a valid line
174          * at the end of stdout_buf
175          */
176         if (rc != 3) {
177                 n = strchr(line, '\n');
178                 if (n)
179                         for (s = n - 1; s >= p->stdout_buf; s--)
180                                 if (*s == '\n') {
181                                         rc = sscanf(s + 1, busybox_fmt,
182                                                 &percentage, &size, &suffix);
183                                         break;
184                                 }
185         }
186
187         if (rc != 3) {
188                 percentage = size = 0;
189                 suffix = ' ';
190         }
191
192         device_handler_status_download(handler, procinfo,
193                         percentage, size, suffix);
194
195         return 0;
196 }
197
198
199
200 static void load_process_to_local_file(struct load_task *task,
201                 const char **argv, int argv_local_idx)
202 {
203         int rc;
204
205         task->result->local = local_name(task->result);
206         if (!task->result->local) {
207                 task->result->status = LOAD_ERROR;
208                 return;
209         }
210         task->result->cleanup_local = true;
211
212         if (argv_local_idx)
213                 argv[argv_local_idx] = task->result->local;
214
215         task->process->argv = argv;
216         task->process->path = argv[0];
217
218         if (task->async) {
219                 rc = process_run_async(task->process);
220                 if (rc) {
221                         process_release(task->process);
222                         task->process = NULL;
223                 }
224                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
225         } else {
226                 rc = process_run_sync(task->process);
227                 if (rc || !process_exit_ok(task->process))
228                         task->result->status = LOAD_ERROR;
229                 else
230                         task->result->status = LOAD_OK;
231                 process_release(task->process);
232                 task->process = NULL;
233         }
234 }
235
236 /**
237  * pb_load_nfs - Create a mountpoint, set the local file within that
238  * mountpoint, and run the appropriate mount command
239  */
240
241 static void load_nfs(struct load_task *task)
242 {
243         char *mountpoint, *opts;
244         int rc;
245         const char *argv[] = {
246                         pb_system_apps.mount,
247                         "-t", "nfs",
248                         NULL,                   /* 3: opts */
249                         task->url->host,
250                         task->url->dir,
251                         NULL,                   /* 6: mountpoint */
252                         NULL,
253         };
254
255         task->result->status = LOAD_ERROR;
256         mountpoint = local_name(task->result);
257         if (!mountpoint)
258                 return;
259         task->result->cleanup_local = true;
260         argv[6] = mountpoint;
261
262         rc = pb_mkdir_recursive(mountpoint);
263         if (rc)
264                 return;
265
266         opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
267         argv[3] = opts;
268
269         if (task->url->port)
270                 opts = talloc_asprintf_append(opts, ",port=%s",
271                                                 task->url->port);
272
273         task->result->local = talloc_asprintf(task->result, "%s/%s",
274                                                         mountpoint,
275                                                         task->url->path);
276
277         task->process->path = pb_system_apps.mount;
278         task->process->argv = argv;
279
280         if (task->async) {
281                 rc = process_run_async(task->process);
282                 if (rc) {
283                         process_release(task->process);
284                         task->process = NULL;
285                 }
286                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
287         } else {
288                 rc = process_run_sync(task->process);
289                 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
290                 process_release(task->process);
291                 task->process = NULL;
292         }
293
294         talloc_free(opts);
295 }
296
297 static void load_sftp(struct load_task *task)
298 {
299         const char *argv[] = {
300                         pb_system_apps.sftp,
301                         NULL,           /* 1: host:path */
302                         NULL,           /* 2: local file */
303                         NULL,
304         };
305
306         argv[1] = talloc_asprintf(task, "%s:%s",
307                                 task->url->host, task->url->path);
308         load_process_to_local_file(task, argv, 2);
309 }
310
311 static enum tftp_type check_tftp_type(void *ctx)
312 {
313         const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
314         struct process *process;
315         enum tftp_type type;
316         int rc;
317
318         process = process_create(ctx);
319         process->path = pb_system_apps.tftp;
320         process->argv = argv;
321         process->keep_stdout = true;
322         process->add_stderr = true;
323         rc = process_run_sync(process);
324
325         if (rc || !process->stdout_buf || process->stdout_len == 0) {
326                 pb_log("Can't check TFTP client type!\n");
327                 type = TFTP_TYPE_BROKEN;
328
329         } else if (memmem(process->stdout_buf, process->stdout_len,
330                                 "tftp-hpa", strlen("tftp-hpa"))) {
331                 pb_debug("Found TFTP client type: tftp-hpa\n");
332                 type = TFTP_TYPE_HPA;
333
334         } else if (memmem(process->stdout_buf, process->stdout_len,
335                                 "BusyBox", strlen("BusyBox"))) {
336                 pb_debug("Found TFTP client type: BusyBox tftp\n");
337                 type = TFTP_TYPE_BUSYBOX;
338
339         } else {
340                 pb_log("Unknown TFTP client type!\n");
341                 type = TFTP_TYPE_BROKEN;
342         }
343
344         process_release(process);
345         return type;
346 }
347
348 static void load_tftp(struct load_task *task)
349 {
350         const char *port = "69";
351         const char *argv[10] = {
352                 pb_system_apps.tftp,
353         };
354
355         if (task->url->port)
356                 port = task->url->port;
357
358         if (tftp_type == TFTP_TYPE_UNKNOWN)
359                 tftp_type = check_tftp_type(task);
360
361         if (tftp_type == TFTP_TYPE_BUSYBOX) {
362                 argv[1] = "-g";
363                 argv[2] = "-l";
364                 argv[3] = NULL; /* 3: local file */
365                 argv[4] = "-r";
366                 argv[5] = task->url->path;
367                 argv[6] = task->url->host;
368                 argv[7] = port;
369                 argv[8] = NULL;
370
371                 load_process_to_local_file(task, argv, 3);
372
373         } else if (tftp_type == TFTP_TYPE_HPA) {
374                 argv[1] = "-m";
375                 argv[2] = "binary";
376                 argv[3] = task->url->host;
377                 argv[4] = port;
378                 argv[5] = "-c";
379                 argv[6] = "get";
380                 argv[7] = task->url->path;
381                 argv[8] = NULL; /* 8: local file */
382                 argv[9] = NULL;
383                 load_process_to_local_file(task, argv, 8);
384
385         } else
386                 task->result->status = LOAD_ERROR;
387 }
388
389 enum wget_flags {
390         wget_empty                      = 0x1,
391         wget_no_check_certificate       = 0x2,
392         wget_verbose                    = 0x4,
393 };
394
395 /**
396  * pb_load_wget - Loads a remote file via wget and returns the local file path.
397  *
398  * Returns the local file path in a talloc'ed character string on success,
399  * or NULL on error.
400  */
401
402 static void load_wget(struct load_task *task, int flags)
403 {
404         const char *argv[] = {
405                 pb_system_apps.wget,
406                 "-O",
407                 NULL, /* 2: local file */
408                 NULL, /* 3 (optional): --quiet */
409                 NULL, /* 4 (optional): --no-check-certificate */
410                 NULL, /* 5: URL */
411                 NULL,
412         };
413         int i;
414
415         if (task->process->stdout_cb)
416                 flags |= wget_verbose;
417
418         i = 3;
419 #if defined(DEBUG)
420         flags |= wget_verbose;
421 #endif
422         if ((flags & wget_verbose) == 0)
423                 argv[i++] = "--quiet";
424
425         if (flags & wget_no_check_certificate)
426                 argv[i++] = "--no-check-certificate";
427
428         argv[i] = task->url->full;
429
430         load_process_to_local_file(task, argv, 2);
431 }
432
433 /* Although we don't need to load anything for a local path (we just return
434  * the path from the file:// URL), the other load helpers will error-out on
435  * non-existant files. So, do the same here with an access() check on the local
436  * filename.
437  */
438 static void load_local(struct load_task *task)
439 {
440         struct load_url_result *result = task->result;
441         int rc;
442
443         rc = access(task->url->path, F_OK);
444         if (rc) {
445                 result->status = LOAD_ERROR;
446         } else {
447                 result->local = talloc_strdup(task->result, task->url->path);
448                 result->status = LOAD_OK;
449         }
450
451         task->async_cb(task->result, task->async_data);
452 }
453
454 static void load_url_async_start_pending(struct load_task *task, int flags)
455 {
456         pb_log("Starting pending job for %s\n", task->url->full);
457
458         switch (task->url->scheme) {
459         case pb_url_ftp:
460         case pb_url_http:
461                 load_wget(task, flags);
462                 break;
463         case pb_url_https:
464                 flags |= wget_no_check_certificate;
465                 load_wget(task, flags);
466                 break;
467         case pb_url_nfs:
468                 load_nfs(task);
469                 break;
470         case pb_url_sftp:
471                 load_sftp(task);
472                 break;
473         case pb_url_tftp:
474                 load_tftp(task);
475                 break;
476         default:
477                 /* Shouldn't be a need via this path but.. */
478                 load_local(task);
479                 break;
480         }
481
482         if (task->result->status == LOAD_ERROR) {
483                 pb_log("Pending job failed for %s\n", task->url->full);
484                 load_url_result_cleanup_local(task->result);
485                 talloc_free(task->result);
486                 talloc_free(task);
487         }
488 }
489
490 void pending_network_jobs_start(void)
491 {
492         struct network_job *job, *tmp;
493
494         if (!pending_network_jobs.head.next)
495                 return;
496
497         list_for_each_entry_safe(&pending_network_jobs, job, tmp, list) {
498                 load_url_async_start_pending(job->task, job->flags);
499                 list_remove(&job->list);
500         }
501 }
502
503 void pending_network_jobs_cancel(void)
504 {
505         struct network_job *job, *tmp;
506
507         if (!pending_network_jobs.head.next)
508                 return;
509
510         list_for_each_entry_safe(&pending_network_jobs, job, tmp, list)
511                 talloc_free(job);
512         list_init(&pending_network_jobs);
513 }
514
515 static void pending_network_jobs_add(struct load_task *task, int flags)
516 {
517         struct network_job *job;
518
519         if (!pending_network_jobs.head.next)
520                 list_init(&pending_network_jobs);
521
522         job = talloc(task, struct network_job);
523         if (!job) {
524                 pb_log("Failed to allocate space for pending job\n");
525                 return;
526         }
527
528         job->task = task;
529         job->flags = flags;
530         list_add_tail(&pending_network_jobs, &job->list);
531 }
532
533
534 /**
535  * load_url - Loads a (possibly) remote URL and returns the local file
536  * path.
537  * @ctx: The talloc context to associate with the returned string.
538  * @url: The remote file URL.
539  * @tempfile: An optional variable pointer to be set when a temporary local
540  *  file is created.
541  * @url_cb: An optional callback pointer if the caller wants to load url
542  *  asynchronously.
543  *
544  * Returns the local file path in a talloc'ed character string on success,
545  * or NULL on error.
546  */
547
548 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
549                 load_url_complete async_cb, void *async_data,
550                 waiter_cb stdout_cb, void *stdout_data)
551 {
552         struct load_url_result *result;
553         struct load_task *task;
554         int flags = 0;
555
556         if (!url)
557                 return NULL;
558
559         task = talloc_zero(ctx, struct load_task);
560         task->url = url;
561         task->async = async_cb != NULL;
562         task->result = talloc_zero(ctx, struct load_url_result);
563         task->result->task = task;
564         task->result->url = url;
565         task->process = process_create(task);
566         if (task->async) {
567                 task->async_cb = async_cb;
568                 task->async_data = async_data;
569                 task->process->exit_cb = load_url_process_exit;
570                 task->process->data = task;
571                 task->process->stdout_cb = stdout_cb;
572                 task->process->stdout_data = stdout_data;
573         }
574
575         if (!stdout_cb && stdout_data && have_busybox())
576                 task->process->stdout_cb = busybox_progress_cb;
577
578         /* Make sure we save output for any task that has a custom handler */
579         if (task->process->stdout_cb) {
580                 task->process->add_stderr = true;
581                 task->process->keep_stdout = true;
582         }
583
584         /* If the url is remote but network is not yet available queue up this
585          * load for later */
586         if (!system_info_network_available() && url->scheme != pb_url_file) {
587                 pb_log("load task for %s queued pending network\n", url->full);
588                 pending_network_jobs_add(task, flags);
589                 task->result->status = LOAD_ASYNC;
590                 return task->result;
591         }
592
593         switch (url->scheme) {
594         case pb_url_ftp:
595         case pb_url_http:
596                 load_wget(task, flags);
597                 break;
598         case pb_url_https:
599                 flags |= wget_no_check_certificate;
600                 load_wget(task, flags);
601                 break;
602         case pb_url_nfs:
603                 load_nfs(task);
604                 break;
605         case pb_url_sftp:
606                 load_sftp(task);
607                 break;
608         case pb_url_tftp:
609                 load_tftp(task);
610                 break;
611         default:
612                 load_local(task);
613                 break;
614         }
615
616         result = task->result;
617         if (result->status == LOAD_ERROR) {
618                 load_url_result_cleanup_local(task->result);
619                 talloc_free(result);
620                 talloc_free(task);
621                 return NULL;
622         }
623
624         if (!task->async || result->status == LOAD_OK)
625                 talloc_free(task);
626
627         return result;
628 }
629
630 struct load_url_result *load_url(void *ctx, struct pb_url *url)
631 {
632         return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
633 }
634
635 void load_url_async_cancel(struct load_url_result *res)
636 {
637         struct load_task *task = res->task;
638
639         /* the completion callback may have already been called; this clears
640          * res->task */
641         if (!task)
642                 return;
643
644         if (res->status == LOAD_CANCELLED)
645                 return;
646
647         assert(task->async);
648         assert(task->process);
649
650         res->status = LOAD_CANCELLED;
651         process_stop_async(task->process);
652 }
653
654 #else
655
656 static void __attribute__((unused)) load_local(
657                 struct load_task *task __attribute__((unused)))
658 {
659 }
660 static void __attribute__((unused)) load_wget(
661                 struct load_task *task __attribute__((unused)),
662                 int flags __attribute__((unused)))
663 {
664 }
665 static void __attribute__((unused)) load_tftp(
666                 struct load_task *task __attribute__((unused)))
667 {
668 }
669 static void __attribute__((unused)) load_sftp(
670                 struct load_task *task __attribute__((unused)))
671 {
672 }
673 static void __attribute__((unused)) load_nfs(
674                 struct load_task *task __attribute__((unused)))
675 {
676 }
677 static void __attribute__((unused)) load_url_process_exit(
678                 struct process *process __attribute__((unused)))
679 {
680 }
681
682 #endif