From owner-freebsd-questions@FreeBSD.ORG Thu Apr 26 11:15:22 2007 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 473BD16A40B for ; Thu, 26 Apr 2007 11:15:22 +0000 (UTC) (envelope-from tevans.uk@googlemail.com) Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.170]) by mx1.freebsd.org (Postfix) with ESMTP id CB18013C4B8 for ; Thu, 26 Apr 2007 11:15:21 +0000 (UTC) (envelope-from tevans.uk@googlemail.com) Received: by ug-out-1314.google.com with SMTP id 71so515781ugh for ; Thu, 26 Apr 2007 04:15:20 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:subject:from:to:cc:in-reply-to:references:content-type:date:message-id:mime-version:x-mailer; b=UxzqHjDagYBSMiMmKfuBfHAwjeu5yNbTcO17BhyaoQk600DNExsWzJraj6MMQzPmWpuf8Q8sDeFPTVdsnyCdPw1gr6YR0ymdiFPZH0sBmg2783k/z4X4P/ldzNRXUyojJx2qaiPG7kgRYfb6B+qrXSkOhu3BW6CUkbrTWgck9Cs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:subject:from:to:cc:in-reply-to:references:content-type:date:message-id:mime-version:x-mailer; b=LRf7aEt96U2hJJqY7VfQ3yFG17yHrhfEm9folGJb07NLmqGsCJ3LhTud4tsINsGHUC3OYGE0EtkdV4K9izoTqJrcfU9+rvuaC3d+jap9WexCoZsmmoJPpMEPShpPGDH0TJnpZwlUvvgCyfPdLQ0EbnSJzC8fu/44pN7k9vPeshY= Received: by 10.67.10.18 with SMTP id n18mr2130476ugi.1177586120571; Thu, 26 Apr 2007 04:15:20 -0700 (PDT) Received: from ?127.0.0.1? ( [213.206.147.185]) by mx.google.com with ESMTP id m1sm4927523uge.2007.04.26.04.15.18; Thu, 26 Apr 2007 04:15:19 -0700 (PDT) From: Tom Evans To: Kelly Jones In-Reply-To: <26face530704241939x1acd5d6en56b26fe4bdf69d85@mail.gmail.com> References: <26face530704241939x1acd5d6en56b26fe4bdf69d85@mail.gmail.com> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-kGPQ+rep418szBg4K85B" Date: Thu, 26 Apr 2007 12:15:16 +0100 Message-Id: <1177586116.3530.4.camel@zoot.mintel.co.uk> Mime-Version: 1.0 X-Mailer: Evolution 2.10.0 FreeBSD GNOME Team Port X-Mailman-Approved-At: Thu, 26 Apr 2007 11:41:02 +0000 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 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Apr 2007 11:15:22 -0000 --=-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 ). >=20 It's quite straightforward. /bin/date actually uses localtime(), not gettimeofday(), but the principle is the same. > $ cat localtime_hack.c=20 #include #include 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--