]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/time_namespaces.7
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man7 / time_namespaces.7
CommitLineData
5bed06a9
MK
1.\" Copyright (c) 2020 by Michael Kerrisk <mtk.manpages@gmail.com>
2.\"
5fbde956 3.\" SPDX-License-Identifier: Linux-man-pages-copyleft
5bed06a9
MK
4.\"
5.\"
4c1c5274 6.TH time_namespaces 7 (date) "Linux man-pages (unreleased)"
5bed06a9
MK
7.SH NAME
8time_namespaces \- overview of Linux time namespaces
9.SH DESCRIPTION
10Time namespaces virtualize the values of two system clocks:
22356d97 11.IP \(bu 3
1ae6b2c7 12.B CLOCK_MONOTONIC
5bed06a9 13(and likewise
1ae6b2c7 14.B CLOCK_MONOTONIC_COARSE
5bed06a9
MK
15and
16.BR CLOCK_MONOTONIC_RAW ),
17a nonsettable clock that represents monotonic time since\(emas
18described by POSIX\(em"some unspecified point in the past".
19.IP \(bu
1ae6b2c7 20.B CLOCK_BOOTTIME
5bed06a9
MK
21(and likewise
22.BR CLOCK_BOOTTIME_ALARM ),
1840148b 23a nonsettable clock that is identical to
5bed06a9
MK
24.BR CLOCK_MONOTONIC ,
25except that it also includes any time that the system is suspended.
26.PP
27Thus, the processes in a time namespace share per-namespace values
28for these clocks.
29This affects various APIs that measure against these clocks, including:
cf406439 30.BR clock_gettime (2),
5bed06a9
MK
31.BR clock_nanosleep (2),
32.BR nanosleep (2),
cf406439
MK
33.BR timer_settime (2),
34.BR timerfd_settime (2),
5bed06a9
MK
35and
36.IR /proc/uptime .
37.PP
38Currently, the only way to create a time namespace is by calling
39.BR unshare (2)
40with the
1ae6b2c7 41.B CLONE_NEWTIME
5bed06a9
MK
42flag.
43This call creates a new time namespace but does
44.I not
45place the calling process in the new namespace.
46Instead, the calling process's
47subsequently created children are placed in the new namespace.
48This allows clock offsets (see below) for the new namespace
49to be set before the first process is placed in the namespace.
50The
1ae6b2c7 51.IR /proc/ pid /ns/time_for_children
5bed06a9
MK
52symbolic link shows the time namespace in which
53the children of a process will be created.
cf406439
MK
54(A process can use a file descriptor opened on
55this symbolic link in a call to
56.BR setns (2)
57in order to move into the namespace.)
5bed06a9
MK
58.\"
59.SS /proc/PID/timens_offsets
60Associated with each time namespace are offsets,
61expressed with respect to the initial time namespace,
1840148b
MK
62that define the values of the monotonic and
63boot-time clocks in that namespace.
5bed06a9
MK
64These offsets are exposed via the file
65.IR /proc/PID/timens_offsets .
66Within this file,
67the offsets are expressed as lines consisting of
68three space-delimited fields:
69.PP
70.in +4n
71.EX
72<clock-id> <offset-secs> <offset-nanosecs>
73.EE
74.in
75.PP
76The
77.I clock-id
0953c1b4
MK
78is a string that identifies the clock whose offsets are being shown.
79This field is either
80.IR monotonic ,
81for
5bed06a9 82.BR CLOCK_MONOTONIC ,
0953c1b4
MK
83or
84.IR boottime ,
85for
5bed06a9
MK
86.BR CLOCK_BOOTTIME .
87The remaining fields express the offset (seconds plus nanoseconds) for the
88clock in this time namespace.
89These offsets are expressed relative to the clock values in
90the initial time namespace.
cf406439
MK
91The
92.I offset-secs
3685736f
MK
93value can be negative, subject to restrictions noted below;
94.I offset-nanosecs
95is an unsigned value.
cf406439
MK
96.PP
97In the initial time namespace, the contents of the
98.I timens_offsets
99file are as follows:
5bed06a9
MK
100.PP
101.in +4n
102.EX
103$ \fBcat /proc/self/timens_offsets\fP
0953c1b4
MK
104monotonic 0 0
105boottime 0 0
5bed06a9
MK
106.EE
107.in
108.PP
109In a new time namespace that has had no member processes,
110the clock offsets can be modified by writing newline-terminated
cf406439
MK
111records of the same form to the
112.I timens_offsets
113file.
5bed06a9
MK
114The file can be written to multiple times,
115but after the first process has been created in or has entered the namespace,
116.BR write (2)s
117on this file fail with the error
118.BR EACCES .
119In order to write to the
1ae6b2c7 120.I timens_offsets
5bed06a9 121file, a process must have the
1ae6b2c7 122.B CAP_SYS_TIME
5bed06a9
MK
123capability in the user namespace that owns the time namespace.
124.PP
3685736f
MK
125Writes to the
126.I timens_offsets
127file can fail with the following errors:
128.TP
129.B EINVAL
130An
131.I offset-nanosecs
132value is greater than 999,999,999.
133.TP
134.B EINVAL
135A
136.I clock-id
137value is not valid.
138.TP
139.B EPERM
140The caller does not have the
1ae6b2c7 141.B CAP_SYS_TIME
3685736f
MK
142capability.
143.TP
144.B ERANGE
145An
146.I offset-secs
147value is out of range.
148In particular;
149.RS
22356d97 150.IP \(bu 3
3685736f
MK
151.I offset-secs
152can't be set to a value which would make the current
153time on the corresponding clock inside the namespace a negative value; and
154.IP \(bu
155.I offset-secs
156can't be set to a value such that the time on the corresponding clock
157inside the namespace would exceed half of the value of the kernel constant
1ae6b2c7 158.B KTIME_SEC_MAX
3685736f
MK
159(this limits the clock value to a maximum of approximately 146 years).
160.RE
161.PP
5bed06a9
MK
162In a new time namespace created by
163.BR unshare (2),
164the contents of the
165.I timens_offsets
166file are inherited from the time namespace of the creating process.
167.SH NOTES
5bed06a9
MK
168Use of time namespaces requires a kernel that is configured with the
169.B CONFIG_TIME_NS
170option.
171.PP
172Note that time namespaces do not virtualize the
1ae6b2c7 173.B CLOCK_REALTIME
5bed06a9
MK
174clock.
175Virtualization of this clock was avoided for reasons of complexity
176and overhead within the kernel.
177.PP
0953c1b4
MK
178For compatibility with the initial implementation, when writing a
179.I clock-id
180to the
1ae6b2c7 181.IR /proc/ pid /timens_offsets
0953c1b4
MK
182file, the numerical values of the IDs can be written
183instead of the symbolic names show above; i.e., 1 instead of
184.IR monotonic ,
185and 7 instead of
186.IR boottime .
713c9be8 187For readability, the use of the symbolic names over the numbers is preferred.
0953c1b4 188.PP
5bed06a9
MK
189The motivation for adding time namespaces was to allow
190the monotonic and boot-time clocks to maintain consistent values
191during container migration and checkpoint/restore.
a14af333 192.SH EXAMPLES
5bed06a9
MK
193The following shell session demonstrates the operation of time namespaces.
194We begin by displaying the inode number of the time namespace
195of a shell in the initial time namespace:
196.PP
197.in +4n
198.EX
199$ \fBreadlink /proc/$$/ns/time\fP
200time:[4026531834]
201.EE
202.in
203.PP
204Continuing in the initial time namespace, we display the system uptime using
205.BR uptime (1)
206and use the
207.I clock_times
208example program shown in
209.BR clock_getres (2)
210to display the values of various clocks:
211.PP
212.in +4n
213.EX
214$ \fBuptime \-\-pretty\fP
215up 21 hours, 17 minutes
216$ \fB./clock_times\fP
217CLOCK_REALTIME : 1585989401.971 (18356 days + 8h 36m 41s)
218CLOCK_TAI : 1585989438.972 (18356 days + 8h 37m 18s)
219CLOCK_MONOTONIC: 56338.247 (15h 38m 58s)
220CLOCK_BOOTTIME : 76633.544 (21h 17m 13s)
221.EE
222.in
223.PP
224We then use
225.BR unshare (1)
226to create a time namespace and execute a
227.BR bash (1)
228shell.
229From the new shell, we use the built-in
230.B echo
231command to write records to the
232.I timens_offsets
233file adjusting the offset for the
234.B CLOCK_MONOTONIC
235clock forward 2 days
236and the offset for the
237.B CLOCK_BOOTTIME
238clock forward 7 days:
239.PP
240.in +4n
241.EX
242$ \fBPS1="ns2# " sudo unshare \-T \-\- bash \-\-norc\fP
0953c1b4
MK
243ns2# \fBecho "monotonic $((2*24*60*60)) 0" > /proc/$$/timens_offsets\fP
244ns2# \fBecho "boottime $((7*24*60*60)) 0" > /proc/$$/timens_offsets\fP
5bed06a9
MK
245.EE
246.in
247.PP
248Above, we started the
249.BR bash (1)
250shell with the
251.B \-\-norc
252options so that no start-up scripts were executed.
253This ensures that no child processes are created from the
254shell before we have a chance to update the
255.I timens_offsets
256file.
257.PP
258We then use
259.BR cat (1)
260to display the contents of the
261.I timens_offsets
262file.
263The execution of
264.BR cat (1)
265creates the first process in the new time namespace,
266after which further attempts to update the
267.I timens_offsets
268file produce an error.
269.PP
270.in +4n
271.EX
272ns2# \fBcat /proc/$$/timens_offsets\fP
0953c1b4
MK
273monotonic 172800 0
274boottime 604800 0
275ns2# \fBecho "boottime $((9*24*60*60)) 0" > /proc/$$/timens_offsets\fP
5bed06a9
MK
276bash: echo: write error: Permission denied
277.EE
278.in
279.PP
280Continuing in the new namespace, we execute
281.BR uptime (1)
282and the
283.I clock_times
284example program:
285.PP
286.in +4n
287.EX
288ns2# \fBuptime \-\-pretty\fP
289up 1 week, 21 hours, 18 minutes
290ns2# \fB./clock_times\fP
291CLOCK_REALTIME : 1585989457.056 (18356 days + 8h 37m 37s)
292CLOCK_TAI : 1585989494.057 (18356 days + 8h 38m 14s)
293CLOCK_MONOTONIC: 229193.332 (2 days + 15h 39m 53s)
294CLOCK_BOOTTIME : 681488.629 (7 days + 21h 18m 8s)
295.EE
296.in
297.PP
298From the above output, we can see that the monotonic
1840148b 299and boot-time clocks have different values in the new time namespace.
5bed06a9
MK
300.PP
301Examining the
1ae6b2c7 302.IR /proc/ pid /ns/time
5bed06a9 303and
1ae6b2c7 304.IR /proc/ pid /ns/time_for_children
5bed06a9 305symbolic links, we see that the shell is a member of the initial time
1840148b 306namespace, but its children are created in the new namespace.
5bed06a9
MK
307.PP
308.in +4n
309.EX
310ns2# \fBreadlink /proc/$$/ns/time\fP
311time:[4026531834]
312ns2# \fBreadlink /proc/$$/ns/time_for_children\fP
313time:[4026532900]
314ns2# \fBreadlink /proc/self/ns/time\fP # Creates a child process
315time:[4026532900]
316.EE
317.in
318.PP
319Returning to the shell in the initial time namespace,
320we see that the monotonic and boot-time clocks
321are unaffected by the
322.I timens_offsets
323changes that were made in the other time namespace:
324.PP
325.in +4n
326.EX
327$ \fBuptime \-\-pretty\fP
328up 21 hours, 19 minutes
329$ \fB./clock_times\fP
330CLOCK_REALTIME : 1585989401.971 (18356 days + 8h 38m 51s)
331CLOCK_TAI : 1585989438.972 (18356 days + 8h 39m 28s)
332CLOCK_MONOTONIC: 56338.247 (15h 41m 8s)
333CLOCK_BOOTTIME : 76633.544 (21h 19m 23s)
334.EE
335.in
336.SH SEE ALSO
337.BR nsenter (1),
338.BR unshare (1),
a8433b3b 339.BR clock_settime (2),
5bed06a9
MK
340.\" clone3() support for time namespaces is a work in progress
341.\" .BR clone3 (2),
342.BR setns (2),
343.BR unshare (2),
344.BR namespaces (7),
345.BR time (7)