Remake
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions
Client

Functions

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

Detailed Description

Function Documentation

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

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

Definition at line 2624 of file remake.cpp.

Referenced by main().

2625 {
2626  if (false)
2627  {
2628  error:
2629  perror("Failed to send targets to server");
2630  exit(EXIT_FAILURE);
2631  }
2632  if (targets.empty()) exit(EXIT_SUCCESS);
2633  DEBUG_open << "Connecting to server... ";
2634 
2635  // Connect to server.
2636 #ifdef WINDOWS
2637  struct sockaddr_in socket_addr;
2638  socket_fd = socket(AF_INET, SOCK_STREAM, 0);
2639  if (socket_fd == INVALID_SOCKET) goto error;
2640  socket_addr.sin_family = AF_INET;
2641  socket_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2642  socket_addr.sin_port = atoi(socket_name);
2643  if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(sockaddr_in)))
2644  goto error;
2645 #else
2646  struct sockaddr_un socket_addr;
2647  size_t len = strlen(socket_name);
2648  if (len >= sizeof(socket_addr.sun_path) - 1) exit(EXIT_FAILURE);
2649  socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2650  if (socket_fd == INVALID_SOCKET) goto error;
2651  socket_addr.sun_family = AF_UNIX;
2652  strcpy(socket_addr.sun_path, socket_name);
2653  if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(socket_addr.sun_family) + len))
2654  goto error;
2655 #ifdef MACOSX
2656  int set_option = 1;
2657  if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option)))
2658  goto error;
2659 #endif
2660 #endif
2661 
2662  // Send current job id.
2663  char *id = getenv("REMAKE_JOB_ID");
2664  int job_id = id ? atoi(id) : -1;
2665  if (send(socket_fd, (char *)&job_id, sizeof(job_id), MSG_NOSIGNAL) != sizeof(job_id))
2666  goto error;
2667 
2668  // Send targets.
2669  for (string_list::const_iterator i = targets.begin(),
2670  i_end = targets.end(); i != i_end; ++i)
2671  {
2672  DEBUG_open << "Sending " << *i << "... ";
2673  ssize_t len = i->length() + 1;
2674  if (send(socket_fd, i->c_str(), len, MSG_NOSIGNAL) != len)
2675  goto error;
2676  }
2677 
2678  // Send terminating nul and wait for reply.
2679  char result = 0;
2680  if (send(socket_fd, &result, 1, MSG_NOSIGNAL) != 1) goto error;
2681  if (recv(socket_fd, &result, 1, 0) != 1) exit(EXIT_FAILURE);
2682  exit(result ? EXIT_SUCCESS : EXIT_FAILURE);
2683 }