2

Working on a project and when I call execl() it is not working. It is called after a fork and is supposed to reexecute the current file. (argument is declared earlier in the file):

argument = argv[0];
int err =execl(argument, argument, left, "1", NULL);
if (err == -1)  printf("never execled");

Everything I have read makes me thing this should work. First argument specifies the path, second the file to be executed, third is a string, fourth is a place holder so that arc == 3 when the execl goes through, and the 4th is a null terminator.

Can anyone help?

ReezaCoriza
  • 133
  • 4
  • 16
  • Can you show a full example that demonstrates this failure? – simonc Oct 15 '13 at 17:42
  • What does `errno` tell you? – Martin R Oct 15 '13 at 17:44
  • @MartinR Errno tells me "No such file or directory" – ReezaCoriza Oct 15 '13 at 18:06
  • What is `argv[0]`? Is it absolute or relative? What is the current directory? Perhaps Igor is on the right track with his answer. – Martin R Oct 15 '13 at 18:08
  • argv[0] is the name of the current binary. which in this case is "bit_count". I have tried, replacing argv[0] with : "bit_count", "/bit_count" and "./bit_count" all with no luck. the argv[0] argument was just my current attempt. – ReezaCoriza Oct 15 '13 at 18:12
  • @ReezaCoriza: And is "bit_count" in the *current directory*? – Martin R Oct 15 '13 at 18:14
  • @MartinR no. But I have also tried "./" and it gives me a "permission denied" message. Same thing happens when I use Igor's idea without appending the argv[0] on the end – ReezaCoriza Oct 15 '13 at 18:18
  • @ReezaCoriza: For `execl(prog, ...)`, the first argument `prog` must be either an absolute path, or a relative path *to the current directory*. It is the same as with `open(file, ...)`. – Martin R Oct 15 '13 at 18:24
  • Can the current directory not be a relative path to itself? What I mean is, is it possible to call a file, whether it be the current file or another, that is in the current directory? Or do i need to move it to a new location? – ReezaCoriza Oct 15 '13 at 18:26
  • @simonc [All of the code hosted on Pastebin](http://pastebin.com/J3cAu8a7) The resulting output is: Parent Original String: 0000000000001111111111111 Measured Length: 25 Parent Left: 000000000000 Parent Right: 1111111111111 made it to leftErrorno: Permission denied – ReezaCoriza Oct 15 '13 at 18:28
  • @ReezaCoriza: It gets confusing now. Perhaps you can clarify: 1) Where is your program located? 2) Which is the current directory when you execute your program? 3) *How* do you execute your program? 4) What does `argv[0]` contain? – Martin R Oct 15 '13 at 18:30
  • @MartinR 1)It is located in a folder named prog1. 2)the current directory when I execute is also prog1. 3)I execute my program via the command line by entering ./bit_count test.txt, where text.txt is a file which is being read from. argv[0] should contain the name of the program, namely bit_count. I really appreciate all of this help. I just want to clear this hurdle so I can move on. [Here is the code in its entirety](http://pastebin.com/J3cAu8a7) – ReezaCoriza Oct 15 '13 at 18:37
  • @ReezaCoriza: If you call the program from the directory where it is located via `./bit_count test.txt`, then `argv[0]` should be "./bit_count", and `execl(argv[0], ...)` should *just work*. Perhaps *check* the value of `argv[0]` immediately prior to the `execl` call to see if it has been modified accidentally. – Martin R Oct 16 '13 at 07:50

1 Answers1

2

argv[0] holds only the filename, but not the full directory path, which execl requires.

Try sth like this:

char *cwd;
cwd=malloc(255);
getcwd(cwd,255);
strcat(cwd,"/");
strcat(cwd,argv[0]);

and use above constructed cwd in execl.

Igor Popov
  • 2,588
  • 17
  • 20
  • I *think* a relative path should work as well, if it is valid with respect to the current directory – Martin R Oct 15 '13 at 17:46
  • @Martin Indeed, but I had once an occasion where the full path was necessary under Linux for invocation of `execl`. – Igor Popov Oct 15 '13 at 18:00
  • @IgorPopov when I tried this I ended up with "...fullpathtocurrentdirectory/./bit_count" which did not work. The file I am trying to execute is the same file I am calling execl, is there any reason why using `execl("./bit_count", "bit_count", left, "1", NULL);` shouldn't be working – ReezaCoriza Oct 15 '13 at 19:02
  • @Reeza I am not sure. On some older linux distro (a few years ago) it worked only with full pathname, while on my current Linux Mint 14 it works with relative path. It is possible that environment variables play role here. Check this: http://web.eecs.utk.edu/~huangj/cs360/360/notes/Exec/lecture.html – Igor Popov Oct 15 '13 at 19:18
  • @Reeza Try with setting current directory (.) as the first argument of your PATH environment variable (e.g. export PATH=.:$PATH). The execl man page contains:"On some other systems, the default path (used when the environment does not contain the variable PATH) has the current working directory listed after /bin and /usr/bin, as an Anti-Trojan-horse measure. Linux uses here the traditional "current directory first" default path." – Igor Popov Oct 15 '13 at 19:24