2

我有一个小 C 脚本的问题,它应该作为服务器运行并为每条到达的消息启动一个弹出窗口。execl语法是正确的,因为如果我尝试一个小脚本

main() { execl(...); }

有用。

当我将它放入 while(1) 循环时,它不起作用。其他一切都在工作,比如printf或字符串操作,但不是execl. 即使我叉它也不起作用。我怎样才能让它工作?

我已经尝试过fork(),但它也不起作用。

这是完整的服务器 C 代码。

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>

#define BUFLEN 512
#define PORT 9930

void diep(char *s) {
     perror(s);
     exit(1);
}

int main() {
    struct sockaddr_in si_me, si_other;
    int s, i, slen=sizeof(si_other), broadcastPermission;
    char buf[100], zeni[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
       diep("socket");

    broadcastPermission = 1;
    if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, sizeof(broadcastPermission)) < 0)
        diep("setsockopt() failed");

    memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(s, &si_me, sizeof(si_me))==-1)
       diep("bind");

    while (1) {
        if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1) diep("recvfrom()");
        //printf("Received packet from %s:%d\nData: %s\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);

 strcpy(zeni, "");
 strcat(zeni, "zenity --warning --title Hack!! --text ");
 strcat(zeni, buf);
 printf("cmd: %s\n", zeni);
 //system (zeni);
 execl("/usr/bin/zenity", "/usr/bin/zenity", "--warning", "--title", "Warn!", "--text", buf, (char *) NULL);
    }

    close(s);
    return 0;

}
4

3 回答 3

3

@jweyrich 已经指出了您使用 的一些问题recvfrom,但是还有一个更根本的问题。编码

while (1) {
    recvfrom(...);
    execl(...);
}

最多只会执行一次。这是因为exec系统调用系列(包括execl)将当前执行的程序替换为对 的调用中给出的程序execl。实际上,execl永远不会返回,除非出现错误。

要在 unix 中创建新的子进程,您必须首先调用fork,它克隆现有进程,然后在子调用execl(或一些相关的系统调用)中将子进程替换为您实际要运行的程序。手动正确执行此操作有些棘手,因此该system功能为您完成了此操作,但是它有其自身的缺点。

于 2010-04-30T08:56:51.403 回答
2

您对 的调用中存在堆栈溢出recvfrom

recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)

我相信你混合了 2 个缓冲区。您正在使用buf大小为 100 的 ,但告诉它的大小是 BUFLEN,即 512。每当有人发送超过 100 个字节时,您的程序很可能会崩溃。

除此之外,它可能recvfrom没有返回,因为它没有收到任何东西。你printf被处决了吗?


更新:正如@Daniel 和@Dale 所指出的,execl除非发生错误,否则不会返回。从手册页引用:

exec 系列函数用新的过程映像替换当前的过程映像。

作为替代方案,您可以使用system.

于 2010-04-30T08:36:00.700 回答
1

请阅读 exec(3) 手册页。

在您的情况下,execl 系统调用将用 /usr/bin/zenity 的映像替换当前进程映像。

您有两种解决方案:在您尝试使用 system(3) 或执行 fork 并在子进程内部运行 execlp。

于 2010-04-30T08:56:08.333 回答