[CRIU] [PATCH 3/4] test: poll -- Use gettimeofday syscall directly

Andrew Vagin avagin at parallels.com
Thu Mar 26 01:42:27 PDT 2015


On Thu, Mar 26, 2015 at 11:23:14AM +0300, Cyrill Gorcunov wrote:
> The gettimeofday uses /etc/localtime (thanks avagin@
> for the hint) which is not present in namespace tests.
> So lets use syscall directly and make test pass in
> namespace environment as well.

gettimeofday() doesn't fail if /etc/localtime doesn't exist.

> 
> Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
> ---
>  test/zdtm/live/static/poll.c | 24 +++++++++++++++++++++---
>  1 file changed, 21 insertions(+), 3 deletions(-)
> 
> diff --git a/test/zdtm/live/static/poll.c b/test/zdtm/live/static/poll.c
> index 85f545593b6e..db4b427ce3e2 100644
> --- a/test/zdtm/live/static/poll.c
> +++ b/test/zdtm/live/static/poll.c
> @@ -19,6 +19,24 @@
>  const char *test_doc	= "Check poll() timeouts";
>  const char *test_author	= "Cyrill Gorcunov <gorcunov at parallels.com>";
>  
> +#ifdef __x86_64__
> +#define __NR_gettimeofday 96
> +#else
> +#define __NR_gettimeofday 78
> +#endif

gettimeofday() is an old syscall, so you don't need to define
__NR_gettimeofday.

#include <unistd.h>

> +
> +static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
> +{
> +	int ret;
> +
> +	ret = syscall(__NR_gettimeofday, tv, tz);

I think glibc uses vdso to handle gettimeofday()

> +	if (ret) {
> +		errno = -ret;

We don't need to set errno here.

man 2 syscall
syscall() stores any error
       code returned by the system call in errno(3) if an error occurs

> +		ret = -1;
> +	}
> +	return ret;
> +}
> +
>  static void show_timestamp(char *prefix, unsigned long tv_sec, unsigned long tv_usec)
>  {
>  	test_msg("%8s: sec %20lu nsec %20lu\n", prefix, tv_sec, tv_usec);
> @@ -61,7 +79,7 @@ int main(int argc, char *argv[])
>  
>  	show_pollfd(ufds, 2);
>  
> -	if (gettimeofday(&time1, NULL)) {
> +	if (sys_gettimeofday(&time1, NULL)) {
>  		err("Can't get first delta");
>  		exit(1);
>  	}
> @@ -72,7 +90,7 @@ int main(int argc, char *argv[])
>  		err("Fork failed");
>  		exit(1);
>  	} else if (pid == 0) {
> -		if (gettimeofday(&time1, NULL)) {
> +		if (sys_gettimeofday(&time1, NULL)) {
>  			err("Can't get from times");
>  			exit(1);
>  		}
> @@ -89,7 +107,7 @@ int main(int argc, char *argv[])
>  				exit(1);
>  			}
>  
> -			if (gettimeofday(&time2, NULL)) {
> +			if (sys_gettimeofday(&time2, NULL)) {
>  				err("Can't get from times");
>  				exit(1);
>  			}
> -- 
> 1.9.3
> 


More information about the CRIU mailing list