Commit b94ba026e0e61aac48f2fe244113640df90cb211
1 parent
4cbc5252
Open fd and socket with the CLOEXEC flag when available
Showing
2 changed files
with
13 additions
and
2 deletions
src/modbus-rtu.c
| @@ -373,6 +373,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) | @@ -373,6 +373,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) | ||
| 373 | #else | 373 | #else |
| 374 | struct termios tios; | 374 | struct termios tios; |
| 375 | speed_t speed; | 375 | speed_t speed; |
| 376 | + int flags; | ||
| 376 | #endif | 377 | #endif |
| 377 | modbus_rtu_t *ctx_rtu = ctx->backend_data; | 378 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 378 | 379 | ||
| @@ -526,7 +527,12 @@ static int _modbus_rtu_connect(modbus_t *ctx) | @@ -526,7 +527,12 @@ static int _modbus_rtu_connect(modbus_t *ctx) | ||
| 526 | 527 | ||
| 527 | Timeouts are ignored in canonical input mode or when the | 528 | Timeouts are ignored in canonical input mode or when the |
| 528 | NDELAY option is set on the file via open or fcntl */ | 529 | NDELAY option is set on the file via open or fcntl */ |
| 529 | - ctx->s = open(ctx_rtu->device, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL); | 530 | + flags = O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL; |
| 531 | +#ifdef O_CLOEXEC | ||
| 532 | + flags |= O_CLOEXEC; | ||
| 533 | +#endif | ||
| 534 | + | ||
| 535 | + ctx->s = open(ctx_rtu->device, flags); | ||
| 530 | if (ctx->s == -1) { | 536 | if (ctx->s == -1) { |
| 531 | fprintf(stderr, "ERROR Can't open the device %s (%s)\n", | 537 | fprintf(stderr, "ERROR Can't open the device %s (%s)\n", |
| 532 | ctx_rtu->device, strerror(errno)); | 538 | ctx_rtu->device, strerror(errno)); |
src/modbus-tcp.c
| @@ -244,6 +244,7 @@ static int _modbus_tcp_connect(modbus_t *ctx) | @@ -244,6 +244,7 @@ static int _modbus_tcp_connect(modbus_t *ctx) | ||
| 244 | int rc; | 244 | int rc; |
| 245 | struct sockaddr_in addr; | 245 | struct sockaddr_in addr; |
| 246 | modbus_tcp_t *ctx_tcp = ctx->backend_data; | 246 | modbus_tcp_t *ctx_tcp = ctx->backend_data; |
| 247 | + int flags = SOCK_STREAM; | ||
| 247 | 248 | ||
| 248 | #ifdef OS_WIN32 | 249 | #ifdef OS_WIN32 |
| 249 | if (_modbus_tcp_init_win32() == -1) { | 250 | if (_modbus_tcp_init_win32() == -1) { |
| @@ -251,7 +252,11 @@ static int _modbus_tcp_connect(modbus_t *ctx) | @@ -251,7 +252,11 @@ static int _modbus_tcp_connect(modbus_t *ctx) | ||
| 251 | } | 252 | } |
| 252 | #endif | 253 | #endif |
| 253 | 254 | ||
| 254 | - ctx->s = socket(PF_INET, SOCK_STREAM, 0); | 255 | +#ifdef SOCK_CLOEXEC |
| 256 | + flags |= SOCK_CLOEXEC; | ||
| 257 | +#endif | ||
| 258 | + | ||
| 259 | + ctx->s = socket(PF_INET, flags, 0); | ||
| 255 | if (ctx->s == -1) { | 260 | if (ctx->s == -1) { |
| 256 | return -1; | 261 | return -1; |
| 257 | } | 262 | } |