HooksManager::unloadLibraries();
// Get rid of any marker files.
- static_cast<void>(unlink(LOAD_MARKER_FILE));
- static_cast<void>(unlink(UNLOAD_MARKER_FILE));
+ static_cast<void>(remove(LOAD_MARKER_FILE));
+ static_cast<void>(remove(UNLOAD_MARKER_FILE));
}
+
+ /// @brief sends commands over specified UNIX socket
+ ///
+ /// @param command command to be sent (should be valid JSON)
+ /// @param response response received (expected to be a valid JSON)
+ /// @param socket_path UNIX socket path
+ ///
+ /// @return true if send/response exchange was successful, false otherwise
+ bool sendCommandUnixSocket(const std::string& command,
+ std::string& response,
+ const std::string& socket_path) {
+
+ // Create UNIX socket
+ int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (socket_fd < 0) {
+ ADD_FAILURE() << "Failed to open unix stream socket.";
+ return (false);
+ }
+
+ // Prepare socket address
+ struct sockaddr_un srv_addr;
+ memset(&srv_addr, 0, sizeof(struct sockaddr_un));
+ srv_addr.sun_family = AF_UNIX;
+ strncpy(srv_addr.sun_path, socket_path.c_str(), sizeof(srv_addr.sun_path));
+ socklen_t len = sizeof(srv_addr);
+
+ // Connect to the specified UNIX socket
+ int status = connect(socket_fd, (struct sockaddr*)&srv_addr, len);
+ if (status == -1) {
+ ADD_FAILURE() << "Failed to connect unix socket: fd=" << socket_fd
+ << ", path=" << socket_path;
+ close(socket_fd);
+ return (false);
+ }
+
+
+ // Send command
+ cout << "Sending command: " << command << endl;
+ int bytes_sent = send(socket_fd, command.c_str(), command.length(), 0);
+ if (bytes_sent < command.length()) {
+ ADD_FAILURE() << "Failed to send " << command.length()
+ << " bytes, send() returned " << bytes_sent;
+ close(socket_fd);
+ return (false);
+ }
+
+ // Receive response
+ /// @todo: this may block if server fails to respond. Some sort of
+ /// of a timer is needed.
+ char buf[65536];
+ memset(buf, 0, sizeof(buf));
+ int bytes_rcvd = recv(socket_fd, buf, sizeof(buf), 0);
+ if (bytes_rcvd < 0) {
+ ADD_FAILURE() << "Failed to receive a response. recv() returned "
+ << bytes_rcvd;
+ close(socket_fd);
+ return (false);
+ }
+
+ // Convert the response to a string, close the socket and return
+ response = string(buf, bytes_rcvd);
+ cout << "Received response: " << response << endl;
+ close(socket_fd);
+ return (true);
+ }
};
TEST_F(CtrlDhcpv4SrvTest, commands) {