summaryrefslogtreecommitdiff
path: root/fs_selfservice/FS-SelfService/freeside-selfservice-clientd
blob: 149f894e094601b95ae45794ecaefcb00c7d87c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/perl -w
#
# freeside-selfservice-clientd
#
# This is run REMOTELY over ssh by freeside-selfservice-server

use strict;
use subs qw(spawn logmsg);
use Fcntl qw(:flock);
use Socket;
use Storable qw(nstore_fd fd_retrieve);
use IO::Handle;
use IO::Select;
use IPC::Open2;

use LockFile::Simple qw(lock unlock);

use vars qw( $Debug );
$Debug = 2;

my $socket = "/usr/local/freeside/selfservice_socket";
my $pid_file = "$socket.pid";
my $lock_file = "$socket.lock";
unlink $lock_file;

my $me = '[client]';

$|=1;

#read data to be cached or something
#warn "$me Reading init data\n" if $Debug;
#my $signup_init = 

warn "[client] Creating $socket\n" if $Debug;
my $uaddr = sockaddr_un($socket);
my $proto = getprotobyname('tcp');
socket(Server,PF_UNIX,SOCK_STREAM,0) or die "socket: $!";
unlink($socket);
bind(Server, $uaddr) or die "bind: $!";
listen(Server,SOMAXCONN) or die "listen: $!";

if ( -e $pid_file ) {
  open(PIDFILE,"<$pid_file");
  #chomp( my $old_pid = <PIDFILE> );
  my $old_pid = <PIDFILE>;
  close PIDFILE;
  $old_pid =~ /^(\d+)$/;
  kill 'TERM', $1;
}
open(PIDFILE,">$pid_file");
print PIDFILE "$$\n";
close PIDFILE;

#my $waitedpid;
#sub REAPER { $waitedpid = wait; $SIG{CHLD} = \&REAPER; }
#$SIG{CHLD} =  \&REAPER;

warn "[client] entering main loop\n" if $Debug;

#sub spawn;
#sub logmsg;

my %kids;

  #    my $gar = <STDIN>;

#my $s = new IO::Select;
#$s->add(\*STDIN);
#$s->add(\*Server);

#for ( $waitedpid = 0;
#      accept(Client,Server) || $waitedpid;
#      $waitedpid = 0, close Client)
#{
#  next if $waitedpid;

#$SIG{PIPE} = sub { warn "SIGPIPE received" };
#$SIG{CHLD} = sub { warn "SIGCHLD received" };

sub REAPER { warn "SIGCHLD received"; my $pid = wait; $SIG{CHLD} = \&REAPER; }
#sub REAPER { my $pid = wait; delete $kids{$pid}; $SIG{CHLD} = \&REAPER; }
$SIG{CHLD} =  \&REAPER;

warn "[client] creating IO::Select\n" if $Debug;
my $s = new IO::Select;
$s->add(\*STDIN);
$s->add(\*Server);

while (1) {

warn "[client] waiting for connection or token\n" if $Debug;
while ( my @handles = $s->can_read ) {

  foreach my $handle ( @handles ) {

    if ( $handle == \*STDIN ) {

#      my $gar = <STDIN>;
#      die $gar;

      my $packet = fd_retrieve(\*STDIN);
      my $token = $packet->{'_token'};
      warn "[client] received packet with token $token\n".
           join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
        if $Debug;
     if ( exists($kids{$token}) ) {
        warn "[client] sending return packet to $token via $kids{$token}\n"
          if $Debug;
        nstore_fd($packet, $kids{$token});
        warn "[client] flushing $kids{$token}\n" if $Debug;
        $kids{$token}->flush;
        #eval { $kids{$token}->flush; };
        #die "error flushing?!?!? $@\n" if $@ ne '';
        #warn "[client] closing $kids{$token}\n";
        #close $kids{$token};
        #warn "[client] deleting $kids{$token}\n";
        #delete $kids{$token};
        warn "[client] done with $token\n" if $Debug;
      } else {
        warn "[client] WARNING: unknown token $token, discarding message";
        #die "[client] FATAL: unknown token $token, discarding message";
      }

    } elsif ( $handle == \*Server ) {

      warn "[client] received local connection; forking\n" if $Debug;

      accept(Client, Server);

      spawn sub { #child
        warn "[client-$$] reading packet from local client" if $Debug > 1;
        my $packet = fd_retrieve(\*Client);
        warn "[client-$$] packet received:\n".
             join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
          if $Debug > 1;
        my $command = $packet->{'command'};
        #handle some commands weirdly?
        $packet->{_token}=$$; #??

        warn "[client-$$] sending packet to remote server" if $Debug > 1;
        flock(STDOUT, LOCK_EX); #acquire write lock
        #lock($lock_file);
        nstore_fd($packet, \*STDOUT);
        STDOUT->flush;
        #unlock($lock_file);
        flock(STDOUT, LOCK_UN); #release write lock

        warn "[client-$$] waiting for response from parent" if $Debug > 1;

        #block until parent has a message
        my $w = new IO::Select;
        $w->add(\*STDIN);
        my @wait = $w->can_read;
        my $rv = fd_retrieve(\*STDIN);

        #close STDIN;

        warn "[client-$$] sending response to local client" if $Debug > 1;

        #send message to local client
        nstore_fd($rv, \*Client);
        Client->flush;

        close Client;

        warn "[client-$$] child exiting" if $Debug > 1;

        #while (1) { sleep 5 };
        #sleep 5;
        exit;

      }; #eo child

      #close Client; #in parent, right?

    } else {
      die "wtf?  $handle";
    }

  }

  warn "[client] done handling messages; returning to wait-state" if $Debug;;

}

#die "[client] died unexpectedly: $!\n";
warn "[client] fell-through unexpectedly: $!\n" if $Debug;

} #WTF?

sub spawn {
    my $coderef = shift;

    unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
        use Carp;
        confess "usage: spawn CODEREF";
    }

    my $pid;
    #if (!defined($pid = fork)) {
    my $kid = new IO::Handle;
    if (!defined($pid = open($kid, '|-'))) {
        logmsg "WARNING: cannot fork: $!";
        return;
    } elsif ($pid) {
        logmsg "begat $pid" if $Debug;
        $kids{$pid} = $kid;
        #$kids{$pid}->autoflush;
        return; # I'm the parent
    }
    # else I'm the child -- go spawn

#    open(STDIN,  "<&Client")   || die "can't dup client to stdin";
#    open(STDOUT, ">&Client")   || die "can't dup client to stdout";
    ## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
    exit &$coderef();
}

#sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
#DON'T PRINT!!!!!
sub logmsg { warn "[client] $0 $$: @_ at ", scalar localtime, "\n" }