我正在开发一个C程序,它与数据库建立连接,抓取设备列表,然后分叉请求并创建与该设备的SSH连接。我遇到的问题是,具有700个结果的查询总是在它遇到5个分叉后从头开始。
基本上,我已经研究过pthread
和glibc
来处理线程,但是我发现的一些例子没有按预期工作,或者增加了太多的复杂性。
我遇到的问题是,它将停留在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;
}
答案 0 :(得分:2)
connect_to_device
返回后会发生什么?看起来该线程也将在while循环中启动,因为我没有看到子进程退出,让你坐在while children >=5
循环中。在你的条件和5线程中的5不是巧合。
运行中的某些输出会有所帮助,同时也会削减代码。
尝试让脚手架工作,没有ssh代码。只是让进程停止并启动不应该需要ssh。然后,一旦确定底层支持有效,就添加应用程序逻辑。
答案 1 :(得分:2)
您的子进程正在退出 - 特别是退出状态7 - 您的connect_to_device
函数返回了,但忽略了这一点,并且每个孩子开始绕圈,创建更多的孩子。
你可能想要:return connect_to_device ( &row );
代替。
wait()
返回死亡的子PID,而不是其状态 - 位于WEXITSTATUS(status)
。
答案 2 :(得分:1)
或许ServerAliveCountMax
正在阻碍:
从NETCODER下面编辑
ServerAliveCountMax
设置服务器活动消息的数量(请参阅 可以在没有 ssh 的情况下发送(1)接收任何消息 从服务器。如果在服务器活动时达到此阈值 邮件正在发送,ssh将与服务器断开连接, 终止会话。重要的是要注意使用 服务器活动消息与 TCPKeepAlive (下面)非常不同。该 服务器活动消息通过加密通道发送 因此不会是可欺骗的。启用TCP keepalive选项 TCPKeepAlive 是可欺骗的。服务器活动机制很有价值 客户端或服务器依赖于知道连接何时成为 不活动的。默认值为3.例如,如果 ServerAliveInterval (请参阅 如果设置为15,则 ServerAliveCountMax 保留默认值 服务器无响应,ssh将断开连接 大约45秒。此选项适用于协议版本2 仅
有关详细信息,请参阅man page for ssh_config(man 5 ssh_config
)。