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