Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/interp.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 39.7 KB
Line 
1'\"
2'\" Copyright (c) 1995-1996 Sun Microsystems, Inc.
3'\" Copyright (c) 2004 Donal K. Fellows
4'\"
5'\" See the file "license.terms" for information on usage and redistribution
6'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7'\"
8'\" RCS: @(#) $Id: interp.n,v 1.38 2007/12/13 15:22:32 dgp Exp $
9'\"
10.so man.macros
11.TH interp n 7.6 Tcl "Tcl Built-In Commands"
12.BS
13'\" Note:  do not modify the .SH NAME line immediately below!
14.SH NAME
15interp \- Create and manipulate Tcl interpreters
16.SH SYNOPSIS
17\fBinterp \fIsubcommand \fR?\fIarg arg ...\fR?
18.BE
19.SH DESCRIPTION
20.PP
21This command makes it possible to create one or more new Tcl
22interpreters that co-exist with the creating interpreter in the
23same application.  The creating interpreter is called the \fImaster\fR
24and the new interpreter is called a \fIslave\fR.
25A master can create any number of slaves, and each slave can
26itself create additional slaves for which it is master, resulting
27in a hierarchy of interpreters.
28.PP
29Each interpreter is independent from the others: it has its own name
30space for commands, procedures, and global variables.
31A master interpreter may create connections between its slaves and
32itself using a mechanism called an \fIalias\fR.  An \fIalias\fR is
33a command in a slave interpreter which, when invoked, causes a
34command to be invoked in its master interpreter or in another slave
35interpreter.  The only other connections between interpreters are
36through environment variables (the \fBenv\fR variable), which are
37normally shared among all interpreters in the application,
38.VS 8.5
39and by resource limit exceeded callbacks.
40.VE 8.5
41Note that the
42name space for files (such as the names returned by the \fBopen\fR command)
43is no longer shared between interpreters. Explicit commands are provided to
44share files and to transfer references to open files from one interpreter
45to another.
46.PP
47The \fBinterp\fR command also provides support for \fIsafe\fR
48interpreters.  A safe interpreter is a slave whose functions have
49been greatly restricted, so that it is safe to execute untrusted
50scripts without fear of them damaging other interpreters or the
51application's environment. For example, all IO channel creation
52commands and subprocess creation commands are made inaccessible to safe
53interpreters.
54See \fBSAFE INTERPRETERS\fR below for more information on
55what features are present in a safe interpreter.
56The dangerous functionality is not removed from the safe interpreter;
57instead, it is \fIhidden\fR, so that only trusted interpreters can obtain
58access to it. For a detailed explanation of hidden commands, see
59\fBHIDDEN COMMANDS\fR, below.
60The alias mechanism can be used for protected communication (analogous to a
61kernel call) between a slave interpreter and its master.
62See \fBALIAS INVOCATION\fR, below, for more details
63on how the alias mechanism works.
64.PP
65A qualified interpreter name is a proper Tcl lists containing a subset of its
66ancestors in the interpreter hierarchy, terminated by the string naming the
67interpreter in its immediate master. Interpreter names are relative to the
68interpreter in which they are used. For example, if \fBa\fR is a slave of
69the current interpreter and it has a slave \fBa1\fR, which in turn has a
70slave \fBa11\fR, the qualified name of \fBa11\fR in \fBa\fR is the list
71\fBa1 a11\fR.
72.PP
73The \fBinterp\fR command, described below, accepts qualified interpreter
74names as arguments; the interpreter in which the command is being evaluated
75can always be referred to as \fB{}\fR (the empty list or string). Note that
76it is impossible to refer to a master (ancestor) interpreter by name in a
77slave interpreter except through aliases. Also, there is no global name by
78which one can refer to the first interpreter created in an application.
79Both restrictions are motivated by safety concerns.
80.SH "THE INTERP COMMAND"
81.PP
82The \fBinterp\fR command is used to create, delete, and manipulate
83slave interpreters, and to share or transfer
84channels between interpreters.  It can have any of several forms, depending
85on the \fIsubcommand\fR argument:
86.TP
87\fBinterp\fR \fBalias\fR \fIsrcPath\fR \fIsrcToken\fR
88Returns a Tcl list whose elements are the \fItargetCmd\fR and
89\fIarg\fRs associated with the alias represented by \fIsrcToken\fR
90(this is the value returned when the alias was
91created; it is possible that the name of the source command in the
92slave is different from \fIsrcToken\fR).
93.TP
94\fBinterp\fR \fBalias\fR \fIsrcPath\fR \fIsrcToken\fR \fB{}\fR
95Deletes the alias for \fIsrcToken\fR in the slave interpreter identified by
96\fIsrcPath\fR.
97\fIsrcToken\fR refers to the value returned when the alias
98was created;  if the source command has been renamed, the renamed
99command will be deleted.
100.TP
101\fBinterp\fR \fBalias\fR \fIsrcPath\fR \fIsrcCmd\fR \fItargetPath\fR \fItargetCmd \fR?\fIarg arg ...\fR?
102This command creates an alias between one slave and another (see the
103\fBalias\fR slave command below for creating aliases between a slave
104and its master).  In this command, either of the slave interpreters
105may be anywhere in the hierarchy of interpreters under the interpreter
106invoking the command.
107\fISrcPath\fR and \fIsrcCmd\fR identify the source of the alias.
108\fISrcPath\fR is a Tcl list whose elements select a particular
109interpreter.  For example,
110.QW "\fBa b\fR"
111identifies an interpreter
112\fBb\fR, which is a slave of interpreter \fBa\fR, which is a slave
113of the invoking interpreter.  An empty list specifies the interpreter
114invoking the command.  \fIsrcCmd\fR gives the name of a new
115command, which will be created in the source interpreter.
116\fITargetPath\fR and \fItargetCmd\fR specify a target interpreter
117and command, and the \fIarg\fR arguments, if any, specify additional
118arguments to \fItargetCmd\fR which are prepended to any arguments specified
119in the invocation of \fIsrcCmd\fR.
120\fITargetCmd\fR may be undefined at the time of this call, or it may
121already exist; it is not created by this command.
122The alias arranges for the given target command to be invoked
123in the target interpreter whenever the given source command is
124invoked in the source interpreter.  See \fBALIAS INVOCATION\fR below for
125more details.
126The command returns a token that uniquely identifies the command created
127\fIsrcCmd\fR, even if the command is renamed afterwards. The token may but
128does not have to be equal to \fIsrcCmd\fR.
129.TP
130\fBinterp\fR \fBaliases \fR?\fIpath\fR?
131This command returns a Tcl list of the tokens of all the source commands for
132aliases defined in the interpreter identified by \fIpath\fR. The tokens
133correspond to the values returned when
134the aliases were created (which may not be the same
135as the current names of the commands).
136.TP
137\fBinterp bgerror \fIpath\fR ?\fIcmdPrefix\fR?
138.VS 8.5
139This command either gets or sets the current background error handler
140for the interpreter identified by \fIpath\fR. If \fIcmdPrefix\fR is
141absent, the current background error handler is returned, and if it is
142present, it is a list of words (of minimum length one) that describes
143what to set the interpreter's background error to. See the
144\fBBACKGROUND ERROR HANDLING\fR section for more details.
145.VE 8.5
146.TP
147\fBinterp\fR \fBcreate \fR?\fB\-safe\fR? ?\fB\-\|\-\fR? ?\fIpath\fR?
148Creates a slave interpreter identified by \fIpath\fR and a new command,
149called a \fIslave command\fR. The name of the slave command is the last
150component of \fIpath\fR. The new slave interpreter and the slave command
151are created in the interpreter identified by the path obtained by removing
152the last component from \fIpath\fR. For example, if \fIpath\fR is \fBa b
153c\fR then a new slave interpreter and slave command named \fBc\fR are
154created in the interpreter identified by the path \fBa b\fR.
155The slave command may be used to manipulate the new interpreter as
156described below. If \fIpath\fR is omitted, Tcl creates a unique name of the
157form \fBinterp\fIx\fR, where \fIx\fR is an integer, and uses it for the
158interpreter and the slave command. If the \fB\-safe\fR switch is specified
159(or if the master interpreter is a safe interpreter), the new slave
160interpreter will be created as a safe interpreter with limited
161functionality; otherwise the slave will include the full set of Tcl
162built-in commands and variables. The \fB\-\|\-\fR switch can be used to
163mark the end of switches;  it may be needed if \fIpath\fR is an unusual
164value such as \fB\-safe\fR. The result of the command is the name of the
165new interpreter. The name of a slave interpreter must be unique among all
166the slaves for its master;  an error occurs if a slave interpreter by the
167given name already exists in this master.
168The initial recursion limit of the slave interpreter is set to the
169current recursion limit of its parent interpreter.
170.TP
171\fBinterp\fR \fBdelete \fR?\fIpath ...?\fR
172Deletes zero or more interpreters given by the optional \fIpath\fR
173arguments, and for each interpreter, it also deletes its slaves. The
174command also deletes the slave command for each interpreter deleted.
175For each \fIpath\fR argument, if no interpreter by that name
176exists, the command raises an error.
177.TP
178\fBinterp\fR \fBeval\fR \fIpath arg \fR?\fIarg ...\fR?
179This command concatenates all of the \fIarg\fR arguments in the same
180fashion as the \fBconcat\fR command, then evaluates the resulting string as
181a Tcl script in the slave interpreter identified by \fIpath\fR. The result
182of this evaluation (including all \fBreturn\fR options,
183such as \fB\-errorinfo\fR and \fB\-errorcode\fR information, if an
184error occurs) is returned to the invoking interpreter.
185Note that the script will be executed in the current context stack frame of the
186\fIpath\fR interpreter; this is so that the implementations (in a master
187interpreter) of aliases in a slave interpreter can execute scripts in
188the slave that find out information about the slave's current state
189and stack frame.
190.TP
191\fBinterp exists \fIpath\fR
192Returns \fB1\fR if a slave interpreter by the specified \fIpath\fR
193exists in this master, \fB0\fR otherwise. If \fIpath\fR is omitted, the
194invoking interpreter is used.
195.TP
196\fBinterp expose \fIpath\fR \fIhiddenName\fR ?\fIexposedCmdName\fR?
197Makes the hidden command \fIhiddenName\fR exposed, eventually bringing
198it back under a new \fIexposedCmdName\fR name (this name is currently
199accepted only if it is a valid global name space name without any ::),
200in the interpreter
201denoted by \fIpath\fR.
202If an exposed command with the targeted name already exists, this command
203fails.
204Hidden commands are explained in more detail in \fBHIDDEN COMMANDS\fR, below.
205.TP
206\fBinterp\fR \fBhide\fR \fIpath\fR \fIexposedCmdName\fR ?\fIhiddenCmdName\fR?
207Makes the exposed command \fIexposedCmdName\fR hidden, renaming
208it to the hidden command \fIhiddenCmdName\fR, or keeping the same name if
209\fIhiddenCmdName\fR is not given, in the interpreter denoted
210by \fIpath\fR.
211If a hidden command with the targeted name already exists, this command
212fails.
213Currently both \fIexposedCmdName\fR and \fIhiddenCmdName\fR can
214not contain namespace qualifiers, or an error is raised.
215Commands to be hidden by \fBinterp hide\fR are looked up in the global
216namespace even if the current namespace is not the global one. This
217prevents slaves from fooling a master interpreter into hiding the wrong
218command, by making the current namespace be different from the global one.
219Hidden commands are explained in more detail in \fBHIDDEN COMMANDS\fR, below.
220.TP
221\fBinterp\fR \fBhidden\fR \fIpath\fR
222Returns a list of the names of all hidden commands in the interpreter
223identified by \fIpath\fR.
224.TP
225\fBinterp\fR \fBinvokehidden\fR \fIpath\fR ?\fI\-option ...\fR? \fIhiddenCmdName\fR ?\fIarg ...\fR?
226Invokes the hidden command \fIhiddenCmdName\fR with the arguments supplied
227in the interpreter denoted by \fIpath\fR. No substitutions or evaluation
228are applied to the arguments. Three \fI\-option\fRs are supported, all
229of which start with \fB\-\fR: \fB\-namespace\fR (which takes a single
230argument afterwards, \fInsName\fR), \fB\-global\fR, and \fB\-\|\-\fR.
231If the \fB\-namespace\fR flag is present, the hidden command is invoked in
232the namespace called \fInsName\fR in the target interpreter.
233If the \fB\-global\fR flag is present, the hidden command is invoked at the
234global level in the target interpreter; otherwise it is invoked at the
235current call frame and can access local variables in that and outer call
236frames.
237The \fB\-\|\-\fR flag allows the \fIhiddenCmdName\fR argument to start with a
238.QW \-
239character, and is otherwise unnecessary.
240If both the \fB\-namespace\fR and \fB\-global\fR flags are present, the
241\fB\-namespace\fR flag is ignored.
242Note that the hidden command will be executed (by default) in the
243current context stack frame of the \fIpath\fR interpreter.
244Hidden commands are explained in more detail in \fBHIDDEN COMMANDS\fR, below.
245.TP
246\fBinterp\fR \fBlimit\fR \fIpath\fR \fIlimitType\fR ?\fI\-option\fR? ?\fIvalue\fR \fI...\fR?
247.VS 8.5
248Sets up, manipulates and queries the configuration of the resource
249limit \fIlimitType\fR for the interpreter denoted by \fIpath\fR.  If
250no \fI\-option\fR is specified, return the current configuration of the
251limit.  If \fI\-option\fR is the sole argument, return the value of that
252option.  Otherwise, a list of \fI\-option\fR/\fIvalue\fR argument pairs
253must supplied. See \fBRESOURCE LIMITS\fR below for a more detailed
254explanation of what limits and options are supported.
255.VE 8.5
256.TP
257\fBinterp issafe\fR ?\fIpath\fR?
258Returns \fB1\fR if the interpreter identified by the specified \fIpath\fR
259is safe, \fB0\fR otherwise.
260.TP
261\fBinterp marktrusted\fR \fIpath\fR
262Marks the interpreter identified by \fIpath\fR as trusted. Does
263not expose the hidden commands. This command can only be invoked from a
264trusted interpreter.
265The command has no effect if the interpreter identified by \fIpath\fR is
266already trusted.
267.TP
268\fBinterp\fR \fBrecursionlimit\fR \fIpath\fR ?\fInewlimit\fR?
269Returns the maximum allowable nesting depth for the interpreter
270specified by \fIpath\fR.  If \fInewlimit\fR is specified,
271the interpreter recursion limit will be set so that nesting
272of more than \fInewlimit\fR calls to \fBTcl_Eval()\fR
273and related procedures in that interpreter will return an error.
274The \fInewlimit\fR value is also returned.
275The \fInewlimit\fR value must be a positive integer between 1 and the
276maximum value of a non-long integer on the platform.
277.RS
278.PP
279The command sets the maximum size of the Tcl call stack only. It cannot
280by itself prevent stack overflows on the C stack being used by the
281application. If your machine has a limit on the size of the C stack, you
282may get stack overflows before reaching the limit set by the command. If
283this happens, see if there is a mechanism in your system for increasing
284the maximum size of the C stack.
285.RE
286.TP
287\fBinterp\fR \fBshare\fR \fIsrcPath channelId destPath\fR
288Causes the IO channel identified by \fIchannelId\fR to become shared
289between the interpreter identified by \fIsrcPath\fR and the interpreter
290identified by \fIdestPath\fR. Both interpreters have the same permissions
291on the IO channel.
292Both interpreters must close it to close the underlying IO channel; IO
293channels accessible in an interpreter are automatically closed when an
294interpreter is destroyed.
295.TP
296\fBinterp\fR \fBslaves\fR ?\fIpath\fR?
297Returns a Tcl list of the names of all the slave interpreters associated
298with the interpreter identified by \fIpath\fR. If \fIpath\fR is omitted,
299the invoking interpreter is used.
300.TP
301\fBinterp\fR \fBtarget\fR \fIpath alias\fR
302Returns a Tcl list describing the target interpreter for an alias. The
303alias is specified with an interpreter path and source command name, just
304as in \fBinterp alias\fR above. The name of the target interpreter is
305returned as an interpreter path, relative to the invoking interpreter.
306If the target interpreter for the alias is the invoking interpreter then an
307empty list is returned. If the target interpreter for the alias is not the
308invoking interpreter or one of its descendants then an error is generated.
309The target command does not have to be defined at the time of this invocation.
310.TP
311\fBinterp\fR \fBtransfer\fR \fIsrcPath channelId destPath\fR
312Causes the IO channel identified by \fIchannelId\fR to become available in
313the interpreter identified by \fIdestPath\fR and unavailable in the
314interpreter identified by \fIsrcPath\fR.
315.SH "SLAVE COMMAND"
316.PP
317For each slave interpreter created with the \fBinterp\fR command, a
318new Tcl command is created in the master interpreter with the same
319name as the new interpreter. This command may be used to invoke
320various operations on the interpreter.  It has the following
321general form:
322.CS
323\fIslave command \fR?\fIarg arg ...\fR?
324.CE
325\fISlave\fR is the name of the interpreter, and \fIcommand\fR
326and the \fIarg\fRs determine the exact behavior of the command.
327The valid forms of this command are:
328.TP
329\fIslave \fBaliases\fR
330Returns a Tcl list whose elements are the tokens of all the
331aliases in \fIslave\fR.  The tokens correspond to the values returned when
332the aliases were created (which may not be the same
333as the current names of the commands).
334.TP
335\fIslave \fBalias \fIsrcToken\fR
336Returns a Tcl list whose elements are the \fItargetCmd\fR and
337\fIarg\fRs associated with the alias represented by \fIsrcToken\fR
338(this is the value returned when the alias was
339created; it is possible that the actual source command in the
340slave is different from \fIsrcToken\fR).
341.TP
342\fIslave \fBalias \fIsrcToken \fB{}\fR
343Deletes the alias for \fIsrcToken\fR in the slave interpreter.
344\fIsrcToken\fR refers to the value returned when the alias
345was created;  if the source command has been renamed, the renamed
346command will be deleted.
347.TP
348\fIslave \fBalias \fIsrcCmd targetCmd \fR?\fIarg ..\fR?
349Creates an alias such that whenever \fIsrcCmd\fR is invoked
350in \fIslave\fR, \fItargetCmd\fR is invoked in the master.
351The \fIarg\fR arguments will be passed to \fItargetCmd\fR as additional
352arguments, prepended before any arguments passed in the invocation of
353\fIsrcCmd\fR.
354See \fBALIAS INVOCATION\fR below for details.
355The command returns a token that uniquely identifies the command created
356\fIsrcCmd\fR, even if the command is renamed afterwards. The token may but
357does not have to be equal to \fIsrcCmd\fR.
358.TP
359\fIslave \fBbgerror\fR ?\fIcmdPrefix\fR?
360.VS 8.5
361This command either gets or sets the current background error handler
362for the \fIslave\fR interpreter. If \fIcmdPrefix\fR is
363absent, the current background error handler is returned, and if it is
364present, it is a list of words (of minimum length one) that describes
365what to set the interpreter's background error to. See the
366\fBBACKGROUND ERROR HANDLING\fR section for more details.
367.VE 8.5
368.TP
369\fIslave \fBeval \fIarg \fR?\fIarg ..\fR?
370This command concatenates all of the \fIarg\fR arguments in
371the same fashion as the \fBconcat\fR command, then evaluates
372the resulting string as a Tcl script in \fIslave\fR.
373The result of this evaluation (including all \fBreturn\fR options,
374such as \fB\-errorinfo\fR and \fB\-errorcode\fR information, if an
375error occurs) is returned to the invoking interpreter.
376Note that the script will be executed in the current context stack frame
377of \fIslave\fR; this is so that the implementations (in a master
378interpreter) of aliases in a slave interpreter can execute scripts in
379the slave that find out information about the slave's current state
380and stack frame.
381.TP
382\fIslave \fBexpose \fIhiddenName \fR?\fIexposedCmdName\fR?
383This command exposes the hidden command \fIhiddenName\fR, eventually bringing
384it back under a new \fIexposedCmdName\fR name (this name is currently
385accepted only if it is a valid global name space name without any ::),
386in \fIslave\fR.
387If an exposed command with the targeted name already exists, this command
388fails.
389For more details on hidden commands, see \fBHIDDEN COMMANDS\fR, below.
390.TP
391\fIslave \fBhide \fIexposedCmdName\fR ?\fIhiddenCmdName\fR?
392This command hides the exposed command \fIexposedCmdName\fR, renaming it to
393the hidden command \fIhiddenCmdName\fR, or keeping the same name if the
394argument is not given, in the \fIslave\fR interpreter.
395If a hidden command with the targeted name already exists, this command
396fails.
397Currently both \fIexposedCmdName\fR and \fIhiddenCmdName\fR can
398not contain namespace qualifiers, or an error is raised.
399Commands to be hidden are looked up in the global
400namespace even if the current namespace is not the global one. This
401prevents slaves from fooling a master interpreter into hiding the wrong
402command, by making the current namespace be different from the global one.
403For more details on hidden commands, see \fBHIDDEN COMMANDS\fR, below.
404.TP
405\fIslave \fBhidden\fR
406Returns a list of the names of all hidden commands in \fIslave\fR.
407.TP
408\fIslave \fBinvokehidden\fR ?\fI\-option ...\fR? \fIhiddenName \fR?\fIarg ..\fR?
409This command invokes the hidden command \fIhiddenName\fR with the
410supplied arguments, in \fIslave\fR. No substitutions or evaluations are
411applied to the arguments. Three \fI\-option\fRs are supported, all
412of which start with \fB\-\fR: \fB\-namespace\fR (which takes a single
413argument afterwards, \fInsName\fR), \fB\-global\fR, and \fB\-\|\-\fR.
414If the \fB\-namespace\fR flag is given, the hidden command is invoked in
415the specified namespace in the slave.
416If the \fB\-global\fR flag is given, the command is invoked at the global
417level in the slave; otherwise it is invoked at the current call frame and
418can access local variables in that or outer call frames.
419The \fB\-\|\-\fR flag allows the \fIhiddenCmdName\fR argument to start with a
420.QW \-
421character, and is otherwise unnecessary.
422If both the \fB\-namespace\fR and \fB\-global\fR flags are given, the
423\fB\-namespace\fR flag is ignored.
424Note that the hidden command will be executed (by default) in the
425current context stack frame of \fIslave\fR.
426For more details on hidden commands,
427see \fBHIDDEN COMMANDS\fR, below.
428.TP
429\fIslave \fBissafe\fR
430Returns  \fB1\fR if the slave interpreter is safe, \fB0\fR otherwise.
431.TP
432\fIslave \fBlimit\fR \fIlimitType\fR ?\fI\-option\fR? ?\fIvalue\fR \fI...\fR?
433.VS 8.5
434Sets up, manipulates and queries the configuration of the resource
435limit \fIlimitType\fR for the slave interpreter.  If no \fI\-option\fR
436is specified, return the current configuration of the limit.  If
437\fI\-option\fR is the sole argument, return the value of that option.
438Otherwise, a list of \fI\-option\fR/\fIvalue\fR argument pairs must
439supplied. See \fBRESOURCE LIMITS\fR below for a more detailed explanation of
440what limits and options are supported.
441.VE 8.5
442.TP
443\fIslave \fBmarktrusted\fR
444Marks the slave interpreter as trusted. Can only be invoked by a
445trusted interpreter. This command does not expose any hidden
446commands in the slave interpreter. The command has no effect if the slave
447is already trusted.
448.TP
449\fIslave\fR \fBrecursionlimit\fR ?\fInewlimit\fR?
450Returns the maximum allowable nesting depth for the \fIslave\fR interpreter.
451If \fInewlimit\fR is specified, the recursion limit in \fIslave\fR will be
452set so that nesting of more than \fInewlimit\fR calls to \fBTcl_Eval()\fR
453and related procedures in \fIslave\fR will return an error.
454The \fInewlimit\fR value is also returned.
455The \fInewlimit\fR value must be a positive integer between 1 and the
456maximum value of a non-long integer on the platform.
457.RS
458.PP
459The command sets the maximum size of the Tcl call stack only. It cannot
460by itself prevent stack overflows on the C stack being used by the
461application. If your machine has a limit on the size of the C stack, you
462may get stack overflows before reaching the limit set by the command. If
463this happens, see if there is a mechanism in your system for increasing
464the maximum size of the C stack.
465.RE
466.SH "SAFE INTERPRETERS"
467.PP
468A safe interpreter is one with restricted functionality, so that
469is safe to execute an arbitrary script from your worst enemy without
470fear of that script damaging the enclosing application or the rest
471of your computing environment.  In order to make an interpreter
472safe, certain commands and variables are removed from the interpreter.
473For example, commands to create files on disk are removed, and the
474\fBexec\fR command is removed, since it could be used to cause damage
475through subprocesses.
476Limited access to these facilities can be provided, by creating
477aliases to the master interpreter which check their arguments carefully
478and provide restricted access to a safe subset of facilities.
479For example, file creation might be allowed in a particular subdirectory
480and subprocess invocation might be allowed for a carefully selected and
481fixed set of programs.
482.PP
483A safe interpreter is created by specifying the \fB\-safe\fR switch
484to the \fBinterp create\fR command.  Furthermore, any slave created
485by a safe interpreter will also be safe.
486.PP
487A safe interpreter is created with exactly the following set of
488built-in commands:
489.DS
490.ta 1.2i 2.4i 3.6i
491\fBafter\fR     \fBappend\fR    \fBapply\fR     \fBarray\fR
492\fBbinary\fR    \fBbreak\fR     \fBcatch\fR     \fBchan\fR
493\fBclock\fR     \fBclose\fR     \fBconcat\fR    \fBcontinue\fR
494\fBdict\fR      \fBeof\fR       \fBerror\fR     \fBeval\fR
495\fBexpr\fR      \fBfblocked\fR  \fBfcopy\fR     \fBfileevent\fR
496\fBflush\fR     \fBfor\fR       \fBforeach\fR   \fBformat\fR
497\fBgets\fR      \fBglobal\fR    \fBif\fR        \fBincr\fR
498\fBinfo\fR      \fBinterp\fR    \fBjoin\fR      \fBlappend\fR
499\fBlassign\fR   \fBlindex\fR    \fBlinsert\fR   \fBlist\fR
500\fBllength\fR   \fBlrange\fR    \fBlrepeat\fR   \fBlreplace\fR
501\fBlsearch\fR   \fBlset\fR      \fBlsort\fR     \fBnamespace\fR
502\fBpackage\fR   \fBpid\fR       \fBproc\fR      \fBputs\fR
503\fBread\fR      \fBregexp\fR    \fBregsub\fR    \fBrename\fR
504\fBreturn\fR    \fBscan\fR      \fBseek\fR      \fBset\fR
505\fBsplit\fR     \fBstring\fR    \fBsubst\fR     \fBswitch\fR
506\fBtell\fR      \fBtime\fR      \fBtrace\fR     \fBunset\fR
507\fBupdate\fR    \fBuplevel\fR   \fBupvar\fR     \fBvariable\fR
508\fBvwait\fR     \fBwhile\fR
509.DE
510The following commands are hidden by \fBinterp create\fR when it
511creates a safe interpreter:
512.DS
513.ta 1.2i 2.4i 3.6i
514\fBcd\fR        \fBencoding\fR  \fBexec\fR      \fBexit\fR
515\fBfconfigure\fR        \fBfile\fR      \fBglob\fR      \fBload\fR
516\fBopen\fR      \fBpwd\fR       \fBsocket\fR    \fBsource\fR
517\fBunload\fR
518.DE
519These commands can be recreated later as Tcl procedures or aliases, or
520re-exposed by \fBinterp expose\fR.
521.PP
522The following commands from Tcl's library of support procedures are
523not present in a safe interpreter:
524.DS
525.ta 1.6i 3.2i
526\fBauto_exec_ok\fR      \fBauto_import\fR       \fBauto_load\fR
527\fBauto_load_index\fR   \fBauto_qualify\fR      \fBunknown\fR
528.DE
529Note in particular that safe interpreters have no default \fBunknown\fR
530command, so Tcl's default autoloading facilities are not available.
531Autoload access to Tcl's commands that are normally autoloaded:
532.DS
533.ta 2.1i
534\fBauto_mkindex\fR      \fBauto_mkindex_old\fR
535\fBauto_reset\fR        \fBhistory\fR
536\fBparray\fR    \fBpkg_mkIndex\fR
537\fB::pkg::create\fR     \fB::safe::interpAddToAccessPath\fR
538\fB::safe::interpCreate\fR      \fB::safe::interpConfigure\fR
539\fB::safe::interpDelete\fR      \fB::safe::interpFindInAccessPath\fR
540\fB::safe::interpInit\fR        \fB::safe::setLogCmd\fR
541\fBtcl_endOfWord\fR     \fBtcl_findLibrary\fR
542\fBtcl_startOfNextWord\fR       \fBtcl_startOfPreviousWord\fR
543\fBtcl_wordBreakAfter\fR        \fBtcl_wordBreakBefore\fR
544.DE
545can only be provided by explicit definition of an \fBunknown\fR command
546in the safe interpreter.  This will involve exposing the \fBsource\fR
547command.  This is most easily accomplished by creating the safe interpreter
548with Tcl's \fBSafe\-Tcl\fR mechanism.  \fBSafe\-Tcl\fR provides safe
549versions of \fBsource\fR, \fBload\fR, and other Tcl commands needed
550to support autoloading of commands and the loading of packages.
551.PP
552In addition, the \fBenv\fR variable is not present in a safe interpreter,
553so it cannot share environment variables with other interpreters. The
554\fBenv\fR variable poses a security risk, because users can store
555sensitive information in an environment variable. For example, the PGP
556manual recommends storing the PGP private key protection password in
557the environment variable \fIPGPPASS\fR. Making this variable available
558to untrusted code executing in a safe interpreter would incur a
559security risk.
560.PP
561If extensions are loaded into a safe interpreter, they may also restrict
562their own functionality to eliminate unsafe commands. For a discussion of
563management of extensions for safety see the manual entries for
564\fBSafe\-Tcl\fR and the \fBload\fR Tcl command.
565.PP
566A safe interpreter may not alter the recursion limit of any interpreter,
567including itself.
568.SH "ALIAS INVOCATION"
569.PP
570The alias mechanism has been carefully designed so that it can
571be used safely when an untrusted script is executing
572in a safe slave and the target of the alias is a trusted
573master.  The most important thing in guaranteeing safety is to
574ensure that information passed from the slave to the master is
575never evaluated or substituted in the master;  if this were to
576occur, it would enable an evil script in the slave to invoke
577arbitrary functions in the master, which would compromise security.
578.PP
579When the source for an alias is invoked in the slave interpreter, the
580usual Tcl substitutions are performed when parsing that command.
581These substitutions are carried out in the source interpreter just
582as they would be for any other command invoked in that interpreter.
583The command procedure for the source command takes its arguments
584and merges them with the \fItargetCmd\fR and \fIarg\fRs for the
585alias to create a new array of arguments.  If the words
586of \fIsrcCmd\fR were
587.QW "\fIsrcCmd arg1 arg2 ... argN\fR" ,
588the new set of words will be
589.QW "\fItargetCmd arg arg ... arg arg1 arg2 ... argN\fR" ,
590where \fItargetCmd\fR and \fIarg\fRs are the values supplied when the
591alias was created.  \fITargetCmd\fR is then used to locate a command
592procedure in the target interpreter, and that command procedure
593is invoked with the new set of arguments.  An error occurs if
594there is no command named \fItargetCmd\fR in the target interpreter.
595No additional substitutions are performed on the words:  the
596target command procedure is invoked directly, without
597going through the normal Tcl evaluation mechanism.
598Substitutions are thus performed on each word exactly once:
599\fItargetCmd\fR and \fIargs\fR were substituted when parsing the command
600that created the alias, and \fIarg1 - argN\fR are substituted when
601the alias's source command is parsed in the source interpreter.
602.PP
603When writing the \fItargetCmd\fRs for aliases in safe interpreters,
604it is very important that the arguments to that command never be
605evaluated or substituted, since this would provide an escape
606mechanism whereby the slave interpreter could execute arbitrary
607code in the master.  This in turn would compromise the security
608of the system.
609.SH "HIDDEN COMMANDS"
610.PP
611Safe interpreters greatly restrict the functionality available to Tcl
612programs executing within them.
613Allowing the untrusted Tcl program to have direct access to this
614functionality is unsafe, because it can be used for a variety of
615attacks on the environment.
616However, there are times when there is a legitimate need to use the
617dangerous functionality in the context of the safe interpreter. For
618example, sometimes a program must be \fBsource\fRd into the interpreter.
619Another example is Tk, where windows are bound to the hierarchy of windows
620for a specific interpreter; some potentially dangerous functions, e.g.
621window management, must be performed on these windows within the
622interpreter context.
623.PP
624The \fBinterp\fR command provides a solution to this problem in the form of
625\fIhidden commands\fR. Instead of removing the dangerous commands entirely
626from a safe interpreter, these commands are hidden so they become
627unavailable to Tcl scripts executing in the interpreter. However, such
628hidden commands can be invoked by any trusted ancestor of the safe
629interpreter, in the context of the safe interpreter, using \fBinterp
630invoke\fR. Hidden commands and exposed commands reside in separate name
631spaces. It is possible to define a hidden command and an exposed command by
632the same name within one interpreter.
633.PP
634Hidden commands in a slave interpreter can be invoked in the body of
635procedures called in the master during alias invocation. For example, an
636alias for \fBsource\fR could be created in a slave interpreter. When it is
637invoked in the slave interpreter, a procedure is called in the master
638interpreter to check that the operation is allowable (e.g. it asks to
639source a file that the slave interpreter is allowed to access). The
640procedure then it invokes the hidden \fBsource\fR command in the slave
641interpreter to actually source in the contents of the file. Note that two
642commands named \fBsource\fR exist in the slave interpreter: the alias, and
643the hidden command.
644.PP
645Because a master interpreter may invoke a hidden command as part of
646handling an alias invocation, great care must be taken to avoid evaluating
647any arguments passed in through the alias invocation.
648Otherwise, malicious slave interpreters could cause a trusted master
649interpreter to execute dangerous commands on their behalf. See the section
650on \fBALIAS INVOCATION\fR for a more complete discussion of this topic.
651To help avoid this problem, no substitutions or evaluations are
652applied to arguments of \fBinterp invokehidden\fR.
653.PP
654Safe interpreters are not allowed to invoke hidden commands in themselves
655or in their descendants. This prevents safe slaves from gaining access to
656hidden functionality in themselves or their descendants.
657.PP
658The set of hidden commands in an interpreter can be manipulated by a trusted
659interpreter using \fBinterp expose\fR and \fBinterp hide\fR. The \fBinterp
660expose\fR command moves a hidden command to the
661set of exposed commands in the interpreter identified by \fIpath\fR,
662potentially renaming the command in the process. If an exposed command by
663the targeted name already exists, the operation fails. Similarly,
664\fBinterp hide\fR moves an exposed command to the set of hidden commands in
665that interpreter. Safe interpreters are not allowed to move commands
666between the set of hidden and exposed commands, in either themselves or
667their descendants.
668.PP
669Currently, the names of hidden commands cannot contain namespace
670qualifiers, and you must first rename a command in a namespace to the
671global namespace before you can hide it.
672Commands to be hidden by \fBinterp hide\fR are looked up in the global
673namespace even if the current namespace is not the global one. This
674prevents slaves from fooling a master interpreter into hiding the wrong
675command, by making the current namespace be different from the global one.
676.SH "RESOURCE LIMITS"
677.VS 8.5
678.PP
679Every interpreter has two kinds of resource limits that may be imposed by any
680master interpreter upon its slaves. Command limits (of type \fBcommand\fR)
681restrict the total number of Tcl commands that may be executed by an
682interpreter (as can be inspected via the \fBinfo cmdcount\fR command), and
683time limits (of type \fBtime\fR) place a limit by which execution within the
684interpreter must complete. Note that time limits are expressed as
685\fIabsolute\fR times (as in \fBclock seconds\fR) and not relative times (as in
686\fBafter\fR) because they may be modified after creation.
687.PP
688When a limit is exceeded for an interpreter, first any handler callbacks
689defined by master interpreters are called. If those callbacks increase or
690remove the limit, execution within the (previously) limited interpreter
691continues. If the limit is still in force, an error is generated at that point
692and normal processing of errors within the interpreter (by the \fBcatch\fR
693command) is disabled, so the error propagates outwards (building a stack-trace
694as it goes) to the point where the limited interpreter was invoked (e.g. by
695\fBinterp eval\fR) where it becomes the responsibility of the calling code to
696catch and handle.
697.SS "LIMIT OPTIONS"
698.PP
699Every limit has a number of options associated with it, some of which are
700common across all kinds of limits, and others of which are particular to the
701kind of limit.
702.VE 8.5
703.TP
704\fB\-command\fR
705.VS 8.5
706This option (common for all limit types) specifies (if non-empty) a Tcl script
707to be executed in the global namespace of the interpreter reading and writing
708the option when the particular limit in the limited interpreter is exceeded.
709The callback may modify the limit on the interpreter if it wishes the limited
710interpreter to continue executing. If the callback generates an error, it is
711reported through the background error mechanism (see \fBBACKGROUND ERROR
712HANDLING\fR). Note that the callbacks defined by one interpreter are
713completely isolated from the callbacks defined by another, and that the order
714in which those callbacks are called is undefined.
715.VE 8.5
716.TP
717\fB\-granularity\fR
718.VS 8.5
719This option (common for all limit types) specifies how frequently (out of the
720points when the Tcl interpreter is in a consistent state where limit checking
721is possible) that the limit is actually checked. This allows the tuning of how
722frequently a limit is checked, and hence how often the limit-checking overhead
723(which may be substantial in the case of time limits) is incurred.
724.VE 8.5
725.TP
726\fB\-milliseconds\fR
727.VS 8.5
728This option specifies the number of milliseconds after the moment defined in
729the \fB\-seconds\fR option that the time limit will fire. It should only ever
730be specified in conjunction with the \fB\-seconds\fR option (whether it was
731set previously or is being set this invocation.)
732.VE 8.5
733.TP
734\fB\-seconds\fR
735.VS 8.5
736This option specifies the number of seconds after the epoch (see \fBclock
737seconds\fR) that the time limit for the interpreter will be triggered. The
738limit will be triggered at the start of the second unless specified at a
739sub-second level using the \fB\-milliseconds\fR option. This option may be the
740empty string, which indicates that a time limit is not set for the
741interpreter.
742.VE 8.5
743.TP
744\fB\-value\fR
745.VS 8.5
746This option specifies the number of commands that the interpreter may execute
747before triggering the command limit. This option may be the empty string,
748which indicates that a command limit is not set for the interpreter.
749.PP
750Where an interpreter with a resource limit set on it creates a slave
751interpreter, that slave interpreter will have resource limits imposed on it
752that are at least as restrictive as the limits on the creating master
753interpreter. If the master interpreter of the limited master wishes to relax
754these conditions, it should hide the \fBinterp\fR command in the child and
755then use aliases and the \fBinterp invokehidden\fR subcommand to provide such
756access as it chooses to the \fBinterp\fR command to the limited master as
757necessary.
758.SH "BACKGROUND ERROR HANDLING"
759.PP
760When an error happens in a situation where it cannot be reported directly up
761the stack (e.g. when processing events in an \fBupdate\fR or \fBvwait\fR call)
762the error is instead reported through the background error handling mechanism.
763Every interpreter has a background error handler registered; the default error
764handler arranges for the \fBbgerror\fR command in the interpreter's global
765namespace to be called, but other error handlers may be installed and process
766background errors in substantially different ways.
767.PP
768A background error handler consists of a non-empty list of words to which will
769be appended two further words at invocation time. The first word will be the
770error message string, and the second will a dictionary of return options (this
771is also the sort of information that can be obtained by trapping a normal
772error using \fBcatch\fR of course.) The resulting list will then be executed
773in the interpreter's global namespace without further substitutions being
774performed.
775.VE 8.5
776.SH CREDITS
777The safe interpreter mechanism is based on the Safe-Tcl prototype implemented
778by Nathaniel Borenstein and Marshall Rose.
779.SH EXAMPLES
780Creating and using an alias for a command in the current interpreter:
781.CS
782\fBinterp alias\fR {} getIndex {} lsearch {alpha beta gamma delta}
783set idx [getIndex delta]
784.CE
785.PP
786Executing an arbitrary command in a safe interpreter where every
787invocation of \fBlappend\fR is logged:
788.CS
789set i [\fBinterp create\fR -safe]
790\fBinterp hide\fR $i lappend
791\fBinterp alias\fR $i lappend {} loggedLappend $i
792proc loggedLappend {i args} {
793   puts "logged invocation of lappend $args"
794   \fBinterp invokehidden\fR $i lappend {*}$args
795}
796\fBinterp eval\fR $i $someUntrustedScript
797.CE
798.PP
799.VS 8.5
800Setting a resource limit on an interpreter so that an infinite loop
801terminates.
802.CS
803set i [\fBinterp create\fR]
804\fBinterp limit\fR $i command -value 1000
805\fBinterp eval\fR $i {
806   set x 0
807   while {1} {
808      puts "Counting up... [incr x]"
809   }
810}
811.CE
812.VE 8.5
813.SH "SEE ALSO"
814bgerror(n), load(n), safe(n), Tcl_CreateSlave(3)
815.SH KEYWORDS
816alias, master interpreter, safe interpreter, slave interpreter
Note: See TracBrowser for help on using the repository browser.