From 57fc90ee519b724a0d244d1d9a355a64711d8644 Mon Sep 17 00:00:00 2001 From: marius Date: Sat, 5 Aug 2017 12:54:07 +0000 Subject: [PATCH] MFC: r274394, r274399, r307802 - Default `bsdconfig timezone' and `tzsetup' to `-s' in a VM. - Hide dialog specific code behind HAVE_DIALOG. It allows to build a stripped down version (missing the dialog UI) but perfectly function tzsetup when world is built WITHOUT_DIALOG. git-svn-id: svn://svn.freebsd.org/base/stable/10@322096 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- tools/build/mk/OptionalObsoleteFiles.inc | 2 - usr.sbin/Makefile | 3 +- usr.sbin/bsdconfig/timezone/timezone | 5 +- usr.sbin/tzsetup/Makefile | 10 +- usr.sbin/tzsetup/tzsetup.c | 163 +++++++++++++++-------- 5 files changed, 118 insertions(+), 65 deletions(-) diff --git a/tools/build/mk/OptionalObsoleteFiles.inc b/tools/build/mk/OptionalObsoleteFiles.inc index 2365f4de4..0fb9d287a 100644 --- a/tools/build/mk/OptionalObsoleteFiles.inc +++ b/tools/build/mk/OptionalObsoleteFiles.inc @@ -1069,12 +1069,10 @@ OLD_FILES+=usr/lib/libdpv.so OLD_FILES+=usr/lib/libdpv.so.1 OLD_FILES+=usr/lib/libdpv_p.a OLD_FILES+=usr/sbin/bsdconfig -OLD_FILES+=usr/sbin/tzsetup OLD_FILES+=usr/share/man/man1/dialog.1.gz OLD_FILES+=usr/share/man/man1/dpv.1.gz OLD_FILES+=usr/share/man/man3/dialog.3.gz OLD_FILES+=usr/share/man/man3/dpv.3.gz -OLD_FILES+=usr/share/man/man8/tzsetup.8.gz OLD_FILES+=usr/share/man/man8/bsdconfig.8.gz .endif diff --git a/usr.sbin/Makefile b/usr.sbin/Makefile index b3e8c8af1..a3b1030c7 100644 --- a/usr.sbin/Makefile +++ b/usr.sbin/Makefile @@ -89,7 +89,7 @@ SUBDIR= adduser \ tcpdump \ traceroute \ trpt \ - ${_tzsetup} \ + tzsetup \ uefisign \ ugidfw \ vigr \ @@ -154,7 +154,6 @@ SUBDIR+= cxgbetool .if ${MK_DIALOG} != "no" _bsdconfig= bsdconfig -_tzsetup= tzsetup .endif .if ${MK_FLOPPY} != "no" diff --git a/usr.sbin/bsdconfig/timezone/timezone b/usr.sbin/bsdconfig/timezone/timezone index a830ba93d..a29144298 100755 --- a/usr.sbin/bsdconfig/timezone/timezone +++ b/usr.sbin/bsdconfig/timezone/timezone @@ -62,7 +62,7 @@ _PATH_WALL_CMOS_CLOCK="/etc/wall_cmos_clock" REALLYDOIT=1 REINSTALL= USEDIALOG=1 -SKIPUTC= +SKIPUTC= # See MAIN VERBOSE= TZ_OR_FAIL= CHROOTENV= @@ -119,6 +119,9 @@ dialog_menu_main() ############################################################ MAIN +# Skip initial question regarding UTC v. Wall-Clock time if run in VM +[ "$( sysctl -n kern.vm_guest 2> /dev/null )" = "none" ] || SKIPUTC=1 + # Incorporate rc-file if it exists [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc" diff --git a/usr.sbin/tzsetup/Makefile b/usr.sbin/tzsetup/Makefile index 5c80a4800..d28c0af06 100644 --- a/usr.sbin/tzsetup/Makefile +++ b/usr.sbin/tzsetup/Makefile @@ -1,16 +1,18 @@ # $FreeBSD$ +.include + PROG= tzsetup MAN= tzsetup.8 -CFLAGS+= -I${.CURDIR}/../../contrib/dialog -I. +CFLAGS+= -I. +.if ${MK_DIALOG} != no WARNS?= 3 - +CFLAGS+= -I${.CURDIR}/../../contrib/dialog -DHAVE_DIALOG DPADD= ${LIBDIALOG} ${LIBM} LDADD= -ldialog -lm - -.include +.endif .if ${MK_NCURSESW} == "no" DPADD+= ${LIBNCURSES} diff --git a/usr.sbin/tzsetup/tzsetup.c b/usr.sbin/tzsetup/tzsetup.c index 8c4230b05..2637924f2 100644 --- a/usr.sbin/tzsetup/tzsetup.c +++ b/usr.sbin/tzsetup/tzsetup.c @@ -47,8 +47,11 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include +#ifdef HAVE_DIALOG #include +#endif #define _PATH_ZONETAB "/usr/share/zoneinfo/zone.tab" #define _PATH_ISO3166 "/usr/share/misc/iso3166" @@ -71,6 +74,19 @@ __FBSDID("$FreeBSD$"); #define DITEM_LEAVE_MENU (1 << 16) #define DITEM_RECREATE (1 << 18) +static char path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN], + path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN], + path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN]; + +static int reallydoit = 1; +static int reinstall = 0; +static char *chrootenv = NULL; + +static void usage(void); +static int install_zoneinfo(const char *zoneinfo); +static int install_zoneinfo_file(const char *zoneinfo_file); + +#ifdef HAVE_DIALOG /* for use in describing more exotic behaviors */ typedef struct dialogMenuItem { char *prompt; @@ -186,20 +202,10 @@ again: return result; } -static char path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN], - path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN], - path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN]; - -static int reallydoit = 1; -static int reinstall = 0; static int usedialog = 1; -static char *chrootenv = NULL; -static void usage(void); static int confirm_zone(const char *filename); static int continent_country_menu(dialogMenuItem *); -static int install_zoneinfo(const char *zoneinfo); -static int install_zoneinfo_file(const char *zoneinfo_file); static int set_zone_multi(dialogMenuItem *); static int set_zone_whole_country(dialogMenuItem *); static int set_zone_menu(dialogMenuItem *); @@ -642,6 +648,53 @@ set_zone_utc(void) return (install_zoneinfo("UTC")); } +static int +confirm_zone(const char *filename) +{ + char title[64], prompt[64]; + time_t t = time(0); + struct tm *tm; + int rv; + + setenv("TZ", filename == NULL ? "" : filename, 1); + tzset(); + tm = localtime(&t); + + snprintf(title, sizeof(title), "Confirmation"); + snprintf(prompt, sizeof(prompt), + "Does the abbreviation `%s' look reasonable?", tm->tm_zone); + rv = !dialog_yesno(title, prompt, 5, 72); + return (rv); +} + +static int +set_zone_multi(dialogMenuItem *dmi) +{ + struct zone *zp = dmi->data; + int rv; + + if (!confirm_zone(zp->filename)) + return (DITEM_FAILURE | DITEM_RECREATE); + + rv = install_zoneinfo(zp->filename); + return (rv); +} + +static int +set_zone_whole_country(dialogMenuItem *dmi) +{ + struct country *cp = dmi->data; + int rv; + + if (!confirm_zone(cp->filename)) + return (DITEM_FAILURE | DITEM_RECREATE); + + rv = install_zoneinfo(cp->filename); + return (rv); +} + +#endif + static int install_zoneinfo_file(const char *zoneinfo_file) { @@ -671,9 +724,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Creating symbolic link %s to %s", path_localtime, zoneinfo_file); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); #endif @@ -684,9 +739,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Could not delete %s: %s", path_localtime, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -696,9 +753,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Could not delete %s: %s", path_db, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -707,9 +766,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(title, sizeof(title), "Done"); snprintf(prompt, sizeof(prompt), "Removed %s", path_localtime); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); #endif return (DITEM_LEAVE_MENU); @@ -722,9 +783,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Could not open %s: %s", zoneinfo_file, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } @@ -733,10 +796,12 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Could not unlink %s: %s", path_localtime, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) { snprintf(title, sizeof(title), "Error"); dialog_msgbox(title, prompt, 8, 72, 1); } else +#endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } @@ -748,9 +813,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Could not open %s: %s", path_localtime, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } @@ -764,9 +831,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Error copying %s to %s %s", zoneinfo_file, path_localtime, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); /* Better to leave none than a corrupt one. */ unlink(path_localtime); @@ -780,9 +849,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Cannot access %s: %s", zoneinfo_file, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } @@ -790,10 +861,12 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Could not unlink %s: %s", path_localtime, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) { snprintf(title, sizeof(title), "Error"); dialog_msgbox(title, prompt, 8, 72, 1); } else +#endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } @@ -803,9 +876,11 @@ install_zoneinfo_file(const char *zoneinfo_file) "Cannot create symbolic link %s to %s: %s", path_localtime, zoneinfo_file, strerror(errno)); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } @@ -821,9 +896,11 @@ install_zoneinfo_file(const char *zoneinfo_file) snprintf(prompt, sizeof(prompt), "Created symbolic link from %s to %s", zoneinfo_file, path_localtime); +#ifdef HAVE_DIALOG if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else +#endif fprintf(stderr, "%s\n", prompt); #endif } /* reallydoit */ @@ -854,51 +931,6 @@ install_zoneinfo(const char *zoneinfo) return (rv); } -static int -confirm_zone(const char *filename) -{ - char title[64], prompt[64]; - time_t t = time(0); - struct tm *tm; - int rv; - - setenv("TZ", filename == NULL ? "" : filename, 1); - tzset(); - tm = localtime(&t); - - snprintf(title, sizeof(title), "Confirmation"); - snprintf(prompt, sizeof(prompt), - "Does the abbreviation `%s' look reasonable?", tm->tm_zone); - rv = !dialog_yesno(title, prompt, 5, 72); - return (rv); -} - -static int -set_zone_multi(dialogMenuItem *dmi) -{ - struct zone *zp = dmi->data; - int rv; - - if (!confirm_zone(zp->filename)) - return (DITEM_FAILURE | DITEM_RECREATE); - - rv = install_zoneinfo(zp->filename); - return (rv); -} - -static int -set_zone_whole_country(dialogMenuItem *dmi) -{ - struct country *cp = dmi->data; - int rv; - - if (!confirm_zone(cp->filename)) - return (DITEM_FAILURE | DITEM_RECREATE); - - rv = install_zoneinfo(cp->filename); - return (rv); -} - static void usage(void) { @@ -911,10 +943,21 @@ usage(void) int main(int argc, char **argv) { +#ifdef HAVE_DIALOG char title[64], prompt[128]; - int c, fd, rv, skiputc; + int fd; +#endif + int c, rv, skiputc; + char vm_guest[16] = ""; + size_t len = sizeof(vm_guest); skiputc = 0; + + /* Default skiputc to 1 for VM guests */ + if (sysctlbyname("kern.vm_guest", vm_guest, &len, NULL, 0) == 0 && + strcmp(vm_guest, "none") != 0) + skiputc = 1; + while ((c = getopt(argc, argv, "C:nrs")) != -1) { switch(c) { case 'C': @@ -925,7 +968,9 @@ main(int argc, char **argv) break; case 'r': reinstall = 1; +#ifdef HAVE_DIALOG usedialog = 0; +#endif break; case 's': skiputc = 1; @@ -989,12 +1034,15 @@ main(int argc, char **argv) struct stat sb; if (stat(argv[optind], &sb) != 0) { +#ifdef HAVE_DIALOG usedialog = 0; +#endif rv = install_zoneinfo(argv[optind]); exit(rv & ~DITEM_LEAVE_MENU); } /* FALLTHROUGH */ } +#ifdef HAVE_DIALOG read_iso3166_table(); read_zones(); @@ -1055,5 +1103,8 @@ main(int argc, char **argv) dlg_clear(); end_dialog(); +#else + usage(); +#endif return (0); } -- 2.42.0