exec() 不适用于 Firefox

发布于 2024-10-08 15:14:43 字数 1025 浏览 3 评论 0原文

我一直在使用 fork()exec() 的组合在 Linux 上执行一些外部命令,但是,每当我尝试执行 < code>/usr/bin/firefox 这是指向真实二进制文件的符号链接。

有谁知道如何解决这个问题?我已经用其他程序进行了测试(它们实际上是可执行二进制文件,而不是它们的符号链接)并且它有效。

这是该程序的代码:

#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;
}

编辑:我更新了代码以包含答案并更改了主题的标题,因为问题实际上似乎根本不是由于符号链接造成的。谢谢大家。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城月光淡如水﹏ 2024-10-15 15:14:43

您可能想在 execvp 之后添加一些代码来输出一些诊断信息(即检查 errno,打印一些有意义的;))。

您还可以尝试使用 strace 在不修改源代码的情况下对其进行分析或 gdb 就此事而言。

另请参阅:execve

根据评论进行后续更新
Firefox 对 argv[0] 为空不满意,不幸的是,这就是 argList 的样子。

经验教训:彻底了解您作为 argv 传递给您执行的程序的内容。 :)

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. :)

深者入戏 2024-10-15 15:14:43

Firefox 是否坚持使用非空 argv[0] ?您通常应该将命令的名称(“firefox”或“/usr/bin/firefox”)传递给命令,但您没有这样做。


[...要检查上面更深入的评论 - 看来这是正确的诊断,但晚了 21 分钟左右...]

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...]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文