From 66b81e7a0a27cc6bd7209642925cc1c6a41c7999 Mon Sep 17 00:00:00 2001 From: wangxiaopeng Date: Mon, 2 May 2016 05:09:22 -0400 Subject: [PATCH] bug 1017: make a sleep before retrying too much when xcatd trys to read the request data from an xcatd client, if there are too many retry, sleep for a while before retrying too much. --- xCAT-server/sbin/xcatd | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/xCAT-server/sbin/xcatd b/xCAT-server/sbin/xcatd index 2a6ee47cc..9a9b0a745 100644 --- a/xCAT-server/sbin/xcatd +++ b/xCAT-server/sbin/xcatd @@ -2465,23 +2465,41 @@ sub get_request { my $sock = shift; my $encode = shift; my $request = shift; + if ($encode eq "xml") { - my $line = $request; + my $readtry = 0; while ((!$request) || ($request !~ m/<\/xcatrequest>/)) { + if ($readtry > 10) { + # if retry too much, sleep for a while and try again + # otherwise it will use too much cpu + sleep 1; + $readtry = 0; + } my $flags=fcntl($sock,F_GETFL,0); $flags |= O_NONBLOCK; # we want sysread to bail on us, select seems to be evil to us still.. fcntl($sock,F_SETFL,$flags); my $bytesread; - if (!($line) ) { $line = ''; } + my $line = ''; do { $bytesread=sysread($sock,$line,65536,length($line)) } while ($bytesread); if (length($line)==0) { - if (not defined $bytesread and ($! == EAGAIN or $! == ECHILD)) { next; } # ECHILD makes no sense, but some platform does it + if (not defined $bytesread and ($! == EAGAIN or $! == ECHILD)) { + # retry when an error happens + # ECHILD makes no sense, but some platform does it + $readtry++; + next; + } return undef; } $flags=fcntl($sock,F_GETFL,0); $flags &= ~O_NONBLOCK; # now we want *print* to be blocking IO fcntl($sock,F_SETFL,$flags); - $request = $line; + $request .= $line; + + # check the validity of the request message + if ($request and length($request) > 15 and ($request !~ m//)) { + xCAT::MsgUtils->message("S", "xcatd: Close an invalid connection."); + return undef; + } } return eval { XMLin($request, SuppressEmpty=>undef,ForceArray=>1) }; } elsif ($encode eq "storable") {