head 1.72; access; symbols netbsd-10-0-RELEASE:1.72 netbsd-10-0-RC6:1.72 netbsd-10-0-RC5:1.72 netbsd-10-0-RC4:1.72 netbsd-10-0-RC3:1.72 netbsd-10-0-RC2:1.72 netbsd-10-0-RC1:1.72 netbsd-10:1.72.0.2 netbsd-10-base:1.72 netbsd-9-3-RELEASE:1.66 cjep_sun2x-base1:1.66 cjep_sun2x:1.66.0.8 cjep_sun2x-base:1.66 cjep_staticlib_x-base1:1.66 netbsd-9-2-RELEASE:1.66 cjep_staticlib_x:1.66.0.6 cjep_staticlib_x-base:1.66 netbsd-9-1-RELEASE:1.66 phil-wifi-20200421:1.66 phil-wifi-20200411:1.66 is-mlppp:1.66.0.4 is-mlppp-base:1.66 phil-wifi-20200406:1.66 netbsd-8-2-RELEASE:1.57.2.2 netbsd-9-0-RELEASE:1.66 netbsd-9-0-RC2:1.66 netbsd-9-0-RC1:1.66 phil-wifi-20191119:1.66 netbsd-9:1.66.0.2 netbsd-9-base:1.66 phil-wifi-20190609:1.66 netbsd-8-1-RELEASE:1.57.2.2 netbsd-8-1-RC1:1.57.2.2 pgoyette-compat-merge-20190127:1.59.2.3 pgoyette-compat-20190127:1.62 pgoyette-compat-20190118:1.62 pgoyette-compat-1226:1.62 pgoyette-compat-1126:1.61 pgoyette-compat-1020:1.60 pgoyette-compat-0930:1.60 pgoyette-compat-0906:1.60 netbsd-7-2-RELEASE:1.35 pgoyette-compat-0728:1.59 netbsd-8-0-RELEASE:1.57.2.1 phil-wifi:1.59.0.4 phil-wifi-base:1.59 pgoyette-compat-0625:1.59 netbsd-8-0-RC2:1.57.2.1 pgoyette-compat-0521:1.59 pgoyette-compat-0502:1.59 pgoyette-compat-0422:1.59 netbsd-8-0-RC1:1.57.2.1 pgoyette-compat-0415:1.59 pgoyette-compat-0407:1.59 pgoyette-compat-0330:1.59 pgoyette-compat-0322:1.59 pgoyette-compat-0315:1.59 netbsd-7-1-2-RELEASE:1.35 pgoyette-compat:1.59.0.2 pgoyette-compat-base:1.59 netbsd-7-1-1-RELEASE:1.35 matt-nb8-mediatek:1.57.2.1.0.2 matt-nb8-mediatek-base:1.57.2.1 perseant-stdc-iso10646:1.58.0.2 perseant-stdc-iso10646-base:1.58 netbsd-8:1.57.0.2 netbsd-8-base:1.57 prg-localcount2-base3:1.56 prg-localcount2-base2:1.54 prg-localcount2-base1:1.54 prg-localcount2:1.53.0.2 prg-localcount2-base:1.53 pgoyette-localcount-20170426:1.53 bouyer-socketcan-base1:1.51 pgoyette-localcount-20170320:1.51 netbsd-7-1:1.35.0.12 netbsd-7-1-RELEASE:1.35 netbsd-7-1-RC2:1.35 netbsd-7-nhusb-base-20170116:1.35 bouyer-socketcan:1.48.0.2 bouyer-socketcan-base:1.48 pgoyette-localcount-20170107:1.47 netbsd-7-1-RC1:1.35 pgoyette-localcount-20161104:1.47 netbsd-7-0-2-RELEASE:1.35 localcount-20160914:1.47 netbsd-7-nhusb:1.35.0.10 netbsd-7-nhusb-base:1.35 pgoyette-localcount-20160806:1.47 pgoyette-localcount-20160726:1.47 pgoyette-localcount:1.47.0.2 pgoyette-localcount-base:1.47 netbsd-7-0-1-RELEASE:1.35 netbsd-7-0:1.35.0.8 netbsd-7-0-RELEASE:1.35 netbsd-7-0-RC3:1.35 netbsd-7-0-RC2:1.35 netbsd-7-0-RC1:1.35 netbsd-5-2-3-RELEASE:1.30 netbsd-5-1-5-RELEASE:1.30 netbsd-6-0-6-RELEASE:1.32 netbsd-6-1-5-RELEASE:1.32 netbsd-7:1.35.0.6 netbsd-7-base:1.35 yamt-pagecache-base9:1.35 yamt-pagecache-tag8:1.32.2.3 netbsd-6-1-4-RELEASE:1.32 netbsd-6-0-5-RELEASE:1.32 tls-earlyentropy:1.35.0.4 tls-earlyentropy-base:1.35 riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.35 riastradh-drm2-base3:1.35 netbsd-6-1-3-RELEASE:1.32 netbsd-6-0-4-RELEASE:1.32 netbsd-5-2-2-RELEASE:1.30 netbsd-5-1-4-RELEASE:1.30 netbsd-6-1-2-RELEASE:1.32 netbsd-6-0-3-RELEASE:1.32 netbsd-5-2-1-RELEASE:1.30 netbsd-5-1-3-RELEASE:1.30 netbsd-6-1-1-RELEASE:1.32 riastradh-drm2-base2:1.35 riastradh-drm2-base1:1.35 riastradh-drm2:1.35.0.2 riastradh-drm2-base:1.35 netbsd-6-1:1.32.0.10 netbsd-6-0-2-RELEASE:1.32 netbsd-6-1-RELEASE:1.32 khorben-n900:1.33.0.8 netbsd-6-1-RC4:1.32 netbsd-6-1-RC3:1.32 agc-symver:1.33.0.6 agc-symver-base:1.33 netbsd-6-1-RC2:1.32 netbsd-6-1-RC1:1.32 yamt-pagecache-base8:1.33 netbsd-5-2:1.30.0.24 netbsd-6-0-1-RELEASE:1.32 yamt-pagecache-base7:1.33 netbsd-5-2-RELEASE:1.30 netbsd-5-2-RC1:1.30 matt-nb6-plus-nbase:1.32 yamt-pagecache-base6:1.33 netbsd-6-0:1.32.0.8 netbsd-6-0-RELEASE:1.32 netbsd-6-0-RC2:1.32 tls-maxphys:1.33.0.2 tls-maxphys-base:1.35 matt-nb6-plus:1.32.0.6 matt-nb6-plus-base:1.32 netbsd-6-0-RC1:1.32 yamt-pagecache-base5:1.33 yamt-pagecache-base4:1.33 netbsd-6:1.32.0.4 netbsd-6-base:1.32 netbsd-5-1-2-RELEASE:1.30 netbsd-5-1-1-RELEASE:1.30 yamt-pagecache-base3:1.32 yamt-pagecache-base2:1.32 yamt-pagecache:1.32.0.2 yamt-pagecache-base:1.32 cherry-xenmp:1.31.0.2 cherry-xenmp-base:1.31 bouyer-quota2-nbase:1.31 bouyer-quota2:1.30.0.22 bouyer-quota2-base:1.30 matt-mips64-premerge-20101231:1.30 matt-nb5-mips64-premerge-20101231:1.30 matt-nb5-pq3:1.30.0.20 matt-nb5-pq3-base:1.30 netbsd-5-1:1.30.0.18 netbsd-5-1-RELEASE:1.30 netbsd-5-1-RC4:1.30 matt-nb5-mips64-k15:1.30 netbsd-5-1-RC3:1.30 netbsd-5-1-RC2:1.30 netbsd-5-1-RC1:1.30 netbsd-5-0-2-RELEASE:1.30 matt-nb5-mips64-premerge-20091211:1.30 matt-premerge-20091211:1.30 matt-nb5-mips64-u2-k2-k4-k7-k8-k9:1.30 matt-nb4-mips64-k7-u2a-k9b:1.30 matt-nb5-mips64-u1-k1-k5:1.30 matt-nb5-mips64:1.30.0.16 netbsd-5-0-1-RELEASE:1.30 jym-xensuspend-nbase:1.30 netbsd-5-0:1.30.0.14 netbsd-5-0-RELEASE:1.30 netbsd-5-0-RC4:1.30 netbsd-5-0-RC3:1.30 netbsd-5-0-RC2:1.30 jym-xensuspend:1.30.0.12 jym-xensuspend-base:1.30 netbsd-5-0-RC1:1.30 netbsd-5:1.30.0.10 netbsd-5-base:1.30 matt-mips64-base2:1.30 matt-mips64:1.29.0.22 mjf-devfs2:1.30.0.8 mjf-devfs2-base:1.30 netbsd-4-0-1-RELEASE:1.29 wrstuden-revivesa-base-3:1.30 wrstuden-revivesa-base-2:1.30 wrstuden-fixsa-newbase:1.29 wrstuden-revivesa-base-1:1.30 yamt-pf42-base4:1.30 yamt-pf42-base3:1.30 hpcarm-cleanup-nbase:1.30 yamt-pf42-baseX:1.30 yamt-pf42-base2:1.30 wrstuden-revivesa:1.30.0.6 wrstuden-revivesa-base:1.30 yamt-pf42:1.30.0.4 yamt-pf42-base:1.30 keiichi-mipv6:1.30.0.2 keiichi-mipv6-base:1.30 matt-armv6-nbase:1.30 matt-armv6-prevmlocking:1.29 wrstuden-fixsa-base-1:1.29 netbsd-4-0:1.29.0.20 netbsd-4-0-RELEASE:1.29 cube-autoconf:1.29.0.18 cube-autoconf-base:1.29 netbsd-4-0-RC5:1.29 netbsd-4-0-RC4:1.29 netbsd-4-0-RC3:1.29 netbsd-4-0-RC2:1.29 netbsd-4-0-RC1:1.29 matt-armv6:1.29.0.16 matt-armv6-base:1.29 matt-mips64-base:1.29 hpcarm-cleanup:1.29.0.14 hpcarm-cleanup-base:1.30 netbsd-3-1-1-RELEASE:1.29 netbsd-3-0-3-RELEASE:1.29 wrstuden-fixsa:1.29.0.12 wrstuden-fixsa-base:1.29 abandoned-netbsd-4-base:1.29 abandoned-netbsd-4:1.29.0.6 netbsd-3-1:1.29.0.8 netbsd-3-1-RELEASE:1.29 netbsd-3-0-2-RELEASE:1.29 netbsd-3-1-RC4:1.29 netbsd-3-1-RC3:1.29 netbsd-3-1-RC2:1.29 netbsd-3-1-RC1:1.29 netbsd-4:1.29.0.10 netbsd-4-base:1.29 netbsd-3-0-1-RELEASE:1.29 netbsd-3-0:1.29.0.4 netbsd-3-0-RELEASE:1.29 netbsd-3-0-RC6:1.29 netbsd-3-0-RC5:1.29 netbsd-3-0-RC4:1.29 netbsd-3-0-RC3:1.29 netbsd-3-0-RC2:1.29 netbsd-3-0-RC1:1.29 netbsd-2-0-3-RELEASE:1.28.2.1 netbsd-2-1:1.28.2.1.0.4 netbsd-2-1-RELEASE:1.28.2.1 netbsd-2-1-RC6:1.28.2.1 netbsd-2-1-RC5:1.28.2.1 netbsd-2-1-RC4:1.28.2.1 netbsd-2-1-RC3:1.28.2.1 netbsd-2-1-RC2:1.28.2.1 netbsd-2-1-RC1:1.28.2.1 netbsd-2-0-2-RELEASE:1.28.2.1 netbsd-3:1.29.0.2 netbsd-3-base:1.29 netbsd-2-0-1-RELEASE:1.28.2.1 netbsd-2:1.28.2.1.0.2 netbsd-2-base:1.28.2.1 netbsd-2-0-RELEASE:1.28.2.1 netbsd-2-0-RC5:1.28.2.1 netbsd-2-0-RC4:1.28.2.1 netbsd-2-0-RC3:1.28.2.1 netbsd-2-0-RC2:1.28.2.1 netbsd-2-0-RC1:1.28.2.1 netbsd-2-0:1.28.0.2 netbsd-2-0-base:1.28 netbsd-1-6-PATCH002-RELEASE:1.23 netbsd-1-6-PATCH002:1.23 netbsd-1-6-PATCH002-RC4:1.23 netbsd-1-6-PATCH002-RC3:1.23 netbsd-1-6-PATCH002-RC2:1.23 netbsd-1-6-PATCH002-RC1:1.23 netbsd-1-6-PATCH001:1.23 netbsd-1-6-PATCH001-RELEASE:1.23 netbsd-1-6-PATCH001-RC3:1.23 netbsd-1-6-PATCH001-RC2:1.23 netbsd-1-6-PATCH001-RC1:1.23 fvdl_fs64_base:1.27 netbsd-1-6-RELEASE:1.23 netbsd-1-6-RC3:1.23 netbsd-1-6-RC2:1.23 netbsd-1-6-RC1:1.23 netbsd-1-6:1.23.0.2 netbsd-1-6-base:1.23 netbsd-1-5-PATCH003:1.22 ELRICshvfork:1.22.0.6 ELRICshvfork-base:1.22 netbsd-1-5-PATCH002:1.22 netbsd-1-5-PATCH001:1.22 netbsd-1-5-RELEASE:1.22 netbsd-1-5-BETA2:1.22 netbsd-1-5-BETA:1.22 netbsd-1-4-PATCH003:1.20 netbsd-1-5-ALPHA2:1.22 netbsd-1-5:1.22.0.4 netbsd-1-5-base:1.22 minoura-xpg4dl:1.22.0.2 minoura-xpg4dl-base:1.22 netbsd-1-4-PATCH002:1.20 wrstuden-devbsize-19991221:1.20 wrstuden-devbsize:1.20.0.6 wrstuden-devbsize-base:1.20 comdex-fall-1999:1.20.0.4 comdex-fall-1999-base:1.20 netbsd-1-4-PATCH001:1.20 netbsd-1-4-RELEASE:1.20 netbsd-1-4:1.20.0.2 netbsd-1-4-base:1.20 netbsd-1-3-PATCH003:1.16 netbsd-1-3-PATCH003-CANDIDATE2:1.16 netbsd-1-3-PATCH003-CANDIDATE1:1.16 netbsd-1-3-PATCH003-CANDIDATE0:1.16 netbsd-1-3-PATCH002:1.16 netbsd-1-3-PATCH001:1.16 netbsd-1-3-RELEASE:1.16 netbsd-1-3-BETA:1.16 netbsd-1-3:1.16.0.2 netbsd-1-3-base:1.16 netbsd-1-2-PATCH001:1.12.6.1 netbsd-1-2-RELEASE:1.12 netbsd-1-2-BETA:1.12 netbsd-1-2-base:1.12 netbsd-1-2:1.12.0.6 netbsd-1-1-PATCH001:1.12 netbsd-1-1-RELEASE:1.12 netbsd-1-1:1.12.0.2 netbsd-1-1-base:1.12 netbsd-1-0-PATCH06:1.8 netbsd-1-0-PATCH05:1.8 netbsd-1-0-PATCH04:1.8 netbsd-1-0-PATCH03:1.8 netbsd-1-0-PATCH02:1.8 netbsd-1-0-PATCH1:1.8 netbsd-1-0-PATCH0:1.8 netbsd-1-0-RELEASE:1.8 netbsd-1-0:1.8.0.2 netbsd-1-0-base:1.8 lite-1:1.1.1.2 CSRG:1.1.1 netbsd-0-9-RELEASE:1.4 netbsd-0-9-BETA:1.4 netbsd-0-9-ALPHA2:1.4 netbsd-0-9-ALPHA:1.4 netbsd-0-9:1.4.0.2 netbsd-0-9-base:1.4 netbsd-0-8:1.3 netbsd-alpha-1:1.3 patchkit-0-2-2:1.1.1.1 WFJ-386bsd-01:1.1.1.1 WFJ-920714:1.1.1; locks; strict; comment @ * @; 1.72 date 2021.11.22.05.17.43; author kre; state Exp; branches; next 1.71; commitid dPk2IHgMZP4avKhD; 1.71 date 2021.11.16.11.27.50; author kre; state Exp; branches; next 1.70; commitid r0Si718YHEyuM0hD; 1.70 date 2021.11.10.15.26.34; author kre; state Exp; branches; next 1.69; commitid qTPHrRbStXDSgggD; 1.69 date 2021.09.15.20.21.47; author kre; state Exp; branches; next 1.68; commitid jMX6KleWCtgeJ59D; 1.68 date 2021.09.15.18.29.45; author kre; state Exp; branches; next 1.67; commitid SuhSRbaBbujN659D; 1.67 date 2021.09.14.14.49.39; author kre; state Exp; branches; next 1.66; commitid 6e3ZNYgup5ghVV8D; 1.66 date 2019.03.01.06.15.01; author kre; state Exp; branches; next 1.65; commitid K7ArHC9LZFw8UCdB; 1.65 date 2019.03.01.05.23.35; author kre; state Exp; branches; next 1.64; commitid GzlhszmTlfXuCCdB; 1.64 date 2019.02.09.09.34.43; author kre; state Exp; branches; next 1.63; commitid cLLsZxMpQKNfE4bB; 1.63 date 2019.02.09.03.35.55; author kre; state Exp; branches; next 1.62; commitid beAXZdtNE2VCE2bB; 1.62 date 2018.11.26.20.03.39; author kamil; state Exp; branches; next 1.61; commitid 6vfmLTSw9y4Dxu1B; 1.61 date 2018.11.23.23.41.20; author kre; state Exp; branches; next 1.60; commitid wCud37GN1XPbP71B; 1.60 date 2018.08.13.22.13.02; author kre; state Exp; branches; next 1.59; commitid sUKinChD2toVC0OA; 1.59 date 2017.11.15.09.21.48; author kre; state Exp; branches 1.59.2.1 1.59.4.1; next 1.58; commitid L5WfRX8DPNoS47fA; 1.58 date 2017.06.30.23.01.21; author kre; state Exp; branches; next 1.57; commitid 5ix3sf7rUwYV2sXz; 1.57 date 2017.05.29.22.21.00; author kre; state Exp; branches 1.57.2.1; next 1.56; commitid uXLTi8gXA4v2QkTz; 1.56 date 2017.05.18.13.56.58; author kre; state Exp; branches; next 1.55; commitid p1OJ0bijg6DvpSRz; 1.55 date 2017.05.14.17.27.05; author kre; state Exp; branches; next 1.54; commitid HsViAHnG7qEEHnRz; 1.54 date 2017.04.29.15.14.28; author kre; state Exp; branches; next 1.53; commitid nnyWhTq1Q7knsrPz; 1.53 date 2017.04.22.16.02.39; author kre; state Exp; branches 1.53.2.1; next 1.52; commitid g3mgAp866NFPUxOz; 1.52 date 2017.04.22.15.54.53; author kre; state Exp; branches; next 1.51; commitid 1t3O4w6idpKTTxOz; 1.51 date 2017.02.03.23.16.38; author kre; state Exp; branches; next 1.50; commitid BNrMh18n0fElMyEz; 1.50 date 2017.02.02.20.00.40; author christos; state Exp; branches; next 1.49; commitid FV7Vp8x26V2MNpEz; 1.49 date 2017.01.21.23.03.36; author christos; state Exp; branches; next 1.48; commitid Dm1oXROh6o35cTCz; 1.48 date 2017.01.10.20.43.08; author christos; state Exp; branches 1.48.2.1; next 1.47; commitid z4i4JtFZLYioMsBz; 1.47 date 2016.05.12.13.31.37; author kre; state Exp; branches 1.47.2.1; next 1.46; commitid iu37RZX8UnWu7c6z; 1.46 date 2016.05.09.20.50.08; author kre; state Exp; branches; next 1.45; commitid rktXh1rmpPHUHQ5z; 1.45 date 2016.05.08.20.14.27; author kre; state Exp; branches; next 1.44; commitid 73PIcgQOdzWpyI5z; 1.44 date 2016.05.08.03.51.15; author kre; state Exp; branches; next 1.43; commitid yQQyUFvwEOls7D5z; 1.43 date 2016.05.02.01.46.31; author christos; state Exp; branches; next 1.42; commitid LoF0CYUx8JLoCQ4z; 1.42 date 2016.03.13.01.22.42; author christos; state Exp; branches; next 1.41; commitid hDgytuSsrC096qYy; 1.41 date 2016.03.13.00.52.05; author christos; state Exp; branches; next 1.40; commitid wpwkNzPPUCweVpYy; 1.40 date 2016.03.12.21.35.13; author christos; state Exp; branches; next 1.39; commitid 6UQydKTU47THOoYy; 1.39 date 2016.01.04.13.57.15; author christos; state Exp; branches; next 1.38; commitid e7lpm6YFP7usuCPy; 1.38 date 2016.01.04.03.00.24; author christos; state Exp; branches; next 1.37; commitid CBd29bq5y6VfOyPy; 1.37 date 2014.10.23.21.03.25; author christos; state Exp; branches; next 1.36; commitid 0XbcHY7Bl6YyVmVx; 1.36 date 2014.10.15.14.54.25; author christos; state Exp; branches; next 1.35; commitid blxR7FARUviH7jUx; 1.35 date 2013.06.27.23.22.04; author yamt; state Exp; branches; next 1.34; commitid p2d3GDv22fOUcjVw; 1.34 date 2013.06.12.01.36.52; author yamt; state Exp; branches; next 1.33; commitid q67wPkbxjAETtgTw; 1.33 date 2012.03.20.18.42.29; author matt; state Exp; branches 1.33.2.1; next 1.32; 1.32 date 2011.08.31.16.24.55; author plunky; state Exp; branches 1.32.2.1; next 1.31; 1.31 date 2011.02.17.15.13.49; author pooka; state Exp; branches; next 1.30; 1.30 date 2008.01.21.06.43.03; author msaitoh; state Exp; branches 1.30.22.1; next 1.29; 1.29 date 2004.07.08.03.57.33; author christos; state Exp; branches 1.29.2.1 1.29.10.1 1.29.16.1; next 1.28; 1.28 date 2003.08.07.09.05.37; author agc; state Exp; branches 1.28.2.1; next 1.27; 1.27 date 2002.11.24.22.35.42; author christos; state Exp; branches; next 1.26; 1.26 date 2002.09.28.01.25.02; author christos; state Exp; branches; next 1.25; 1.25 date 2002.09.27.22.56.24; author christos; state Exp; branches; next 1.24; 1.24 date 2002.09.27.18.56.55; author christos; state Exp; branches; next 1.23; 1.23 date 2002.05.15.16.33.35; author christos; state Exp; branches; next 1.22; 1.22 date 2000.05.22.10.18.47; author elric; state Exp; branches 1.22.6.1; next 1.21; 1.21 date 2000.05.13.20.50.15; author elric; state Exp; branches; next 1.20; 1.20 date 99.02.04.16.17.39; author christos; state Exp; branches; next 1.19; 1.19 date 98.07.28.11.41.58; author mycroft; state Exp; branches; next 1.18; 1.18 date 98.07.28.05.31.28; author mycroft; state Exp; branches; next 1.17; 1.17 date 98.07.27.17.12.45; author christos; state Exp; branches; next 1.16; 1.16 date 97.07.04.21.02.21; author christos; state Exp; branches; next 1.15; 1.15 date 97.04.21.12.38.25; author christos; state Exp; branches; next 1.14; 1.14 date 97.01.11.02.04.46; author tls; state Exp; branches; next 1.13; 1.13 date 96.10.16.15.16.04; author christos; state Exp; branches; next 1.12; 1.12 date 95.05.11.21.30.10; author christos; state Exp; branches 1.12.6.1; next 1.11; 1.11 date 95.03.21.09.10.04; author cgd; state Exp; branches; next 1.10; 1.10 date 94.12.23.13.24.13; author cgd; state Exp; branches; next 1.9; 1.9 date 94.12.05.19.07.52; author cgd; state Exp; branches; next 1.8; 1.8 date 94.06.11.16.12.30; author mycroft; state Exp; branches; next 1.7; 1.7 date 94.05.11.17.10.39; author jtc; state Exp; branches; next 1.6; 1.6 date 94.04.25.18.47.17; author cgd; state Exp; branches; next 1.5; 1.5 date 93.08.01.18.58.01; author mycroft; state Exp; branches; next 1.4; 1.4 date 93.05.02.01.28.44; author sef; state Exp; branches; next 1.3; 1.3 date 93.03.23.00.29.14; author cgd; state Exp; branches; next 1.2; 1.2 date 93.03.22.08.04.00; author cgd; state Exp; branches; next 1.1; 1.1 date 93.03.21.09.45.37; author cgd; state Exp; branches 1.1.1.1; next ; 1.59.2.1 date 2018.09.06.06.51.32; author pgoyette; state Exp; branches; next 1.59.2.2; commitid HCi1bXD317XIK0RA; 1.59.2.2 date 2018.11.26.01.49.54; author pgoyette; state Exp; branches; next 1.59.2.3; commitid Zj4q5SspGdKXto1B; 1.59.2.3 date 2018.12.26.14.01.03; author pgoyette; state Exp; branches; next ; commitid xUhK8IAeBM1azj5B; 1.59.4.1 date 2019.06.10.21.41.04; author christos; state Exp; branches; next 1.59.4.2; commitid jtc8rnCzWiEEHGqB; 1.59.4.2 date 2020.04.21.18.41.06; author martin; state dead; branches; next 1.59.4.3; commitid 86tA4aEmdr3VCh5C; 1.59.4.3 date 2020.04.21.19.37.34; author martin; state Exp; branches; next ; commitid x6IB64bYH9UmWh5C; 1.57.2.1 date 2017.07.23.14.58.14; author snj; state Exp; branches; next 1.57.2.2; commitid IcSSefq8ASq6Dm0A; 1.57.2.2 date 2018.12.07.13.12.02; author martin; state Exp; branches; next ; commitid ntDPl0vJxaOBUR2B; 1.53.2.1 date 2017.05.02.03.19.14; author pgoyette; state Exp; branches; next 1.53.2.2; commitid oFKELrgrBgUNoLPz; 1.53.2.2 date 2017.05.19.00.22.51; author pgoyette; state Exp; branches; next ; commitid QNTxgGjVagwoSVRz; 1.48.2.1 date 2017.04.21.16.50.42; author bouyer; state Exp; branches; next ; commitid dUG7nkTKALCadqOz; 1.47.2.1 date 2017.03.20.06.51.32; author pgoyette; state Exp; branches; next 1.47.2.2; commitid jjw7cAwgyKq7RfKz; 1.47.2.2 date 2017.04.26.02.52.13; author pgoyette; state Exp; branches; next ; commitid ojV02aOSdzvBqZOz; 1.33.2.1 date 2013.06.23.06.26.13; author tls; state Exp; branches; next 1.33.2.2; commitid OnlO1cBgtQRcIHUw; 1.33.2.2 date 2014.08.19.23.45.11; author tls; state Exp; branches; next ; commitid jTnpym9Qu0o4R1Nx; 1.32.2.1 date 2012.04.17.00.01.38; author yamt; state Exp; branches; next 1.32.2.2; 1.32.2.2 date 2013.06.12.01.08.16; author yamt; state Exp; branches; next 1.32.2.3; commitid Wm4HAlDZBmw5kgTw; 1.32.2.3 date 2013.06.12.01.36.09; author yamt; state Exp; branches; next 1.32.2.4; commitid FyzZINAgW28EtgTw; 1.32.2.4 date 2014.05.22.11.26.23; author yamt; state Exp; branches; next ; commitid OarWMuV9WFtzGwBx; 1.30.22.1 date 2011.03.05.15.08.31; author bouyer; state Exp; branches; next ; 1.29.2.1 date 2009.02.26.08.25.35; author snj; state Exp; branches; next ; 1.29.10.1 date 2009.02.26.08.31.55; author snj; state Exp; branches; next ; 1.29.16.1 date 2008.03.23.00.11.41; author matt; state Exp; branches; next ; 1.28.2.1 date 2004.08.22.14.14.37; author tron; state Exp; branches; next ; 1.22.6.1 date 2002.03.27.20.37.41; author elric; state Exp; branches; next ; 1.12.6.1 date 97.01.26.04.57.37; author rat; state Exp; branches; next ; 1.1.1.1 date 93.03.21.09.45.37; author cgd; state Exp; branches; next 1.1.1.2; 1.1.1.2 date 94.05.11.17.01.10; author jtc; state Exp; branches; next ; desc @@ 1.72 log @ PR bin/53550 Here we go again... One more time to redo how here docs are processed (it has been a few years since the last time!) This is actually a relatively minor change, mostly to timimg (to just when things happen). Now here docs are expanded at the same time the "filename" word in a redirect is expanded, rather than later when the heredoc was being sent to its process. This actually makes things more consistent - but does break one of the ATF tests which was testing that we were (effectively) internally inconsistent in this area. Not all shells agree on the context in which redirection expansions should happen, some make any side effects visible to the parent shell (the majority do) others do the redirection expansions in a subshell so any side effcts are lost. We used to have a foot in each camp, with the majority for everything but here docs, and the minority for here docs. Now we're all the way with LBJ ... (or something like that). @ text @/* $NetBSD: redir.c,v 1.71 2021/11/16 11:27:50 kre Exp $ */ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #ifndef lint #if 0 static char sccsid[] = "@@(#)redir.c 8.2 (Berkeley) 5/4/95"; #else __RCSID("$NetBSD: redir.c,v 1.71 2021/11/16 11:27:50 kre Exp $"); #endif #endif /* not lint */ #include #include /* PIPE_BUF */ #include #include #include #include #include #include #include /* * Code for dealing with input/output redirection. */ #include "main.h" #include "builtins.h" #include "shell.h" #include "nodes.h" #include "jobs.h" #include "options.h" #include "expand.h" #include "redir.h" #include "output.h" #include "memalloc.h" #include "mystring.h" #include "error.h" #include "show.h" #define EMPTY -2 /* marks an unused slot in redirtab */ #define CLOSED -1 /* fd was not open before redir */ #ifndef PIPE_BUF # define PIPESIZE 4096 /* amount of buffering in a pipe */ #else # define PIPESIZE PIPE_BUF #endif #ifndef FD_CLOEXEC # define FD_CLOEXEC 1 /* well known from before there was a name */ #endif #ifndef F_DUPFD_CLOEXEC #define F_DUPFD_CLOEXEC F_DUPFD #define CLOEXEC(fd) (fcntl((fd), F_SETFD, fcntl((fd),F_GETFD) | FD_CLOEXEC)) #else #define CLOEXEC(fd) #endif MKINIT struct renamelist { struct renamelist *next; int orig; int into; }; MKINIT struct redirtab { struct redirtab *next; struct renamelist *renamed; }; MKINIT struct redirtab *redirlist; /* * We keep track of whether or not fd0 has been redirected. This is for * background commands, where we want to redirect fd0 to /dev/null only * if it hasn't already been redirected. */ STATIC int fd0_redirected = 0; /* * And also where to put internal use fds that should be out of the * way of user defined fds (normally) */ STATIC int big_sh_fd = 0; STATIC const struct renamelist *is_renamed(const struct renamelist *, int); STATIC void fd_rename(struct redirtab *, int, int); STATIC int * saved_redirected_fd(int); STATIC void free_rl(struct redirtab *, int); STATIC void openredirect(union node *, char[10], int); STATIC int openhere(const union node *); STATIC int copyfd(int, int, int); STATIC void find_big_fd(void); struct shell_fds { /* keep track of internal shell fds */ struct shell_fds *nxt; void (*cb)(int, int); int fd; }; STATIC struct shell_fds *sh_fd_list; STATIC int pick_new_fd(int); STATIC void renumber_sh_fd(struct shell_fds *); STATIC struct shell_fds *sh_fd(int); STATIC const struct renamelist * is_renamed(const struct renamelist *rl, int fd) { while (rl != NULL) { if (rl->orig == fd) return rl; rl = rl->next; } return NULL; } STATIC int * saved_redirected_fd(int fd) { struct redirtab *rt; struct renamelist *rl; for (rt = redirlist; rt != NULL; rt = rt->next) { for (rl = rt->renamed; rl != NULL; rl = rl->next) { if (rl->into == fd) return &rl->into; } } return NULL; } STATIC void free_rl(struct redirtab *rt, int reset) { struct renamelist *rl, *rn = rt->renamed; while ((rl = rn) != NULL) { rn = rl->next; if (rl->orig == 0) fd0_redirected--; VTRACE(DBG_REDIR, ("popredir %d%s: %s", rl->orig, rl->orig==0 ? " (STDIN)" : "", reset ? "" : "no reset\n")); if (reset) { if (rl->into < 0) { VTRACE(DBG_REDIR, ("closed\n")); close(rl->orig); } else { VTRACE(DBG_REDIR, ("from %d\n", rl->into)); movefd(rl->into, rl->orig); } } ckfree(rl); } rt->renamed = NULL; } STATIC void fd_rename(struct redirtab *rt, int from, int to) { /* XXX someday keep a short list (8..10) of freed renamelists XXX */ struct renamelist *rl = ckmalloc(sizeof(struct renamelist)); rl->next = rt->renamed; rt->renamed = rl; rl->orig = from; rl->into = to; } /* * Process a list of redirection commands. If the REDIR_PUSH flag is set, * old file descriptors are stashed away so that the redirection can be * undone by calling popredir. If the REDIR_BACKQ flag is set, then the * standard output, and the standard error if it becomes a duplicate of * stdout, is saved in memory. */ void redirect(union node *redir, int flags) { union node *n; struct redirtab *sv = NULL; int i; int fd; char memory[10]; /* file descriptors to write to memory */ CTRACE(DBG_REDIR, ("redirect(F=0x%x):%s\n", flags, redir?"":" NONE")); for (i = 10 ; --i >= 0 ; ) memory[i] = 0; memory[1] = flags & REDIR_BACKQ; if (flags & REDIR_PUSH) { /* * We don't have to worry about REDIR_VFORK here, as * flags & REDIR_PUSH is never true if REDIR_VFORK is set. */ sv = ckmalloc(sizeof (struct redirtab)); sv->renamed = NULL; sv->next = redirlist; redirlist = sv; } for (n = redir ; n ; n = n->nfile.next) { int *renamed; fd = n->nfile.fd; VTRACE(DBG_REDIR, ("redir %d (max=%d limit=%ld) ", fd, max_user_fd, user_fd_limit)); if (fd < user_fd_limit && fd > max_user_fd) max_user_fd = fd; if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && n->ndup.dupfd == fd) { VTRACE(DBG_REDIR, ("!cloexec\n")); if (sh_fd(fd) != NULL || saved_redirected_fd(fd) != NULL) error("fd %d: %s", fd, strerror(EBADF)); /* redirect from/to same file descriptor */ /* make sure it stays open */ if (fcntl(fd, F_SETFD, 0) < 0) error("fd %d: %s", fd, strerror(errno)); continue; } if ((renamed = saved_redirected_fd(fd)) != NULL) { int to = pick_new_fd(fd); VTRACE(DBG_REDIR, ("redirect: moved holding fd %d to %d\n", fd, to)); *renamed = to; if (to != fd) /* always... */ (void)close(fd); } renumber_sh_fd(sh_fd(fd)); if ((flags & REDIR_PUSH) && !is_renamed(sv->renamed, fd)) { int bigfd; INTOFF; if (big_sh_fd < 10) find_big_fd(); if ((bigfd = big_sh_fd) < max_user_fd) bigfd = max_user_fd; if ((i = fcntl(fd, F_DUPFD, bigfd + 1)) == -1) { switch (errno) { case EBADF: i = CLOSED; break; case EMFILE: case EINVAL: find_big_fd(); i = fcntl(fd, F_DUPFD, big_sh_fd); if (i >= 0) break; if (errno == EMFILE || errno == EINVAL) i = fcntl(fd, F_DUPFD, 3); if (i >= 0) break; /* FALLTHRU */ default: error("%d: %s", fd, strerror(errno)); /* NOTREACHED */ } } if (i >= 0) (void)fcntl(i, F_SETFD, FD_CLOEXEC); fd_rename(sv, fd, i); VTRACE(DBG_REDIR, ("fd %d saved as %d ", fd, i)); INTON; } VTRACE(DBG_REDIR, ("%s\n", fd == 0 ? "STDIN" : "")); if (fd == 0) fd0_redirected++; openredirect(n, memory, flags); } if (memory[1]) out1 = &memout; if (memory[2]) out2 = &memout; } STATIC void openredirect(union node *redir, char memory[10], int flags) { struct stat sb; int fd = redir->nfile.fd; char *fname; int f; int eflags, cloexec; /* * We suppress interrupts so that we won't leave open file * descriptors around. This may not be such a good idea because * an open of a device or a fifo can block indefinitely. */ INTOFF; if (fd < 10) memory[fd] = 0; switch (redir->nfile.type) { case NFROM: fname = redir->nfile.expfname; if (flags & REDIR_VFORK) eflags = O_NONBLOCK; else eflags = 0; if ((f = open(fname, O_RDONLY|eflags)) < 0) goto eopen; VTRACE(DBG_REDIR, ("openredirect(< '%s') -> %d [%#x]", fname, f, eflags)); if (eflags) (void)fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags); break; case NFROMTO: fname = redir->nfile.expfname; if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) goto ecreate; VTRACE(DBG_REDIR, ("openredirect(<> '%s') -> %d", fname, f)); break; case NTO: if (Cflag) { fname = redir->nfile.expfname; if ((f = open(fname, O_WRONLY)) == -1) { if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) goto ecreate; } else if (fstat(f, &sb) == -1) { int serrno = errno; close(f); errno = serrno; goto ecreate; } else if (S_ISREG(sb.st_mode)) { close(f); errno = EEXIST; goto ecreate; } VTRACE(DBG_REDIR, ("openredirect(>| '%s') -> %d", fname, f)); break; } /* FALLTHROUGH */ case NCLOBBER: fname = redir->nfile.expfname; if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) goto ecreate; VTRACE(DBG_REDIR, ("openredirect(> '%s') -> %d", fname, f)); break; case NAPPEND: fname = redir->nfile.expfname; if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) goto ecreate; VTRACE(DBG_REDIR, ("openredirect(>> '%s') -> %d", fname, f)); break; case NTOFD: case NFROMFD: if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ if (sh_fd(redir->ndup.dupfd) != NULL || saved_redirected_fd(redir->ndup.dupfd) != NULL) error("Redirect (from %d to %d) failed: %s", redir->ndup.dupfd, fd, strerror(EBADF)); if (fd < 10 && redir->ndup.dupfd < 10 && memory[redir->ndup.dupfd]) memory[fd] = 1; else if (copyfd(redir->ndup.dupfd, fd, (flags & REDIR_KEEP) == 0) < 0) error("Redirect (from %d to %d) failed: %s", redir->ndup.dupfd, fd, strerror(errno)); VTRACE(DBG_REDIR, ("openredirect: %d%c&%d\n", fd, "<>"[redir->nfile.type==NTOFD], redir->ndup.dupfd)); } else { (void) close(fd); VTRACE(DBG_REDIR, ("openredirect: %d%c&-\n", fd, "<>"[redir->nfile.type==NTOFD])); } INTON; return; case NHERE: case NXHERE: VTRACE(DBG_REDIR, ("openredirect: %d<<...", fd)); f = openhere(redir); break; default: abort(); } cloexec = fd > 2 && (flags & REDIR_KEEP) == 0 && !posix; if (f != fd) { VTRACE(DBG_REDIR, (" -> %d", fd)); if (copyfd(f, fd, cloexec) < 0) { int e = errno; VTRACE(DBG_REDIR, (" failed: %s\n", strerror(e))); close(f); error("redirect reassignment (fd %d) failed: %s", fd, strerror(e)); } close(f); } else if (cloexec) (void)fcntl(f, F_SETFD, FD_CLOEXEC); VTRACE(DBG_REDIR, ("%s\n", cloexec ? " cloexec" : "")); INTON; return; ecreate: exerrno = 1; error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); eopen: exerrno = 1; error("cannot open %s: %s", fname, errmsg(errno, E_OPEN)); } /* * Handle here documents. Normally we fork off a process to write the * data to a pipe. If the document is short, we can stuff the data in * the pipe without forking. */ STATIC int openhere(const union node *redir) { int pip[2]; int len = 0; if (pipe(pip) < 0) error("Pipe call failed"); len = strlen(redir->nhere.text); VTRACE(DBG_REDIR, ("openhere(%p) [%d] \"%.*s\"%s\n", redir, len, (len < 40 ? len : 40), redir->nhere.text, (len < 40 ? "" : "..."))); if (len <= PIPESIZE) { /* XXX eventually copy FreeBSD method */ xwrite(pip[1], redir->nhere.text, len); goto out; } VTRACE(DBG_REDIR, (" forking [%d,%d]\n", pip[0], pip[1])); if (forkshell(NULL, NULL, FORK_NOJOB) == 0) { close(pip[0]); signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGHUP, SIG_IGN); #ifdef SIGTSTP signal(SIGTSTP, SIG_IGN); #endif signal(SIGPIPE, SIG_DFL); xwrite(pip[1], redir->nhere.text, len); VTRACE(DBG_PROCS|DBG_REDIR, ("wrote here doc. exiting(0)\n")); _exit(0); } VTRACE(DBG_REDIR, ("openhere (closing %d)", pip[1])); out: close(pip[1]); VTRACE(DBG_REDIR, (" (pipe fd=%d)", pip[0])); return pip[0]; } /* * Undo the effects of the last redirection. */ void popredir(void) { struct redirtab *rp = redirlist; INTOFF; free_rl(rp, 1); redirlist = rp->next; ckfree(rp); INTON; } /* * Undo all redirections. Called on error or interrupt. */ #ifdef mkinit INCLUDE "redir.h" RESET { while (redirlist) popredir(); } SHELLPROC { clearredir(0); } #endif /* Return true if fd 0 has already been redirected at least once. */ int fd0_redirected_p(void) { return fd0_redirected != 0; } /* * Discard all saved file descriptors. */ void clearredir(int vforked) { struct redirtab *rp; struct renamelist *rl; for (rp = redirlist ; rp ; rp = rp->next) { if (!vforked) free_rl(rp, 0); else for (rl = rp->renamed; rl; rl = rl->next) if (rl->into >= 0) close(rl->into); } } /* * Copy a file descriptor to be == to. * cloexec indicates if we want close-on-exec or not. * Returns -1 if any error occurs. */ STATIC int copyfd(int from, int to, int cloexec) { int newfd; if (cloexec && to > 2) { #ifdef O_CLOEXEC newfd = dup3(from, to, O_CLOEXEC); #else newfd = dup2(from, to); fcntl(newfd, F_SETFD, fcntl(newfd,F_GETFD) | FD_CLOEXEC); #endif } else newfd = dup2(from, to); return newfd; } /* * rename fd from to be fd to (closing from). * close-on-exec is never set on 'to' (unless * from==to and it was set on from) - ie: a no-op * returns to (or errors() if an error occurs). * * This is mostly used for rearranging the * results from pipe(). */ int movefd(int from, int to) { if (from == to) return to; (void) close(to); if (copyfd(from, to, 0) != to) { int e = errno; (void) close(from); error("Unable to make fd %d: %s", to, strerror(e)); } (void) close(from); return to; } STATIC void find_big_fd(void) { int i, fd; static int last_start = 3; /* aim to keep sh fd's under 20 */ if (last_start < 10) last_start++; for (i = (1 << last_start); i >= 10; i >>= 1) { if ((fd = fcntl(0, F_DUPFD, i - 1)) >= 0) { close(fd); break; } } fd = (i / 5) * 4; if (fd < 10) fd = 10; big_sh_fd = fd; } /* * If possible, move file descriptor fd out of the way * of expected user fd values. Returns the new fd * (which may be the input fd if things do not go well.) * Always set close-on-exec on the result, and close * the input fd unless it is to be our result. */ int to_upper_fd(int fd) { int i; VTRACE(DBG_REDIR|DBG_OUTPUT, ("to_upper_fd(%d)", fd)); if (big_sh_fd < 10 || big_sh_fd >= user_fd_limit) find_big_fd(); do { i = fcntl(fd, F_DUPFD_CLOEXEC, big_sh_fd); if (i >= 0) { if (fd != i) close(fd); VTRACE(DBG_REDIR|DBG_OUTPUT, ("-> %d\n", i)); return i; } if (errno != EMFILE && errno != EINVAL) break; find_big_fd(); } while (big_sh_fd > 10); /* * If we wanted to move this fd to some random high number * we certainly do not intend to pass it through exec, even * if the reassignment failed. */ (void)fcntl(fd, F_SETFD, FD_CLOEXEC); VTRACE(DBG_REDIR|DBG_OUTPUT, (" fails ->%d\n", fd)); return fd; } void register_sh_fd(int fd, void (*cb)(int, int)) { struct shell_fds *fp; fp = ckmalloc(sizeof (struct shell_fds)); if (fp != NULL) { fp->nxt = sh_fd_list; sh_fd_list = fp; fp->fd = fd; fp->cb = cb; } } void sh_close(int fd) { struct shell_fds **fpp, *fp; fpp = &sh_fd_list; while ((fp = *fpp) != NULL) { if (fp->fd == fd) { *fpp = fp->nxt; ckfree(fp); break; } fpp = &fp->nxt; } (void)close(fd); } STATIC struct shell_fds * sh_fd(int fd) { struct shell_fds *fp; for (fp = sh_fd_list; fp != NULL; fp = fp->nxt) if (fp->fd == fd) return fp; return NULL; } STATIC int pick_new_fd(int fd) { int to; to = fcntl(fd, F_DUPFD_CLOEXEC, big_sh_fd); if (to == -1 && big_sh_fd >= 22) to = fcntl(fd, F_DUPFD_CLOEXEC, big_sh_fd/2); if (to == -1) to = fcntl(fd, F_DUPFD_CLOEXEC, fd + 1); if (to == -1) to = fcntl(fd, F_DUPFD_CLOEXEC, 10); if (to == -1) to = fcntl(fd, F_DUPFD_CLOEXEC, 3); if (to == -1) error("insufficient file descriptors available"); CLOEXEC(to); return to; } STATIC void renumber_sh_fd(struct shell_fds *fp) { int to; if (fp == NULL) return; /* * if we have had a collision, and the sh fd was a "big" one * try moving the sh fd base to a higher number (if possible) * so future sh fds are less likely to be in the user's sights * (incl this one when moved) */ if (fp->fd >= big_sh_fd) find_big_fd(); to = pick_new_fd(fp->fd); if (fp->fd == to) /* impossible? */ return; VTRACE(DBG_REDIR, ("renumber_sh_fd: moved shell fd %d to %d\n", fp->fd, to)); (*fp->cb)(fp->fd, to); (void)close(fp->fd); fp->fd = to; } static const struct flgnames { const char *name; uint16_t minch; uint32_t value; } nv[] = { #ifdef O_APPEND { "append", 2, O_APPEND }, #else # define O_APPEND 0 #endif #ifdef O_ASYNC { "async", 2, O_ASYNC }, #else # define O_ASYNC 0 #endif #ifdef O_SYNC { "sync", 2, O_SYNC }, #else # define O_SYNC 0 #endif #ifdef O_NONBLOCK { "nonblock", 3, O_NONBLOCK }, #else # define O_NONBLOCK 0 #endif #ifdef O_FSYNC { "fsync", 2, O_FSYNC }, #else # define O_FSYNC 0 #endif #ifdef O_DSYNC { "dsync", 2, O_DSYNC }, #else # define O_DSYNC 0 #endif #ifdef O_RSYNC { "rsync", 2, O_RSYNC }, #else # define O_RSYNC 0 #endif #ifdef O_ALT_IO { "altio", 2, O_ALT_IO }, #else # define O_ALT_IO 0 #endif #ifdef O_DIRECT { "direct", 2, O_DIRECT }, #else # define O_DIRECT 0 #endif #ifdef O_NOSIGPIPE { "nosigpipe", 3, O_NOSIGPIPE }, #else # define O_NOSIGPIPE 0 #endif #define ALLFLAGS (O_APPEND|O_ASYNC|O_SYNC|O_NONBLOCK|O_DSYNC|O_RSYNC|\ O_ALT_IO|O_DIRECT|O_NOSIGPIPE) #ifndef O_CLOEXEC # define O_CLOEXEC ((~ALLFLAGS) ^ ((~ALLFLAGS) & ((~ALLFLAGS) - 1))) #endif /* for any system we support, close on exec is always defined */ { "cloexec", 2, O_CLOEXEC }, { 0, 0, 0 } }; #ifndef O_ACCMODE # define O_ACCMODE 0 #endif #ifndef O_RDONLY # define O_RDONLY 0 #endif #ifndef O_WRONLY # define O_WRONLY 0 #endif #ifndef O_RWDR # define O_RWDR 0 #endif #ifndef O_SHLOCK # define O_SHLOCK 0 #endif #ifndef O_EXLOCK # define O_EXLOCK 0 #endif #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 #endif #ifndef O_CREAT # define O_CREAT 0 #endif #ifndef O_TRUNC # define O_TRUNC 0 #endif #ifndef O_EXCL # define O_EXCL 0 #endif #ifndef O_NOCTTY # define O_NOCTTY 0 #endif #ifndef O_DIRECTORY # define O_DIRECTORY 0 #endif #ifndef O_REGULAR # define O_REGULAR 0 #endif /* * flags that F_GETFL might return that we want to ignore * * F_GETFL should not actually return these, they're all just open() * modifiers, rather than state, but just in case... */ #define IGNFLAGS (O_ACCMODE|O_RDONLY|O_WRONLY|O_RDWR|O_SHLOCK|O_EXLOCK| \ O_NOFOLLOW|O_CREAT|O_TRUNC|O_EXCL|O_NOCTTY|O_DIRECTORY|O_REGULAR) static int getflags(int fd, int p) { int c, f; if (sh_fd(fd) != NULL || saved_redirected_fd(fd) != NULL) { if (!p) return -1; error("Can't get status for fd=%d (%s)", fd, strerror(EBADF)); } if ((c = fcntl(fd, F_GETFD)) == -1) { if (!p) return -1; error("Can't get status for fd=%d (%s)", fd, strerror(errno)); } if ((f = fcntl(fd, F_GETFL)) == -1) { if (!p) return -1; error("Can't get flags for fd=%d (%s)", fd, strerror(errno)); } f &= ~IGNFLAGS; /* clear anything we know about, but ignore */ if (c & FD_CLOEXEC) f |= O_CLOEXEC; return f; } static void printone(int fd, int p, int verbose, int pfd) { int f = getflags(fd, p); const struct flgnames *fn; if (f == -1) return; if (pfd) outfmt(out1, "%d: ", fd); for (fn = nv; fn->name; fn++) { if (f & fn->value) { outfmt(out1, "%s%s", verbose ? "+" : "", fn->name); f &= ~fn->value; } else if (verbose) outfmt(out1, "-%s", fn->name); else continue; if (f || (verbose && fn[1].name)) outfmt(out1, ","); } if (verbose && f) /* f should be normally be 0 */ outfmt(out1, " +%#x", f); outfmt(out1, "\n"); } static void parseflags(char *s, int *p, int *n) { int *v, *w; const struct flgnames *fn; size_t len; *p = 0; *n = 0; for (s = strtok(s, ","); s; s = strtok(NULL, ",")) { switch (*s++) { case '+': v = p; w = n; break; case '-': v = n; w = p; break; default: error("Missing +/- indicator before flag %s", s-1); } len = strlen(s); for (fn = nv; fn->name; fn++) if (len >= fn->minch && strncmp(s,fn->name,len) == 0) { *v |= fn->value; *w &=~ fn->value; break; } if (fn->name == 0) error("Bad flag `%s'", s); } } static void setone(int fd, int pos, int neg, int verbose) { int f = getflags(fd, 1); int n, cloexec; if (f == -1) return; cloexec = -1; if ((pos & O_CLOEXEC) && !(f & O_CLOEXEC)) cloexec = FD_CLOEXEC; if ((neg & O_CLOEXEC) && (f & O_CLOEXEC)) cloexec = 0; if (cloexec != -1 && fcntl(fd, F_SETFD, cloexec) == -1) error("Can't set status for fd=%d (%s)", fd, strerror(errno)); pos &= ~O_CLOEXEC; neg &= ~O_CLOEXEC; f &= ~O_CLOEXEC; n = f; n |= pos; n &= ~neg; if (n != f && fcntl(fd, F_SETFL, n) == -1) error("Can't set flags for fd=%d (%s)", fd, strerror(errno)); if (verbose) printone(fd, 1, verbose, 1); } int fdflagscmd(int argc, char *argv[]) { char *num; int verbose = 0, ch, pos = 0, neg = 0; char *setflags = NULL; optreset = 1; optind = 1; /* initialize getopt */ while ((ch = getopt(argc, argv, ":vs:")) != -1) switch ((char)ch) { case 'v': verbose = 1; break; case 's': if (setflags) goto msg; setflags = optarg; break; case '?': default: msg: error("Usage: fdflags [-v] [-s fd] [fd...]"); /* NOTREACHED */ } argc -= optind, argv += optind; if (setflags) parseflags(setflags, &pos, &neg); if (argc == 0) { int i; if (setflags) goto msg; for (i = 0; i <= max_user_fd; i++) printone(i, 0, verbose, 1); } else while ((num = *argv++) != NULL) { int fd = number(num); while (num[0] == '0' && num[1] != '\0') /* skip 0's */ num++; if (strlen(num) > 5 || (fd >= user_fd_limit && fd > max_user_fd)) error("%s: too big to be a file descriptor", num); if (setflags) setone(fd, pos, neg, verbose); else printone(fd, 1, verbose, argc > 1); } flushout(out1); if (io_err(out1)) { out2str("fdflags: I/O error\n"); return 1; } return 0; } #undef MAX /* in case we inherited them from somewhere */ #undef MIN #define MIN(a,b) (/*CONSTCOND*/((a)<=(b)) ? (a) : (b)) #define MAX(a,b) (/*CONSTCOND*/((a)>=(b)) ? (a) : (b)) /* now make the compiler work for us... */ #define MIN_REDIR MIN(MIN(MIN(MIN(NTO,NFROM), MIN(NTOFD,NFROMFD)), \ MIN(MIN(NCLOBBER,NAPPEND), MIN(NHERE,NXHERE))), NFROMTO) #define MAX_REDIR MAX(MAX(MAX(MAX(NTO,NFROM), MAX(NTOFD,NFROMFD)), \ MAX(MAX(NCLOBBER,NAPPEND), MAX(NHERE,NXHERE))), NFROMTO) static const char *redir_sym[MAX_REDIR - MIN_REDIR + 1] = { [NTO - MIN_REDIR]= ">", [NFROM - MIN_REDIR]= "<", [NTOFD - MIN_REDIR]= ">&", [NFROMFD - MIN_REDIR]= "<&", [NCLOBBER - MIN_REDIR]= ">|", [NAPPEND - MIN_REDIR]= ">>", [NHERE - MIN_REDIR]= "<<", [NXHERE - MIN_REDIR]= "<<", [NFROMTO - MIN_REDIR]= "<>", }; int outredir(struct output *out, union node *n, int sep) { if (n == NULL) return 0; if (n->type < MIN_REDIR || n->type > MAX_REDIR || redir_sym[n->type - MIN_REDIR] == NULL) return 0; if (sep) outc(sep, out); /* * ugly, but all redir node types have "fd" in same slot... * (and code other places assumes it as well) */ if ((redir_sym[n->type - MIN_REDIR][0] == '<' && n->nfile.fd != 0) || (redir_sym[n->type - MIN_REDIR][0] == '>' && n->nfile.fd != 1)) outfmt(out, "%d", n->nfile.fd); outstr(redir_sym[n->type - MIN_REDIR], out); switch (n->type) { case NHERE: outstr("'...'", out); break; case NXHERE: outstr("...", out); break; case NTOFD: case NFROMFD: if (n->ndup.dupfd < 0) outc('-', out); else outfmt(out, "%d", n->ndup.dupfd); break; default: outstr(n->nfile.expfname, out); break; } return 1; } @ 1.71 log @Detect write errors to stdout, and exit(1) from some built-in commands which (primarily) are used just to generate output (or with a particular option combination do so). @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.70 2021/11/10 15:26:34 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.70 2021/11/10 15:26:34 kre Exp $"); d462 6 a467 6 if (redir->type == NHERE) { len = strlen(redir->nhere.doc->narg.text); if (len <= PIPESIZE) { xwrite(pip[1], redir->nhere.doc->narg.text, len); goto out; } d479 1 a479 4 if (redir->type == NHERE) xwrite(pip[1], redir->nhere.doc->narg.text, len); else expandhere(redir->nhere.doc, pip[1]); @ 1.70 log @ DEBUG mode changes only. NFC (NC) for any normally compiled shell. Mostly adding DEBUG mode tracing (when appropriate verbose tracing is enabled generally) whenever a shell (including sushell) process exits, so shells that the tracing should indicate why ehslls that vanish did that. Note for future investigators: if the relevant tracing is enabled, and a (sub-)shell still simply seems to have vanished without trace, the likely cause is that it was killed by a signal - and of those, the most common that occurs is SIGPIPE. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.69 2021/09/15 20:21:47 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.69 2021/09/15 20:21:47 kre Exp $"); a1035 2 return 0; } d1037 1 a1037 1 while ((num = *argv++) != NULL) { d1051 5 @ 1.69 log @Fix an ordering error in the previous (and even earlier, going back a way, but made more serious with the recent changes). The n>&n operation (more or less a no-op, except it clears CLOEXEC) should precede almost everything else - and simply be made to fail if an attempt is made to apply it to a sh internal fd. We were renumbering the internal fd (the n> part considered first) which was dumb, but OK, before, but now rejecting the operation (the >&n) part when n should not be visible to the script. That made something of a mess (and could lead to the shell believing its job control tty was at a fd it never got moved to). Do things in the correct order, and simply fail that case for internal fds (for every other n>xxx for any xxx sh simply renumbers its internal fd n to some other fd before attempting the operation, even n>&- ... those are all fine). [In all the above the '>' is used in place of any redirect operator]. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.68 2021/09/15 18:29:45 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.68 2021/09/15 18:29:45 kre Exp $"); d483 1 a483 1 VTRACE(DBG_PROCS|DBG_REDIR, ("wrote here doc. exiting\n")); @ 1.68 log @Improve the solution for the 2nd access to a fd which shouldn't be available ("13") issue reported by Jan Schaumann on netbsd-users. This fixes a bug in the earlier fix (a day or so ago) which could allow the shell's idea of which fd range was in use by the script to get wildly incorrect, but now also actually looks to see which fd's are in use as renamed other user fd's during the lifetime of a redirection which needs to be able to be undone (most redirections occur after a fork and are permanent in the child process). Attempting to access such a fd (as with attempts to access a fd in use by the shell for its own purposes) is treated as an attempt to access a closed fd (EBADF). Attempting to reuse the fd for some other purpose (which should be rare, even for scripts attempting to cause problems, since the shell generally knows which fds the script wants to use, and avoids them) will cause the renamed (renumbered) fd to be renamed again (moved aside to some other available fd), just as happens with the shell's private fds. Also, when a generic fd is required, don't give up because of EMFILE or similar unless there are no available fds at all (we might prefer >10 or bigger, but if there are none there, use anything). This avoids redirection errors when ulimit -n has been set small, and all the fds >10 that are available have been used, but we need somewhere to park the old user of a fd while we reuse that fd for the redirection. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.67 2021/09/14 14:49:39 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.67 2021/09/14 14:49:39 kre Exp $"); d248 12 a269 9 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && n->ndup.dupfd == fd) { /* redirect from/to same file descriptor */ /* make sure it stays open */ if (fcntl(fd, F_SETFD, 0) < 0) error("fd %d: %s", fd, strerror(errno)); VTRACE(DBG_REDIR, ("!cloexec\n")); continue; } d427 1 @ 1.67 log @Deal with some issues where fds intended only for internal use by the shell were available for manipulation by scripts (or the user). These issues were reported by Jan Schaumann on netbsd-users. The first allows the user to reference sh internal fds, and is a simple fix - any sh internal fd is simply treated as if it were closed when referenced by the script. These fds can be discovered by examining /proc/N/fd so it is not difficult for a script to discover which fd it should attempt to access. The second allows the user to reference a user level fd which is one that is normally available to it, but at a point where it should no longer be visible (when that fd has been redirected, for a built in command, so the original fd needs to be saved so it can be restored, the saving fd should not be accessible). It is not as easy for the script to determine which fd to attempt here, as the relevant one exists only during the lifetime of a built-in command (and similar), but there are ways in some cases (aside from looking at /proc from another process). Fix this one by watching which fds the user script is attempting to use, and avoid using those as temporary fds. This is possible in this case as we know what command is being run, before we need to save the fds it uses. That's different from the earlier case where when the shell allocates its fds we have no idea what it might reference later. Also clean up a couple of other minor code issues (NFC intended) that I noticed while here... @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.66 2019/03/01 06:15:01 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.66 2019/03/01 06:15:01 kre Exp $"); d124 1 d140 1 d155 15 d241 2 d244 3 a246 2 VTRACE(DBG_REDIR, ("redir %d (max=%d) ", fd, max_user_fd)); if (fd > max_user_fd) d248 9 d287 4 d300 1 a300 1 VTRACE(DBG_REDIR, ("saved as %d ", i)); d389 2 a390 1 if (sh_fd(redir->ndup.dupfd) != NULL) d641 1 a641 1 if (big_sh_fd < 10) d709 20 d746 1 a746 12 to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd); if (to == -1 && big_sh_fd >= 22) to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd/2); if (to == -1) to = fcntl(fp->fd, F_DUPFD_CLOEXEC, fp->fd + 1); if (to == -1) to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 10); if (to == -1) to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 3); if (to == -1) error("insufficient file descriptors available"); CLOEXEC(to); d751 3 d880 1 a880 1 if (sh_fd(fd) != NULL) { d1040 3 a1042 2 if (strlen(num) > 5) error("%s too big to be a file descriptor", num); @ 1.66 log @The previous commit was obviously made by a broken mindless automoton with an IQ that underflows when one attempts to enter it as an unnormalised 160 bit long long double... Whoever would believe that (~0 & anything) was a meaningful thing to write? And three times in one #define. That could not possibly have been me, could it? Simplify, simplify, simplify. NFC. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.65 2019/03/01 05:23:35 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.65 2019/03/01 05:23:35 kre Exp $"); d240 2 d245 3 a247 1 if ((i = fcntl(fd, F_DUPFD, big_sh_fd)) == -1) { d260 1 a260 2 i = errno; error("%d: %s", fd, strerror(i)); d356 3 d693 1 a693 1 if (to == -1) d837 1 a837 2 error("Can't get status for fd=%d (%s)", fd, "Bad file descriptor"); /*XXX*/ @ 1.65 log @Inspired by (really the need for) Maya's patch to pkgsrc/shells/bash to allow bash to build fdflags on Solaris 10, here are some mods that fix that, and some other similar issues in the NetBSD version of fdflags. The bash implementation of fdflags is based upon the one Christos did for the NetBSD sh, so the issues are similar ... the NetBSD sh cannot yet (easily anyway) build on anything except NetBSD, so this change makes no current difference at all (just adds some compile time tests (#ifdef) which always work out the way things did before, when built on NetBSD). However, there is no system on which any modern shell can hope to work which does not support close on exec, or fcntl(F_SETFD,...) to set it. The O_CLOEXEC and FD_CLOEXEC definitions might not exist, but close on exec can still be manipulated. Since the primary rationale for the fdflags builtin was to be able to manipulate that state bit from scripts, it would be annoying to lose that one, and keep all the (less important) others, just because O_CLOEXEC is not defined, so do the fix (workaround) a different way than was done in the bash patch. Further, more than fdflags() will fail if O_CLOEXEC is not defined, so handle that as well. Also fix another oddity ... (noticed by reading the code) - if fcntl(F_GETFL,...) returned any bits set that we don't understand, the code was supposed to simply print their values as a hex constant, when fdflags is run with -v. However, the getflags() function was clearing all bits that the code did not know about ... so there is no way any unknown bit could ever make it out to be printed. Handle that a different way - instead of clearing unknown bits, clear any bits that get returned which we understand, but do not want to deal with (stuff like O_WRONLY, which should not be returned from the fcntl(), but who knows...) Leave any unknown bits that happen to be set, set, so that printone() can display them if appropriate. (This is most likely to happen when running an older shell on a new kernel where the kernel supports some new flag that the shell has not been taught to understand). NFCI that anyone should notice anytime soon. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.64 2019/02/09 09:34:43 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.64 2019/02/09 09:34:43 kre Exp $"); d767 1 a767 2 # define O_CLOEXEC ((~0 & ~ALLFLAGS) ^ \ ((~0 & ~ALLFLAGS) & ((~0 & ~ALLFLAGS) - 1))) @ 1.64 log @ DEBUG mode change only. Add one extra trace point. NFC for normal builds. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.63 2019/02/09 03:35:55 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.63 2019/02/09 03:35:55 kre Exp $"); d81 11 d525 2 a526 1 if (cloexec && to > 2) d528 5 a532 1 else a676 7 #ifndef F_DUPFD_CLOEXEC #define F_DUPFD_CLOEXEC F_DUPFD #define CLOEXEC(fd) (fcntl((fd), F_SETFD, fcntl((fd),F_GETFD) | FD_CLOEXEC)) #else #define CLOEXEC(fd) #endif d714 2 d719 2 d724 2 d729 2 d734 2 d739 2 d744 2 d749 2 d754 2 d759 2 d762 10 a771 1 #ifdef O_CLOEXEC a772 1 #endif d775 48 a822 2 #define ALLFLAGS (O_APPEND|O_ASYNC|O_SYNC|O_NONBLOCK|O_DSYNC|O_RSYNC|\ O_ALT_IO|O_DIRECT|O_NOSIGPIPE|O_CLOEXEC) d846 1 d849 1 a849 1 return f & ALLFLAGS; @ 1.63 log @ INTON / INTOFF audit and cleanup. No visible differences expected - there is a remote chance that some internal lossage may no longer occur in interactive shells that receive SIGINT (untrapped) at inopportune times, but you would have had to have been very unlucky to have ever suffered from that. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.62 2018/11/26 20:03:39 kamil Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.62 2018/11/26 20:03:39 kamil Exp $"); d428 1 @ 1.62 log @Fix typo: O_ALTIO -> O_ALT_IO Noted by @@jbeich via GitHub. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.61 2018/11/23 23:41:20 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.61 2018/11/23 23:41:20 kre Exp $"); a245 1 INTON; /* XXX not needed here ? */ @ 1.61 log @ Fix the <> redirection operator, which has been broken since it was first implemented in response to PR bin/4966 (PR Feb 1998, fix Feb 1999). The file named should not be truncated. No other shell truncates the file (<> was added to FreeBSD sh in Oct 2000, and did not include O_TRUNC) and POSIX certainly does not suggest that should happen (just that the file is to be created if it does not exist.) Bug pointed out in off-list e-mail by Martijn Dekker @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.60 2018/08/13 22:13:02 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.60 2018/08/13 22:13:02 kre Exp $"); d724 1 a724 1 #ifdef O_ALTIO @ 1.60 log @ NFC: DEBUG (compile time) mode only change: Add some extra redirection (fd manipulation) tracing. While here, some white space fixes, and very minor KNF. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.59 2017/11/15 09:21:48 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.59 2017/11/15 09:21:48 kre Exp $"); d302 1 a302 1 if ((f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) @ 1.59 log @DEBUG mode only change. Add some tracing. NFC (without DEBUG). @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.58 2017/06/30 23:01:21 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.58 2017/06/30 23:01:21 kre Exp $"); d151 3 d155 2 a156 1 if (rl->into < 0) d158 2 a159 1 else d161 1 d171 1 d198 1 d203 2 a204 1 /* We don't have to worry about REDIR_VFORK here, as d214 1 d224 1 d254 1 d257 1 d323 2 d361 1 a362 1 VTRACE(DBG_REDIR, ("openredirect: %d<<...", fd)); d385 1 a385 1 ecreate: d388 1 a388 1 eopen: d415 1 d431 2 a432 1 out: d434 1 d526 1 a526 1 * returns to (or errors() if an error occurs). d817 1 a817 1 @ 1.59.4.1 log @Sync with HEAD @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.66 2019/03/01 06:15:01 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.66 2019/03/01 06:15:01 kre Exp $"); a80 11 #ifndef FD_CLOEXEC # define FD_CLOEXEC 1 /* well known from before there was a name */ #endif #ifndef F_DUPFD_CLOEXEC #define F_DUPFD_CLOEXEC F_DUPFD #define CLOEXEC(fd) (fcntl((fd), F_SETFD, fcntl((fd),F_GETFD) | FD_CLOEXEC)) #else #define CLOEXEC(fd) #endif a150 3 VTRACE(DBG_REDIR, ("popredir %d%s: %s", rl->orig, rl->orig==0 ? " (STDIN)" : "", reset ? "" : "no reset\n")); d152 1 a152 2 if (rl->into < 0) { VTRACE(DBG_REDIR, ("closed\n")); d154 1 a154 2 } else { VTRACE(DBG_REDIR, ("from %d\n", rl->into)); a155 1 } a164 1 /* XXX someday keep a short list (8..10) of freed renamelists XXX */ a190 1 CTRACE(DBG_REDIR, ("redirect(F=0x%x):%s\n", flags, redir?"":" NONE")); d195 1 a195 2 /* * We don't have to worry about REDIR_VFORK here, as a204 1 VTRACE(DBG_REDIR, ("redir %d (max=%d) ", fd, max_user_fd)); a213 1 VTRACE(DBG_REDIR, ("!cloexec\n")); d235 1 a242 1 VTRACE(DBG_REDIR, ("saved as %d ", i)); a244 1 VTRACE(DBG_REDIR, ("%s\n", fd == 0 ? "STDIN" : "")); d289 1 a289 1 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) a309 2 VTRACE(DBG_REDIR, ("openredirect(>| '%s') -> %d", fname, f)); d346 1 a347 1 f = openhere(redir); d370 1 a370 1 ecreate: d373 1 a373 1 eopen: a399 1 VTRACE(DBG_REDIR, (" forking [%d,%d]\n", pip[0], pip[1])); a412 1 VTRACE(DBG_PROCS|DBG_REDIR, ("wrote here doc. exiting\n")); d415 1 a415 2 VTRACE(DBG_REDIR, ("openhere (closing %d)", pip[1])); out: a416 1 VTRACE(DBG_REDIR, (" (pipe fd=%d)", pip[0])); d496 1 a496 2 if (cloexec && to > 2) { #ifdef O_CLOEXEC d498 1 a498 5 #else newfd = dup2(from, to); fcntl(newfd, F_SETFD, fcntl(newfd,F_GETFD) | FD_CLOEXEC); #endif } else d508 1 a508 1 * returns to (or errors() if an error occurs). d643 7 a686 2 #else # define O_APPEND 0 a689 2 #else # define O_ASYNC 0 a692 2 #else # define O_SYNC 0 a695 2 #else # define O_NONBLOCK 0 a698 2 #else # define O_FSYNC 0 a701 2 #else # define O_DSYNC 0 a704 2 #else # define O_RSYNC 0 d706 1 a706 1 #ifdef O_ALT_IO a707 2 #else # define O_ALT_IO 0 a710 2 #else # define O_DIRECT 0 a713 2 #else # define O_NOSIGPIPE 0 d715 2 a716 6 #define ALLFLAGS (O_APPEND|O_ASYNC|O_SYNC|O_NONBLOCK|O_DSYNC|O_RSYNC|\ O_ALT_IO|O_DIRECT|O_NOSIGPIPE) #ifndef O_CLOEXEC # define O_CLOEXEC ((~ALLFLAGS) ^ ((~ALLFLAGS) & ((~ALLFLAGS) - 1))) a717 3 /* for any system we support, close on exec is always defined */ { "cloexec", 2, O_CLOEXEC }, d720 2 a721 48 #ifndef O_ACCMODE # define O_ACCMODE 0 #endif #ifndef O_RDONLY # define O_RDONLY 0 #endif #ifndef O_WRONLY # define O_WRONLY 0 #endif #ifndef O_RWDR # define O_RWDR 0 #endif #ifndef O_SHLOCK # define O_SHLOCK 0 #endif #ifndef O_EXLOCK # define O_EXLOCK 0 #endif #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 #endif #ifndef O_CREAT # define O_CREAT 0 #endif #ifndef O_TRUNC # define O_TRUNC 0 #endif #ifndef O_EXCL # define O_EXCL 0 #endif #ifndef O_NOCTTY # define O_NOCTTY 0 #endif #ifndef O_DIRECTORY # define O_DIRECTORY 0 #endif #ifndef O_REGULAR # define O_REGULAR 0 #endif /* * flags that F_GETFL might return that we want to ignore * * F_GETFL should not actually return these, they're all just open() * modifiers, rather than state, but just in case... */ #define IGNFLAGS (O_ACCMODE|O_RDONLY|O_WRONLY|O_RDWR|O_SHLOCK|O_EXLOCK| \ O_NOFOLLOW|O_CREAT|O_TRUNC|O_EXCL|O_NOCTTY|O_DIRECTORY|O_REGULAR) a744 1 f &= ~IGNFLAGS; /* clear anything we know about, but ignore */ d747 1 a747 1 return f; d799 1 a799 1 @ 1.59.4.2 log @Sync with HEAD @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.59.4.1 2019/06/10 21:41:04 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.59.4.1 2019/06/10 21:41:04 christos Exp $"); @ 1.59.4.3 log @Ooops, restore accidently removed files from merge mishap @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.66 2019/03/01 06:15:01 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.66 2019/03/01 06:15:01 kre Exp $"); @ 1.59.2.1 log @Sync with HEAD Resolve a couple of conflicts (result of the uimin/uimax changes) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.60 2018/08/13 22:13:02 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.60 2018/08/13 22:13:02 kre Exp $"); a150 3 VTRACE(DBG_REDIR, ("popredir %d%s: %s", rl->orig, rl->orig==0 ? " (STDIN)" : "", reset ? "" : "no reset\n")); d152 1 a152 2 if (rl->into < 0) { VTRACE(DBG_REDIR, ("closed\n")); d154 1 a154 2 } else { VTRACE(DBG_REDIR, ("from %d\n", rl->into)); a155 1 } a164 1 /* XXX someday keep a short list (8..10) of freed renamelists XXX */ a190 1 CTRACE(DBG_REDIR, ("redirect(F=0x%x):%s\n", flags, redir?"":" NONE")); d195 1 a195 2 /* * We don't have to worry about REDIR_VFORK here, as a204 1 VTRACE(DBG_REDIR, ("redir %d (max=%d) ", fd, max_user_fd)); a213 1 VTRACE(DBG_REDIR, ("!cloexec\n")); a242 1 VTRACE(DBG_REDIR, ("saved as %d ", i)); a244 1 VTRACE(DBG_REDIR, ("%s\n", fd == 0 ? "STDIN" : "")); a309 2 VTRACE(DBG_REDIR, ("openredirect(>| '%s') -> %d", fname, f)); d346 1 a347 1 f = openhere(redir); d370 1 a370 1 ecreate: d373 1 a373 1 eopen: a399 1 VTRACE(DBG_REDIR, (" forking [%d,%d]\n", pip[0], pip[1])); d415 1 a415 2 VTRACE(DBG_REDIR, ("openhere (closing %d)", pip[1])); out: a416 1 VTRACE(DBG_REDIR, (" (pipe fd=%d)", pip[0])); d508 1 a508 1 * returns to (or errors() if an error occurs). d799 1 a799 1 @ 1.59.2.2 log @Sync with HEAD, resolve a couple of conflicts @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.61 2018/11/23 23:41:20 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.61 2018/11/23 23:41:20 kre Exp $"); d302 1 a302 1 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) @ 1.59.2.3 log @Sync with HEAD, resolve a few conflicts @ text @d1 1 a1 1 /* $NetBSD$ */ d40 1 a40 1 __RCSID("$NetBSD$"); d724 1 a724 1 #ifdef O_ALT_IO @ 1.58 log @ Include redirections in trace output from "set -x" @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre Exp $"); d70 1 d282 2 d291 1 d317 1 d323 1 d335 3 a337 1 } else d339 3 d347 1 d355 1 d366 1 d566 1 d574 1 d588 1 @ 1.57 log @ Now that the shell is protecting its internal fds properly, moving them whenever the user tries to step on one, we can change our behaviour back to what the kernel considers to be that of a well behaved shell (wrt file descriptor usage). If our user causes problems, we will soon move into recalcitrant process territory, but that should normally be rare. This should reduce kernel overheads a little. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.56 2017/05/18 13:56:58 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.56 2017/05/18 13:56:58 kre Exp $"); d881 67 @ 1.57.2.1 log @Pull up following revision(s) (requested by kre in ticket #103): bin/kill/kill.c: 1.28 bin/sh/Makefile: 1.111-1.113 bin/sh/arith_token.c: 1.5 bin/sh/arith_tokens.h: 1.2 bin/sh/arithmetic.c: 1.3 bin/sh/arithmetic.h: 1.2 bin/sh/bltin/bltin.h: 1.15 bin/sh/cd.c: 1.49-1.50 bin/sh/error.c: 1.40 bin/sh/eval.c: 1.142-1.151 bin/sh/exec.c: 1.49-1.51 bin/sh/exec.h: 1.26 bin/sh/expand.c: 1.113-1.119 bin/sh/expand.h: 1.23 bin/sh/histedit.c: 1.49-1.52 bin/sh/input.c: 1.57-1.60 bin/sh/input.h: 1.19-1.20 bin/sh/jobs.c: 1.86-1.87 bin/sh/main.c: 1.71-1.72 bin/sh/memalloc.c: 1.30 bin/sh/memalloc.h: 1.17 bin/sh/mknodenames.sh: 1.4 bin/sh/mkoptions.sh: 1.3-1.4 bin/sh/myhistedit.h: 1.12-1.13 bin/sh/nodetypes: 1.16-1.18 bin/sh/option.list: 1.3-1.5 bin/sh/parser.c: 1.133-1.141 bin/sh/parser.h: 1.22-1.23 bin/sh/redir.c: 1.58 bin/sh/redir.h: 1.24 bin/sh/sh.1: 1.149-1.159 bin/sh/shell.h: 1.24 bin/sh/show.c: 1.43-1.47 bin/sh/show.h: 1.11 bin/sh/syntax.c: 1.4 bin/sh/syntax.h: 1.8 bin/sh/trap.c: 1.41 bin/sh/var.c: 1.56-1.65 bin/sh/var.h: 1.29-1.35 An initial attempt at implementing LINENO to meet the specs. Aside from one problem (not too hard to fix if it was ever needed) this version does about as well as most other shell implementations when expanding $((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the latter, and that is very accurate. Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce the same value when used on the same line (a defect that other shells do not share - aside from the FreeBSD sh as it is today, where only the LINENO hack exists and so (like for us before this commit) $((LINENO)) is always either 0, or at least whatever value was last set, perhaps by LINENO=${LINENO} which does actually work ... for that one line...) This could be corrected by simply removing the LINENO hack (look for the string LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the same (not perfectly accurate) values, as do most other shells. POSIX requires that LINENO be set before each command, and this implementation does that fairly literally - except that we only bother before the commands which actually expand words (for, case and simple commands). Unfortunately this forgot that expansions also occur in redirects, and the other compound commands can also have redirects, so if a redirect on one of the other compound commands wants to use the value of $((LINENO)) as a part of a generated file name, then it will get an incorrect value. This is the "one problem" above. (Because the LINENO hack is still enabled, using ${LINENO} works.) This could be fixed, but as this version of the LINENO implementation is just for reference purposes (it will be superseded within minutes by a better one) I won't bother. However should anyone else decide that this is a better choice (it is probably a smaller implementation, in terms of code & data space then the replacement, but also I would expect, slower, and definitely less accurate) this defect is something to bear in mind, and fix. This version retains the *BSD historical practice that line numbers in functions (all functions) count from 1 from the start of the function, and elsewhere, start from 1 from where the shell started reading the input file/stream in question. In an "eval" expression the line number starts at the line of the "eval" (and then increases if the input is a multi-line string). Note: this version is not documented (beyond as much as LINENO was before) hence this slightly longer than usual commit message. A better LINENO implementation. This version deletes (well, #if 0's out) the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)). (Code to invert the LINENO hack when required, like when de-compiling the execution tree to provide the "jobs" command strings, is still included, that can be deleted when the LINENO hack is completely removed - look for refs to VSLINENO throughout the code. The var funclinno in parser.c can also be removed, it is used only for the LINENO hack.) This version produces accurate results: $((LINENO)) was made as accurate as the LINENO hack made ${LINENO} which is very good. That's why the LINENO hack is not yet completely removed, so it can be easily re-enabled. If you can tell the difference when it is in use, or not in use, then something has broken (or I managed to miss a case somewhere.) The way that LINENO works is documented in its own (new) section in the man page, so nothing more about that, or the new options, etc, here. This version introduces the possibility of having a "reference" function associated with a variable, which gets called whenever the value of the variable is required (that's what implements LINENO). There is just one function pointer however, so any particular variable gets at most one of the set function (as used for PATH, etc) or the reference function. The VFUNCREF bit in the var flags indicates which func the variable in question uses (if any - the func ptr, as before, can be NULL). I would not call the results of this perfect yet, but it is close. Unbreak (at least) i386 build .... I have no idea why this built for me on amd64 (problem was missing prototype for snprintf witout ) While here, add some (DEBUG mode only) tracing that proved useful in solving another problem. Set the line number before expanding args, not after. As the line_number would have usually been set earlier, this change is mostly an effective no-op, but it is better this way (just in case) - not observed to have caused any problems. Undo some over agressive fixes for a (pre-commit) bug that did not need these changes to be fixed - and these cause problems in another absurd use case. Either of these issues is unlikely to be seen by anyone who isn't an idiot masochist... PR bin/52280 removescapes_nl in expari() even when not quoted, CRTNONL's appear regardless of quoting (unlike CTLESC). New sentence, new line. Whitespace. Improve the (new) LINENO section, markup changes (with thanks to wiz@@ for assistace) and some better wording in a few placed. I am an idiot... revert the previous unintended commit. Remove some left over baggage from the LINENO v1 implementation that didn't get removed with v2, and should have. This would have had (I think, without having tested it) one very minor effect on the way LINENO worked in the v2 implementation, but my guess is it would have taken a long time before anyone noticed... Correct spelling in comments of DEBUG only code... (Perhaps) temporary fix to pkgtools (cwrappers) build (configure). Expanding `` containing \ \n sequences looks to have been giving problems. I don't think this is the correct fix, but it will do no worse harm than (perhaps) incorrectly calculating LINENO in this kind of (rare) circumstance. I'll look and see if there should be a better fix later. s/volatile/const/ -- wonderful how opposites attract like this. NFC (normal use) - DEBUG only change, when showing empty arg list don't omit terminating \n. Free stack memory in a couple of obscure cases where it wasn't being done (one in probably dead code that is never compiled, the other in a very rare error case.) Since it is stack memory it wasn't lost in any case, just held longer than needed. Many internal memory management type fixes. PR bin/52302 (core dump with interactive shell, here doc and error on same line) is fixed. (An old bug.) echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )" consistently prints 1002 lines (x, 1000 empty ones, then y) as it should (And you don't want to know what it did before, or why.) (Another old one.) (Recently added) Problems with ~ expansion fixed (mem management related). Proper fix for the cwrappers configure problem (which includes the quick fix that was done earlier, but extends upon that to be correct). (This was another newly added problem.) And the really devious (and rare) old bug - if STACKSTRNUL() needs to allocate a new buffer in which to store the \0, calculate the size of the string space remaining correctly, unlike when SPUTC() grows the buffer, there is no actual data being stored in the STACKSTRNUL() case - the string space remaining was calculated as one byte too few. That would be harmless, unless the next buffer also filled, in which case it was assumed that it was really full, not one byte less, meaning one junk char (a nul, or anything) was being copied into the next (even bigger buffer) corrupting the data. Consistent use of stalloc() to allocate a new block of (stack) memory, and grabstackstr() to claim a block of (stack) memory that had already been occupied but not claimed as in use. Since grabstackstr is implemented as just a call to stalloc() this is a no-op change in practice, but makes it much easier to comprehend what is really happening. Previous code sometimes used stalloc() when the use case was really for grabstackstr(). Change grabstackstr() to actually use the arg passed to it, instead of (not much better than) guessing how much space to claim, More care when using unstalloc()/ungrabstackstr() to return space, and in particular when the stack must be returned to its previous state, rather than just returning no-longer needed space, neither of those work. They also don't work properly if there have been (really, even might have been) any stack mem allocations since the last stalloc()/grabstackstr(). (If we know there cannot have been then the alloc/release sequence is kind of pointless.) To work correctly in general we must use setstackmark()/popstackmark() so do that when needed. Have those also save/restore the top of stack string space remaining. [Aside: for those reading this, the "stack" mentioned is not in any way related to the thing used for maintaining the C function call state, ie: the "stack segment" of the program, but the shell's internal memory management strategy.] More comments to better explain what is happening in some cases. Also cleaned up some hopelessly broken DEBUG mode data that were recently added (no effect on anyone but the poor semi-human attempting to make sense of it...). User visible changes: Proper counting of line numbers when a here document is delimited by a multi-line end-delimiter, as in cat << 'REALLY END' here doc line 1 here doc line 2 REALLY END (which is an obscure case, but nothing says should not work.) The \n in the end-delimiter of the here doc (the last one) was not incrementing the line number, which from that point on in the script would be 1 too low (or more, for end-delimiters with more than one \n in them.) With tilde expansion: unset HOME; echo ~ changed to return getpwuid(getuid())->pw_home instead of failing (returning ~) POSIX says this is unspecified, which makes it difficult for a script to compensate for being run without HOME set (as in env -i sh script), so while not able to be used portably, this seems like a useful extension (and is implemented the same way by some other shells). Further, with HOME=; printf %s ~ we now write nothing (which is required by POSIX - which requires ~ to expand to the value of $HOME if it is set) previously if $HOME (in this case) or a user's directory in the passwd file (for ~user) were a null STRING, We failed the ~ expansion and left behind '~' or '~user'. Changed the long name for the -L option from lineno_fn_relative to local_lineno as the latter seemed to be marginally more popular, and perhaps more importantly, is the same length as the peviously existing quietprofile option, which means the man page indentation for the list of options can return to (about) what it was before... (That is, less indented, which means more data/line, which means less lines of man page - a good thing!) Cosmetic changes to variable flags - make their values more suited to my delicate sensibilities... (NFC). Arrange not to barf (ever) if some turkey makes _ readonly. Do this by adding a VNOERROR flag that causes errors in var setting to be ignored (intended use is only for internal shell var setting, like of "_"). (nb: invalid var name errors ignore this flag, but those should never occur on a var set by the shell itself.) From FreeBSD: don't simply discard memory if a variable is not set for any reason (including because it is readonly) if the var's value had been malloc'd. Free it instead... NFC - DEBUG changes, update this to new TRACE method. KNF - white space and comment formatting. NFC - DEBUG mode only change - convert this to the new TRACE() format. NFC - DEBUG mode only change - complete a change made earlier (marking the line number when included in the trace line tag to show whether it comes from the parser, or the elsewhere as they tend to be quite different). Initially only one case was changed, while I pondered whether I liked it or not. Now it is all done... Also when there is a line tag at all, always include the root/sub-shell indicator character, not only when the pid is included. NFC: DEBUG related comment change - catch up with reality. NFC: DEBUG mode only change. Fix botched cleanup of one TRACE(). "b" more forgiving when sorting options to allow reasonable (and intended) flexibility in option.list format. Changes nothing for current option.list. Now that excessive use of STACKSTRNUL has served its purpose (well, accidental purpose) in exposing the bug in its implementation, go back to not using it when not needed for DEBUG TRACE purposes. This change should have no practical effect on either a DEBUG shell (where the STACKSTRNUL() calls remain) or a non DEBUG shell where they are not needed. Correct the initial line number used for processing -c arg strings. (It was inheriting the value from end of profile file processing) - I didn't notice before as I usually test with empty or no profile files to avoid complications. Trivial change which should have very limited impact. Fix from FreeBSD (applied there in July 2008...) Don't dump core with input like sh -c 'x=; echo >&$x' - that is where the word after a >& or <& redirect expands to nothing at all. Another fix from FreeBSD (this one from April 2009). When processing a string (as in eval, trap, or sh -c) don't allow trailing \n's to destroy the exit status of the last command executed. That is: sh -c 'false ' echo $? should produce 1, not 0. It is amazing what nonsense appears to work sometimes... (all my nonsense too!) Two bugs here, one benign because of the way the script is used. The other hidden by NetBSD's sort being stable, and the data not really requiring sorting at all... So as it happens these fixes change nothing, but they are needed anyway. (The contents of the generated file are only used in DEBUG shells, so this is really even less important than it seems.) Another ancient (highly improbable) bug bites the dust. This one caused by incorrect macro usage (ie: using the wrong one) which has been in the sources since version 1.1 (ie: forever). Like the previous (STACKSTRNUL) bug, the probability of this one actually occurring has been infinitesimal but the LINENO code increases that to infinitesimal and a smidgen... (or a few, depending upon usage). Still, apparently that was enough, Kamil Rytarowski discovered that the zsh configure script (damn competition!) managed to trigger this problem. source .editrc after we initialize so that commands persist! Make arg parsing in kill POSIX compatible with POSIX (XBD 2.12) by parsing the way getopt(3) would, if only it could handle the (required) -signumber and -signame options. This adds two "features" to kill, -ssigname and -lstatus now work (ie: one word with all of the '-', the option letter, and its value) and "--" also now works (kill -- -pid1 pid2 will not attempt to send the pid1 signal to pid2, but rather SIGTERM to the pid1 process group and pid2). It is still the case that (apart from --) at most 1 option is permitted (-l, -s, -signame, or -signumber.) Note that we now have an ambiguity, -sname might mean "-s name" or send the signal "sname" - if one of those turns out to be valid, that will be accepted, otherwise the error message will indicate that "sname" is not a valid signal name, not that "name" is not. Keeping the "-s" and signal name as separate words avoids this issue. Also caution: should someone be weird enough to define a new signal name (as in the part after SIG) which is almost the same name as an existing name that starts with 'S' by adding an extra 'S' prepended (eg: adding a SIGSSYS) then the ambiguity problem becomes much worse. In that case "kill -ssys" will be resolved in favour of the "-s" flag being used (the more modern syntax) and would send a SIGSYS, rather that a SIGSSYS. So don't do that. While here, switch to using signalname(3) (bye bye NSIG, et. al.), add some constipation, and show a little pride in formatting the signal names for "kill -l" (and in the usage when appropriate -- same routine.) Respect COLUMNS (POSIX XBD 8.3) as primary specification of the width (terminal width, not number of columns to print) for kill -l, a very small value for COLUMNS will cause kill -l output to list signals one per line, a very large value will cause them all to be listed on one line.) (eg: "COLUMNS=1 kill -l") TODO: the signal printing for "trap -l" and that for "kill -l" should be switched to use a common routine (for the sh builtin versions.) All changes of relevance here are to bin/kill - the (minor) changes to bin/sh are only to properly expose the builtin version of getenv(3) so the builtin version of kill can use it (ie: make its prototype available.) Properly support EDITRC - use it as (naming) the file when setting up libedit, and re-do the config whenever EDITRC is set. Get rid of workarounds for ancient groff html backend. Simplify macro usage. Make one example more like a real world possibility (it still isn't, but is closer) - though the actual content is irrelevant to the point being made. Add literal prompt support this allows one to do: CA="$(printf '\1')" PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} " Now libedit supports embedded mode switch sequence, improve sh support for them (adds PSlit variable to set the magic character). NFC: DEBUG only change - provide an externally visible (to the DEBUG sh internals) interface to one of the internal (private to trace code) functions Include redirections in trace output from "set -x" Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic expansions, and if enabled by the promptcmds option, command substitutions.) Implement a bunch of new shell environment variables. many mostly useful in prompts when expanded at prompt time, but all available for general use. Many of the new ones are not available in SMALL shells (they work as normal if assigned, but the shell does not set or use them - and there is no magic in a SMALL shell (usually for install media.)) Omnibus manual update for prompt expansions and new variables. Throw in some random cleanups as a bonus. Correct a markup typo (why did I not see this before the prev commit??) Sort options (our default is 0..9AaBbZz). Fix markup problems and a typo. Make $- list flags in the same order they appear in sh(1) Do a better job of detecting the error in pkgsrc/devel/libbson-1.6.3's configure script, ie: $(( which is intended to be a sub-shell in a command substitution, but is an arith subst instead, it needs to be written $( ( to do as intended. Instead of just blindly carrying on to find the missing )) somewhere, anywhere, give up as soon as we have seen an unbalanced ')' that isn't immediately followed by another ')' which in a valid arith subst it always would be. While here, there has been a comment in the code for quite a while noting a difference in the standard between the text descr & grammar when it comes to the syntax of case statements. Add more comments to explain why parsing it as we do is in fact definitely the correct way (ie: the grammar wins arguments like this...). DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode to the new style. NFC (when not debugging sh). Mostly DEBUG and white space changes. Convert DEEBUG TRACE() calls to the new format. Also #if 0 a function definition that is used nowhere. While here, change the function of pushfile() slightly - it now sets the buf pointer in the top (new) input descriptor to NULL, instead of simply leaving it - code that needs a buffer always (before and after) must malloc() one and assign it after the call. But code which does not (which will be reading from a string or similar) now does not have to explicitly set it to NULL (cleaner interface.) NFC intended (or observed.) DEBUG changes: convert DEBUG TRACE() calls to new format. ALso, cause exec failures to always cause the shell to exit with status 126 or 127, whatever the cause. 127 is intended for lookup failures (and is used that way), 126 is used for anything else that goes wrong (as in several other shells.) We no longer use 2 (more easily confused with an exit status of the command exec'd) for shell exec failures. DEBUG only changes. Convert the TRACE() calls in the remaining files that still used it to the new format. NFC. Fix a reference after free (and consequent nonsense diagnostic for attempts to set readonly variables) I added in 1.60 by incompletely copying the FreeBSD fix for the lost memory issue. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre Exp $"); a880 67 #undef MAX /* in case we inherited them from somewhere */ #undef MIN #define MIN(a,b) (/*CONSTCOND*/((a)<=(b)) ? (a) : (b)) #define MAX(a,b) (/*CONSTCOND*/((a)>=(b)) ? (a) : (b)) /* now make the compiler work for us... */ #define MIN_REDIR MIN(MIN(MIN(MIN(NTO,NFROM), MIN(NTOFD,NFROMFD)), \ MIN(MIN(NCLOBBER,NAPPEND), MIN(NHERE,NXHERE))), NFROMTO) #define MAX_REDIR MAX(MAX(MAX(MAX(NTO,NFROM), MAX(NTOFD,NFROMFD)), \ MAX(MAX(NCLOBBER,NAPPEND), MAX(NHERE,NXHERE))), NFROMTO) static const char *redir_sym[MAX_REDIR - MIN_REDIR + 1] = { [NTO - MIN_REDIR]= ">", [NFROM - MIN_REDIR]= "<", [NTOFD - MIN_REDIR]= ">&", [NFROMFD - MIN_REDIR]= "<&", [NCLOBBER - MIN_REDIR]= ">|", [NAPPEND - MIN_REDIR]= ">>", [NHERE - MIN_REDIR]= "<<", [NXHERE - MIN_REDIR]= "<<", [NFROMTO - MIN_REDIR]= "<>", }; int outredir(struct output *out, union node *n, int sep) { if (n == NULL) return 0; if (n->type < MIN_REDIR || n->type > MAX_REDIR || redir_sym[n->type - MIN_REDIR] == NULL) return 0; if (sep) outc(sep, out); /* * ugly, but all redir node types have "fd" in same slot... * (and code other places assumes it as well) */ if ((redir_sym[n->type - MIN_REDIR][0] == '<' && n->nfile.fd != 0) || (redir_sym[n->type - MIN_REDIR][0] == '>' && n->nfile.fd != 1)) outfmt(out, "%d", n->nfile.fd); outstr(redir_sym[n->type - MIN_REDIR], out); switch (n->type) { case NHERE: outstr("'...'", out); break; case NXHERE: outstr("...", out); break; case NTOFD: case NFROMFD: if (n->ndup.dupfd < 0) outc('-', out); else outfmt(out, "%d", n->ndup.dupfd); break; default: outstr(n->nfile.expfname, out); break; } return 1; } @ 1.57.2.2 log @Pull up following revision(s) (requested by kre in ticket #1125): bin/sh/redir.c: revision 1.61 Fix the <> redirection operator, which has been broken since it was first implemented in response to PR bin/4966 (PR Feb 1998, fix Feb 1999). The file named should not be truncated. No other shell truncates the file (<> was added to FreeBSD sh in Oct 2000, and did not include O_TRUNC) and POSIX certainly does not suggest that should happen (just that the file is to be created if it does not exist.) Bug pointed out in off-list e-mail by Martijn Dekker @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.57.2.1 2017/07/23 14:58:14 snj Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.57.2.1 2017/07/23 14:58:14 snj Exp $"); d286 1 a286 1 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) @ 1.56 log @ Allow abbreviations of option names for the "fdflags -s" command. While documenting that, cleanup markup of the fdflags section of the man page. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.55 2017/05/14 17:27:05 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.55 2017/05/14 17:27:05 kre Exp $"); d521 1 a521 1 static int last_start = 6; d633 9 @ 1.55 log @ When opening a file descritor with "exec n>/file" (and similar) only set the close-on-exec bit when not in posix mode (to comply with posix...) OK: christos@@ @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.54 2017/04/29 15:14:28 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.54 2017/04/29 15:14:28 kre Exp $"); d656 1 d660 1 a660 1 { "append", O_APPEND }, d663 1 a663 1 { "async", O_ASYNC }, d666 1 a666 1 { "sync", O_SYNC }, d669 1 a669 1 { "nonblock", O_NONBLOCK }, d672 1 a672 1 { "fsync", O_FSYNC }, d675 1 a675 1 { "dsync", O_DSYNC }, d678 1 a678 1 { "rsync", O_RSYNC }, d681 1 a681 1 { "altio", O_ALT_IO }, d684 1 a684 1 { "direct", O_DIRECT }, d687 1 a687 1 { "nosigpipe", O_NOSIGPIPE }, d690 1 a690 1 { "cloexec", O_CLOEXEC }, d692 1 a692 1 { 0, 0 } d756 1 d774 1 d776 1 a776 1 if (strcmp(s, fn->name) == 0) { a851 5 /* * XXX we should only ever operate on user defined fds * XXX not on sh internal fds that might be open. * XXX but for that we need to know their range (later) */ @ 1.54 log @ Keep track of which file descriptors the shell is using for its own purposes, and move them elsewhere whenever a user redirection happens to pick the same number. With this we can move the shell file descriptors back to lower values (be slightly kinder to the kernel) since we can no longer clash. (Also get rid of a little old unneeded code.) This also completes the fdflags command, which no longer permits access to (by way or either obtaining, or changing) the shell's internal fds. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.53 2017/04/22 16:02:39 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.53 2017/04/22 16:02:39 kre Exp $"); d341 1 a341 1 cloexec = fd > 2 && (flags & REDIR_KEEP) == 0; @ 1.53 log @ Keep track of the biggest fd used by, or available to, the user/script and use that to control which fd's are examined by a (bare) fdflags (with no fd args). Usually this will mean that fdflags will no longer show the shell's internal use fds, only user fds. This is only a partial fix however, a user can easily discover the shell's fd usage (eg: using fstat) and can then still use fdflags to manipulate those fds (or even send output to them). The shell needs to monitor its own fd usage better, and keep out of the way of user fds - coming sometime later... @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.52 2017/04/22 15:54:53 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.52 2017/04/22 15:54:53 kre Exp $"); d118 12 d206 1 d521 1 d523 4 a526 1 for (i = (1 << 10); i >= 10; i >>= 1) { d534 1 a534 3 if ((i - fd) > 100) fd = i - 100; else if (fd < 10) d561 1 a561 1 if (errno != EMFILE) d575 79 d701 7 @ 1.53.2.1 log @Sync with HEAD - tag prg-localcount2-base1 @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.54 2017/04/29 15:14:28 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.54 2017/04/29 15:14:28 kre Exp $"); a117 12 struct shell_fds { /* keep track of internal shell fds */ struct shell_fds *nxt; void (*cb)(int, int); int fd; }; STATIC struct shell_fds *sh_fd_list; STATIC void renumber_sh_fd(struct shell_fds *); STATIC struct shell_fds *sh_fd(int); a193 1 renumber_sh_fd(sh_fd(fd)); a507 1 static int last_start = 6; d509 1 a509 4 if (last_start < 10) last_start++; for (i = (1 << last_start); i >= 10; i >>= 1) { d517 3 a519 1 if (fd < 10) d546 1 a546 1 if (errno != EMFILE && errno != EINVAL) a559 79 void register_sh_fd(int fd, void (*cb)(int, int)) { struct shell_fds *fp; fp = ckmalloc(sizeof (struct shell_fds)); if (fp != NULL) { fp->nxt = sh_fd_list; sh_fd_list = fp; fp->fd = fd; fp->cb = cb; } } void sh_close(int fd) { struct shell_fds **fpp, *fp; fpp = &sh_fd_list; while ((fp = *fpp) != NULL) { if (fp->fd == fd) { *fpp = fp->nxt; ckfree(fp); break; } fpp = &fp->nxt; } (void)close(fd); } STATIC struct shell_fds * sh_fd(int fd) { struct shell_fds *fp; for (fp = sh_fd_list; fp != NULL; fp = fp->nxt) if (fp->fd == fd) return fp; return NULL; } STATIC void renumber_sh_fd(struct shell_fds *fp) { int to; if (fp == NULL) return; #ifndef F_DUPFD_CLOEXEC #define F_DUPFD_CLOEXEC F_DUPFD #define CLOEXEC(fd) (fcntl((fd), F_SETFD, fcntl((fd),F_GETFD) | FD_CLOEXEC)) #else #define CLOEXEC(fd) #endif to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd); if (to == -1) to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd/2); if (to == -1) to = fcntl(fp->fd, F_DUPFD_CLOEXEC, fp->fd + 1); if (to == -1) to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 10); if (to == -1) to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 3); if (to == -1) error("insufficient file descriptors available"); CLOEXEC(to); if (fp->fd == to) /* impossible? */ return; (*fp->cb)(fp->fd, to); (void)close(fp->fd); fp->fd = to; } a606 7 if (sh_fd(fd) != NULL) { if (!p) return -1; error("Can't get status for fd=%d (%s)", fd, "Bad file descriptor"); /*XXX*/ } @ 1.53.2.2 log @Resolve conflicts from previous merge (all resulting from $NetBSD keywork expansion) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.56 2017/05/18 13:56:58 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.56 2017/05/18 13:56:58 kre Exp $"); d341 1 a341 1 cloexec = fd > 2 && (flags & REDIR_KEEP) == 0 && !posix; a655 1 uint16_t minch; d659 1 a659 1 { "append", 2, O_APPEND }, d662 1 a662 1 { "async", 2, O_ASYNC }, d665 1 a665 1 { "sync", 2, O_SYNC }, d668 1 a668 1 { "nonblock", 3, O_NONBLOCK }, d671 1 a671 1 { "fsync", 2, O_FSYNC }, d674 1 a674 1 { "dsync", 2, O_DSYNC }, d677 1 a677 1 { "rsync", 2, O_RSYNC }, d680 1 a680 1 { "altio", 2, O_ALT_IO }, d683 1 a683 1 { "direct", 2, O_DIRECT }, d686 1 a686 1 { "nosigpipe", 3, O_NOSIGPIPE }, d689 1 a689 1 { "cloexec", 2, O_CLOEXEC }, d691 1 a691 1 { 0, 0, 0 } a754 1 size_t len; a771 1 len = strlen(s); d773 1 a773 1 if (len >= fn->minch && strncmp(s,fn->name,len) == 0) { d849 5 @ 1.52 log @ When verifying the size of the fd arg for fdflags skip leading 0's (fdflags 0000000001 should work, fdflags 10000000 should not) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.51 2017/02/03 23:16:38 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.51 2017/02/03 23:16:38 kre Exp $"); d192 2 d743 1 a743 1 int maxfd; a748 2 * XXX this has to go, maxfd might be 700 (or something) * d753 1 a753 4 maxfd = fcntl(0, F_MAXFD); if (maxfd == -1) error("Can't get max fd (%s)", strerror(errno)); for (int i = 0; i <= maxfd; i++) @ 1.51 log @ Fiddle the (new) fdflags implementation: Remove some unnecessary cuteness that limited error reporting. Permit just one -s arg to fdflags Be deterministic in the case of fdflags -s +cloexec,-cloexec 0 (and similar) - use the last specified, always. Allow: FD_0_FLAGS=$( fdflags -v 0 ) # do stuff, manipulating the flags fdflags -s "FD_0_FLAGS" 0 to save/restore flags for a fd. Correctly mask result of fcntl(fd, F_GETFD) with FD_CLOEXEC as the specs require before deciding close on exec is set. Improve portability as a tool, don't assume strtoi(), nor __arraycount() and avoid needlessly requiring recent C versions (ie: there's no need to sprinkle declarations in the middle of the code, it just makes them hard to find, and benefits nothing.) Still to do: As currently implemented, both user, and shell internal fds are reported, and can be manipulated. Allowing users to touch the shell's internal fds is bogus, and providing this easy way to allow users to discover which values they have is poor. Fixing this means getting rid of the use of fcntl(F_MAXFD) and replacing it with a shell maintained memory of what fds the user (script) has allocated. The shell's fd manipulation really still needs major work (including properly fixing bin/48875) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.50 2017/02/02 20:00:40 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.50 2017/02/02 20:00:40 christos Exp $"); d764 2 @ 1.50 log @Add fdflags builtin. Discussed with Chet and he has implemented it for bash too. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.49 2017/01/21 23:03:36 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.49 2017/01/21 23:03:36 christos Exp $"); d68 1 d558 1 a558 1 static const struct { d595 1 d615 1 a615 1 if (c) d621 1 a621 1 printone(int fd, int p, int verbose) d624 1 d629 6 a634 5 outfmt(out1, "%d:", fd); for (size_t i = 0; i < __arraycount(nv); i++) { if (f & nv[i].value) { outfmt(out1, "%s%s", verbose ? "+" : "", nv[i].name); f &= ~nv[i].value; d636 1 a636 1 outfmt(out1, "-%s", nv[i].name); d639 1 a639 1 if (f || (verbose && i != __arraycount(nv) - 1)) d642 2 d647 1 a647 1 static int d650 2 a651 1 int f = 0, *v; d656 1 a656 2 size_t i; switch (*s) { d659 1 a659 1 s++; d663 1 a663 1 s++; d666 1 a666 2 v = &f; break; d669 4 a672 3 for (i = 0; i < __arraycount(nv); i++) if (strcmp(s, nv[i].name) == 0) { *v |= nv[i].value; d675 1 a675 1 if (i == __arraycount(nv)) a677 1 return f; d684 2 d689 1 a689 1 int cloexec = -1; d701 1 a701 1 int n = f; d707 1 a707 1 printone(fd, 1, verbose); d724 2 d731 1 a731 1 error("Usage: fdflags [-v] [-s ] [fd...]"); d737 2 a738 2 if (setflags && parseflags(setflags, &pos, &neg)) error("Need + or - before flags"); d741 2 d745 9 a753 1 int maxfd = fcntl(0, F_MAXFD); d757 1 a757 1 printone(i, 0, verbose); d762 5 a766 4 int e; int fd = (int)strtoi(num, NULL, 0, 0, INT_MAX, &e); if (e) error("Can't parse `%s' (%s)", num, strerror(e)); d770 1 a770 1 printone(fd, 1, verbose); @ 1.49 log @Don't let set cloexec for "exec n>&1" like ksh does (but not bash). Unit tests pass... If we don't like that, we should add some unittests that fail. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.48 2017/01/10 20:43:08 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.48 2017/01/10 20:43:08 christos Exp $"); d59 1 d556 198 @ 1.48 log @add missing @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.47 2016/05/12 13:31:37 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.47 2016/05/12 13:31:37 kre Exp $"); d309 1 a309 1 (flags&(REDIR_PUSH|REDIR_KEEP)) == REDIR_PUSH) < 0) @ 1.48.2.1 log @Sync with HEAD @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.51 2017/02/03 23:16:38 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.51 2017/02/03 23:16:38 kre Exp $"); a58 1 #include "builtins.h" a66 1 #include "mystring.h" d309 1 a309 1 (flags & REDIR_KEEP) == 0) < 0) a554 217 static const struct flgnames { const char *name; uint32_t value; } nv[] = { #ifdef O_APPEND { "append", O_APPEND }, #endif #ifdef O_ASYNC { "async", O_ASYNC }, #endif #ifdef O_SYNC { "sync", O_SYNC }, #endif #ifdef O_NONBLOCK { "nonblock", O_NONBLOCK }, #endif #ifdef O_FSYNC { "fsync", O_FSYNC }, #endif #ifdef O_DSYNC { "dsync", O_DSYNC }, #endif #ifdef O_RSYNC { "rsync", O_RSYNC }, #endif #ifdef O_ALTIO { "altio", O_ALT_IO }, #endif #ifdef O_DIRECT { "direct", O_DIRECT }, #endif #ifdef O_NOSIGPIPE { "nosigpipe", O_NOSIGPIPE }, #endif #ifdef O_CLOEXEC { "cloexec", O_CLOEXEC }, #endif { 0, 0 } }; #define ALLFLAGS (O_APPEND|O_ASYNC|O_SYNC|O_NONBLOCK|O_DSYNC|O_RSYNC|\ O_ALT_IO|O_DIRECT|O_NOSIGPIPE|O_CLOEXEC) static int getflags(int fd, int p) { int c, f; if ((c = fcntl(fd, F_GETFD)) == -1) { if (!p) return -1; error("Can't get status for fd=%d (%s)", fd, strerror(errno)); } if ((f = fcntl(fd, F_GETFL)) == -1) { if (!p) return -1; error("Can't get flags for fd=%d (%s)", fd, strerror(errno)); } if (c & FD_CLOEXEC) f |= O_CLOEXEC; return f & ALLFLAGS; } static void printone(int fd, int p, int verbose, int pfd) { int f = getflags(fd, p); const struct flgnames *fn; if (f == -1) return; if (pfd) outfmt(out1, "%d: ", fd); for (fn = nv; fn->name; fn++) { if (f & fn->value) { outfmt(out1, "%s%s", verbose ? "+" : "", fn->name); f &= ~fn->value; } else if (verbose) outfmt(out1, "-%s", fn->name); else continue; if (f || (verbose && fn[1].name)) outfmt(out1, ","); } if (verbose && f) /* f should be normally be 0 */ outfmt(out1, " +%#x", f); outfmt(out1, "\n"); } static void parseflags(char *s, int *p, int *n) { int *v, *w; const struct flgnames *fn; *p = 0; *n = 0; for (s = strtok(s, ","); s; s = strtok(NULL, ",")) { switch (*s++) { case '+': v = p; w = n; break; case '-': v = n; w = p; break; default: error("Missing +/- indicator before flag %s", s-1); } for (fn = nv; fn->name; fn++) if (strcmp(s, fn->name) == 0) { *v |= fn->value; *w &=~ fn->value; break; } if (fn->name == 0) error("Bad flag `%s'", s); } } static void setone(int fd, int pos, int neg, int verbose) { int f = getflags(fd, 1); int n, cloexec; if (f == -1) return; cloexec = -1; if ((pos & O_CLOEXEC) && !(f & O_CLOEXEC)) cloexec = FD_CLOEXEC; if ((neg & O_CLOEXEC) && (f & O_CLOEXEC)) cloexec = 0; if (cloexec != -1 && fcntl(fd, F_SETFD, cloexec) == -1) error("Can't set status for fd=%d (%s)", fd, strerror(errno)); pos &= ~O_CLOEXEC; neg &= ~O_CLOEXEC; f &= ~O_CLOEXEC; n = f; n |= pos; n &= ~neg; if (n != f && fcntl(fd, F_SETFL, n) == -1) error("Can't set flags for fd=%d (%s)", fd, strerror(errno)); if (verbose) printone(fd, 1, verbose, 1); } int fdflagscmd(int argc, char *argv[]) { char *num; int verbose = 0, ch, pos = 0, neg = 0; char *setflags = NULL; optreset = 1; optind = 1; /* initialize getopt */ while ((ch = getopt(argc, argv, ":vs:")) != -1) switch ((char)ch) { case 'v': verbose = 1; break; case 's': if (setflags) goto msg; setflags = optarg; break; case '?': default: msg: error("Usage: fdflags [-v] [-s fd] [fd...]"); /* NOTREACHED */ } argc -= optind, argv += optind; if (setflags) parseflags(setflags, &pos, &neg); if (argc == 0) { int maxfd; if (setflags) goto msg; /* * XXX this has to go, maxfd might be 700 (or something) * * XXX we should only ever operate on user defined fds * XXX not on sh internal fds that might be open. * XXX but for that we need to know their range (later) */ maxfd = fcntl(0, F_MAXFD); if (maxfd == -1) error("Can't get max fd (%s)", strerror(errno)); for (int i = 0; i <= maxfd; i++) printone(i, 0, verbose, 1); return 0; } while ((num = *argv++) != NULL) { int fd = number(num); if (strlen(num) > 5) error("%s too big to be a file descriptor", num); if (setflags) setone(fd, pos, neg, verbose); else printone(fd, 1, verbose, argc > 1); } return 0; } @ 1.47 log @ More work on file descriptors... This is the copyfd() cleanup. copyfd() duplicates file descriptors - it used to be widely used, but these days has seen its popularity dwindle. Strip it of an option that ceased to be variable (simplifying code) and cause all its users to check its result, so it does not need to handle errors itself (simplifying code further), and make it become a private inernal routine in redir.c (all callers from other places have switched to a more modern interface.) Make sure we error() if N>&N fails (if N is closed.) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.46 2016/05/09 20:50:08 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.46 2016/05/09 20:50:08 kre Exp $"); d46 1 @ 1.47.2.1 log @Sync with HEAD @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.51 2017/02/03 23:16:38 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.51 2017/02/03 23:16:38 kre Exp $"); a45 1 #include a57 1 #include "builtins.h" a65 1 #include "mystring.h" d308 1 a308 1 (flags & REDIR_KEEP) == 0) < 0) a553 217 static const struct flgnames { const char *name; uint32_t value; } nv[] = { #ifdef O_APPEND { "append", O_APPEND }, #endif #ifdef O_ASYNC { "async", O_ASYNC }, #endif #ifdef O_SYNC { "sync", O_SYNC }, #endif #ifdef O_NONBLOCK { "nonblock", O_NONBLOCK }, #endif #ifdef O_FSYNC { "fsync", O_FSYNC }, #endif #ifdef O_DSYNC { "dsync", O_DSYNC }, #endif #ifdef O_RSYNC { "rsync", O_RSYNC }, #endif #ifdef O_ALTIO { "altio", O_ALT_IO }, #endif #ifdef O_DIRECT { "direct", O_DIRECT }, #endif #ifdef O_NOSIGPIPE { "nosigpipe", O_NOSIGPIPE }, #endif #ifdef O_CLOEXEC { "cloexec", O_CLOEXEC }, #endif { 0, 0 } }; #define ALLFLAGS (O_APPEND|O_ASYNC|O_SYNC|O_NONBLOCK|O_DSYNC|O_RSYNC|\ O_ALT_IO|O_DIRECT|O_NOSIGPIPE|O_CLOEXEC) static int getflags(int fd, int p) { int c, f; if ((c = fcntl(fd, F_GETFD)) == -1) { if (!p) return -1; error("Can't get status for fd=%d (%s)", fd, strerror(errno)); } if ((f = fcntl(fd, F_GETFL)) == -1) { if (!p) return -1; error("Can't get flags for fd=%d (%s)", fd, strerror(errno)); } if (c & FD_CLOEXEC) f |= O_CLOEXEC; return f & ALLFLAGS; } static void printone(int fd, int p, int verbose, int pfd) { int f = getflags(fd, p); const struct flgnames *fn; if (f == -1) return; if (pfd) outfmt(out1, "%d: ", fd); for (fn = nv; fn->name; fn++) { if (f & fn->value) { outfmt(out1, "%s%s", verbose ? "+" : "", fn->name); f &= ~fn->value; } else if (verbose) outfmt(out1, "-%s", fn->name); else continue; if (f || (verbose && fn[1].name)) outfmt(out1, ","); } if (verbose && f) /* f should be normally be 0 */ outfmt(out1, " +%#x", f); outfmt(out1, "\n"); } static void parseflags(char *s, int *p, int *n) { int *v, *w; const struct flgnames *fn; *p = 0; *n = 0; for (s = strtok(s, ","); s; s = strtok(NULL, ",")) { switch (*s++) { case '+': v = p; w = n; break; case '-': v = n; w = p; break; default: error("Missing +/- indicator before flag %s", s-1); } for (fn = nv; fn->name; fn++) if (strcmp(s, fn->name) == 0) { *v |= fn->value; *w &=~ fn->value; break; } if (fn->name == 0) error("Bad flag `%s'", s); } } static void setone(int fd, int pos, int neg, int verbose) { int f = getflags(fd, 1); int n, cloexec; if (f == -1) return; cloexec = -1; if ((pos & O_CLOEXEC) && !(f & O_CLOEXEC)) cloexec = FD_CLOEXEC; if ((neg & O_CLOEXEC) && (f & O_CLOEXEC)) cloexec = 0; if (cloexec != -1 && fcntl(fd, F_SETFD, cloexec) == -1) error("Can't set status for fd=%d (%s)", fd, strerror(errno)); pos &= ~O_CLOEXEC; neg &= ~O_CLOEXEC; f &= ~O_CLOEXEC; n = f; n |= pos; n &= ~neg; if (n != f && fcntl(fd, F_SETFL, n) == -1) error("Can't set flags for fd=%d (%s)", fd, strerror(errno)); if (verbose) printone(fd, 1, verbose, 1); } int fdflagscmd(int argc, char *argv[]) { char *num; int verbose = 0, ch, pos = 0, neg = 0; char *setflags = NULL; optreset = 1; optind = 1; /* initialize getopt */ while ((ch = getopt(argc, argv, ":vs:")) != -1) switch ((char)ch) { case 'v': verbose = 1; break; case 's': if (setflags) goto msg; setflags = optarg; break; case '?': default: msg: error("Usage: fdflags [-v] [-s fd] [fd...]"); /* NOTREACHED */ } argc -= optind, argv += optind; if (setflags) parseflags(setflags, &pos, &neg); if (argc == 0) { int maxfd; if (setflags) goto msg; /* * XXX this has to go, maxfd might be 700 (or something) * * XXX we should only ever operate on user defined fds * XXX not on sh internal fds that might be open. * XXX but for that we need to know their range (later) */ maxfd = fcntl(0, F_MAXFD); if (maxfd == -1) error("Can't get max fd (%s)", strerror(errno)); for (int i = 0; i <= maxfd; i++) printone(i, 0, verbose, 1); return 0; } while ((num = *argv++) != NULL) { int fd = number(num); if (strlen(num) > 5) error("%s too big to be a file descriptor", num); if (setflags) setone(fd, pos, neg, verbose); else printone(fd, 1, verbose, argc > 1); } return 0; } @ 1.47.2.2 log @Sync with HEAD @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.53 2017/04/22 16:02:39 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.53 2017/04/22 16:02:39 kre Exp $"); a191 2 if (fd > max_user_fd) max_user_fd = fd; d741 1 a741 1 int i; d747 2 d753 4 a756 1 for (i = 0; i <= max_user_fd; i++) a763 2 while (num[0] == '0' && num[1] != '\0') /* skip 0's */ num++; @ 1.46 log @ Finish the fd reassignment fixes from 1.43 and 1.45 ... if we are moving a fd to an unspecified high fd number, we certainly do not want to hand that high fd off to other processes after an exec, so always set close-on-exec on the result (even if lack of fd's means no fd alteration happens.) This will (eventually) allow some other code that sets close-on-exec to be removed, but for now, doing it twice won't hurt. Also, in a N>&M type redirection, do not set close-on-exec if we don't want it. OK christos@@ @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.45 2016/05/08 20:14:27 kre Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.45 2016/05/08 20:14:27 kre Exp $"); d112 1 d192 3 a194 1 fcntl(fd, F_SETFD, 0); /* make sure it stays open */ d307 4 a310 3 else copyfd(redir->ndup.dupfd, fd, 1, (flags & (REDIR_PUSH|REDIR_KEEP)) == REDIR_PUSH); d312 1 a312 1 close(fd); d325 7 a331 1 copyfd(f, fd, 1, cloexec); d454 3 a456 3 * Copy a file descriptor to be >= to. Returns -1 * if the source file descriptor is closed, EMPTY if there are no unused * file descriptors left. d459 2 a460 2 int copyfd(int from, int to, int equal, int cloexec) d464 5 a468 17 if (cloexec && to > 2) { if (equal) newfd = dup3(from, to, O_CLOEXEC); else newfd = fcntl(from, F_DUPFD_CLOEXEC, to); } else { if (equal) newfd = dup2(from, to); else newfd = fcntl(from, F_DUPFD, to); } if (newfd < 0) { if (errno == EMFILE) return EMPTY; else error("%d: %s", from, strerror(errno)); } d472 9 d488 6 a493 2 if (copyfd(from, to, 1, 0) != to) error("Unable to make fd %d", to); d520 7 d547 1 a547 1 * If we wamted to move this fd to some random high number @ 1.45 log @ PR bin/51123 - make >&- work in all cases, and while doing that fix things so that >/dev/stdout ndup.dupfd, fd, 1, (flags & REDIR_PUSH) != 0); d517 1 a517 1 i = fcntl(fd, F_DUPFD, big_sh_fd); d528 6 @ 1.44 log @ Whitespace fixes. No functional change. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.43 2016/05/02 01:46:31 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.43 2016/05/02 01:46:31 christos Exp $"); a221 2 } else { close(fd); d307 2 a308 1 } @ 1.43 log @Fix handing of user file descriptors outside the 0..9 range. Also, move (most of) the shell's internal use fd's to much higher values (depending upon what ulimit -n allows) so they are less likely to clash with user supplied fd numbers. A future patch will (hopefully) avoid this problem completely by dynamically moving the shell's internal fds around as needed. (From kre@@) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.42 2016/03/13 01:22:42 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.42 2016/03/13 01:22:42 christos Exp $"); d70 1 a70 1 #define CLOSED -1 /* fd was not open before redir */ d225 2 a226 2 if (fd == 0) fd0_redirected++; d420 1 a420 1 return fd0_redirected != 0; @ 1.42 log @dedup. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.41 2016/03/13 00:52:05 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.41 2016/03/13 00:52:05 christos Exp $"); d79 7 d88 1 a88 1 short renamed[10]; d98 2 a99 2 */ int fd0_redirected = 0; d101 9 d112 32 d145 11 d182 1 a182 2 for (i = 0 ; i < 10 ; i++) sv->renamed[i] = EMPTY; d189 5 a193 2 n->ndup.dupfd == fd) continue; /* redirect from/to same file descriptor */ d195 1 a195 1 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { d197 3 a199 1 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) { d204 7 d212 3 a214 2 INTON; error("%d: %s", fd, strerror(errno)); d217 2 a218 1 } else d220 1 a220 1 sv->renamed[fd] = i; d251 2 a252 1 memory[fd] = 0; d303 2 a304 1 if (memory[redir->ndup.dupfd]) a388 1 int i; a389 11 for (i = 0 ; i < 10 ; i++) { if (rp->renamed[i] != EMPTY) { if (i == 0) fd0_redirected--; close(i); if (rp->renamed[i] >= 0) { copyfd(rp->renamed[i], i, 1, 0); close(rp->renamed[i]); } } } d391 1 d418 2 a419 1 fd0_redirected_p (void) { d431 1 a431 1 int i; d434 5 a438 7 for (i = 0 ; i < 10 ; i++) { if (rp->renamed[i] >= 0) { close(rp->renamed[i]); } if (!vforked) rp->renamed[i] = EMPTY; } d474 57 @ 1.41 log @Test for REDIR_KEEP in the non-copy case: $ cat other1 #!/bin/sh ./other2 3>out $ cat other2 #!/bin/sh echo other2 1>&3 $ ./other1 @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.40 2016/03/12 21:35:13 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.40 2016/03/12 21:35:13 christos Exp $"); d171 1 a171 1 int eflags; d246 1 d248 1 a248 1 copyfd(f, fd, 1, fd > 2 && (flags & REDIR_KEEP) == 0); d250 1 a250 1 } else if (f > 2 && (flags & REDIR_KEEP) == 0) @ 1.40 log @Don't close-on-exec redirections created explicitly for the command being ran; i.e. we want this to work: $ cat succ1 #!/bin/sh ./succ2 6>out $ cat succ2 #!/bin/sh echo succ2 >&6 $ ./succ1 And this to fail: $ cat fail1 #!/bin/sh exec 6> out echo "fail1" >&6 ./fail2 exec 6>&- $ cat fail2 #!obj.amd64/sh echo "fail2" >&6 $ ./fail1 ./fail2: 6: Bad file descriptor XXX: Do we want a -k (keep flag on exec to make redirections not close-on-exec? @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.39 2016/01/04 13:57:15 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.39 2016/01/04 13:57:15 christos Exp $"); d249 1 a249 1 } else if (f > 2) @ 1.39 log @PR/50619: Fix reversed test. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.38 2016/01/04 03:00:24 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.38 2016/01/04 03:00:24 christos Exp $"); d247 1 a247 1 copyfd(f, fd, 1, fd > 2); @ 1.38 log @Don't leak redirected rescriptors to exec'ed processes. This is what ksh does, but bash does not. For example: $ cat test1 #!/bin/sh exec 6> out echo "test" >&6 sh ./test2 exec 6>&- $ cat test2 echo "test2" >&6 $ ./test1 ./test2: 6: Bad file descriptor This fixes by side effect the problem of the rc system leaking file descriptors 7 and 8 to all starting daemons: $ fstat -p 1359 USER CMD PID FD MOUNT INUM MODE SZ|DV R/W root powerd 1359 wd / 2 drwxr-xr-x 512 r root powerd 1359 0 / 63029 crw-rw-rw- null rw root powerd 1359 1 / 63029 crw-rw-rw- null rw root powerd 1359 2 / 63029 crw-rw-rw- null rw root powerd 1359 3* kqueue pending 0 root powerd 1359 4 / 64463 crw-r----- power r root powerd 1359 7 flags 0x80034 root powerd 1359 8 flags 0x80034 root powerd 1359 9* pipe 0xfffffe815d7bfdc0 -> 0x0 w Note fd=7,8 pointing to the revoked pty from the parent rc process. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.37 2014/10/23 21:03:25 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.37 2014/10/23 21:03:25 christos Exp $"); d234 1 a234 1 (flags & REDIR_PUSH) == 0); @ 1.37 log @simplify and eliminate TOCTOU. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.36 2014/10/15 14:54:25 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.36 2014/10/15 14:54:25 christos Exp $"); d233 2 a234 1 copyfd(redir->ndup.dupfd, fd, 1); d247 1 a247 1 copyfd(f, fd, 1); d249 3 a251 1 } d322 1 a322 1 copyfd(rp->renamed[i], i, 1); d388 1 a388 1 copyfd(int from, int to, int equal) d392 11 a402 4 if (equal) newfd = dup2(from, to); else newfd = fcntl(from, F_DUPFD, to); @ 1.36 log @PR/48201: Miwa Susumu: Fix set -C (no clobber) for POSIX; from FreeBSD Can't use O_EXCL because of device nodes; also truncate. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.35 2013/06/27 23:22:04 yamt Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.35 2013/06/27 23:22:04 yamt Exp $"); d200 1 a200 1 if (stat(fname, &sb) == -1) { d204 7 a210 10 } else if (!S_ISREG(sb.st_mode)) { if ((f = open(fname, O_WRONLY, 0666)) < 0) goto ecreate; if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) { close(f); errno = EEXIST; goto ecreate; } } else { @ 1.35 log @fix descriptor leaks. PR/47805 this fix was taken from FreeBSD SVN rev 199953 (Jilles Tjoelker) ------------------------------------------------------------------------ r199953 | jilles | 2009-11-30 07:33:59 +0900 (Mon, 30 Nov 2009) | 16 lines Fix some cases where file descriptors from redirections leak to programs. - Redirecting fds that were not open before kept two copies of the redirected file. sh -c '{ :; } 7>/dev/null; fstat -p $$; true' (both fd 7 and 10 remained open) - File descriptors used to restore things after redirection were not set close-on-exec, instead they were explicitly closed before executing a program normally and before executing a shell procedure. The latter must remain but the former is replaced by close-on-exec. sh -c 'exec 7/dev/null; true' (fd 10 remained open) The examples above are simpler than the testsuite because I do not want to use fstat or procstat in the testsuite. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.34 2013/06/12 01:36:52 yamt Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.34 2013/06/12 01:36:52 yamt Exp $"); d167 1 d171 1 a171 1 int oflags = O_WRONLY|O_CREAT|O_TRUNC, eflags; d198 21 a218 2 if (Cflag) oflags |= O_EXCL; d222 1 a222 1 if ((f = open(fname, oflags, 0666)) < 0) @ 1.34 log @constify @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.33 2012/03/20 18:42:29 matt Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.33 2012/03/20 18:42:29 matt Exp $"); d70 1 a112 1 int try; a129 1 try = 0; a135 1 again: d139 2 a140 6 if (!try) { openredirect(n, memory, flags); try++; goto again; } /* FALLTHROUGH*/ d146 3 a148 5 } if (!try) { sv->renamed[fd] = i; close(fd); } d155 1 a155 2 if (!try) openredirect(n, memory, flags); @ 1.33 log @Use C89 function definitions @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.32 2011/08/31 16:24:55 plunky Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.32 2011/08/31 16:24:55 plunky Exp $"); d94 1 a94 1 STATIC int openhere(union node *); d259 1 a259 1 openhere(union node *redir) @ 1.33.2.1 log @resync from head @ text @d1 1 a1 1 /* $NetBSD$ */ d40 1 a40 1 __RCSID("$NetBSD$"); d94 1 a94 1 STATIC int openhere(const union node *); d259 1 a259 1 openhere(const union node *redir) @ 1.33.2.2 log @Rebase to HEAD as of a few days ago. @ text @a69 1 #define CLOSED -1 /* fd was not open before redir */ d112 1 d130 1 d137 1 d141 6 a146 2 i = CLOSED; break; d152 5 a156 3 } else (void)fcntl(i, F_SETFD, FD_CLOEXEC); sv->renamed[fd] = i; d163 2 a164 1 openredirect(n, memory, flags); @ 1.32 log @NULL does not need a cast @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.31 2011/02/17 15:13:49 pooka Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.31 2011/02/17 15:13:49 pooka Exp $"); d343 1 a343 1 fd0_redirected_p () { d352 1 a352 2 clearredir(vforked) int vforked; @ 1.32.2.1 log @sync with head @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.32 2011/08/31 16:24:55 plunky Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.32 2011/08/31 16:24:55 plunky Exp $"); d343 1 a343 1 fd0_redirected_p (void) { d352 2 a353 1 clearredir(int vforked) @ 1.32.2.2 log @constify @ text @d1 1 a1 1 /* $NetBSD$ */ d40 1 a40 1 __RCSID("$NetBSD$"); d94 1 a94 1 STATIC int openhere(const union node *); d259 1 a259 1 openhere(const union node *redir) @ 1.32.2.3 log @revert the previous (wrong branch) @ text @d94 1 a94 1 STATIC int openhere(union node *); d259 1 a259 1 openhere(union node *redir) @ 1.32.2.4 log @sync with head. for a reference, the tree before this commit was tagged as yamt-pagecache-tag8. this commit was splitted into small chunks to avoid a limitation of cvs. ("Protocol error: too many arguments") @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.32.2.3 2013/06/12 01:36:09 yamt Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.32.2.3 2013/06/12 01:36:09 yamt Exp $"); a69 1 #define CLOSED -1 /* fd was not open before redir */ d94 1 a94 1 STATIC int openhere(const union node *); d112 1 d130 1 d137 1 d141 6 a146 2 i = CLOSED; break; d152 5 a156 3 } else (void)fcntl(i, F_SETFD, FD_CLOEXEC); sv->renamed[fd] = i; d163 2 a164 1 openredirect(n, memory, flags); d259 1 a259 1 openhere(const union node *redir) @ 1.31 log @Tell copyfd if the caller wants the exact tofd to just fd >= tofd. Fixes "echo foo > /rump/bar" in a rump hijacked shell. reviewed by christos @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.30 2008/01/21 06:43:03 msaitoh Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.30 2008/01/21 06:43:03 msaitoh Exp $"); d273 1 a273 1 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { @ 1.30 log @Conform to XCU Section 2.8.2 (Exit Status for Commands) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $"); d225 1 a225 1 copyfd(redir->ndup.dupfd, fd); d238 1 a238 1 copyfd(f, fd); d311 1 a311 1 copyfd(rp->renamed[i], i); d378 1 a378 1 copyfd(int from, int to) d382 4 a385 1 newfd = fcntl(from, F_DUPFD, to); @ 1.30.22.1 log @Sync with HEAD @ text @d1 1 a1 1 /* $NetBSD$ */ d40 1 a40 1 __RCSID("$NetBSD$"); d225 1 a225 1 copyfd(redir->ndup.dupfd, fd, 1); d238 1 a238 1 copyfd(f, fd, 1); d311 1 a311 1 copyfd(rp->renamed[i], i, 1); d378 1 a378 1 copyfd(int from, int to, int equal) d382 1 a382 4 if (equal) newfd = dup2(from, to); else newfd = fcntl(from, F_DUPFD, to); @ 1.29 log @PR/25699: David Laight: sh(1) hangs opening a named pipe as stdin for background process This happens because we vfork, and then open a named pipe with O_RDONLY and block in the child. We avoid this, by opening the file with O_NONBLOCK, and then reset it if we are vforked. XXX: this is an ugly fix. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.28 2003/08/07 09:05:37 agc Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.28 2003/08/07 09:05:37 agc Exp $"); d244 1 d247 1 @ 1.29.10.1 log @Pull up following revision(s) (requested by msaitoh in ticket #1281): bin/sh/redir.c: revision 1.30 Conform to XCU Section 2.8.2 (Exit Status for Commands) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $"); a243 1 exerrno = 1; a245 1 exerrno = 1; @ 1.29.2.1 log @Pull up following revision(s) (requested by msaitoh in ticket #1992): bin/sh/redir.c: revision 1.30 Conform to XCU Section 2.8.2 (Exit Status for Commands) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $"); a243 1 exerrno = 1; a245 1 exerrno = 1; @ 1.29.16.1 log @sync with HEAD @ text @d1 1 a1 1 /* redir.c,v 1.29 2004/07/08 03:57:33 christos Exp */ d40 1 a40 1 __RCSID("redir.c,v 1.29 2004/07/08 03:57:33 christos Exp"); a243 1 exerrno = 1; a245 1 exerrno = 1; @ 1.28 log @Move UCB-licensed code from 4-clause to 3-clause licence. Patches provided by Joel Baker in PR 22249, verified by myself. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.27 2002/11/24 22:35:42 christos Exp $ */ d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.27 2002/11/24 22:35:42 christos Exp $"); d93 1 a93 1 STATIC void openredirect(union node *, char[10 ]); d142 1 a142 1 openredirect(n, memory); d164 1 a164 1 openredirect(n, memory); d174 1 a174 1 openredirect(union node *redir, char memory[10]) d179 1 a179 1 int flags = O_WRONLY|O_CREAT|O_TRUNC; d191 5 a195 1 if ((f = open(fname, O_RDONLY)) < 0) d197 2 d207 1 a207 1 flags |= O_EXCL; d211 1 a211 1 if ((f = open(fname, flags, 0666)) < 0) @ 1.28.2.1 log @Pull up revision 1.29 (requested by chs in ticket #777): PR/25699: David Laight: sh(1) hangs opening a named pipe as stdin for background process This happens because we vfork, and then open a named pipe with O_RDONLY and block in the child. We avoid this, by opening the file with O_NONBLOCK, and then reset it if we are vforked. XXX: this is an ugly fix. @ text @d1 1 a1 1 /* $NetBSD$ */ d40 1 a40 1 __RCSID("$NetBSD$"); d93 1 a93 1 STATIC void openredirect(union node *, char[10], int); d142 1 a142 1 openredirect(n, memory, flags); d164 1 a164 1 openredirect(n, memory, flags); d174 1 a174 1 openredirect(union node *redir, char memory[10], int flags) d179 1 a179 1 int oflags = O_WRONLY|O_CREAT|O_TRUNC, eflags; d191 1 a191 5 if (flags & REDIR_VFORK) eflags = O_NONBLOCK; else eflags = 0; if ((f = open(fname, O_RDONLY|eflags)) < 0) a192 2 if (eflags) (void)fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags); d201 1 a201 1 oflags |= O_EXCL; d205 1 a205 1 if ((f = open(fname, oflags, 0666)) < 0) @ 1.27 log @Fixes from David Laight: - ansification - format of output of jobs command (etc) - job identiers %+, %- etc - $? and $(...) - correct quoting of output of set, export -p and readonly -p - differentiation between nornal and 'posix special' builtins - correct behaviour (posix) for errors on builtins and special builtins - builtin printf and kill - set -o debug (if compiled with DEBUG) - cd src obj (as ksh - too useful to do without) - unset -e name, remove non-readonly variable from export list. (so I could unset -e PS1 before running the test shell...) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.26 2002/09/28 01:25:02 christos Exp $ */ d18 1 a18 5 * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors d40 1 a40 1 __RCSID("$NetBSD: redir.c,v 1.26 2002/09/28 01:25:02 christos Exp $"); @ 1.26 log @Revert previous change. No need to save rootshell. It is only affecting the non-vfork case. Having said that, it would be nice if pipelines of simple commands were vforked too. Right now they are not. Explain that setpgid() might fail because we are doing it both in the parent and the child case, because we don't know which one will come first. Suspending a pipeline prints %1 Suspended n times where n is the number of processes, but that was there before. It is easy to fix, but I'll leave the code alone for now. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.25 2002/09/27 22:56:24 christos Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.25 2002/09/27 22:56:24 christos Exp $"); d97 2 a98 2 STATIC void openredirect __P((union node *, char[10 ])); STATIC int openhere __P((union node *)); d110 2 a111 4 redirect(redir, flags) union node *redir; int flags; { d178 2 a179 4 openredirect(redir, memory) union node *redir; char memory[10]; { d255 2 a256 3 openhere(redir) union node *redir; { d296 2 a297 1 popredir() { d374 1 a374 3 copyfd(from, to) int from; int to; @ 1.25 log @Deal with rootshell not being maintained correctly in the vfork() case. Propagate isroot, throughout the eval process and maintain it properly. Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in their own process groups, because wasroot was always true in the children. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.24 2002/09/27 18:56:55 christos Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.24 2002/09/27 18:56:55 christos Exp $"); a263 1 int isroot = rootshell; d274 1 a274 1 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB, &isroot) == 0) { @ 1.24 log @VFork()ing shell: From elric@@netbsd.org: Plus my changes: - walking process group fix in foregrounding a job. - reset of process group in parent shell if interrupted before the wait. - move INTON lower in the dowait so that the job structure is consistent. - error check all setpgid(), tcsetpgrp() calls. - eliminate unneeded strpgid() call. - check that we don't belong in the process group before we try to set it. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.22.6.1 2002/03/27 20:37:41 elric Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.23 2002/05/15 16:33:35 christos Exp $"); d61 1 d264 1 d275 1 a275 1 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { @ 1.23 log @implement noclobber. From Ben Harris, with minor tweaks from me. Two unimplemented comments to go. Go Ben! @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.22 2000/05/22 10:18:47 elric Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.22 2000/05/22 10:18:47 elric Exp $"); d124 3 d335 1 a335 1 clearredir(); d351 3 a353 1 clearredir() { d362 2 a363 1 rp->renamed[i] = EMPTY; @ 1.22 log @Back out previous vfork changes. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.20 1999/02/04 16:17:39 christos Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.20 1999/02/04 16:17:39 christos Exp $"); d64 1 d183 1 d204 4 d209 1 a209 2 #ifdef O_CREAT if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) a210 4 #else if ((f = creat(fname, 0666)) < 0) goto ecreate; #endif a213 1 #ifdef O_APPEND a215 6 #else if ((f = open(fname, O_WRONLY)) < 0 && (f = creat(fname, 0666)) < 0) goto ecreate; lseek(f, (off_t)0, 2); #endif @ 1.22.6.1 log @Doing the vfork work on ash on a branch to try to shake out the problems before I expose everyone to them. This checkin represents a merge of the prior work, which I backed out a while ago, to the HEAD only and does not incorporate any additional bugfixes. The additional bugfixes and code-cleanup will occur in later checkins. For reference the patches that were used are: cvs diff -kk -r1.51 -r1.55 eval.c | patch cvs diff -kk -r1.27 -r1.28 exec.c | patch cvs diff -kk -r1.15 -r1.16 exec.h | patch cvs diff -kk -r1.32 -r1.33 input.c | patch cvs diff -kk -r1.10 -r1.11 input.h | patch cvs diff -kk -r1.32 -r1.35 jobs.c | patch cvs diff -kk -r1.9 -r1.11 jobs.h | patch cvs diff -kk -r1.36 -r1.37 main.c | patch cvs diff -kk -r1.20 -r1.21 redir.c | patch cvs diff -kk -r1.10 -r1.11 redir.h | patch cvs diff -kk -r1.10 -r1.12 shell.h | patch cvs diff -kk -r1.22 -r1.23 trap.c | patch cvs diff -kk -r1.12 -r1.13 trap.h | patch cvs diff -kk -r1.23 -r1.24 var.c | patch cvs diff -kk -r1.16 -r1.17 var.h | patch All other changes were simply the resolution of the resulting conflicts, which occured only in the merge of jobs.c. Begins to address PR: bin/5475 @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.22 2000/05/22 10:18:47 elric Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.22 2000/05/22 10:18:47 elric Exp $"); a122 3 /* We don't have to worry about REDIR_VFORK here, as * flags & REDIR_PUSH is never true if REDIR_VFORK is set. */ d338 1 a338 1 clearredir(0); d354 1 a354 3 clearredir(vforked) int vforked; { d363 1 a363 2 if (!vforked) rp->renamed[i] = EMPTY; @ 1.21 log @Now we use vfork(2) instead of fork(2) when we can. @ text @a122 3 /* We don't have to worry about REDIR_VFORK here, as * flags & REDIR_PUSH is never true if REDIR_VFORK is set. */ d338 1 a338 1 clearredir(0); d354 1 a354 3 clearredir(vforked) int vforked; { d363 1 a363 2 if (!vforked) rp->renamed[i] = EMPTY; @ 1.20 log @PR/4966: Joel Reicher: Implement <> redirections which are documented in the man page. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.19 1998/07/28 11:41:58 mycroft Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.19 1998/07/28 11:41:58 mycroft Exp $"); d123 3 d341 1 a341 1 clearredir(); d357 3 a359 1 clearredir() { d368 2 a369 1 rp->renamed[i] = EMPTY; @ 1.19 log @Be more retentive about use of NOTREACHED and noreturn. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.18 1998/07/28 05:31:28 mycroft Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.18 1998/07/28 05:31:28 mycroft Exp $"); d194 6 a199 6 error("cannot open %s: %s", fname, errmsg(errno, E_OPEN)); movefd: if (f != fd) { copyfd(f, fd); close(f); } d205 1 a205 1 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); d208 1 a208 1 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); d210 1 a210 1 goto movefd; d215 1 a215 1 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); d219 1 a219 1 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); d222 1 a222 1 goto movefd; d231 2 a232 1 break; d236 1 a236 1 goto movefd; d240 5 d246 5 @ 1.18 log @Delint. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.17 1998/07/27 17:12:45 christos Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.17 1998/07/27 17:12:45 christos Exp $"); d151 1 a151 1 break; a278 1 /* NOTREACHED */ @ 1.17 log @PR/5848: David Holland: Use PIPE_BUF instead of hardcoding 4k @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.16 1997/07/04 21:02:21 christos Exp $ */ d44 1 a44 1 __RCSID("$NetBSD: redir.c,v 1.16 1997/07/04 21:02:21 christos Exp $"); d279 1 @ 1.16 log @Fix compiler warnings. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.15 1997/04/21 12:38:25 christos Exp $ */ d44 1 a44 1 __RCSID("$NetBSD$"); d49 1 d72 5 a76 1 #define PIPESIZE 4096 /* amount of buffering in a pipe */ @ 1.15 log @PR/3452: Jerry Peek: Redirections of unopened fd to file failed. for arg in a b c do echo hi this is $arg 1>&3 done 3> foo @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.14 1997/01/11 02:04:46 tls Exp $ */ d39 1 d44 1 a44 1 static char rcsid[] = "$NetBSD: redir.c,v 1.14 1997/01/11 02:04:46 tls Exp $"; d108 1 a108 1 struct redirtab *sv; @ 1.14 log @kill 'register' @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.13 1996/10/16 15:16:04 christos Exp $ */ d43 1 a43 1 static char rcsid[] = "$NetBSD: redir.c,v 1.13 1996/10/16 15:16:04 christos Exp $"; d110 1 d125 1 d129 1 d132 17 a148 1 if ((i = copyfd(fd, 10)) != EMPTY) { a152 2 if (i == EMPTY) error("Out of file descriptors"); d158 2 a159 1 openredirect(n, memory); @ 1.13 log @PR/2808: fix redirection to the same file descriptor better error messages for failed pipes (from FreeBSD) @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.12 1995/05/11 21:30:10 christos Exp $ */ d43 1 a43 1 static char rcsid[] = "$NetBSD: redir.c,v 1.12 1995/05/11 21:30:10 christos Exp $"; d269 1 a269 1 register struct redirtab *rp = redirlist; d320 1 a320 1 register struct redirtab *rp; @ 1.12 log @Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0 bug. @ text @d1 1 a1 1 /* $NetBSD$ */ d43 1 a43 1 static char rcsid[] = "$NetBSD$"; d82 1 a82 1 /* d85 1 a85 1 * if it hasn't already been redirected. d110 1 a110 1 char memory[10]; /* file descriptors to write to memory */ d124 3 d342 1 a342 1 copyfd(from, to) d349 6 a354 2 if (newfd < 0 && errno == EMFILE) return EMPTY; @ 1.12.6.1 log @Update /bin/sh from trunk per request of Christos Zoulas. Fixes many bugs. @ text @d1 1 a1 1 /* $NetBSD: redir.c,v 1.14 1997/01/11 02:04:46 tls Exp $ */ d43 1 a43 1 static char rcsid[] = "$NetBSD: redir.c,v 1.14 1997/01/11 02:04:46 tls Exp $"; d82 1 a82 1 /* d85 1 a85 1 * if it hasn't already been redirected. d110 1 a110 1 char memory[10]; /* file descriptors to write to memory */ a123 3 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && n->ndup.dupfd == fd) continue; /* redirect from/to same file descriptor */ d266 1 a266 1 struct redirtab *rp = redirlist; d317 1 a317 1 struct redirtab *rp; d339 1 a339 1 copyfd(from, to) d346 2 a347 6 if (newfd < 0) { if (errno == EMFILE) return EMPTY; else error("%d: %s", from, strerror(errno)); } @ 1.11 log @convert to new RCS id conventions. @ text @d41 1 a41 1 static char sccsid[] = "@@(#)redir.c 8.1 (Berkeley) 5/31/93"; d47 8 a66 6 #include #include #include #include #include #include d89 2 a90 8 #ifdef __STDC__ STATIC void openredirect(union node *, char *); STATIC int openhere(union node *); #else STATIC void openredirect(); STATIC int openhere(); #endif d227 1 a227 1 int len; @ 1.10 log @pull prototypes into scope for string functions. @ text @d1 2 d40 5 a44 2 /*static char sccsid[] = "from: @@(#)redir.c 8.1 (Berkeley) 5/31/93";*/ static char *rcsid = "$Id: redir.c,v 1.9 1994/12/05 19:07:52 cgd Exp $"; @ 1.9 log @clean up further. more patches from Jim Jegers @ text @d39 1 a39 1 static char *rcsid = "$Id: redir.c,v 1.8 1994/06/11 16:12:30 mycroft Exp $"; d56 1 @ 1.8 log @Add RCS ids. @ text @d39 1 a39 1 static char *rcsid = "$Id: $"; d337 4 a340 1 copyfd(from, to) { @ 1.7 log @sync with 4.4lite @ text @d38 2 a39 1 static char sccsid[] = "@@(#)redir.c 8.1 (Berkeley) 5/31/93"; @ 1.6 log @lseek long lossage. @ text @d2 2 a3 2 * Copyright (c) 1991 The Regents of the University of California. * All rights reserved. d38 1 a38 2 /*static char sccsid[] = "from: @@(#)redir.c 5.1 (Berkeley) 3/7/91";*/ static char rcsid[] = "$Id: redir.c,v 1.5 1993/08/01 18:58:01 mycroft Exp $"; a44 1 #include d53 1 d57 1 d73 5 a77 3 /* We keep track of whether or not fd0 has been redirected. This is for background commands, where we want to redirect fd0 to /dev/null only if it hasn't already been redirected. */ d133 2 a134 2 if (fd == 0) fd0_redirected++; d190 1 a190 1 lseek(f, 0, 2); d268 2 a269 2 if (i == 0) fd0_redirected--; a282 2 d302 5 d330 1 a330 1 * Copy a file descriptor, like the F_DUPFD option of fcntl. Returns -1 a336 1 #ifdef F_DUPFD a342 27 #else char toclose[32]; int i; int newfd; int e; for (i = 0 ; i < to ; i++) toclose[i] = 0; INTOFF; while ((newfd = dup(from)) >= 0 && newfd < to) toclose[newfd] = 1; e = errno; for (i = 0 ; i < to ; i++) { if (toclose[i]) close(i); } INTON; if (newfd < 0 && e == EMFILE) return EMPTY; return newfd; #endif } /* Return true if fd 0 has already been redirected at least once. */ int fd0_redirected_p () { return fd0_redirected != 0; @ 1.5 log @Add RCS identifiers. @ text @d39 1 a39 1 static char rcsid[] = "$Id: $"; d46 1 d188 1 a188 1 lseek(f, 0L, 2); @ 1.4 log @Jim "wilson@@moria.cygnus.com" Wilson's patches to make C News (and other things) work. @ text @d38 2 a39 2 static char sccsid[] = "@@(#)redir.c 5.1 (Berkeley) 3/7/91"; static char rcsid[] = "$Header: /b/source/CVS/src/bin/sh/redir.c,v 1.3 1993/03/23 00:29:14 cgd Exp $"; @ 1.3 log @changed "Id" to "Header" for rcsids @ text @d39 1 a39 1 static char rcsid[] = "$Header: redir.c,v 1.2 93/03/22 08:12:44 cgd Exp $"; d72 4 d130 2 d265 2 d359 6 @ 1.2 log @added rcs ids to all files @ text @d39 1 a39 1 static char rcsid[] = "$Id: redir.c,v 1.2 93/03/21 22:15:55 cgd Exp $"; @ 1.1 log @Initial revision @ text @d39 1 @ 1.1.1.1 log @initial import of 386bsd-0.1 sources @ text @@ 1.1.1.2 log @44lite code @ text @d2 2 a3 2 * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. d38 1 a38 1 static char sccsid[] = "@@(#)redir.c 8.1 (Berkeley) 5/31/93"; a52 1 #include a55 1 #include a70 6 /* * We keep track of whether or not fd0 has been redirected. This is for * background commands, where we want to redirect fd0 to /dev/null only * if it hasn't already been redirected. */ int fd0_redirected = 0; a124 2 if (fd == 0) fd0_redirected++; d180 1 a180 1 lseek(f, (off_t)0, 2); a257 2 if (i == 0) fd0_redirected--; d271 2 a291 5 /* Return true if fd 0 has already been redirected at least once. */ int fd0_redirected_p () { return fd0_redirected != 0; } d315 1 a315 1 * Copy a file descriptor to be >= to. Returns -1 d322 1 d329 21 @