summaryrefslogtreecommitdiff
path: root/src/lib/libcoshell/coshell.3
blob: b0a1201db5dd8d0de16b8fca0bc3eaed15bd131d (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
.fp 5 CW
.de L		\" literal font
.ft 5
.if !\\$1 \&\\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \f1
..
.de LR
.}S 5 1 \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
..
.de RL
.}S 1 5 \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
..
.de EX		\" start example
.ta 1i 2i 3i 4i 5i 6i
.PP
.RS 
.PD 0
.ft 5
.nf
..
.de EE		\" end example
.fi
.ft
.PD
.RE
.PP
..
.TH COSHELL 3
.SH NAME \" @(#)coshell.3 (gsf@research.att.com) 10/11/90
coshell \- shell coprocess support
.SH SYNOPSIS
.L "#include <coshell.h>"
.br
.L "\-lcoshell \-last"
.SH DESCRIPTION
The
.I coshell
routines support the shell as a coprocess.
This coprocess may be either
.IR ksh (1)
or
.IR sh (1)
executing on the local host, or it may be
.IR coshell (1)
with access to shells on hosts throughout the local network.
.PP
The coshell inherits the environment of the calling process.
Signals sent to the calling process are passed to the coshell.
.PP
More than one coshell may be open in the current process.
If the
.L Coshell_t*
argument to the
.LR cowait() ,
.LR cokill() ,
.LR copending() ,
.LR cozombie() ,
or
.L coclose()
calls below is
.L 0
then the call is applied to all open coshell.
.PP
.L "Coshell_t* coopen(const char* shell, int flags, const char* attributes)"
.PP
Returns a pointer to a new coshell.
.L NULL 
is returned on error.
If
.L shell
is
.L NULL
then the coshell executable is determined by doing the usual path search,
in order, on the value of the environment variable
.B COSHELL
and the commands
.BR ksh
and
.BR sh .
.L flags
is the inclusive-or of the following:
.TP
.L CO_ANY
Return a pointer to a previously opened coshell if possible, otherwise
open a new coshell.
.TP
.L CO_DEBUG
Enable library debug tracing.
.TP
.L CO_IGNORE
Ignore any command errors.
By default any command error that is not tested by a condtional causes
the job to terminate.
.TP
.L CO_LOCAL
Commands are to be executed on the local host only.
.TP
.L CO_SHELL
The caller is
.BR sh (1):
internal file descriptors are moved to 10 or above;
SIGSTOP and SIGCONT handlers are not installed.
.TP
.L CO_SILENT
Don't trace commands.
By default commands are traced using the shell
.B \-x
option.
.TP
.L CO_NONBLOCK
Normally
.L coexec()
blocks when the job queue is full and waits until a job completes.
.L CO_NONBLOCK
causes
.L coexec()
to return
.L NULL
when the job queue is full.
.PP
.L attributes
is a string that is interpreted by the coshell.
If
.L attributes
is
.L NULL
then the value of the environment variable
.B COATTRIBUTES
is used if defined.
.B ksh
and
.B sh
ignore this string.
The return value points to a structure with the following readonly elements:
.TP
.L "int flags"
The default flags.
.TP
.L "int outstanding"
The number of jobs that have not been waited for.
.TP
.L "int running"
The number of jobs still running.
.TP
.L "int total"
The total number of jobs sent to the coshell.
.TP
.L "unsigned long user"
The total user time of all completed jobs in
.L 1/CO_QUANT
second increments.
.TP
.L "unsigned long sys"
The total system time of all completed jobs in
.L 1/CO_QUANT
second increments.
.PP
.L "int coclose(Coshell_t* sh)"
.PP
Close an open coshell pointed to by
.LR sh .
The coshell exit status is returned.
.PP
.L "Cojob_t* coexec(Coshell_t* sh, const char* cmd, int flags, const char* out, const char* err, const char* att)"
.PP
Sends the shell command line
.L cmd
to the open coshell pointed to by
.L sh
for execution.
.L flags
are the same as in the
.L coopen()
call, and are used to augment the default settings from
.LR coopen() .
.L out
is the standard output file name and defaults to
.B stdout
if
.LR NULL .
.L err
is the standard error file name and defaults to
.B stderr
if
.LR NULL .
.LR att ,
if
.RL non- NULL ,
contains job attributes that are appended to the attributes from
.L coopen()
before being sent to the coshell.
The return value points to a structure with the following elements:
.TP
.L "int id"
A number that uniquely identifies the job within the coshell.
.TP
.L "int status"
The job exit status, valid only for job pointers returned by
.LR cowait() .
.TP
.L "int flags"
The flags enabled for this job.
.TP
.L "void* local"
A user reserved pointer, initially set to
.L NULL
on return from
.LR coexec() .
This is the only job field that may be modified by the user.
The user defined value is preserved until after the
.L cowait()
call that returns the job pointer.
.TP
.L "unsigned long user"
The user time of this job in
.L 1/CO_QUANT
second increments, valid only for job pointers returned by
.LR cowait() .
.TP
.L "unsigned long sys"
The system time of this job in
.L 1/CO_QUANT
second increments, valid only for job pointers returned by
.LR cowait() .
.PP
.L "Cojob_t* cowait(Coshell_t* sh, Cojob_t* job, int timeout)"
.PP
Returns the job pointer in the coshell pointed to by
.L sh
for the job pointed to by
.LR job .
If
.L job
is
.L NULL
then a pointer to any completed job is returned.
.L cowait()
blocks until the specified job(s) complete.
.L NULL
is returned on error or if all jobs have completed and
.L errno
is set to EINVAL for coshell communication errors,
ECHILD if
.L job
is
.L NULL
and there are no children, and ESRCH if
.L job
is not
.L NULL
and not an active job.
.L "cozombie(sh)"
is the number of jobs that may be waited for without blocking.
The return value
.LR status ,
.L user
and
.L sys
job fields are set to their final values.
The return value is valid until the next
.LR coexec() ,
.L cowait()
or
.L coclose()
call.
.L timeout
is the maximum time in milliseconds that wait will block.
If the wait times out then 0 is returned.
A negative
.L timeout
waits until a job completes or a signal is received.
.TP
.L "int cojobs(Coshell_t* sh)"
Returns the number of outstanding jobs that are children of the caller.
(Remote jobs or jobs executed by a separate daemon are not counted here.)
.TP
.L "int copending(Coshell_t* sh)"
Returns the number of pending jobs; this is the number of
.L cowait()
calls required to reap all running jobs.
.TP
.L "int cozombie(Coshell_t* sh)"
Returns the number of jobs that have completed but have not been
.L cowait()'d
for.
.TP
.L "int cokill(Coshell_t* sh, Cojob_t* job, int sig)"
The signal
.L sig
is sent to the job pointed to by
.L job
running in the coshell pointed to by
.LR sh .
If
.L job
is
.L NULL
then the signal is sent to all jobs in the coshell.
If both
.L sh
and
.L job
are
.L NULL 
then the signal is sent to all jobs in all coshells.
.L \-1
is returned on error,
.L 0
otherwise.
.TP
.L "int cosync(Coshell_t* sh, const char* path, int fd, int mode)"
Sync all outstanding file operations for either the file
.L path
or the file descriptor
.L fd
after its shell action has completed in
.LR sh .
If
.L path
is
.L NULL
then 
.L fd
is used.
If 
.L fd<0
and
.L mode>=0
then
.L path
is opened using
.L mode.
This is an unfortunate workaround for NFS and remote coshells, and is a
no-op for all real file systems.
It should be called after
.L cowait()
to ensure that all file system cache info has been flushed.
.IR sync (2)
or
.IR fsync (2)
must still be called to schedule file data to be written to disk.
.TP
.L "char* coinit(Coshell_t* sh)"
Returns the shell initialization commands for the next job.
These commands represent process state changes that may have occurred
since the last call to
.L coinit(),
e.g., current working directory or umask.
If
.L sh
is local (a child of the calling process)
.L coinit()
may return the empty string;
if
.L sh
is remote then considerably more information may be returned.
This routine is used by remote coshell implementations and is
not normally called from user code.
.TP
.L "void coquote(Sfio_T* sp, const char* string, int type)"
Applies shell single quoting to
.L string
and copies the result into the sfio stream
.L sp.
If
.L type!=0
then any occurence of \f5/\fP\fIhosttype\fP\f5/\fP is translated to
\f5/$HOSTTYPE/\fP, where
.I hosttype
is the current value of the
.L HOSTTYPE
environment variable.
This routine is used by remote coshell implementations and is
not normally called from user code.
.SH CAVEATS
.L cosync()
is a hack workaround, but we do have to work in the real world.
.PP
A bug in
.IR bsh (1)
and
.IR ksh (1)
implementations up to and including ksh88e causes some interrupted
jobs to return 0 exit status.
This should be fixed in later shell releases.
.PP
.L "trap 0"
is reserved by
.L coexec()
at the outermost scope.
To use
.L "trap 0"
use
.L "(...)"
to force a subshell.
.SH "SEE ALSO"
coshell(1), ksh(1), nmake(1), sh(1), cs(3), libast(3)