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 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, 3 will log packet contents,
23 #including potentially compromising information
25 my $socket = "/usr/local/freeside/selfservice_socket$tag";
26 my $pid_file = "$socket.pid";
28 my $log_file = "/usr/local/freeside/selfservice$tag.log";
30 my $lock_file = "/usr/local/freeside/selfservice$tag.writelock";
36 $SIG{__WARN__} = \&_logmsg;
38 #read data to be cached or something
39 #warn "$me Reading init data\n" if $Debug;
42 warn "Creating $lock_file\n" if $Debug;
43 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
45 warn "Creating $socket\n" if $Debug;
46 my $uaddr = sockaddr_un($socket);
47 my $proto = getprotobyname('tcp');
48 socket(Server,PF_UNIX,SOCK_STREAM,0) or die "socket: $!";
50 bind(Server, $uaddr) or die "bind: $!";
51 listen(Server,SOMAXCONN) or die "listen: $!";
54 open(PIDFILE,"<$pid_file");
55 my $old_pid = <PIDFILE>;
57 $old_pid =~ /^(\d+)$/;
60 open(PIDFILE,">$pid_file");
65 #sub REAPER { $waitedpid = wait; $SIG{CHLD} = \&REAPER; }
66 #$SIG{CHLD} = \&REAPER;
68 warn "entering main loop\n" if $Debug;
72 my $s = new IO::Select;
76 #for ( $waitedpid = 0;
77 # accept(Client,Server) || $waitedpid;
78 # $waitedpid = 0, close Client)
82 #$SIG{PIPE} = sub { warn "SIGPIPE received" };
83 #$SIG{CHLD} = sub { warn "SIGCHLD received" };
85 #sub REAPER { warn "SIGCHLD received"; my $pid = wait; $SIG{CHLD} = \&REAPER; }
86 #sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; }
87 #sub REAPER { my $pid = wait; delete $kids{$pid}; $SIG{CHLD} = \&REAPER; }
88 #$SIG{CHLD} = \&REAPER;
95 warn "waiting for connection\n" if $Debug && !$undisp;
97 #my @handles = $s->can_read();
98 my @handles = $s->can_read(5);
99 $undisp = !scalar(@handles);
100 foreach my $handle ( @handles ) {
102 if ( $handle == \*STDIN ) {
104 warn "receiving packet from server\n" if $Debug;
106 my $packet = fd_retrieve(\*STDIN);
107 my $token = $packet->{'_token'};
108 warn "received packet from server with token $token\n".
110 ? join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
114 if ( exists($kids{$token}) ) {
115 warn "sending return packet to $token via $kids{$token}\n"
117 nstore_fd($packet, $kids{$token});
118 warn "flushing to $token\n" if $Debug;
119 until ( $kids{$token}->flush ) {
120 warn "WARNING: error flushing: $!";
123 #no close or delete here - will block waiting for child
124 warn "done with $token\n" if $Debug;
126 warn "WARNING: unknown token $token, discarding message";
129 } elsif ( $handle == \*Server ) {
131 until ( accept(Client, Server) ) {
132 warn "WARNING: accept failed: $!";
136 warn "received local connection; forking\n" if $Debug;
139 warn "[child-$$] reading packet from local client" if $Debug > 1;
140 my $packet = fd_retrieve(\*Client);
141 warn "[child-$$] packet received:\n".
142 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
144 my $command = $packet->{'command'};
145 #handle some commands weirdly?
146 $packet->{_token}=$$;
148 warn "[child-$$] locking write stream\n" if $Debug > 1;
151 warn "[child-$$] sending packet to remote server\n" if $Debug > 1;
152 nstore_fd($packet, \*STDOUT) or die "FATAL: can't send response: $!";
154 warn "[child-$$] flushing write stream\n" if $Debug > 1;
155 STDOUT->flush or die "FATAL: can't flush: $!";
157 warn "[child-$$] releasing write lock\n" if $Debug > 1;
160 warn "[child-$$] closing write stream\n" if $Debug > 1;
161 close STDOUT or die "FATAL: can't close write stream: $!"; #??!
163 warn "[child-$$] waiting for response from parent\n" if $Debug > 1;
164 my $w = new IO::Select;
166 until ( $w->can_read ) {
167 warn "[child-$$] WARNING: interrupted select: $!\n";
169 my $rv = fd_retrieve(\*STDIN);
173 warn "[child-$$] sending response to local client" if $Debug > 1;
174 nstore_fd($rv, \*Client);
175 Client->flush or die "FATAL: can't flush to local client: $!";
176 close Client or die "FATAL: can't close connection to local client: $!";
178 warn "[child-$$] child exiting" if $Debug > 1;
194 #warn "reaping kids\n";
195 foreach my $pid ( keys %kids ) {
196 my $kid = waitpid($pid, WNOHANG);
202 #warn "done reaping\n";
208 unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
210 confess "usage: spawn CODEREF";
214 #if (!defined($pid = fork)) {
215 my $kid = new IO::Handle;
216 if (!defined($pid = open($kid, '|-'))) {
217 warn "WARNING: cannot fork: $!";
220 warn "begat $pid" if $Debug;
222 #$kids{$pid}->autoflush;
223 return; # I'm the parent
225 # else I'm the child -- go spawn
227 # open(STDIN, "<&Client") || die "can't dup client to stdin";
228 # open(STDOUT, ">&Client") || die "can't dup client to stdout";
229 # open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
234 chomp( my $msg = shift );
235 my $log = new IO::File ">>$log_file";
236 flock($log, LOCK_EX);
238 print $log "[client] [". scalar(localtime). "] [$$] $msg\n";
239 flock($log, LOCK_UN);
245 #flock(STDOUT, LOCK_EX) or die "FATAL: can't lock write stream: $!";
247 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
252 #flock(STDOUT, LOCK_UN) or die "FATAL: can't release write lock: $!";
254 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";