Remake
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 2838 of file remake.cpp.

Referenced by main().

2839 {
2840  if (false)
2841  {
2842  error:
2843  perror("Failed to send targets to server");
2844  exit(EXIT_FAILURE);
2845  }
2846  if (targets.empty()) exit(EXIT_SUCCESS);
2847  DEBUG_open << "Connecting to server... ";
2848 
2849  // Connect to server.
2850 #ifdef WINDOWS
2851  struct sockaddr_in socket_addr;
2852  socket_fd = socket(AF_INET, SOCK_STREAM, 0);
2853  if (socket_fd == INVALID_SOCKET) goto error;
2854  socket_addr.sin_family = AF_INET;
2855  socket_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2856  socket_addr.sin_port = atoi(socket_name);
2857  if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(sockaddr_in)))
2858  goto error;
2859 #else
2860  struct sockaddr_un socket_addr;
2861  size_t len = strlen(socket_name);
2862  if (len >= sizeof(socket_addr.sun_path) - 1) exit(EXIT_FAILURE);
2863  socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2864  if (socket_fd == INVALID_SOCKET) goto error;
2865  socket_addr.sun_family = AF_UNIX;
2866  strcpy(socket_addr.sun_path, socket_name);
2867  if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(socket_addr.sun_family) + len))
2868  goto error;
2869 #ifdef MACOSX
2870  int set_option = 1;
2871  if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option)))
2872  goto error;
2873 #endif
2874 #endif
2875 
2876  // Send current job id.
2877  char *id = getenv("REMAKE_JOB_ID");
2878  int job_id = id ? atoi(id) : -1;
2879  if (send(socket_fd, (char *)&job_id, sizeof(job_id), MSG_NOSIGNAL) != sizeof(job_id))
2880  goto error;
2881 
2882  // Send targets.
2883  for (string_list::const_iterator i = targets.begin(),
2884  i_end = targets.end(); i != i_end; ++i)
2885  {
2886  DEBUG_open << "Sending target " << *i << "... ";
2887  std::string s = 'T' + *i;
2888  ssize_t len = s.length() + 1;
2889  if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2890  goto error;
2891  }
2892 
2893  // Send variables.
2894  for (variable_map::const_iterator i = variables.begin(),
2895  i_end = variables.end(); i != i_end; ++i)
2896  {
2897  DEBUG_open << "Sending variable " << i->first << "... ";
2898  std::string s = 'V' + i->first;
2899  ssize_t len = s.length() + 1;
2900  if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2901  goto error;
2902  for (string_list::const_iterator j = i->second.begin(),
2903  j_end = i->second.end(); j != j_end; ++j)
2904  {
2905  std::string s = 'W' + *j;
2906  len = s.length() + 1;
2907  if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2908  goto error;
2909  }
2910  }
2911 
2912  // Send terminating nul and wait for reply.
2913  char result = 0;
2914  if (send(socket_fd, &result, 1, MSG_NOSIGNAL) != 1) goto error;
2915  if (recv(socket_fd, &result, 1, 0) != 1) exit(EXIT_FAILURE);
2916  exit(result ? EXIT_SUCCESS : EXIT_FAILURE);
2917 }