VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/RTTimeZoneGetCurrent-posix.cpp@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1/* $Id: RTTimeZoneGetCurrent-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - RTTimeZoneGetCurrent, POSIX.
4 */
5
6/*
7 * Copyright (C) 2020-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/time.h>
42#include "internal/iprt.h"
43
44#include <iprt/env.h>
45#include <iprt/file.h>
46#include <iprt/path.h>
47#include <iprt/string.h>
48#include <iprt/err.h>
49#include <iprt/errcore.h>
50#include <iprt/types.h>
51#include <iprt/symlink.h>
52#include <iprt/stream.h>
53
54#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
55# include <tzfile.h>
56#else
57# define TZDIR "/usr/share/zoneinfo"
58# define TZ_MAGIC "TZif"
59#endif
60
61
62/*********************************************************************************************************************************
63* Defined Constants And Macros *
64*********************************************************************************************************************************/
65#define PATH_LOCALTIME "/etc/localtime"
66#if defined(RT_OS_FREEBSD)
67# define PATH_TIMEZONE "/var/db/zoneinfo"
68#else
69# define PATH_TIMEZONE "/etc/timezone"
70#endif
71#define PATH_SYSCONFIG_CLOCK "/etc/sysconfig/clock"
72
73
74/**
75 * Checks if a time zone database file is valid by verifying it begins with
76 * TZ_MAGIC.
77 *
78 * @returns IPRT status code.
79 * @param pszTimezone The time zone database file relative to
80 * <tzfile.h>:TZDIR (normally /usr/share/zoneinfo),
81 * e.g. Europe/London, or Etc/UTC, or UTC, or etc.
82 */
83static int rtIsValidTimeZoneFile(const char *pszTimeZone)
84{
85 if (pszTimeZone == NULL || *pszTimeZone == '\0' || *pszTimeZone == '/')
86 return VERR_INVALID_PARAMETER;
87
88 int rc = RTStrValidateEncoding(pszTimeZone);
89 if (RT_SUCCESS(rc))
90 {
91 /* construct full pathname of the time zone file */
92 char szTZPath[RTPATH_MAX];
93 rc = RTPathJoin(szTZPath, sizeof(szTZPath), TZDIR, pszTimeZone);
94 if (RT_SUCCESS(rc))
95 {
96 /* open the time zone file and check that it begins with the correct magic number */
97 RTFILE hFile = NIL_RTFILE;
98 rc = RTFileOpen(&hFile, szTZPath, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
99 if (RT_SUCCESS(rc))
100 {
101 char achTZBuf[sizeof(TZ_MAGIC)];
102 rc = RTFileRead(hFile, achTZBuf, sizeof(achTZBuf), NULL);
103 RTFileClose(hFile);
104 if (RT_SUCCESS(rc))
105 {
106 if (memcmp(achTZBuf, RT_STR_TUPLE(TZ_MAGIC)) == 0)
107 rc = VINF_SUCCESS;
108 else
109 rc = VERR_INVALID_MAGIC;
110 }
111 }
112 }
113 }
114
115 return rc;
116}
117
118
119/**
120 * Return the system time zone.
121 *
122 * @returns IPRT status code.
123 * @param pszName The buffer to return the time zone in.
124 * @param cbName The size of the pszName buffer.
125 */
126RTDECL(int) RTTimeZoneGetCurrent(char *pszName, size_t cbName)
127{
128 int rc = RTEnvGetEx(RTENV_DEFAULT, "TZ", pszName, cbName, NULL);
129 if (RT_SUCCESS(rc))
130 {
131 /*
132 * $TZ can have two different formats and one of them doesn't specify
133 * a time zone database file under <tzfile.h>:TZDIR but since all
134 * current callers of this routine expect a time zone filename we do
135 * the validation check here so that if it is invalid then we fall back
136 * to the other mechanisms to return the system's current time zone.
137 */
138 if (*pszName == ':') /* POSIX allows $TZ to begin with a colon (:) so we allow for that here */
139 memmove(pszName, pszName + 1, strlen(pszName));
140 /** @todo this isn't perfect for absolute paths... Should probably try treat
141 * it like /etc/localtime. */
142 rc = rtIsValidTimeZoneFile(pszName);
143 if (RT_SUCCESS(rc))
144 return rc;
145 }
146 else if (rc != VERR_ENV_VAR_NOT_FOUND)
147 return rc;
148
149 /*
150 * /etc/localtime is a symbolic link to the system time zone on many OSes
151 * including Solaris, macOS, Ubuntu, RH/OEL 6 and later, Arch Linux, NetBSD,
152 * and etc. We extract the time zone pathname relative to TZDIR defined in
153 * <tzfile.h> which is normally /usr/share/zoneinfo.
154 *
155 * N.B. Some OSes have /etc/localtime as a regular file instead of a
156 * symlink and while we could trawl through all the files under TZDIR
157 * looking for a match we instead fallback to other popular mechanisms of
158 * specifying the system-wide time zone for the sake of simplicity.
159 */
160 char szBuf[RTPATH_MAX];
161 const char *pszPath = PATH_LOCALTIME;
162 if (RTSymlinkExists(pszPath))
163 {
164 /* the contents of the symink may contain '..' or other links */
165 char szLinkPathReal[RTPATH_MAX];
166 rc = RTPathReal(pszPath, szLinkPathReal, sizeof(szLinkPathReal));
167 if (RT_SUCCESS(rc))
168 {
169 rc = RTPathReal(TZDIR, szBuf, sizeof(szBuf));
170 AssertRC(rc);
171 if (RT_SUCCESS(rc))
172 {
173 Assert(RTPathStartsWith(szLinkPathReal, szBuf));
174 if (RTPathStartsWith(szLinkPathReal, szBuf))
175 {
176 /* <tzfile.h>:TZDIR doesn't include a trailing slash */
177 const char *pszTimeZone = &szLinkPathReal[strlen(szBuf) + 1];
178 rc = rtIsValidTimeZoneFile(pszTimeZone);
179 if (RT_SUCCESS(rc))
180 return RTStrCopy(pszName, cbName, pszTimeZone);
181 }
182 }
183 }
184 }
185
186 /*
187 * /etc/timezone is a regular file consisting of a single line containing
188 * the time zone (e.g. Europe/London or Etc/UTC or etc.) and is used by a
189 * variety of Linux distros such as Ubuntu, Gentoo, Debian, and etc.
190 * The equivalent on FreeBSD is /var/db/zoneinfo.
191 */
192 pszPath = PATH_TIMEZONE;
193 if (RTFileExists(pszPath))
194 {
195 RTFILE hFile = NIL_RTFILE;
196 rc = RTFileOpen(&hFile, PATH_TIMEZONE, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
197 if (RT_SUCCESS(rc))
198 {
199 size_t cbRead = 0;
200 rc = RTFileRead(hFile, szBuf, sizeof(szBuf), &cbRead);
201 RTFileClose(hFile);
202 if (RT_SUCCESS(rc) && cbRead > 0)
203 {
204 /* Get the first line and strip it. */
205 szBuf[RT_MIN(sizeof(szBuf) - 1, cbRead)] = '\0';
206 size_t const offNewLine = RTStrOffCharOrTerm(szBuf, '\n');
207 szBuf[offNewLine] = '\0';
208 const char *pszTimeZone = RTStrStrip(szBuf);
209
210 rc = rtIsValidTimeZoneFile(pszTimeZone);
211 if (RT_SUCCESS(rc))
212 return RTStrCopy(pszName, cbName, pszTimeZone);
213 }
214 }
215 }
216
217 /*
218 * Older versions of RedHat / OEL don't have /etc/localtime as a symlink or
219 * /etc/timezone but instead have /etc/sysconfig/clock which contains a line
220 * of the syntax ZONE=Europe/London amongst other entries.
221 */
222 pszPath = PATH_SYSCONFIG_CLOCK;
223 if (RTFileExists(pszPath))
224 {
225 PRTSTREAM pStrm;
226 rc = RTStrmOpen(pszPath, "r", &pStrm);
227 if (RT_SUCCESS(rc))
228 {
229 while (RT_SUCCESS(rc = RTStrmGetLine(pStrm, szBuf, sizeof(szBuf))))
230 {
231 static char const s_szVarEq[] = "ZONE=";
232 if (memcmp(szBuf, RT_STR_TUPLE(s_szVarEq)) == 0)
233 {
234 const char *pszTimeZone = &szBuf[sizeof(s_szVarEq) - 1];
235 rc = rtIsValidTimeZoneFile(pszTimeZone);
236 if (RT_SUCCESS(rc))
237 {
238 RTStrmClose(pStrm);
239 return RTStrCopy(pszName, cbName, pszTimeZone);
240 }
241 }
242 }
243 RTStrmClose(pStrm);
244 }
245 }
246
247 return rc;
248}
249
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use