1

我正在开发一个 C 程序,它与数据库建立连接,获取设备列表,然后分叉请求并创建与该设备的 SSH 连接。我遇到的问题是,有 700 个结果的查询总是在遇到 5 个分叉后从头开始。

从本质上讲,我已经研究pthreadglibc处理了线程,但是我发现的一些示例没有按预期工作,或者增加了太多复杂性。

我遇到的问题是,它会在 5 个孩子处停止,然后停止,而不是完成其余 700 台设备。

示例代码:

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

3 回答 3

2

connect_to_device退货后会发生什么?看起来该线程也将在 while 循环中启动,因为我没有看到该子进程退出,让您处于while children >=5循环中。您的情况下的 5 个和 5 个线程并非巧合。

运行的一些输出会有所帮助,同时也会减少代码。

尝试在没有 ssh 代码的情况下让您的脚手架工作。只是让进程停止和启动不需要 ssh。一旦确定底层支持有效,然后添加应用程序逻辑。

于 2013-01-10T14:10:59.303 回答
2

您的子进程正在退出 - 特别是没有退出状态 7 - 您的connect_to_device函数中有一个 return,但它被忽略了,每个子进程开始循环,创建更多的子进程。

你可能想要:return connect_to_device ( &row );相反。

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

于 2013-01-10T14:24:32.673 回答
1

也许ServerAliveCountMax正在妨碍:

从下面的 NETCODER 编辑

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

默认值为 3。例如,如果ServerAliveInterval(见下文)设置为 15 并且ServerAliveCountMax保留默认值,如果服务器变得无响应,则 ssh 将在大约 45 秒后断开连接。此选项仅适用于协议版本 2。

有关详细信息,请参见ssh_config ( )的手册页。man 5 ssh_config

于 2013-01-10T14:11:01.717 回答