Discussion:
getline in /proc
(too old to reply)
Simon Vaillancourt
2005-09-16 18:15:18 UTC
Permalink
Hello,
I'm not sure if this is a gawk(using version 3.1.1) bug or a procfs
issue but but when doing a getline on a file that doesn't exist in
/proc, it loops forever, for example :

echo test | gawk 'BEGIN { while (getline <
"/proc/anypid/unexistingfile") {print "line="$0}}'

I stumbled on this while writing a process analysis tool, when a process
for which I am readins properties in /proc suddently dies, my script
goes crazy. I can work around this problem by copying the files in /tmp
before reading them.

Thanks

Simon
Simon Vaillancourt
2005-09-16 19:11:37 UTC
Permalink
Just wanted to add that version 3.1.5 also has that problem.
Post by Simon Vaillancourt
Hello,
I'm not sure if this is a gawk(using version 3.1.1) bug or a procfs
issue but but when doing a getline on a file that doesn't exist in
echo test | gawk 'BEGIN { while (getline <
"/proc/anypid/unexistingfile") {print "line="$0}}'
I stumbled on this while writing a process analysis tool, when a
process for which I am readins properties in /proc suddently dies, my
script goes crazy. I can work around this problem by copying the files
in /tmp before reading them.
Thanks
Simon
Andrew J. Schorr
2005-09-17 16:05:19 UTC
Permalink
Post by Simon Vaillancourt
Hello,
I'm not sure if this is a gawk(using version 3.1.1) bug or a procfs
issue but but when doing a getline on a file that doesn't exist in
echo test | gawk 'BEGIN { while (getline <
"/proc/anypid/unexistingfile") {print "line="$0}}'
I stumbled on this while writing a process analysis tool, when a process
for which I am readins properties in /proc suddently dies, my script
goes crazy. I can work around this problem by copying the files in /tmp
before reading them.
I think the problem is that you need to check for a non-positive return
code from getline. For example, this script loops forever:

gawk 'BEGIN {while (getline < "/tmp/does.not.exist") print "yes"}'

But this one exits immediately:

gawk 'BEGIN {while ((getline < "/tmp/does.not.exist") > 0) print "yes"}'
"The getline command returns 0 on end of file and -1 on an error. Upon
an error, ERRNO contains a string describing the problem."

Regards,
Andy
Stepan Kasal
2005-09-17 16:23:28 UTC
Permalink
Hello,
Post by Simon Vaillancourt
echo test | gawk 'BEGIN { while (getline <
"/proc/anypid/unexistingfile") {print "line="$0}}'
the manpage says:
"The getline command returns 0 on end of file and -1 on an error."

Thus your code contains a bug. I think this bug occures quite often.

Corrected version:

gawk 'BEGIN { while ( (getline < "/proc/anypid/unexistingfile") > 0 )
{print "line="$0}}'

And, since it's a good habit to close the files when you are done,
it's customary to use a variable for the file name:

gawk 'BEGIN { a_file = "/proc/anypid/unexistingfile"
while ( (getline <a_file) > 0 ) {
print "line="$0
}
}'

Have a nice day,
Stepan Kasal
Simon Vaillancourt
2005-09-17 17:19:35 UTC
Permalink
Yikes, I got that from a sample in a tutorial, I should have RTFM, very sorry to have bothered you with that.



Simon









__________________________________________________
Sent Sat 9/17/2005 10:23 AM
To Simon Vaillancourt <***@oracle.com>
Cc bug-***@gnu.org
Subject Re: getline in /proc





__________________________________________________

Hello,
Post by Simon Vaillancourt
echo test | gawk 'BEGIN { while (getline <
"/proc/anypid/unexistingfile") {print "line="$0}}'
the manpage says:



"The getline command returns 0 on end of file and -1 on an error."







Thus your code contains a bug. I think this bug occures quite often.







Corrected version:







gawk 'BEGIN { while ( (getline < "/proc/anypid/unexistingfile") > 0 )



{print "line="$0}}'







And, since it's a good habit to close the files when you are done,



it's customary to use a variable for the file name:







gawk 'BEGIN { a_file = "/proc/anypid/unexistingfile"



while ( (getline <a_file) > 0 ) {



print "line="$0



}



}'







Have a nice day,



Stepan Kasal
Jürgen Kahrs
2005-09-17 16:25:36 UTC
Permalink
Post by Simon Vaillancourt
Hello,
I'm not sure if this is a gawk(using version 3.1.1) bug or a procfs
issue but but when doing a getline on a file that doesn't exist in
echo test | gawk 'BEGIN { while (getline <
"/proc/anypid/unexistingfile") {print "line="$0}}'
Yes, this loops forever. And this is your bug.
Read page 62 of "The AWK Programming Language".

Loading...