Re: funny gettimeofday



On Sun, 23 Jul 2006 21:40:29 +0200, Nicolas George said:
>
> > Always check return codes. gettimeofday() *can* fail (most likely,
> > tv1 and/or tv2 are bum pointers that don't point where you think).
> 
> As a matter of fact, no, it can not, at least as long as its second argument
> is NULL:
> 
> # RETURN VALUE
> #
> #     The gettimeofday() function shall return 0 and no value shall be
> #     reserved to indicate an error.
> 
> (Single Unix version 3)

However, checking on my system (Fedora Core 5):

man gettimeofday says:

RETURN VALUE
       gettimeofday() and settimeofday() return 0 for success, or -1 for fail-
       ure (in which case errno is set appropriately).

ERRORS
       EFAULT One of tv or tz pointed outside the accessible address space.

       EINVAL Timezone (or something else) is invalid.

       EPERM  The  calling process has insufficient privilege to call settime-
              ofday(); under Linux the CAP_SYS_TIME capability is required.

And checking the actual Linux kernel source code in kernel/time.c:

asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __user *tz)
{
        if (likely(tv != NULL)) {
                struct timeval ktv;
                do_gettimeofday(&ktv);
                if (copy_to_user(tv, &ktv, sizeof(ktv)))
                        return -EFAULT;
        }

So on Linux systems, it can, in fact, return EFAULT rather than 0.

(Note that the Single Unix Spec is rather long in the tooth indeed, and
you'd be better off checking what the POSIX requirements are.  A quick check
of a Solaris 9 system and an AIX 5.2 system indicate that both of *those*
also can return error conditions.  The Solaris one lists the same three
codes (EFAULT, EINVAL, and EPERM) as Linux, and *also* shows this one
as well:

     Additionally, the gettimeofday() function will fail for  32-
     bit interfaces if:

SunOS 5.9           Last change: 24 May 2000                    1

Standard C Library Functions                     gettimeofday(3C)

     EOVERFLOW
           The system time has progressed beyond 2038,  thus  the
           size  of  the  tv_sec  member of the timeval structure
           pointed to by tp is insufficient to hold  the  current
           time in seconds.


Attachment: pgp52T68gMifB.pgp
Description: PGP signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]