@@ -237,9 +237,10 @@ int handle_remote_data(libvchan_t *data_vchan, int stdin_fd, int *status,
237
237
if (hdr .len == 0 ) {
238
238
/* restore flags */
239
239
set_block (stdin_fd );
240
- if (shutdown (stdin_fd , SHUT_WR ) < 0 ) {
241
- if (errno == ENOTSOCK )
242
- close (stdin_fd );
240
+ if (!child_process_pid || stdin_fd == 1 ||
241
+ (shutdown (stdin_fd , SHUT_WR ) == -1 &&
242
+ errno == ENOTSOCK )) {
243
+ close (stdin_fd );
243
244
}
244
245
stdin_fd = -1 ;
245
246
return 0 ;
@@ -251,9 +252,10 @@ int handle_remote_data(libvchan_t *data_vchan, int stdin_fd, int *status,
251
252
return 1 ;
252
253
case WRITE_STDIN_ERROR :
253
254
if (errno == EPIPE || errno == ECONNRESET ) {
254
- if (shutdown (stdin_fd , SHUT_WR ) < 0 ) {
255
- if (errno == ENOTSOCK )
256
- close (stdin_fd );
255
+ if (!child_process_pid || stdin_fd == 1 ||
256
+ (shutdown (stdin_fd , SHUT_WR ) == -1 &&
257
+ errno == ENOTSOCK )) {
258
+ close (stdin_fd );
257
259
}
258
260
stdin_fd = -1 ;
259
261
} else {
@@ -317,9 +319,10 @@ int process_child_io(libvchan_t *data_vchan,
317
319
if (stdin_fd >= 0 ) {
318
320
/* restore flags */
319
321
set_block (stdin_fd );
320
- if (shutdown (stdin_fd , SHUT_WR ) < 0 ) {
321
- if (errno == ENOTSOCK )
322
- close (stdin_fd );
322
+ if (!child_process_pid || stdin_fd == 1 ||
323
+ (shutdown (stdin_fd , SHUT_WR ) == -1 &&
324
+ errno == ENOTSOCK )) {
325
+ close (stdin_fd );
323
326
}
324
327
stdin_fd = -1 ;
325
328
}
@@ -409,10 +412,12 @@ int process_child_io(libvchan_t *data_vchan,
409
412
stdin_fd = -1 ;
410
413
break ;
411
414
case -2 :
412
- /* remote process exited, no sense in sending more data to it */
413
- if (shutdown (stdout_fd , SHUT_RD ) < 0 ) {
414
- if (errno == ENOTSOCK )
415
- close (stdout_fd );
415
+ /* remote process exited, no sense in sending more data to it;
416
+ * be careful to not shutdown socket inherited from parent */
417
+ if (!child_process_pid || stdout_fd == 0 ||
418
+ (shutdown (stdout_fd , SHUT_RD ) == -1 &&
419
+ errno == ENOTSOCK )) {
420
+ close (stdout_fd );
416
421
}
417
422
stdout_fd = -1 ;
418
423
close (stderr_fd );
@@ -448,18 +453,20 @@ int process_child_io(libvchan_t *data_vchan,
448
453
if (stdout_fd != -1 ) {
449
454
/* restore flags */
450
455
set_block (stdout_fd );
451
- if (shutdown (stdout_fd , SHUT_RD ) < 0 ) {
452
- if (errno == ENOTSOCK )
453
- close (stdout_fd );
456
+ /* be careful to not shutdown socket inherited from parent */
457
+ if (!child_process_pid || stdout_fd == 0 ||
458
+ (shutdown (stdout_fd , SHUT_RD ) == -1 && errno == ENOTSOCK )) {
459
+ close (stdout_fd );
454
460
}
455
461
stdout_fd = -1 ;
456
462
}
457
463
if (stdin_fd != -1 ) {
458
464
/* restore flags */
459
465
set_block (stdin_fd );
460
- if (shutdown (stdin_fd , SHUT_WR ) < 0 ) {
461
- if (errno == ENOTSOCK )
462
- close (stdin_fd );
466
+ /* be careful to not shutdown socket inherited from parent */
467
+ if (!child_process_pid || stdin_fd == 1 ||
468
+ (shutdown (stdin_fd , SHUT_WR ) == -1 && errno == ENOTSOCK )) {
469
+ close (stdin_fd );
463
470
}
464
471
stdin_fd = -1 ;
465
472
}
0 commit comments