This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
One more typo, reported by H.Merijn Brand
[perl5.git] / pod / perlipc.pod
CommitLineData
a0d0e21e
LW
1=head1 NAME
2
184e9718 3perlipc - Perl interprocess communication (signals, fifos, pipes, safe subprocesses, sockets, and semaphores)
a0d0e21e
LW
4
5=head1 DESCRIPTION
6
4633a7c4
LW
7The basic IPC facilities of Perl are built out of the good old Unix
8signals, named pipes, pipe opens, the Berkeley socket routines, and SysV
9IPC calls. Each is used in slightly different situations.
10
11=head1 Signals
12
490f90af
JH
13Perl uses a simple signal handling model: the %SIG hash contains names
14or references of user-installed signal handlers. These handlers will
15be called with an argument which is the name of the signal that
16triggered it. A signal may be generated intentionally from a
17particular keyboard sequence like control-C or control-Z, sent to you
18from another process, or triggered automatically by the kernel when
cf21866a
TC
19special events transpire, like a child process exiting, your own process
20running out of stack space, or hitting a process file-size limit.
4633a7c4 21
a11adca0 22For example, to trap an interrupt signal, set up a handler like this:
4633a7c4 23
73af1a12
DG
24 our $shucks;
25
4633a7c4 26 sub catch_zap {
322c2516
SF
27 my $signame = shift;
28 $shucks++;
29 die "Somebody sent me a SIG$signame";
54310121 30 }
cf21866a 31 $SIG{INT} = __PACKAGE__ . "::catch_zap";
4633a7c4
LW
32 $SIG{INT} = \&catch_zap; # best strategy
33
490f90af
JH
34Prior to Perl 5.7.3 it was necessary to do as little as you possibly
35could in your handler; notice how all we do is set a global variable
36and then raise an exception. That's because on most systems,
37libraries are not re-entrant; particularly, memory allocation and I/O
38routines are not. That meant that doing nearly I<anything> in your
39handler could in theory trigger a memory fault and subsequent core
ec488bcf 40dump - see L</Deferred Signals (Safe Signals)> below.
a11adca0 41
4633a7c4
LW
42The names of the signals are the ones listed out by C<kill -l> on your
43system, or you can retrieve them from the Config module. Set up an
cf21866a 44@signame list indexed by number to get the name and a %signo hash table
4633a7c4
LW
45indexed by name to get the number:
46
47 use Config;
cf21866a
TC
48 defined($Config{sig_name}) || die "No sigs?";
49 foreach $name (split(" ", $Config{sig_name})) {
322c2516 50 $signo{$name} = $i;
cf21866a 51 $signame[$i] = $name;
322c2516 52 $i++;
54310121 53 }
4633a7c4 54
6a3992aa 55So to check whether signal 17 and SIGALRM were the same, do just this:
4633a7c4
LW
56
57 print "signal #17 = $signame[17]\n";
54310121 58 if ($signo{ALRM}) {
322c2516 59 print "SIGALRM is $signo{ALRM}\n";
54310121 60 }
4633a7c4 61
cf21866a 62You may also choose to assign the strings C<"IGNORE"> or C<"DEFAULT"> as
4633a7c4 63the handler, in which case Perl will try to discard the signal or do the
f648820c
GS
64default thing.
65
19799a22 66On most Unix platforms, the C<CHLD> (sometimes also known as C<CLD>) signal
cf21866a
TC
67has special behavior with respect to a value of C<"IGNORE">.
68Setting C<$SIG{CHLD}> to C<"IGNORE"> on such a platform has the effect of
f648820c 69not creating zombie processes when the parent process fails to C<wait()>
cf21866a
TC
70on its child processes (i.e., child processes are automatically reaped).
71Calling C<wait()> with C<$SIG{CHLD}> set to C<"IGNORE"> usually returns
f648820c
GS
72C<-1> on such platforms.
73
cf21866a
TC
74Some signals can be neither trapped nor ignored, such as the KILL and STOP
75(but not the TSTP) signals. One strategy for temporarily ignoring signals
76is to use a local() on that hash element, automatically restoring a
77previous value once your block is exited. Remember that values created by
78the dynamically-scoped local() are "inherited" by functions called from
79within their caller's scope.
4633a7c4
LW
80
81 sub precious {
cf21866a
TC
82 local $SIG{INT} = "IGNORE";
83 more_functions();
54310121 84 }
4633a7c4 85 sub more_functions {
322c2516 86 # interrupts still ignored, for now...
54310121 87 }
4633a7c4
LW
88
89Sending a signal to a negative process ID means that you send the signal
cf21866a
TC
90to the entire Unix process group. This code sends a hang-up signal to all
91processes in the current process group, and also sets $SIG{HUP} to C<"IGNORE">
92so it doesn't kill itself:
4633a7c4 93
cf21866a 94 # block scope for local
4633a7c4 95 {
cf21866a 96 local $SIG{HUP} = "IGNORE";
322c2516 97 kill HUP => -$$;
cf21866a 98 # snazzy writing of: kill("HUP", -$$)
4633a7c4 99 }
a0d0e21e 100
4633a7c4 101Another interesting signal to send is signal number zero. This doesn't
1e9c1022 102actually affect a child process, but instead checks whether it's alive
54310121 103or has changed its UID.
a0d0e21e 104
4633a7c4 105 unless (kill 0 => $kid_pid) {
322c2516 106 warn "something wicked happened to $kid_pid";
54310121 107 }
a0d0e21e 108
1e9c1022
JL
109When directed at a process whose UID is not identical to that
110of the sending process, signal number zero may fail because
111you lack permission to send the signal, even though the process is alive.
bf003f36 112You may be able to determine the cause of failure using C<%!>.
1e9c1022 113
cf21866a 114 unless (kill(0 => $pid) || $!{EPERM}) {
322c2516 115 warn "$pid looks dead";
1e9c1022
JL
116 }
117
4633a7c4
LW
118You might also want to employ anonymous functions for simple signal
119handlers:
a0d0e21e 120
4633a7c4 121 $SIG{INT} = sub { die "\nOutta here!\n" };
a0d0e21e 122
4633a7c4 123But that will be problematic for the more complicated handlers that need
54310121 124to reinstall themselves. Because Perl's signal mechanism is currently
184e9718 125based on the signal(3) function from the C library, you may sometimes be so
cf21866a 126unfortunate as to run on systems where that function is "broken"; that
4633a7c4
LW
127is, it behaves in the old unreliable SysV way rather than the newer, more
128reasonable BSD and POSIX fashion. So you'll see defensive people writing
129signal handlers like this:
a0d0e21e 130
54310121 131 sub REAPER {
322c2516
SF
132 $waitedpid = wait;
133 # loathe SysV: it makes us not only reinstate
134 # the handler, but place it after the wait
135 $SIG{CHLD} = \&REAPER;
4633a7c4
LW
136 }
137 $SIG{CHLD} = \&REAPER;
138 # now do something that forks...
139
816229cf 140or better still:
4633a7c4 141
6a3992aa 142 use POSIX ":sys_wait_h";
54310121 143 sub REAPER {
322c2516
SF
144 my $child;
145 # If a second child dies while in the signal handler caused by the
146 # first death, we won't get another signal. So must loop here else
147 # we will leave the unreaped child as a zombie. And the next time
148 # two children die we get another zombie. And so on.
cf21866a 149 while (($child = waitpid(-1, WNOHANG)) > 0) {
322c2516
SF
150 $Kid_Status{$child} = $?;
151 }
152 $SIG{CHLD} = \&REAPER; # still loathe SysV
4633a7c4
LW
153 }
154 $SIG{CHLD} = \&REAPER;
155 # do something that forks...
156
cf21866a
TC
157Be careful: qx(), system(), and some modules for calling external commands
158do a fork(), then wait() for the result. Thus, your signal handler
159(C<&REAPER> in the example) will be called. Because wait() was already
160called by system() or qx(), the wait() in the signal handler will see no
161more zombies and will therefore block.
0a18a49b 162
cf21866a 163The best way to prevent this issue is to use waitpid(), as in the following
0a18a49b
MH
164example:
165
166 use POSIX ":sys_wait_h"; # for nonblocking read
167
168 my %children;
169
170 $SIG{CHLD} = sub {
171 # don't change $! and $? outside handler
cf21866a 172 local ($!, $?);
0a18a49b
MH
173 my $pid = waitpid(-1, WNOHANG);
174 return if $pid == -1;
175 return unless defined $children{$pid};
176 delete $children{$pid};
177 cleanup_child($pid, $?);
178 };
179
180 while (1) {
181 my $pid = fork();
cf21866a 182 die "cannot fork" unless defined $pid;
0a18a49b
MH
183 if ($pid == 0) {
184 # ...
185 exit 0;
186 } else {
cf21866a 187 $children{$pid}=1;
0a18a49b
MH
188 # ...
189 system($command);
190 # ...
191 }
192 }
193
194Signal handling is also used for timeouts in Unix. While safely
4633a7c4
LW
195protected within an C<eval{}> block, you set a signal handler to trap
196alarm signals and then schedule to have one delivered to you in some
197number of seconds. Then try your blocking operation, clearing the alarm
198when it's done but not before you've exited your C<eval{}> block. If it
199goes off, you'll use die() to jump out of the block, much as you might
200using longjmp() or throw() in other languages.
201
202Here's an example:
203
cf21866a 204 my $ALARM_EXCEPTION = "alarm clock restart";
54310121 205 eval {
cf21866a 206 local $SIG{ALRM} = sub { die $ALARM_EXCEPTION };
54310121 207 alarm 10;
cf21866a
TC
208 flock(FH, 2) # blocking write lock
209 || die "cannot flock: $!";
54310121 210 alarm 0;
4633a7c4 211 };
cf21866a 212 if ($@ && $@ !~ quotemeta($ALARM_EXCEPTION)) { die }
4633a7c4 213
8a4f6ac2
GS
214If the operation being timed out is system() or qx(), this technique
215is liable to generate zombies. If this matters to you, you'll
216need to do your own fork() and exec(), and kill the errant child process.
217
4633a7c4
LW
218For more complex signal handling, you might see the standard POSIX
219module. Lamentably, this is almost entirely undocumented, but
220the F<t/lib/posix.t> file from the Perl source distribution has some
221examples in it.
222
28494392
SB
223=head2 Handling the SIGHUP Signal in Daemons
224
225A process that usually starts when the system boots and shuts down
226when the system is shut down is called a daemon (Disk And Execution
227MONitor). If a daemon process has a configuration file which is
228modified after the process has been started, there should be a way to
cf21866a
TC
229tell that process to reread its configuration file without stopping
230the process. Many daemons provide this mechanism using a C<SIGHUP>
231signal handler. When you want to tell the daemon to reread the file,
232simply send it the C<SIGHUP> signal.
28494392 233
3031ea75
JH
234Not all platforms automatically reinstall their (native) signal
235handlers after a signal delivery. This means that the handler works
cf21866a
TC
236the first time the signal is sent, only. The solution to this problem
237is to use C<POSIX> signal handlers if available; their behavior
3031ea75 238is well-defined.
28494392
SB
239
240The following example implements a simple daemon, which restarts
241itself every time the C<SIGHUP> signal is received. The actual code is
cf21866a
TC
242located in the subroutine C<code()>, which just prints some debugging
243info to show that it works; it should be replaced with the real code.
28494392
SB
244
245 #!/usr/bin/perl -w
d6fd60d6 246
28494392
SB
247 use POSIX ();
248 use FindBin ();
249 use File::Basename ();
250 use File::Spec::Functions;
d6fd60d6 251
cf21866a 252 $| = 1;
d6fd60d6 253
28494392
SB
254 # make the daemon cross-platform, so exec always calls the script
255 # itself with the right path, no matter how the script was invoked.
256 my $script = File::Basename::basename($0);
cf21866a 257 my $SELF = catfile($FindBin::Bin, $script);
d6fd60d6 258
28494392
SB
259 # POSIX unmasks the sigprocmask properly
260 my $sigset = POSIX::SigSet->new();
cf21866a 261 my $action = POSIX::SigAction->new("sigHUP_handler",
28494392
SB
262 $sigset,
263 &POSIX::SA_NODEFER);
264 POSIX::sigaction(&POSIX::SIGHUP, $action);
d6fd60d6 265
28494392
SB
266 sub sigHUP_handler {
267 print "got SIGHUP\n";
cf21866a 268 exec($SELF, @ARGV) || die "$0: couldn't restart: $!";
28494392 269 }
d6fd60d6 270
28494392 271 code();
d6fd60d6 272
28494392
SB
273 sub code {
274 print "PID: $$\n";
275 print "ARGV: @ARGV\n";
cf21866a
TC
276 my $count = 0;
277 while (++$count) {
28494392 278 sleep 2;
cf21866a 279 print "$count\n";
28494392
SB
280 }
281 }
28494392
SB
282
283
4633a7c4
LW
284=head1 Named Pipes
285
286A named pipe (often referred to as a FIFO) is an old Unix IPC
287mechanism for processes communicating on the same machine. It works
cf21866a
TC
288just like regular anonymous pipes, except that the
289processes rendezvous using a filename and need not be related.
4633a7c4 290
3341d187
AT
291To create a named pipe, use the C<POSIX::mkfifo()> function.
292
293 use POSIX qw(mkfifo);
cf21866a 294 mkfifo($path, 0700) || die "mkfifo $path failed: $!";
3341d187 295
cf21866a
TC
296You can also use the Unix command mknod(1), or on some
297systems, mkfifo(1). These may not be in your normal path, though.
4633a7c4
LW
298
299 # system return val is backwards, so && not ||
300 #
301 $ENV{PATH} .= ":/etc:/usr/etc";
cf21866a
TC
302 if ( system("mknod", $path, "p")
303 && system("mkfifo", $path) )
4633a7c4 304 {
322c2516 305 die "mk{nod,fifo} $path failed";
54310121 306 }
4633a7c4
LW
307
308
309A fifo is convenient when you want to connect a process to an unrelated
310one. When you open a fifo, the program will block until there's something
54310121 311on the other end.
4633a7c4
LW
312
313For example, let's say you'd like to have your F<.signature> file be a
314named pipe that has a Perl program on the other end. Now every time any
6a3992aa 315program (like a mailer, news reader, finger program, etc.) tries to read
cf21866a
TC
316from that file, the reading program will read the new signature from your
317program. We'll use the pipe-checking file-test operator, B<-p>, to find
318out whether anyone (or anything) has accidentally removed our fifo.
4633a7c4 319
cf21866a
TC
320 chdir(); # go home
321 my $FIFO = ".signature";
4633a7c4
LW
322
323 while (1) {
322c2516 324 unless (-p $FIFO) {
cf21866a
TC
325 unlink $FIFO; # discard any failure, will catch later
326 require POSIX; # delayed loading of heavy module
322c2516 327 POSIX::mkfifo($FIFO, 0700)
cf21866a 328 || die "can't mkfifo $FIFO: $!";
322c2516
SF
329 }
330
cf21866a
TC
331 # next line blocks till there's a reader
332 open (FIFO, "> $FIFO") || die "can't open $FIFO: $!";
322c2516 333 print FIFO "John Smith (smith\@host.org)\n", `fortune -s`;
cf21866a
TC
334 close(FIFO) || die "can't close $FIFO: $!";
335 sleep 2; # to avoid dup signals
4633a7c4 336 }
a0d0e21e 337
ffc145e8 338=head2 Deferred Signals (Safe Signals)
5a964f20 339
cf21866a
TC
340Before Perl 5.7.3, installing Perl code to deal with signals exposed you to
341danger from two things. First, few system library functions are
342re-entrant. If the signal interrupts while Perl is executing one function
343(like malloc(3) or printf(3)), and your signal handler then calls the same
344function again, you could get unpredictable behavior--often, a core dump.
345Second, Perl isn't itself re-entrant at the lowest levels. If the signal
346interrupts Perl while Perl is changing its own internal data structures,
347similarly unpredictable behavior may result.
5a964f20 348
a11adca0
NIS
349There were two things you could do, knowing this: be paranoid or be
350pragmatic. The paranoid approach was to do as little as possible in your
5a964f20
TC
351signal handler. Set an existing integer variable that already has a
352value, and return. This doesn't help you if you're in a slow system call,
7b34eba2 353which will just restart. That means you have to C<die> to longjmp(3) out
5a964f20
TC
354of the handler. Even this is a little cavalier for the true paranoiac,
355who avoids C<die> in a handler because the system I<is> out to get you.
b432a672
AL
356The pragmatic approach was to say "I know the risks, but prefer the
357convenience", and to do anything you wanted in your signal handler,
a11adca0
NIS
358and be prepared to clean up core dumps now and again.
359
cf21866a
TC
360Perl 5.7.3 and later avoid these problems by "deferring" signals. That is,
361when the signal is delivered to the process by the system (to the C code
362that implements Perl) a flag is set, and the handler returns immediately.
363Then at strategic "safe" points in the Perl interpreter (e.g. when it is
364about to execute a new opcode) the flags are checked and the Perl level
365handler from %SIG is executed. The "deferred" scheme allows much more
366flexibility in the coding of signal handlers as we know the Perl
367interpreter is in a safe state, and that we are not in a system library function when the handler is called. However the implementation does
368differ from previous Perls in the following ways:
5a964f20 369
a11adca0 370=over 4
5a964f20 371
e188fdae
CB
372=item Long-running opcodes
373
cf21866a 374As the Perl interpreter looks at signal flags only when it is about
e188fdae
CB
375to execute a new opcode, a signal that arrives during a long-running
376opcode (e.g. a regular expression operation on a very large string) will
377not be seen until the current opcode completes.
378
cf21866a 379If a signal of any given type fires multiple times during an opcode
e188fdae 380(such as from a fine-grained timer), the handler for that signal will
cf21866a 381be called only once, after the opcode completes; all other
e188fdae
CB
382instances will be discarded. Furthermore, if your system's signal queue
383gets flooded to the point that there are signals that have been raised
384but not yet caught (and thus not deferred) at the time an opcode
385completes, those signals may well be caught and deferred during
386subsequent opcodes, with sometimes surprising results. For example, you
387may see alarms delivered even after calling C<alarm(0)> as the latter
388stops the raising of alarms but does not cancel the delivery of alarms
389raised but not yet caught. Do not depend on the behaviors described in
390this paragraph as they are side effects of the current implementation and
391may change in future versions of Perl.
a11adca0 392
a11adca0
NIS
393=item Interrupting IO
394
cf21866a
TC
395When a signal is delivered (e.g., SIGINT from a control-C) the operating
396system breaks into IO operations like I<read>(2), which is used to
397implement Perl's readline() function, the C<< <> >> operator. On older
398Perls the handler was called immediately (and as C<read> is not "unsafe",
399this worked well). With the "deferred" scheme the handler is I<not> called
400immediately, and if Perl is using the system's C<stdio> library that
401library may restart the C<read> without returning to Perl to give it a
402chance to call the %SIG handler. If this happens on your system the
403solution is to use the C<:perlio> layer to do IO--at least on those handles
404that you want to be able to break into with signals. (The C<:perlio> layer
405checks the signal flags and calls %SIG handlers before resuming IO
406operation.)
407
408The default in Perl 5.7.3 and later is to automatically use
490f90af 409the C<:perlio> layer.
a11adca0 410
cf21866a
TC
411Some networking library functions like gethostbyname() are known to have
412their own implementations of timeouts which may conflict with your
413timeouts. If you have problems with such functions, try using the POSIX
414sigaction() function, which bypasses Perl safe signals. Be warned that
415this does subject you to possible memory corruption, as described above.
416
417Instead of setting C<$SIG{ALRM}>:
91d81acc 418
e399c6ae
SB
419 local $SIG{ALRM} = sub { die "alarm" };
420
421try something like the following:
422
423 use POSIX qw(SIGALRM);
424 POSIX::sigaction(SIGALRM,
425 POSIX::SigAction->new(sub { die "alarm" }))
cf21866a 426 || die "Error setting SIGALRM handler: $!\n";
91d81acc 427
a1966b02 428Another way to disable the safe signal behavior locally is to use
cf21866a
TC
429the C<Perl::Unsafe::Signals> module from CPAN, which affects
430all signals.
a1966b02 431
9ce5b4ad
SG
432=item Restartable system calls
433
434On systems that supported it, older versions of Perl used the
435SA_RESTART flag when installing %SIG handlers. This meant that
436restartable system calls would continue rather than returning when
437a signal arrived. In order to deliver deferred signals promptly,
438Perl 5.7.3 and later do I<not> use SA_RESTART. Consequently,
439restartable system calls can fail (with $! set to C<EINTR>) in places
440where they previously would have succeeded.
441
cf21866a
TC
442The default C<:perlio> layer retries C<read>, C<write>
443and C<close> as described above; interrupted C<wait> and
9ce5b4ad
SG
444C<waitpid> calls will always be retried.
445
a11adca0
NIS
446=item Signals as "faults"
447
cf21866a
TC
448Certain signals like SEGV, ILL, and BUS are generated by virtual memory
449addressing errors and similiar "faults". These are normally fatal: there is
450little a Perl-level handler can do with them. So Perl now delivers them
e188fdae 451immediately rather than attempting to defer them.
a11adca0
NIS
452
453=item Signals triggered by operating system state
454
490f90af 455On some operating systems certain signal handlers are supposed to "do
cf21866a 456something" before returning. One example can be CHLD or CLD, which
490f90af
JH
457indicates a child process has completed. On some operating systems the
458signal handler is expected to C<wait> for the completed child
459process. On such systems the deferred signal scheme will not work for
cf21866a
TC
460those signals: it does not do the C<wait>. Again the failure will
461look like a loop as the operating system will reissue the signal because
462there are completed child processes that have not yet been C<wait>ed for.
a11adca0 463
818c4caa 464=back
a0d0e21e 465
cf21866a 466If you want the old signal behavior back despite possible
4ffa73a3 467memory corruption, set the environment variable C<PERL_SIGNALS> to
cf21866a 468C<"unsafe">. This feature first appeared in Perl 5.8.1.
4ffa73a3 469
4633a7c4
LW
470=head1 Using open() for IPC
471
490f90af
JH
472Perl's basic open() statement can also be used for unidirectional
473interprocess communication by either appending or prepending a pipe
474symbol to the second argument to open(). Here's how to start
475something up in a child process you intend to write to:
4633a7c4 476
54310121 477 open(SPOOLER, "| cat -v | lpr -h 2>/dev/null")
cf21866a 478 || die "can't fork: $!";
4633a7c4
LW
479 local $SIG{PIPE} = sub { die "spooler pipe broke" };
480 print SPOOLER "stuff\n";
cf21866a 481 close SPOOLER || die "bad spool: $! $?";
4633a7c4
LW
482
483And here's how to start up a child process you intend to read from:
484
485 open(STATUS, "netstat -an 2>&1 |")
cf21866a 486 || die "can't fork: $!";
4633a7c4 487 while (<STATUS>) {
322c2516
SF
488 next if /^(tcp|udp)/;
489 print;
54310121 490 }
cf21866a 491 close STATUS || die "bad netstat: $! $?";
4633a7c4 492
cf21866a
TC
493If one can be sure that a particular program is a Perl script expecting
494filenames in @ARGV, the clever programmer can write something like this:
4633a7c4 495
5a964f20 496 % program f1 "cmd1|" - f2 "cmd2|" f3 < tmpfile
4633a7c4 497
cf21866a 498and no matter which sort of shell it's called from, the Perl program will
4633a7c4
LW
499read from the file F<f1>, the process F<cmd1>, standard input (F<tmpfile>
500in this case), the F<f2> file, the F<cmd2> command, and finally the F<f3>
501file. Pretty nifty, eh?
502
54310121 503You might notice that you could use backticks for much the
4633a7c4
LW
504same effect as opening a pipe for reading:
505
506 print grep { !/^(tcp|udp)/ } `netstat -an 2>&1`;
cf21866a 507 die "bad netstatus ($?)" if $?;
4633a7c4
LW
508
509While this is true on the surface, it's much more efficient to process the
510file one line or record at a time because then you don't have to read the
19799a22 511whole thing into memory at once. It also gives you finer control of the
cf21866a 512whole process, letting you kill off the child process early if you'd like.
4633a7c4 513
cf21866a 514Be careful to check the return values from both open() and close(). If
4633a7c4
LW
515you're I<writing> to a pipe, you should also trap SIGPIPE. Otherwise,
516think of what happens when you start up a pipe to a command that doesn't
517exist: the open() will in all likelihood succeed (it only reflects the
518fork()'s success), but then your output will fail--spectacularly. Perl
cf21866a 519can't know whether the command worked, because your command is actually
4633a7c4 520running in a separate process whose exec() might have failed. Therefore,
cf21866a
TC
521while readers of bogus commands return just a quick EOF, writers
522to bogus commands will get hit with a signal, which they'd best be prepared
523to handle. Consider:
4633a7c4 524
cf21866a
TC
525 open(FH, "|bogus") || die "can't fork: $!";
526 print FH "bang\n"; # neither necessary nor sufficient
527 # to check print retval!
528 close(FH) || die "can't close: $!";
5a964f20 529
cf21866a
TC
530The reason for not checking the return value from print() is because of
531pipe buffering; physical writes are delayed. That won't blow up until the
532close, and it will blow up with a SIGPIPE. To catch it, you could use
533this:
5a964f20 534
cf21866a
TC
535 $SIG{PIPE} = "IGNORE";
536 open(FH, "|bogus") || die "can't fork: $!";
537 print FH "bang\n";
538 close(FH) || die "can't close: status=$?";
4633a7c4 539
68dc0745
PP
540=head2 Filehandles
541
5a964f20
TC
542Both the main process and any child processes it forks share the same
543STDIN, STDOUT, and STDERR filehandles. If both processes try to access
45bc9206 544them at once, strange things can happen. You may also want to close
5a964f20
TC
545or reopen the filehandles for the child. You can get around this by
546opening your pipe with open(), but on some systems this means that the
547child process cannot outlive the parent.
68dc0745
PP
548
549=head2 Background Processes
550
551You can run a command in the background with:
552
7b05b7e3 553 system("cmd &");
68dc0745
PP
554
555The command's STDOUT and STDERR (and possibly STDIN, depending on your
556shell) will be the same as the parent's. You won't need to catch
cf21866a 557SIGCHLD because of the double-fork taking place; see below for details.
68dc0745
PP
558
559=head2 Complete Dissociation of Child from Parent
560
561In some cases (starting server processes, for instance) you'll want to
893af57a 562completely dissociate the child process from the parent. This is
cf21866a
TC
563often called daemonization. A well-behaved daemon will also chdir()
564to the root directory so it doesn't prevent unmounting the filesystem
565containing the directory from which it was launched, and redirect its
566standard file descriptors from and to F</dev/null> so that random
567output doesn't wind up on the user's terminal.
893af57a 568
cf21866a 569 use POSIX "setsid";
893af57a
RS
570
571 sub daemonize {
cf21866a
TC
572 chdir("/") || die "can't chdir to /: $!";
573 open(STDIN, "< /dev/null") || die "can't read /dev/null: $!";
574 open(STDOUT, "> /dev/null") || die "can't write to /dev/null: $!";
575 defined(my $pid = fork()) || die "can't fork: $!";
c0919ef1 576 exit if $pid; # non-zero now means I am the parent
cf21866a
TC
577 (setsid() != -1) || die "Can't start a new session: $!"
578 open(STDERR, ">&STDOUT") || die "can't dup stdout: $!";
893af57a 579 }
5a964f20 580
cf21866a
TC
581The fork() has to come before the setsid() to ensure you aren't a
582process group leader; the setsid() will fail if you are. If your
893af57a 583system doesn't have the setsid() function, open F</dev/tty> and use the
f979aebc 584C<TIOCNOTTY> ioctl() on it instead. See tty(4) for details.
5a964f20 585
cf21866a
TC
586Non-Unix users should check their C<< I<Your_OS>::Process >> module for
587other possible solutions.
68dc0745 588
4633a7c4
LW
589=head2 Safe Pipe Opens
590
591Another interesting approach to IPC is making your single program go
cf21866a 592multiprocess and communicate between--or even amongst--yourselves. The
4633a7c4
LW
593open() function will accept a file argument of either C<"-|"> or C<"|-">
594to do a very interesting thing: it forks a child connected to the
595filehandle you've opened. The child is running the same program as the
596parent. This is useful for safely opening a file when running under an
597assumed UID or GID, for example. If you open a pipe I<to> minus, you can
cf21866a 598write to the filehandle you opened and your kid will find it in I<his>
4633a7c4 599STDIN. If you open a pipe I<from> minus, you can read from the filehandle
cf21866a 600you opened whatever your kid writes to I<his> STDOUT.
4633a7c4 601
cf21866a
TC
602 use English qw[ -no_match_vars ];
603 my $PRECIOUS = "/path/to/some/safe/file";
604 my $sleep_count;
605 my $pid;
4633a7c4 606
54310121 607 do {
322c2516
SF
608 $pid = open(KID_TO_WRITE, "|-");
609 unless (defined $pid) {
610 warn "cannot fork: $!";
611 die "bailing out" if $sleep_count++ > 6;
612 sleep 10;
613 }
4633a7c4
LW
614 } until defined $pid;
615
cf21866a 616 if ($pid) { # I am the parent
322c2516 617 print KID_TO_WRITE @some_data;
cf21866a
TC
618 close(KID_TO_WRITE) || warn "kid exited $?";
619 } else { # I am the child
620 # drop permissions in setuid and/or setgid programs:
621 ($EUID, $EGID) = ($UID, $GID);
622 open (OUTFILE, "> $PRECIOUS")
623 || die "can't open $PRECIOUS: $!";
322c2516 624 while (<STDIN>) {
cf21866a 625 print OUTFILE; # child's STDIN is parent's KID_TO_WRITE
322c2516 626 }
cf21866a
TC
627 close(OUTFILE) || die "can't close $PRECIOUS: $!";
628 exit(0); # don't forget this!!
54310121 629 }
4633a7c4
LW
630
631Another common use for this construct is when you need to execute
632something without the shell's interference. With system(), it's
54310121 633straightforward, but you can't use a pipe open or backticks safely.
4633a7c4
LW
634That's because there's no way to stop the shell from getting its hands on
635your arguments. Instead, use lower-level control to call exec() directly.
636
54310121 637Here's a safe backtick or pipe open for read:
4633a7c4 638
cf21866a
TC
639 my $pid = open(KID_TO_READ, "-|");
640 defined($pid) || die "can't fork: $!";
4633a7c4 641
cf21866a 642 if ($pid) { # parent
322c2516 643 while (<KID_TO_READ>) {
cf21866a 644 # do something interesting
322c2516 645 }
cf21866a 646 close(KID_TO_READ) || warn "kid exited $?";
4633a7c4 647
cf21866a 648 } else { # child
322c2516
SF
649 ($EUID, $EGID) = ($UID, $GID); # suid only
650 exec($program, @options, @args)
cf21866a 651 || die "can't exec program: $!";
322c2516 652 # NOTREACHED
54310121 653 }
4633a7c4 654
4633a7c4
LW
655And here's a safe pipe open for writing:
656
cf21866a
TC
657 my $pid = open(KID_TO_WRITE, "|-");
658 defined($pid) || die "can't fork: $!";
659
76c0e0db 660 $SIG{PIPE} = sub { die "whoops, $program pipe broke" };
4633a7c4 661
cf21866a
TC
662 if ($pid) { # parent
663 print KID_TO_WRITE @data;
322c2516 664 close(KID_TO_WRITE) || warn "kid exited $?";
4633a7c4 665
cf21866a 666 } else { # child
322c2516
SF
667 ($EUID, $EGID) = ($UID, $GID);
668 exec($program, @options, @args)
cf21866a 669 || die "can't exec program: $!";
322c2516 670 # NOTREACHED
54310121 671 }
4633a7c4 672
c40e8e9b 673It is very easy to dead-lock a process using this form of open(), or
cf21866a
TC
674indeed with any use of pipe() with multiple subprocesses. The
675example above is "safe" because it is simple and calls exec(). See
c40e8e9b
SV
676L</"Avoiding Pipe Deadlocks"> for general safety principles, but there
677are extra gotchas with Safe Pipe Opens.
678
679In particular, if you opened the pipe using C<open FH, "|-">, then you
680cannot simply use close() in the parent process to close an unwanted
681writer. Consider this code:
682
cf21866a
TC
683 my $pid = open(WRITER, "|-"); # fork open a kid
684 defined($pid) || die "first fork failed: $!";
c40e8e9b
SV
685 if ($pid) {
686 if (my $sub_pid = fork()) {
cf21866a
TC
687 defined($sub_pid) || die "second fork failed: $!";
688 close(WRITER) || die "couldn't close WRITER: $!";
689 # now do something else...
c40e8e9b
SV
690 }
691 else {
cf21866a
TC
692 # first write to WRITER
693 # ...
694 # then when finished
695 close(WRITER) || die "couldn't close WRITER: $!";
696 exit(0);
c40e8e9b
SV
697 }
698 }
699 else {
cf21866a
TC
700 # first do something with STDIN, then
701 exit(0);
c40e8e9b
SV
702 }
703
cf21866a 704In the example above, the true parent does not want to write to the WRITER
c40e8e9b 705filehandle, so it closes it. However, because WRITER was opened using
cf21866a
TC
706C<open FH, "|-">, it has a special behavior: closing it calls
707waitpid() (see L<perlfunc/waitpid>), which waits for the subprocess
c40e8e9b 708to exit. If the child process ends up waiting for something happening
cf21866a 709in the section marked "do something else", you have deadlock.
c40e8e9b 710
cf21866a 711This can also be a problem with intermediate subprocesses in more
c40e8e9b 712complicated code, which will call waitpid() on all open filehandles
cf21866a 713during global destruction--in no predictable order.
c40e8e9b
SV
714
715To solve this, you must manually use pipe(), fork(), and the form of
cf21866a 716open() which sets one file descriptor to another, as shown below:
c40e8e9b 717
cf21866a 718 pipe(READER, WRITER) || die "pipe failed: $!";
c40e8e9b 719 $pid = fork();
cf21866a 720 defined($pid) || die "first fork failed: $!";
c40e8e9b 721 if ($pid) {
322c2516 722 close READER;
c40e8e9b 723 if (my $sub_pid = fork()) {
cf21866a
TC
724 defined($sub_pid) || die "first fork failed: $!";
725 close(WRITER) || die "can't close WRITER: $!";
c40e8e9b
SV
726 }
727 else {
728 # write to WRITER...
cf21866a
TC
729 # ...
730 # then when finished
731 close(WRITER) || die "can't close WRITER: $!";
732 exit(0);
c40e8e9b
SV
733 }
734 # write to WRITER...
735 }
736 else {
cf21866a
TC
737 open(STDIN, "<&READER") || die "can't reopen STDIN: $!";
738 close(WRITER) || die "can't close WRITER: $!";
c40e8e9b 739 # do something...
cf21866a 740 exit(0);
c40e8e9b
SV
741 }
742
cf21866a
TC
743Since Perl 5.8.0, you can also use the list form of C<open> for pipes.
744This is preferred when you wish to avoid having the shell interpret
745metacharacters that may be in your command string.
307eac13 746
cf21866a 747So for example, instead of using:
307eac13 748
cf21866a 749 open(PS_PIPE, "ps aux|") || die "can't open ps pipe: $!";
307eac13 750
cf21866a 751One would use either of these:
4633a7c4 752
cf21866a
TC
753 open(PS_PIPE, "-|", "ps", "aux")
754 || die "can't open ps pipe: $!";
c40e8e9b 755
cf21866a
TC
756 @ps_args = qw[ ps aux ];
757 open(PS_PIPE, "-|", @ps_args)
758 || die "can't open @ps_args|: $!";
c40e8e9b 759
cf21866a
TC
760Because there are more than three arguments to open(), forks the ps(1)
761command I<without> spawning a shell, and reads its standard output via the
762C<PS_PIPE> filehandle. The corresponding syntax to I<write> to command
763pipes is to use C<"|-"> in place of C<"-|">.
c40e8e9b 764
cf21866a
TC
765This was admittedly a rather silly example, because you're using string
766literals whose content is perfectly safe. There is therefore no cause to
faa783ac 767resort to the harder-to-read, multi-argument form of pipe open(). However,
cf21866a
TC
768whenever you cannot be assured that the program arguments are free of shell
769metacharacters, the fancier form of open() should be used. For example:
c40e8e9b 770
cf21866a
TC
771 @grep_args = ("egrep", "-i", $some_pattern, @many_files);
772 open(GREP_PIPE, "-|", @grep_args)
773 || die "can't open @grep_args|: $!";
774
775Here the multi-argument form of pipe open() is preferred because the
776pattern and indeed even the filenames themselves might hold metacharacters.
777
778Be aware that these operations are full Unix forks, which means they may
779not be correctly implemented on all alien systems. Additionally, these are
780not true multithreading. To learn more about threading, see the F<modules>
781file mentioned below in the SEE ALSO section.
782
783=head2 Avoiding Pipe Deadlocks
784
785Whenever you have more than one subprocess, you must be careful that each
786closes whichever half of any pipes created for interprocess communication
787it is not using. This is because any child process reading from the pipe
788and expecting an EOF will never receive it, and therefore never exit. A
789single process closing a pipe is not enough to close it; the last process
790with the pipe open must close it for it to read EOF.
791
792Certain built-in Unix features help prevent this most of the time. For
793instance, filehandles have a "close on exec" flag, which is set I<en masse>
794under control of the C<$^F> variable. This is so any filehandles you
795didn't explicitly route to the STDIN, STDOUT or STDERR of a child
796I<program> will be automatically closed.
797
798Always explicitly and immediately call close() on the writable end of any
799pipe, unless that process is actually writing to it. Even if you don't
800explicitly call close(), Perl will still close() all filehandles during
801global destruction. As previously discussed, if those filehandles have
802been opened with Safe Pipe Open, this will result in calling waitpid(),
803which may again deadlock.
c40e8e9b 804
7b05b7e3 805=head2 Bidirectional Communication with Another Process
4633a7c4
LW
806
807While this works reasonably well for unidirectional communication, what
cf21866a 808about bidirectional communication? The most obvious approach doesn't work:
4633a7c4 809
cf21866a 810 # THIS DOES NOT WORK!!
c07a80fd 811 open(PROG_FOR_READING_AND_WRITING, "| some program |")
4633a7c4 812
cf21866a
TC
813If you forget to C<use warnings>, you'll miss out entirely on the
814helpful diagnostic message:
4633a7c4
LW
815
816 Can't do bidirectional pipe at -e line 1.
817
cf21866a
TC
818If you really want to, you can use the standard open2() from the
819C<IPC::Open2> module to catch both ends. There's also an open3() in
820C<IPC::Open3> for tridirectional I/O so you can also catch your child's
821STDERR, but doing so would then require an awkward select() loop and
822wouldn't allow you to use normal Perl input operations.
4633a7c4
LW
823
824If you look at its source, you'll see that open2() uses low-level
cf21866a
TC
825primitives like the pipe() and exec() syscalls to create all the
826connections. Although it might have been more efficient by using
827socketpair(), this would have been even less portable than it already
828is. The open2() and open3() functions are unlikely to work anywhere
829except on a Unix system, or at least one purporting POSIX compliance.
830
831=for TODO
832Hold on, is this even true? First it says that socketpair() is avoided
833for portability, but then it says it probably won't work except on
834Unixy systems anyway. Which one of those is true?
4633a7c4
LW
835
836Here's an example of using open2():
837
838 use FileHandle;
839 use IPC::Open2;
cf21866a 840 $pid = open2(*Reader, *Writer, "cat -un");
4633a7c4
LW
841 print Writer "stuff\n";
842 $got = <Reader>;
843
cf21866a
TC
844The problem with this is that buffering is really going to ruin your
845day. Even though your C<Writer> filehandle is auto-flushed so the process
846on the other end gets your data in a timely manner, you can't usually do
847anything to force that process to give its data to you in a similarly quick
848fashion. In this special case, we could actually so, because we gave
849I<cat> a B<-u> flag to make it unbuffered. But very few commands are
850designed to operate over pipes, so this seldom works unless you yourself
851wrote the program on the other end of the double-ended pipe.
852
853A solution to this is to use a library which uses pseudottys to make your
854program behave more reasonably. This way you don't have to have control
855over the source code of the program you're using. The C<Expect> module
856from CPAN also addresses this kind of thing. This module requires two
857other modules from CPAN, C<IO::Pty> and C<IO::Stty>. It sets up a pseudo
858terminal to interact with programs that insist on talking to the terminal
859device driver. If your system is supported, this may be your best bet.
c8db1d39 860
5a964f20
TC
861=head2 Bidirectional Communication with Yourself
862
cf21866a
TC
863If you want, you may make low-level pipe() and fork() syscalls to stitch
864this together by hand. This example only talks to itself, but you could
865reopen the appropriate handles to STDIN and STDOUT and call other processes.
866(The following example lacks proper error checking.)
5a964f20
TC
867
868 #!/usr/bin/perl -w
869 # pipe1 - bidirectional communication using two pipe pairs
870 # designed for the socketpair-challenged
322c2516 871 use IO::Handle; # thousands of lines just for autoflush :-(
cf21866a
TC
872 pipe(PARENT_RDR, CHILD_WTR); # XXX: check failure?
873 pipe(CHILD_RDR, PARENT_WTR); # XXX: check failure?
5a964f20
TC
874 CHILD_WTR->autoflush(1);
875 PARENT_WTR->autoflush(1);
876
cf21866a
TC
877 if ($pid = fork()) {
878 close PARENT_RDR;
879 close PARENT_WTR;
322c2516
SF
880 print CHILD_WTR "Parent Pid $$ is sending this\n";
881 chomp($line = <CHILD_RDR>);
882 print "Parent Pid $$ just read this: `$line'\n";
883 close CHILD_RDR; close CHILD_WTR;
cf21866a 884 waitpid($pid, 0);
5a964f20 885 } else {
322c2516 886 die "cannot fork: $!" unless defined $pid;
cf21866a
TC
887 close CHILD_RDR;
888 close CHILD_WTR;
322c2516
SF
889 chomp($line = <PARENT_RDR>);
890 print "Child Pid $$ just read this: `$line'\n";
891 print PARENT_WTR "Child Pid $$ is sending this\n";
cf21866a
TC
892 close PARENT_RDR;
893 close PARENT_WTR;
894 exit(0);
5a964f20
TC
895 }
896
a11adca0 897But you don't actually have to make two pipe calls. If you
5a964f20
TC
898have the socketpair() system call, it will do this all for you.
899
900 #!/usr/bin/perl -w
901 # pipe2 - bidirectional communication using socketpair
902 # "the best ones always go both ways"
903
904 use Socket;
322c2516 905 use IO::Handle; # thousands of lines just for autoflush :-(
cf21866a 906
5a964f20
TC
907 # We say AF_UNIX because although *_LOCAL is the
908 # POSIX 1003.1g form of the constant, many machines
909 # still don't have it.
910 socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
cf21866a 911 || die "socketpair: $!";
5a964f20
TC
912
913 CHILD->autoflush(1);
914 PARENT->autoflush(1);
915
cf21866a 916 if ($pid = fork()) {
322c2516
SF
917 close PARENT;
918 print CHILD "Parent Pid $$ is sending this\n";
919 chomp($line = <CHILD>);
920 print "Parent Pid $$ just read this: `$line'\n";
921 close CHILD;
cf21866a 922 waitpid($pid, 0);
5a964f20 923 } else {
322c2516
SF
924 die "cannot fork: $!" unless defined $pid;
925 close CHILD;
926 chomp($line = <PARENT>);
cf21866a 927 print "Child Pid $$ just read this: '$line'\n";
322c2516
SF
928 print PARENT "Child Pid $$ is sending this\n";
929 close PARENT;
cf21866a 930 exit(0);
5a964f20
TC
931 }
932
4633a7c4 933=head1 Sockets: Client/Server Communication
a0d0e21e 934
cf21866a
TC
935While not entirely limited to Unix-derived operating systems (e.g., WinSock
936on PCs provides socket support, as do some VMS libraries), you might not have
937sockets on your system, in which case this section probably isn't going to
938do you much good. With sockets, you can do both virtual circuits like TCP
939streams and datagrams like UDP packets. You may be able to do even more
4633a7c4
LW
940depending on your system.
941
cf21866a 942The Perl functions for dealing with sockets have the same names as
4633a7c4 943the corresponding system calls in C, but their arguments tend to differ
cf21866a 944for two reasons. First, Perl filehandles work differently than C file
4633a7c4
LW
945descriptors. Second, Perl already knows the length of its strings, so you
946don't need to pass that information.
a0d0e21e 947
cf21866a
TC
948One of the major problems with ancient, antemillennial socket code in Perl
949was that it used hard-coded values for some of the constants, which
950severely hurt portability. If you ever see code that does anything like
951explicitly setting C<$AF_INET = 2>, you know you're in for big trouble.
952An immeasurably superior approach is to use the C<Socket> module, which more
953reliably grants access to the various constants and functions you'll need.
a0d0e21e 954
68dc0745
PP
955If you're not writing a server/client for an existing protocol like
956NNTP or SMTP, you should give some thought to how your server will
957know when the client has finished talking, and vice-versa. Most
958protocols are based on one-line messages and responses (so one party
4a6725af 959knows the other has finished when a "\n" is received) or multi-line
68dc0745
PP
960messages and responses that end with a period on an empty line
961("\n.\n" terminates a message/response).
962
5a964f20
TC
963=head2 Internet Line Terminators
964
965The Internet line terminator is "\015\012". Under ASCII variants of
966Unix, that could usually be written as "\r\n", but under other systems,
967"\r\n" might at times be "\015\015\012", "\012\012\015", or something
968completely different. The standards specify writing "\015\012" to be
969conformant (be strict in what you provide), but they also recommend
cf21866a 970accepting a lone "\012" on input (be lenient in what you require).
5a964f20 971We haven't always been very good about that in the code in this manpage,
cf21866a
TC
972but unless you're on a Mac from way back in its pre-Unix dark ages, you'll
973probably be ok.
5a964f20 974
4633a7c4 975=head2 Internet TCP Clients and Servers
a0d0e21e 976
4633a7c4
LW
977Use Internet-domain sockets when you want to do client-server
978communication that might extend to machines outside of your own system.
979
980Here's a sample TCP client using Internet-domain sockets:
981
982 #!/usr/bin/perl -w
4633a7c4
LW
983 use strict;
984 use Socket;
cf21866a 985 my ($remote, $port, $iaddr, $paddr, $proto, $line);
4633a7c4 986
cf21866a 987 $remote = shift || "localhost";
4633a7c4 988 $port = shift || 2345; # random port
cf21866a 989 if ($port =~ /\D/) { $port = getservbyname($port, "tcp") }
4633a7c4 990 die "No port" unless $port;
322c2516 991 $iaddr = inet_aton($remote) || die "no host: $remote";
4633a7c4
LW
992 $paddr = sockaddr_in($port, $iaddr);
993
cf21866a 994 $proto = getprotobyname("tcp");
322c2516 995 socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
cf21866a
TC
996 connect(SOCK, $paddr) || die "connect: $!";
997 while ($line = <SOCK>) {
322c2516 998 print $line;
54310121 999 }
4633a7c4 1000
cf21866a
TC
1001 close (SOCK) || die "close: $!";
1002 exit(0);
4633a7c4
LW
1003
1004And here's a corresponding server to go along with it. We'll
cf21866a 1005leave the address as C<INADDR_ANY> so that the kernel can choose
54310121 1006the appropriate interface on multihomed hosts. If you want sit
c07a80fd 1007on a particular interface (like the external side of a gateway
cf21866a 1008or firewall machine), fill this in with your real address instead.
c07a80fd
PP
1009
1010 #!/usr/bin/perl -Tw
c07a80fd 1011 use strict;
cf21866a 1012 BEGIN { $ENV{PATH} = "/usr/bin:/bin" }
c07a80fd
PP
1013 use Socket;
1014 use Carp;
5865a7df 1015 my $EOL = "\015\012";
c07a80fd 1016
cf21866a 1017 sub logmsg { print "$0 $$: @_ at ", scalar localtime(), "\n" }
c07a80fd 1018
cf21866a
TC
1019 my $port = shift || 2345;
1020 die "invalid port" unless if $port =~ /^ \d+ $/x;
51ee6500 1021
cf21866a 1022 my $proto = getprotobyname("tcp");
6a3992aa 1023
322c2516 1024 socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
cf21866a
TC
1025 setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
1026 || die "setsockopt: $!";
322c2516 1027 bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!";
cf21866a 1028 listen(Server, SOMAXCONN) || die "listen: $!";
c07a80fd
PP
1029
1030 logmsg "server started on port $port";
1031
1032 my $paddr;
1033
1034 $SIG{CHLD} = \&REAPER;
1035
cf21866a
TC
1036 for ( ; $paddr = accept(Client, Server); close Client) {
1037 my($port, $iaddr) = sockaddr_in($paddr);
1038 my $name = gethostbyaddr($iaddr, AF_INET);
c07a80fd 1039
322c2516
SF
1040 logmsg "connection from $name [",
1041 inet_ntoa($iaddr), "]
1042 at port $port";
c07a80fd 1043
322c2516 1044 print Client "Hello there, $name, it's now ",
cf21866a 1045 scalar localtime(), $EOL;
54310121 1046 }
c07a80fd 1047
54310121 1048And here's a multithreaded version. It's multithreaded in that
cf21866a 1049like most typical servers, it spawns (fork()s) a slave server to
c07a80fd
PP
1050handle the client request so that the master server can quickly
1051go back to service a new client.
4633a7c4
LW
1052
1053 #!/usr/bin/perl -Tw
4633a7c4 1054 use strict;
cf21866a 1055 BEGIN { $ENV{PATH} = "/usr/bin:/bin" }
a0d0e21e 1056 use Socket;
4633a7c4 1057 use Carp;
5865a7df 1058 my $EOL = "\015\012";
a0d0e21e 1059
4633a7c4 1060 sub spawn; # forward declaration
cf21866a 1061 sub logmsg { print "$0 $$: @_ at ", scalar localtime(), "\n" }
a0d0e21e 1062
cf21866a
TC
1063 my $port = shift || 2345;
1064 die "invalid port" unless if $port =~ /^ \d+ $/x;
51ee6500 1065
cf21866a 1066 my $proto = getprotobyname("tcp");
54310121 1067
322c2516 1068 socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
cf21866a
TC
1069 setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
1070 || die "setsockopt: $!";
1071 bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!";
1072 listen(Server, SOMAXCONN) || die "listen: $!";
a0d0e21e 1073
4633a7c4 1074 logmsg "server started on port $port";
a0d0e21e 1075
4633a7c4
LW
1076 my $waitedpid = 0;
1077 my $paddr;
a0d0e21e 1078
816229cf 1079 use POSIX ":sys_wait_h";
c5ae6365
AW
1080 use Errno;
1081
54310121 1082 sub REAPER {
c5ae6365 1083 local $!; # don't let waitpid() overwrite current error
cf21866a
TC
1084 while ((my $pid = waitpid(-1, WNOHANG)) > 0 && WIFEXITED($?)) {
1085 logmsg "reaped $waitedpid" . ($? ? " with exit $?" : "");
c5ae6365 1086 }
abf724c9 1087 $SIG{CHLD} = \&REAPER; # loathe SysV
4633a7c4
LW
1088 }
1089
1090 $SIG{CHLD} = \&REAPER;
1091
cf21866a 1092 while (1) {
c5ae6365 1093 $paddr = accept(Client, Server) || do {
cf21866a 1094 # try again if accept() returned because got a signal
c5ae6365
AW
1095 next if $!{EINTR};
1096 die "accept: $!";
1097 };
1098 my ($port, $iaddr) = sockaddr_in($paddr);
1099 my $name = gethostbyaddr($iaddr, AF_INET);
1100
1101 logmsg "connection from $name [",
1102 inet_ntoa($iaddr),
1103 "] at port $port";
1104
1105 spawn sub {
cf21866a
TC
1106 $| = 1;
1107 print "Hello there, $name, it's now ", scalar localtime(), $EOL;
1108 exec "/usr/games/fortune" # XXX: "wrong" line terminators
c5ae6365
AW
1109 or confess "can't exec fortune: $!";
1110 };
1111 close Client;
54310121 1112 }
a0d0e21e 1113
4633a7c4 1114 sub spawn {
c5ae6365
AW
1115 my $coderef = shift;
1116
cf21866a 1117 unless (@_ == 0 && $coderef && ref($coderef) eq "CODE") {
c5ae6365
AW
1118 confess "usage: spawn CODEREF";
1119 }
1120
1121 my $pid;
cf21866a 1122 unless (defined($pid = fork())) {
c5ae6365
AW
1123 logmsg "cannot fork: $!";
1124 return;
1125 }
1126 elsif ($pid) {
1127 logmsg "begat $pid";
1128 return; # I'm the parent
1129 }
1130 # else I'm the child -- go spawn
1131
cf21866a
TC
1132 open(STDIN, "<&Client") || die "can't dup client to stdin";
1133 open(STDOUT, ">&Client") || die "can't dup client to stdout";
c5ae6365 1134 ## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
cf21866a 1135 exit($coderef->());
54310121 1136 }
4633a7c4 1137
c5ae6365
AW
1138This server takes the trouble to clone off a child version via fork()
1139for each incoming request. That way it can handle many requests at
1140once, which you might not always want. Even if you don't fork(), the
1141listen() will allow that many pending connections. Forking servers
1142have to be particularly careful about cleaning up their dead children
1143(called "zombies" in Unix parlance), because otherwise you'll quickly
1144fill up your process table. The REAPER subroutine is used here to
1145call waitpid() for any child processes that have finished, thereby
1146ensuring that they terminate cleanly and don't join the ranks of the
1147living dead.
1148
1149Within the while loop we call accept() and check to see if it returns
cf21866a
TC
1150a false value. This would normally indicate a system error needs
1151to be reported. However, the introduction of safe signals (see
c5ae6365 1152L</Deferred Signals (Safe Signals)> above) in Perl 5.7.3 means that
cf21866a
TC
1153accept() might also be interrupted when the process receives a signal.
1154This typically happens when one of the forked subprocesses exits and
c5ae6365
AW
1155notifies the parent process with a CHLD signal.
1156
cf21866a
TC
1157If accept() is interrupted by a signal, $! will be set to EINTR.
1158If this happens, we can safely continue to the next iteration of
c5ae6365 1159the loop and another call to accept(). It is important that your
cf21866a
TC
1160signal handling code not modify the value of $!, or else this test
1161will likely fail. In the REAPER subroutine we create a local version
1162of $! before calling waitpid(). When waitpid() sets $! to ECHILD as
1163it inevitably does when it has no more children waiting, it
1164updates the local copy and leaves the original unchanged.
4633a7c4 1165
cf21866a 1166You should use the B<-T> flag to enable taint checking (see L<perlsec>)
4633a7c4 1167even if we aren't running setuid or setgid. This is always a good idea
cf21866a 1168for servers or any program run on behalf of someone else (like CGI
4633a7c4
LW
1169scripts), because it lessens the chances that people from the outside will
1170be able to compromise your system.
1171
1172Let's look at another TCP client. This one connects to the TCP "time"
1173service on a number of different machines and shows how far their clocks
1174differ from the system on which it's being run:
1175
1176 #!/usr/bin/perl -w
4633a7c4
LW
1177 use strict;
1178 use Socket;
1179
cf21866a
TC
1180 my $SECS_OF_70_YEARS = 2208988800;
1181 sub ctime { scalar localtime(shift() || time()) }
4633a7c4 1182
cf21866a
TC
1183 my $iaddr = gethostbyname("localhost");
1184 my $proto = getprotobyname("tcp");
1185 my $port = getservbyname("time", "tcp");
4633a7c4
LW
1186 my $paddr = sockaddr_in(0, $iaddr);
1187 my($host);
1188
1189 $| = 1;
cf21866a 1190 printf "%-24s %8s %s\n", "localhost", 0, ctime();
4633a7c4
LW
1191
1192 foreach $host (@ARGV) {
322c2516
SF
1193 printf "%-24s ", $host;
1194 my $hisiaddr = inet_aton($host) || die "unknown host";
1195 my $hispaddr = sockaddr_in($port, $hisiaddr);
cf21866a
TC
1196 socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
1197 || die "socket: $!";
322c2516 1198 connect(SOCKET, $hispaddr) || die "connect: $!";
cf21866a 1199 my $rtime = pack("C4", ());
322c2516
SF
1200 read(SOCKET, $rtime, 4);
1201 close(SOCKET);
cf21866a
TC
1202 my $histime = unpack("N", $rtime) - $SECS_OF_70_YEARS;
1203 printf "%8d %s\n", $histime - time(), ctime($histime);
a0d0e21e
LW
1204 }
1205
4633a7c4
LW
1206=head2 Unix-Domain TCP Clients and Servers
1207
a2eb9003 1208That's fine for Internet-domain clients and servers, but what about local
4633a7c4
LW
1209communications? While you can use the same setup, sometimes you don't
1210want to. Unix-domain sockets are local to the current host, and are often
54310121 1211used internally to implement pipes. Unlike Internet domain sockets, Unix
4633a7c4
LW
1212domain sockets can show up in the file system with an ls(1) listing.
1213
5a964f20 1214 % ls -l /dev/log
4633a7c4 1215 srw-rw-rw- 1 root 0 Oct 31 07:23 /dev/log
a0d0e21e 1216
4633a7c4
LW
1217You can test for these with Perl's B<-S> file test:
1218
cf21866a 1219 unless (-S "/dev/log") {
322c2516 1220 die "something's wicked with the log system";
54310121 1221 }
4633a7c4
LW
1222
1223Here's a sample Unix-domain client:
1224
1225 #!/usr/bin/perl -w
4633a7c4
LW
1226 use Socket;
1227 use strict;
1228 my ($rendezvous, $line);
1229
cf21866a 1230 $rendezvous = shift || "catsock";
322c2516
SF
1231 socket(SOCK, PF_UNIX, SOCK_STREAM, 0) || die "socket: $!";
1232 connect(SOCK, sockaddr_un($rendezvous)) || die "connect: $!";
54310121 1233 while (defined($line = <SOCK>)) {
322c2516 1234 print $line;
54310121 1235 }
cf21866a 1236 exit(0);
4633a7c4 1237
5a964f20
TC
1238And here's a corresponding server. You don't have to worry about silly
1239network terminators here because Unix domain sockets are guaranteed
1240to be on the localhost, and thus everything works right.
4633a7c4
LW
1241
1242 #!/usr/bin/perl -Tw
4633a7c4
LW
1243 use strict;
1244 use Socket;
1245 use Carp;
1246
cf21866a 1247 BEGIN { $ENV{PATH} = "/usr/bin:/bin" }
5865a7df 1248 sub spawn; # forward declaration
cf21866a 1249 sub logmsg { print "$0 $$: @_ at ", scalar localtime(), "\n" }
4633a7c4 1250
cf21866a 1251 my $NAME = "catsock";
4633a7c4 1252 my $uaddr = sockaddr_un($NAME);
cf21866a 1253 my $proto = getprotobyname("tcp");
4633a7c4 1254
cf21866a 1255 socket(Server, PF_UNIX, SOCK_STREAM, 0) || die "socket: $!";
4633a7c4 1256 unlink($NAME);
322c2516 1257 bind (Server, $uaddr) || die "bind: $!";
cf21866a 1258 listen(Server, SOMAXCONN) || die "listen: $!";
4633a7c4
LW
1259
1260 logmsg "server started on $NAME";
1261
5a964f20
TC
1262 my $waitedpid;
1263
816229cf 1264 use POSIX ":sys_wait_h";
5a964f20 1265 sub REAPER {
322c2516 1266 my $child;
cf21866a
TC
1267 while (($waitedpid = waitpid(-1, WNOHANG)) > 0) {
1268 logmsg "reaped $waitedpid" . ($? ? " with exit $?" : "");
322c2516
SF
1269 }
1270 $SIG{CHLD} = \&REAPER; # loathe SysV
5a964f20
TC
1271 }
1272
4633a7c4
LW
1273 $SIG{CHLD} = \&REAPER;
1274
5a964f20 1275
54310121 1276 for ( $waitedpid = 0;
cf21866a 1277 accept(Client, Server) || $waitedpid;
322c2516 1278 $waitedpid = 0, close Client)
4633a7c4 1279 {
322c2516
SF
1280 next if $waitedpid;
1281 logmsg "connection on $NAME";
1282 spawn sub {
cf21866a
TC
1283 print "Hello there, it's now ", scalar localtime(), "\n";
1284 exec("/usr/games/fortune") || die "can't exec fortune: $!";
322c2516 1285 };
54310121 1286 }
4633a7c4 1287
5865a7df 1288 sub spawn {
cf21866a 1289 my $coderef = shift();
322c2516 1290
cf21866a 1291 unless (@_ == 0 && $coderef && ref($coderef) eq "CODE") {
322c2516
SF
1292 confess "usage: spawn CODEREF";
1293 }
1294
1295 my $pid;
cf21866a 1296 unless (defined($pid = fork())) {
322c2516
SF
1297 logmsg "cannot fork: $!";
1298 return;
cf21866a
TC
1299 }
1300 elsif ($pid) {
322c2516
SF
1301 logmsg "begat $pid";
1302 return; # I'm the parent
cf21866a
TC
1303 }
1304 else {
1305 # I'm the child -- go spawn
322c2516 1306 }
322c2516 1307
cf21866a
TC
1308 open(STDIN, "<&Client") || die "can't dup client to stdin";
1309 open(STDOUT, ">&Client") || die "can't dup client to stdout";
322c2516 1310 ## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
cf21866a 1311 exit($coderef->());
5865a7df
NC
1312 }
1313
4633a7c4
LW
1314As you see, it's remarkably similar to the Internet domain TCP server, so
1315much so, in fact, that we've omitted several duplicate functions--spawn(),
cf21866a 1316logmsg(), ctime(), and REAPER()--which are the same as in the other server.
4633a7c4
LW
1317
1318So why would you ever want to use a Unix domain socket instead of a
1319simpler named pipe? Because a named pipe doesn't give you sessions. You
1320can't tell one process's data from another's. With socket programming,
cf21866a 1321you get a separate session for each client; that's why accept() takes two
4633a7c4
LW
1322arguments.
1323
cf21866a
TC
1324For example, let's say that you have a long-running database server daemon
1325that you want folks to be able to access from the Web, but only
4633a7c4
LW
1326if they go through a CGI interface. You'd have a small, simple CGI
1327program that does whatever checks and logging you feel like, and then acts
1328as a Unix-domain client and connects to your private server.
1329
7b05b7e3
TC
1330=head1 TCP Clients with IO::Socket
1331
1332For those preferring a higher-level interface to socket programming, the
cf21866a
TC
1333IO::Socket module provides an object-oriented approach. IO::Socket has
1334been included in the standard Perl distribution ever since Perl 5.004. If
1335you're running an earlier version of Perl (in which case, how are you
1336reading this manpage?), just fetch IO::Socket from CPAN, where you'll also
1337find modules providing easy interfaces to the following systems: DNS, FTP,
1338Ident (RFC 931), NIS and NISPlus, NNTP, Ping, POP3, SMTP, SNMP, SSLeay,
1339Telnet, and Time--to name just a few.
7b05b7e3
TC
1340
1341=head2 A Simple Client
1342
1343Here's a client that creates a TCP connection to the "daytime"
1344service at port 13 of the host name "localhost" and prints out everything
1345that the server there cares to provide.
1346
1347 #!/usr/bin/perl -w
1348 use IO::Socket;
1349 $remote = IO::Socket::INET->new(
322c2516
SF
1350 Proto => "tcp",
1351 PeerAddr => "localhost",
1352 PeerPort => "daytime(13)",
1353 )
cf21866a
TC
1354 || die "can't connect to daytime service on localhost";
1355 while (<$remote>) { print }
7b05b7e3
TC
1356
1357When you run this program, you should get something back that
1358looks like this:
1359
1360 Wed May 14 08:40:46 MDT 1997
1361
cf21866a 1362Here are what those parameters to the new() constructor mean:
7b05b7e3 1363
13a2d996 1364=over 4
7b05b7e3
TC
1365
1366=item C<Proto>
1367
1368This is which protocol to use. In this case, the socket handle returned
1369will be connected to a TCP socket, because we want a stream-oriented
1370connection, that is, one that acts pretty much like a plain old file.
1371Not all sockets are this of this type. For example, the UDP protocol
1372can be used to make a datagram socket, used for message-passing.
1373
1374=item C<PeerAddr>
1375
1376This is the name or Internet address of the remote host the server is
1377running on. We could have specified a longer name like C<"www.perl.com">,
cf21866a 1378or an address like C<"207.171.7.72">. For demonstration purposes, we've
7b05b7e3
TC
1379used the special hostname C<"localhost">, which should always mean the
1380current machine you're running on. The corresponding Internet address
cf21866a 1381for localhost is C<"127.0.0.1">, if you'd rather use that.
7b05b7e3
TC
1382
1383=item C<PeerPort>
1384
1385This is the service name or port number we'd like to connect to.
1386We could have gotten away with using just C<"daytime"> on systems with a
1387well-configured system services file,[FOOTNOTE: The system services file
cf21866a
TC
1388is found in I</etc/services> under Unixy systems.] but here we've specified the
1389port number (13) in parentheses. Using just the number would have also
1390worked, but numeric literals make careful programmers nervous.
7b05b7e3
TC
1391
1392=back
1393
1394Notice how the return value from the C<new> constructor is used as
cf21866a
TC
1395a filehandle in the C<while> loop? That's what's called an I<indirect
1396filehandle>, a scalar variable containing a filehandle. You can use
7b05b7e3
TC
1397it the same way you would a normal filehandle. For example, you
1398can read one line from it this way:
1399
1400 $line = <$handle>;
1401
1402all remaining lines from is this way:
1403
1404 @lines = <$handle>;
1405
1406and send a line of data to it this way:
1407
1408 print $handle "some data\n";
1409
1410=head2 A Webget Client
1411
1412Here's a simple client that takes a remote host to fetch a document
cf21866a 1413from, and then a list of files to get from that host. This is a
7b05b7e3
TC
1414more interesting client than the previous one because it first sends
1415something to the server before fetching the server's response.
1416
1417 #!/usr/bin/perl -w
1418 use IO::Socket;
cf21866a 1419 unless (@ARGV > 1) { die "usage: $0 host url ..." }
7b05b7e3 1420 $host = shift(@ARGV);
5a964f20
TC
1421 $EOL = "\015\012";
1422 $BLANK = $EOL x 2;
cf21866a 1423 for my $document (@ARGV) {
322c2516
SF
1424 $remote = IO::Socket::INET->new( Proto => "tcp",
1425 PeerAddr => $host,
1426 PeerPort => "http(80)",
cf21866a 1427 ) || die "cannot connect to httpd on $host";
322c2516
SF
1428 $remote->autoflush(1);
1429 print $remote "GET $document HTTP/1.0" . $BLANK;
1430 while ( <$remote> ) { print }
1431 close $remote;
7b05b7e3
TC
1432 }
1433
cf21866a
TC
1434The web server handling the HTTP service is assumed to be at
1435its standard port, number 80. If the server you're trying to
1436connect to is at a different port, like 1080 or 8080, you should specify it
c47ff5f1 1437as the named-parameter pair, C<< PeerPort => 8080 >>. The C<autoflush>
7b05b7e3 1438method is used on the socket because otherwise the system would buffer
cf21866a
TC
1439up the output we sent it. (If you're on a prehistoric Mac, you'll also
1440need to change every C<"\n"> in your code that sends data over the network
1441to be a C<"\015\012"> instead.)
7b05b7e3
TC
1442
1443Connecting to the server is only the first part of the process: once you
1444have the connection, you have to use the server's language. Each server
1445on the network has its own little command language that it expects as
1446input. The string that we send to the server starting with "GET" is in
1447HTTP syntax. In this case, we simply request each specified document.
1448Yes, we really are making a new connection for each document, even though
1449it's the same host. That's the way you always used to have to speak HTTP.
1450Recent versions of web browsers may request that the remote server leave
1451the connection open a little while, but the server doesn't have to honor
1452such a request.
1453
1454Here's an example of running that program, which we'll call I<webget>:
1455
5a964f20 1456 % webget www.perl.com /guanaco.html
7b05b7e3
TC
1457 HTTP/1.1 404 File Not Found
1458 Date: Thu, 08 May 1997 18:02:32 GMT
1459 Server: Apache/1.2b6
1460 Connection: close
1461 Content-type: text/html
1462
1463 <HEAD><TITLE>404 File Not Found</TITLE></HEAD>
1464 <BODY><H1>File Not Found</H1>
1465 The requested URL /guanaco.html was not found on this server.<P>
1466 </BODY>
1467
1468Ok, so that's not very interesting, because it didn't find that
1469particular document. But a long response wouldn't have fit on this page.
1470
cf21866a 1471For a more featureful version of this program, you should look to
7b05b7e3
TC
1472the I<lwp-request> program included with the LWP modules from CPAN.
1473
1474=head2 Interactive Client with IO::Socket
1475
1476Well, that's all fine if you want to send one command and get one answer,
1477but what about setting up something fully interactive, somewhat like
1478the way I<telnet> works? That way you can type a line, get the answer,
1479type a line, get the answer, etc.
1480
1481This client is more complicated than the two we've done so far, but if
1482you're on a system that supports the powerful C<fork> call, the solution
1483isn't that rough. Once you've made the connection to whatever service
1484you'd like to chat with, call C<fork> to clone your process. Each of
1485these two identical process has a very simple job to do: the parent
1486copies everything from the socket to standard output, while the child
1487simultaneously copies everything from standard input to the socket.
1488To accomplish the same thing using just one process would be I<much>
1489harder, because it's easier to code two processes to do one thing than it
1490is to code one process to do two things. (This keep-it-simple principle
5a964f20
TC
1491a cornerstones of the Unix philosophy, and good software engineering as
1492well, which is probably why it's spread to other systems.)
7b05b7e3
TC
1493
1494Here's the code:
1495
1496 #!/usr/bin/perl -w
1497 use strict;
1498 use IO::Socket;
1499 my ($host, $port, $kidpid, $handle, $line);
1500
1501 unless (@ARGV == 2) { die "usage: $0 host port" }
1502 ($host, $port) = @ARGV;
1503
1504 # create a tcp connection to the specified host and port
1505 $handle = IO::Socket::INET->new(Proto => "tcp",
322c2516
SF
1506 PeerAddr => $host,
1507 PeerPort => $port)
cf21866a 1508 || die "can't connect to port $port on $host: $!";
7b05b7e3 1509
cf21866a 1510 $handle->autoflush(1); # so output gets there right away
7b05b7e3
TC
1511 print STDERR "[Connected to $host:$port]\n";
1512
1513 # split the program into two processes, identical twins
1514 die "can't fork: $!" unless defined($kidpid = fork());
1515
1516 # the if{} block runs only in the parent process
1517 if ($kidpid) {
322c2516
SF
1518 # copy the socket to standard output
1519 while (defined ($line = <$handle>)) {
1520 print STDOUT $line;
1521 }
cf21866a 1522 kill("TERM", $kidpid); # send SIGTERM to child
7b05b7e3
TC
1523 }
1524 # the else{} block runs only in the child process
1525 else {
322c2516
SF
1526 # copy standard input to the socket
1527 while (defined ($line = <STDIN>)) {
1528 print $handle $line;
1529 }
cf21866a 1530 exit(0); # just in case
7b05b7e3
TC
1531 }
1532
1533The C<kill> function in the parent's C<if> block is there to send a
cf21866a 1534signal to our child process, currently running in the C<else> block,
7b05b7e3
TC
1535as soon as the remote server has closed its end of the connection.
1536
7b05b7e3
TC
1537If the remote server sends data a byte at time, and you need that
1538data immediately without waiting for a newline (which might not happen),
1539you may wish to replace the C<while> loop in the parent with the
1540following:
1541
1542 my $byte;
1543 while (sysread($handle, $byte, 1) == 1) {
322c2516 1544 print STDOUT $byte;
7b05b7e3
TC
1545 }
1546
1547Making a system call for each byte you want to read is not very efficient
1548(to put it mildly) but is the simplest to explain and works reasonably
1549well.
1550
1551=head1 TCP Servers with IO::Socket
1552
5a964f20 1553As always, setting up a server is little bit more involved than running a client.
7b05b7e3
TC
1554The model is that the server creates a special kind of socket that
1555does nothing but listen on a particular port for incoming connections.
c47ff5f1 1556It does this by calling the C<< IO::Socket::INET->new() >> method with
7b05b7e3
TC
1557slightly different arguments than the client did.
1558
13a2d996 1559=over 4
7b05b7e3
TC
1560
1561=item Proto
1562
1563This is which protocol to use. Like our clients, we'll
1564still specify C<"tcp"> here.
1565
1566=item LocalPort
1567
1568We specify a local
1569port in the C<LocalPort> argument, which we didn't do for the client.
1570This is service name or port number for which you want to be the
1571server. (Under Unix, ports under 1024 are restricted to the
1572superuser.) In our sample, we'll use port 9000, but you can use
1573any port that's not currently in use on your system. If you try
1574to use one already in used, you'll get an "Address already in use"
19799a22 1575message. Under Unix, the C<netstat -a> command will show
7b05b7e3
TC
1576which services current have servers.
1577
1578=item Listen
1579
1580The C<Listen> parameter is set to the maximum number of
1581pending connections we can accept until we turn away incoming clients.
1582Think of it as a call-waiting queue for your telephone.
1583The low-level Socket module has a special symbol for the system maximum, which
1584is SOMAXCONN.
1585
1586=item Reuse
1587
1588The C<Reuse> parameter is needed so that we restart our server
1589manually without waiting a few minutes to allow system buffers to
1590clear out.
1591
1592=back
1593
1594Once the generic server socket has been created using the parameters
1595listed above, the server then waits for a new client to connect
d1be9408
JF
1596to it. The server blocks in the C<accept> method, which eventually accepts a
1597bidirectional connection from the remote client. (Make sure to autoflush
7b05b7e3
TC
1598this handle to circumvent buffering.)
1599
1600To add to user-friendliness, our server prompts the user for commands.
1601Most servers don't do this. Because of the prompt without a newline,
1602you'll have to use the C<sysread> variant of the interactive client above.
1603
cf21866a
TC
1604This server accepts one of five different commands, sending output back to
1605the client. Unlike most network servers, this one handles only one
1606incoming client at a time. Multithreaded servers are covered in
faa783ac 1607Chapter 16 of the Camel.
7b05b7e3
TC
1608
1609Here's the code. We'll
1610
1611 #!/usr/bin/perl -w
1612 use IO::Socket;
cf21866a 1613 use Net::hostent; # for OOish version of gethostbyaddr
7b05b7e3 1614
322c2516 1615 $PORT = 9000; # pick something not in use
7b05b7e3 1616
cf21866a 1617 $server = IO::Socket::INET->new( Proto => "tcp",
7b05b7e3
TC
1618 LocalPort => $PORT,
1619 Listen => SOMAXCONN,
1620 Reuse => 1);
1621
1622 die "can't setup server" unless $server;
1623 print "[Server $0 accepting clients]\n";
1624
1625 while ($client = $server->accept()) {
1626 $client->autoflush(1);
1627 print $client "Welcome to $0; type help for command list.\n";
1628 $hostinfo = gethostbyaddr($client->peeraddr);
78fc38e1 1629 printf "[Connect from %s]\n", $hostinfo ? $hostinfo->name : $client->peerhost;
7b05b7e3
TC
1630 print $client "Command? ";
1631 while ( <$client>) {
322c2516 1632 next unless /\S/; # blank line
cf21866a
TC
1633 if (/quit|exit/i) { last }
1634 elsif (/date|time/i) { printf $client "%s\n", scalar localtime() }
1635 elsif (/who/i ) { print $client `who 2>&1` }
1636 elsif (/cookie/i ) { print $client `/usr/games/fortune 2>&1` }
1637 elsif (/motd/i ) { print $client `cat /etc/motd 2>&1` }
7b05b7e3
TC
1638 else {
1639 print $client "Commands: quit date who cookie motd\n";
1640 }
1641 } continue {
1642 print $client "Command? ";
1643 }
1644 close $client;
1645 }
1646
1647=head1 UDP: Message Passing
4633a7c4
LW
1648
1649Another kind of client-server setup is one that uses not connections, but
1650messages. UDP communications involve much lower overhead but also provide
1651less reliability, as there are no promises that messages will arrive at
1652all, let alone in order and unmangled. Still, UDP offers some advantages
1653over TCP, including being able to "broadcast" or "multicast" to a whole
1654bunch of destination hosts at once (usually on your local subnet). If you
1655find yourself overly concerned about reliability and start building checks
6a3992aa 1656into your message system, then you probably should use just TCP to start
4633a7c4
LW
1657with.
1658
cf21866a
TC
1659UDP datagrams are I<not> a bytestream and should not be treated as such.
1660This makes using I/O mechanisms with internal buffering like stdio (i.e.
1661print() and friends) especially cumbersome. Use syswrite(), or better
1662send(), like in the example below.
90034919 1663
4633a7c4 1664Here's a UDP program similar to the sample Internet TCP client given
7b05b7e3 1665earlier. However, instead of checking one host at a time, the UDP version
4633a7c4
LW
1666will check many of them asynchronously by simulating a multicast and then
1667using select() to do a timed-out wait for I/O. To do something similar
1668with TCP, you'd have to use a different socket handle for each host.
1669
1670 #!/usr/bin/perl -w
1671 use strict;
4633a7c4
LW
1672 use Socket;
1673 use Sys::Hostname;
1674
54310121 1675 my ( $count, $hisiaddr, $hispaddr, $histime,
322c2516 1676 $host, $iaddr, $paddr, $port, $proto,
cf21866a 1677 $rin, $rout, $rtime, $SECS_OF_70_YEARS);
4633a7c4 1678
cf21866a 1679 $SECS_OF_70_YEARS = 2_208_988_800;
4633a7c4
LW
1680
1681 $iaddr = gethostbyname(hostname());
cf21866a
TC
1682 $proto = getprotobyname("udp");
1683 $port = getservbyname("time", "udp");
4633a7c4
LW
1684 $paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
1685
1686 socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!";
1687 bind(SOCKET, $paddr) || die "bind: $!";
1688
1689 $| = 1;
cf21866a 1690 printf "%-12s %8s %s\n", "localhost", 0, scalar localtime();
4633a7c4
LW
1691 $count = 0;
1692 for $host (@ARGV) {
322c2516 1693 $count++;
cf21866a 1694 $hisiaddr = inet_aton($host) || die "unknown host";
322c2516
SF
1695 $hispaddr = sockaddr_in($port, $hisiaddr);
1696 defined(send(SOCKET, 0, 0, $hispaddr)) || die "send $host: $!";
4633a7c4
LW
1697 }
1698
cf21866a 1699 $rin = "";
4633a7c4
LW
1700 vec($rin, fileno(SOCKET), 1) = 1;
1701
1702 # timeout after 10.0 seconds
1703 while ($count && select($rout = $rin, undef, undef, 10.0)) {
cf21866a
TC
1704 $rtime = "";
1705 $hispaddr = recv(SOCKET, $rtime, 4, 0) || die "recv: $!";
322c2516
SF
1706 ($port, $hisiaddr) = sockaddr_in($hispaddr);
1707 $host = gethostbyaddr($hisiaddr, AF_INET);
cf21866a 1708 $histime = unpack("N", $rtime) - $SECS_OF_70_YEARS;
322c2516 1709 printf "%-12s ", $host;
cf21866a 1710 printf "%8d %s\n", $histime - time(), scalar localtime($histime);
322c2516 1711 $count--;
4633a7c4
LW
1712 }
1713
cf21866a
TC
1714This example does not include any retries and may consequently fail to
1715contact a reachable host. The most prominent reason for this is congestion
1716of the queues on the sending host if the number of hosts to contact is
1717sufficiently large.
90034919 1718
4633a7c4
LW
1719=head1 SysV IPC
1720
1721While System V IPC isn't so widely used as sockets, it still has some
cf21866a
TC
1722interesting uses. However, you cannot use SysV IPC or Berkeley mmap() to
1723have a variable shared amongst several processes. That's because Perl
1724would reallocate your string when you weren't wanting it to. You might
1725look into the C<IPC::Shareable> or C<threads::shared> modules for that.
4633a7c4 1726
54310121 1727Here's a small example showing shared memory usage.
a0d0e21e 1728
7b34eba2 1729 use IPC::SysV qw(IPC_PRIVATE IPC_RMID S_IRUSR S_IWUSR);
0ade1984 1730
a0d0e21e 1731 $size = 2000;
cf21866a
TC
1732 $id = shmget(IPC_PRIVATE, $size, S_IRUSR | S_IWUSR);
1733 defined($id) || die "shmget: $!";
41d6edb2 1734 print "shm key $id\n";
a0d0e21e
LW
1735
1736 $message = "Message #1";
cf21866a 1737 shmwrite($id, $message, 0, 60) || die "shmwrite: $!";
0ade1984 1738 print "wrote: '$message'\n";
cf21866a 1739 shmread($id, $buff, 0, 60) || die "shmread: $!";
0ade1984 1740 print "read : '$buff'\n";
a0d0e21e 1741
0ade1984 1742 # the buffer of shmread is zero-character end-padded.
cf21866a 1743 substr($buff, index($buff, "\0")) = "":
0ade1984
JH
1744 print "un" unless $buff eq $message;
1745 print "swell\n";
a0d0e21e 1746
41d6edb2 1747 print "deleting shm $id\n";
cf21866a 1748 shmctl($id, IPC_RMID, 0) || die "shmctl: $!";
a0d0e21e
LW
1749
1750Here's an example of a semaphore:
1751
0ade1984
JH
1752 use IPC::SysV qw(IPC_CREAT);
1753
a0d0e21e 1754 $IPC_KEY = 1234;
cf21866a
TC
1755 $id = semget($IPC_KEY, 10, 0666 | IPC_CREAT);
1756 defined($id) || die "shmget: $!";
41d6edb2 1757 print "shm key $id\n";
a0d0e21e 1758
a2eb9003 1759Put this code in a separate file to be run in more than one process.
a0d0e21e
LW
1760Call the file F<take>:
1761
1762 # create a semaphore
1763
1764 $IPC_KEY = 1234;
cf21866a
TC
1765 $id = semget($IPC_KEY, 0, 0);
1766 defined($id) || die "shmget: $!";
a0d0e21e 1767
cf21866a 1768 $semnum = 0;
a0d0e21e
LW
1769 $semflag = 0;
1770
cf21866a 1771 # "take" semaphore
a0d0e21e
LW
1772 # wait for semaphore to be zero
1773 $semop = 0;
41d6edb2 1774 $opstring1 = pack("s!s!s!", $semnum, $semop, $semflag);
a0d0e21e
LW
1775
1776 # Increment the semaphore count
1777 $semop = 1;
41d6edb2 1778 $opstring2 = pack("s!s!s!", $semnum, $semop, $semflag);
cf21866a 1779 $opstring = $opstring1 . $opstring2;
a0d0e21e 1780
cf21866a 1781 semop($id, $opstring) || die "semop: $!";
a0d0e21e 1782
a2eb9003 1783Put this code in a separate file to be run in more than one process.
a0d0e21e
LW
1784Call this file F<give>:
1785
cf21866a 1786 # "give" the semaphore
a0d0e21e
LW
1787 # run this in the original process and you will see
1788 # that the second process continues
1789
1790 $IPC_KEY = 1234;
41d6edb2 1791 $id = semget($IPC_KEY, 0, 0);
cf21866a 1792 die unless defined($id);
a0d0e21e 1793
cf21866a 1794 $semnum = 0;
a0d0e21e
LW
1795 $semflag = 0;
1796
1797 # Decrement the semaphore count
1798 $semop = -1;
41d6edb2 1799 $opstring = pack("s!s!s!", $semnum, $semop, $semflag);
a0d0e21e 1800
cf21866a 1801 semop($id, $opstring) || die "semop: $!";
a0d0e21e 1802
7b05b7e3 1803The SysV IPC code above was written long ago, and it's definitely
0ade1984
JH
1804clunky looking. For a more modern look, see the IPC::SysV module
1805which is included with Perl starting from Perl 5.005.
4633a7c4 1806
41d6edb2
JH
1807A small example demonstrating SysV message queues:
1808
7b34eba2 1809 use IPC::SysV qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRUSR S_IWUSR);
41d6edb2 1810
7b34eba2 1811 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR | S_IWUSR);
cf21866a 1812 defined($id) || die "msgget failed: $!";
41d6edb2 1813
cf21866a 1814 my $sent = "message";
e343e2e2 1815 my $type_sent = 1234;
cf21866a
TC
1816
1817 msgsnd($id, pack("l! a*", $type_sent, $sent), 0)
1818 || die "msgsnd failed: $!";
1819
1820 msgrcv($id, my $rcvd_buf, 60, 0, 0)
1821 || die "msgrcv failed: $!";
1822
1823 my($type_rcvd, $rcvd) = unpack("l! a*", $rcvd_buf);
1824
1825 if ($rcvd eq $sent) {
1826 print "okay\n";
41d6edb2 1827 } else {
cf21866a 1828 print "not okay\n";
41d6edb2
JH
1829 }
1830
cf21866a
TC
1831 msgctl($id, IPC_RMID, 0) || die "msgctl failed: $!\n";
1832
4633a7c4
LW
1833=head1 NOTES
1834
5a964f20
TC
1835Most of these routines quietly but politely return C<undef> when they
1836fail instead of causing your program to die right then and there due to
1837an uncaught exception. (Actually, some of the new I<Socket> conversion
cf21866a 1838functions do croak() on bad arguments.) It is therefore essential to
5a964f20 1839check return values from these functions. Always begin your socket
cf21866a
TC
1840programs this way for optimal success, and don't forget to add the B<-T>
1841taint-checking flag to the C<#!> line for servers:
4633a7c4 1842
5a964f20 1843 #!/usr/bin/perl -Tw
4633a7c4
LW
1844 use strict;
1845 use sigtrap;
1846 use Socket;
1847
1848=head1 BUGS
1849
cf21866a 1850These routines all create system-specific portability problems. As noted
4633a7c4 1851elsewhere, Perl is at the mercy of your C libraries for much of its system
cf21866a 1852behavior. It's probably safest to assume broken SysV semantics for
6a3992aa 1853signals and to stick with simple TCP and UDP socket operations; e.g., don't
a2eb9003 1854try to pass open file descriptors over a local UDP datagram socket if you
4633a7c4
LW
1855want your code to stand a chance of being portable.
1856
4633a7c4
LW
1857=head1 AUTHOR
1858
1859Tom Christiansen, with occasional vestiges of Larry Wall's original
7b05b7e3 1860version and suggestions from the Perl Porters.
4633a7c4
LW
1861
1862=head1 SEE ALSO
1863
7b05b7e3
TC
1864There's a lot more to networking than this, but this should get you
1865started.
1866
cf21866a
TC
1867For intrepid programmers, the indispensable textbook is I<Unix Network
1868Programming, 2nd Edition, Volume 1> by W. Richard Stevens (published by
1869Prentice-Hall). Most books on networking address the subject from the
1870perspective of a C programmer; translation to Perl is left as an exercise
1871for the reader.
7b05b7e3
TC
1872
1873The IO::Socket(3) manpage describes the object library, and the Socket(3)
1874manpage describes the low-level interface to sockets. Besides the obvious
cf21866a
TC
1875functions in L<perlfunc>, you should also check out the F<modules> file at
1876your nearest CPAN site, especially
1877L<http://www.cpan.org/modules/00modlist.long.html#ID5_Networking_>.
1878See L<perlmodlib> or best yet, the F<Perl FAQ> for a description
1879of what CPAN is and where to get it if the previous link doesn't work
1880for you.
1881
1882Section 5 of CPAN's F<modules> file is devoted to "Networking, Device
1883Control (modems), and Interprocess Communication", and contains numerous
1884unbundled modules numerous networking modules, Chat and Expect operations,
1885CGI programming, DCE, FTP, IPC, NNTP, Proxy, Ptty, RPC, SNMP, SMTP, Telnet,
1886Threads, and ToolTalk--to name just a few.