summary refs log tree commit diff
path: root/doc/fdreserve.html
blob: ef047dc1eccc290d78c5346023c7ee1a5f41c238 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
    <title>execline: the fdreserve program</title>
    <meta name="Description" content="execline: the fdreserve program" />
    <meta name="Keywords" content="execline command fdreserve file descriptor" />
    <!-- <link rel="stylesheet" type="text/css" href="http://skarnet.org/default.css" /> -->
  </head>
<body>

<p>
<a href="index.html">execline</a><br />
<a href="http://skarnet.org/software/">Software</a><br />
<a href="http://skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>fdreserve</tt> program </h1>

<p>
<tt>fdreserve</tt> updates the environment with file descriptors that
are guaranteed safe to use, then executes a program.
</p>

<h2> Interface </h2>

<pre>
     fdreserve <em>n</em> <em>prog...</em>
</pre>

<ul>
 <li> <tt>fdreserve</tt> tries to reserve <em>n</em> file descriptors. </li>
 <li> <tt>fdreserve</tt> sets the <tt>FD0</tt>, <tt>FD1</tt>, ...,
<tt>FD<em>n-1</em></tt> environment variables: each FD<em>i</em> contains a
valid file descriptor, that can be safely opened. </li>
 <li> <tt>fdreserve</tt> then execs into <em>prog</em> with its arguments.
</ul>

<h2> Common use </h2>

<p>
<tt>fdreserve</tt> can be used when you do not want to hardcode file
descriptors in your scripts. For instance, to create a pipe, you could
use:
</p>

<pre>
 #!/command/execlineb
 fdreserve 2
 multisubstitute
 {
   importas fdr FD0
   importas fdw FD1
  }
 piperw $fdr $fdw
 <em>prog...</em>
</pre>

<p>
 Warning: <tt>fdreserve</tt> does not allocate descriptors, it merely returns
descriptors that are free at the time it is run. A program like
</p>

<pre>
 #!/command/execlineb
 fdreserve 3
 multisubstitute
 {
   importas fdr FD0
   importas fdw FD1
 }
 piperw $fdr $fdw
 fdreserve 1
 multisubstitute
 {
   importas oldfd FD2
   importas newfd FD0
 }
 <em>prog...</em>
</pre>

<p>
may fail, because <em>oldfd</em> and <em>newfd</em> may be the same.
To avoid that, you should make sure that all descriptors returned by
<tt>fdreserve</tt> are actually allocated before calling <tt>fdreserve</tt>
again.
(Thanks to <a href="http://code.dogmap.org/">Paul Jarc</a> for having
spotted that case.)
</p>

</body>
</html>