Re: Using LD_PRELOAD to make date return a specific date

Anthony Martinez <[email protected]>
Newsgroups gmane.org.user-groups.nmlug
Message-ID <[email protected]>
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
> [email protected]
> 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

_______________________________________________
NMLUG mailing list
[email protected]
http://lists.b9.com/cgi-bin/mailman/listinfo/nmlug
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGMPz+KaiGM/xGKzQRArxNAJ9+PElXYi1b6gzaXC3BXubZPFh0vgCcCFhI
jLrt6zXLW31aaBt1Vrq+DoE=
=AJFp
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.