From owner-freebsd-questions@FreeBSD.ORG Tue Dec 13 06:36:39 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 484E01065676 for ; Tue, 13 Dec 2011 06:36:39 +0000 (UTC) (envelope-from bonomi@mail.r-bonomi.com) Received: from mail.r-bonomi.com (mx-out.r-bonomi.com [204.87.227.120]) by mx1.freebsd.org (Postfix) with ESMTP id 16F178FC16 for ; Tue, 13 Dec 2011 06:36:38 +0000 (UTC) Received: (from bonomi@localhost) by mail.r-bonomi.com (8.14.4/rdb1) id pBD6chAw002637; Tue, 13 Dec 2011 00:38:43 -0600 (CST) Date: Tue, 13 Dec 2011 00:38:43 -0600 (CST) From: Robert Bonomi Message-Id: <201112130638.pBD6chAw002637@mail.r-bonomi.com> To: freebsd-questions@freebsd.org, johnl@iecc.com In-Reply-To: <20111213051220.45894.qmail@joyce.lan> Cc: Subject: Re: What's wrong with this code? 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: Tue, 13 Dec 2011 06:36:39 -0000 From: "John Levine" wrote: > > This tiny routine is in a .so loadable module I use. (It's part of the > mailfront SMTP daemon.) > > static const char* date_string(void) > { > static char datebuf[64]; > time_t now = time(0); > struct tm* tm = gmtime(&now); > strftime(datebuf, sizeof datebuf - 1, "%d %b %Y %H:%M:%S -0000", tm); > return datebuf; > } > > I was getting bogus dates. Running it under GDB, time() is returning > -1, and setting errno to 22, which is EINVAL. Changing the call to > time to time(NULL) or time(&now) made no difference. > > I changed it to a call to gettimeofday(), which works fine. But what > could the problem have been? When I splice this routine into a tiny > test program that calls it and prints out the result, it works fine. how about a tiny .so that includes -only- that routine, and a 3-line or so main() that links against -that- .so? > The obvious problem, since it's in a .so, is that it's linking to something > other than the system library time() function, but I did an nm on the .so, > and it said this, which sure looks like the system time() function to me: > > U time@@FBSD_1.0 > > Setting a breakpoint in gdb gets a complaint about trying to set a > breakpoint in /lib/libc.so.7. > > Any ideas what the problem was? The errorno value you report is *NOT* a defined return code for date(3). The libc date(3) will return only EFAULT, or EPERM, per the manpage. This lends credence to the possibility of a run-time linker issue. HOWEVER, there is also the possiblity of memory getting trashed -- in just the 'right' wrong way -- *elsewhere* in the code. OR a corrupted .so Mentioning O/S release level, and CPU architecture would be a good idea :)