All files / lib/internal child_process.js

96.64% Statements 1094/1132
90.41% Branches 302/334
97.87% Functions 46/47
96.64% Lines 1094/1132

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 62x 113x 113x 113x 60x 60x 113x 113x 113x 113x 113x 113x 1x 113x 113x 113x 1x 1x 1x 1x 1x 113x 113x 113x 113x 38x 38x 37x 37x 37x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 37x 37x 37x 37x 37x 37x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 1x 1x 1x 1x 36x 37x 37x 113x 113x 113x 37x 37x 37x 37x 36x 36x 36x 36x 36x 36x 36x 36x     36x 113x 113x 113x 93x 93x 93x 93x 93x 93x 93x 93x 7x 7x 7x 7x 7x 7x 7x 93x 93x 93x 113x 113x 113x 113x 113x 113x 31x 113x 113x 113x 31x 31x 113x 113x 113x 113x 113x 113x 1x 1x 1x 113x 113x 113x 1x 1x 1x 1x 1x 1x 113x 113x 113x 2719x 2719x 2719x 2719x 2719x 2719x 2719x 2719x 2719x 2x 2719x 2717x 2717x 2717x 2717x 2719x 113x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2062x 2006x 219x 2003x 1787x 1787x 2006x 2006x 1253x 1253x 2006x 2006x 2006x 2006x 2006x 5x 5x 5x 5x 5x 5x 5x 5x 2006x 2001x 2001x 1995x 1995x 1995x 1995x 1995x 1995x 1995x 1995x 1995x 1995x 2062x 2062x 1x 1x 1x 1x 2062x 113x 113x 113x 113x 1992x 1992x 1992x 1992x 1991x 1991x 6501x 6501x 6501x 6501x 6501x 6368x 6368x 133x 133x 1992x 113x 113x 3873x 3873x 3873x 113x 113x 48x 48x 48x 48x 48x 34x 34x 48x 113x 35x 35x 35x 35x 113x 113x 113x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 544x 544x 544x 544x 544x 536x 536x 536x 536x 536x 2069x 2069x 2069x 2069x 2090x   2090x 2061x 2061x 2061x 2053x 2053x 2053x 2053x 2053x 2090x 2090x 2090x 2090x 5x 5x 5x 5x 5x 5x 2090x 2x 2x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2048x 2046x 2046x 2050x 2050x 2050x 2087x 6694x 6694x 6640x 6688x 536x 536x 536x 6104x 6104x 6104x 6104x 6682x 7x 7x 7x 7x 7x 7x 7x 7x 6097x 6631x 3873x 3873x 3873x 3873x 2584x 2584x 2508x 2584x 2584x 3873x 6694x 2050x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 2090x 6694x 2050x 2050x 2083x 2050x 2050x 113x 113x 113x 5x 5x 5x 113x 113x 2046x 2046x 2046x 113x 113x 113x 295x 295x 295x 295x 295x 258x 258x 258x 258x 258x 258x                   258x 36x 36x 36x 113x 113x 113x 113x 1x 113x 113x 113x 113x 4x 113x 113x 6x 6x 6x 6x 6x 6x 949x 949x 949x 949x 6x 6x 6x 6x 6x 6x 874x 404x 404x 874x 6x 6x 439x 142x 142x 142x 439x 6x 6x       6x 6x 1x 1x 1x 6x 6x 48x 48x 6x 113x 113x 113x 113x 113x 949x 949x 949x 949x 949x 949x 949x 949x 1x 949x 949x   949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 4276x 4276x 4276x 3743x 3743x 3743x 3743x 3743x 3743x 3743x 3445x 3445x 3445x 3445x 1325x 186x 186x 1293x 1139x 1139x 3445x 2120x 2120x 3445x 4276x 533x 533x 533x 533x 533x 533x 533x 949x 949x 949x 949x 949x 949x 949x 1415x 1415x 1415x 130x 130x 35x 35x 35x           35x 130x 130x 130x 130x 130x 130x           130x 130x 125x 125x 125x 130x 130x 130x 130x 130x 130x 130x 1285x 1293x 186x 186x 186x 186x 186x 347x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 347x     186x 186x 186x 186x 186x 949x 949x 949x 4476x 4476x 4238x 4238x 3535x 3535x 3535x 3538x 1x 1x 1x 1x 1x 1x 4476x 949x 949x 949x 5372x 7x 7x 7x 5370x 4x 4x 5365x 15x 15x 5358x 5358x 5358x 5358x 5355x 5355x 3x 6x 1x 5x 2x 2x 3x 949x 949x 949x 5688x 5688x 5688x 5688x 5686x 5686x 5686x 5686x 5686x 5686x 5688x 5688x 5688x 1x 1x 1x 5685x 5685x 5688x 208x 208x 5685x 5685x 5685x 5685x 5688x 226x 226x 226x 226x 226x 226x 226x 226x 126x 226x 1x 100x 64x 99x 1x 35x 34x 34x     226x 226x 226x 93x 93x 93x 93x 93x 93x 93x 93x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 226x 133x 133x 226x     5688x 34x 5459x 32x 32x 32x 32x 32x 32x 32x 32x 32x 5560x 5560x 5560x 5560x 5560x 5560x 5560x 3509x 132x 132x 132x 132x 132x 3509x 3509x 137x 137x 137x 137x 137x 137x 3490x 6x 6x 5688x 2051x 2051x 2051x 2051x 2051x 2051x 2051x   2051x 2051x 2051x 2051x 2051x 5560x 5560x 5560x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 949x 739x 1x 1x 1x 738x 738x 738x 738x 738x 738x 738x 738x 738x 949x 949x 949x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x 736x           736x 736x 949x 949x 949x 3631x 3628x 3628x 3628x 3x 3x 3x 3x 3x 3631x 949x 949x 3631x 3631x 3631x 3631x 3631x 3631x 3631x 949x 949x 949x 949x 113x 113x 3631x 3631x 3631x 3631x 3631x 1419x 3631x 3631x 113x 113x 113x 3060x 3060x 3060x 3060x 3060x 3060x 2230x 2852x 1x 1x 3058x 3058x 3058x 3058x 3058x 3060x 3058x 3058x 3058x 3058x 9722x 4x 4x 4x 4x 4x 9722x 9722x 9722x 20x 20x 9722x 9722x 81x 9716x 9641x 6775x 6775x 6775x 6775x 6775x 6775x 6775x 6775x 6775x 6775x 9641x 547x 2x 2x 2x 2x 1x 1x 2x 545x 545x 545x 545x 545x 545x 545x 545x 545x 2866x 156x 156x 156x 156x 2319x 2154x 2154x 2154x 2154x 2163x 9x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9718x 9718x 3058x 3058x 3058x 3060x 113x 113x 38x 38x 38x 38x 16x 16x 16x 38x 38x 113x 113x 5036x 5036x 5036x 5036x 1984x 1984x 5036x 113x 964x 964x 964x 964x 263x 789x 789x 520x 520x 263x 964x 964x 964x 964x 964x 158x 158x 158x 158x 964x 964x 964x 113x 113x 113x 113x 113x 113x 113x 113x 113x  
'use strict';
 
const {
  ArrayIsArray,
  ArrayPrototypePush,
  ArrayPrototypeReduce,
  ArrayPrototypeSlice,
  FunctionPrototype,
  FunctionPrototypeCall,
  ObjectDefineProperty,
  ObjectSetPrototypeOf,
  ReflectApply,
  StringPrototypeSlice,
  Symbol,
  Uint8Array,
} = primordials;
 
const {
  errnoException,
  codes: {
    ERR_INVALID_ARG_TYPE,
    ERR_INVALID_ARG_VALUE,
    ERR_INVALID_HANDLE_TYPE,
    ERR_INVALID_SYNC_FORK_INPUT,
    ERR_IPC_CHANNEL_CLOSED,
    ERR_IPC_DISCONNECTED,
    ERR_IPC_ONE_PIPE,
    ERR_IPC_SYNC_FORK,
    ERR_MISSING_ARGS
  }
} = require('internal/errors');
const {
  validateArray,
  validateObject,
  validateOneOf,
  validateString,
} = require('internal/validators');
const EventEmitter = require('events');
const net = require('net');
const dgram = require('dgram');
const inspect = require('internal/util/inspect').inspect;
const assert = require('internal/assert');
 
const { Process } = internalBinding('process_wrap');
const {
  WriteWrap,
  kReadBytesOrError,
  kArrayBufferOffset,
  kLastWriteWasAsync,
  streamBaseState
} = internalBinding('stream_wrap');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
const { TCP } = internalBinding('tcp_wrap');
const { TTY } = internalBinding('tty_wrap');
const { UDP } = internalBinding('udp_wrap');
const SocketList = require('internal/socket_list');
const { owner_symbol } = require('internal/async_hooks').symbols;
const { convertToValidSignal, deprecate } = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
const spawn_sync = internalBinding('spawn_sync');
const { kStateSymbol } = require('internal/dgram');
const dc = require('diagnostics_channel');
const childProcessChannel = dc.channel('child_process');
 
const {
  UV_EACCES,
  UV_EAGAIN,
  UV_EINVAL,
  UV_EMFILE,
  UV_ENFILE,
  UV_ENOENT,
  UV_ENOSYS,
  UV_ESRCH
} = internalBinding('uv');
 
const { SocketListSend, SocketListReceive } = SocketList;
 
// Lazy loaded for startup performance and to allow monkey patching of
// internalBinding('http_parser').HTTPParser.
let freeParser;
let HTTPParser;
 
const MAX_HANDLE_RETRANSMISSIONS = 3;
const kChannelHandle = Symbol('kChannelHandle');
const kIsUsedAsStdio = Symbol('kIsUsedAsStdio');
const kPendingMessages = Symbol('kPendingMessages');
 
// This object contain function to convert TCP objects to native handle objects
// and back again.
const handleConversion = {
  'net.Native': {
    simultaneousAccepts: true,
 
    send(message, handle, options) {
      return handle;
    },
 
    got(message, handle, emit) {
      emit(handle);
    }
  },
 
  'net.Server': {
    simultaneousAccepts: true,
 
    send(message, server, options) {
      return server._handle;
    },
 
    got(message, handle, emit) {
      const server = new net.Server();
      server.listen(handle, () => {
        emit(server);
      });
    }
  },
 
  'net.Socket': {
    send(message, socket, options) {
      if (!socket._handle)
        return;
 
      // If the socket was created by net.Server
      if (socket.server) {
        // The worker should keep track of the socket
        message.key = socket.server._connectionKey;
 
        const firstTime = !this[kChannelHandle].sockets.send[message.key];
        const socketList = getSocketList('send', this, message.key);
 
        // The server should no longer expose a .connection property
        // and when asked to close it should query the socket status from
        // the workers
        if (firstTime) socket.server._setupWorker(socketList);
 
        // Act like socket is detached
        if (!options.keepOpen)
          socket.server._connections--;
      }
 
      const handle = socket._handle;
 
      // Remove handle from socket object, it will be closed when the socket
      // will be sent
      if (!options.keepOpen) {
        handle.onread = nop;
        socket._handle = null;
        socket.setTimeout(0);
 
        if (freeParser === undefined)
          freeParser = require('_http_common').freeParser;
        if (HTTPParser === undefined)
          HTTPParser = require('_http_common').HTTPParser;
 
        // In case of an HTTP connection socket, release the associated
        // resources
        if (socket.parser && socket.parser instanceof HTTPParser) {
          freeParser(socket.parser, null, socket);
          if (socket._httpMessage)
            socket._httpMessage.detachSocket(socket);
        }
      }
 
      return handle;
    },
 
    postSend(message, handle, options, callback, target) {
      // Store the handle after successfully sending it, so it can be closed
      // when the NODE_HANDLE_ACK is received. If the handle could not be sent,
      // just close it.
      if (handle && !options.keepOpen) {
        if (target) {
          // There can only be one _pendingMessage as passing handles are
          // processed one at a time: handles are stored in _handleQueue while
          // waiting for the NODE_HANDLE_ACK of the current passing handle.
          assert(!target._pendingMessage);
          target._pendingMessage =
              { callback, message, handle, options, retransmissions: 0 };
        } else {
          handle.close();
        }
      }
    },
 
    got(message, handle, emit) {
      const socket = new net.Socket({
        handle: handle,
        readable: true,
        writable: true
      });
 
      // If the socket was created by net.Server we will track the socket
      if (message.key) {
 
        // Add socket to connections list
        const socketList = getSocketList('got', this, message.key);
        socketList.add({
          socket: socket
        });
      }
 
      emit(socket);
    }
  },
 
  'dgram.Native': {
    simultaneousAccepts: false,
 
    send(message, handle, options) {
      return handle;
    },
 
    got(message, handle, emit) {
      emit(handle);
    }
  },
 
  'dgram.Socket': {
    simultaneousAccepts: false,
 
    send(message, socket, options) {
      message.dgramType = socket.type;
 
      return socket[kStateSymbol].handle;
    },
 
    got(message, handle, emit) {
      const socket = new dgram.Socket(message.dgramType);
 
      socket.bind(handle, () => {
        emit(socket);
      });
    }
  }
};
 
function stdioStringToArray(stdio, channel) {
  const options = [];
 
  switch (stdio) {
    case 'ignore':
    case 'overlapped':
    case 'pipe': ArrayPrototypePush(options, stdio, stdio, stdio); break;
    case 'inherit': ArrayPrototypePush(options, 0, 1, 2); break;
    default:
      throw new ERR_INVALID_ARG_VALUE('stdio', stdio);
  }
 
  if (channel) ArrayPrototypePush(options, channel);
 
  return options;
}
 
function ChildProcess() {
  FunctionPrototypeCall(EventEmitter, this);
 
  this._closesNeeded = 1;
  this._closesGot = 0;
  this.connected = false;
 
  this.signalCode = null;
  this.exitCode = null;
  this.killed = false;
  this.spawnfile = null;
 
  this._handle = new Process();
  this._handle[owner_symbol] = this;
 
  this._handle.onexit = (exitCode, signalCode) => {
    if (signalCode) {
      this.signalCode = signalCode;
    } else {
      this.exitCode = exitCode;
    }
 
    if (this.stdin) {
      this.stdin.destroy();
    }
 
    this._handle.close();
    this._handle = null;
 
    if (exitCode < 0) {
      const syscall = this.spawnfile ? 'spawn ' + this.spawnfile : 'spawn';
      const err = errnoException(exitCode, syscall);
 
      if (this.spawnfile)
        err.path = this.spawnfile;
 
      err.spawnargs = ArrayPrototypeSlice(this.spawnargs, 1);
      this.emit('error', err);
    } else {
      this.emit('exit', this.exitCode, this.signalCode);
    }
 
    // If any of the stdio streams have not been touched,
    // then pull all the data through so that it can get the
    // eof and emit a 'close' event.
    // Do it on nextTick so that the user has one last chance
    // to consume the output, if for example they only want to
    // start reading the data once the process exits.
    process.nextTick(flushStdio, this);
 
    maybeClose(this);
  };
  if (childProcessChannel.hasSubscribers) {
    childProcessChannel.publish({
      process: this,
    });
  }
}
ObjectSetPrototypeOf(ChildProcess.prototype, EventEmitter.prototype);
ObjectSetPrototypeOf(ChildProcess, EventEmitter);
 
 
function flushStdio(subprocess) {
  const stdio = subprocess.stdio;
 
  if (stdio == null) return;
 
  for (let i = 0; i < stdio.length; i++) {
    const stream = stdio[i];
    // TODO(addaleax): This doesn't necessarily account for all the ways in
    // which data can be read from a stream, e.g. being consumed on the
    // native layer directly as a StreamBase.
    if (!stream || !stream.readable || stream[kIsUsedAsStdio]) {
      continue;
    }
    stream.resume();
  }
}
 
 
function createSocket(pipe, readable) {
  return net.Socket({ handle: pipe, readable });
}
 
 
function getHandleWrapType(stream) {
  if (stream instanceof Pipe) return 'pipe';
  if (stream instanceof TTY) return 'tty';
  if (stream instanceof TCP) return 'tcp';
  if (stream instanceof UDP) return 'udp';
 
  return false;
}
 
function closePendingHandle(target) {
  target._pendingMessage.handle.close();
  target._pendingMessage = null;
}
 
 
ChildProcess.prototype.spawn = function(options) {
  let i = 0;
 
  validateObject(options, 'options');
 
  // If no `stdio` option was given - use default
  let stdio = options.stdio || 'pipe';
 
  stdio = getValidStdio(stdio, false);
 
  const ipc = stdio.ipc;
  const ipcFd = stdio.ipcFd;
  stdio = options.stdio = stdio.stdio;
 
 
  validateOneOf(options.serialization, 'options.serialization',
                [undefined, 'json', 'advanced']);
  const serialization = options.serialization || 'json';
 
  if (ipc !== undefined) {
    // Let child process know about opened IPC channel
    if (options.envPairs === undefined)
      options.envPairs = [];
    else
      validateArray(options.envPairs, 'options.envPairs');
 
    ArrayPrototypePush(options.envPairs, `NODE_CHANNEL_FD=${ipcFd}`);
    ArrayPrototypePush(options.envPairs,
                       `NODE_CHANNEL_SERIALIZATION_MODE=${serialization}`);
  }
 
  validateString(options.file, 'options.file');
  this.spawnfile = options.file;
 
  if (options.args === undefined) {
    this.spawnargs = [];
  } else {
    validateArray(options.args, 'options.args');
    this.spawnargs = options.args;
  }
 
  const err = this._handle.spawn(options);
 
  // Run-time errors should emit an error, not throw an exception.
  if (err === UV_EACCES ||
      err === UV_EAGAIN ||
      err === UV_EMFILE ||
      err === UV_ENFILE ||
      err === UV_ENOENT) {
    process.nextTick(onErrorNT, this, err);
 
    // There is no point in continuing when we've hit EMFILE or ENFILE
    // because we won't be able to set up the stdio file descriptors.
    if (err === UV_EMFILE || err === UV_ENFILE)
      return err;
  } else if (err) {
    // Close all opened fds on error
    for (i = 0; i < stdio.length; i++) {
      const stream = stdio[i];
      if (stream.type === 'pipe') {
        stream.handle.close();
      }
    }
 
    this._handle.close();
    this._handle = null;
    throw errnoException(err, 'spawn');
  } else {
    process.nextTick(onSpawnNT, this);
  }
 
  this.pid = this._handle.pid;
 
  for (i = 0; i < stdio.length; i++) {
    const stream = stdio[i];
    if (stream.type === 'ignore') continue;
 
    if (stream.ipc) {
      this._closesNeeded++;
      continue;
    }
 
    // The stream is already cloned and piped, thus stop its readable side,
    // otherwise we might attempt to read from the stream when at the same time
    // the child process does.
    if (stream.type === 'wrap') {
      stream.handle.reading = false;
      stream.handle.readStop();
      stream._stdio.pause();
      stream._stdio.readableFlowing = false;
      stream._stdio._readableState.reading = false;
      stream._stdio[kIsUsedAsStdio] = true;
      continue;
    }
 
    if (stream.handle) {
      stream.socket = createSocket(this.pid !== 0 ?
        stream.handle : null, i > 0);
 
      if (i > 0 && this.pid !== 0) {
        this._closesNeeded++;
        stream.socket.on('close', () => {
          maybeClose(this);
        });
      }
    }
  }
 
  this.stdin = stdio.length >= 1 && stdio[0].socket !== undefined ?
    stdio[0].socket : null;
  this.stdout = stdio.length >= 2 && stdio[1].socket !== undefined ?
    stdio[1].socket : null;
  this.stderr = stdio.length >= 3 && stdio[2].socket !== undefined ?
    stdio[2].socket : null;
 
  this.stdio = [];
 
  for (i = 0; i < stdio.length; i++)
    ArrayPrototypePush(this.stdio,
                       stdio[i].socket === undefined ? null : stdio[i].socket);
 
  // Add .send() method and start listening for IPC data
  if (ipc !== undefined) setupChannel(this, ipc, serialization);
 
  return err;
};
 
 
function onErrorNT(self, err) {
  self._handle.onexit(err);
}
 
 
function onSpawnNT(self) {
  self.emit('spawn');
}
 
 
ChildProcess.prototype.kill = function(sig) {
 
  const signal = sig === 0 ? sig :
    convertToValidSignal(sig === undefined ? 'SIGTERM' : sig);
 
  if (this._handle) {
    const err = this._handle.kill(signal);
    if (err === 0) {
      /* Success. */
      this.killed = true;
      return true;
    }
    if (err === UV_ESRCH) {
      /* Already dead. */
    } else if (err === UV_EINVAL || err === UV_ENOSYS) {
      /* The underlying platform doesn't support this signal. */
      throw errnoException(err, 'kill');
    } else {
      /* Other error, almost certainly EPERM. */
      this.emit('error', errnoException(err, 'kill'));
    }
  }
 
  /* Kill didn't succeed. */
  return false;
};
 
 
ChildProcess.prototype.ref = function() {
  if (this._handle) this._handle.ref();
};
 
 
ChildProcess.prototype.unref = function() {
  if (this._handle) this._handle.unref();
};
 
class Control extends EventEmitter {
  #channel = null;
  #refs = 0;
  #refExplicitlySet = false;
 
  constructor(channel) {
    super();
    this.#channel = channel;
    this[kPendingMessages] = [];
  }
 
  // The methods keeping track of the counter are being used to track the
  // listener count on the child process object as well as when writes are
  // in progress. Once the user has explicitly requested a certain state, these
  // methods become no-ops in order to not interfere with the user's intentions.
  refCounted() {
    if (++this.#refs === 1 && !this.#refExplicitlySet) {
      this.#channel.ref();
    }
  }
 
  unrefCounted() {
    if (--this.#refs === 0 && !this.#refExplicitlySet) {
      this.#channel.unref();
      this.emit('unref');
    }
  }
 
  ref() {
    this.#refExplicitlySet = true;
    this.#channel.ref();
  }
 
  unref() {
    this.#refExplicitlySet = true;
    this.#channel.unref();
  }
 
  get fd() {
    return this.#channel ? this.#channel.fd : undefined;
  }
}
 
const channelDeprecationMsg = '_channel is deprecated. ' +
                              'Use ChildProcess.channel instead.';
 
let serialization;
function setupChannel(target, channel, serializationMode) {
  const control = new Control(channel);
  target.channel = control;
  target[kChannelHandle] = channel;
 
  ObjectDefineProperty(target, '_channel', {
    __proto__: null,
    get: deprecate(() => {
      return target.channel;
    }, channelDeprecationMsg, 'DEP0129'),
    set: deprecate((val) => {
      target.channel = val;
    }, channelDeprecationMsg, 'DEP0129'),
    configurable: true,
    enumerable: false
  });
 
  target._handleQueue = null;
  target._pendingMessage = null;
 
  if (serialization === undefined)
    serialization = require('internal/child_process/serialization');
  const {
    initMessageChannel,
    parseChannelMessages,
    writeChannelMessage
  } = serialization[serializationMode];
 
  let pendingHandle = null;
  initMessageChannel(channel);
  channel.pendingHandle = null;
  channel.onread = function(arrayBuffer) {
    const recvHandle = channel.pendingHandle;
    channel.pendingHandle = null;
    if (arrayBuffer) {
      const nread = streamBaseState[kReadBytesOrError];
      const offset = streamBaseState[kArrayBufferOffset];
      const pool = new Uint8Array(arrayBuffer, offset, nread);
      if (recvHandle)
        pendingHandle = recvHandle;
 
      for (const message of parseChannelMessages(channel, pool)) {
        // There will be at most one NODE_HANDLE message in every chunk we
        // read because SCM_RIGHTS messages don't get coalesced. Make sure
        // that we deliver the handle with the right message however.
        if (isInternal(message)) {
          if (message.cmd === 'NODE_HANDLE') {
            handleMessage(message, pendingHandle, true);
            pendingHandle = null;
          } else {
            handleMessage(message, undefined, true);
          }
        } else {
          handleMessage(message, undefined, false);
        }
      }
    } else {
      this.buffering = false;
      target.disconnect();
      channel.onread = nop;
      channel.close();
      target.channel = null;
      maybeClose(target);
    }
  };
 
  // Object where socket lists will live
  channel.sockets = { got: {}, send: {} };
 
  // Handlers will go through this
  target.on('internalMessage', function(message, handle) {
    // Once acknowledged - continue sending handles.
    if (message.cmd === 'NODE_HANDLE_ACK' ||
        message.cmd === 'NODE_HANDLE_NACK') {
 
      if (target._pendingMessage) {
        if (message.cmd === 'NODE_HANDLE_ACK') {
          closePendingHandle(target);
        } else if (target._pendingMessage.retransmissions++ ===
                   MAX_HANDLE_RETRANSMISSIONS) {
          closePendingHandle(target);
          process.emitWarning('Handle did not reach the receiving process ' +
                              'correctly', 'SentHandleNotReceivedWarning');
        }
      }
 
      assert(ArrayIsArray(target._handleQueue));
      const queue = target._handleQueue;
      target._handleQueue = null;
 
      if (target._pendingMessage) {
        target._send(target._pendingMessage.message,
                     target._pendingMessage.handle,
                     target._pendingMessage.options,
                     target._pendingMessage.callback);
      }
 
      for (let i = 0; i < queue.length; i++) {
        const args = queue[i];
        target._send(args.message, args.handle, args.options, args.callback);
      }
 
      // Process a pending disconnect (if any).
      if (!target.connected && target.channel && !target._handleQueue)
        target._disconnect();
 
      return;
    }
 
    if (message.cmd !== 'NODE_HANDLE') return;
 
    // It is possible that the handle is not received because of some error on
    // ancillary data reception such as MSG_CTRUNC. In this case, report the
    // sender about it by sending a NODE_HANDLE_NACK message.
    if (!handle)
      return target._send({ cmd: 'NODE_HANDLE_NACK' }, null, true);
 
    // Acknowledge handle receival. Don't emit error events (for example if
    // the other side has disconnected) because this call to send() is not
    // initiated by the user and it shouldn't be fatal to be unable to ACK
    // a message.
    target._send({ cmd: 'NODE_HANDLE_ACK' }, null, true);
 
    const obj = handleConversion[message.type];
 
    // Update simultaneous accepts on Windows
    if (process.platform === 'win32') {
      handle.setSimultaneousAccepts(false);
    }
 
    // Convert handle object
    obj.got.call(this, message, handle, (handle) => {
      handleMessage(message.msg, handle, isInternal(message.msg));
    });
  });
 
  target.on('newListener', function() {
 
    process.nextTick(() => {
      if (!target.channel || !target.listenerCount('message'))
        return;
 
      const messages = target.channel[kPendingMessages];
      const { length } = messages;
      if (!length) return;
 
      for (let i = 0; i < length; i++) {
        ReflectApply(target.emit, target, messages[i]);
      }
 
      target.channel[kPendingMessages] = [];
    });
  });
 
  target.send = function(message, handle, options, callback) {
    if (typeof handle === 'function') {
      callback = handle;
      handle = undefined;
      options = undefined;
    } else if (typeof options === 'function') {
      callback = options;
      options = undefined;
    } else if (options !== undefined) {
      validateObject(options, 'options');
    }
 
    options = { swallowErrors: false, ...options };
 
    if (this.connected) {
      return this._send(message, handle, options, callback);
    }
    const ex = new ERR_IPC_CHANNEL_CLOSED();
    if (typeof callback === 'function') {
      process.nextTick(callback, ex);
    } else {
      process.nextTick(() => this.emit('error', ex));
    }
    return false;
  };
 
  target._send = function(message, handle, options, callback) {
    assert(this.connected || this.channel);
 
    if (message === undefined)
      throw new ERR_MISSING_ARGS('message');
 
    // Non-serializable messages should not reach the remote
    // end point; as any failure in the stringification there
    // will result in error message that is weakly consumable.
    // So perform a final check on message prior to sending.
    if (typeof message !== 'string' &&
        typeof message !== 'object' &&
        typeof message !== 'number' &&
        typeof message !== 'boolean') {
      throw new ERR_INVALID_ARG_TYPE(
        'message', ['string', 'object', 'number', 'boolean'], message);
    }
 
    // Support legacy function signature
    if (typeof options === 'boolean') {
      options = { swallowErrors: options };
    }
 
    let obj;
 
    // Package messages with a handle object
    if (handle) {
      // This message will be handled by an internalMessage event handler
      message = {
        cmd: 'NODE_HANDLE',
        type: null,
        msg: message
      };
 
      if (handle instanceof net.Socket) {
        message.type = 'net.Socket';
      } else if (handle instanceof net.Server) {
        message.type = 'net.Server';
      } else if (handle instanceof TCP || handle instanceof Pipe) {
        message.type = 'net.Native';
      } else if (handle instanceof dgram.Socket) {
        message.type = 'dgram.Socket';
      } else if (handle instanceof UDP) {
        message.type = 'dgram.Native';
      } else {
        throw new ERR_INVALID_HANDLE_TYPE();
      }
 
      // Queue-up message and handle if we haven't received ACK yet.
      if (this._handleQueue) {
        ArrayPrototypePush(this._handleQueue, {
          callback: callback,
          handle: handle,
          options: options,
          message: message.msg,
        });
        return this._handleQueue.length === 1;
      }
 
      obj = handleConversion[message.type];
 
      // convert TCP object to native handle object
      handle = ReflectApply(handleConversion[message.type].send,
                            target, [message, handle, options]);
 
      // If handle was sent twice, or it is impossible to get native handle
      // out of it - just send a text without the handle.
      if (!handle)
        message = message.msg;
 
      // Update simultaneous accepts on Windows
      if (obj.simultaneousAccepts && process.platform === 'win32') {
        handle.setSimultaneousAccepts(true);
      }
    } else if (this._handleQueue &&
               !(message && (message.cmd === 'NODE_HANDLE_ACK' ||
                             message.cmd === 'NODE_HANDLE_NACK'))) {
      // Queue request anyway to avoid out-of-order messages.
      ArrayPrototypePush(this._handleQueue, {
        callback: callback,
        handle: null,
        options: options,
        message: message,
      });
      return this._handleQueue.length === 1;
    }
 
    const req = new WriteWrap();
 
    const err = writeChannelMessage(channel, req, message, handle);
    const wasAsyncWrite = streamBaseState[kLastWriteWasAsync];
 
    if (err === 0) {
      if (handle) {
        if (!this._handleQueue)
          this._handleQueue = [];
        if (obj && obj.postSend)
          obj.postSend(message, handle, options, callback, target);
      }
 
      if (wasAsyncWrite) {
        req.oncomplete = () => {
          control.unrefCounted();
          if (typeof callback === 'function')
            callback(null);
        };
        control.refCounted();
      } else if (typeof callback === 'function') {
        process.nextTick(callback, null);
      }
    } else {
      // Cleanup handle on error
      if (obj && obj.postSend)
        obj.postSend(message, handle, options, callback);
 
      if (!options.swallowErrors) {
        const ex = errnoException(err, 'write');
        if (typeof callback === 'function') {
          process.nextTick(callback, ex);
        } else {
          process.nextTick(() => this.emit('error', ex));
        }
      }
    }
 
    /* If the primary is > 2 read() calls behind, please stop sending. */
    return channel.writeQueueSize < (65536 * 2);
  };
 
  // Connected will be set to false immediately when a disconnect() is
  // requested, even though the channel might still be alive internally to
  // process queued messages. The three states are distinguished as follows:
  // - disconnect() never requested: channel is not null and connected
  //   is true
  // - disconnect() requested, messages in the queue: channel is not null
  //   and connected is false
  // - disconnect() requested, channel actually disconnected: channel is
  //   null and connected is false
  target.connected = true;
 
  target.disconnect = function() {
    if (!this.connected) {
      this.emit('error', new ERR_IPC_DISCONNECTED());
      return;
    }
 
    // Do not allow any new messages to be written.
    this.connected = false;
 
    // If there are no queued messages, disconnect immediately. Otherwise,
    // postpone the disconnect so that it happens internally after the
    // queue is flushed.
    if (!this._handleQueue)
      this._disconnect();
  };
 
  target._disconnect = function() {
    assert(this.channel);
 
    // This marks the fact that the channel is actually disconnected.
    this.channel = null;
    this[kChannelHandle] = null;
 
    if (this._pendingMessage)
      closePendingHandle(this);
 
    let fired = false;
    function finish() {
      if (fired) return;
      fired = true;
 
      channel.close();
      target.emit('disconnect');
    }
 
    // If a message is being read, then wait for it to complete.
    if (channel.buffering) {
      this.once('message', finish);
      this.once('internalMessage', finish);

      return;
    }
 
    process.nextTick(finish);
  };
 
  function emit(event, message, handle) {
    if ('internalMessage' === event || target.listenerCount('message')) {
      target.emit(event, message, handle);
      return;
    }
 
    ArrayPrototypePush(
      target.channel[kPendingMessages],
      [event, message, handle]
    );
  }
 
  function handleMessage(message, handle, internal) {
    if (!target.channel)
      return;
 
    const eventName = (internal ? 'internalMessage' : 'message');
 
    process.nextTick(emit, eventName, message, handle);
  }
 
  channel.readStart();
  return control;
}
 
const INTERNAL_PREFIX = 'NODE_';
function isInternal(message) {
  return (message !== null &&
          typeof message === 'object' &&
          typeof message.cmd === 'string' &&
          message.cmd.length > INTERNAL_PREFIX.length &&
          StringPrototypeSlice(message.cmd, 0, INTERNAL_PREFIX.length) ===
            INTERNAL_PREFIX);
}
 
const nop = FunctionPrototype;
 
function getValidStdio(stdio, sync) {
  let ipc;
  let ipcFd;
 
  // Replace shortcut with an array
  if (typeof stdio === 'string') {
    stdio = stdioStringToArray(stdio);
  } else if (!ArrayIsArray(stdio)) {
    throw new ERR_INVALID_ARG_VALUE('stdio', stdio);
  }
 
  // At least 3 stdio will be created
  // Don't concat() a new Array() because it would be sparse, and
  // stdio.reduce() would skip the sparse elements of stdio.
  // See https://stackoverflow.com/a/5501711/3561
  while (stdio.length < 3) ArrayPrototypePush(stdio, undefined);
 
  // Translate stdio into C++-readable form
  // (i.e. PipeWraps or fds)
  stdio = ArrayPrototypeReduce(stdio, (acc, stdio, i) => {
    function cleanup() {
      for (let i = 0; i < acc.length; i++) {
        if ((acc[i].type === 'pipe' || acc[i].type === 'ipc') && acc[i].handle)
          acc[i].handle.close();
      }
    }
 
    // Defaults
    if (stdio == null) {
      stdio = i < 3 ? 'pipe' : 'ignore';
    }
 
    if (stdio === 'ignore') {
      ArrayPrototypePush(acc, { type: 'ignore' });
    } else if (stdio === 'pipe' || stdio === 'overlapped' ||
               (typeof stdio === 'number' && stdio < 0)) {
      const a = {
        type: stdio === 'overlapped' ? 'overlapped' : 'pipe',
        readable: i === 0,
        writable: i !== 0
      };
 
      if (!sync)
        a.handle = new Pipe(PipeConstants.SOCKET);
 
      ArrayPrototypePush(acc, a);
    } else if (stdio === 'ipc') {
      if (sync || ipc !== undefined) {
        // Cleanup previously created pipes
        cleanup();
        if (!sync)
          throw new ERR_IPC_ONE_PIPE();
        else
          throw new ERR_IPC_SYNC_FORK();
      }
 
      ipc = new Pipe(PipeConstants.IPC);
      ipcFd = i;
 
      ArrayPrototypePush(acc, {
        type: 'pipe',
        handle: ipc,
        ipc: true
      });
    } else if (stdio === 'inherit') {
      ArrayPrototypePush(acc, {
        type: 'inherit',
        fd: i
      });
    } else if (typeof stdio === 'number' || typeof stdio.fd === 'number') {
      ArrayPrototypePush(acc, {
        type: 'fd',
        fd: typeof stdio === 'number' ? stdio : stdio.fd
      });
    } else if (getHandleWrapType(stdio) || getHandleWrapType(stdio.handle) ||
               getHandleWrapType(stdio._handle)) {
      const handle = getHandleWrapType(stdio) ?
        stdio :
        getHandleWrapType(stdio.handle) ? stdio.handle : stdio._handle;
 
      ArrayPrototypePush(acc, {
        type: 'wrap',
        wrapType: getHandleWrapType(handle),
        handle: handle,
        _stdio: stdio
      });
    } else if (isArrayBufferView(stdio) || typeof stdio === 'string') {
      if (!sync) {
        cleanup();
        throw new ERR_INVALID_SYNC_FORK_INPUT(inspect(stdio));
      }
    } else {
      // Cleanup
      cleanup();
      throw new ERR_INVALID_ARG_VALUE('stdio', stdio);
    }
 
    return acc;
  }, []);
 
  return { stdio, ipc, ipcFd };
}
 
 
function getSocketList(type, worker, key) {
  const sockets = worker[kChannelHandle].sockets[type];
  let socketList = sockets[key];
  if (!socketList) {
    const Construct = type === 'send' ? SocketListSend : SocketListReceive;
    socketList = sockets[key] = new Construct(worker, key);
  }
  return socketList;
}
 
 
function maybeClose(subprocess) {
  subprocess._closesGot++;
 
  if (subprocess._closesGot === subprocess._closesNeeded) {
    subprocess.emit('close', subprocess.exitCode, subprocess.signalCode);
  }
}
 
function spawnSync(options) {
  const result = spawn_sync.spawn(options);
 
  if (result.output && options.encoding && options.encoding !== 'buffer') {
    for (let i = 0; i < result.output.length; i++) {
      if (!result.output[i])
        continue;
      result.output[i] = result.output[i].toString(options.encoding);
    }
  }
 
  result.stdout = result.output && result.output[1];
  result.stderr = result.output && result.output[2];
 
  if (result.error) {
    result.error = errnoException(result.error, 'spawnSync ' + options.file);
    result.error.path = options.file;
    result.error.spawnargs = ArrayPrototypeSlice(options.args, 1);
  }
 
  return result;
}
 
module.exports = {
  ChildProcess,
  kChannelHandle,
  setupChannel,
  getValidStdio,
  stdioStringToArray,
  spawnSync
};