Use fcntl() to set FD_CLOEXEC on OS X since the SOCK_CLOEXEC flag for socket() isn't supported.

This commit is contained in:
Micke Prag 2012-12-05 11:18:07 +01:00
parent 38e624af66
commit 174c31f4ca
2 changed files with 17 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <math.h>
#include <string>
@ -17,6 +18,9 @@
#include "common/Strings.h"
#define BUFSIZE 512
#if defined(_MACOSX) && !defined(SOCK_CLOEXEC)
#define SOCK_CLOEXEC 0
#endif
namespace TelldusCore {
@ -60,6 +64,10 @@ void Socket::connect(const std::wstring &server) {
if ((d->socket = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) {
return;
}
#if defined(_MACOSX)
int op = fcntl(d->socket, F_GETFD);
fcntl(d->socket, op | FD_CLOEXEC); // OS X doesn't support SOCK_CLOEXEC yet
#endif
std::string name = "/tmp/" + std::string(server.begin(), server.end());
remote.sun_family = AF_UNIX;
snprintf(remote.sun_path, sizeof(remote.sun_path), "%s", name.c_str());

View file

@ -10,12 +10,17 @@
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string>
#include "service/ConnectionListener.h"
#include "common/Socket.h"
#if defined(_MACOSX) && !defined(SOCK_CLOEXEC)
#define SOCK_CLOEXEC 0
#endif
class ConnectionListener::PrivateData {
public:
TelldusCore::EventRef waitEvent;
@ -51,6 +56,10 @@ void ConnectionListener::run() {
if (serverSocket < 0) {
return;
}
#if defined(_MACOSX)
int op = fcntl(serverSocket, F_GETFD);
fcntl(serverSocket, op | FD_CLOEXEC); // OS X doesn't support SOCK_CLOEXEC yet
#endif
name.sun_family = AF_LOCAL;
memset(name.sun_path, '\0', sizeof(name.sun_path));
strncpy(name.sun_path, d->name.c_str(), sizeof(name.sun_path));