VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 2 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: 10.0 KB
Line 
1/* $Id: RTTimeZoneGetCurrent-posix.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - RTTimeZoneGetCurrent, POSIX.
4 */
5
6/*
7 * Copyright (C) 2020-2024 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 *
83 * @note File format is documented in RFC-8536.
84 */
85static int rtIsValidTimeZoneFile(const char *pszTimeZone)
86{
87 if (pszTimeZone == NULL || *pszTimeZone == '\0' || *pszTimeZone == '/')
88 return VERR_INVALID_PARAMETER;
89
90 int rc = RTStrValidateEncoding(pszTimeZone);
91 if (RT_SUCCESS(rc))
92 {
93 /* construct full pathname of the time zone file */
94 char szTZPath[RTPATH_MAX];
95 rc = RTPathJoin(szTZPath, sizeof(szTZPath), TZDIR, pszTimeZone);
96 if (RT_SUCCESS(rc))
97 {
98 /* open the time zone file and check that it begins with the correct magic number */
99 RTFILE hFile = NIL_RTFILE;
100 rc = RTFileOpen(&hFile, szTZPath, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
101 if (RT_SUCCESS(rc))
102 {
103 char achTZBuf[sizeof(TZ_MAGIC)];
104 rc = RTFileRead(hFile, achTZBuf, sizeof(achTZBuf), NULL);
105 RTFileClose(hFile);
106 if (RT_SUCCESS(rc))
107 {
108 if (memcmp(achTZBuf, RT_STR_TUPLE(TZ_MAGIC)) == 0)
109 rc = VINF_SUCCESS;
110 else
111 rc = VERR_INVALID_MAGIC;
112 }
113 }
114 }
115 }
116
117 return rc;
118}
119
120
121/**
122 * Return the system time zone.
123 *
124 * @returns IPRT status code.
125 * @param pszName The buffer to return the time zone in.
126 * @param cbName The size of the pszName buffer.
127 */
128RTDECL(int) RTTimeZoneGetCurrent(char *pszName, size_t cbName)
129{
130 int rc = RTEnvGetEx(RTENV_DEFAULT, "TZ", pszName, cbName, NULL);
131 if (RT_SUCCESS(rc))
132 {
133 /*
134 * $TZ can have two different formats and one of them doesn't specify
135 * a time zone database file under <tzfile.h>:TZDIR but since all
136 * current callers of this routine expect a time zone filename we do
137 * the validation check here so that if it is invalid then we fall back
138 * to the other mechanisms to return the system's current time zone.
139 */
140 if (*pszName == ':') /* POSIX allows $TZ to begin with a colon (:) so we allow for that here */
141 memmove(pszName, pszName + 1, strlen(pszName));
142 /** @todo this isn't perfect for absolute paths... Should probably try treat
143 * it like /etc/localtime. */
144 rc = rtIsValidTimeZoneFile(pszName);
145 if (RT_SUCCESS(rc))
146 return rc;
147 }
148 else if (rc != VERR_ENV_VAR_NOT_FOUND)
149 return rc;
150
151 /*
152 * /etc/localtime is a symbolic link to the system time zone on many OSes
153 * including Solaris, macOS, Ubuntu, RH/OEL 6 and later, Arch Linux, NetBSD,
154 * and etc. We extract the time zone pathname relative to TZDIR defined in
155 * <tzfile.h> which is normally /usr/share/zoneinfo.
156 *
157 * N.B. Some OSes have /etc/localtime as a regular file instead of a
158 * symlink and while we could trawl through all the files under TZDIR
159 * looking for a match we instead fallback to other popular mechanisms of
160 * specifying the system-wide time zone for the sake of simplicity.
161 */
162 char szBuf[RTPATH_MAX];
163 const char *pszPath = PATH_LOCALTIME;
164 if (RTSymlinkExists(pszPath))
165 {
166 /* the contents of the symink may contain '..' or other links */
167 char szLinkPathReal[RTPATH_MAX];
168 rc = RTPathReal(pszPath, szLinkPathReal, sizeof(szLinkPathReal));
169 if (RT_SUCCESS(rc))
170 {
171 rc = RTPathReal(TZDIR, szBuf, sizeof(szBuf));
172 AssertRC(rc);
173 if (RT_SUCCESS(rc))
174 {
175 Assert(RTPathStartsWith(szLinkPathReal, szBuf));
176 if (RTPathStartsWith(szLinkPathReal, szBuf))
177 {
178 /* <tzfile.h>:TZDIR doesn't include a trailing slash */
179 const char *pszTimeZone = &szLinkPathReal[strlen(szBuf) + 1];
180 rc = rtIsValidTimeZoneFile(pszTimeZone);
181 if (RT_SUCCESS(rc))
182 return RTStrCopy(pszName, cbName, pszTimeZone);
183 }
184 }
185 }
186 }
187
188 /*
189 * /etc/timezone is a regular file consisting of a single line containing
190 * the time zone (e.g. Europe/London or Etc/UTC or etc.) and is used by a
191 * variety of Linux distros such as Ubuntu, Gentoo, Debian, and etc.
192 * The equivalent on FreeBSD is /var/db/zoneinfo.
193 */
194 pszPath = PATH_TIMEZONE;
195 if (RTFileExists(pszPath))
196 {
197 RTFILE hFile = NIL_RTFILE;
198 rc = RTFileOpen(&hFile, PATH_TIMEZONE, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
199 if (RT_SUCCESS(rc))
200 {
201 size_t cbRead = 0;
202 rc = RTFileRead(hFile, szBuf, sizeof(szBuf), &cbRead);
203 RTFileClose(hFile);
204 if (RT_SUCCESS(rc) && cbRead > 0)
205 {
206 /* Get the first line and strip it. */
207 szBuf[RT_MIN(sizeof(szBuf) - 1, cbRead)] = '\0';
208 size_t const offNewLine = RTStrOffCharOrTerm(szBuf, '\n');
209 szBuf[offNewLine] = '\0';
210 const char *pszTimeZone = RTStrStrip(szBuf);
211
212 rc = rtIsValidTimeZoneFile(pszTimeZone);
213 if (RT_SUCCESS(rc))
214 return RTStrCopy(pszName, cbName, pszTimeZone);
215 }
216 }
217 }
218
219 /*
220 * Older versions of RedHat / OEL don't have /etc/localtime as a symlink or
221 * /etc/timezone but instead have /etc/sysconfig/clock which contains a line
222 * of the syntax ZONE=Europe/London or ZONE="Europe/London" amongst other entries.
223 */
224 pszPath = PATH_SYSCONFIG_CLOCK;
225 if (RTFileExists(pszPath))
226 {
227 PRTSTREAM pStrm;
228 rc = RTStrmOpen(pszPath, "r", &pStrm);
229 if (RT_SUCCESS(rc))
230 {
231 while (RT_SUCCESS(rc = RTStrmGetLine(pStrm, szBuf, sizeof(szBuf))))
232 {
233 static char const s_szVarEq[] = "ZONE=";
234 char *pszStart = RTStrStrip(szBuf);
235 if (memcmp(pszStart, RT_STR_TUPLE(s_szVarEq)) == 0)
236 {
237 char *pszTimeZone = &pszStart[sizeof(s_szVarEq) - 1];
238
239 /* Drop any quoting before using the value, assuming it is plain stuff: */
240 if (*pszTimeZone == '\"' || *pszTimeZone == '\'')
241 {
242 pszTimeZone++;
243 size_t const cchTimeZone = strlen(pszTimeZone);
244 if (cchTimeZone && (pszTimeZone[cchTimeZone - 1] == '"' || pszTimeZone[cchTimeZone - 1] == '\''))
245 pszTimeZone[cchTimeZone - 1] = '\0';
246 }
247
248 rc = rtIsValidTimeZoneFile(pszTimeZone);
249 if (RT_SUCCESS(rc))
250 {
251 RTStrmClose(pStrm);
252 return RTStrCopy(pszName, cbName, pszTimeZone);
253 }
254 }
255 }
256 RTStrmClose(pStrm);
257 }
258 }
259
260 return rc;
261}
262
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette