免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4530 | 回复: 1
打印 上一主题 下一主题

execl error: No such file or directory,搞定! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-14 11:07 |只看该作者 |倒序浏览
本代理程序的目的想实现 接受远程请求,启动网元连接程序vcit,然后传递远程任务和接收网元返回报告。
采用双管道的方式,但是fork的子进程 在执行execl出错:execl error: No such file or directory,
该代码片断开始是能执行的, 加入socket之后就不行了,请高人帮忙,指点迷津!

// File: prg6_1.c
#include <stdio.h>          /* These are the usual header files */
#include <strings.h>          /* for bzero() */
#include <unistd.h>         /* for close() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 2006   /* Port that will be opened */
#define BACKLOG 5   /* Number of allowed connections */
#define MAXDATASIZE 1000  
#define MAXLINE 256

void process_cli(int connectfd, struct sockaddr_in client)
{
   int num;
   int i,n;
   char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE];
   char line[MAXLINE];
   int fd1[2],fd2[2];
   int vcitopened = -1;
   pid_t pid;
   
   printf("You got a connection from %s\n",inet_ntoa(client.sin_addr) ); /* prints client's IP */

   while (num = recv(connectfd, recvbuf, MAXDATASIZE,0)) {
       recvbuf[num] = '\0';
       printf("Received client message: %s",recvbuf);
      
       if (strncmp(recvbuf, "CLOSE", 5) == 0)
       {
           if (vcitopened == 1)
           {
               if (!fd1[1])
               {
                          write(fd1[1],"quit",4);
                   close(fd1[0]);
                   close(fd1[1]);
                   close(fd2[0]);
                   close(fd2[1]);
               }
               strcpy(sendbuf,"connection closed.");
               send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
               vcitopened = -1;
           }
       }
       else
       if (strncmp(recvbuf, "OPEN", 4) == 0)
       {
           if(vcitopened == 1)
           {
               strcpy(sendbuf, "close pre connection first!");
               send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
           }
           else
           {
               //open vcit
               if (pipe(fd1) < 0 || pipe(fd2) < 0)
               {
                   perror("pipe error\n");
                   exit(1);
               }
               if((pid = fork()) < 0)
               {
                   perror("pipe error\n");
                   exit(1);
               }
               else if (pid > 0) /*parent*/
               {
                   close(fd1[0]);  /* pipe fd1: parent -> child */
                   close(fd2[1]);  /* pipe fd2: child -> parent */
               }
               else
               {
                   close(fd1[1]);  /* pipe fd1: parent -> child */
                   close(fd2[0]);  /* pipe fd2: child -> parent */
                   if(fd1[0] != STDIN_FILENO)
                   {
                       if(dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
                       {
                           perror("child: dup2 error");
                           exit(1);
                       }
                       close(fd1[0]);
                   }
                   if(fd2[1] != STDOUT_FILENO)
                   {
                       if(dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
                       {
                           perror("child: dup2 error");
                           exit(1);
                       }
                       close(fd2[1]);
                   }
                   //if( execl("./vcit" "vcit", (char *) 0) < 0)
                   if( execl("/bin/ls" "ls","-l", (char *) 0) < 0)
                   {
                       perror("child: execl error");
                       close(fd1[0]);
                       close(fd1[1]);
                       close(fd2[1]);
                       close(fd2[0]);
                       vcitopened = -1;
                       strcpy(sendbuf, "ne open failed!");
                       send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
                       exit(1);
                   }
                   else
                       vcitopened = 1;
                     
               }  /* pid < 0 */
               
               strcpy(sendbuf, "open ne connection!");
               send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
               //vcitopened = 1;
               //recvbuf[0] = '\0';
           }
           
       }   
       else
       {
           if(vcitopened == -1)
           {
               strcpy(sendbuf, "open connection first!");
               send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
           }
           else
           {
               if(pid > 0)
               {
                  n = strlen(recvbuf);
                  if (write(fd1[1], recvbuf, n) != n)
                  {
                       perror("parent: write error!");
                       exit(1);
                  }
                  while((n = read(fd2[0], line, MAXLINE)) > 0)
                       send(connectfd, line, n , 0);
               }
               strcpy(sendbuf, "send cmd.");
               send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
           }
       }

   }

   close(connectfd); /*  close connectfd */
}


int main()
{
   int listenfd, connectfd; /* socket descriptors */
   struct sockaddr_in server; /* server's address information */
   struct sockaddr_in client; /* client's address information */
   int sin_size;

   /* Create TCP socket  */
   if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)  {
       /* handle exception */
       perror("Creating socket failed.");
       exit(1);
   }

   int opt = SO_REUSEADDR;
   setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

   bzero(&server,sizeof(server));
   server.sin_family=AF_INET;
   server.sin_port=htons(PORT);
   server.sin_addr.s_addr = htonl (INADDR_ANY);
   if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
       /* handle exception */
       perror("Bind error.");
       exit(1);
   }   

   if(listen(listenfd,BACKLOG) == -1){  /* calls listen() */
       perror("listen() error\n");
       exit(1);
   }

   sin_size=sizeof(struct sockaddr_in);
   while (1) {
       if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {
          perror("accept() error\n");
          exit(1);
       }   
       process_cli(connectfd, client);
   }

   close(connectfd);
   close(listenfd);   /* close listenfd */
   return 0;        
}

[ 本帖最后由 microchu 于 2006-8-14 11:42 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-08-14 11:40 |只看该作者

自己解决了!还是路径的问题

现在 excel可以执行了,修改如下:
if( execl("/home/test/ipc/vcit", "/home/test/ipc/vcit", recvbuf + 5,(char *) 0) < 0)

[ 本帖最后由 microchu 于 2006-8-14 11:41 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP