tahoma2d/toonz/sources/toonzfarm/tfarmcontroller/tfarmcontroller_shutdown.cpp

67 lines
1.6 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#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
//----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int call_socket(char *hostname, unsigned short portnum) {
struct sockaddr_in sa;
struct hostent *hp;
int s;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if ((hp = gethostbyname(hostname)) == NULL) /* do we know the host's */
{
errno = ECONNREFUSED; /* address? */
return (-1); /* no */
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
memset(&sa, 0, sizeof(sa));
memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length); /* set address */
sa.sin_family = hp->h_addrtype;
sa.sin_port = htons((u_short)portnum);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) /* get socket */
return (-1);
if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) /* connect */
{
close(s);
return (-1);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return (s);
2016-03-19 06:57:51 +13:00
}
//----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int main(int argc, char *argv[]) {
char myname[MAXHOSTNAME + 1];
gethostname(myname, MAXHOSTNAME); /* who are we? */
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int s;
int portNumber = 8000;
{
std::ifstream is("/tmp/.tfarmcontroller.dat");
is >> portNumber;
}
// std::cout << "shutting down " << portNumber << std::endl;
if ((s = call_socket(myname, portNumber)) < 0) {
fprintf(stderr, "Unable to stop the tfarmcontroller daemon\n");
exit(1);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
write(s, "shutdown", strlen("shutdown") + 1);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
close(s);
exit(0);
2016-03-19 06:57:51 +13:00
}