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> |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 10 | #include <string.h> |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 11 | #include <unistd.h> |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 12 | #include <stdlib.h> |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 13 | |
| 14 | void try_program(char const* path, char** args) { |
| 15 | if (access(path, X_OK) == 0) { |
| 16 | execv(path, args); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | int main(int argc, char** argv) { |
| 21 | char* var; |
| 22 | |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 23 | var = getenv("QEMU_BRIDGE_HELPER"); |
| 24 | if (var && var[0] != '\0') { |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 25 | execvp(var, argv); |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 26 | return 1; |
| 27 | } |
| 28 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 29 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { |
| 30 | fprintf(stderr, "Helper function to find and exec qemu-bridge-helper. Set QEMU_BRIDGE_HELPER to override default search path\n"); |
| 31 | return 0; |
| 32 | } |
| 33 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 34 | try_program("/usr/libexec/qemu-bridge-helper", argv); |
| 35 | try_program("/usr/lib/qemu/qemu-bridge-helper", argv); |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 36 | |
| 37 | fprintf(stderr, "No bridge helper found\n"); |
| 38 | return 1; |
| 39 | } |
| 40 | |