Implement platform_read, platform_write and replace all read()/write() calls

This commit is contained in:
Guido Vranken 2017-06-27 10:54:01 +02:00
parent c19da2307f
commit df5659cd97
12 changed files with 52 additions and 34 deletions

View file

@ -328,7 +328,7 @@ void
buf_write_string_file(const struct buffer *buf, const char *filename, int fd)
{
const int len = strlen((char *) BPTR(buf));
const int size = write(fd, BPTR(buf), len);
const int size = platform_write(fd, BPTR(buf), len);
if (size != len)
{
msg(M_ERR, "Write error on file '%s'", filename);

View file

@ -33,6 +33,7 @@
#include "syshead.h"
#include "console.h"
#include "misc.h"
#include "platform.h"
#include <systemd/sd-daemon.h>
@ -77,7 +78,7 @@ get_console_input_systemd(const char *prompt, const bool echo, char *input, cons
return false;
}
memset(input, 0, capacity);
if (read(std_out, input, capacity-1) != 0)
if (platform_read(std_out, input, capacity-1) != 0)
{
chomp(input);
ret = true;

View file

@ -1263,7 +1263,7 @@ read_key_file(struct key2 *key2, const char *file, const unsigned int flags)
{
msg(M_ERR, "Cannot open file key file '%s'", file);
}
size = read(fd, in.data, in.capacity);
size = platform_read(fd, in.data, in.capacity);
if (size < 0)
{
msg(M_FATAL, "Read error on key file ('%s')", file);

View file

@ -40,6 +40,7 @@
#include "error.h"
#include "misc.h"
#include "mstats.h"
#include "platform.h"
#include "memdbg.h"
@ -77,7 +78,7 @@ mstats_open(const char *fn)
* struct mmap_stats, and zero it */
CLEAR(ms);
ms.state = MSTATS_ACTIVE;
stat = write(fd, &ms, sizeof(ms));
stat = platform_write(fd, &ms, sizeof(ms));
if (stat != sizeof(ms))
{
msg(M_ERR, "mstats_open: write error: %s", fn);

View file

@ -44,6 +44,7 @@
#include "mstats.h"
#include "ssl_verify.h"
#include <inttypes.h>
#include "platform.h"
#include "memdbg.h"
@ -2119,7 +2120,7 @@ multi_process_file_closed(struct multi_context *m, const unsigned int mpp_flags)
{
char buffer[INOTIFY_EVENT_BUFFER_SIZE];
size_t buffer_i = 0;
int r = read(m->top.c2.inotify_fd, buffer, INOTIFY_EVENT_BUFFER_SIZE);
int r = platform_read(m->top.c2.inotify_fd, buffer, INOTIFY_EVENT_BUFFER_SIZE);
while (buffer_i < r)
{

View file

@ -43,6 +43,7 @@
#include "packet_id.h"
#include "misc.h"
#include "integer.h"
#include "platform.h"
#include "memdbg.h"
@ -460,7 +461,7 @@ packet_id_persist_load(struct packet_id_persist *p, const char *filename)
#endif
p->filename = filename;
n = read(p->fd, &image, sizeof(image));
n = platform_read(p->fd, &image, sizeof(image));
if (n == sizeof(image))
{
p->time = p->time_last_written = image.time;
@ -496,7 +497,7 @@ packet_id_persist_save(struct packet_id_persist *p)
seek_ret = lseek(p->fd, (off_t)0, SEEK_SET);
if (seek_ret == (off_t)0)
{
n = write(p->fd, &image, sizeof(image));
n = platform_write(p->fd, &image, sizeof(image));
if (n == sizeof(image))
{
p->time_last_written = p->time;

View file

@ -396,6 +396,14 @@ ssize_t platform_send(int sockfd, const void *buf, size_t len, int flags)
return send(sockfd, buf, len, flags);
}
ssize_t platform_read(int fd, void* buf, size_t len)
{
return platform_recv(fd, buf, len, 0);
}
ssize_t platform_write(int fd, const void* buf, size_t len)
{
return platform_send(fd, buf, len, 0);
}
ssize_t platform_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{

View file

@ -149,6 +149,8 @@ int platform_stat(const char *path, platform_stat_t *buf);
ssize_t platform_recv(int sockfd, void* buf, size_t len, int flags);
ssize_t platform_send(int sockfd, const void* buf, size_t len, int flags);
ssize_t platform_read(int fd, void* buf, size_t len);
ssize_t platform_write(int fd, const void* buf, size_t len);
ssize_t platform_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
char* platform_fgets(char *s, int size, FILE *stream);
int platform_fgetc(FILE *stream);

View file

@ -36,6 +36,7 @@
#include "fdmisc.h"
#include "crypto.h"
#include "ps.h"
#include "platform.h"
#include "memdbg.h"
@ -145,7 +146,7 @@ static int
recv_control(const socket_descriptor_t fd)
{
unsigned char c;
const ssize_t size = read(fd, &c, sizeof(c));
const ssize_t size = platform_read(fd, &c, sizeof(c));
if (size == sizeof(c))
{
return c;
@ -160,7 +161,7 @@ static int
send_control(const socket_descriptor_t fd, int code)
{
unsigned char c = (unsigned char) code;
const ssize_t size = write(fd, &c, sizeof(c));
const ssize_t size = platform_write(fd, &c, sizeof(c));
if (size == sizeof(c))
{
return (int) size;
@ -361,7 +362,7 @@ journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c
fd = platform_open(jfn, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP);
if (fd != -1)
{
if (write(fd, f, strlen(f)) != strlen(f))
if (platform_write(fd, f, strlen(f)) != strlen(f))
{
msg(M_WARN, "PORT SHARE: writing to journal file (%s) failed", jfn);
}

View file

@ -41,6 +41,7 @@
#include "manage.h"
#include "win32.h"
#include "options.h"
#include "platform.h"
#include "memdbg.h"
@ -3625,14 +3626,14 @@ get_default_gateway(struct route_gateway_info *rgi)
msg(M_WARN, "GDG: socket #1 failed");
goto done;
}
if (write(sockfd, (char *)&m_rtmsg, l) < 0)
if (platform_write(sockfd, (char *)&m_rtmsg, l) < 0)
{
msg(M_WARN, "GDG: problem writing to routing socket");
goto done;
}
do
{
l = read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg));
l = platform_read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg));
} while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
close(sockfd);
sockfd = -1;
@ -3852,7 +3853,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
msg(M_WARN, "GDG6: socket #1 failed");
goto done;
}
if (write(sockfd, (char *)&m_rtmsg, l) < 0)
if (platform_write(sockfd, (char *)&m_rtmsg, l) < 0)
{
msg(M_WARN, "GDG6: problem writing to routing socket");
goto done;
@ -3860,7 +3861,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
do
{
l = read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg));
l = platform_read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg));
}
while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));

View file

@ -33,6 +33,7 @@
#include "perf.h"
#include "misc.h"
#include "fdmisc.h"
#include "platform.h"
#include "memdbg.h"
@ -266,7 +267,7 @@ status_printf(struct status_output *so, const char *format, ...)
len = strlen(buf);
if (len > 0)
{
if (write(so->fd, buf, len) != len)
if (platform_write(so->fd, buf, len) != len)
{
so->errors = true;
}
@ -300,7 +301,7 @@ status_read(struct status_output *so, struct buffer *buf)
int len;
ASSERT(buf_init(&so->read_buf, 0));
len = read(so->fd, BPTR(&so->read_buf), BCAP(&so->read_buf));
len = platform_read(so->fd, BPTR(&so->read_buf), BCAP(&so->read_buf));
if (len <= 0)
{
break;

View file

@ -45,6 +45,7 @@
#include "manage.h"
#include "route.h"
#include "win32.h"
#include "platform.h"
#include "memdbg.h"
@ -1675,7 +1676,7 @@ write_tun_header(struct tuntap *tt, uint8_t *buf, int len)
}
else
{
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
}
@ -1696,7 +1697,7 @@ read_tun_header(struct tuntap *tt, uint8_t *buf, int len)
}
else
{
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
}
#endif /* if defined (TARGET_OPENBSD) || (defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H) */
@ -1908,13 +1909,13 @@ close_tun(struct tuntap *tt)
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
#elif defined(TARGET_LINUX)
@ -2177,13 +2178,13 @@ close_tun(struct tuntap *tt)
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
#elif defined(TARGET_SOLARIS)
@ -2745,7 +2746,7 @@ write_tun(struct tuntap *tt, uint8_t *buf, int len)
}
else
{
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
}
@ -2766,7 +2767,7 @@ read_tun(struct tuntap *tt, uint8_t *buf, int len)
}
else
{
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
}
@ -2869,7 +2870,7 @@ write_tun(struct tuntap *tt, uint8_t *buf, int len)
}
else
{
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
}
@ -2890,7 +2891,7 @@ read_tun(struct tuntap *tt, uint8_t *buf, int len)
}
else
{
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
}
@ -2964,7 +2965,7 @@ write_tun(struct tuntap *tt, uint8_t *buf, int len)
}
else
{
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
}
@ -2985,7 +2986,7 @@ read_tun(struct tuntap *tt, uint8_t *buf, int len)
}
else
{
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
}
@ -3221,7 +3222,7 @@ write_tun(struct tuntap *tt, uint8_t *buf, int len)
}
else
#endif
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
int
@ -3234,7 +3235,7 @@ read_tun(struct tuntap *tt, uint8_t *buf, int len)
}
else
#endif
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
#elif defined(TARGET_AIX)
@ -3370,13 +3371,13 @@ close_tun(struct tuntap *tt)
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
#elif defined(_WIN32)
@ -6359,13 +6360,13 @@ close_tun(struct tuntap *tt)
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return write(tt->fd, buf, len);
return platform_write(tt->fd, buf, len);
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return read(tt->fd, buf, len);
return platform_read(tt->fd, buf, len);
}
#endif /* if defined (TARGET_ANDROID) */