2013-01-10 55 views
1

我正在研究一个C程序,它连接到数据库,抓取设备列表,然后分叉请求并创建到该设备的SSH连接。我遇到的问题是,具有700个结果的查询始终从碰到5个分支后开始。从本质上讲,我已经研究了pthreadglibc来处理线程,但是我发现的一些例子没有按需要工作,或者增加了太多的复杂性。C分叉5个孩子后停止

我遇到的问题是,它会停在5个孩子,并停止,而不是完成700个设备的其余部分。

示例代码:后connect_to_device回报

#include <libssh2.h> 

#include <sys/socket.h> 
#include <netinet/in.h> 
#include <sys/select.h> 
#include <unistd.h> 

#ifdef HAVE_ARPA_INET_H 
#include <arpa/inet.h> 
#endif 

#include <sys/time.h> 
#include <sys/wait.h> 
#include <sys/types.h> 
#include <stdlib.h> 
#include <fcntl.h> 
#include <errno.h> 
#include <stdio.h> 
#include <ctype.h> 
#include <limits.h> 

#include <mysql.h> 


/******************************** 
* 
* gcc -o confmgr cfgmgr.c -Wall -lpthread -lz -lm -lrt -ldl -lssh2 $(mysql_config --cflags) $(mysql_config --libs) -std=gnu99 
* 
********************************/ 
static int waitsocket(int socket_fd, LIBSSH2_SESSION *session) 
{ 
    struct timeval timeout; 
    int rc; 
    fd_set fd; 
    fd_set *writefd = NULL; 
    fd_set *readfd = NULL; 
    int dir; 

    timeout.tv_sec = 10; 
    timeout.tv_usec = 0; 

    FD_ZERO(&fd); 

    FD_SET(socket_fd, &fd); 

    /* now make sure we wait in the correct direction */ 
    dir = libssh2_session_block_directions(session); 


    if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) 
     readfd = &fd; 

    if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) 
     writefd = &fd; 

    rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout); 

    return rc; 
} 

int *connect_to_device(MYSQL_RES** args){ 

    printf("%s", args[2]); 

    const char *hostname = "1.1.1.1"; 
    const char *commandline = "command_to_run "; 
    const char *username = "static_user"; 
    const char *password = "static_pass"; 

    unsigned long hostaddr; 

    int sock; 

    struct sockaddr_in sin; 

    const char *fingerprint; 
    LIBSSH2_SESSION *session; 
    LIBSSH2_CHANNEL *channel; 
    int rc; 
    int exitcode; 
    char *exitsignal=(char *)"none"; 
    int bytecount = 0; 
    size_t len; 
    LIBSSH2_KNOWNHOSTS *nh; 
    int type; 



    rc = libssh2_init (0); 

    if (rc != 0) { 
     fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); 
     return 1; 
    } 

    hostaddr = inet_addr(hostname); 

    /* Ultra basic "connect to port 22 on localhost" 
    * Your code is responsible for creating the socket establishing the 
    * connection 
    */ 
    sock = socket(AF_INET, SOCK_STREAM, 0); 

    sin.sin_family = AF_INET; 
    sin.sin_port = htons(22); 
    sin.sin_addr.s_addr = hostaddr; 
    if (connect(sock, (struct sockaddr*)(&sin), 
       sizeof(struct sockaddr_in)) != 0) { 
     fprintf(stderr, "failed to connect!\n"); 
     return -1; 
    } 

    /* Create a session instance */ 
    session = libssh2_session_init(); 

    if (!session) 
     return -1; 

    /* tell libssh2 we want it all done non-blocking */ 
    libssh2_session_set_blocking(session, 0); 


    /* ... start it up. This will trade welcome banners, exchange keys, 
    * and setup crypto, compression, and MAC layers 
    */ 
    while ((rc = libssh2_session_handshake(session, sock)) == 

      LIBSSH2_ERROR_EAGAIN); 
    if (rc) { 
     fprintf(stderr, "Failure establishing SSH session: %d\n", rc); 
     return -1; 
    } 




    /* We could authenticate via password */ 
    while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); 
    if (rc) { 

      fprintf(stderr, "Authentication by password failed.\n"); 

      goto shutdown; 
    } 


    libssh2_trace(session, LIBSSH2_TRACE_TRANS | LIBSSH2_TRACE_KEX | LIBSSH2_TRACE_AUTH | LIBSSH2_TRACE_CONN | LIBSSH2_TRACE_SCP | LIBSSH2_TRACE_SFTP | LIBSSH2_TRACE_ERROR | LIBSSH2_TRACE_PUBLICKEY); 

    /* Exec non-blocking on the remove host */ 
    while((channel = libssh2_channel_open_session(session)) == NULL && 
      libssh2_session_last_error(session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN) 
    { 
     waitsocket(sock, session); 
    } 
    if(channel == NULL) 
    { 
     fprintf(stderr,"Error\n"); 
     exit(1); 
    } 
    while((rc = libssh2_channel_exec(channel, commandline)) == LIBSSH2_ERROR_EAGAIN) 
    { 
     waitsocket(sock, session); 
    } 

    if(rc != 0) 
    { 
     fprintf(stderr,"Error\n"); 
     exit(1); 
    } 

    for(;;) 
    { 
     // loop until we block 
     int rc; 
     do 
     { 
      char buffer[0x4000]; 

      /* strange thing */ 
      sleep(1); 

      rc = libssh2_channel_read(channel, buffer, sizeof(buffer)); 
      if(rc > 0) 
      { 
       int i; 
       for(i=0; i < rc; ++i) 
        putchar(buffer[i]); 
      } 
     } 
     while(rc > 0); 
     // this is due to blocking that would occur otherwise so we loop on this condition 
     if(rc == LIBSSH2_ERROR_EAGAIN) 
     { 
      waitsocket(sock, session); 
     } 
     else if(rc == 0) 
      break; 
    } 


    while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) 
     ; 
    if(rc == 0) 
    { 
     //does-not-work if(libssh2_channel_wait_closed(channel) == 0) 
     exitcode = libssh2_channel_get_exit_status(channel); 
    } 
    printf("\n%d\n", 221); 

    libssh2_channel_free(channel); 
    channel = NULL; 


    /***********************/ 

    shutdown: 

     libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); 
     libssh2_session_free(session); 


    close(sock); 

    fprintf(stderr, "\n----------------------\nScript Finished\n\n"); 

    libssh2_exit(); 

    return 7; 
} 

/******************************** 
* 
* 
* 
* 
********************************/ 
int main(int argc, char *argv[]){ 

    pid_t childPID; 
    int children = 0; 

    MYSQL *conn; 
    MYSQL_RES *res; 
    MYSQL_ROW row; 

    char *mySQLserver = "localhost"; 
    char *mySQLuser = "root"; 
    char *mySQLpassword = ""; /* set me first */ 
    char *mySQLdatabase = "Devices"; 


    conn = mysql_init(NULL); 

    /* Connect to database */ 
    if (!mysql_real_connect(conn, mySQLserver, 
     mySQLuser, mySQLpassword, mySQLdatabase, 0, NULL, 0)) { 
     fprintf(stderr, "%s\n", mysql_error(conn)); 
     exit(1); 
    } 
    /* send SQL query */ 
    if (mysql_query(conn, "SELECT Hostname,Descr,IP,Username,Password FROM All_Active_Devices")) { 
     fprintf(stderr, "%s\n", mysql_error(conn)); 
     exit(1); 
    } 

    res = mysql_use_result(conn); 
    /* output table name */ 
    printf("MySQL Tables in mysql database:\n"); 
    while ((row = mysql_fetch_row(res)) != NULL){ 


     printf("%s \n", row[0]); 

     children++; // Last fork() was successful 


     while (children >= 5) 
     { 
      int status; 
      // Wait for one child to exit 
      if (wait(&status) == 7) 
      { 
       children--; 
      } 
     } 

     childPID = fork(); 
     if (childPID < 0) { 
        printf("Fork Error \n"); 

     } else if (childPID == 0) { 

     printf("\tCreating Fork for %s: pid %d \n", row[0], childPID); 
     connect_to_device (&row); 

     } 
     else{ 

     printf("\tDid not create Fork for %s \n", row[0]); 

     } 


    } 

    /* close connection */ 
    mysql_free_result(res); 
    mysql_close(conn); 



    return 0; 
} 

回答

2

您的孩子进程正在退出 - 尤其是没有退出状态7 - 您在connect_to_device函数中有回报,但忽略这一点,并且每个孩子开始循环,创建更多的孩子。

您可能想要:return connect_to_device (&row);改为。

wait()返回死亡的子PID,而不是其状态 - 它在WEXITSTATUS(status)

+0

谢谢,你说得对。他们正在用他们的PID而不是返回值退出。 –

+0

谢谢,你是对的。像你的更新一样,'wait()'返回PID。会做'if(wait(&status)!= -1){children--; '没问题,还是流程处理不好? –

+0

@sixeightzero这似乎是合理的,但如果你也获得'-1',你应该可能会有某种错误处理。 –

2

会发生什么?它看起来像那个线程也会在while循环中开始,因为我没有看到子进程退出,坐在while children >=5循环中。在你的条件和5线程中的5并不是巧合。

运行中的某些输出会很有帮助,也会削减代码。

试着让你的脚手架工作,没有SSH代码。只是让进程停止并开始不应该需要ssh。然后在确定底层支持有效后添加应用程序逻辑。

1

也许ServerAliveCountMax的凑了过来:

编辑从NETCODER低于

ServerAliveCountMax
设置服务器活着的消息(见下文 ),这可能是数发送时没有ssh(1)收到任何消息 f rom服务器。如果在服务器活动 消息正在发送时达到此阈值,则ssh将从服务器断开连接, 终止会话。需要注意的是,使用 服务器存活消息与TCPKeepAlive(下文)非常不同。 服务器存活消息通过加密通道发送,因此不会被欺骗。 TCP Keepalive选项启用 TCPKeepAlive是可欺骗的。当客户端或服务器依赖于知道连接已成为 处于非活动状态时,服务器活动机制非常有用。

缺省值是3。如果,例如,ServerAliveInterval(见下文 )被设置为15和ServerAliveCountMax留在默认情况下,如果 服务器变得无法响应,ssh将 大约后断开45秒。该选项仅适用于协议版本2 。

查看man page for ssh_configman 5 ssh_config)了解详情。

+0

这是我检查的第一件事,但不是原因。感谢您的建议! –

+0

@netcoder - 我不确定这些消息的创建速度。也许客户端在服务器和网络上的运行速度都要快很多。只是一个想法。已经知道发生了这种情况。 –

+0

@EdHeal:对,刚刚编辑了包含更多内容的答案。最好包含一些关于你的建议的内容,而不仅仅是一个链接。 ;-) – netcoder