| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Socket::UNIX.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: UNIX-socket based queue: common utilities | ||
| 6 | |||
| 7 | package DTA::CAB::Socket::UNIX; | ||
| 8 | use DTA::CAB::Socket ':flags'; | ||
| 9 | use DTA::CAB::Utils ':files'; | ||
| 10 | use IO::Handle; | ||
| 11 | use IO::File; | ||
| 12 | use IO::Socket; | ||
| 13 | use IO::Socket::UNIX; | ||
| 14 | use Carp; | ||
| 15 | use Exporter; | ||
| 16 | use strict; | ||
| 17 | |||
| 18 | ##============================================================================== | ||
| 19 | ## Globals | ||
| 20 | ##============================================================================== | ||
| 21 | our @ISA = qw(DTA::CAB::Socket); | ||
| 22 | |||
| 23 | ##============================================================================== | ||
| 24 | ## Constructors etc. | ||
| 25 | ##============================================================================== | ||
| 26 | |||
| 27 | ## $s = DTA::CAB::Socket::UNIX->new(%args) | ||
| 28 | ## + %$s, %args: | ||
| 29 | ## ( | ||
| 30 | ## ##-- NEW in DTA::CAB::Socket::UNIX | ||
| 31 | ## local => $path, ##-- path to local UNIX socket (for server; set to empty string to use a tempfile) | ||
| 32 | ## peer => $path, ##-- path to peer socket (for client) | ||
| 33 | ## listen => $n, ##-- queue size for listen (default=SOMAXCONN) | ||
| 34 | ## perms => $perms, ##-- file create permissions for server socket (default=0600) | ||
| 35 | ## unlink => $bool, ##-- if true, server socket will be unlink()ed on DESTROY() (default=true) | ||
| 36 | ## pid => $pid, ##-- pid of creating process (unlink() is only called if !defined($pid) || $$==$pid); (re-)set by open() | ||
| 37 | ## ## | ||
| 38 | ## ##-- INHERITED from DTA::CAB::Socket | ||
| 39 | ## fh => $sockfh, ##-- an IO::Socket::UNIX object for the socket | ||
| 40 | ## timeout => $secs, ##-- default timeout for select() (default=undef: none) | ||
| 41 | ## nonblocking => $bool, ##-- if true, set O_NONBLOCK on open() | ||
| 42 | ## logSocket => $level, ##-- log level for full trace (default=undef (none)) | ||
| 43 | ## logRequest => $level, ##-- log level for client requests (server only; default=undef (none)) | ||
| 44 | ## ) | ||
| 45 | sub new { | ||
| 46 | my ($that,%args) = @_; | ||
| 47 | my $s = $that->SUPER::new( | ||
| 48 | #local =>undef, | ||
| 49 | #peer =>undef, | ||
| 50 | ## | ||
| 51 | ##-- server-only options | ||
| 52 | listen =>SOMAXCONN, | ||
| 53 | unlink =>1, | ||
| 54 | pid =>$$, | ||
| 55 | perms =>0600, | ||
| 56 | %args, | ||
| 57 | ); | ||
| 58 | return $s->open() if (!defined($s->{fh}) && (defined($s->{local}) || defined($s->{peer}))); | ||
| 59 | return $s; | ||
| 60 | } | ||
| 61 | |||
| 62 | ## undef = $qs->DESTROY | ||
| 63 | ## + destructor calls close() | ||
| 64 | sub DESTROY { | ||
| 65 | $_[0]->unlink() if ($_[0]{local} && $_[0]{unlink} && (!defined($_[0]{pid}) || $_[0]{pid}==$$)); | ||
| 66 | } | ||
| 67 | |||
| 68 | ## $path = $s->path() | ||
| 69 | ## + returns path to unix socket | ||
| 70 | sub path { | ||
| 71 | return $_[0]{local} || $_[0]{peer} || undef; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | ##============================================================================== | ||
| 76 | ## Open/Close | ||
| 77 | |||
| 78 | ## $s = $s->unlink() | ||
| 79 | ## + unlinks $s->{local} if possible | ||
| 80 | ## + implicitly calls close() | ||
| 81 | sub unlink { | ||
| 82 | $_[0]->close(); | ||
| 83 | CORE::unlink($_[0]{local}) if ($_[0]{local} && -w $_[0]{local}); | ||
| 84 | } | ||
| 85 | |||
| 86 | ## $s_or_undef = $s->open(%args) | ||
| 87 | ## + wrapper for $s->{fh} = IO::Socket::UNIX->new(Type=>SOCK_STREAM, %args) | ||
| 88 | ## + no sanity checks are performed | ||
| 89 | sub open { | ||
| 90 | $_[0]->vtrace("open ", @_[1..$#_]); | ||
| 91 | my ($s,%args) = @_; | ||
| 92 | |||
| 93 | ##-- close and unlink if we can | ||
| 94 | $s->close() if ($s->opened); | ||
| 95 | $s->unlink() if ($s->{local} && $s->{unlink}); | ||
| 96 | |||
| 97 | ##-- clobber %$s with %$args | ||
| 98 | @$s{keys %args} = values %args; | ||
| 99 | $s->{pid} = $$; | ||
| 100 | |||
| 101 | if (defined($s->{local})) { | ||
| 102 | ##-------- server socket | ||
| 103 | $s->{local} = tmpfsfile('cabXXXXX') if (!$s->{local}); ##-- local=>'' : use tempfile | ||
| 104 | my $path = $s->{local}; | ||
| 105 | |||
| 106 | ##-- unlink any stale files of new pathname | ||
| 107 | if (-e $path) { | ||
| 108 | CORE::unlink($path) | ||
| 109 | or $s->logconfess("cannot unlink existing file at UNIX socket path '$path': $!"); | ||
| 110 | } | ||
| 111 | |||
| 112 | ##-- bind the socket | ||
| 113 | $s->{fh} = IO::Socket::UNIX->new(Type=>SOCK_STREAM, Local=>$s->{local}, Listen=>($s->{listen}||SOMAXCONN)) | ||
| 114 | or $s->logconfess("cannot bind local UNIX socket '$path': $!"); | ||
| 115 | |||
| 116 | ##-- set permissions | ||
| 117 | if (defined($s->{perms})) { | ||
| 118 | chmod($s->{perms}, $path) | ||
| 119 | or $s->logcluck(sprintf("cannot set perms=%0.4o for local UNIX socket '%s': $!", $s->{perms}, $path)); | ||
| 120 | } | ||
| 121 | |||
| 122 | ##-- report | ||
| 123 | $s->vlog('info', sprintf("created UNIX socket '%s' with permissions %0.4o", $path, ((stat($path))[2] & 0777))); | ||
| 124 | } | ||
| 125 | elsif (defined($s->{peer})) { | ||
| 126 | ##-------- client socket | ||
| 127 | $s->{fh} = IO::Socket::UNIX->new(Type=>SOCK_STREAM, Peer=>$s->{peer}) | ||
| 128 | or $s->logconfess("cannot connect to UNIX socket $s->{peer} as client: $!"); | ||
| 129 | |||
| 130 | ##-- report | ||
| 131 | $s->vtrace("connected to UNIX socket '$s->{peer}'"); | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | ##-- unknown | ||
| 135 | $s->logconfess("open(): no 'local' or 'peer' argument defined"); | ||
| 136 | } | ||
| 137 | |||
| 138 | ##-- set non-blocking mode if requested | ||
| 139 | $s->nonblocking(1) if ($s->{nonblocking}); | ||
| 140 | |||
| 141 | ##-- return | ||
| 142 | return $s; | ||
| 143 | } | ||
| 144 | |||
| 145 | ##============================================================================== | ||
| 146 | ## Server Methods | ||
| 147 | |||
| 148 | ## $client = $CLASS_OR_OBJECT->newClient(%args) | ||
| 149 | ## + wrapper for clients, called by $s->accept() | ||
| 150 | ## + default just calls $CLASS_OR_OBJECT->new(%args) | ||
| 151 | sub newClient { | ||
| 152 | my $that = shift; | ||
| 153 | my %args = (%$that,@_); | ||
| 154 | delete($args{local}); | ||
| 155 | $args{peer} = $that->{local} if (ref($that) && defined($that->{local})); | ||
| 156 | return $that->clientClass->new(%args); | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | 1; ##-- be happy | ||
| 161 | |||
| 162 | __END__ |