Re: Using LD_PRELOAD to make date return a specific date

2007-04-26 Thread Tom Evans
On Tue, 2007-04-24 at 19:39 -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"?
> 
> 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 ).
> 

It's quite straightforward. /bin/date actually uses localtime(), not
gettimeofday(), but the principle is the same.

> $ cat localtime_hack.c 
#include 
#include 

struct tm *
localtime(const time_t *clock)
{
static struct tm tv;
time_t epochal = 1;
localtime_r(&epochal, &tv);
return &tv;
}

> $ gcc -Wall -fpic -c -o localtime_hack.o localtime_hack.c

> $ gcc -shared -Wl,-soname,localtime_hack.so.1 -o \  
 liblocaltime_hack.so.1.0 localtime_hack.o

> $ LD_PRELOAD="`pwd`/liblocaltime_hack.so.1.0" /bin/date
Thu  1 Jan 1970 01:00:01 BST


Cheers

Tom


signature.asc
Description: This is a digitally signed message part


Using LD_PRELOAD to make date return a specific date

2007-04-24 Thread Kelly Jones

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"?

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 ).

--
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.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"