Branch: Tag:

1998-01-17

1998-01-17 07:50:06 by Fredrik Hübinette (Hubbe) <hubbe@hubbe.net>

rsh replacement for NT + accept() fix

Rev: NT/init_nt:1.8
Rev: NT/tools/lib:1.10
Rev: NT/tools/rntcc:1.8
Rev: NT/tools/sprsh:1.1
Rev: NT/tools/sprshd:1.1
Rev: NT/tools/uname:1.3
Rev: src/modules/files/socket.c:1.23

1: + #!/usr/local/bin/pike    -  + inherit Stdio.Port; +  + void handle_incoming_connection(object(Stdio.File) io) + { +  object p; +  sscanf(io->read(4),"%4c",int args); +  string *cmd=allocate(args); +  for(int e=0;e<args;e++) +  { +  sscanf(io->read(4),"%4c",int len); +  cmd[e]=io->read(len); +  } +  +  object pi=Stdio.File(); +  object p2=pi->pipe(); +  string dir=cmd[0]; +  cmd=cmd[1..]; +  +  switch(cmd[0]) +  { +  case "getenv": +  { +  string s=getenv(cmd[1])+"\n"; +  io->write(sprintf("%4c%s",strlen(s),s)); +  io->write(sprintf("%4c",0)); +  io->write(sprintf("%4c",0)); +  break; +  } +  +  default: +  mixed err=catch { +  p=Process.create_process(cmd, +  ([ +  "stdin":io, +  "stdout":p2, +  "stderr":p2, +  "cwd":dir, +  ])); }; +  +  destruct(p2); +  +  if(!err) +  { +  while(1) +  { +  string s=pi->read(1000,1); +  if(!s || !strlen(s)) break; +  io->write(sprintf("%4c%s",strlen(s),s)); +  } +  +  io->write(sprintf("%4c",0)); +  io->write(sprintf("%4c",p->wait())); +  }else{ +  io->write(sprintf("%4c",0)); +  io->write(sprintf("%4c",69)); +  } +  } +  io->close("w"); +  destruct(io); + } +  +  + int main(int argc, string *argv) + { +  if(c<2) +  { +  werror("Usage: sprshd <port> <host to accept connections from>\n"); +  exit(1); +  } +  if(!bind((int)argv[1])) +  { +  werror("Failed to bind port.\n"); +  exit(1); +  } +  +  string *hosts=gethostbyname(argv[2])[1]; +  +  while(1) +  { +  if(object io=accept()) +  { +  sscanf(io->query_address(),"%s ",string ip); +  if(search(hosts, ip)==-1) +  { +  destruct(io); +  continue; +  } +  thread_create(handle_incoming_connection,io); +  }else{ +  werror("Accept failed "+errno()+"\n"); +  } +  } + }   Newline at end of file added.