8

I am learning Python. Till now I've been doing only basic Python coding. A day ago, I checked python implementation of tree command. Suddenly I thought of creating a Python clone for uptime. I don't have any clue about which language it is implemented in and what would be the complexity involved in cloning it.

But I couldn't find its source code. I am using Fedora 14. kernel-devel package is installed. I did whereis uptime but the resulting /usr/bin/uptime file shows weird symbols when opened using vim. Googling for its source code couldn't yield desired results either. Where can I find its source code?

2
  • 1
    Rather than (or in addition to) opening files with vim to see what they're made of, try file /usr/bin/uptime. The file command uses a series of semi-magical heuristics to guess as to the contents of the file — in this case, an ELF-format binary executable.
    – mattdm
    Mar 15, 2011 at 19:39
  • A debugging tool I like is strace. It will show all the syscalls your application makes. If you run strace uptime it would show (after loading) open calls on /proc/uptime and /proc/loadavg. Though it doesn't show everything, it's sometimes a decent start on reverse engineering Mar 15, 2011 at 19:53

2 Answers 2

9

In general, on a RPM-based distribution like Fedora, you can find the name of the package which provides a given command with rpm -qf /path/to/command. Like this:

$ rpm -qf $( which uptime )
procps-3.2.8-18.20110302git.fc16.x86_64

You can then download the source RPM with yumdownloader --source procps. (yumdownloader comes from the yum-utils package, if you don't have that installed already.) Once you have the src.rpm, you can unpack it with either rpm -i or with rpm2cpio.

Alternately, in many cases you can query the RPM package for the project URL, which may helpfully point you to the upstream source:

$ rpm -qi procps|grep ^URL
URL         : http://gitorious.org/procps

And, cool, there's the code conveniently browsable online. If you follow through it, basically all it does is read /proc/uptime and print it prettily.

8

Uptime is part of the 'procps' package, the upstream source is at http://procps.sourceforge.net/ (Not a fedora user, so not sure where to find their .src.rpm).

To answer the question you didn't ask, however; take a look in /proc/uptime The first number is seconds since boot. You should be able to turn that into something usable fairly easily :)

2
  • Thanks. Got the code. I did checkout /proc/uptime before posting here. That was the last option I was gonna use.
    – Dharmit
    Mar 15, 2011 at 17:15
  • 1
    Just looked at the source, the code just reads the uptime in from /proc/uptime. Check the .../proc/sysinfo.c in the procps source, for the uptime() function: gitorious.org/procps/procps/blobs/master/proc/sysinfo.c#line75
    – jsbillings
    Mar 15, 2011 at 18:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .