0

I have a C binary executable called requestAudit in /apps/tools/public/requestAudit . This binary invokes a python script under /apps/tools/sf/audits/audit.py . Now I need to find the absolute path of the Binary i.e. /apps/tools/public/requestAudit inside my audit.py script. This is needed to create log files under the directory where the binary executable resides. is there any way we can get it programmatically. Note that even when I call that binary from anywhere I should always get the absolute path of binary executable.

Thanks

4
  • 1
    Check os.getcwd() from the python script Commented Jun 12, 2020 at 3:55
  • What @rdas suggests should work, as long as you start the script with the current working directory unchanged from where the executable sits. Commented Jun 12, 2020 at 3:57
  • os.getcwd() may or may not be the path where the binary is located, that will depend on how it's been designed. At any rate you can use os.getppid() to get the PID of the process that invoked the script, I'm not sure how you get the process image path from the PID though, maybe this helps. Commented Jun 12, 2020 at 4:00
  • @Grismar, This won't work as you rightly said as it is not always guaranteed script started from the directory where the executable resides. I could be in /users/rg/ and invoking binary like /apps/tools/public/requestAudit Commented Jun 12, 2020 at 5:10

1 Answer 1

1

Well, the main thing you need to do is get the current working directory in your C script, and then you can either write some C code to combine the result with argv[0] to create the full path to the file and pass that to your python script as command line argument when you call it. Alternatively, you could pass both argv[0] and the result of getcwd() as parameters to your Python script, which is what I did below:

requestAudit.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char* argv[]) {
    char* cwd = getcwd(NULL, 0);
    char* command = "python audit.py ";
    char* combine;

    // I added 2 because 1 for the null terminator and 1 for the space
    // between cwd and argv[0]
    size_t len = strlen(command) + strlen(cwd) + strlen(argv[0]) + 2;
    combine = malloc(sizeof(*combine)*len);
    strncpy(combine, command, strlen(command) + 1);
    strncat(combine, cwd, strlen(cwd) + 1);
    strncat(combine, " ", 2);
    strncat(combine, argv[0], strlen(argv[0]) + 1);
    return system(combine);
}

audit.py

import sys

if __name__ == "__main__":
    full_path = sys.argv[1] + sys.argv[2][1:]
    print(full_path)

This is just one way to do it, I'm sure there are other, probably better, ways to do this.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. The problem here is some time audity.py called by binary executable and some time someone invokes directly. So I was looking for a python solution to handle in audit.py without relying on the caller.
I was under the impression that you just wanted to call the binary from anywhere, in which case my solution still works, you would just modify the python script a little. But if what you want to do is get the full path to the binary (requestAudit) whenever audit.py is run, whether directly or invoked from requestAudit, then basically in your Python script it seems like you just want to search the computer for requestAudit? Otherwise you can hard-code the full path into your Python script. It sounds like you don't need to do anything special with requestAudit at all.
Yes, your solution will work. I just have to modify my python code a bit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.