Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Daniel Mills Newsgroups: gnu.bash.bug Subject: Re: Reading from a file by using its FD returns its contents only once Date: Mon, 31 Dec 2018 12:23:03 -0500 Lines: 86 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: usenet.stanford.edu 1546277000 1566 208.118.235.17 (31 Dec 2018 17:23:20 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash@gnu.org To: mike b Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=TZijPxtvN14zaz0zn51LFqpYt+LvTdmmPf4o1xEj5h4=; b=iX0WoBx7H4tVZzXHRsvuqoSNkix6pS9fd3XflxyoY3qIq/zTi9kYdKgABevXiqb+oC 20PvsSdev5YD6CSDi7sjk18GoS08g8xWBbp/KK/KwYnWEl8hnkT0F39st836afFejkzJ 6DNtGrw4g8K4t4xNFMOMpzdgPz/1bayRcTD7/9xVbswsSst9NCmhrVwGuITMgIjp/ZfH QsIZTJjgSE0757S0JLvEee2aO8F4bWTYTpsdJiFkJqaxNz7S3BVbR5y/l88uetaDZKUm nzj5nuPPhwd/D9R0gLexVzDVLtfildRtBu23EVmK2udJbMOjcJ0VXZc6qp0HspEewGfX iSsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=TZijPxtvN14zaz0zn51LFqpYt+LvTdmmPf4o1xEj5h4=; b=HLfOlGGx9YV8vwZcAphi20SxNnvnVIyTY0eFBQSFAebWq76aQXzBaBN8/USh7gD735 o7xaqRs+BPTchUbfSTj2xtlsnQ3C5+FeQTspGKc5OsUqDDuC5aPtucfhfUw4wIuMArza C3sN29bhXwaYMGVABKDs2PagG8evZ0d3HZrsLkCkxjxKDfx/EbdeSoddTz1NxPGrNQpG xevXd71X02quRrG1/UFgEuxtklOakcvsckW/15EvBHR0qRmdDhtk1FuHglDwak+el28E 8ceBrxirE2JBs4byuW0yWHtG+aOQHNOAci7QO9Lp6wirmDPqAYloe/EmUvne4p1PNoxu /R+A== X-Gm-Message-State: AJcUukdajeHFgFOw3hrmqnNiX9wrNDgCh4GkF6diuN+y9+dL9xa834uR HiY8+gG6jQajUBJ9bldbtBcGLaANmb0aSVwf484= X-Google-Smtp-Source: ALg8bN4rkvZeDczf+njLxgFjP1u0chZjqT12jC3XkbdusqaiKaGaYrUxKtdRc15JM9qBk6K2Wzn33/AD4ucD4j/SWWM= X-Received: by 2002:a6b:d618:: with SMTP id w24mr28220694ioa.24.1546276996201; Mon, 31 Dec 2018 09:23:16 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::d42 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:15018 On Sun, Dec 30, 2018 at 8:37 PM mike b wrote: > Configuration Information: > Machine: x86_64 > OS: linux-gnu > Compiler: gcc > Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' > -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' > -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL > -DHAVE_CONFIG_H -I. -I../. -I.././include -I.././lib -Wdate-time > -D_FORTIFY_SOURCE=2 -g -O2 -fdebug-prefix-map=/build/bash-7fckc0/bash-4.4=. > -fstack-protector-strong -Wformat -Werror=format-security -Wall -no-pie > -Wno-parentheses -Wno-format-security > uname output: Linux debian9 4.9.0-8-amd64 #1 SMP Debian 4.9.130-2 > (2018-10-27) x86_64 GNU/Linux > Machine Type: x86_64-pc-linux-gnu > > Bash Version: 4.4 > Patch Level: 12 > Release Status: release > > I am not quite sure if this is a bug, but here's what I find as a bit odd > behavior: > > # modprobe zram num_devices=0 > # exec {add} # read -r id <&"$add"; echo "$id" > 0 > # read -r id <&"$add"; echo "$id" # <- $id ends up empty, no data is read > > # read -r id 1 > # read -r id 2 > # readlink -f "/proc/$$/fd/$add" > /sys/class/zram-control/hot_add > > The above sysfs interface is used for creating a zram device by performing > a read on the hot_add file. The value that should be returned is the id of > the newly created device. In first instance file is opened by dynamically > allocating the fd to use "$add" (the fd) across reads instead of > referencing the file directly. But from the above example you can see that > $id is assigned an actual value only on first read. On every next one, $id > would become empty - when using $add, that is. However, when file is read > in a standard way, by using it directly, everything works as it should. > > The above is just an example. Doing reads on any other regular file like > this yields same behavior: > # echo bla >./t > # exec 10<./t > # read -r <&10 > # echo $REPLY > bla > # read -r <&10 > # echo $REPLY > > # > Playing with something like: > # zram=/sys/class/zram/control/hot_add > # c=0; while ((++c <= 3)); do read -r; echo "${REPLY:-NULL}"; done > <"$zram" > 0 > NULL > NULL > # > also gets same results. In contrast to: > # c=0; while (( ++c <= 3 )); do read -r; echo "out: ${REPLY:-NULL}"; done > foo > out: foo > foo > out: foo > foo > out: foo > # > which keeps reading from default stdin (terminal in this case) without any > hiccups (as expected). > > So, considering all the above, any hints on why subsequent reads in these > scenarios don't get any data from a file would be really appreciated. :) > > Same behavior is seen in 4.3.30 and 5.0 versions. > Redirections treat all files as streams, it won't seek back to the beginning once you hit the end of the file. You would need to close and re-open it