+ print $logfh "$time $msg\n";
+}
+
+sub mydie {
+ my $msg = shift;
+ log($msg);
+ exit 1;
+}
+
+sub readdata {
+ my $fh = shift;
+ my $in = undef;
+ my $out = undef;
+ my $bytesread = 0;
+ my $ret;
+
+ my $flags= fcntl($fh, F_GETFL, 0)
+ or mydie "Can't get flags for socket: $!\n";
+ fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
+ or mydie "Can't make socket nonblocking: $!\n";
+
+ while (($bytesread < 1024) && ($out !~ /\n/)) {
+ $ret = sysread($fh, $in, 1024);
+ return undef if (!defined($ret) || ($ret == 0));
+ $bytesread += $ret;
+ $out .= $in;
+ }
+
+ $out =~ /(.*?)\n/;
+ return $1;