.
D 2009-07-25T13:59:49
J assignee andreas_kupries
J cmimetype text/plain
J comment In\sUnix.\sredirection\sof\sstandard\serror\sof\sa\schild\sprocess\sto\sthe\sstdout\schannel\s\ndoesn't\sredirect\sit\sto\sthe\sstdout\sof\sthe\sparent,\sbut\sto\sthe\sstdout\sof\sthe\schild.\s\nThis\smeans\s2>@stdout\sbehaves\slike\s2>@1,\swhich\sis\snot\sconsistent\swith\nthe\sdocumentation\sof\s2>@\sredirection.\n\nEXAMPLE\n\nConsider\sfirst\sa\strivial\schild\swhich\swrites\sboth\sto\sstandard\soutput\sand\sstandard\nerror:\n\n%\scatch\s{exec\stclsh\s<<\s{puts\sstdout\sOUT;\sputs\sstderr\sERR;\sputs\sstdout\sOUT;\sputs\sstderr\sERR;\sexit}}\sres\n1\n%\sset\sres\nOUT\nOUT\nERR\nERR\n\nThis\sis\sthe\susual\s"throw\serror\sif\swriting\sto\sstderr"\sbehaviour\sof\s[exec].\sIn\sorder\s\nto\swork\saround\sit,\sone\smay\sredirect\sthe\schild's\sstandard\serror:\n\n%\scatch\s{exec\stclsh\s<<\s{puts\sstdout\sOUT;\sputs\sstderr\sERR;\sputs\sstdout\sOUT;\sputs\sstderr\sERR;\sexit}\s2>@\sstderr}\sres\nERR\nERR\n0\n%\sset\sres\nOUT\nOUT\n\nSo\sfar,\sall\sis\swell.\sMy\sproblem\sis\showever\sthat\sI\swanted\sto\sredirect\sa\schild's\nstandard\serror\sto\sthe\sparent\sstdout,\sas\sI\sfelt\sthat\swould\sbetter\sreflect\sthe\sactual\nseverity\sof\sthe\smessage\sthat\sit\scould\scarry.\sTo\smy\ssurprise,\sthis\srather\shad\s\nthe\seffect\sof\scombining\sstandard\serror\swith\sstandard\sout:\n\n%\scatch\s{exec\stclsh\s<<\s{puts\sstdout\sOUT;\sputs\sstderr\sERR;\sputs\sstdout\sOUT;\sputs\sstderr\sERR;\sexit}\s2>@\sstdout}\sres\n0\n%\sset\sres\nOUT\nERR\nOUT\nERR\n\nThis\sis\sthe\sdocumented\sbehaviour\sof\s2>@1,\sbut\snot\sof\s2>@stdout,\ssince\s2>@\nasks\sfor\sthe\sname\sof\sa\schannel\sin\sthe\sparent,\sand\sstdout\sthere\sis\squite\sdistinct\s\nfrom\sthe\snever-registered\schannel\sthat\s[exec]\sinternally\sreads\sits\sresult\sfrom.\n\nThe\sbug\sappears\sto\sbe\sin\sunix/tclUnixPipe.c,\smore\sprecisely\sthe\s\nTclpCreateProcess\sfunction,\sand\sin\sparticular\swith\sthe\scode\sblock\n\n/*\n\s*\sSet\sup\sstdio\sfile\shandles\sfor\sthe\schild\sprocess.\n\s*/\n\nif\s(!SetupStdFile(inputFile,\sTCL_STDIN)\n||\s!SetupStdFile(outputFile,\sTCL_STDOUT)\n||\s(!joinThisError\s&&\s!SetupStdFile(errorFile,\sTCL_STDERR))\n||\s(joinThisError\s&&\n((dup2(1,2)\s==\s-1)\s||\s(fcntl(2,\sF_SETFD,\s0)\s!=\s0))))\s{\n\s\s\s\ssprintf(errSpace,\n\s\s\s\s"%dforked\sprocess\scouldn't\sset\sup\sinput/output:\s",\serrno);\n\s\s\s\swrite(fd,\serrSpace,\s(size_t)\sstrlen(errSpace));\n\s\s\s\s_exit(1);\n}\n\nAs\sfar\sas\sI\scan\stell,\swhat\shappens\shere\sin\sthe\scase\sof\san\s[exec\s...\s2>@\sstdout]\s\nis\sthat\serrorFile\spoints\sto\sfile\sdescriptor\s1\swhereas\soutputFile\spoints\sto\ssome\s\nhigher\snumber\sfile\sdescriptor\s(T,\ssay),\sand\sjoinThisError\sis\stherefore\sfalse.\s\nWhat\sgoes\swrong\sis\sthat\sSetupStdFile(outputFile,\sTCL_STDOUT)\sperforms\sa\s\ndup2(T,1)\sbefore\sSetupStdFile(errorFile,\sTCL_STDERR)\sgets\sto\sdo\sdup2(1,2),\s\nand\stherefore\sboth\sfile\sdescriptors\s1\sand\s2\sare\smade\sduplicates\sof\sfile\s\ndescriptor\sT,\srather\sthan\s2\sbeing\smade\sa\sduplicate\sof\sthe\sold\s1\sas\sthe\sfunction\s\ncomments\ssay\sit\sshould.\sIn\spseudocode,\swe\swant\sto\sdo\n\n\s\s(fd[1],fd[2])\s:=\s(fd[T],fd[1]);\n\n(parallel\sassignment)\sbut\scurrently\sserialize\sit\sas\n\n\s\sfd[1]\s:=\sfd[T];\s\sfd[2]\s:=\sfd[1];\n\nwhich\sis\snot\sthe\ssame\sthing.\n\nFrom\sthis\sanalogy\swith\sassignment\s--\sthat\n\s\s(a,b)\s:=\s(b,a);\nneeds\sat\sleast\sthree\ssteps\swhen\sserialized,\se.g.\n\s\sc:=a;\sa:=b;\sb:=c;\n--\sit\sfollows\sthat\sthe\sonly\sway\sto\sget\sthis\sright\sin\sall\scases\sis\sto\spreserve\s\nthe\serrorFile\sin\sa\sthird\sfile\sdescriptor,\sso\sone\sneeds\sto\sadd\sa\sdup()\scall\ssomehow,\s\nbut\sit\sis\snot\sobvious\sto\sme\show\sto\sdo\sthis,\ssince\sit\sappears\sa\sTclFile\sisn't\squite\s\na\sfile\sdescriptor\snumber,\sand\sSetupStdFile\sis\ssometimes\screating\sthe\sfile\s\ndescriptor...\sAnyway,\sa\stest\sthat\sit\sall\sworks\scould\sbe\n\nexec\stclsh\s<<\s{\n\s\s\sexec\stclsh\s<<\s{\n\s\s\s\s\s\sputs\sstdout\sOUT1;\sputs\sstderr\sERR1\n\s\s\s\s\s\sputs\sstdout\sOUT2;\sputs\sstderr\sERR2\n\s\s\s\s\s\sexit\n\s\s\s}\s>@stderr\s2>@stdout\n\s\s\sputs\sstdout\s----\n\s\s\sexit\n}\n\n(or\ssomething\smore\srobust\sto\sthe\ssame\seffect),\swhich\sshould\shave\sa\sreturn\s\ncode\sof\s1\sand\sa\sreturn\svalue\sof\nERR1\nERR2\n-----\nOUT1\nOUT2\nsince\sthe\smiddle\stclsh\sswitches\splaces\sbetween\sstdout\sand\sstderr\sof\sthe\s\ninnermost\stclsh,\sso\swhat\sit\swrites\sto\sits\sstderr\swould\send\sup\sin\sthe\sstdout\s\npart\sof\sthe\sresult\sof\sthe\souter\s[exec].\sIn\sthe\spresent\sbuggy\sstate,\sit\sinstead\s\ncomes\sout\sas\n----\nOUT1\nERR1\nOUT2\nERR2\n(inner\sstdout\sand\sstderr\sboth\sredirected\sto\smiddle\sstderr).\n\n\nA\sslightly\sworrying\sfactor\sis\sthat\supon\srereading\sthe\s2>@1\sTIP,\sI\ssee\sthat\sit\s\ncan\sbe\sinterpreted\sas\sstating\sthat\sthis\sbehaviour\sof\s2>@stdout\sas\s2>@1\n(though\scontrary\sto\sdocumentation)\smight\shave\sbeen\sknown\sand\sused\sas\s\nan\sidiom\sfor\s2>@1\sbefore\sthe\slatter\swas\sadded.\sIf\sthat\sis\sso,\sthen\sthere\smay\s\nbe\sbackwards\scompatibility\sissues,\sthough\scertainly\sno\slarger\sthan\swhat\swe\s\nhave\sseen\sat\sminor\sversion\sincrements\sin\sthe\spast.\n\n
J foundin obsolete:\s8.6b1
J is_private 0
J priority 5\sMedium
J resolution None
J status Open
J submitter lars_h
J subsystem 27.\sChannel\sTypes
J title 2>@stdout\sdoes\s2>@1
J type Bug
K 2827015fffffffffffffffffffffffffffffffff
U lars_h
Z 4842667f0b0b03b2e5b71d2a10298957