home Mail List
Info
Info
Meetings
Goals
Upcoming
Projects
FAQ
Security
Links

[Date Prev][Date Next] [Chronological] [Thread] [Top]

[NMLUG] Using LD_PRELOAD to make date return a specific date



On Tue, Apr 24, 2007 at 07:39:50PM -0700, Kelly Jones wrote:
> I recently discovered LD_PRELOAD, a cool environment variable that
> lets a library "intercept" system calls. For example, setting
> LD_PRELOAD to /usr/lib/libtsocks.so lets tsocks intercept socket
> connections and redirect them to a SOCKS proxy.
> 
> My question: how can I write a library that intercepts the
> gettimeofday() system call (or time() or whatever the 'date' command
> uses) and gets 'date' to return, say, "Thu Jan 1 00:00:00 UTC 1970"?

$ strace date
[lots of chunkage removed]
clock_gettime(CLOCK_REALTIME, {1177614160, 204001532}) = 0
[remaining spew removed]
$ man clock_gettime

clock_gettime takes a clock identifier, and a pointer to the timespec
struct, which looks like this:

struct timespec {
    time_t   tv_sec;        /* seconds */
    long     tv_nsec;       /* nanoseconds */
};

It also sets errno to 0 and returns 0 for success. Our library is going
to want to do that. So, let's do it:

/* new_gettime.c */
#include <time.h>
#include <errno.h>
extern int errno;
int clock_gettime(clockid_t unused, struct timespec *in) {
    errno = 0;
    in->tv_sec = 0;
    in->tv_nsec = 0;
    return 0;
}

To compile it:

$ gcc -fPIC   -c -o new_gettime.o new_gettime.c
$ gcc -shared new_gettime.o -o new_gettime.so

And to run it:

$ LD_PRELOAD=./new_gettime.so date
Wed Dec 31 17:00:00 MST 1969

Ta-da.

I got the compilation commands from http://neworder.box.sk/newsread.php?newsid=13857
but I wrote the code before I even went to go look for how to compile
it. :P

-- Pi

> 
> I realize this involves a couple of steps (writing a C "library" for
> one), so any pointers are appreciated. My real intentions are more
> complex (and sinister <G>).

LD_PRELOAD usually is. ;)

> 
> -- 
> We're just a Bunch Of Regular Guys, a collective group that's trying
> to understand and assimilate technology. We feel that resistance to
> new ideas and technology is unwise and ultimately futile.
> _______________________________________________
> NMLUG mailing list
> NMLUG at nmlug.org
> http://lists.b9.com/cgi-bin/mailman/listinfo/nmlug
> 


--
I have always wished that my computer would be as easy to use as my telephone.
My wish has come true. I no longer know how to use my telephone.
-- Bjarne Stroustrup 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.b9.com/pipermail/nmlug/attachments/20070426/012095fc/attachment.pgp 




Please send sugestions and comments to webmaster@nmlug.org.
Valid XHTML 1.1! Valid CSS! Powered by Debian Powered by Apache