2
# Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3
# Licensed under the GPL
17
!defined($base) and $base = ".";
18
my $me = { files => { }, handles => { }, base => $base };
31
$me->{files}->{$file} = $handler;
40
my $full = $me->{base} . "/proc/$dir";
43
my $out = `mkdir -p $full 2>&1`;
44
$? and die "mkdir '$full' failed : $out";
47
my $out = `chmod 755 $full 2>&1`;
48
$? and die "chmod 755 $full failed : $out";
50
defined($file) and unlink "$full/$file";
51
return("$full/$file");
55
my ($me, $file, undef, $mode) = @_;
57
my $full = $me->prepare($file, $mode);
59
my $sock = sockaddr_un($full);
60
!defined($sock) and die "sockaddr_un of '$sock' failed : $!";
62
!defined(socket(my $fh, AF_UNIX, SOCK_STREAM, 0)) and
63
die "socket failed : $!";
65
!defined(bind($fh, $sock)) and die "bind failed : $!";
66
my $out = `chmod 777 $full 2>&1`;
67
$? ne 0 and die "'chmod 777 $full' failed : $!";
69
!defined(listen($fh, 5)) and die "listen failed : $!";
71
$me->{select}->add(\*$fh);
72
$me->{handles}->{fileno(\*$fh)} = $file;
78
my $full = $me->prepare($file);
80
my $out = `touch $full/remove 2>&1`;
81
$? != 0 and die "touch $full/remove failed : $out";
86
$me->{select} = IO::Select->new();
88
foreach my $file (keys(%{$me->{files}})){
89
my $handler = $me->{files}->{$file};
90
if(ref($handler) eq "ARRAY"){
91
$me->setup_sock($file, @$handler);
93
elsif($handler eq "remove"){
94
$me->setup_remove($file);
97
die "Bad handler for '$file'";
102
my @ready = $me->{select}->can_read();
104
foreach my $sock (@ready){
105
my $file = $me->{handles}->{fileno($sock)};
106
!defined($file) and die "Couldn't map from socket to file";
108
!accept(CONN, $sock) and die "accept failed : $!";
110
my ($handler, $mode) = @{$me->{files}->{$file}};
112
(!defined($handler) || !defined($mode)) and
113
die "Couldn't map from file to handler";
118
my $input = join("", <CONN>);
119
$output = $handler->($input);
122
$output = $handler->();