|
| 1 | + /* nbd client library in userspace |
| 2 | + * Copyright (C) 2013-2023 Red Hat Inc. |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + */ |
| 18 | + |
| 19 | +#include <config.h> |
| 20 | + |
| 21 | +#include <errno.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#include "internal.h" |
| 27 | + |
| 28 | +extern char **environ; |
| 29 | + |
| 30 | +/* This is a perror() replacement that makes the error message more |
| 31 | + * machine-readable, for a select few error numbers. Do not use it as a general |
| 32 | + * error() replacement, only upon nbd_internal_execvpe_init() and |
| 33 | + * nbd_internal_fork_safe_execvpe() failure. |
| 34 | + */ |
| 35 | +static void |
| 36 | +xperror (const char *s) |
| 37 | +{ |
| 38 | + const char *err; |
| 39 | + |
| 40 | + if (s != NULL && *s != '\0') |
| 41 | + (void)fprintf (stderr, "%s: ", s); |
| 42 | + |
| 43 | + switch (errno) { |
| 44 | + case EACCES: |
| 45 | + err = "EACCES"; |
| 46 | + break; |
| 47 | + case ELOOP: |
| 48 | + err = "ELOOP"; |
| 49 | + break; |
| 50 | + case ENOENT: |
| 51 | + err = "ENOENT"; |
| 52 | + break; |
| 53 | + case ENOTDIR: |
| 54 | + err = "ENOTDIR"; |
| 55 | + break; |
| 56 | + default: |
| 57 | + err = strerror (errno); |
| 58 | + } |
| 59 | + (void)fprintf (stderr, "%s\n", err); |
| 60 | +} |
| 61 | + |
| 62 | +int |
| 63 | +main (int argc, char **argv) |
| 64 | +{ |
| 65 | + struct execvpe ctx; |
| 66 | + const char *prog_file; |
| 67 | + string_vector prog_argv; |
| 68 | + size_t i; |
| 69 | + |
| 70 | + if (argc < 3) { |
| 71 | + fprintf (stderr, "%1$s: usage: %1$s program-to-exec argv0 ...\n", argv[0]); |
| 72 | + return EXIT_FAILURE; |
| 73 | + } |
| 74 | + |
| 75 | + prog_file = argv[1]; |
| 76 | + |
| 77 | + /* For the argv of the program to execute, we need to drop our argv[0] (= our |
| 78 | + * own name) and argv[1] (= the program we need to execute), and to tack on a |
| 79 | + * terminating null pointer. Note that "argc" does not include the terminating |
| 80 | + * NULL. |
| 81 | + */ |
| 82 | + prog_argv = (string_vector)empty_vector; |
| 83 | + if (string_vector_reserve (&prog_argv, argc - 2 + 1) == -1) { |
| 84 | + perror ("string_vector_reserve"); |
| 85 | + return EXIT_FAILURE; |
| 86 | + } |
| 87 | + |
| 88 | + for (i = 2; i < argc; ++i) |
| 89 | + (void)string_vector_append (&prog_argv, argv[i]); |
| 90 | + (void)string_vector_append (&prog_argv, NULL); |
| 91 | + |
| 92 | + if (nbd_internal_execvpe_init (&ctx, prog_file, prog_argv.len) == -1) { |
| 93 | + xperror ("nbd_internal_execvpe_init"); |
| 94 | + goto reset_prog_argv; |
| 95 | + } |
| 96 | + |
| 97 | + /* Print out the generated candidates. */ |
| 98 | + for (i = 0; i < ctx.pathnames.len; ++i) |
| 99 | + (void)fprintf (stdout, "%s\n", ctx.pathnames.ptr[i]); |
| 100 | + |
| 101 | + if (fflush (stdout) == EOF) { |
| 102 | + perror ("fflush"); |
| 103 | + goto uninit_execvpe; |
| 104 | + } |
| 105 | + |
| 106 | + (void)nbd_internal_fork_safe_execvpe (&ctx, &prog_argv, environ); |
| 107 | + xperror ("nbd_internal_fork_safe_execvpe"); |
| 108 | + /* fall through */ |
| 109 | + |
| 110 | +uninit_execvpe: |
| 111 | + nbd_internal_execvpe_uninit (&ctx); |
| 112 | + |
| 113 | +reset_prog_argv: |
| 114 | + string_vector_reset (&prog_argv); |
| 115 | + |
| 116 | + return EXIT_FAILURE; |
| 117 | +} |
0 commit comments