gpt4 book ai didi

C POSIX : read() give bad file descriptor error (in client) and select() freeze only at run time not in debug mode (in server)

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:21 25 4
gpt4 key购买 nike

我正在编写一个 OOB 信号应用程序,但我遇到了从客户端到服务器的通信问题。当我尝试发送一个 (uint64_t) 标识符时,send() 函数返回:错误的文件描述符错误。这只发生在运行时,似乎我没有调试问题应用程序使用 AF_UNIX 套接字,并不实际发送带外数据,如您从代码中看到的那样。

我使用此代码进行连接并从客户端发送到服务器:

int OOBclient_com() {

...

endiansUid=userID;
endiansUid=htobe64(userID);
*endUid=endiansUid;

for (index=0;index<w;index++){

clock_gettime(CLOCK_REALTIME,&secret_idle);
secret_idle.tv_sec+=secret/1000;
secret_idle.tv_nsec+=(secret%1000)*1000000;
errno=0;
readAndPopcheck=readAndPop(&randConnection_list)-1;
if (readAndPopcheck<0){
errormsg(err_buffer,ERROR,"%s","[client_configurator] elenco dei server corrotto");
pthread_exit((void*) -1);
}
printf("OOB_signal %d sent\n",index);
errno=0;
if((send(oob_skt[readAndPopcheck],endUid,sizeof(uint64_t),0)!=-1)&&(errno==0))

nanosleep(CLOCK_REALTIME,&secret_idle);

if (errno==EINTR)
errormsg(err_buffer,ERROR,"%s","[OOBclient_connect] idle time not respected");
if(errno!=0)
perror("send ");
}
for (index=0;index<p;index++){
close(oob_skt[index]);
}
/**/
printf("CLIENT %llx DONE",(unsigned long long int)userID);
return 0;
}

这是我的运行服务器函数:

    static void run_server() {

struct sockaddr_un psa;

int fd_sk;
fd_set readExpectedSktFds_set;

_Bool l_flag, c_flag;

/*cronometro della connesione*/
struct timespec starter;
struct timespec listenTime;
int disconnected_fd;
int fd_num;

disconnected_fd=0;
c_flag=l_flag=0;
fd_num=0;

if((pthread_mutex_lock(&names_mtx))<0)
errormsg(serv_name,CRITICAL,"%s","[run_server]");

strncpy(psa.sun_path,sock_name,UNIX_PATH_MAX);
psa.sun_family=AF_UNIX;

fd_sk = socket(AF_UNIX, SOCK_STREAM, 0);

if(bind(fd_sk,(struct sockaddr*)&psa, sizeof(psa))<0){
errormsg(serv_name,ERROR,"%s","[connection_acceptor] binding fallito");
if (remove(sock_name)<0 || bind(fd_sk,(struct sockaddr*)&psa, sizeof(psa))<0 )
pthread_exit((void*) -3);/*("[run_server] binding error");*/

else {printf("binding problem solved\n");}
}


if(listen(fd_sk, SOMAXCONN)<0){
pthread_exit((void*) -4);/*("[run_server] impossibile effettuare il listning");*/
}


if (fd_sk > fd_num)
fd_num = fd_sk;

FD_ZERO(&readExpectedSktFds_set);

FD_SET(fd_sk,&readExpectedSktFds_set);

if((pthread_mutex_lock(&fdskt_mtx))==0){

activeSktFds_set=readExpectedSktFds_set;
refresh_fd_num=fd_num;
fd_originalSk=fd_sk;
}
else
errormsg(serv_name,CRITICAL,"%s","[run_server]");

if((pthread_mutex_unlock(&fdskt_mtx))<0){
errormsg(serv_name,CRITICAL,"%s","[run_server]");
}
/*connection acceptor thread */
if(pthread_create(&client_connAccept, NULL,conn_acceptor, NULL)!=0)
serv_fatalErrhandler("[run_server] main socket error");

/*hash table manager thread */
if(pthread_create(&client_dataStruct, NULL,rec_organizer, NULL)!=0)
serv_fatalErrhandler("[run_server] data struct problem");

/*pending request queue thread*/
if(pthread_create(&queue_dataStruct, NULL,queue_manager, NULL)!=0)
serv_fatalErrhandler("[run_server] queue creation fail");

/*Supervisor pipe comunication thread*/
if(pthread_create(&pipe_dataSender, NULL,pipe_sender, NULL)!=0)
serv_fatalErrhandler("[run_server] pipe thread fail");


printf("%s ACTIVE\n",serv_name);

while(1) {
if(d_flag!=0){
if((pthread_mutex_lock(&nodeBuffer_mtx))==0){
dl_flag=1;

while((disconnected_fd=readAndPop(&fd_trash))!=-1){
FD_CLR(disconnected_fd,&activeSktFds_set);
}
}
else
serv_fatalErrhandler("[run_server] error");
}
if (c_flag!=0){
if((pthread_mutex_lock(&fdskt_mtx))==0){
readExpectedSktFds_set = activeSktFds_set;
fd_num=refresh_fd_num;
l_flag=1;
}
else
serv_fatalErrhandler("[run_server] error");
}
clock_gettime(CLOCK_REALTIME,&listenTime);
listenTime.tv_sec+=(((SECRET_RANGE_LIMIT*2)/1000));
listenTime.tv_nsec=0;

if ((pselect(fd_num + 1, &readExpectedSktFds_set, NULL, NULL,&listenTime,NULL)) < 0){
serv_fatalErrhandler("[run_server] impossibile gestire le connessioni OOB");

}
clock_gettime(CLOCK_REALTIME,&starter);
for (fd = 0; fd <= fd_num; fd++) {
if (FD_ISSET(fd, &readExpectedSktFds_set)) {
/* socket ready connection to the original socket*/
if (fd == fd_sk){
c_flag=1;
d_flag=0;
if(l_flag!=1)
if (pthread_mutex_trylock(&fdskt_mtx)==0)
l_flag=1;

if(l_flag!=0){
if ((pthread_mutex_unlock(&fdskt_mtx)!=0) || (pthread_cond_signal(&fdskt_check))!=0){
serv_fatalErrhandler("[run_server] unlock acceptor: error");
}
l_flag=0;
}
else
printf ("%s CONNECT FROM CLIENT REFUSED.\n",serv_name);
}
/* sock I/0 pronto: Stanno arrivando dati su socket gia' connessi*/
else {
c_flag=0;
d_flag=1;
if(dl_flag!=1)
if((pthread_mutex_lock(&nodeBuffer_mtx))<0)
dl_flag=1;

node_timerec.tv_nsec =starter.tv_nsec;
node_timerec.tv_sec=starter.tv_sec;
fd_sender=fd;

if(dl_flag!=0){
if ((pthread_cond_signal(&nodeBuffer_check))!=0)
serv_fatalErrhandler("[run_server] queue: cond signal error");

if (pthread_mutex_unlock(&nodeBuffer_mtx)!=0)
serv_fatalErrhandler("[run_server] queue: unlock error");
}
}
}
}
}
}

这是运行时发生的事情:

./客户 1 1 4

CLIENT c1 SECRET 621 

OOB_signal 0 sent

send : Bad address
...

OOB_signal 3 sent

send : Bad address

./服务器 1 100 200

( SERVER-1 ) [connection_acceptor] binding fallito

Address already in use

binding problem solved

SERVER-1 ACTIVE

SERVER-1 CONNECT FROM CLIENT.

SERVER-1 INCOMING FROM 4800000000000000 @ 1404935101.597292187

5 分钟后,我按 : ^C 和 ...

*** Error in `./server': double free or corruption (fasttop): 0x00007f5608001270 ***
======= Backtrace: =========
/lib64/libc.so.6[0x30f787d0b8]
./server[0x4034ea]
./server[0x401cfe]
./server[0x4030be]
/lib64/libpthread.so.0[0x30f800ef90]
/lib64/libc.so.6(pselect+0xa9)[0x30f78ed629]
./server[0x401710]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x30f7821b45]
./server[0x4018d9]
======= Memory map: ========
00400000-00405000 r-xp 00000000 08:02 929019

/home/hellion/OOB_sockDIR/server
00604000-00605000 r--p 00004000 08:02 929019 /home/hellion/OOB_sockDIR/server
00605000-00606000 rw-p 00005000 08:02 929019 /home/hellion/OOB_sockDIR/server
00606000-00608000 rw-p 00000000 00:00 0
01e49000-01e6a000 rw-p 00000000 00:00 0 [heap]
30f7400000-30f7421000 r-xp 00000000 08:02 1182569 /usr/lib64/ld-2.17.so
30f7620000-30f7621000 r--p 00020000 08:02 1182569 /usr/lib64/ld-2.17.so
30f7621000-30f7622000 rw-p 00021000 08:02 1182569 /usr/lib64/ld-2.17.so
30f7622000-30f7623000 rw-p 00000000 00:00 0
30f7800000-30f79b6000 r-xp 00000000 08:02 1182900 /usr/lib64/libc-2.17.so
30f79b6000-30f7bb6000 ---p 001b6000 08:02 1182900

....

30f9a14000-30f9a15000 r--p 00014000 08:02 1186784                        /usr/lib64/libgcc_s-4.8.2-20131212.so.1
30f9a15000-30f9a16000 rw-p 00015000 08:02 1186784 /usr/lib64/libgcc_s-4.8.2-20131212.so.1
7f55f8000000-7f55fb3fb000 rw-p 00000000 00:00 0
7f55fb3fb000-7f55fc000000 ---p 00000000 00:00 0
7f55fc000000-7f55fc021000 rw-p 00000000 00:00 0
7f55fc021000-7f5600000000 ---p 00000000 00:00 0
7f5600000000-7f5600021000 rw-p 00000000 00:00 0
7f5600021000-7f5604000000 ---p 00000000 00:00 0
7f5606a00000-7f5606a01000 ---p 00000000 00:00 0
7f5606a01000-7f5607201000 rw-p 00000000 00:00 0
7f5608000000-7f5608021000 rw-p 00000000 00:00 0
7f5608021000-7f560c000000 ---p 00000000 00:00 0
7f560f201000-7f560f202000 ---p 00000000 00:00 0
7f560f202000-7f560fa02000 rw-p 00000000 00:00 0 [stack:12630]
7f560fa02000-7f560fa03000 ---p 00000000 00:00 0
7f560fa03000-7f5610203000 rw-p 00000000 00:00 0 [stack:12628]
7f5610203000-7f5610204000 ---p 00000000 00:00 0
7f5610204000-7f5610a04000 rw-p 00000000 00:00 0
7f5610a04000-7f5610a05000 ---p 00000000 00:00 0
7f5610a05000-7f5611208000 rw-p 00000000 00:00 0 [stack:12626]
7f5611229000-7f561122c000 rw-p 00000000 00:00 0
7fff5129f000-7fff512c0000 rw-p 00000000 00:00 0 [stack]
7fff513fe000-7fff51400000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Annullato (core dumped)

感谢关注!

wildplasser:感谢您的回答。我按照您的建议修改了代码,但输出没有改变。我希望这个修复是正确的:

_Bool read_clientMsg (int fd_r ,uint64_t* client_userID) {

_Bool retry=1;
uint64_t msg_buffer=0;
int readed_num=0;

do {
readed_num=recv(fd_r,(uint64_t*)&msg_buffer,sizeof(uint64_t),0);

if ((readed_num==-1)){
if((errno==EAGAIN) || (errno==EINTR))
retry=0;
else{
errormsg(serv_name,ERROR,"%s","[read_from_client] read fallita ");
}
}
if (readed_num==0)
return 1;

else{
/*da big a small*/
msg_buffer=htole64(msg_buffer);
*client_userID=msg_buffer;
retry=0;
}

}while(retry);

return (0);

}这是服务器的 strace 输出:

strace -ttT ./server 1 100 200 >servLOG
02:25:44.236556 execve("./server", ["./server", "1", "100", "200"], [/* 51 vars */]) = 0 <0.023989>
02:25:44.260845 brk(0) = 0xa2a000 <0.000014>
02:25:44.260981 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8889be4000 <0.000017>
02:25:44.261066 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) <0.000019>
02:25:44.261168 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 <0.000023>
02:25:44.261234 fstat(3, {st_mode=S_IFREG|0644, st_size=141126, ...}) = 0 <0.000014>
02:25:44.261292 mmap(NULL, 141126, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f8889bc1000 <0.000016>
02:25:44.261340 close(3) = 0 <0.000014>
02:25:44.261406 open("/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3 <0.000027>
02:25:44.261466 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0k\0\3700\0\0\0"..., 832) = 832 <0.000016>
02:25:44.261524 fstat(3, {st_mode=S_IFREG|0755, st_size=144672, ...}) = 0 <0.000021>
02:25:44.261583 mmap(0x30f8000000, 2208800, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x30f8000000 <0.000019>
02:25:44.261635 mprotect(0x30f8016000, 2097152, PROT_NONE) = 0 <0.000028>
02:25:44.261695 mmap(0x30f8216000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x30f8216000 <0.000024>
02:25:44.261767 mmap(0x30f8218000, 13344, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x30f8218000 <0.000018>
02:25:44.261823 close(3) = 0 <0.000013>
02:25:44.261904 open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 <0.000022>
02:25:44.261960 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\34\202\3670\0\0\0"..., 832) = 832 <0.000014>
02:25:44.262009 fstat(3, {st_mode=S_IFREG|0755, st_size=2108632, ...}) = 0 <0.000013>
02:25:44.262061 mmap(0x30f7800000, 3932768, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x30f7800000 <0.000017>
02:25:44.262110 mprotect(0x30f79b6000, 2097152, PROT_NONE) = 0 <0.000022>
02:25:44.262162 mmap(0x30f7bb6000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b6000) = 0x30f7bb6000 <0.000019>
02:25:44.262225 mmap(0x30f7bbc000, 16992, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x30f7bbc000 <0.000018>
02:25:44.262281 close(3) = 0 <0.000012>
02:25:44.262330 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8889bc0000 <0.000015>
02:25:44.262400 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8889bbe000 <0.000015>
02:25:44.262459 arch_prctl(ARCH_SET_FS, 0x7f8889bbe740) = 0 <0.000013>
02:25:44.262766 mprotect(0x30f7bb6000, 16384, PROT_READ) = 0 <0.000021>
02:25:44.263128 mprotect(0x30f8216000, 4096, PROT_READ) = 0 <0.000020>
02:25:44.263391 mprotect(0x604000, 4096, PROT_READ) = 0 <0.000019>
02:25:44.263651 mprotect(0x30f7620000, 4096, PROT_READ) = 0 <0.000020>
02:25:44.263928 munmap(0x7f8889bc1000, 141126) = 0 <0.000030>
02:25:44.264201 set_tid_address(0x7f8889bbea10) = 14265 <0.000014>
02:25:44.264447 set_robust_list(0x7f8889bbea20, 24) = 0 <0.000013>
02:25:44.264705 rt_sigaction(SIGRTMIN, {0x30f8006660, [], SA_RESTORER|SA_SIGINFO, 0x30f800ef90}, NULL, 8) = 0 <0.000014>
02:25:44.264995 rt_sigaction(SIGRT_1, {0x30f80066f0, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x30f800ef90}, NULL, 8) = 0 <0.000013>
02:25:44.265252 rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0 <0.000013>
02:25:44.265515 getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0 <0.000013>
02:25:44.265828 rt_sigaction(SIGINT, {0x403020, [], SA_RESTORER|SA_SIGINFO, 0x30f800ef90}, NULL, 8) = 0 <0.000015>
02:25:44.266170 brk(0) = 0xa2a000 <0.000013>
02:25:44.266418 brk(0xa4b000) = 0xa4b000 <0.000015>
02:25:44.266672 brk(0) = 0xa4b000 <0.000013>
02:25:44.267040 socket(PF_LOCAL, SOCK_STREAM, 0) = 3 <0.000062>
02:25:44.267345 bind(3, {sa_family=AF_LOCAL, sun_path="./OOB-server-1"}, 110) = -1 EADDRINUSE (Address already in use) <0.000021>
02:25:44.267640 write(2, "( SERVER-1 ) [connection_accepto"..., 74( SERVER-1 ) [connection_acceptor] binding fallito
Address already in use
) = 74 <0.000180>
02:25:44.267944 unlink("./OOB-server-1") = 0 <0.000055>
02:25:44.268138 bind(3, {sa_family=AF_LOCAL, sun_path="./OOB-server-1"}, 110) = 0 <0.000102>
02:25:44.268301 fstat(1, {st_mode=S_IFREG|0664, st_size=0, ...}) = 0 <0.000013>
02:25:44.268373 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8889be3000 <0.000020>
02:25:44.268712 listen(3, 128) = 0 <0.000013>
02:25:44.268781 mmap(NULL, 8392704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f88893bd000 <0.000014>
02:25:44.268855 mprotect(0x7f88893bd000, 4096, PROT_NONE) = 0 <0.000017>
02:25:44.268941 clone(child_stack=0x7f8889bbcfb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CL ONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f8889bbd9d0, tls=0x7f8889bbd700, child_tidptr=0x7f8889bbd9d0) = 14266 <0.000040>
02:25:44.269210 mmap(NULL, 8392704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f8888bbc000 <0.000016>
02:25:44.269287 mprotect(0x7f8888bbc000, 4096, PROT_NONE) = 0 <0.000016>
02:25:44.269354 clone(child_stack=0x7f88893bbfb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CL ONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f88893bc9d0, tls=0x7f88893bc700, child_tidptr=0x7f88893bc9d0) = 14267 <0.000026>
02:25:44.269431 mmap(NULL, 8392704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f88883bb000 <0.000014>
02:25:44.269501 mprotect(0x7f88883bb000, 4096, PROT_NONE) = 0 <0.000014>
02:25:44.269563 clone(child_stack=0x7f8888bbafb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CL ONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f8888bbb9d0, tls=0x7f8888bbb700, child_tidptr=0x7f8888bbb9d0) = 14268 <0.000025>
02:25:44.269637 mmap(NULL, 8392704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f8887bba000 <0.000014>
02:25:44.269707 mprotect(0x7f8887bba000, 4096, PROT_NONE) = 0 <0.000015>
02:25:44.269769 clone(child_stack=0x7f88883b9fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CL ONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f88883ba9d0, tls=0x7f88883ba700, child_tidptr=0x7f88883ba9d0) = 14269 <0.000025>
02:25:44.269856 pselect6(4, [3], NULL, NULL, {1404951950, 0}, {NULL, 8}

/和客户端连接之后/

   02:28:13.095795 futex(0x607564, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0x607560, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1 <0.000016>
02:28:13.095857 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999995391}) <0.000020>
02:28:13.095941 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996159}) <0.000019>
02:28:13.096005 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996577}) <0.000019>
02:28:13.096068 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996508}) <0.000018>
02:28:13.096131 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999995390}) <0.000020>
02:28:13.096195 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996298}) <0.000017>
02:28:13.096257 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996159}) <0.000017>
02:28:13.096321 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996298}) <0.000018>
02:28:13.096383 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996368}) <0.000017>
02:28:13.096446 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996019}) <0.000019>
02:28:13.096509 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996438}) <0.000017>
02:28:13.096572 pselect6(4, [3], NULL, NULL, {1404952099, 0}, {NULL, 8}) = 1 (in [3], left {1404952098, 999996158}) <0.000019>

/*my ctrl-c*/

这是客户端的 strace 输出:

strace -ttT ./client 1 1 4
02:28:12.975914 execve("./client", ["./client", "1", "1", "4"], [/* 51 vars */]) = 0 <0.043949>
02:28:13.020545 brk(0) = 0x739000 <0.000014>
02:28:13.020649 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9840d22000 <0.000016>
02:28:13.020728 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) <0.000019>
02:28:13.040967 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 <0.000025>
02:28:13.041073 fstat(3, {st_mode=S_IFREG|0644, st_size=141126, ...}) = 0 <0.000014>
02:28:13.041137 mmap(NULL, 141126, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f9840cff000 <0.000021>
02:28:13.041191 close(3) = 0 <0.000013>
02:28:13.041255 open("/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3 <0.000025>
02:28:13.041312 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0k\0\3700\0\0\0"..., 832) = 832 <0.000016>
02:28:13.041369 fstat(3, {st_mode=S_IFREG|0755, st_size=144672, ...}) = 0 <0.000013>
02:28:13.041418 mmap(0x30f8000000, 2208800, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x30f8000000 <0.000018>
02:28:13.041469 mprotect(0x30f8016000, 2097152, PROT_NONE) = 0 <0.000025>
02:28:13.041523 mmap(0x30f8216000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x30f8216000 <0.000027>
02:28:13.055274 mmap(0x30f8218000, 13344, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x30f8218000 <0.000035>
02:28:13.055393 close(3) = 0 <0.000013>
02:28:13.061829 open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 <0.000045>
02:28:13.061987 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\34\202\3670\0\0\0"..., 832) = 832 <0.000017>
02:28:13.062049 fstat(3, {st_mode=S_IFREG|0755, st_size=2108632, ...}) = 0 <0.000089>
02:28:13.062192 mmap(0x30f7800000, 3932768, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x30f7800000 <0.000023>
02:28:13.062269 mprotect(0x30f79b6000, 2097152, PROT_NONE) = 0 <0.000048>
02:28:13.062355 mmap(0x30f7bb6000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b6000) = 0x30f7bb6000 <0.000029>
02:28:13.062434 mmap(0x30f7bbc000, 16992, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x30f7bbc000 <0.000019>
02:28:13.062491 close(3) = 0 <0.000014>
02:28:13.062548 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9840cfe000 <0.000016>
02:28:13.062616 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9840cfc000 <0.000015>
02:28:13.062677 arch_prctl(ARCH_SET_FS, 0x7f9840cfc740) = 0 <0.000014>
02:28:13.062845 mprotect(0x30f7bb6000, 16384, PROT_READ) = 0 <0.000032>
02:28:13.062930 mprotect(0x30f8216000, 4096, PROT_READ) = 0 <0.000022>
02:28:13.062986 mprotect(0x603000, 4096, PROT_READ) = 0 <0.000019>
02:28:13.063039 mprotect(0x30f7620000, 4096, PROT_READ) = 0 <0.000021>
02:28:13.063091 munmap(0x7f9840cff000, 141126) = 0 <0.000034>
02:28:13.063159 set_tid_address(0x7f9840cfca10) = 14298 <0.000013>
02:28:13.063205 set_robust_list(0x7f9840cfca20, 24) = 0 <0.000013>
02:28:13.063259 rt_sigaction(SIGRTMIN, {0x30f8006660, [], SA_RESTORER|SA_SIGINFO, 0x30f800ef90}, NULL, 8) = 0 <0.000014>
02:28:13.063320 rt_sigaction(SIGRT_1, {0x30f80066f0, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x30f800ef90}, NULL, 8) = 0 <0.000013>
02:28:13.063371 rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0 <0.000013>
02:28:13.063424 getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0 <0.000013>
02:28:13.063558 mmap(NULL, 8392704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f98404fb000 <0.000016>
02:28:13.063670 brk(0) = 0x739000 <0.000013>
02:28:13.063714 brk(0x75a000) = 0x75a000 <0.000016>
02:28:13.063759 brk(0) = 0x75a000 <0.000013>
02:28:13.063813 mprotect(0x7f98404fb000, 4096, PROT_NONE) = 0 <0.000015>
02:28:13.063866 clone(child_stack=0x7f9840cfafb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f9840cfb9d0, tls=0x7f9840cfb700, child_tidptr=0x7f9840cfb9d0) = 14299 <0.000039>
02:28:13.093967 futex(0x6042a0, FUTEX_WAKE_PRIVATE, 1) = 1 <0.000021>
02:28:13.094073 futex(0x604264, FUTEX_WAIT_PRIVATE, 1, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000013>
02:28:13.094151 socket(PF_LOCAL, SOCK_STREAM, 0) = 3 <0.000062>
02:28:13.094251 connect(3, {sa_family=AF_LOCAL, sun_path="./OOB-server-1"}, 110) = 0 <0.000070>
02:28:13.094365 futex(0x6041c4, FUTEX_WAIT_PRIVATE, 1, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000013>
02:28:13.094439 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0 <0.000014>
02:28:13.094494 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9840d21000 <0.000016>
02:28:13.094557 write(1, "CLIENT e5 SECRET 2041 \n", 23CLIENT e5 SECRET 2041
) = 23 <0.000016>
02:28:13.094615 write(1, "OOB_signal 0 sent\n", 18OOB_signal 0 sent
) = 18 <0.000015>
02:28:13.094665 sendto(3, "\0\0\0\0\0\0\0\345", 8, 0, NULL, 0) = 8 <0.000016>
02:28:13.094716 nanosleep(NULL, 0x7fff60b53aa0) = -1 EFAULT (Bad address) <0.000017>
02:28:13.094773 dup(2) = 4 <0.000012>
02:28:13.094816 fcntl(4, F_GETFL) = 0x8002 (flags O_RDWR|O_LARGEFILE) <0.000014>
02:28:13.094863 fstat(4, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0 <0.000014>
02:28:13.094926 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9840d20000 <0.000015>
02:28:13.094975 lseek(4, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) <0.000012>
02:28:13.095044 write(4, "send : Bad address\n", 19send : Bad address
) = 19 <0.000015>
02:28:13.095093 close(4) = 0 <0.000013>
02:28:13.095135 munmap(0x7f9840d20000, 4096) = 0 <0.000024>
02:28:13.095193 write(1, "OOB_signal 1 sent\n", 18OOB_signal 1 sent
) = 18 <0.000016>
02:28:13.095241 sendto(3, "\0\0\0\0\0\0\0\345", 8, 0, NULL, 0) = 8 <0.000015>
02:28:13.095289 nanosleep(NULL, 0x7fff60b53aa0) = -1 EFAULT (Bad address) <0.000015>
02:28:13.095335 dup(2) = 4 <0.000013>
02:28:13.095378 fcntl(4, F_GETFL) = 0x8002 (flags O_RDWR|O_LARGEFILE) <0.000014>
02:28:13.095423 fstat(4, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0 <0.000013>
02:28:13.095471 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9840d20000 <0.000015>
02:28:13.095519 lseek(4, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) <0.000013>
02:28:13.095571 write(4, "send : Bad address\n", 19send : Bad address
) = 19 <0.000015>
02:28:13.095620 close(4) = 0 <0.004135>
02:28:13.099811 munmap(0x7f9840d20000, 4096) = 0 <0.000055>
02:28:13.099981 write(1, "OOB_signal 2 sent\n", 18OOB_signal 2 sent
) = 18 <0.000023>
02:28:13.100151 sendto(3, "\0\0\0\0\0\0\0\345", 8, 0, NULL, 0) = 8 <0.000025>
02:28:13.100322 nanosleep(NULL, 0x7fff60b53aa0) = -1 EFAULT (Bad address) <0.000023>
02:28:13.100487 dup(2) = 4 <0.000014>
02:28:13.100593 fcntl(4, F_GETFL) = 0x8002 (flags O_RDWR|O_LARGEFILE) <0.000013>
02:28:13.100741 fstat(4, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0 <0.000055>
02:28:13.100997 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9840d20000 <0.000021>
02:28:13.101115 lseek(4, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) <0.000012>
02:28:13.101286 write(4, "send : Bad address\n", 19send : Bad address
) = 19 <0.000021>
02:28:13.101470 close(4) = 0 <0.000013>
02:28:13.101573 munmap(0x7f9840d20000, 4096) = 0 <0.000027>
02:28:13.101733 write(1, "OOB_signal 3 sent\n", 18OOB_signal 3 sent
) = 18 <0.000021>
02:28:13.101898 sendto(3, "\0\0\0\0\0\0\0\345", 8, 0, NULL, 0) = 8 <0.000018>
02:28:13.102046 nanosleep(NULL, 0x7fff60b53aa0) = -1 EFAULT (Bad address) <0.000017>
02:28:13.102144 dup(2) = 4 <0.000013>
02:28:13.102320 fcntl(4, F_GETFL) = 0x8002 (flags O_RDWR|O_LARGEFILE) <0.000012>
02:28:13.102427 fstat(4, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0 <0.000013>
02:28:13.102577 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9840d20000 <0.000019>
02:28:13.102725 lseek(4, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) <0.000013>
02:28:13.102835 write(4, "send : Bad address\n", 19send : Bad address
) = 19 <0.000058>
02:28:13.103006 close(4) = 0 <0.000012>
02:28:13.103107 munmap(0x7f9840d20000, 4096) = 0 <0.000022>
02:28:13.103264 close(3) = 0 <0.000024>
02:28:13.103411 write(1, "CLIENT e5 DONE", 14CLIENT e5 DONE) = 14 <0.000018>
02:28:13.103614 exit_group(0) = ?
02:28:13.103904 +++ exited with 0 +++

最佳答案

strace 显示 nanosleep() 失败 EFAULT,即“Bad Address”,并将 errno 设置为该地址。您在调用 nanosleep() 之后获取 errno 并将其视为 send() 的结果,它具有,事实上,成功了!

最好假设 errno 可能会被对您直接控制之外的任何内容的任何调用覆盖。此外,如果函数不返回“错误”,那么最好将 errno 视为未定义。总而言之,这意味着如果(且仅当)函数返回错误指示,则在调用任何其他函数之前获取并使用 errno,或将其保存。

关于C POSIX : read() give bad file descriptor error (in client) and select() freeze only at run time not in debug mode (in server),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24661969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com