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