Commit b94ba026e0e61aac48f2fe244113640df90cb211

Authored by Stéphane Raimbault
1 parent 4cbc5252

Open fd and socket with the CLOEXEC flag when available

src/modbus-rtu.c
... ... @@ -373,6 +373,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
373 373 #else
374 374 struct termios tios;
375 375 speed_t speed;
  376 + int flags;
376 377 #endif
377 378 modbus_rtu_t *ctx_rtu = ctx->backend_data;
378 379  
... ... @@ -526,7 +527,12 @@ static int _modbus_rtu_connect(modbus_t *ctx)
526 527  
527 528 Timeouts are ignored in canonical input mode or when the
528 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 536 if (ctx->s == -1) {
531 537 fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
532 538 ctx_rtu->device, strerror(errno));
... ...
src/modbus-tcp.c
... ... @@ -244,6 +244,7 @@ static int _modbus_tcp_connect(modbus_t *ctx)
244 244 int rc;
245 245 struct sockaddr_in addr;
246 246 modbus_tcp_t *ctx_tcp = ctx->backend_data;
  247 + int flags = SOCK_STREAM;
247 248  
248 249 #ifdef OS_WIN32
249 250 if (_modbus_tcp_init_win32() == -1) {
... ... @@ -251,7 +252,11 @@ static int _modbus_tcp_connect(modbus_t *ctx)
251 252 }
252 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 260 if (ctx->s == -1) {
256 261 return -1;
257 262 }
... ...