Signed-off-by: Pierre Habouzit <madcoder@debian.org>
#include "common.h"
#include "daemon.h"
#include "common.h"
#include "daemon.h"
+static int setnonblock(int sock)
+{
+ int res = fcntl(sock, F_GETFL);
+
+ if (res < 0) {
+ UNIXERR("fcntl");
+ return -1;
+ }
+
+ if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0) {
+ UNIXERR("fcntl");
+ return -1;
+ }
+
+ return 0;
+}
+
+
int tcp_listen(const struct sockaddr *addr, socklen_t len)
{
int sock;
int tcp_listen(const struct sockaddr *addr, socklen_t len)
{
int sock;
+ if (setnonblock(sock)) {
+ close(sock);
+ return -1;
+ }
+
if (listen(sock, 0) < 0) {
UNIXERR("bind");
close(sock);
if (listen(sock, 0) < 0) {
UNIXERR("bind");
close(sock);
+int accept_nonblock(int fd)
+{
+ int sock = accept(fd, NULL, 0);
+
+ if (sock < 0) {
+ UNIXERR("accept");
+ return -1;
+ }
+
+ if (setnonblock(sock)) {
+ close(sock);
+ return -1;
+ }
+
+ return sock;
+}
#define POSTLICYD_DAEMON_H
int tcp_listen(const struct sockaddr *addr, socklen_t len);
#define POSTLICYD_DAEMON_H
int tcp_listen(const struct sockaddr *addr, socklen_t len);
+int accept_nonblock(int fd);
- sock = accept(srsd->fd, NULL, NULL);
+ sock = accept_nonblock(srsd->fd);
if (sock < 0) {
UNIXERR("accept");
continue;
if (sock < 0) {
UNIXERR("accept");
continue;