From 96ac47e3126d4001093b745baf4b3d8f31636314 Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Tue, 3 Aug 2010 17:47:26 +0000 Subject: [PATCH] Made some progress with consolidation of timezone-related code: * renamed syscalls _kern_[gs]et_tzfilename to _kern_[gs]et_real_time_clock_is_gmt, as the filename part is no longer relevant (and the two corresponding parameters were removed) * C++-ified and reworked clockconfig to use the info from 'Time settings' to setup the timezone info during boot * removed invocation of _kern_get_tzfilename() from tzset(), as the syscall no longer exists and tzset() is currently broken anyway * adjusted the Time preflet to use the renamed syscall when getting/setting the RTC info git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37881 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/kernel/real_time_clock.h | 4 +- headers/private/system/syscalls.h | 6 +- src/bin/Jamfile | 4 +- src/bin/clockconfig.c | 81 -------------------- src/bin/clockconfig.cpp | 92 +++++++++++++++++++++++ src/preferences/time/DateTimeView.cpp | 2 +- src/system/kernel/real_time_clock.cpp | 18 +---- src/system/libroot/posix/time/localtime.c | 2 +- 8 files changed, 103 insertions(+), 106 deletions(-) delete mode 100644 src/bin/clockconfig.c create mode 100644 src/bin/clockconfig.cpp diff --git a/headers/private/kernel/real_time_clock.h b/headers/private/kernel/real_time_clock.h index 126cd0f689..2a18b4a1a0 100644 --- a/headers/private/kernel/real_time_clock.h +++ b/headers/private/kernel/real_time_clock.h @@ -42,8 +42,8 @@ bigtime_t _user_system_time(void); status_t _user_set_real_time_clock(uint32 time); status_t _user_set_timezone(int32 timezoneOffset, bool daylightSavingTime); status_t _user_get_timezone(int32 *_timezoneOffset, bool *_daylightSavingTime); -status_t _user_set_tzfilename(const char* filename, size_t length, bool isGMT); -status_t _user_get_tzfilename(char *filename, size_t length, bool *_isGMT); +status_t _user_set_real_time_clock_is_gmt(bool isGMT); +status_t _user_get_real_time_clock_is_gmt(bool *_isGMT); #ifdef __cplusplus } diff --git a/headers/private/system/syscalls.h b/headers/private/system/syscalls.h index 76380f855a..a3d754dafd 100644 --- a/headers/private/system/syscalls.h +++ b/headers/private/system/syscalls.h @@ -367,10 +367,8 @@ extern status_t _kern_set_timezone(int32 timezoneOffset, bool daylightSavingTime); extern status_t _kern_get_timezone(int32 *_timezoneOffset, bool *_daylightSavingTime); -extern status_t _kern_set_tzfilename(const char *filename, size_t length, - bool isGMT); -extern status_t _kern_get_tzfilename(char *filename, size_t length, - bool *_isGMT); +extern status_t _kern_set_real_time_clock_is_gmt(bool isGMT); +extern status_t _kern_get_real_time_clock_is_gmt(bool *_isGMT); extern bigtime_t _kern_system_time(); extern status_t _kern_snooze_etc(bigtime_t time, int timebase, int32 flags); diff --git a/src/bin/Jamfile b/src/bin/Jamfile index 38602903ba..0d58dffe5c 100644 --- a/src/bin/Jamfile +++ b/src/bin/Jamfile @@ -29,7 +29,6 @@ StdBinCommands cal.c chop.c clear.c - clockconfig.c # csplit.c driveinfo.c # echo.c @@ -83,6 +82,7 @@ StdBinCommands catattr.cpp checkfs.cpp clipboard.cpp + clockconfig.cpp df.cpp diskimage.cpp dpms.cpp @@ -203,7 +203,7 @@ StdBinCommands StdBinCommands checkitout.cpp : be tracker $(TARGET_LIBSUPC++) : $(haiku-utils_rsrc) ; - + #standard commands that need libbe.so, libtracker.so, liblocale.so StdBinCommands filepanel.cpp diff --git a/src/bin/clockconfig.c b/src/bin/clockconfig.c deleted file mode 100644 index efa0318c33..0000000000 --- a/src/bin/clockconfig.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2004, Jérôme Duval, jerome.duval@free.fr. - * Distributed under the terms of the MIT License. - */ - - -#include -#include -#include -#include -#include - - -#ifdef COMPILE_FOR_R5 -void _kset_tzfilename_(const char *name, size_t length, bool isGMT); -void _kset_tzspecs_(uint32 timezone_offset, bool dst_observed); - -# define _kern_set_tzfilename _kset_tzfilename_ -# define _kern_set_timezone _kset_tzspecs_ -#else -# include -#endif - - -int -main(int argc, char **argv) -{ - char path[B_PATH_NAME_LENGTH]; - char link[B_PATH_NAME_LENGTH]; - FILE *file; - bool isGMT = false; - struct tm* tm = NULL; - time_t t; - - printf("Looking for RTC settings file\n"); - if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, path, B_PATH_NAME_LENGTH) != B_OK) { - fprintf(stderr, "%s: can't find settings directory\n", argv[0]); - exit(1); - } - - strcat(path, "/RTC_time_settings"); - file = fopen(path, "r"); - if (file != NULL) { - char string[10]; - fscanf(file, "%s", string); - isGMT = (strncmp(string, "local", 5) != 0); - fclose(file); - } else { - fprintf(stderr, "%s: can't read RTC settings\n", argv[0]); - printf("%s: No knowledge about contents of RTC\n", argv[0]); - - // we don't care if not RTC settings is found, default is local - } - - printf("%s: RTC stores %s time.\n", argv[0], isGMT ? "GMT" : "local" ); - - if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, path, B_PATH_NAME_LENGTH) != B_OK) { - fprintf(stderr, "can't find settings directory\n"); - return 1; - } - - strcat(path, "/timezone"); - - printf("Looking for %s file\n", path); - if (readlink(path, link, B_PATH_NAME_LENGTH) > 0) { - printf("%s: Setting timezone to '%s'\n", argv[0], link); - - _kern_set_tzfilename(link, strlen(link), isGMT); - - tzset(); - - time(&t); - tm = localtime(&t); - _kern_set_timezone(tm->tm_gmtoff, tm->tm_isdst); - } else { - fprintf(stderr, "%s: can't read link for timezone\n", argv[0]); - printf("%s: No timezone setting.\n", argv[0]); - } - - return 0; -} diff --git a/src/bin/clockconfig.cpp b/src/bin/clockconfig.cpp new file mode 100644 index 0000000000..5466cef80f --- /dev/null +++ b/src/bin/clockconfig.cpp @@ -0,0 +1,92 @@ +/* + * Copyright 2004, Jérôme Duval, jerome.duval@free.fr. + * Copyright 2010, Oliver Tappe + * All rights reserved. Distributed under the terms of the MIT License. + */ + + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + + +static char* program; + + +void +setRealTimeClockIsGMT(BPath path) +{ + path.Append("RTC_time_settings"); + BFile file; + status_t status = file.SetTo(path.Path(), B_READ_ONLY); + if (status != B_OK) { + fprintf(stderr, "%s: can't open RTC settings file\n", program); + return; + } + + char buffer[10]; + ssize_t bytesRead = file.Read(buffer, sizeof(buffer)); + if (bytesRead < 0) { + fprintf(stderr, "%s: unable to read RTC settings file\n", program); + return; + } + bool isGMT = strncmp(buffer, "local", 5) != 0; + + _kern_set_real_time_clock_is_gmt(isGMT); + printf("RTC stores %s time.\n", isGMT ? "GMT" : "local" ); +} + + +void +setTimeZoneOffset(BPath path) +{ + path.Append("Time settings"); + BFile file; + status_t status = file.SetTo(path.Path(), B_READ_ONLY); + if (status != B_OK) { + fprintf(stderr, "%s: can't open Time settings file\n", program); + return; + } + + BMessage settings; + status = settings.Unflatten(&file); + if (status != B_OK) { + fprintf(stderr, "%s: unable to parse Time settings file\n", program); + return; + } + int32 timeZoneOffset; + if (settings.FindInt32("offset", &timeZoneOffset) != B_OK) { + fprintf(stderr, "%s: no timezone offset found\n", program); + return; + } + + _kern_set_timezone(timeZoneOffset, false); + printf("timezone offset is %ld seconds from GMT.\n", timeZoneOffset); +} + + +int +main(int argc, char **argv) +{ + program = argv[0]; + + BPath path; + status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); + if (status != B_OK) { + fprintf(stderr, "%s: can't find settings directory\n", program); + return EXIT_FAILURE; + } + + setRealTimeClockIsGMT(path); + setTimeZoneOffset(path); + + return 0; +} diff --git a/src/preferences/time/DateTimeView.cpp b/src/preferences/time/DateTimeView.cpp index e8d3cfae11..a36999f037 100644 --- a/src/preferences/time/DateTimeView.cpp +++ b/src/preferences/time/DateTimeView.cpp @@ -319,7 +319,7 @@ DateTimeView::_UpdateGmtSettings() { _WriteRTCSettings(); - _kern_set_tzfilename(NULL, 0, fUseGmtTime); + _kern_set_real_time_clock_is_gmt(fUseGmtTime); } diff --git a/src/system/kernel/real_time_clock.cpp b/src/system/kernel/real_time_clock.cpp index e0739122c8..0e5570689b 100644 --- a/src/system/kernel/real_time_clock.cpp +++ b/src/system/kernel/real_time_clock.cpp @@ -31,7 +31,6 @@ static struct real_time_data *sRealTimeData; static bool sIsGMT = false; -static char sTimezoneFilename[B_PATH_NAME_LENGTH] = ""; static bigtime_t sTimezoneOffset = 0; static bool sDaylightSavingTime = false; @@ -290,19 +289,13 @@ _user_get_timezone(time_t *_timezoneOffset, bool *_daylightSavingTime) status_t -_user_set_tzfilename(const char *filename, size_t length, bool isGMT) +_user_set_real_time_clock_is_gmt(bool isGMT) { // store previous value bool wasGMT = sIsGMT; if (geteuid() != 0) return B_NOT_ALLOWED; - if (filename != NULL && length > 0) { - if (!IS_USER_ADDRESS(filename) - || user_strlcpy(sTimezoneFilename, filename, B_PATH_NAME_LENGTH) < 0) - return B_BAD_ADDRESS; - } - sIsGMT = isGMT; if (wasGMT != sIsGMT) { @@ -316,16 +309,11 @@ _user_set_tzfilename(const char *filename, size_t length, bool isGMT) status_t -_user_get_tzfilename(char *userFilename, size_t length, bool *_userIsGMT) +_user_get_real_time_clock_is_gmt(bool *_userIsGMT) { - if ((userFilename == NULL || length == 0) && _userIsGMT == NULL) + if (_userIsGMT == NULL) return B_BAD_VALUE; - if (userFilename != NULL - && (!IS_USER_ADDRESS(userFilename) - || user_strlcpy(userFilename, sTimezoneFilename, length) < 0)) - return B_BAD_ADDRESS; - if (_userIsGMT != NULL && (!IS_USER_ADDRESS(_userIsGMT) || user_memcpy(_userIsGMT, &sIsGMT, sizeof(bool)) != B_OK)) diff --git a/src/system/libroot/posix/time/localtime.c b/src/system/libroot/posix/time/localtime.c index aee2df89bf..41a0cd7662 100644 --- a/src/system/libroot/posix/time/localtime.c +++ b/src/system/libroot/posix/time/localtime.c @@ -1173,7 +1173,7 @@ tzset P((void)) name = getenv("TZ"); if (name == NULL) { - _kern_get_tzfilename(tzfilename, sizeof(tzfilename), &is_gmt); +// _kern_get_tzfilename(tzfilename, sizeof(tzfilename), &is_gmt); if (tzfilename[0] == '\0') { tzsetwall(); return;