diff --git a/telldus-core/common/Socket_unix.cpp b/telldus-core/common/Socket_unix.cpp index 046cdaf3..3f8a68b7 100644 --- a/telldus-core/common/Socket_unix.cpp +++ b/telldus-core/common/Socket_unix.cpp @@ -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()); diff --git a/telldus-core/service/ConnectionListener_unix.cpp b/telldus-core/service/ConnectionListener_unix.cpp index 3ea0a55e..adef9cb8 100644 --- a/telldus-core/service/ConnectionListener_unix.cpp +++ b/telldus-core/service/ConnectionListener_unix.cpp @@ -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; } diff --git a/telldus-core/service/SettingsConfuse.cpp b/telldus-core/service/SettingsConfuse.cpp index f3049e8e..9c30fb7f 100644 --- a/telldus-core/service/SettingsConfuse.cpp +++ b/telldus-core/service/SettingsConfuse.cpp @@ -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; }