Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 Garmin Ltd. or its subsidiaries |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0 |
| 5 | * |
| 6 | * Attempts to find and exec the host qemu-bridge-helper program |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | void try_program(char const* path, char** args) { |
| 13 | if (access(path, X_OK) == 0) { |
| 14 | execv(path, args); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | int main(int argc, char** argv) { |
| 19 | char* var; |
| 20 | |
| 21 | /* Copy arguments so that they are a NULL terminated list, skipping argv[0] |
| 22 | * since it is this program name */ |
| 23 | char** args = malloc(argc * sizeof(char*)); |
| 24 | for (int i = 0; i < argc - 1; i++) { |
| 25 | args[i] = argv[i + 1]; |
| 26 | } |
| 27 | args[argc - 1] = NULL; |
| 28 | |
| 29 | var = getenv("QEMU_BRIDGE_HELPER"); |
| 30 | if (var && var[0] != '\0') { |
| 31 | execvp(var, args); |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | try_program("/usr/libexec/qemu-bridge-helper", args); |
| 36 | try_program("/usr/lib/qemu/qemu-bridge-helper", args); |
| 37 | |
| 38 | fprintf(stderr, "No bridge helper found\n"); |
| 39 | return 1; |
| 40 | } |
| 41 | |