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
This commit is contained in:
@@ -357,7 +357,7 @@ static int initHTTPConnection(InputStream * inStream)
|
|||||||
struct addrinfo *ans = NULL;
|
struct addrinfo *ans = NULL;
|
||||||
struct addrinfo *ap = NULL;
|
struct addrinfo *ap = NULL;
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
int error, flags;
|
int error;
|
||||||
InputStreamHTTPData *data = (InputStreamHTTPData *) inStream->data;
|
InputStreamHTTPData *data = (InputStreamHTTPData *) inStream->data;
|
||||||
/**
|
/**
|
||||||
* Setup hints
|
* Setup hints
|
||||||
@@ -397,8 +397,7 @@ static int initHTTPConnection(InputStream * inStream)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
flags = fcntl(data->sock, F_GETFL, 0);
|
set_nonblocking(data->sock);
|
||||||
fcntl(data->sock, F_SETFL, flags | O_NONBLOCK);
|
|
||||||
|
|
||||||
if (connect(data->sock, ap->ai_addr, ap->ai_addrlen) >= 0
|
if (connect(data->sock, ap->ai_addr, ap->ai_addrlen) >= 0
|
||||||
|| errno == EINPROGRESS) {
|
|| errno == EINPROGRESS) {
|
||||||
|
@@ -127,8 +127,6 @@ static void set_send_buf_size(Interface * interface)
|
|||||||
|
|
||||||
static void openInterface(Interface * interface, int fd)
|
static void openInterface(Interface * interface, int fd)
|
||||||
{
|
{
|
||||||
int flags;
|
|
||||||
|
|
||||||
assert(interface->fd < 0);
|
assert(interface->fd < 0);
|
||||||
|
|
||||||
interface->cmd_list_size = 0;
|
interface->cmd_list_size = 0;
|
||||||
@@ -137,8 +135,7 @@ static void openInterface(Interface * interface, int fd)
|
|||||||
interface->bufferLength = 0;
|
interface->bufferLength = 0;
|
||||||
interface->bufferPos = 0;
|
interface->bufferPos = 0;
|
||||||
interface->fd = fd;
|
interface->fd = fd;
|
||||||
while ((flags = fcntl(fd, F_GETFL)) < 0 && errno == EINTR) ;
|
set_nonblocking(fd);
|
||||||
while (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 && errno == EINTR) ;
|
|
||||||
interface->lastTime = time(NULL);
|
interface->lastTime = time(NULL);
|
||||||
interface->cmd_list = NULL;
|
interface->cmd_list = NULL;
|
||||||
interface->cmd_list_tail = NULL;
|
interface->cmd_list_tail = NULL;
|
||||||
|
@@ -93,7 +93,7 @@ static int establishListen(unsigned int port,
|
|||||||
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
|
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
|
||||||
FATAL("socket < 0\n");
|
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",
|
FATAL("problems setting nonblocking on listen socket: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
|
41
src/notify.c
41
src/notify.c
@@ -17,40 +17,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "notify.h"
|
#include "notify.h"
|
||||||
|
#include "os_compat.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include <assert.h>
|
void initNotify(Notify *notify)
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
int set_nonblock(int fd)
|
|
||||||
{
|
{
|
||||||
int ret;
|
if (pipe(notify->fds) < 0)
|
||||||
|
FATAL("Couldn't open pipe: %s", strerror(errno));
|
||||||
assert(fd >= 0);
|
if (set_nonblocking(notify->fds[1]) < 0)
|
||||||
|
FATAL("Couldn't set non-blocking on notify fd: %s",
|
||||||
ret = fcntl(fd, F_GETFL, 0);
|
strerror(errno));
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
return fcntl(fd, F_SETFL, ret|O_NONBLOCK);
|
|
||||||
}
|
|
||||||
|
|
||||||
int 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int waitNotify(Notify *notify)
|
int waitNotify(Notify *notify)
|
||||||
|
@@ -38,7 +38,7 @@ typedef struct _Notify {
|
|||||||
int fds[2];
|
int fds[2];
|
||||||
} Notify;
|
} Notify;
|
||||||
|
|
||||||
int initNotify(Notify *notify);
|
void initNotify(Notify *notify);
|
||||||
|
|
||||||
int waitNotify(Notify *notify);
|
int waitNotify(Notify *notify);
|
||||||
|
|
||||||
|
15
src/utils.c
15
src/utils.c
@@ -214,3 +214,18 @@ char *parsePath(char *path)
|
|||||||
|
|
||||||
return newPath;
|
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);
|
char *parsePath(char *path);
|
||||||
|
|
||||||
|
int set_nonblocking(int fd);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user