forked from SWI-Prolog/packages-clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix.pl
281 lines (249 loc) · 9.1 KB
/
unix.pl
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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2013, University of Amsterdam
VU University Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module(unix,
[ fork/1, % -'client'|pid
exec/1, % +Command(...Args...)
fork_exec/1, % +Command(...Args...)
wait/2, % -Pid, -Reason
kill/2, % +Pid. +Signal
pipe/2, % +Read, +Write
dup/2, % +From, +To
detach_IO/0,
detach_IO/1, % +Stream
environ/1 % -[Name=Value]
]).
:- use_module(library(shlib)).
/** <module> Unix specific operations
@ingroup SWIclib
The library(unix) library provides the commonly used Unix primitives to
deal with process management. These primitives are useful for many
tasks, including server management, parallel computation, exploiting and
controlling other processes, etc.
The predicates in this library are modelled closely after their native
Unix counterparts.
@see library(process) provides a portable high level interface to create
and manage processes.
*/
:- use_foreign_library(foreign(unix), install_unix).
%% fork(-Pid) is det.
%
% Clone the current process into two branches. In the child, Pid
% is unified to child. In the original process, Pid is unified to
% the process identifier of the created child. Both parent and
% child are fully functional Prolog processes running the same
% program. The processes share open I/O streams that refer to Unix
% native streams, such as files, sockets and pipes. Data is not
% shared, though on most Unix systems data is initially shared and
% duplicated only if one of the programs attempts to modify the
% data.
%
% Unix fork() is the only way to create new processes and fork/1
% is a simple direct interface to it.
%
% @error permission_error(fork, process, main) is raised if
% the calling thread is not the only thread in the
% process. Forking a Prolog process with threads
% will typically deadlock because only the calling
% thread is cloned in the fork, while all thread
% synchronization are cloned.
fork(Pid) :-
fork_warn_threads,
fork_(Pid).
%% fork_warn_threads
%
% See whether we are the only thread. If not, we cannot fork
fork_warn_threads :-
findall(T, other_thread(T), Others),
( Others == []
-> true
; throw(error(permission_error(fork, process, main),
context(_, running_threads(Others))))
).
other_thread(T) :-
thread_self(Me),
thread_property(T, status(Status)),
T \== Me,
( Status == running
-> true
; print_message(warning, fork(join(T, Status))),
thread_join(T, _),
fail
).
%% fork_exec(+Command) is det.
%
% Fork (as fork/1) and exec (using exec/1) the child immediately.
% This behaves as the code below, but bypasses the check for the
% existence of other threads because this is a safe scenario.
%
% ==
% fork_exec(Command) :-
% ( fork(child)
% -> exec(Command)
% ; true
% ).
% ==
fork_exec(Command) :-
( fork_(child)
-> exec(Command)
; true
).
%% exec(+Command)
%
% Replace the running program by starting Command. Command is a
% callable term. The functor is the command and the arguments
% provide the command-line arguments for the command. Each
% command-line argument must be atomic and is converted to a
% string before passed to the Unix call execvp(). Here are some
% examples:
%
% - exec(ls('-l'))
% - exec('/bin/ls'('-l', '/home/jan'))
%
% Unix exec() is the only way to start an executable file
% executing. It is commonly used together with fork/1. For example
% to start netscape on an URL in the background, do:
%
% ==
% run_netscape(URL) :-
% ( fork(child),
% exec(netscape(URL))
% ; true
% ).
% ==
%
% Using this code, netscape remains part of the process-group of
% the invoking Prolog process and Prolog does not wait for
% netscape to terminate. The predicate wait/2 allows waiting for a
% child, while detach_IO/0 disconnects the child as a deamon
% process.
%% wait(?Pid, -Status) is det.
%
% Wait for a child to change status. Then report the child that
% changed status as well as the reason. If Pid is bound on entry
% then the status of the specified child is reported. If not, then
% the status of any child is reported. Status is unified with
% exited(ExitCode) if the child with pid Pid was terminated by
% calling exit() (Prolog halt/1). ExitCode is the return status.
% Status is unified with signaled(Signal) if the child died due to
% a software interrupt (see kill/2). Signal contains the signal
% number. Finally, if the process suspended execution due to a
% signal, Status is unified with stopped(Signal).
%% kill(+Pid, +Signal) is det.
%
% Deliver a software interrupt to the process with identifier Pid
% using software-interrupt number Signal. See also on_signal/2.
% Signals can be specified as an integer or signal name, where
% signal names are derived from the C constant by dropping the
% =SIG= prefix and mapping to lowercase. E.g. =int= is the same as
% =SIGINT= in C. The meaning of the signal numbers can be found in
% the Unix manual.
%% pipe(-InSream, -OutStream) is det.
%
% Create a communication-pipe. This is normally used to make a
% child communicate to its parent. After pipe/2, the process is
% cloned and, depending on the desired direction, both processes
% close the end of the pipe they do not use. Then they use the
% remaining stream to communicate. Here is a simple example:
%
% ==
% :- use_module(library(unix)).
%
% fork_demo(Result) :-
% pipe(Read, Write),
% fork(Pid),
% ( Pid == child
% -> close(Read),
% format(Write, '~q.~n',
% [hello(world)]),
% flush_output(Write),
% halt
% ; close(Write),
% read(Read, Result),
% close(Read)
% ).
% ==
%% dup(+FromStream, +ToStream) is det.
%
% Interface to Unix dup2(), copying the underlying filedescriptor
% and thus making both streams point to the same underlying
% object. This is normally used together with fork/1 and pipe/2 to
% talk to an external program that is designed to communicate
% using standard I/O.
%
% Both FromStream and ToStream either refer to a Prolog stream or
% an integer descriptor number to refer directly to OS
% descriptors. See also demo/pipe.pl in the source-distribution of
% this package.
%% detach_IO(+Stream) is det.
%
% This predicate is intended to create Unix _deamon_ processes. It
% performs two actions.
%
% 1. The I/O streams =user_input=, =user_output= and
% =user_error= are closed if they are connected to a terminal
% (see =tty= property in stream_property/2). Input streams are
% rebound to a dummy stream that returns EOF. Output streams are
% reboud to forward their output to Stream.
%
% 2. The process is detached from the current process-group and
% its controlling terminal. This is achieved using setsid() if
% provided or using ioctl() =TIOCNOTTY= on =|/dev/tty|=.
%
% To ignore all output, it may be rebound to a null stream. For
% example:
%
% ==
% ...,
% open_null_stream(Out),
% detach_IO(Out).
% ==
%
% The detach_IO/1 should be called only once per process.
% Subsequent calls silently succeed without any side effects.
%
% @see detach_IO/0.
%% detach_IO is det.
%
% Detach I/O similar to detach_IO/1. The output streams are bound
% to a file =|/tmp/pl-out.<pid>|=. Output is line buffered (see
% set_stream/2).
%
% @compat Older versions of this predicate only created this file
% if there was output.
% @tbd If should be possible to use the syslog facilities for
% writing messages.
detach_IO :-
current_prolog_flag(pid, Pid),
atom_concat('/tmp/pl-out.', Pid, TmpFile),
open(TmpFile, write, Out, [alias(daemon_output)]),
set_stream(Out, buffer(line)),
detach_IO(Out).
/*******************************
* MESSAGES *
*******************************/
:- multifile
prolog:message//1.
prolog:message(fork(join(T, Status))) -->
[ 'Fork: joining thead ~p (status: ~p)'-[T, Status] ].