Commit 232c9f6c authored by Eric Wong's avatar Eric Wong

notify: cleanups

* move set_nonblock{,ing}() into utils.c since we use it elsewhere, too * add proper error checking to set_nonblocking() * use os_compat.h instead of individually #includ-ing system headers git-svn-id: https://svn.musicpd.org/mpd/trunk@7217 09075e82-0dd4-0310-85a5-a0d7c8717e4f
parent 70dbc2b0
......@@ -357,7 +357,7 @@ static int initHTTPConnection(InputStream * inStream)
struct addrinfo *ans = NULL;
struct addrinfo *ap = NULL;
struct addrinfo hints;
int error, flags;
int error;
InputStreamHTTPData *data = (InputStreamHTTPData *) inStream->data;
/**
* Setup hints
......@@ -397,8 +397,7 @@ static int initHTTPConnection(InputStream * inStream)
return -1;
}
flags = fcntl(data->sock, F_GETFL, 0);
fcntl(data->sock, F_SETFL, flags | O_NONBLOCK);
set_nonblocking(data->sock);
if (connect(data->sock, ap->ai_addr, ap->ai_addrlen) >= 0
|| errno == EINPROGRESS) {
......
......@@ -127,8 +127,6 @@ static void set_send_buf_size(Interface * interface)
static void openInterface(Interface * interface, int fd)
{
int flags;
assert(interface->fd < 0);
interface->cmd_list_size = 0;
......@@ -137,8 +135,7 @@ static void openInterface(Interface * interface, int fd)
interface->bufferLength = 0;
interface->bufferPos = 0;
interface->fd = fd;
while ((flags = fcntl(fd, F_GETFL)) < 0 && errno == EINTR) ;
while (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 && errno == EINTR) ;
set_nonblocking(fd);
interface->lastTime = time(NULL);
interface->cmd_list = NULL;
interface->cmd_list_tail = NULL;
......
......@@ -93,7 +93,7 @@ static int establishListen(unsigned int port,
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
FATAL("socket < 0\n");
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
if (set_nonblocking(sock) < 0) {
FATAL("problems setting nonblocking on listen socket: %s\n",
strerror(errno));
}
......
......@@ -17,40 +17,17 @@
*/
#include "notify.h"
#include "os_compat.h"
#include "log.h"
#include "utils.h"
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
int set_nonblock(int fd)
{
int ret;
assert(fd >= 0);
ret = fcntl(fd, F_GETFL, 0);
if (ret < 0)
return ret;
return fcntl(fd, F_SETFL, ret|O_NONBLOCK);
}
int initNotify(Notify *notify)
void initNotify(Notify *notify)
{
int ret;
ret = pipe(notify->fds);
if (ret < 0)
return -1;
ret = set_nonblock(notify->fds[1]);
if (ret < 0) {
close(notify->fds[0]);
close(notify->fds[1]);
return -1;
}
return 0;
if (pipe(notify->fds) < 0)
FATAL("Couldn't open pipe: %s", strerror(errno));
if (set_nonblocking(notify->fds[1]) < 0)
FATAL("Couldn't set non-blocking on notify fd: %s",
strerror(errno));
}
int waitNotify(Notify *notify)
......
......@@ -38,7 +38,7 @@ typedef struct _Notify {
int fds[2];
} Notify;
int initNotify(Notify *notify);
void initNotify(Notify *notify);
int waitNotify(Notify *notify);
......
......@@ -214,3 +214,18 @@ char *parsePath(char *path)
return newPath;
}
int set_nonblocking(int fd)
{
int ret, flags;
assert(fd >= 0);
while ((flags = fcntl(fd, F_GETFL)) < 0 && errno == EINTR) ;
if (flags < 0)
return flags;
flags |= O_NONBLOCK;
while ((ret = fcntl(fd, F_SETFL, flags)) < 0 && errno == EINTR) ;
return ret;
}
......@@ -79,4 +79,6 @@ mpd_malloc void *xcalloc(size_t nmemb, size_t size);
char *parsePath(char *path);
int set_nonblocking(int fd);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment