I can't figure out why my calls to execv() are failing in my child processes. I can hard code a C-string literal in place of my variable arguments and it works fine, but it doesn't like the variable version.
I haven't dealt with C-strings much so it may be some confusion on my part between various types (const char* pathname, char* const args[], char buffer[30]).
The command line argument is intended to be a text file that consists of various Linux executables, one per line such as:
/bin/pwd
/usr/bin/whoami
/bin/df
/bin/date
Interesting...thanks for the tip! So the issue is with the pathname argument to execv()
using the above input (I also added a line to output args[0] to the screen to verify it's contents) I get:
********************* START *********************
size[9], command: /bin/pwd
argv[0] will be: /bin/pwd
Error in execv(): 2: No such file or directory
size[16], command: /usr/bin/whoami
argv[0] will be: /usr/bin/whoami
Error in execv(): 2: No such file or directory
size[8], command: /bin/df
argv[0] will be: /bin/df
Error in execv(): 2: No such file or directory
size[10], command: /bin/date
argv[0] will be: /bin/date
Error in execv(): 2: No such file or directory
********************* END *********************
How is it not finding these? Are they not absolute pathnames? I can type them into the shell and it has no problem running the command... The documentation I've got in front of me for the exec() library functions seems to be telling me the argument format is correct.
Well, that is good to know. Thanks for your assistance with this problem.
I'm running the code above as-is except with the modified block you suggested utilizing errno and it's not performing the commands for me. I get the 'No such file or directory' error message shown in the output above.
Maybe it's something machine-specific? I'm running Ubuntu 12.10. I've got access to some remote machines running Fedora I can try this code on.
james@lucy:~/eclipse_workspace/cs431_prog2$ for f in `cat launch.txt`; do $f; done
: No such file or directory
: No such file or directory
: No such file or directory
: No such file or directory
james@lucy:~/eclipse_workspace/cs431_prog2$ cat launch.txt
/bin/pwd
/usr/bin/whoami
/bin/df
/bin/date
changed the single quotes from ` to '
james@lucy:~/eclipse_workspace/cs431_prog2$ for f in 'cat launch.txt'; do $f; done
/bin/pwd
/usr/bin/whoami
/bin/df
/bin/date
Thanks for your help kbw & tcs
Turns out that .getline() was filling the buffer char array with the entire line of the input file including the cntrl character (newline, specifically). So for the first input line it had the strlen() of 9 with contents "/bin/pwd\n" and my system wasn't recognizing a program named "pwd\n" that execv() was passing to replace the child process' image.
Adding the following code to overwrite a cntrl char with the C-string null terminating char fixed my issue:
I think you've edited your test case file on MS-Windows and your test process is running on Linux? Then you got an extra carriage return character before the final newline.