syscalls/settimeofday01: convert to new library
PATCH 1/2 syscalls/settimeofday01: convert to new library
Also, since glibc 2.31[1], when tz and tv are both null, it will get SIGSEGV sig. So, remove this EFAULT error test to adopt glibc2.31.
Moreover, musl 1.2.0 is now available and changes time_t for 32-bit archs to a 64-bit type. It is updated to slove y2038 problem and get time function no longer used for 32bit arch but not affecting set time function and 64 bit arch, more info see[2]. So for gettimeofday(), we use tst_syscall instead of calling libc to avoid this problem.
[1]https://sourceware.org/ml/libc-announce/2020/msg00001.html [2]https://musl.libc.org/time64.html
--- .../syscalls/settimeofday/settimeofday01.c | 200 ++++-------------- 1 file changed, 46 insertions(+), 154 deletions(-)
testcases/kernel/syscalls/settimeofday/settimeofday01.c +46 -154
@@ -1,52 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* -* Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -/* -* NAME -* settimeofday01.c +* Copyright (c) International Business Machines Corp., 2001 * * DESCRIPTION * Testcase to check the basic functionality of settimeofday(). * -* ALGORITHM -* Setup: -* Setup signal handling. -* Check that we are the proper user. -* Setup expected errnos. -* Pause for SIGUSER1 if option specified. -* Save the current time values. -* Loop if the proper options are given. -* Call settimeofday and verify the time was changed. -* Call settimeofday with invalid Args and verify that the call fails. -* Cleanup: -* Restore the original time values. -* Print errno log and/or timing stats if options given. -* -* USAGE: <for command-line> -* settimeofday01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] -* where, -c n : Run n copies concurrently. -* -e : Turn on errno logging. -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. -* * History * 07/2001 John George * -Ported
@@ -58,135 +16,69 @@#include <sys/time.h> #include <errno.h> #include <unistd.h> -#include "test.h" +#include "tst_test.h" +#include "lapi/syscalls.h" -#define FAILED 1#define VAL_SEC 100#define VAL_MSEC 100 -#define ACCEPTABLE_DELTA 500/* in milli-seconds */ +#define ACCEPTABLE_DELTA 500#define USEC_PER_SEC 1000000L -char *TCID ="settimeofday01" ; -int TST_TOTAL =1 ; -time_t save_tv_sec, save_tv_usec; -struct timeval tp, tp1, tp2; - -void setup (void ); -void cleanup (void ); +struct timeval tv_saved; -#if !defined(UCLINUX ) - -int main (int argc,char **argv) +static void verify_settimeofday (void ) { -int lc;suseconds_t delta; +struct timeval tv1, tv2; -tst_parse_opts (argc, argv,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { -int condition_number =1 ; -/* reset tst_count in case we are looping */ - tst_count =0 ; - -gettimeofday (&tp,NULL ); - tp.tv_sec +=VAL_SEC ; - tp.tv_usec +=VAL_MSEC ; -if (tp.tv_usec >=USEC_PER_SEC ) - tp.tv_usec =VAL_MSEC ; - -TEST (settimeofday (&tp,NULL )); -if (TEST_RETURN ==-1 ) { -tst_resm (TFAIL ,"Error Setting Time, errno=%d" , -TEST_ERRNO ); - } - -if ((gettimeofday (&tp2, (struct timezone *)&tp1)) ==-1 ) { -tst_resm (TBROK ,"Error Getting Time, errno=%d" , errno); - } +if (tst_syscall (__NR_gettimeofday, &tv1,NULL ) ==-1 ) +tst_brk (TBROK |TERRNO ,"gettimeofday(&tv1, NULL) failed" ); -if (tp2.tv_sec > tp.tv_sec) { - delta = - (suseconds_t) (tp2.tv_sec - tp.tv_sec) *1000 + - (tp2.tv_usec - tp.tv_usec) /1000 ; - }else { - delta = - (suseconds_t) (tp.tv_sec - tp2.tv_sec) *1000 + - (tp.tv_usec - tp2.tv_usec) /1000 ; - } + tv1.tv_sec +=VAL_SEC ; + tv1.tv_usec +=VAL_MSEC ; +if (tv1.tv_usec >=USEC_PER_SEC ) + tv1.tv_usec =VAL_MSEC ; -if (delta > -ACCEPTABLE_DELTA && delta <ACCEPTABLE_DELTA ) { -tst_resm (TPASS ,"Test condition %d successful" , - condition_number++); - }else { -tst_resm (TFAIL ,"Test condition %d failed" , - condition_number++); - } - -/* Invalid Args : Error Condition where tp = NULL */ -TEST (settimeofday ((struct timeval *)-1 ,NULL )); -if (TEST_RETURN ==-1 ) { -tst_resm (TPASS ,"Test condition %d successful" , - condition_number++); - }else { -tst_resm (TFAIL ,"Test condition %d failed" , - condition_number); - } +TEST (settimeofday (&tv1,NULL )); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"settimeofday(&tv1, NULL) failed" ); +return ; + } +if (tst_syscall (__NR_gettimeofday, &tv2,NULL ) ==-1 ) +tst_brk (TBROK |TERRNO ,"gettimeofday(&tv2, NULL) failed" ); + +if (tv2.tv_sec > tv1.tv_sec) { + delta = + (suseconds_t) (tv2.tv_sec - tv1.tv_sec) *1000 + + (tv2.tv_usec - tv1.tv_usec) /1000 ; + }else { + delta = + (suseconds_t) (tv1.tv_sec - tv2.tv_sec) *1000 + + (tv1.tv_usec - tv2.tv_usec) /1000 ; } -cleanup (); -tst_exit (); +if (delta > -ACCEPTABLE_DELTA && delta <ACCEPTABLE_DELTA ) +tst_res (TPASS ,"settimeofday() pass" ); +else +tst_res (TFAIL ,"settimeofday() fail" ); } -#else - -int main (void ) +static void setup (void ) { -tst_resm (TINFO ,"test is not available on uClinux" ); -tst_exit (); +if (tst_syscall (__NR_gettimeofday, &tv_saved,NULL ) ==-1 ) +tst_brk (TBROK |TERRNO ,"gettimeofday(&tv_saved, NULL) failed" ); } -#endif /* if !defined(UCLINUX) */ - -/* -* setup() -* performs all ONE TIME setup for this test -*/ -void setup (void ) +static void cleanup (void ) { -tst_require_root (); - -tst_sig (FORK ,DEF_HANDLER , cleanup); - -/* Pause if that option was specified -* TEST_PAUSE contains the code to fork the test with the -c option. -*/ -TEST_PAUSE ; - -/* Save the current time values */ -if ((gettimeofday (&tp, (struct timezone *)&tp1)) ==-1 ) { -tst_brkm (TBROK , cleanup,"gettimeofday failed. " -"errno=%d" , errno); - } - save_tv_sec = tp.tv_sec; - save_tv_usec = tp.tv_usec; +if ((settimeofday (&tv_saved,NULL )) ==-1 ) +tst_brk (TBROK |TERRNO ,"settimeofday(&tv_saved, NULL) failed" ); } -/* -* cleanup() -* performs all ONE TIME cleanup for this test at -* completion or premature exit -*/ -void cleanup (void ) -{ -/* restore the original time values. */ - tp.tv_sec = save_tv_sec; - tp.tv_usec = save_tv_usec; -if ((settimeofday (&tp,NULL )) ==-1 ) { -tst_resm (TWARN ,"FATAL COULD NOT RESET THE CLOCK" ); -tst_resm (TFAIL ,"Error Setting Time, errno=%d" , errno); - } - -} +static struct tst_test test = { + .setup = setup, + .cleanup = cleanup, + .test_all = verify_settimeofday, + .needs_root =1 , +};
Signature · 1 line
2.18.0
PATCH 2/2 syscalls/settimeofday02: convert to new library
As well as settimeofday01, using tst_sycall for gettimeofday and remove EFAULT error test. Also, add EINVAL error test.
--- .../syscalls/settimeofday/settimeofday02.c | 207 +++++------------- 1 file changed, 49 insertions(+), 158 deletions(-)
testcases/kernel/syscalls/settimeofday/settimeofday02.c +49 -158
@@ -1,189 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* -* Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -/* -* NAME -* settimeofday02.c +* Copyright (c) International Business Machines Corp., 2001 * * DESCRIPTION * Testcase to check that settimeofday() sets errnos correctly. * -* ALGORITHM -* Setup: -* Setup signal handling. -* Check that we are not root. -* Setup expected errnos. -* Pause for SIGUSER1 if option specified. -* Save the current time values. -* Loop if the proper options are given. -* Call settimeofday with an invalid "buf" address and verify that -* errno is set to EFAULT. -* Call settimeofday as a non-root user. Verify that the call fails -* and errno is set to EPERM. -* Cleanup: -* Print errno log and/or timing stats if options given. -* -* USAGE: <for command-line> -* settimeofday02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] -* where, -c n : Run n copies concurrently. -* -e : Turn on errno logging. -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. -* * History * 07/2001 John George * -Ported -* -* Restrictions -* Must not be run as root */ #include <stdio.h> #include <sys/time.h> #include <errno.h> -#include "test.h" -#include <pwd.h> +#include <linux/capability.h> +#include "tst_capability.h" +#include "tst_test.h" +#include "lapi/syscalls.h" #define VAL_SEC 100#define VAL_MSEC 100 -char *TCID ="settimeofday02" ; -int TST_TOTAL =1 ; - -struct timeval tp; -time_t save_tv_sec, save_tv_usec; - -char nobody_uid[] ="nobody" ; -struct passwd *ltpuser; +struct timeval tv_saved; +static int flag; -void setup (void ); -void cleanup (void ); -void restore_time (void ); +static struct tcase { +struct timeval tv; +int exp_errno; +char *message; +} tcases[] = { + {{-1 ,0 },EINVAL ,"tv.tv_sec is negative" }, + {{0 ,-1 },EINVAL ,"tv.tv_usec is outside the range [0..999,999]" }, + {{100 ,100 },EPERM ,"calling process without CAP_SYS_TIME capability" }, +}; -#if !defined(UCLINUX ) - -int main (int argc,char **argv) +static void verify_settimeofday (unsigned int n) { -int lc; - -tst_parse_opts (argc, argv,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { -/* reset tst_count in case we are looping */ - tst_count =0 ; - -TEST (settimeofday ((void *)-1 ,NULL )); -if (TEST_RETURN !=-1 ) { -tst_resm (TFAIL ,"settimeofday(2) failed to FAIL" ); -restore_time (); - }else { -if (TEST_ERRNO !=EFAULT ) { -tst_resm (TFAIL ,"Expected EFAULT got %d" , -TEST_ERRNO ); - }else { -tst_resm (TPASS ,"Received expected errno" ); - } - } - - tp.tv_sec =VAL_SEC ; - tp.tv_usec =VAL_MSEC ; -TEST (settimeofday (&tp,NULL )); -if (TEST_RETURN !=-1 ) { -tst_resm (TFAIL ,"settimeofday(2) failed to FAIL" ); -restore_time (); - }else { -if (TEST_ERRNO !=EPERM ) { -tst_resm (TFAIL ,"Expected EPERM got %d" , -TEST_ERRNO ); - }else { -tst_resm (TPASS ,"Received expected errno" ); - } - } +struct tcase *tc = &tcases[n]; + + flag =0 ; +tst_res (TINFO ,"%s" , tc->message); +TEST (settimeofday (&tc->tv,NULL )); +if (TST_RET !=-1 ) { +tst_res (TFAIL ,"settimeofday() succeeded unexpectedly" ); + flag =1 ; +return ; } -cleanup (); -tst_exit (); +if (TST_ERR != tc->exp_errno) +tst_res (TFAIL |TTERRNO ,"Expected %s got " ,tst_strerrno (tc->exp_errno)); +else +tst_res (TPASS |TTERRNO ,"Received expected errno" ); } -#else - -int main (void ) +static void setup (void ) { -tst_resm (TINFO ,"test is not available on uClinux" ); -tst_exit (); +if (tst_syscall (__NR_gettimeofday, &tv_saved,NULL ) ==-1 ) +tst_brk (TBROK |TERRNO ,"gettimeofday(&tv_saved, NULL) failed" ); } -#endif /* if !defined(UCLINUX) */ - -/* -* setup() -* performs all ONE TIME setup for this test -*/ -void setup (void ) +static void cleanup (void ) { -tst_require_root (); - -tst_sig (FORK ,DEF_HANDLER , cleanup); - -/* Switch to nobody user for correct error code collection */ - ltpuser =getpwnam (nobody_uid); -if (setuid (ltpuser->pw_uid) ==-1 ) { -tst_resm (TINFO ,"setuid failed to " -"to set the effective uid to %d" , ltpuser->pw_uid); -perror ("setuid" ); - } - -/* Pause if that option was specified -* TEST_PAUSE contains the code to fork the test with the -c option. -*/ -TEST_PAUSE ; - -/* Save the current time values */ -if ((gettimeofday (&tp, (struct timezone *)&tp)) ==-1 ) { -tst_brkm (TBROK , cleanup,"gettimeofday failed. " -"errno=%d" , errno); - } - save_tv_sec = tp.tv_sec; - save_tv_usec = tp.tv_usec; +if (!flag) +return ; +if ((settimeofday (&tv_saved,NULL )) ==-1 ) +tst_brk (TBROK |TERRNO ,"settimeofday(&tv_saved, NULL) failed" ); } -/* -* cleanup() -* performs all ONE TIME cleanup for this test at -* completion or premature exit -*/ -void cleanup (void ) -{ +static struct tst_test test = { + .setup = setup, + .cleanup = cleanup, + .test = verify_settimeofday, + .tcnt =ARRAY_SIZE (tcases), + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_SYS_TIME ), + {} + }, +}; -} - -void restore_time (void ) -{ -/* restore the original time values. */ - tp.tv_sec = save_tv_sec; - tp.tv_usec = save_tv_usec; -if ((settimeofday (&tp,NULL )) ==-1 ) { -tst_resm (TWARN ,"FATAL COULD NOT RESET THE CLOCK" ); -tst_resm (TFAIL ,"Error Setting Time, errno=%d" , errno); - } -}
Signature · 1 line
2.18.0
PATCH 2/2 syscalls/settimeofday02: convert to new library
Hi Xu,
Quoted 1 line
+static void
verify_settimeofday(unsigned int n)
...
testcases/kernel/syscalls/settimeofday/settimeofday02.c line 45
Quoted 7 lines
testcases/kernel/syscalls/settimeofday/settimeofday02.c +7
-0+struct tcase *tc = &tcases[n]; + + flag =0 ; +tst_res (TINFO ,"%s" , tc->message); +TEST (settimeofday (&tc->tv,NULL )); +if (TST_RET !=-1 ) { +tst_res (TFAIL ,"settimeofday() succeeded unexpectedly" );
Maybe add TTERRNO here as well?
Review
Quoted 2 lines
(quoted diff) +2
-0+ flag = 1; + return;
Kind regards,
Petr
PATCH 2/2 syscalls/settimeofday02: convert to new library
Hi
Quoted 4 lines
Hi Xu,
Quoted 1 line
+static void
verify_settimeofday(unsigned int n)...
Quoted 8 lines
testcases/kernel/syscalls/settimeofday/settimeofday02.c line 45
Quoted 7 lines
testcases/kernel/syscalls/settimeofday/settimeofday02.c +7
-0+struct tcase *tc = &tcases[n]; + + flag =0 ; +tst_res (TINFO ,"%s" , tc->message); +TEST (settimeofday (&tc->tv,NULL )); +if (TST_RET !=-1 ) { +tst_res (TFAIL ,"settimeofday() succeeded unexpectedly" );Maybe add TTERRNO here as well?
Why? When run into here, settimeofyday succeeded unexpectedly, errno should be 0.
Best Regards
Yang Xu
Quoted 7 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ flag = 1; + return;Kind regards,
Petr
PATCH 2/2 syscalls/settimeofday02: convert to new library
Hi Xu,
Quoted 10 lines
Quoted 10 lines
Quoted 1 line
+static void
verify_settimeofday(unsigned int n)...
testcases/kernel/syscalls/settimeofday/settimeofday02.c line 45
Quoted 7 lines
testcases/kernel/syscalls/settimeofday/settimeofday02.c +7
-0+struct tcase *tc = &tcases[n]; + + flag =0 ; +tst_res (TINFO ,"%s" , tc->message); +TEST (settimeofday (&tc->tv,NULL )); +if (TST_RET !=-1 ) { +tst_res (TFAIL ,"settimeofday() succeeded unexpectedly" );Maybe add TTERRNO here as well?
Quoted 2 lines
Why? When run into here, settimeofyday succeeded unexpectedly, errno should be 0.
Correct. I'm sorry, I read != as ==.
Kind regards,
Petr
PATCH 2/2 syscalls/settimeofday02: convert to new library
In reply to Yang XuHi Xu,
Quoted 2 lines
#define
VAL_SEC100
#defineVAL_MSEC100
These 2 definitions aren't needed any more.
Kind regards,
Petr
PATCH 2/2 syscalls/settimeofday02: convert to new library
Hi Petr
Quoted 5 lines
Hi Xu,
Quoted 2 lines
#define VAL_SEC 100 #define VAL_MSEC 100These 2 definitions aren't needed any more.
Yes. I forgot to delete...
Best Regards
Yang Xu
Quoted 5 lines
Kind regards,
Petr
PATCH 2/2 syscalls/settimeofday02: convert to new library
Hi Xu,
Quoted 4 lines
Quoted 3 lines
Quoted 2 lines
#define VAL_SEC 100 #define VAL_MSEC 100These 2 definitions aren't needed any more.
Yes. I forgot to delete...
This can be done during merge.
Kind regards,
Petr
PATCH 1/2 syscalls/settimeofday01: convert to new library
In reply to Yang XuHi Xu,
Quoted 2 lines
Also, since glibc 2.31[1], when tz and tv are both null, it will get SIGSEGV sig. So, remove this EFAULT error test to adopt glibc 2.31.
+1, thanks for testing on new glibc.
Quoted 2 lines
Moreover, musl 1.2.0 is now available and changes
time_tfor 32-bit archs to a 64-bit type. It is updated to slove y2038 problem and get
=> typo solve.
Quoted 3 lines
time function no longer used for 32bit arch but not affecting set time function and 64 bit arch, more info see[2]. So for
gettimeofday(), we usetst_syscallinstead of calling libc to avoid this problem.
Hm, it'd be nice to test both raw syscall and libc implementations (via tst_variant), but IMHO there is no way to detect musl change. Or am I missing something?
+ one would prefer this change to be in a separate commit, but we can live with that :).
I guess there is no need to setup SAFE_{G,S}ETTIMEOFDAY(), because only these 2 tests and stime_var.h are using it (or at least no now).
Kind regards,
Petr
PATCH 1/2 syscalls/settimeofday01: convert to new library
Hi Petr
Quoted 5 lines
Hi Xu,
Quoted 2 lines
Also, since glibc 2.31[1], when tz and tv are both null, it will get SIGSEGV sig. So, remove this EFAULT error test to adopt glibc 2.31.
+1, thanks for testing on new glibc.
Quoted 8 lines
Quoted 2 lines
Moreover, musl 1.2.0 is now available and changes
time_tfor 32-bit archs to a 64-bit type. It is updated to slove y2038 problem and get=> typo solve.
Quoted 3 lines
time function no longer used for 32bit arch but not affecting set time function and 64 bit arch, more info see[2]. So for
gettimeofday(), we usetst_syscallinstead of calling libc to avoid this problem.Hm, it'd be nice to test both raw syscall and libc implementations (via
tst_variant),
From glibc 2.31 announce,
"settimeofday itself is obsolescent according to POSIX. Programs
that set the system time should use clock_settime and/or the adjtime family of functions instead".
This is why I am not using tst_variant for this case. Also, glibc actually call clock_settime/gettime for this pair.
Quoted 3 lines
but IMHO there is no way to detect musl change. Or am I missing something?
I remembered we have a commit for this "
syscalls: Check for time64 unsafe syscalls before using them" Looking musl glibc code[1], can we use __NR_gettimeofday_time32/__NR_settimeofday_time32 in syscalls.h to detect this change?
[1]http://git.musl-libc.org/cgit/musl/commit/?id=5a105f19b5aae79dd302899e634b6b18b3dcd0d6
Quoted 4 lines
+ one would prefer this change to be in a separate commit, but we can live with that :).
I guess there is no need to setup SAFE_{G,S}ETTIMEOFDAY(), because only these 2 tests and
stime_var.hare using it (or at least no now).
Agree. The more important reason is that get/settimeofday is obsolete, we should use other syscall to set/get time.
Best Regards
Yang Xu
Quoted 7 lines
Reviewed-by Petr Vorel <[email protected]>Kind regards,
Petr
PATCH 1/2 syscalls/settimeofday01: convert to new library
Hi Xu,
both patches accepted.
Thank you.
Kind regards,
Petr