Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <errno.h> /* obligatory includes */</errno.h>
Toshihiro Shimizu 890ddd
#include <signal.h></signal.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys socket.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys wait.h=""></sys>
Toshihiro Shimizu 890ddd
#include <netinet in.h=""></netinet>
Toshihiro Shimizu 890ddd
#include <netdb.h></netdb.h>
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
#define MAXHOSTNAME 1024
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int call_socket(char *hostname, unsigned short portnum) {
Shinya Kitaoka 120a6e
  struct sockaddr_in sa;
Shinya Kitaoka 120a6e
  struct hostent *hp;
Shinya Kitaoka 120a6e
  int s;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if ((hp = gethostbyname(hostname)) == NULL) /* do we know the host's */
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    errno = ECONNREFUSED; /* address? */
Shinya Kitaoka 120a6e
    return (-1);          /* no */
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  memset(&sa, 0, sizeof(sa));
Shinya Kitaoka 120a6e
  memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length); /* set address */
Shinya Kitaoka 120a6e
  sa.sin_family = hp->h_addrtype;
Shinya Kitaoka 120a6e
  sa.sin_port   = htons((u_short)portnum);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) /* get socket */
Shinya Kitaoka 120a6e
    return (-1);
Shinya Kitaoka 120a6e
  if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) /* connect */
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    close(s);
Shinya Kitaoka 120a6e
    return (-1);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return (s);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int main(int argc, char *argv[]) {
Shinya Kitaoka 120a6e
  char myname[MAXHOSTNAME + 1];
Shinya Kitaoka 120a6e
  gethostname(myname, MAXHOSTNAME); /* who are we? */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int s;
Shinya Kitaoka 120a6e
  int portNumber = 8002;
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    std::ifstream is("/tmp/.tfarmserverd.dat");
Shinya Kitaoka 120a6e
    is >> portNumber;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  //  std::cout << "shutting down " << portNumber << std::endl;
Shinya Kitaoka 120a6e
  if ((s = call_socket(myname, portNumber)) < 0) {
Shinya Kitaoka 120a6e
    fprintf(stderr, "Unable to stop the tfarmserved daemon\n");
Shinya Kitaoka 120a6e
    exit(1);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  write(s, "shutdown", strlen("shutdown") + 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  close(s);
Shinya Kitaoka 120a6e
  exit(0);
Toshihiro Shimizu 890ddd
}