Date: Thu, 26 Apr 2007 12:15:16 +0100 From: Tom Evans <tevans.uk@googlemail.com> To: Kelly Jones <kelly.terry.jones@gmail.com> Cc: techtalk@linuxchix.org, freebsd-questions@freebsd.org, linuxusersgroup@googlegroups.com, nmosug-l@mailman.swcp.com, nmlug@nmlug.org Subject: Re: Using LD_PRELOAD to make date return a specific date Message-ID: <1177586116.3530.4.camel@zoot.mintel.co.uk> In-Reply-To: <26face530704241939x1acd5d6en56b26fe4bdf69d85@mail.gmail.com> References: <26face530704241939x1acd5d6en56b26fe4bdf69d85@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
--=-kGPQ+rep418szBg4K85B Content-Type: text/plain Content-Transfer-Encoding: quoted-printable 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. >=20 > 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"? >=20 > 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>). >=20 It's quite straightforward. /bin/date actually uses localtime(), not gettimeofday(), but the principle is the same. > $ cat localtime_hack.c=20 #include <sys/types.h> #include <time.h> struct tm * localtime(const time_t *clock) { static struct tm tv; time_t epochal =3D 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 \ =20 liblocaltime_hack.so.1.0 localtime_hack.o > $ LD_PRELOAD=3D"`pwd`/liblocaltime_hack.so.1.0" /bin/date Thu 1 Jan 1970 01:00:01 BST Cheers Tom --=-kGPQ+rep418szBg4K85B Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (FreeBSD) iD8DBQBGMIm/lcRvFfyds/cRAp6TAJ4oGg26SQi7we9WC0JUPxNMfcBdkQCcDXVE jjgNvQR4jkXOBNzslSj+7Zo= =ztsu -----END PGP SIGNATURE----- --=-kGPQ+rep418szBg4K85B--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1177586116.3530.4.camel>