How do I trace coreutils down to a syscall?

I am trying to navigate and understand whoami (and other coreutils) all the way down to the lowest level source code, just as an exercise.

My dive so far:

  1. Where is the actual binary?

    which whoami /usr/bin/whoami

  2. Where is it maintained?

    http://www.gnu.org/software/coreutils/coreutils.html

  3. How do I get source?

git clone git://git.sv.gnu.org/coreutils

  1. Where is whoami source code within the repository?

# find . | grep whoami

./man/whoami.x
./man/whoami.1
./src/whoami.c
./src/whoami
./src/whoami.o
./src/.deps/src_libsinglebin_whoami_a-whoami.Po
./src/.deps/whoami.Po

relevant line (84):

uid = geteuid ();

This is approximately where my rabbit hole stops. geteuid() is mentioned in gnulib/lib/euidaccess.c, but not explicitly defined AFAICT. It's also referenced in /usr/local/unistd.h as extern but there's no heavy lifting related to grabbing a uid that I can see.

I got here by mostly grepping for geteuid within known system headers and includes as I'm having trouble backtracing its definition.

Question: How can I dive down further and explore the source code of geteuid()? What is the most efficient way to explore this codebase quickly without grepping around?

I'm on Ubuntu server 15.04 using Vim and some ctags (which hasn't been very helpful for navigating existing system headers). I'm a terrible developer and this is my method of learning, though I can't get through this roadblock.

Answers


Normally you should read the documentation for geteuid. You can either read GNU documentation, the specification from the Open Group or consult the man page.

If that doesn't help you could install the debug symbols for the c-library (it's called libc6-dbg or something similar) and download the source code for libc6) then you point out the path to the source file when you step into the library.

In this case I don't think this would take you much further, what probably happens in geteuid is that it simply issues an actual syscall and then it's into kernel space. You cannot debug that (kernel) code in the same way as you would debug a normal program.

So in your case you should better consult the documentation and read it carefully and try to figure out why geteuid doesn't return what you expect. Probably this will lead to you changing your expectation of what geteuid should return to match what's actually returned.


Need Your Help

SQL query not returning value, setting to session variable

php mysql sql sql-server

I've been trying to set a value from my mysql server table field equal too the $_SESSION["mypermissions"] session variable. Whatever I try it comes up blank though or with a value 0. I feel like th...

Defining generic function jquery

javascript jquery function checkbox syntax

I have a simple function that shows a div when the user clicks on a given checkbox. I'd like to have the same behaviour on another checkbox, so that's why I'd like to generalize it as a function pa...