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