]> git.ozlabs.org Git - petitboot/blob - discover/paths.c
discover/yaboot: Clear globals_done when we see an image definition
[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
10 #include <talloc/talloc.h>
11 #include <system/system.h>
12 #include <process/process.h>
13 #include <url/url.h>
14 #include <log/log.h>
15
16 #include "paths.h"
17
18 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
19
20 struct load_task {
21         struct pb_url           *url;
22         struct process          *process;
23         struct load_url_result  *result;
24         bool                    async;
25         load_url_complete       async_cb;
26         void                    *async_data;
27 };
28
29 const char *mount_base(void)
30 {
31         return DEVICE_MOUNT_BASE;
32 }
33
34 char *join_paths(void *alloc_ctx, const char *a, const char *b)
35 {
36         char *full_path;
37
38         full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
39
40         strcpy(full_path, a);
41         if (b[0] != '/' && a[strlen(a) - 1] != '/')
42                 strcat(full_path, "/");
43         strcat(full_path, b);
44
45         return full_path;
46 }
47
48
49 static char *local_name(void *ctx)
50 {
51         char *ret, tmp[] = "/tmp/pb-XXXXXX";
52         int fd;
53
54         fd = mkstemp(tmp);
55
56         if (fd < 0)
57                 return NULL;
58
59         close(fd);
60
61         ret = talloc_strdup(ctx, tmp);
62
63         return ret;
64 }
65
66 static void load_url_result_cleanup_local(struct load_url_result *result)
67 {
68         if (result->cleanup_local)
69                 unlink(result->local);
70 }
71
72 static void load_url_process_exit(struct process *process)
73 {
74         struct load_task *task = process->data;
75         struct load_url_result *result;
76         load_url_complete cb;
77         void *data;
78
79         pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
80                         process->path, process->pid, task->url->full,
81                         process->exit_status);
82
83         result = task->result;
84         data = task->async_data;
85         cb = task->async_cb;
86
87         if (result->status == LOAD_CANCELLED) {
88                 load_url_result_cleanup_local(result);
89         } else if (process->exit_status == 0) {
90                 result->status = LOAD_OK;
91         } else {
92                 result->status = LOAD_ERROR;
93                 load_url_result_cleanup_local(result);
94         }
95
96         /* The load callback may well free the ctx, which was the
97          * talloc parent of the task. Therefore, we want to do our cleanup
98          * before invoking it
99          */
100         process_release(process);
101         talloc_free(task);
102         result->task = NULL;
103
104         cb(result, data);
105 }
106
107 static void load_process_to_local_file(struct load_task *task,
108                 const char **argv, int argv_local_idx)
109 {
110         int rc;
111
112         task->result->local = local_name(task->result);
113         if (!task->result->local) {
114                 task->result->status = LOAD_ERROR;
115                 return;
116         }
117         task->result->cleanup_local = true;
118
119         if (argv_local_idx)
120                 argv[argv_local_idx] = task->result->local;
121
122         task->process->argv = argv;
123         task->process->path = argv[0];
124
125         if (task->async) {
126                 rc = process_run_async(task->process);
127                 if (rc) {
128                         process_release(task->process);
129                         task->process = NULL;
130                 }
131                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
132         } else {
133                 rc = process_run_sync(task->process);
134                 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
135                 process_release(task->process);
136                 task->process = NULL;
137         }
138 }
139
140 /**
141  * pb_load_nfs - Create a mountpoint, set the local file within that
142  * mountpoint, and run the appropriate mount command
143  */
144
145 static void load_nfs(struct load_task *task)
146 {
147         char *mountpoint, *opts;
148         int rc;
149         const char *argv[] = {
150                         pb_system_apps.mount,
151                         "-t", "nfs",
152                         NULL,                   /* 3: opts */
153                         task->url->host,
154                         task->url->dir,
155                         NULL,                   /* 6: mountpoint */
156                         NULL,
157         };
158
159         task->result->status = LOAD_ERROR;
160         mountpoint = local_name(task->result);
161         if (!mountpoint)
162                 return;
163         task->result->cleanup_local = true;
164         argv[6] = mountpoint;
165
166         rc = pb_mkdir_recursive(mountpoint);
167         if (rc)
168                 return;
169
170         opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
171         argv[3] = opts;
172
173         if (task->url->port)
174                 opts = talloc_asprintf_append(opts, ",port=%s",
175                                                 task->url->port);
176
177         task->result->local = talloc_asprintf(task->result, "%s/%s",
178                                                         mountpoint,
179                                                         task->url->path);
180
181         task->process->path = pb_system_apps.mount;
182         task->process->argv = argv;
183
184         if (task->async) {
185                 rc = process_run_async(task->process);
186                 if (rc) {
187                         process_release(task->process);
188                         task->process = NULL;
189                 }
190                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
191         } else {
192                 rc = process_run_sync(task->process);
193                 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
194                 process_release(task->process);
195                 task->process = NULL;
196         }
197
198         talloc_free(opts);
199 }
200
201 static void load_sftp(struct load_task *task)
202 {
203         const char *argv[] = {
204                         pb_system_apps.sftp,
205                         NULL,           /* 1: host:path */
206                         NULL,           /* 2: local file */
207                         NULL,
208         };
209
210         argv[1] = talloc_asprintf(task, "%s:%s",
211                                 task->url->host, task->url->path);
212         load_process_to_local_file(task, argv, 2);
213 }
214
215 static enum tftp_type check_tftp_type(void *ctx)
216 {
217         const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
218         struct process *process;
219         enum tftp_type type;
220
221         process = process_create(ctx);
222         process->path = pb_system_apps.tftp;
223         process->argv = argv;
224         process->keep_stdout = true;
225         process_run_sync(process);
226
227         if (!process->stdout_buf || process->stdout_len == 0) {
228                 pb_log("Can't check TFTP client type!\n");
229                 type = TFTP_TYPE_BROKEN;
230
231         } else if (memmem(process->stdout_buf, process->stdout_len,
232                                 "tftp-hpa", strlen("tftp-hpa"))) {
233                 pb_debug("Found TFTP client type: tftp-hpa\n");
234                 type = TFTP_TYPE_HPA;
235
236         } else if (memmem(process->stdout_buf, process->stdout_len,
237                                 "BusyBox", strlen("BusyBox"))) {
238                 pb_debug("Found TFTP client type: BusyBox tftp\n");
239                 type = TFTP_TYPE_BUSYBOX;
240
241         } else {
242                 pb_log("Unknown TFTP client type!\n");
243                 type = TFTP_TYPE_BROKEN;
244         }
245
246         process_release(process);
247         return type;
248 }
249
250 static void load_tftp(struct load_task *task)
251 {
252         const char *port = "69";
253         const char *argv[10] = {
254                 pb_system_apps.tftp,
255         };
256
257         if (task->url->port)
258                 port = task->url->port;
259
260         if (tftp_type == TFTP_TYPE_UNKNOWN)
261                 tftp_type = check_tftp_type(task);
262
263         if (tftp_type == TFTP_TYPE_BUSYBOX) {
264                 argv[1] = "-g";
265                 argv[2] = "-l";
266                 argv[3] = NULL; /* 3: local file */
267                 argv[4] = "-r";
268                 argv[5] = task->url->path;
269                 argv[6] = task->url->host;
270                 argv[7] = port;
271                 argv[8] = NULL;
272
273                 load_process_to_local_file(task, argv, 3);
274
275         } else if (tftp_type == TFTP_TYPE_HPA) {
276                 argv[1] = "-m";
277                 argv[2] = "binary";
278                 argv[3] = task->url->host;
279                 argv[4] = port;
280                 argv[5] = "-c";
281                 argv[6] = "get";
282                 argv[7] = task->url->path;
283                 argv[8] = NULL; /* 8: local file */
284                 argv[9] = NULL;
285                 load_process_to_local_file(task, argv, 8);
286
287         } else
288                 task->result->status = LOAD_ERROR;
289 }
290
291 enum wget_flags {
292         wget_empty = 0,
293         wget_no_check_certificate = 1,
294 };
295
296 /**
297  * pb_load_wget - Loads a remote file via wget and returns the local file path.
298  *
299  * Returns the local file path in a talloc'ed character string on success,
300  * or NULL on error.
301  */
302
303 static void load_wget(struct load_task *task, int flags)
304 {
305         const char *argv[] = {
306                 pb_system_apps.wget,
307                 "-O",
308                 NULL, /* 2: local file */
309                 NULL,
310                 NULL,
311                 NULL,
312         };
313         int i;
314
315         i = 3;
316 #if !defined(DEBUG)
317         argv[i++] = "--quiet";
318 #endif
319         if (flags & wget_no_check_certificate)
320                 argv[i++] = "--no-check-certificate";
321
322         argv[i] = task->url->full;
323
324         load_process_to_local_file(task, argv, 2);
325 }
326
327 /* Although we don't need to load anything for a local path (we just return
328  * the path from the file:// URL), the other load helpers will error-out on
329  * non-existant files. So, do the same here with an access() check on the local
330  * filename.
331  */
332 static void load_local(struct load_task *task)
333 {
334         int rc;
335
336         rc = access(task->url->path, F_OK);
337         if (rc) {
338                 task->result->status = LOAD_ERROR;
339         } else {
340                 task->result->local = talloc_strdup(task->result,
341                                                     task->url->path);
342                 task->result->status = LOAD_OK;
343         }
344 }
345
346 /**
347  * load_url - Loads a (possibly) remote URL and returns the local file
348  * path.
349  * @ctx: The talloc context to associate with the returned string.
350  * @url: The remote file URL.
351  * @tempfile: An optional variable pointer to be set when a temporary local
352  *  file is created.
353  * @url_cb: An optional callback pointer if the caller wants to load url
354  *  asynchronously.
355  *
356  * Returns the local file path in a talloc'ed character string on success,
357  * or NULL on error.
358  */
359
360 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
361                 load_url_complete async_cb, void *async_data)
362 {
363         struct load_url_result *result;
364         struct load_task *task;
365
366         if (!url)
367                 return NULL;
368
369         task = talloc_zero(ctx, struct load_task);
370         task->url = url;
371         task->async = async_cb != NULL;
372         task->result = talloc_zero(ctx, struct load_url_result);
373         task->result->task = task;
374         task->process = process_create(task);
375         if (task->async) {
376                 task->async_cb = async_cb;
377                 task->async_data = async_data;
378                 task->process->exit_cb = load_url_process_exit;
379                 task->process->data = task;
380         }
381
382         switch (url->scheme) {
383         case pb_url_ftp:
384         case pb_url_http:
385                 load_wget(task, 0);
386                 break;
387         case pb_url_https:
388                 load_wget(task, wget_no_check_certificate);
389                 break;
390         case pb_url_nfs:
391                 load_nfs(task);
392                 break;
393         case pb_url_sftp:
394                 load_sftp(task);
395                 break;
396         case pb_url_tftp:
397                 load_tftp(task);
398                 break;
399         default:
400                 load_local(task);
401                 break;
402         }
403
404         result = task->result;
405         if (result->status == LOAD_ERROR) {
406                 load_url_result_cleanup_local(task->result);
407                 talloc_free(result);
408                 talloc_free(task);
409                 return NULL;
410         }
411
412         if (!task->async)
413                 talloc_free(task);
414
415         return result;
416 }
417
418 struct load_url_result *load_url(void *ctx, struct pb_url *url)
419 {
420         return load_url_async(ctx, url, NULL, NULL);
421 }
422
423 void load_url_async_cancel(struct load_url_result *res)
424 {
425         struct load_task *task = res->task;
426
427         /* the completion callback may have already been called; this clears
428          * res->task */
429         if (!task)
430                 return;
431
432         if (res->status == LOAD_CANCELLED)
433                 return;
434
435         assert(task->async);
436         assert(task->process);
437
438         res->status = LOAD_CANCELLED;
439         process_stop_async(task->process);
440 }