[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Identifying Binaries/Executables on GNU Linux/UNIX

Is there a way, perhaps using a stat struct with fstat, to identify whether a certain file is an executable binary?  I looked over the fstat man pages, and it says you can identify the files permissions, or whether it's a directory, but I can't see anything to tell whether it's an executable.  
0
chsalvia
Asked:
chsalvia
  • 2
2 Solutions
 
sunnycoderCommented:
Hi chsalvia,

Look into source code of file command ... They use some heuristics such as magic number to determine file type ...
http://www.die.net/doc/linux/man/man1/file.1.html

Cheers!
sunnycoder
0
 
grg99Commented:
Do you mean:

(1)  Does the file have executable permissions for user X?

(2)  Is the file in a format this OS can load and execute?

(3)  #2 and are its prerequisite shared libraries around?

(4) #3 are the aforementioned libraries the right versions for this executable?

(5) #4 and are the config and other  files for this executable in the right places?

(6) #5 and are there enough system resources available to user X to run the program?


All very different questions, and they get harder and harder to verify as you go down the list.



0
 
chsalviaAuthor Commented:
grg, I meant number 2 on the list.  Is the file in a format the OS can load and execute.
0
 
grg99Commented:
okay, you can determine the file format either by using the "file" command, or peeking at the first few bytes of the file.

Search the internet for "a.out file format" which will describe the several magic numbers an a.out format file can have.

Also on Linux most binaries are in ELF format, search for "ELF executable format" for the magic numbers in these files.

Some Unix systems use COFF format, that has its own distinctive header magic words.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now