| |
| |
| #include <errno.h> /* obligatory includes */ |
| #include <signal.h> |
| #include <stdio.h> |
| #include <unistd.h> |
| #include <sys/types.h> |
| #include <sys/socket.h> |
| #include <sys/wait.h> |
| #include <netinet/in.h> |
| #include <netdb.h> |
| #include "tcommon.h" |
| #define MAXHOSTNAME 1024 |
| |
| |
| |
| int call_socket(char *hostname, unsigned short portnum) { |
| struct sockaddr_in sa; |
| struct hostent *hp; |
| int s; |
| |
| if ((hp = gethostbyname(hostname)) == NULL) |
| { |
| errno = ECONNREFUSED; |
| return (-1); |
| } |
| |
| memset(&sa, 0, sizeof(sa)); |
| memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length); |
| sa.sin_family = hp->h_addrtype; |
| sa.sin_port = htons((u_short)portnum); |
| |
| if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) |
| return (-1); |
| if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) |
| { |
| close(s); |
| return (-1); |
| } |
| |
| return (s); |
| } |
| |
| |
| |
| int main(int argc, char *argv[]) { |
| char myname[MAXHOSTNAME + 1]; |
| gethostname(myname, MAXHOSTNAME); |
| |
| int s; |
| int portNumber = 8000; |
| { |
| std::ifstream is("/tmp/.tfarmcontroller.dat"); |
| is >> portNumber; |
| } |
| |
| if ((s = call_socket(myname, portNumber)) < 0) { |
| fprintf(stderr, "Unable to stop the tfarmcontroller daemon\n"); |
| exit(1); |
| } |
| |
| write(s, "shutdown", strlen("shutdown") + 1); |
| |
| close(s); |
| exit(0); |
| } |
| |