mysql - C fork 在 5 个 child 后停止

标签 mysql c ssh fork

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

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

我遇到的问题是,它会在 5 个 child 处停止,然后停止,而不是完成剩余的 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;
}

最佳答案

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

您可能需要:return connect_to_device ( &row );

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

关于mysql - C fork 在 5 个 child 后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14259651/

相关文章:

bash - 执行后Bash脚本不会退出SSH主机

php - 使用 SQLite3 数据库的 PHP 登录脚本的问题

mysql - 插入新的增量值

PHP永不重复的随机数

python - 无法调用Python C扩展中的方法

c - 有没有一种巧妙的方法来执行 strdup() 后跟 strcat()?

php - 将标签附加到 MySQL 数据库中的一行

c - 以下代码将输出什么以及为什么?谁能解释一下吗?

关闭终端时 Bash 脚本无法正常运行

c# - 如何使用 WinSCP 和 C# 从服务器获取 SSH 指纹