X Tutup
Skip to content

Commit 2651d03

Browse files
committed
man: extend documentation on the unit name escaping logic
1 parent 40b1a32 commit 2651d03

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

man/systemd-escape.xml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
special mode of escaping is applied instead, which assumes the
7474
string is already escaped but will escape everything that
7575
appears obviously non-escaped.</para>
76+
77+
<para>For details on the escaping and unescaping algorithms see the relevant section in
78+
<citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
7679
</refsect1>
7780

7881
<refsect1>
@@ -107,11 +110,12 @@
107110
<term><option>--path</option></term>
108111
<term><option>-p</option></term>
109112

110-
<listitem><para>When escaping or unescaping a string, assume
111-
it refers to a file system path. This eliminates leading,
112-
trailing or duplicate <literal>/</literal> characters
113-
and rejects <literal>.</literal> and <literal>..</literal>
114-
path components.</para></listitem>
113+
<listitem><para>When escaping or unescaping a string, assume it refers to a file system path. This eliminates
114+
leading, trailing or duplicate <literal>/</literal> characters and rejects <literal>.</literal> and
115+
<literal>..</literal> path components. This is particularly useful for generating strings suitable for
116+
unescaping with the <literal>%f</literal> specifier in unit files, see
117+
<citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
118+
</para></listitem>
115119
</varlistentry>
116120

117121
<varlistentry>
@@ -172,6 +176,7 @@ systemd-nspawn@My\x20Container\x201.service systemd-nspawn@containerb.service sy
172176
<title>See Also</title>
173177
<para>
174178
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
179+
<citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
175180
<citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
176181
</para>
177182
</refsect1>

man/systemd.unit.xml

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,6 @@
213213
socket-based activation which make dependencies implicit,
214214
resulting in a both simpler and more flexible system.</para>
215215

216-
<para>Some unit names reflect paths existing in the file system
217-
namespace. Example: a device unit
218-
<filename>dev-sda.device</filename> refers to a device with the
219-
device node <filename noindex='true'>/dev/sda</filename> in the
220-
file system namespace. If this applies, a special way to escape
221-
the path name is used, so that the result is usable as part of a
222-
filename. Basically, given a path, "/" is replaced by "-", and all
223-
other characters which are not ASCII alphanumerics are replaced by
224-
C-style "\x2d" escapes (except that "_" is never replaced and "."
225-
is only replaced when it would be the first character in the
226-
escaped path). The root directory "/" is encoded as single dash,
227-
while otherwise the initial and ending "/" are removed from all
228-
paths during transformation. This escaping is reversible. Properly
229-
escaped paths can be generated using the
230-
<citerefentry><refentrytitle>systemd-escape</refentrytitle><manvolnum>1</manvolnum></citerefentry>
231-
command.</para>
232216

233217
<para>Optionally, units may be instantiated from a
234218
template file at runtime. This allows creation of
@@ -265,6 +249,32 @@
265249

266250
</refsect1>
267251

252+
<refsect1>
253+
<title>String Escaping for Inclusion in Unit Names</title>
254+
255+
<para>Sometimes it is useful to convert arbitrary strings into unit names. To facilitate this, a method of string
256+
escaping is used, in order to map strings containing arbitrary byte values (except NUL) into valid unit names and
257+
their restricted character set. A common special case are unit names that reflect paths to objects in the file
258+
system hierarchy. Example: a device unit <filename>dev-sda.device</filename> refers to a device with the device
259+
node <filename noindex='true'>/dev/sda</filename> in the file system.</para>
260+
261+
<para>The escaping algorithm operates as follows: given a string, any <literal>/</literal> character is replaced by
262+
<literal>-</literal>, and all other characters which are not ASCII alphanumerics or <literal>_</literal> are
263+
replaced by C-style <literal>\x2d</literal> escapes. In addition, <literal>.</literal> is replaced with such a
264+
C-style escape when it would appear as the first character in the escaped string.</para>
265+
266+
<para>When the input qualifies as absolute file system path, this algorithm is extended slightly: the path to the
267+
root directory <literal>/</literal> is encoded as single dash <literal>-</literal>. In addition, any leading,
268+
trailing or duplicate <literal>/</literal> characters are removed from the string before transformation. Example:
269+
<filename>/foo//bar/baz/</filename> becomes <literal>foo-bar-baz</literal>.</para>
270+
271+
<para>This escaping is fully reversible, as long as it is known whether the escaped string was a path (the
272+
unescaping results are different for paths and non-path strings). The
273+
<citerefentry><refentrytitle>systemd-escape</refentrytitle><manvolnum>1</manvolnum></citerefentry> command may be
274+
used to apply and reverse escaping on arbitrary strings. Use <command>systemd-escape --path</command> to escape
275+
path strings, and <command>systemd-escape</command> without <option>--path</option> otherwise.</para>
276+
</refsect1>
277+
268278
<refsect1>
269279
<title>Implicit Dependencies</title>
270280

@@ -1241,7 +1251,7 @@
12411251
<row>
12421252
<entry><literal>%N</literal></entry>
12431253
<entry>Unescaped full unit name</entry>
1244-
<entry>Same as <literal>%n</literal>, but with escaping undone</entry>
1254+
<entry>Same as <literal>%n</literal>, but with escaping undone. This undoes the escaping used when generating unit names from arbitrary strings (see above). </entry>
12451255
</row>
12461256
<row>
12471257
<entry><literal>%p</literal></entry>
@@ -1266,7 +1276,7 @@
12661276
<row>
12671277
<entry><literal>%f</literal></entry>
12681278
<entry>Unescaped filename</entry>
1269-
<entry>This is either the unescaped instance name (if applicable) with <filename>/</filename> prepended (if applicable), or the unescaped prefix name prepended with <filename>/</filename>.</entry>
1279+
<entry>This is either the unescaped instance name (if applicable) with <filename>/</filename> prepended (if applicable), or the unescaped prefix name prepended with <filename>/</filename>. This implements unescaping according to the rules for escaping absolute file system paths discussed above.</entry>
12701280
</row>
12711281
<row>
12721282
<entry><literal>%t</literal></entry>

0 commit comments

Comments
 (0)
X Tutup