11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst { 241cb0ef41Sopenharmony_ci isWindows, 251cb0ef41Sopenharmony_ci mustCall, 261cb0ef41Sopenharmony_ci mustCallAtLeast, 271cb0ef41Sopenharmony_ci} = require('../common'); 281cb0ef41Sopenharmony_ciconst assert = require('assert'); 291cb0ef41Sopenharmony_ciconst os = require('os'); 301cb0ef41Sopenharmony_ciconst spawn = require('child_process').spawn; 311cb0ef41Sopenharmony_ciconst debug = require('util').debuglog('test'); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci// We're trying to reproduce: 341cb0ef41Sopenharmony_ci// $ echo "hello\nnode\nand\nworld" | grep o | sed s/o/a/ 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cilet grep, sed, echo; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciif (isWindows) { 391cb0ef41Sopenharmony_ci grep = spawn('grep', ['--binary', 'o']); 401cb0ef41Sopenharmony_ci sed = spawn('sed', ['--binary', 's/o/O/']); 411cb0ef41Sopenharmony_ci echo = spawn('cmd.exe', 421cb0ef41Sopenharmony_ci ['/c', 'echo', 'hello&&', 'echo', 431cb0ef41Sopenharmony_ci 'node&&', 'echo', 'and&&', 'echo', 'world']); 441cb0ef41Sopenharmony_ci} else { 451cb0ef41Sopenharmony_ci grep = spawn('grep', ['o']); 461cb0ef41Sopenharmony_ci sed = spawn('sed', ['s/o/O/']); 471cb0ef41Sopenharmony_ci echo = spawn('echo', ['hello\nnode\nand\nworld\n']); 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci// If the spawn function leaks file descriptors to subprocesses, grep and sed 511cb0ef41Sopenharmony_ci// hang. 521cb0ef41Sopenharmony_ci// This happens when calling pipe(2) and then forgetting to set the 531cb0ef41Sopenharmony_ci// FD_CLOEXEC flag on the resulting file descriptors. 541cb0ef41Sopenharmony_ci// 551cb0ef41Sopenharmony_ci// This test checks child processes exit, meaning they don't hang like 561cb0ef41Sopenharmony_ci// explained above. 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci// pipe echo | grep 601cb0ef41Sopenharmony_ciecho.stdout.on('data', mustCallAtLeast((data) => { 611cb0ef41Sopenharmony_ci debug(`grep stdin write ${data.length}`); 621cb0ef41Sopenharmony_ci if (!grep.stdin.write(data)) { 631cb0ef41Sopenharmony_ci echo.stdout.pause(); 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci})); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci// TODO(@jasnell): This does not appear to ever be 681cb0ef41Sopenharmony_ci// emitted. It's not clear if it is necessary. 691cb0ef41Sopenharmony_cigrep.stdin.on('drain', (data) => { 701cb0ef41Sopenharmony_ci echo.stdout.resume(); 711cb0ef41Sopenharmony_ci}); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci// Propagate end from echo to grep 741cb0ef41Sopenharmony_ciecho.stdout.on('end', mustCall((code) => { 751cb0ef41Sopenharmony_ci grep.stdin.end(); 761cb0ef41Sopenharmony_ci})); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ciecho.on('exit', mustCall(() => { 791cb0ef41Sopenharmony_ci debug('echo exit'); 801cb0ef41Sopenharmony_ci})); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_cigrep.on('exit', mustCall(() => { 831cb0ef41Sopenharmony_ci debug('grep exit'); 841cb0ef41Sopenharmony_ci})); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_cised.on('exit', mustCall(() => { 871cb0ef41Sopenharmony_ci debug('sed exit'); 881cb0ef41Sopenharmony_ci})); 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci// pipe grep | sed 921cb0ef41Sopenharmony_cigrep.stdout.on('data', mustCallAtLeast((data) => { 931cb0ef41Sopenharmony_ci debug(`grep stdout ${data.length}`); 941cb0ef41Sopenharmony_ci if (!sed.stdin.write(data)) { 951cb0ef41Sopenharmony_ci grep.stdout.pause(); 961cb0ef41Sopenharmony_ci } 971cb0ef41Sopenharmony_ci})); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci// TODO(@jasnell): This does not appear to ever be 1001cb0ef41Sopenharmony_ci// emitted. It's not clear if it is necessary. 1011cb0ef41Sopenharmony_cised.stdin.on('drain', (data) => { 1021cb0ef41Sopenharmony_ci grep.stdout.resume(); 1031cb0ef41Sopenharmony_ci}); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci// Propagate end from grep to sed 1061cb0ef41Sopenharmony_cigrep.stdout.on('end', mustCall((code) => { 1071cb0ef41Sopenharmony_ci debug('grep stdout end'); 1081cb0ef41Sopenharmony_ci sed.stdin.end(); 1091cb0ef41Sopenharmony_ci})); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_cilet result = ''; 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci// print sed's output 1151cb0ef41Sopenharmony_cised.stdout.on('data', mustCallAtLeast((data) => { 1161cb0ef41Sopenharmony_ci result += data.toString('utf8', 0, data.length); 1171cb0ef41Sopenharmony_ci debug(data); 1181cb0ef41Sopenharmony_ci})); 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_cised.stdout.on('end', mustCall((code) => { 1211cb0ef41Sopenharmony_ci assert.strictEqual(result, `hellO${os.EOL}nOde${os.EOL}wOrld${os.EOL}`); 1221cb0ef41Sopenharmony_ci})); 123