Browse Source

libbb: introduce and use xsettimeofday()

function                                             old     new   delta
xsettimeofday                                          -      25     +25
rdate_main                                           274     260     -14
step_time                                            348     331     -17
set_kernel_timezone_and_clock                        119     102     -17
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/3 up/down: 25/-48)            Total: -23 bytes
   text	   data	    bss	    dec	    hex	filename
1020753	    559	   5052	1026364	  fa93c	busybox_old
1020708	    559	   5052	1026319	  fa90f	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Denys Vlasenko 3 years ago
parent
commit
eb0c2e218c
6 changed files with 26 additions and 22 deletions
  1. 2 0
      include/libbb.h
  2. 0 12
      libbb/xfuncs.c
  3. 18 0
      libbb/xfuncs_printf.c
  4. 1 2
      networking/ntpd.c
  5. 2 4
      util-linux/hwclock.c
  6. 3 4
      util-linux/rdate.c

+ 2 - 0
include/libbb.h

@@ -657,6 +657,8 @@ void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC;
 time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC;
 char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
 char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
+void xsettimeofday(const struct timeval *tv) FAST_FUNC;
+
 
 int xsocket(int domain, int type, int protocol) FAST_FUNC;
 void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC;

+ 0 - 12
libbb/xfuncs.c

@@ -417,15 +417,3 @@ int FAST_FUNC wait4pid(pid_t pid)
 		return WTERMSIG(status) + 0x180;
 	return 0;
 }
-
-// Useful when we do know that pid is valid, and we just want to wait
-// for it to exit. Not existing pid is fatal. waitpid() status is not returned.
-int FAST_FUNC wait_for_exitstatus(pid_t pid)
-{
-	int exit_status, n;
-
-	n = safe_waitpid(pid, &exit_status, 0);
-	if (n < 0)
-		bb_simple_perror_msg_and_die("waitpid");
-	return exit_status;
-}

+ 18 - 0
libbb/xfuncs_printf.c

@@ -702,3 +702,21 @@ void FAST_FUNC xvfork_parent_waits_and_exits(void)
 	}
 	/* Child continues */
 }
+
+// Useful when we do know that pid is valid, and we just want to wait
+// for it to exit. Not existing pid is fatal. waitpid() status is not returned.
+int FAST_FUNC wait_for_exitstatus(pid_t pid)
+{
+	int exit_status, n;
+
+	n = safe_waitpid(pid, &exit_status, 0);
+	if (n < 0)
+		bb_simple_perror_msg_and_die("waitpid");
+	return exit_status;
+}
+
+void FAST_FUNC xsettimeofday(const struct timeval *tv)
+{
+	if (settimeofday(tv, NULL))
+		bb_simple_perror_msg_and_die("settimeofday");
+}

+ 1 - 2
networking/ntpd.c

@@ -1147,8 +1147,7 @@ step_time(double offset)
 	gettimeofday(&tvc, NULL); /* never fails */
 	dtime = tvc.tv_sec + (1.0e-6 * tvc.tv_usec) + offset;
 	d_to_tv(dtime, &tvn);
-	if (settimeofday(&tvn, NULL) == -1)
-		bb_simple_perror_msg_and_die("settimeofday");
+	xsettimeofday(&tvn);
 
 	VERB2 {
 		tval = tvc.tv_sec;

+ 2 - 4
util-linux/hwclock.c

@@ -184,10 +184,8 @@ static void set_kernel_timezone_and_clock(int utc, const struct timeval *hctosys
 	/*tz.tz_dsttime = 0; already is */
 	set_kernel_tz(&tz); /* MIGHT warp_clock() if 1st call since boot */
 
-	if (hctosys) { /* it's --hctosys: set time too */
-		if (settimeofday(hctosys, NULL))
-			bb_simple_perror_msg_and_die("settimeofday");
-	}
+	if (hctosys) /* it's --hctosys: set time too */
+		xsettimeofday(hctosys);
 }
 
 static void to_sys_clock(const char **pp_rtcname, int utc)

+ 3 - 4
util-linux/rdate.c

@@ -96,11 +96,10 @@ int rdate_main(int argc UNUSED_PARAM, char **argv)
 		if (time(NULL) == remote_time)
 			bb_simple_error_msg("current time matches remote time");
 		else {
-			struct timespec ts;
+			struct timeval ts;
 			ts.tv_sec = remote_time;
-			ts.tv_nsec = 0;
-			if (clock_settime(CLOCK_REALTIME, &ts) < 0)
-				bb_simple_perror_msg_and_die("can't set time of day");
+			ts.tv_usec = 0;
+			xsettimeofday(&ts);
 		}
 	}