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 2623 of file remake.cpp.

Referenced by main().

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