Remake
Functions
Client

Functions

static void client_mode (char *socket_name, string_list const &targets)
 

Detailed Description

Function Documentation

static void client_mode ( char *  socket_name,
string_list const &  targets 
)
static

Connect to the server socket_name, send a request for building targets with some variables, and exit with the status returned by the server.

Definition at line 2865 of file remake.cpp.

Referenced by main().

2866 {
2867  if (false)
2868  {
2869  error:
2870  perror("Failed to send targets to server");
2871  exit(EXIT_FAILURE);
2872  }
2873  if (targets.empty()) exit(EXIT_SUCCESS);
2874  DEBUG_open << "Connecting to server... ";
2875 
2876  // Connect to server.
2877 #ifdef WINDOWS
2878  struct sockaddr_in socket_addr;
2879  socket_fd = socket(AF_INET, SOCK_STREAM, 0);
2880  if (socket_fd == INVALID_SOCKET) goto error;
2881  socket_addr.sin_family = AF_INET;
2882  socket_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2883  socket_addr.sin_port = atoi(socket_name);
2884  if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(sockaddr_in)))
2885  goto error;
2886 #else
2887  struct sockaddr_un socket_addr;
2888  size_t len = strlen(socket_name);
2889  if (len >= sizeof(socket_addr.sun_path) - 1) exit(EXIT_FAILURE);
2890  socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2891  if (socket_fd == INVALID_SOCKET) goto error;
2892  socket_addr.sun_family = AF_UNIX;
2893  strcpy(socket_addr.sun_path, socket_name);
2894  if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(socket_addr.sun_family) + len))
2895  goto error;
2896 #ifdef MACOSX
2897  int set_option = 1;
2898  if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option)))
2899  goto error;
2900 #endif
2901 #endif
2902 
2903  // Send current job id.
2904  char *id = getenv("REMAKE_JOB_ID");
2905  int job_id = id ? atoi(id) : -1;
2906  if (send(socket_fd, (char *)&job_id, sizeof(job_id), MSG_NOSIGNAL) != sizeof(job_id))
2907  goto error;
2908 
2909  // Send targets.
2910  for (string_list::const_iterator i = targets.begin(),
2911  i_end = targets.end(); i != i_end; ++i)
2912  {
2913  DEBUG_open << "Sending target " << *i << "... ";
2914  std::string s = 'T' + *i;
2915  ssize_t len = s.length() + 1;
2916  if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2917  goto error;
2918  }
2919 
2920  // Send variables.
2921  for (variable_map::const_iterator i = variables.begin(),
2922  i_end = variables.end(); i != i_end; ++i)
2923  {
2924  DEBUG_open << "Sending variable " << i->first << "... ";
2925  std::string s = 'V' + i->first;
2926  ssize_t len = s.length() + 1;
2927  if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2928  goto error;
2929  for (string_list::const_iterator j = i->second.begin(),
2930  j_end = i->second.end(); j != j_end; ++j)
2931  {
2932  std::string s = 'W' + *j;
2933  len = s.length() + 1;
2934  if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2935  goto error;
2936  }
2937  }
2938 
2939  // Send terminating nul and wait for reply.
2940  char result = 0;
2941  if (send(socket_fd, &result, 1, MSG_NOSIGNAL) != 1) goto error;
2942  if (recv(socket_fd, &result, 1, 0) != 1) exit(EXIT_FAILURE);
2943  exit(result ? EXIT_SUCCESS : EXIT_FAILURE);
2944 }
#define DEBUG_open
Definition: remake.cpp:801
static char * socket_name
Definition: remake.cpp:695
static variable_map variables
Definition: remake.cpp:604
static socket_t socket_fd
Definition: remake.cpp:684