Set the flag O_CLOEXEC on all file handles so they don't interferes with the execution of scripts. See #100

This commit is contained in:
Micke Prag 2012-12-04 12:20:19 +01:00
parent 3441b9cc45
commit 2cb1a2ac97
3 changed files with 22 additions and 10 deletions

View file

@ -57,7 +57,7 @@ void Socket::connect(const std::wstring &server) {
struct sockaddr_un remote;
socklen_t len;
if ((d->socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
if ((d->socket = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) {
return;
}
std::string name = "/tmp/" + std::string(server.begin(), server.end());

View file

@ -47,7 +47,7 @@ void ConnectionListener::run() {
SOCKET_T serverSocket;
struct sockaddr_un name;
serverSocket = socket(PF_LOCAL, SOCK_STREAM, 0);
serverSocket = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (serverSocket < 0) {
return;
}

View file

@ -99,7 +99,7 @@ int Settings::addNode(Node type) {
TelldusCore::MutexLocker locker(&mutex);
int intNodeId = getNextNodeId(type);
FILE *fp = fopen(CONFIG_FILE, "w");
FILE *fp = fopen(CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle
if (!fp) {
return TELLSTICK_ERROR_PERMISSION_DENIED;
}
@ -145,7 +145,7 @@ int Settings::getNextNodeId(Node type) const {
*/
int Settings::removeNode(Node type, int intNodeId) {
TelldusCore::MutexLocker locker(&mutex);
FILE *fp = fopen(CONFIG_FILE, "w");
FILE *fp = fopen(CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle
if (!fp) {
return TELLSTICK_ERROR_PERMISSION_DENIED;
}
@ -192,7 +192,7 @@ bool Settings::setDeviceState( int intDeviceId, int intDeviceState, const std::w
cfg_setint(cfg_device, "state", intDeviceState);
cfg_setstr(cfg_device, "stateValue", TelldusCore::wideToString(strDeviceStateValue).c_str());
FILE *fp = fopen(VAR_CONFIG_FILE, "w");
FILE *fp = fopen(VAR_CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle
if(fp == 0) {
return false;
}
@ -202,7 +202,7 @@ bool Settings::setDeviceState( int intDeviceId, int intDeviceState, const std::w
}
}
// The device is not found in the file, we must create it manualy...
FILE *fp = fopen(VAR_CONFIG_FILE, "w");
FILE *fp = fopen(VAR_CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle
if(!fp) {
fprintf(stderr, "Failed to write state to %s: %s\n",
VAR_CONFIG_FILE, strerror(errno));
@ -298,7 +298,7 @@ int Settings::setStringSetting(Node type, int intDeviceId, const std::wstring &n
return TELLSTICK_ERROR_CONFIG_SYNTAX;
}
cfg_setstr(p, TelldusCore::wideToString(name).c_str(), newValue.c_str());
FILE *fp = fopen(CONFIG_FILE, "w");
FILE *fp = fopen(CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle
if (!fp) {
return TELLSTICK_ERROR_PERMISSION_DENIED;
}
@ -345,7 +345,7 @@ int Settings::setIntSetting(Node type, int intDeviceId, const std::wstring &name
} else {
cfg_setint(cfg_device, TelldusCore::wideToString(name).c_str(), value);
}
FILE *fp = fopen(CONFIG_FILE, "w");
FILE *fp = fopen(CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle
if (!fp) {
return TELLSTICK_ERROR_PERMISSION_DENIED;
}
@ -403,12 +403,18 @@ bool readConfig(cfg_t **cfg) {
CFG_END()
};
FILE *fp = fopen(CONFIG_FILE, "re"); // e for setting O_CLOEXEC on the file handle
if (!fp) {
return false;
}
(*cfg) = cfg_init(opts, CFGF_NOCASE);
if (cfg_parse((*cfg), CONFIG_FILE) == CFG_PARSE_ERROR) {
if (cfg_parse_fp((*cfg), fp) == CFG_PARSE_ERROR) {
(*cfg) = 0;
fclose(fp);
return false;
}
fclose(fp);
return true;
}
@ -424,11 +430,17 @@ bool readVarConfig(cfg_t **cfg) {
CFG_END()
};
FILE *fp = fopen(VAR_CONFIG_FILE, "re"); // e for setting O_CLOEXEC on the file handle
if (!fp) {
return false;
}
(*cfg) = cfg_init(opts, CFGF_NOCASE);
if (cfg_parse((*cfg), VAR_CONFIG_FILE) == CFG_PARSE_ERROR) {
if (cfg_parse_fp((*cfg), fp) == CFG_PARSE_ERROR) {
(*cfg) = 0;
fclose(fp);
return false;
}
fclose(fp);
return true;
}