]> git.ozlabs.org Git - petitboot/blob - discover/paths.c
discover/ipmi: Use advisory locking on ipmi device
[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_ok(process)) {
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                 if (rc || !process_exit_ok(task->process))
135                         task->result->status = LOAD_ERROR;
136                 else
137                         task->result->status = LOAD_OK;
138                 process_release(task->process);
139                 task->process = NULL;
140         }
141 }
142
143 /**
144  * pb_load_nfs - Create a mountpoint, set the local file within that
145  * mountpoint, and run the appropriate mount command
146  */
147
148 static void load_nfs(struct load_task *task)
149 {
150         char *mountpoint, *opts;
151         int rc;
152         const char *argv[] = {
153                         pb_system_apps.mount,
154                         "-t", "nfs",
155                         NULL,                   /* 3: opts */
156                         task->url->host,
157                         task->url->dir,
158                         NULL,                   /* 6: mountpoint */
159                         NULL,
160         };
161
162         task->result->status = LOAD_ERROR;
163         mountpoint = local_name(task->result);
164         if (!mountpoint)
165                 return;
166         task->result->cleanup_local = true;
167         argv[6] = mountpoint;
168
169         rc = pb_mkdir_recursive(mountpoint);
170         if (rc)
171                 return;
172
173         opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
174         argv[3] = opts;
175
176         if (task->url->port)
177                 opts = talloc_asprintf_append(opts, ",port=%s",
178                                                 task->url->port);
179
180         task->result->local = talloc_asprintf(task->result, "%s/%s",
181                                                         mountpoint,
182                                                         task->url->path);
183
184         task->process->path = pb_system_apps.mount;
185         task->process->argv = argv;
186
187         if (task->async) {
188                 rc = process_run_async(task->process);
189                 if (rc) {
190                         process_release(task->process);
191                         task->process = NULL;
192                 }
193                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
194         } else {
195                 rc = process_run_sync(task->process);
196                 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
197                 process_release(task->process);
198                 task->process = NULL;
199         }
200
201         talloc_free(opts);
202 }
203
204 static void load_sftp(struct load_task *task)
205 {
206         const char *argv[] = {
207                         pb_system_apps.sftp,
208                         NULL,           /* 1: host:path */
209                         NULL,           /* 2: local file */
210                         NULL,
211         };
212
213         argv[1] = talloc_asprintf(task, "%s:%s",
214                                 task->url->host, task->url->path);
215         load_process_to_local_file(task, argv, 2);
216 }
217
218 static enum tftp_type check_tftp_type(void *ctx)
219 {
220         const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
221         struct process *process;
222         enum tftp_type type;
223
224         process = process_create(ctx);
225         process->path = pb_system_apps.tftp;
226         process->argv = argv;
227         process->keep_stdout = true;
228         process->add_stderr = true;
229         process_run_sync(process);
230
231         if (!process->stdout_buf || process->stdout_len == 0) {
232                 pb_log("Can't check TFTP client type!\n");
233                 type = TFTP_TYPE_BROKEN;
234
235         } else if (memmem(process->stdout_buf, process->stdout_len,
236                                 "tftp-hpa", strlen("tftp-hpa"))) {
237                 pb_debug("Found TFTP client type: tftp-hpa\n");
238                 type = TFTP_TYPE_HPA;
239
240         } else if (memmem(process->stdout_buf, process->stdout_len,
241                                 "BusyBox", strlen("BusyBox"))) {
242                 pb_debug("Found TFTP client type: BusyBox tftp\n");
243                 type = TFTP_TYPE_BUSYBOX;
244
245         } else {
246                 pb_log("Unknown TFTP client type!\n");
247                 type = TFTP_TYPE_BROKEN;
248         }
249
250         process_release(process);
251         return type;
252 }
253
254 static void load_tftp(struct load_task *task)
255 {
256         const char *port = "69";
257         const char *argv[10] = {
258                 pb_system_apps.tftp,
259         };
260
261         if (task->url->port)
262                 port = task->url->port;
263
264         if (tftp_type == TFTP_TYPE_UNKNOWN)
265                 tftp_type = check_tftp_type(task);
266
267         if (tftp_type == TFTP_TYPE_BUSYBOX) {
268                 argv[1] = "-g";
269                 argv[2] = "-l";
270                 argv[3] = NULL; /* 3: local file */
271                 argv[4] = "-r";
272                 argv[5] = task->url->path;
273                 argv[6] = task->url->host;
274                 argv[7] = port;
275                 argv[8] = NULL;
276
277                 load_process_to_local_file(task, argv, 3);
278
279         } else if (tftp_type == TFTP_TYPE_HPA) {
280                 argv[1] = "-m";
281                 argv[2] = "binary";
282                 argv[3] = task->url->host;
283                 argv[4] = port;
284                 argv[5] = "-c";
285                 argv[6] = "get";
286                 argv[7] = task->url->path;
287                 argv[8] = NULL; /* 8: local file */
288                 argv[9] = NULL;
289                 load_process_to_local_file(task, argv, 8);
290
291         } else
292                 task->result->status = LOAD_ERROR;
293 }
294
295 enum wget_flags {
296         wget_empty = 0,
297         wget_no_check_certificate = 1,
298 };
299
300 /**
301  * pb_load_wget - Loads a remote file via wget and returns the local file path.
302  *
303  * Returns the local file path in a talloc'ed character string on success,
304  * or NULL on error.
305  */
306
307 static void load_wget(struct load_task *task, int flags)
308 {
309         const char *argv[] = {
310                 pb_system_apps.wget,
311                 "-O",
312                 NULL, /* 2: local file */
313                 NULL, /* 3 (optional): --quiet */
314                 NULL, /* 4 (optional): --no-check-certificate */
315                 NULL, /* 5: URL */
316                 NULL,
317         };
318         int i;
319
320         i = 3;
321 #if !defined(DEBUG)
322         argv[i++] = "--quiet";
323 #endif
324         if (flags & wget_no_check_certificate)
325                 argv[i++] = "--no-check-certificate";
326
327         argv[i] = task->url->full;
328
329         load_process_to_local_file(task, argv, 2);
330 }
331
332 /* Although we don't need to load anything for a local path (we just return
333  * the path from the file:// URL), the other load helpers will error-out on
334  * non-existant files. So, do the same here with an access() check on the local
335  * filename.
336  */
337 static void load_local(struct load_task *task)
338 {
339         int rc;
340
341         rc = access(task->url->path, F_OK);
342         if (rc) {
343                 task->result->status = LOAD_ERROR;
344         } else {
345                 task->result->local = talloc_strdup(task->result,
346                                                     task->url->path);
347                 task->result->status = LOAD_OK;
348         }
349 }
350
351 /**
352  * load_url - Loads a (possibly) remote URL and returns the local file
353  * path.
354  * @ctx: The talloc context to associate with the returned string.
355  * @url: The remote file URL.
356  * @tempfile: An optional variable pointer to be set when a temporary local
357  *  file is created.
358  * @url_cb: An optional callback pointer if the caller wants to load url
359  *  asynchronously.
360  *
361  * Returns the local file path in a talloc'ed character string on success,
362  * or NULL on error.
363  */
364
365 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
366                 load_url_complete async_cb, void *async_data)
367 {
368         struct load_url_result *result;
369         struct load_task *task;
370
371         if (!url)
372                 return NULL;
373
374         task = talloc_zero(ctx, struct load_task);
375         task->url = url;
376         task->async = async_cb != NULL;
377         task->result = talloc_zero(ctx, struct load_url_result);
378         task->result->task = task;
379         task->process = process_create(task);
380         if (task->async) {
381                 task->async_cb = async_cb;
382                 task->async_data = async_data;
383                 task->process->exit_cb = load_url_process_exit;
384                 task->process->data = task;
385         }
386
387         switch (url->scheme) {
388         case pb_url_ftp:
389         case pb_url_http:
390                 load_wget(task, 0);
391                 break;
392         case pb_url_https:
393                 load_wget(task, wget_no_check_certificate);
394                 break;
395         case pb_url_nfs:
396                 load_nfs(task);
397                 break;
398         case pb_url_sftp:
399                 load_sftp(task);
400                 break;
401         case pb_url_tftp:
402                 load_tftp(task);
403                 break;
404         default:
405                 load_local(task);
406                 break;
407         }
408
409         result = task->result;
410         if (result->status == LOAD_ERROR) {
411                 load_url_result_cleanup_local(task->result);
412                 talloc_free(result);
413                 talloc_free(task);
414                 return NULL;
415         }
416
417         if (!task->async)
418                 talloc_free(task);
419
420         return result;
421 }
422
423 struct load_url_result *load_url(void *ctx, struct pb_url *url)
424 {
425         return load_url_async(ctx, url, NULL, NULL);
426 }
427
428 void load_url_async_cancel(struct load_url_result *res)
429 {
430         struct load_task *task = res->task;
431
432         /* the completion callback may have already been called; this clears
433          * res->task */
434         if (!task)
435                 return;
436
437         if (res->status == LOAD_CANCELLED)
438                 return;
439
440         assert(task->async);
441         assert(task->process);
442
443         res->status = LOAD_CANCELLED;
444         process_stop_async(task->process);
445 }