Remake
Loading...
Searching...
No Matches
Client

Functions

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

Detailed Description

Function Documentation

◆ client_mode()

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

2934{
2935 if (false)
2936 {
2937 error:
2938 perror("Failed to send targets to server");
2939 exit(EXIT_FAILURE);
2940 }
2941 if (targets.empty()) exit(EXIT_SUCCESS);
2942 DEBUG_open << "Connecting to server... ";
2943
2944 // Connect to server.
2945#ifdef WINDOWS
2946 struct sockaddr_in socket_addr;
2947 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
2948 if (socket_fd == INVALID_SOCKET) goto error;
2949 socket_addr.sin_family = AF_INET;
2950 socket_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2951 socket_addr.sin_port = atoi(socket_name);
2952 if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(sockaddr_in)))
2953 goto error;
2954#else
2955 struct sockaddr_un socket_addr;
2956 size_t len = strlen(socket_name);
2957 if (len >= sizeof(socket_addr.sun_path) - 1) exit(EXIT_FAILURE);
2958 socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2959 if (socket_fd == INVALID_SOCKET) goto error;
2960 socket_addr.sun_family = AF_UNIX;
2961 strcpy(socket_addr.sun_path, socket_name);
2962 if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(socket_addr.sun_family) + len))
2963 goto error;
2964#ifdef MACOSX
2965 int set_option = 1;
2966 if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option)))
2967 goto error;
2968#endif
2969#endif
2970
2971 // Send current job id.
2972 char *id = getenv("REMAKE_JOB_ID");
2973 int job_id = id ? atoi(id) : -1;
2974 if (send(socket_fd, (char *)&job_id, sizeof(job_id), MSG_NOSIGNAL) != sizeof(job_id))
2975 goto error;
2976
2977 // Send targets.
2978 for (string_list::const_iterator i = targets.begin(),
2979 i_end = targets.end(); i != i_end; ++i)
2980 {
2981 DEBUG_open << "Sending target " << *i << "... ";
2982 std::string s = 'T' + *i;
2983 ssize_t len = s.length() + 1;
2984 if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2985 goto error;
2986 }
2987
2988 // Send variables.
2989 for (variable_map::const_iterator i = variables.begin(),
2990 i_end = variables.end(); i != i_end; ++i)
2991 {
2992 DEBUG_open << "Sending variable " << i->first << "... ";
2993 std::string s = 'V' + i->first;
2994 ssize_t len = s.length() + 1;
2995 if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2996 goto error;
2997 for (string_list::const_iterator j = i->second.begin(),
2998 j_end = i->second.end(); j != j_end; ++j)
2999 {
3000 std::string s = 'W' + *j;
3001 len = s.length() + 1;
3002 if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
3003 goto error;
3004 }
3005 }
3006
3007 // Send terminating nul and wait for reply.
3008 char result = 0;
3009 if (send(socket_fd, &result, 1, MSG_NOSIGNAL) != 1) goto error;
3010 if (recv(socket_fd, &result, 1, 0) != 1) exit(EXIT_FAILURE);
3011 exit(result ? EXIT_SUCCESS : EXIT_FAILURE);
3012}
static variable_map variables
Definition remake.cpp:617
@ INVALID_SOCKET
Definition remake.cpp:461
static char * socket_name
Definition remake.cpp:708
#define DEBUG_open
Definition remake.cpp:816
static socket_t socket_fd
Definition remake.cpp:697

Referenced by main().