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