2010-10-25 04:49:15 -04:00
|
|
|
/* Synchronous socket and file I/O operations useful across the core.
|
2010-10-24 10:22:52 -04:00
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2015-07-26 09:14:57 -04:00
|
|
|
#include "server.h"
|
2010-10-24 10:22:52 -04:00
|
|
|
|
2010-10-25 04:49:15 -04:00
|
|
|
/* ----------------- Blocking sockets I/O with timeouts --------------------- */
|
|
|
|
|
|
|
|
/* Redis performs most of the I/O in a nonblocking way, with the exception
|
|
|
|
* of the SYNC command where the slave does it in a blocking way, and
|
|
|
|
* the MIGRATE command that must be blocking in order to be atomic from the
|
|
|
|
* point of view of the two instances (one migrating the key and one receiving
|
2012-03-31 05:21:45 -04:00
|
|
|
* the key). This is why need the following blocking I/O functions.
|
|
|
|
*
|
|
|
|
* All the functions take the timeout in milliseconds. */
|
|
|
|
|
2015-07-27 08:55:45 -04:00
|
|
|
#define SYNCIO__RESOLUTION 10 /* Resolution in milliseconds */
|
2010-10-25 04:49:15 -04:00
|
|
|
|
2012-05-02 16:41:50 -04:00
|
|
|
/* Write the specified payload to 'fd'. If writing the whole payload will be
|
|
|
|
* done within 'timeout' milliseconds the operation succeeds and 'size' is
|
|
|
|
* returned. Otherwise the operation fails, -1 is returned, and an unspecified
|
|
|
|
* partial write could be performed against the file descriptor. */
|
2012-03-31 05:21:45 -04:00
|
|
|
ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout) {
|
2010-10-24 10:22:52 -04:00
|
|
|
ssize_t nwritten, ret = size;
|
2012-03-31 05:21:45 -04:00
|
|
|
long long start = mstime();
|
|
|
|
long long remaining = timeout;
|
2010-10-24 10:22:52 -04:00
|
|
|
|
2012-03-31 05:21:45 -04:00
|
|
|
while(1) {
|
2015-07-27 08:55:45 -04:00
|
|
|
long long wait = (remaining > SYNCIO__RESOLUTION) ?
|
|
|
|
remaining : SYNCIO__RESOLUTION;
|
2012-03-31 05:21:45 -04:00
|
|
|
long long elapsed;
|
|
|
|
|
2012-05-02 16:41:50 -04:00
|
|
|
/* Optimistically try to write before checking if the file descriptor
|
|
|
|
* is actually writable. At worst we get EAGAIN. */
|
|
|
|
nwritten = write(fd,ptr,size);
|
|
|
|
if (nwritten == -1) {
|
|
|
|
if (errno != EAGAIN) return -1;
|
|
|
|
} else {
|
2010-10-24 10:22:52 -04:00
|
|
|
ptr += nwritten;
|
|
|
|
size -= nwritten;
|
|
|
|
}
|
2012-05-02 16:41:50 -04:00
|
|
|
if (size == 0) return ret;
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
aeWait(fd,AE_WRITABLE,wait);
|
2012-03-31 05:21:45 -04:00
|
|
|
elapsed = mstime() - start;
|
|
|
|
if (elapsed >= timeout) {
|
2010-10-24 10:22:52 -04:00
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-31 05:21:45 -04:00
|
|
|
remaining = timeout - elapsed;
|
2010-10-24 10:22:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 16:41:50 -04:00
|
|
|
/* Read the specified amount of bytes from 'fd'. If all the bytes are read
|
|
|
|
* within 'timeout' milliseconds the operation succeed and 'size' is returned.
|
2012-03-31 05:21:45 -04:00
|
|
|
* Otherwise the operation fails, -1 is returned, and an unspecified amount of
|
|
|
|
* data could be read from the file descriptor. */
|
|
|
|
ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout) {
|
2010-10-24 10:22:52 -04:00
|
|
|
ssize_t nread, totread = 0;
|
2012-03-31 05:21:45 -04:00
|
|
|
long long start = mstime();
|
|
|
|
long long remaining = timeout;
|
2010-10-24 10:22:52 -04:00
|
|
|
|
2012-05-02 16:41:50 -04:00
|
|
|
if (size == 0) return 0;
|
2012-03-31 05:21:45 -04:00
|
|
|
while(1) {
|
2015-07-27 08:55:45 -04:00
|
|
|
long long wait = (remaining > SYNCIO__RESOLUTION) ?
|
|
|
|
remaining : SYNCIO__RESOLUTION;
|
2012-03-31 05:21:45 -04:00
|
|
|
long long elapsed;
|
|
|
|
|
2012-05-02 16:41:50 -04:00
|
|
|
/* Optimistically try to read before checking if the file descriptor
|
|
|
|
* is actually readable. At worst we get EAGAIN. */
|
|
|
|
nread = read(fd,ptr,size);
|
|
|
|
if (nread == 0) return -1; /* short read. */
|
|
|
|
if (nread == -1) {
|
|
|
|
if (errno != EAGAIN) return -1;
|
|
|
|
} else {
|
2010-10-24 10:22:52 -04:00
|
|
|
ptr += nread;
|
|
|
|
size -= nread;
|
|
|
|
totread += nread;
|
|
|
|
}
|
2012-05-02 16:41:50 -04:00
|
|
|
if (size == 0) return totread;
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
aeWait(fd,AE_READABLE,wait);
|
2012-03-31 05:21:45 -04:00
|
|
|
elapsed = mstime() - start;
|
|
|
|
if (elapsed >= timeout) {
|
2010-10-24 10:22:52 -04:00
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-31 05:21:45 -04:00
|
|
|
remaining = timeout - elapsed;
|
2010-10-24 10:22:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-31 05:21:45 -04:00
|
|
|
/* Read a line making sure that every char will not require more than 'timeout'
|
2015-08-04 10:56:00 -04:00
|
|
|
* milliseconds to be read. Empty newlines before the first non-empty line
|
|
|
|
* are ignored. This is useful because since Redis sometimes uses empty
|
|
|
|
* newlines in order to take the connection "alive".
|
2014-06-26 12:48:40 -04:00
|
|
|
*
|
2012-03-31 05:21:45 -04:00
|
|
|
* On success the number of bytes read is returned, otherwise -1.
|
|
|
|
* On success the string is always correctly terminated with a 0 byte. */
|
|
|
|
ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout) {
|
2010-10-24 10:22:52 -04:00
|
|
|
ssize_t nread = 0;
|
|
|
|
|
|
|
|
size--;
|
|
|
|
while(size) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (syncRead(fd,&c,1,timeout) == -1) return -1;
|
|
|
|
if (c == '\n') {
|
2015-08-04 10:56:00 -04:00
|
|
|
/* Ignore empty lines, otherwise return to the caller. */
|
|
|
|
if (nread != 0) {
|
|
|
|
*ptr = '\0';
|
|
|
|
if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
|
|
|
|
return nread;
|
2015-08-05 10:51:50 -04:00
|
|
|
} else {
|
|
|
|
/* Read again with a fresh timeout. */
|
|
|
|
continue;
|
2015-08-04 10:56:00 -04:00
|
|
|
}
|
2010-10-24 10:22:52 -04:00
|
|
|
} else {
|
|
|
|
*ptr++ = c;
|
|
|
|
*ptr = '\0';
|
|
|
|
nread++;
|
|
|
|
}
|
2014-10-16 11:35:11 -04:00
|
|
|
size--;
|
2010-10-24 10:22:52 -04:00
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|