summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2014-03-25 20:08:31 +0800
committerpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2014-03-25 20:08:31 +0800
commit3c0359ecd073ce180b0e9df5849d12f4256ba7f1 (patch)
tree9fcc7ee26d7d8a86660de113c31a81e789cc5ac2
parentae435af58a482b0231e93e169188c302a5c48ff2 (diff)
downloadpttbbs-3c0359ecd073ce180b0e9df5849d12f4256ba7f1.tar
pttbbs-3c0359ecd073ce180b0e9df5849d12f4256ba7f1.tar.gz
pttbbs-3c0359ecd073ce180b0e9df5849d12f4256ba7f1.tar.bz2
pttbbs-3c0359ecd073ce180b0e9df5849d12f4256ba7f1.tar.lz
pttbbs-3c0359ecd073ce180b0e9df5849d12f4256ba7f1.tar.xz
pttbbs-3c0359ecd073ce180b0e9df5849d12f4256ba7f1.tar.zst
pttbbs-3c0359ecd073ce180b0e9df5849d12f4256ba7f1.zip
Prevent SIGPIPE handler breaking toconnect*.
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5966 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r--pttbbs/common/sys/net.c30
1 files changed, 16 insertions, 14 deletions
diff --git a/pttbbs/common/sys/net.c b/pttbbs/common/sys/net.c
index 143f4fd9..eea5b0ba 100644
--- a/pttbbs/common/sys/net.c
+++ b/pttbbs/common/sys/net.c
@@ -16,7 +16,7 @@
#ifndef DEFAULT_TCP_QLEN
#define DEFAULT_TCP_QLEN (10)
-#endif
+#endif
#ifndef DEFAULT_IOWRITE_EAGAIN_WAIT_MICROSECOND
#define DEFAULT_IOWRITE_EAGAIN_WAIT_MICROSECOND (10)
#endif
@@ -145,8 +145,7 @@ int toconnectex(const char *addr, int timeout)
int toconnect3(const char *addr, int timeout, int microseconds)
{
- int sock;
-
+ int sock, n = 1;
assert(addr && *addr);
if (!isdigit(addr[0]) && addr[0] != ':' && addr[0] != '*') {
@@ -156,6 +155,7 @@ int toconnect3(const char *addr, int timeout, int microseconds)
perror("socket");
return -1;
}
+ setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof(n));
serv_name.sun_family = AF_UNIX;
strlcpy(serv_name.sun_path, addr, sizeof(serv_name.sun_path));
@@ -175,6 +175,7 @@ int toconnect3(const char *addr, int timeout, int microseconds)
perror("socket");
return -1;
}
+ setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof(n));
if (timed)
{
@@ -200,26 +201,27 @@ int toconnect3(const char *addr, int timeout, int microseconds)
while (connect(sock, (struct sockaddr*)&serv_name, sizeof(serv_name)) < 0)
{
- if (errno == EINPROGRESS)
+ if (errno == EINPROGRESS)
{
- struct timeval tv = {0};
- fd_set myset;
+ struct timeval tv = {0};
+ fd_set myset;
assert(timed);
// set timeout here
tv.tv_sec = timeout;
tv.tv_usec = microseconds;
- FD_ZERO(&myset);
- FD_SET(sock, &myset);
+ FD_ZERO(&myset);
+ FD_SET(sock, &myset);
if (select(sock+1, NULL, &myset, NULL, &tv) > 0)
{
int result = 1;
socklen_t szresult = sizeof(result);
// szresult = 0: success, otherwise: failure.
- getsockopt(sock, SOL_SOCKET, SO_ERROR, &result, &szresult);
- if (result == 0)
+ if (getsockopt(sock, SOL_SOCKET, SO_ERROR,
+ &result, &szresult) == 0 && result == 0)
break;
+ // fprintf(stderr, "getsockopt SO_ERROR result = %d\n", result);
}
}
@@ -263,7 +265,7 @@ int is_to_readwrite_again(int s)
}
/**
- * same as read(2), but read until exactly size len
+ * same as read(2), but read until exactly size len
*/
int toread(int fd, void *buf, int len)
{
@@ -284,7 +286,7 @@ int toread(int fd, void *buf, int len)
}
/**
- * same as write(2), but write until exactly size len
+ * same as write(2), but write until exactly size len
*/
int towrite(int fd, const void *buf, int len)
{
@@ -305,7 +307,7 @@ int towrite(int fd, const void *buf, int len)
}
/**
- * same as recv(2), but recv until exactly size len
+ * same as recv(2), but recv until exactly size len
*/
int torecv(int fd, void *buf, int len, int flag)
{
@@ -326,7 +328,7 @@ int torecv(int fd, void *buf, int len, int flag)
}
/**
- * similiar to send(2), but send until exactly size len
+ * similiar to send(2), but send until exactly size len
*/
int tosend(int fd, const void *buf, int len, int flag)
{