Question

I've been using a combination of fork() and exec() to execute some external command on linux, however, the code seems to fail whenever I try to execute /usr/bin/firefox which is a symbolic link to a real binary.

Does anyone know how to solve this problem? I've tested with other programs (which really are executable binaries and not symlinks to them) and it works.

Here's the code from the program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>

int main(int argc, char **argv) {
  pid_t pid;
  // this was the old line:
  // char *parmList[] = {"", "index.html", NULL};
  // and this is the one that solves the problem:
  char *parmList[] = {"firefox", "index.html", NULL};
  int a;

  if ((pid = fork()) == -1)
    perror("fork failed");

  if (pid == 0) {
    a = execvp("/usr/bin/firefox", parmList);
    fprintf(stdout, "execvp() returned %d\n", a);
    fprintf(stdout, "errno: %s (%d).\n", strerror(errno), errno);
  }
  else {
    waitpid(pid, 0, 0);
  }

  return 0;
}

Edit: I updated the code to include the answer and changed the topic's title because the problem really didn't seem to be due to symbolic links at all. Thanks everyone.

Was it helpful?

Solution

You might want to add some code right after the execvp to output some diagnostic (i.e. check errno, print something meaningful ;)).

You could also try to analyze it w/o source modification using strace or gdb for that matter.

See also: execve.

Update as follow-up from the comments
Firefox is not happy with argv[0] being empty, which is what argList looked like, unfortunately.

Lessons learned: Be thoroughly aware of what you pass as argv to the program you execute. :)

OTHER TIPS

Does Firefox insist on having a non-empty argv[0]? You should normally pass the name of the command (either just "firefox" or "/usr/bin/firefox") to the command, but you are not doing so.


[...going to check the deeper comments above - and it seems this is the correct diagnosis, but 21 minutes or so late...]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top