LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-06-2012, 09:56 AM   #1
PuPSSMaN
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Rep: Reputation: Disabled
stat returning zero exit code on no-existing files


Hi there!

I've recently ran into a strange issue:
'stat' returning zero exit code for non-existing files.

Like:

Code:
[root@bm ~]# stat /not/a/file
stat: cannot stat `/not/a/file': No such file or directory
[root@bm ~]# echo $?
0
EDIT: it was 1 in the block before - a typo

And this happens for any file.

The machine is a Virtuozzo container with Redhat-4 OS inside. Other containers on the same node behave well.

The 'stat' is not a wrapper.
Same happens for 'file' command. Seems that system call 'stat' might be somehow broken.

How can I ivestigate this further?

Any help would be appreciated!

Last edited by PuPSSMaN; 03-07-2012 at 05:49 AM.
 
Old 03-06-2012, 10:06 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
erm... 1 != 0

all looks find to me.
 
Old 03-06-2012, 11:24 AM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Right. What exit code do you expect stat(1) to be returning for a file that doesn't exist?
 
Old 03-06-2012, 11:50 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Yeah lost me too ??
 
Old 03-07-2012, 01:29 AM   #5
PuPSSMaN
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Sorry to confuse you, there was a typo in the code block.

It is as stated in the topic - stat has zero exit-code.
 
Old 03-07-2012, 03:06 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
you know what's even more confusing? Changing the original post after we've commented on it!
 
Old 03-07-2012, 05:50 AM   #7
PuPSSMaN
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by acid_kewpie View Post
you know what's even more confusing? Changing the original post after we've commented on it!
Sorry for confuse, added an edit note.

So, any idea why this can be happening?
 
Old 03-07-2012, 06:15 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I am still confused on what your issue is? stat has accurately returned that the file does not exist, hence as it has done the job correctly, it has a return value of zero.
 
Old 03-07-2012, 06:31 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
No, it should be a 1. Stat could *not* do the job right, as the file it was asked to act upon didn't exist. same logic as ls and a million other tools at that level.
 
Old 03-07-2012, 06:42 AM   #10
PuPSSMaN
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Yep, that's what I expect.

btw, here is a strace output:
Code:
[root@hw28 ~]# strace stat /not/a/file
execve("/usr/bin/stat", ["stat", "/not/a/file"], [/* 22 vars */]) = 0
brk(0)                                  = 0x1a90000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5954018000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5954017000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=77941, ...}) = 0
mmap(NULL, 77941, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f5954003000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY)      = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\332\341\3652\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1717800, ...}) = 0
mmap(0x32f5e00000, 3498328, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x32f5e00000
mprotect(0x32f5f4e000, 2093056, PROT_NONE) = 0
mmap(0x32f614d000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14d000) = 0x32f614d000
mmap(0x32f6152000, 16728, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x32f6152000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5954002000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5954001000
arch_prctl(ARCH_SET_FS, 0x7f59540016e0) = 0
mprotect(0x32f614d000, 16384, PROT_READ) = 0
mprotect(0x32f5c1b000, 4096, PROT_READ) = 0
munmap(0x7f5954003000, 77941)           = 0
brk(0)                                  = 0x1a90000
brk(0x1ab1000)                          = 0x1ab1000
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=56413968, ...}) = 0
mmap(NULL, 56413968, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f5950a34000
close(3)                                = 0
lstat("/not/a/file", 0x7fffb96a71e0)    = -1 ENOENT (No such file or directory)
open("/usr/share/locale/locale.alias", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2528, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5954016000
read(3, "# Locale name alias data base.\n#"..., 4096) = 2528
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f5954016000, 4096)            = 0
open("/usr/share/locale/en_US.UTF-8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US.utf8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.UTF-8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.utf8/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, "stat: ", 6stat: )                   = 6
write(2, "cannot stat `/not/a/file'", 25cannot stat `/not/a/file') = 25
open("/usr/share/locale/en_US.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US.utf8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en.utf8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, ": No such file or directory", 27: No such file or directory) = 27
write(2, "\n", 1
)                       = 1
exit_group(0)                           = ?
 
Old 03-07-2012, 12:51 PM   #11
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
There's something screwy going on here. Try your test on a single line, like this:
Code:
$ stat /not/a/file ; echo "exit code: $?"
 
Old 03-07-2012, 01:47 PM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Looks like it's a bug in the earlier Redhats/Fedoras, or maybe just an oversight?

RHEL4:
Code:
[eggroll@rhel4 ~]$ stat /not/a/file; echo $?
stat: cannot stat `/not/a/file': No such file or directory
0
[eggroll@rhel4 ~]$ file /not/a/file; echo $?
/not/a/file: cannot open (/not/a/file)
0
[eggroll@rhel4 ~]$ ls /not/a/file; echo $?
ls: /not/a/file: No such file or directory
1
Fedora 4:
Code:
[eggroll@fedora4 ~]$ stat /not/a/file; echo $?
stat: cannot stat `/not/a/file': No such file or directory
0
[eggroll@fedora4 ~]$ file /not/a/file; echo $?
/not/a/file: ERROR: cannot open `/not/a/file' (No such file or directory)
0
[eggroll@fedora4 ~]$ ls /not/a/file; echo $?
ls: /not/a/file: No such file or directory
1
Fedora 15:
Code:
[eggroll@fedora15 ~]$ stat /not/a/file; echo $?
stat: cannot stat `/not/a/file': No such file or directory
1
[eggroll@fedora15 ~]$ file /not/a/file; echo $?
/not/a/file: ERROR: cannot open `/not/a/file' (No such file or directory)
1
[eggroll@fedora15 ~]$ ls /not/a/file; echo $?
ls: cannot access /not/a/file: No such file or directory
2
 
Old 03-07-2012, 02:02 PM   #13
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I'm seeing the expected behavior in RHEL4.

Code:
$ grep Red /etc/issue
Red Hat Enterprise Linux AS release 4 (Nahant Update 9)

$ stat /not/a/file ; echo "exit code: $?"
stat: cannot stat `/not/a/file': No such file or directory
exit code: 1
 
Old 03-07-2012, 02:04 PM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by anomie View Post
I'm seeing the expected behavior in RHEL4.

Code:
$ grep Red /etc/issue
Red Hat Enterprise Linux AS release 4 (Nahant Update 9)

$ stat /not/a/file ; echo "exit code: $?"
stat: cannot stat `/not/a/file': No such file or directory
exit code: 1
Interesting, here's mine

Code:
$ grep Red /etc/issue
Red Hat Enterprise Linux WS release 4 (Nahant Update 4)

$ stat /not/a/file ; echo "exit code: $?"
stat: cannot stat `/not/a/file': No such file or directory
exit code: 0
 
Old 03-07-2012, 02:09 PM   #15
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
It could be that somewhere between RHEL4.4 and RHEL4.9 a bugfix (?) was applied to coreutils to address this.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] exit in script files cause konsole terminal itself to exit Star_Gazer Programming 11 04-12-2010 09:32 AM
how to add existing files to existing imported kdevelop project?? santana Programming 1 05-22-2007 10:53 AM
Could someone check my stat struct code? (C) smoothdogg00 Programming 8 03-20-2006 08:09 AM
lfs5.1.1 cannot find -lc collect2,returning 1 exit status keyur Linux From Scratch 0 08-17-2004 12:41 PM
Problem with wvdial returning exit code 15 bossmonkey666 Linux - Networking 0 08-09-2004 05:17 AM


All times are GMT -5. The time now is 01:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration