如何重定向二进制文件的打开调用

I have a binary b which tries to open ./a. Multiple instances of b run concurrently inside the same directory, but each instance should open a different version of ./a. How can I run b so that when it tries to open ./a it will actually open some/other/dir/a?

I tried wrapping b in a script that cds into some/other/dir before executing b, but this solution is not possible. I also thought about creating a hook which redirects the open() system-call but would prefer something simpler.

我正在使用linux。

评论
  • 心,绞痛
    心,绞痛 回复

    If the current working directory is not important for b (except for starting ./a) you can,
    for each run of binary b:

    • copy b to the respective some/other/dir/ (or create a link)
    • cd to the respective some/other/dir/
    • and run b from there
    • remove the copy of b

    例:

    #! /usr/bin/env bash
    for dir in some/other/dir another/dir and/so/on
        cp b "$dir/"
        (cd "$dir"; ./b; rm b) &
    done
    

    Here we start three instances of b in parallel. The first executes some/other/dir/a, the second executes another/dir/a, and the third executes and/so/on/a.