I am writing in my grammar, in LEX, some code to fork() my process and run a child. The child actually gets some input from the parent, and then returns a result.
I need to call exec on the same binary that loaded the parent, but there I am having an issue. I know that exec does not mean complete sense, but I do this because I have some previous grammar in LEX that I simply want to get rid off, so reloading the process is easier.
I have the following code in the children, after the fork():
char *path = strdup(getenv("PWD"));
size_t size = strlen(path) + strlen("/shell") + 1;
path = (char *) realloc(path, sizeof(char) * size);
path = strcat(path, "/shell");
// call exec
execl(path, NULL);
The issue with this code, is that it works if the process is launched from the same directory, but if I try to load from a folder within this directory, like say, ../shell, then the path is actually wrong, it will include this directory.
I would like to please know how I could get the correct path of the process, and if there is a way to also get the process actual name please? I have looked at the environments variable but have not found anything useful.
Thank you very much,
Jary
The objective, as I understand it, it to have the child process re-execute the same program that represents the parent process.
In the absence of tricks using data from the
/procfile system, I don’t believe there is a completely reliable way to do it.Normally, you rely on the value of
argv[0]being sufficient to find the program, and useexecvp()to find the program via$PATH.Attempt 2 – maybe less confusing
You are correct that using
getenv("PWD")orgetcwd()is not usually correct.The main program, therefore, stashes the value of its
argv[0]in the global variablearg0to make it available to other parts of the process – in particular, the code that is going to (re)run the command.If the program is invoked in the same directory as where the executable resides using “./shell”, then
argv[0](and hencearg0) will contain that pathname. If it is executed using “shell” relying on $PATH to locate the program, thenargv[0]will contain either just “shell” or the absolute pathname of “shell” (less common).If the program is invoked from the
fdsubdirectory, it might be invoked as “../shell”, or it might be invoked as “/absolute/path/to/shell” or it might be invoked as “shell” relying on $PATH to find the executable. Again, in any of these cases, the value inarg0is the name by which the program was originally invoked, or is equivalent to it.The only time this fails is if someone is deliberately setting out to confuse the executable, which is (fortunately) seldom the case.
So, in the child code, you can use:
to re-execute the command as it was originally executed (apart from any auxilliary arguments that were passed originally).
Attempt 1 – somewhat confusing
Assuming that the value of
argv[0]is available via a variablechar *arg0, all that’s necessary is:If you must use
execl(), then you need:The cast is necessary;
execl()is a variable argument list function, and if you write 0, it will be converted tointwhich will fail on a 64-bit system whereintis 32-bits and a pointer is 64-bits. However, that will fail ifarg0does not represent the path (relative or absolute) to the executable. You’d then have to decide what to do ifexecl()returns – you can either give up or search for the program via$PATH, but in that case, why not useexecvp()in the first place to save yourself the pain.