hwclock: remove date(1)
Subsystems (2)
v2 cover hwclock: remove date(1)
* The last commit/patch adds hwclock-date.c
I added this file in case testers do not have 'bison' installed. It may, or may not, need to be tracked depending on how it is to be maintained. The 'c' file probably should be included in the distributed source archive to avoid the bison dependency. Karel, I don't know if you want to create a separate clean and build target for hwclock-date.c, or how you want to handle this. So, if you don't mind, I'll leave this detail to you.
Testers that want to build it can:
$ cd sys-utils/ $ rm hwclock-date.c $ bison -o hwclock-date.c hwclock-date.y
It does require bison 2.0 or newer, yacc won't work.
---
The following changes since commit 7ab6461fae3848ea85f494f29e26229717685ae3:
docs: add note about branches to README (2017-02-24 14:19:04 +0100)
are available in the git repository at:
[email protected]:jwpi/util-linux.git hwclock-date2m
for you to fetch changes up to 46f1d03062ff66125f38abafd922412b6c182f49:
hwclock: add sys-utils/hwclock-date.c (2017-02-24 14:41:32 -0500)
----------------------------------------------------------------
J William Piggott (4):
hwclock: use parse_datetime function
build-system: add hwclock-date
hwclock: add hwclock-date files
hwclock: add sys-utils/hwclock-date.c
include/cctype.h | 357 ++++ sys-utils/Makemodule.am | 1 + sys-utils/hwclock-date.c | 4307 ++++++++++++++++++++++++++++++++++++++++++++++ sys-utils/hwclock-date.y | 2427 ++++++++++++++++++++++++++ sys-utils/hwclock.c | 104 +- sys-utils/hwclock.h | 10 + 6 files changed, 7107 insertions(+), 99 deletions(-) create mode 100644 include/cctype.h create mode 100644 sys-utils/hwclock-date.c create mode 100644 sys-utils/hwclock-date.y
PATCH v2 1/4 hwclock: use parse_datetime function
* hwclock.c: replace interpret_date_string() with parse_datetime2(). Eliminates shell injection vulnerability from popen() date(1). Removes system dependency on date(1).
Significant reduction in hwclock execution time. Significant cleanup of hwclock.c code.
* Bug fix for "hwclock --predict --date 'bad_date'" printing:
hwclock: No usable set-to time. Cannot set clock.
The message now matches date(1) for an invalid --date argument.
--- sys-utils/hwclock.c | 104 +++------------------------------------------------- 1 file changed, 5 insertions(+), 99 deletions(-)
sys-utils/hwclock.c +5 -99
@@ -639,100 +639,6 @@ display_time(const bool hclock_valid, struct timeval hwctime) }/* -* Interpret the value of the --date option, which is something like -* "13:05:01". In fact, it can be any of the myriad ASCII strings that -* specify a time which the "date" program can understand. The date option -* value in question is our "dateopt" argument. -* -* The specified time is in the local time zone. -* -* Our output, "*time_p", is a seconds-into-epoch time. -* -* We use the "date" program to interpret the date string. "date" must be -* runnable by issuing the command "date" to the /bin/sh shell. That means -* in must be in the current PATH. -* -* If anything goes wrong (and many things can), we return code 10 -* and arbitrary *time_p. Otherwise, return code is 0 and *time_p is valid. -*/ -static int interpret_date_string (const struct hwclock_control *ctl, -time_t *const time_p) -{ -FILE *date_child_fp =NULL ; -char *date_command =NULL ; -char *date_resp =NULL ; -size_t len =0 ; -const char magic[] ="seconds-into-epoch=" ; -int retcode =1 ; -long seconds_since_epoch; - -if (!ctl->date_opt) { -warnx (_ ("No --date option specified." )); -return retcode; - } - -/* Quotes in date_opt would ruin the date command we construct. */ -if (strchr (ctl->date_opt,'"' ) !=NULL || -strchr (ctl->date_opt,'`' ) !=NULL || -strchr (ctl->date_opt,'$' ) !=NULL ) { -warnx (_ - ("The value of the --date option is not a valid date.\n" -"In particular, it contains illegal character(s)." )); -return retcode; - } - -xasprintf (&date_command,"date --date=\"%s\" +%s%%s" , - ctl->date_opt, magic); -if (ctl->debug) -printf (_ ("Issuing date command: %s\n" ), date_command); - - date_child_fp =popen (date_command,"r" ); -if (date_child_fp ==NULL ) { -warn (_ ("Unable to run 'date' program in /bin/sh shell. " -"popen() failed" )); - goto out; - } - -if (getline (&date_resp, &len, date_child_fp) <0 ) { -warn (_ ("getline() failed" )); - goto out; - } -if (ctl->debug) -printf (_ ("response from date command = %s\n" ), date_resp); -if (strncmp (date_resp, magic,sizeof (magic) -1 ) !=0 ) { -warnx (_ ("The date command issued by %s returned " -"unexpected results.\n" -"The command was:\n %s\n" -"The response was:\n %s" ), - program_invocation_short_name, date_command, date_resp); - goto out; - } - -if (sscanf (date_resp +sizeof (magic) -1 ,"%ld" , &seconds_since_epoch) <1 ) { -warnx (_ ("The date command issued by %s returned " -"something other than an integer where the " -"converted time value was expected.\n" -"The command was:\n %s\n" -"The response was:\n %s\n" ), - program_invocation_short_name, date_command, date_resp); - }else { - retcode =0 ; - *time_p = seconds_since_epoch; -if (ctl->debug) -printf (_ ("date string %s equates to " -"%ld seconds since 1969.\n" ), - ctl->date_opt, *time_p); - } - out: -free (date_command); -free (date_resp); -if (date_child_fp) -pclose (date_child_fp); - -return retcode; -} - -/* * Set the System Clock to time 'newtime'. * * Also set the kernel time zone value to the value indicated by the TZ
@@ -1460,6 +1366,7 @@ int main(int argc, char **argv)struct hwclock_control ctl = {NULL };struct timeval startup_time;struct adjtime adjtime = {0 }; +struct timespec when = {0 };/* * The time we started up, in seconds into the epoch, including * fractions.
@@ -1699,11 +1606,10 @@ int main(int argc, char **argv)#endif if (ctl.set || ctl.predict) { - rc =interpret_date_string (&ctl, &set_time); -/* (time-consuming) */ -if (rc !=0 ) { -warnx (_ ("No usable set-to time. " -"Cannot set clock." )); +if (parse_datetime2 (&when, ctl.date_opt,NULL , ctl.debug)) + set_time = when.tv_sec; +else { +warnx (_ ("invalid date '%s'" ), ctl.date_opt);hwclock_exit (&ctl,EX_USAGE ); } }
PATCH v2 2/4 build-system: add hwclock-date
In reply to J William Piggottsys-utils/Makemodule.am: add hwclock-date source file sys-utils/hwclock.h: add parse_datetime functions
--- sys-utils/Makemodule.am | 1 + sys-utils/hwclock.h | 10 ++++++++++ 2 files changed, 11 insertions(+)
sys-utils/Makemodule.am +1 -0
@@ -424,6 +424,7 @@ sbin_PROGRAMS += hwclock dist_man_MANS += sys-utils/hwclock.8 PATHFILES += sys-utils/hwclock.8 hwclock_SOURCES = \ + sys-utils/hwclock-date.c \ sys-utils/hwclock.c \ sys-utils/hwclock.h \ sys-utils/hwclock-cmos.c
sys-utils/hwclock.h +10 -0
@@ -82,4 +82,14 @@ extern int set_epoch_rtc(const struct hwclock_control *ctl);extern void hwclock_exit (const struct hwclock_control *ctl,int status); +/* date.c */ +int parse_datetime (struct timespec *,char const *,struct timespec const *); + +/* parse_datetime2 flag: if set, print debug/progress information to STDERR */ +#define PARSE_DATETIME_DEBUG 1 + +/* same as above, supporting additional flags */ +int parse_datetime2 (struct timespec *,char const *,struct timespec const *, +unsigned int flags); +#endif /* HWCLOCK_CLOCK_H */
PATCH v2 3/4 hwclock: add hwclock-date files
In reply to J William Piggottnew file: sys-utils/hwclock-date.y
new file: include/cctype.h
Like ctype.h, only hard coded to the 'C' locale. Functions are prefixed with c_, e.g., c_isalpha()
--- include/cctype.h | 357 +++++++ sys-utils/hwclock-date.y | 2427 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 2784 insertions(+) create mode 100644 include/cctype.h create mode 100644 sys-utils/hwclock-date.y
This patch was too long to post.
PATCH v2 4/4 hwclock: add sys-utils/hwclock-date.c
In reply to J William Piggott--- sys-utils/hwclock-date.c | 4307 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 4307 insertions(+) create mode 100644 sys-utils/hwclock-date.c
This patch was too long to post.
Karel Zak
maintainerQuoted 2 lines · J William Piggott
* The last commit/patch adds
hwclock-date.c
Seems better than the previous attempt.
Quoted 7 lines
I added this file in case testers do not have 'bison' installed. It may, or may not, need to be tracked depending on how it is to be maintained. The 'c' file probably should be included in the distributed source archive to avoid the bison dependency. Karel, I don't know if you want to create a separate clean and build target for
hwclock-date.c, or how you want to handle this. So, if you don't mind, I'll leave this detail to you.
Yes, it's good idea to have .c files in the distribution tarball, but I'd like to avoid generated files in the git repository. I'll try to play with it (autotools) to generate .y to .c on the fly if missing.
Do you want to keep the .y file sync with gnulib and minimize number of our local changes, or is it so stable area that we don't have to care about it? ... because I don't like the coding style and the debug stuff. If the overhead to maintain the file locally will be minimal than we can probably clean up it :-)
I guess it will be possible to move it to lib/parse-datetime.c and use it on another places in util-linux (instead of our minimalistic parse_datetime()), right?
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Quoted 18 lines · Karel Zak
Quoted 2 lines · J William Piggott
* The last commit/patch adds
hwclock-date.cSeems better than the previous attempt.
Quoted 7 lines
I added this file in case testers do not have 'bison' installed. It may, or may not, need to be tracked depending on how it is to be maintained. The 'c' file probably should be included in the distributed source archive to avoid the bison dependency. Karel, I don't know if you want to create a separate clean and build target for
hwclock-date.c, or how you want to handle this. So, if you don't mind, I'll leave this detail to you.Yes, it's good idea to have .c files in the distribution tarball, but I'd like to avoid generated files in the git repository. I'll try to play with it (autotools) to generate .y to .c on the fly if missing.
Quoted 5 lines
Do you want to keep the .y file sync with gnulib and minimize number of our local changes, or is it so stable area that we don't have to care about it? ... because I don't like the coding style and the debug stuff. If the overhead to maintain the file locally will be minimal than we can probably clean up it :-)
Stripping the gnulib dependencies are significant enough to make syncing to with upstream impractical, I think. I fact, I have already excluded the latest changes because they add more gnulib dependencies.
My impression is that the original parse_datetime() is stable. It seems like most of the changes now involve parse_datetime2() to expand capabilities. I was using the new api only for the debug output; I thought if changes were ever needed in the parser it might be helpful. Since that may be unlikely, and since you don't like the debugging, parse_datetime2() along with all of its extensions can be removed.
Quoted 4 lines
I guess it will be possible to move it to
lib/parse-datetime.cand use it on another places in util-linux (instead of our minimalisticparse_datetime()), right?
Sure, I think a name change to avoid confusion with gnulib would be good. Maybe just parse_date.c?
I will work on these changes Karel.
Quoted 3 lines
Karel
Karel Zak
maintainerQuoted 6 lines · J William Piggott
My impression is that the original
parse_datetime()is stable. It seems like most of the changes now involveparse_datetime2()to expand capabilities. I was using the new api only for the debug output; I thought if changes were ever needed in the parser it might be helpful. Since that may be unlikely, and since you don't like the debugging,parse_datetime2()along with all of its extensions can be removed.
Fortunately, this stuff is easy to test, so we can create regression tests for all the possible formats to be sure we do not introduce any regression.
Quoted 6 lines
Quoted 3 lines
I guess it will be possible to move it to
lib/parse-datetime.cand use it on another places in util-linux (instead of our minimalisticparse_datetime()), right?Sure, I think a name change to avoid confusion with gnulib would be good. Maybe just
parse_date.c?
Sounds, good.
Quoted 1 line
I will work on these changes Karel.
OK.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Hello,
Quoted 4 lines · Karel Zak
Quoted 3 lines · J William Piggott
My impression is that the original
parse_datetime()is stable. It seems like most of the changes now involveparse_datetime2()to expand capabilities.
More specifically: parse_datetime was made it re-entrant (removed global timezone variables) and was added debugging code. I don't think the parser itself has changed in a long while.
Quoted 3 lines
Fortunately, this stuff is easy to test, so we can create regression tests for all the possible formats to be sure we do not introduce any regression.
A description of all types of accepted inputs for gnu date (and techincally: gnulib's parse_datetime) is here:
https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
I guess it will be possible to move it to
lib/parse-datetime.cand use it on another places in util-linux (instead of our minimalisticparse_datetime()), right?Sure, I think a name change to avoid confusion with gnulib would be good. Maybe just
parse_date.c?
If you copy the gnulib's parser and adjust it, I might suggest doing away first with relative times, then perhaps with timezones.
There are some confusing combinations, especially with
relative times (e.g. "20170228 + 2 days" vs "20:45:45 + 2 days", the latter parsed as "8:45:45pm+02:00TZ + 1 day"), and then with all sorts of things that are taken as timezones (like "2017A" which is parsed as '8:17pm in timezone -01:00'.
If you don't want to meddle with the parser (which is understandable), as a fallback you might want to add a minor piece of code
that ensures any timezone/relative parts were not encountered during the parsing. To do that, I'd add a small check at the end of 'parse_datetime2', and look at the fields of 'struct parse_control':
http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/parse-datetime.y#n172
something like:
if (pc.dsts_seen==0 && pc.rels_seen==0 && pc.zones_seen==0 etc.)
Then the parser didn't encounter these parts.
I'm happy to help with testing if needed.
regards,
- assaf
P.S.
In fact, I wonder if it'll be easier to add such a flag to gnulib's parse_datetime2 , something like 'PARSE_DATETIME_NO_RELATIVE' and 'PARSE_DATETIME_NO_ZONE' which will reject such input.
Quoted 10 lines · Assaf Gordon
I might suggest doing away first with relative times, then perhaps with timezones.
There are some confusing combinations, especially with relative times (e.g. "20170228 + 2 days" vs "20:45:45 + 2 days", the latter parsed as "8:45:45pm+02:00TZ + 1 day"),
and then with all sorts of things that are taken as timezones (like "2017A" which is parsed as '8:17pm in timezone -01:00'.
Hello Assaf,
Because hwclock(8) was using date(1) to parse --date strings, it has always be subject to these issues. Some time ago I added a warning not to use relative times in hwclock.8. I just added a warning about timezones as well.
I don't think blocking them from the parser is needed. They could be useful to some other util-linux module.
Quoted 2 lines
I'm happy to help with testing if needed.
Thank you very much Assaf.
Karel Zak
maintainerPATCH 1/1 lib: add parse-date.y
In reply to J William Piggott* move sys-utils/hwclock-date.y to lib/parse-date.y
* remove generated .c from git tree
* add necessary autotools stuff to generate .c on the fly (autotools are smart enough to add generated file to tarball)
* check for bison version by ./autogen.sh
* add non-wanted junk to .gitignore
--- .gitignore | 1 + autogen.sh | 20 + configure.ac | 2 +- include/Makemodule.am | 1 + include/parse-date.h | 14 + lib/.gitignore | 1 + lib/Makemodule.am | 1 + sys-utils/hwclock-date.y => lib/parse-date.y | 7 +- sys-utils/Makemodule.am | 1 - sys-utils/hwclock-date.c | 4307 -------------------------- sys-utils/hwclock.c | 1 + sys-utils/hwclock.h | 10 - 12 files changed, 45 insertions(+), 4321 deletions(-) create mode 100644 include/parse-date.h create mode 100644 lib/.gitignore rename sys-utils/hwclock-date.y => lib/parse-date.y (99%) delete mode 100644 sys-utils/hwclock-date.c
.gitignore +1 -0
@@ -55,6 +55,7 @@ test-suite.log tests/run.sh.log tests/run.sh.trs update.log +ylwrap # # binaries
autogen.sh +20 -0
@@ -67,6 +67,25 @@ test -f sys-utils/mount.c || { DIE=1 } +if ! (bison --version) < /dev/null > /dev/null 2>&1; then + echo + echo "You must have bison installed to build the util-linux." + echo + DIE=1 +else + lexver=$(bison --version | awk '/bison \(GNU Bison\)/ { print $4 }') + case "$lexver" in + [2-9].*) + ;; + *) + echo + echo "You must have bison version >= 2.x, but you have $lexver." + echo + DIE=1 + ;; + esac +fi + LIBTOOLIZE=libtoolize case `uname` in Darwin*) LIBTOOLIZE=glibtoolize ;; esac if ! ($LIBTOOLIZE --version) < /dev/null > /dev/null 2>&1; then
@@ -104,6 +123,7 @@ echo " autoconf: $(autoconf --version | head -1)" echo " autoheader: $(autoheader --version | head -1)" echo " automake: $(automake --version | head -1)" echo " libtoolize: $($LIBTOOLIZE --version | head -1)" +echo " bison: $(bison --version | head -1)" rm -rf autom4te.cache
configure.ac +1 -1
@@ -99,6 +99,7 @@ AC_SUBST([usrlib_execdir]) AM_PROG_CC_C_O AC_PROG_MKDIR_P AC_PROG_CC_STDC +AC_PROG_YACC AC_CANONICAL_HOST AC_C_CONST AC_C_VOLATILE
@@ -153,7 +154,6 @@ PKG_PROG_PKG_CONFIG GTK_DOC_CHECK([1.10]) AC_PATH_PROG([XSLTPROC], [xsltproc]) - linux_os=no bsd_os=no AS_CASE([${host_os}],
include/Makemodule.am +1 -0
@@ -33,6 +33,7 @@ dist_noinst_HEADERS += \ include/optutils.h \ include/pager.h \ include/pamfail.h \ + include/parse-date.h \ include/path.h \ include/pathnames.h \ include/plymouth-ctrl.h \
include/parse-date.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef UTIL_LINUX_PARSE_DATE_H +#define UTIL_LINUX_PARSE_DATE_H + +int parse_datetime (struct timespec *,char const *,struct timespec const *); + +/* parse_datetime2 flag: if set, print debug/progress information to STDERR */ +#define PARSE_DATETIME_DEBUG 1 + +/* same as above, supporting additional flags */ +int parse_datetime2 (struct timespec *,char const *,struct timespec const *, +unsigned int flags); + +#endif /* UTIL_LINUX_PARSE_DATE_H */ +
lib/.gitignore new file +1 -0
@@ -0,0 +1 @@ +parse-date.c
lib/Makemodule.am +1 -0
@@ -16,6 +16,7 @@ libcommon_la_SOURCES = \ lib/mbsedit.c\ lib/md5.c \ lib/pager.c \ + lib/parse-date.y \ lib/path.c \ lib/randutils.c \ lib/setproctitle.c \
lib/parse-date.y renamed from sys-utils/hwclock-date.y +5 -2
@@ -33,8 +33,11 @@ /* FIXME: Check for arithmetic overflow in all cases, not just some of them. */ -#include <config.h> -#include "hwclock.h" +#include <sys/time.h> +#include <time.h> + +#include "c.h" +#include "parse-date.h" /* There's no need to extend the stack, so there's no need to involve alloca. */
sys-utils/Makemodule.am +0 -1
@@ -424,7 +424,6 @@ sbin_PROGRAMS += hwclock dist_man_MANS += sys-utils/hwclock.8 PATHFILES += sys-utils/hwclock.8 hwclock_SOURCES = \ - sys-utils/hwclock-date.c \ sys-utils/hwclock.c \ sys-utils/hwclock.h \ sys-utils/hwclock-cmos.c
sys-utils/hwclock-date.c deleted +0 -4307
2,898 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.9.3
Part of the diff is not shown here. View the raw message.