3 # freeside-selfservice-clientd
5 # This is run REMOTELY over ssh by freeside-selfservice-server
8 use subs qw(spawn logmsg lock_write unlock_write);
10 use POSIX qw(:sys_wait_h);
12 use Storable 2.09 qw(nstore_fd fd_retrieve);
13 use IO::Handle qw(_IONBF);
19 my $tag = scalar(@ARGV) ? '.'.shift : '';
21 use vars qw( $Debug );
22 $Debug = 2; #2 will turn on child logging
23 #3 will log packet contents,#including passwords
24 #4 will log receipts of all packets from server including
27 my $socket = "/usr/local/freeside/selfservice_socket$tag";
28 my $pid_file = "$socket.pid";
30 my $log_file = "/usr/local/freeside/selfservice$tag.log";
32 my $lock_file = "/usr/local/freeside/selfservice$tag.writelock";
38 $SIG{__WARN__} = \&_logmsg;
40 #read data to be cached or something
41 #warn "$me Reading init data\n" if $Debug;
44 warn "Creating $lock_file\n" if $Debug;
45 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
48 warn "Creating $socket\n" if $Debug;
49 my $uaddr = sockaddr_un($socket);
50 my $proto = getprotobyname('tcp');
51 socket(Server,PF_UNIX,SOCK_STREAM,0) or die "socket: $!";
53 bind(Server, $uaddr) or die "bind: $!";
54 listen(Server,SOMAXCONN) or die "listen: $!";
57 open(PIDFILE,"<$pid_file");
58 my $old_pid = <PIDFILE>;
60 if ( $old_pid =~ /^(\d+)$/ ) {
64 open(PIDFILE,">$pid_file");
69 #sub REAPER { $waitedpid = wait; $SIG{CHLD} = \&REAPER; }
70 #$SIG{CHLD} = \&REAPER;
72 warn "enabling keep alives\n" if $Debug;
73 nstore_fd( { _packet => '_enable_keepalive' } , \*STDOUT );
75 warn "entering main loop\n" if $Debug;
79 my $s = new IO::Select;
83 #for ( $waitedpid = 0;
84 # accept(Client,Server) || $waitedpid;
85 # $waitedpid = 0, close Client)
89 #$SIG{PIPE} = sub { warn "SIGPIPE received" };
90 #$SIG{CHLD} = sub { warn "SIGCHLD received" };
92 #sub REAPER { warn "SIGCHLD received"; my $pid = wait; $SIG{CHLD} = \&REAPER; }
93 #sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; }
94 #sub REAPER { my $pid = wait; delete $kids{$pid}; $SIG{CHLD} = \&REAPER; }
95 #$SIG{CHLD} = \&REAPER;
102 warn "waiting for connection\n" if $Debug && !$undisp;
104 #my @handles = $s->can_read();
105 my @handles = $s->can_read(5);
106 $undisp = !scalar(@handles);
107 foreach my $handle ( @handles ) {
109 if ( $handle == \*STDIN ) {
111 warn "receiving packet from server\n" if $Debug > 3;
113 my $packet = fd_retrieve(\*STDIN);
114 my $token = $packet->{'_token'};
116 if ( $token eq '_keepalive' ) {
121 warn "received packet from server with token $token\n".
123 ? join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
127 if ( exists($kids{$token}) ) {
128 warn "sending return packet to $token via $kids{$token}\n"
130 nstore_fd($packet, $kids{$token});
131 warn "flushing to $token\n" if $Debug;
132 until ( $kids{$token}->flush ) {
133 warn "WARNING: error flushing: $!";
136 #no close or delete here - will block waiting for child
137 warn "done with $token\n" if $Debug;
139 warn "WARNING: unknown token $token, discarding message";
142 } elsif ( $handle == \*Server ) {
144 until ( accept(Client, Server) ) {
145 warn "WARNING: accept failed: $!";
149 warn "received local connection; forking\n" if $Debug;
152 warn "[child-$$] reading packet from local client" if $Debug > 1;
153 my $packet = fd_retrieve(\*Client);
154 warn "[child-$$] packet received:\n".
155 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
157 my $command = $packet->{'command'};
158 #handle some commands weirdly?
159 $packet->{_token}=$$;
161 warn "[child-$$] locking write stream\n" if $Debug > 1;
164 warn "[child-$$] sending packet to remote server\n" if $Debug > 1;
165 nstore_fd($packet, \*STDOUT) or die "FATAL: can't send response: $!";
167 warn "[child-$$] flushing write stream\n" if $Debug > 1;
168 STDOUT->flush or die "FATAL: can't flush: $!";
170 warn "[child-$$] releasing write lock\n" if $Debug > 1;
173 warn "[child-$$] closing write stream\n" if $Debug > 1;
174 close STDOUT or die "FATAL: can't close write stream: $!"; #??!
176 warn "[child-$$] waiting for response from parent\n" if $Debug > 1;
177 my $w = new IO::Select;
179 until ( $w->can_read ) {
180 warn "[child-$$] WARNING: interrupted select: $!\n";
182 my $rv = fd_retrieve(\*STDIN);
186 warn "[child-$$] sending response to local client" if $Debug > 1;
187 nstore_fd($rv, \*Client);
188 Client->flush or die "FATAL: can't flush to local client: $!";
189 close Client or die "FATAL: can't close connection to local client: $!";
191 warn "[child-$$] child exiting" if $Debug > 1;
207 #warn "reaping kids\n";
208 foreach my $pid ( keys %kids ) {
209 my $kid = waitpid($pid, WNOHANG);
215 #warn "done reaping\n";
221 unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
223 confess "usage: spawn CODEREF";
227 #if (!defined($pid = fork)) {
228 my $kid = new IO::Handle;
229 if (!defined($pid = open($kid, '|-'))) {
230 warn "WARNING: cannot fork: $!";
233 warn "begat $pid" if $Debug;
235 #$kids{$pid}->autoflush;
236 return; # I'm the parent
238 # else I'm the child -- go spawn
240 # open(STDIN, "<&Client") || die "can't dup client to stdin";
241 # open(STDOUT, ">&Client") || die "can't dup client to stdout";
242 # open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
247 chomp( my $msg = shift );
248 my $log = new IO::File ">>$log_file";
249 die "can't open $log_file: $!" unless defined($log);
250 flock($log, LOCK_EX);
252 print $log "[client] [". scalar(localtime). "] [$$] $msg\n";
253 flock($log, LOCK_UN);
259 #flock(STDOUT, LOCK_EX) or die "FATAL: can't lock write stream: $!";
261 #open a new one for each kid to get a unique lock
262 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
264 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
269 #flock(STDOUT, LOCK_UN) or die "FATAL: can't release write lock: $!";
271 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";