test_http_server.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. //
  2. // Copyright (C) 2012 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // This file implements a simple HTTP server. It can exhibit odd behavior
  17. // that's useful for testing. For example, it's useful to test that
  18. // the updater can continue a connection if it's dropped, or that it
  19. // handles very slow data transfers.
  20. // To use this, simply make an HTTP connection to localhost:port and
  21. // GET a url.
  22. #include <err.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <inttypes.h>
  26. #include <netinet/in.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/socket.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include <unistd.h>
  35. #include <algorithm>
  36. #include <string>
  37. #include <vector>
  38. #include <base/logging.h>
  39. #include <base/posix/eintr_wrapper.h>
  40. #include <base/strings/string_split.h>
  41. #include <base/strings/string_util.h>
  42. #include <base/strings/stringprintf.h>
  43. #include "update_engine/common/http_common.h"
  44. // HTTP end-of-line delimiter; sorry, this needs to be a macro.
  45. #define EOL "\r\n"
  46. using std::string;
  47. using std::vector;
  48. namespace chromeos_update_engine {
  49. static const char* kListeningMsgPrefix = "listening on port ";
  50. enum {
  51. RC_OK = 0,
  52. RC_BAD_ARGS,
  53. RC_ERR_READ,
  54. RC_ERR_SETSOCKOPT,
  55. RC_ERR_BIND,
  56. RC_ERR_LISTEN,
  57. RC_ERR_GETSOCKNAME,
  58. RC_ERR_REPORT,
  59. };
  60. struct HttpRequest {
  61. string raw_headers;
  62. string host;
  63. string url;
  64. off_t start_offset{0};
  65. off_t end_offset{0}; // non-inclusive, zero indicates unspecified.
  66. HttpResponseCode return_code{kHttpResponseOk};
  67. };
  68. bool ParseRequest(int fd, HttpRequest* request) {
  69. string headers;
  70. do {
  71. char buf[1024];
  72. ssize_t r = read(fd, buf, sizeof(buf));
  73. if (r < 0) {
  74. perror("read");
  75. exit(RC_ERR_READ);
  76. }
  77. headers.append(buf, r);
  78. } while (!base::EndsWith(headers, EOL EOL, base::CompareCase::SENSITIVE));
  79. LOG(INFO) << "got headers:\n--8<------8<------8<------8<----\n"
  80. << headers << "\n--8<------8<------8<------8<----";
  81. request->raw_headers = headers;
  82. // Break header into lines.
  83. vector<string> lines = base::SplitStringUsingSubstr(
  84. headers.substr(0, headers.length() - strlen(EOL EOL)),
  85. EOL,
  86. base::TRIM_WHITESPACE,
  87. base::SPLIT_WANT_ALL);
  88. // Decode URL line.
  89. vector<string> terms = base::SplitString(lines[0],
  90. base::kWhitespaceASCII,
  91. base::KEEP_WHITESPACE,
  92. base::SPLIT_WANT_NONEMPTY);
  93. CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(3));
  94. CHECK_EQ(terms[0], "GET");
  95. request->url = terms[1];
  96. LOG(INFO) << "URL: " << request->url;
  97. // Decode remaining lines.
  98. size_t i;
  99. for (i = 1; i < lines.size(); i++) {
  100. terms = base::SplitString(lines[i],
  101. base::kWhitespaceASCII,
  102. base::KEEP_WHITESPACE,
  103. base::SPLIT_WANT_NONEMPTY);
  104. if (terms[0] == "Range:") {
  105. CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(2));
  106. string& range = terms[1];
  107. LOG(INFO) << "range attribute: " << range;
  108. CHECK(base::StartsWith(range, "bytes=", base::CompareCase::SENSITIVE) &&
  109. range.find('-') != string::npos);
  110. request->start_offset = atoll(range.c_str() + strlen("bytes="));
  111. // Decode end offset and increment it by one (so it is non-inclusive).
  112. if (range.find('-') < range.length() - 1)
  113. request->end_offset = atoll(range.c_str() + range.find('-') + 1) + 1;
  114. request->return_code = kHttpResponsePartialContent;
  115. string tmp_str = base::StringPrintf(
  116. "decoded range offsets: "
  117. "start=%jd end=",
  118. (intmax_t)request->start_offset);
  119. if (request->end_offset > 0)
  120. base::StringAppendF(
  121. &tmp_str, "%jd (non-inclusive)", (intmax_t)request->end_offset);
  122. else
  123. base::StringAppendF(&tmp_str, "unspecified");
  124. LOG(INFO) << tmp_str;
  125. } else if (terms[0] == "Host:") {
  126. CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(2));
  127. request->host = terms[1];
  128. LOG(INFO) << "host attribute: " << request->host;
  129. } else {
  130. LOG(WARNING) << "ignoring HTTP attribute: `" << lines[i] << "'";
  131. }
  132. }
  133. return true;
  134. }
  135. string Itoa(off_t num) {
  136. char buf[100] = {0};
  137. snprintf(buf, sizeof(buf), "%" PRIi64, num);
  138. return buf;
  139. }
  140. // Writes a string into a file. Returns total number of bytes written or -1 if a
  141. // write error occurred.
  142. ssize_t WriteString(int fd, const string& str) {
  143. const size_t total_size = str.size();
  144. size_t remaining_size = total_size;
  145. char const* data = str.data();
  146. while (remaining_size) {
  147. ssize_t written = write(fd, data, remaining_size);
  148. if (written < 0) {
  149. perror("write");
  150. LOG(INFO) << "write failed";
  151. return -1;
  152. }
  153. data += written;
  154. remaining_size -= written;
  155. }
  156. return total_size;
  157. }
  158. // Writes the headers of an HTTP response into a file.
  159. ssize_t WriteHeaders(int fd,
  160. const off_t start_offset,
  161. const off_t end_offset,
  162. HttpResponseCode return_code) {
  163. ssize_t written = 0, ret;
  164. ret = WriteString(fd,
  165. string("HTTP/1.1 ") + Itoa(return_code) + " " +
  166. GetHttpResponseDescription(return_code) +
  167. EOL "Content-Type: application/octet-stream" EOL);
  168. if (ret < 0)
  169. return -1;
  170. written += ret;
  171. // Compute content legnth.
  172. const off_t content_length = end_offset - start_offset;
  173. // A start offset that equals the end offset indicates that the response
  174. // should contain the full range of bytes in the requested resource.
  175. if (start_offset || start_offset == end_offset) {
  176. ret = WriteString(
  177. fd,
  178. string("Accept-Ranges: bytes" EOL "Content-Range: bytes ") +
  179. Itoa(start_offset == end_offset ? 0 : start_offset) + "-" +
  180. Itoa(end_offset - 1) + "/" + Itoa(end_offset) + EOL);
  181. if (ret < 0)
  182. return -1;
  183. written += ret;
  184. }
  185. ret = WriteString(
  186. fd, string("Content-Length: ") + Itoa(content_length) + EOL EOL);
  187. if (ret < 0)
  188. return -1;
  189. written += ret;
  190. return written;
  191. }
  192. // Writes a predetermined payload of lines of ascending bytes to a file. The
  193. // first byte of output is appropriately offset with respect to the request line
  194. // length. Returns the number of successfully written bytes.
  195. size_t WritePayload(int fd,
  196. const off_t start_offset,
  197. const off_t end_offset,
  198. const char first_byte,
  199. const size_t line_len) {
  200. CHECK_LE(start_offset, end_offset);
  201. CHECK_GT(line_len, static_cast<size_t>(0));
  202. LOG(INFO) << "writing payload: " << line_len << "-byte lines starting with `"
  203. << first_byte << "', offset range " << start_offset << " -> "
  204. << end_offset;
  205. // Populate line of ascending characters.
  206. string line;
  207. line.reserve(line_len);
  208. char byte = first_byte;
  209. size_t i;
  210. for (i = 0; i < line_len; i++)
  211. line += byte++;
  212. const size_t total_len = end_offset - start_offset;
  213. size_t remaining_len = total_len;
  214. bool success = true;
  215. // If start offset is not aligned with line boundary, output partial line up
  216. // to the first line boundary.
  217. size_t start_modulo = start_offset % line_len;
  218. if (start_modulo) {
  219. string partial = line.substr(start_modulo, remaining_len);
  220. ssize_t ret = WriteString(fd, partial);
  221. if ((success = (ret >= 0 && (size_t)ret == partial.length())))
  222. remaining_len -= partial.length();
  223. }
  224. // Output full lines up to the maximal line boundary below the end offset.
  225. while (success && remaining_len >= line_len) {
  226. ssize_t ret = WriteString(fd, line);
  227. if ((success = (ret >= 0 && (size_t)ret == line_len)))
  228. remaining_len -= line_len;
  229. }
  230. // Output a partial line up to the end offset.
  231. if (success && remaining_len) {
  232. string partial = line.substr(0, remaining_len);
  233. ssize_t ret = WriteString(fd, partial);
  234. if ((success = (ret >= 0 && (size_t)ret == partial.length())))
  235. remaining_len -= partial.length();
  236. }
  237. return (total_len - remaining_len);
  238. }
  239. // Write default payload lines of the form 'abcdefghij'.
  240. inline size_t WritePayload(int fd,
  241. const off_t start_offset,
  242. const off_t end_offset) {
  243. return WritePayload(fd, start_offset, end_offset, 'a', 10);
  244. }
  245. // Send an empty response, then kill the server.
  246. void HandleQuit(int fd) {
  247. WriteHeaders(fd, 0, 0, kHttpResponseOk);
  248. LOG(INFO) << "pid(" << getpid() << "): HTTP server exiting ...";
  249. exit(RC_OK);
  250. }
  251. // Generates an HTTP response with payload corresponding to requested offsets
  252. // and length. Optionally, truncate the payload at a given length and add a
  253. // pause midway through the transfer. Returns the total number of bytes
  254. // delivered or -1 for error.
  255. ssize_t HandleGet(int fd,
  256. const HttpRequest& request,
  257. const size_t total_length,
  258. const size_t truncate_length,
  259. const int sleep_every,
  260. const int sleep_secs) {
  261. ssize_t ret;
  262. size_t written = 0;
  263. // Obtain start offset, make sure it is within total payload length.
  264. const size_t start_offset = request.start_offset;
  265. if (start_offset >= total_length) {
  266. LOG(WARNING) << "start offset (" << start_offset
  267. << ") exceeds total length (" << total_length
  268. << "), generating error response ("
  269. << kHttpResponseReqRangeNotSat << ")";
  270. return WriteHeaders(
  271. fd, total_length, total_length, kHttpResponseReqRangeNotSat);
  272. }
  273. // Obtain end offset, adjust to fit in total payload length and ensure it does
  274. // not preceded the start offset.
  275. size_t end_offset =
  276. (request.end_offset > 0 ? request.end_offset : total_length);
  277. if (end_offset < start_offset) {
  278. LOG(WARNING) << "end offset (" << end_offset << ") precedes start offset ("
  279. << start_offset << "), generating error response";
  280. return WriteHeaders(fd, 0, 0, kHttpResponseBadRequest);
  281. }
  282. if (end_offset > total_length) {
  283. LOG(INFO) << "requested end offset (" << end_offset
  284. << ") exceeds total length (" << total_length << "), adjusting";
  285. end_offset = total_length;
  286. }
  287. // Generate headers
  288. LOG(INFO) << "generating response header: range=" << start_offset << "-"
  289. << (end_offset - 1) << "/" << (end_offset - start_offset)
  290. << ", return code=" << request.return_code;
  291. if ((ret = WriteHeaders(fd, start_offset, end_offset, request.return_code)) <
  292. 0)
  293. return -1;
  294. LOG(INFO) << ret << " header bytes written";
  295. written += ret;
  296. // Compute payload length, truncate as necessary.
  297. size_t payload_length = end_offset - start_offset;
  298. if (truncate_length > 0 && truncate_length < payload_length) {
  299. LOG(INFO) << "truncating request payload length (" << payload_length
  300. << ") at " << truncate_length;
  301. payload_length = truncate_length;
  302. end_offset = start_offset + payload_length;
  303. }
  304. LOG(INFO) << "generating response payload: range=" << start_offset << "-"
  305. << (end_offset - 1) << "/" << (end_offset - start_offset);
  306. // Decide about optional midway delay.
  307. if (truncate_length > 0 && sleep_every > 0 && sleep_secs >= 0 &&
  308. start_offset % (truncate_length * sleep_every) == 0) {
  309. const off_t midway_offset = start_offset + payload_length / 2;
  310. if ((ret = WritePayload(fd, start_offset, midway_offset)) < 0)
  311. return -1;
  312. LOG(INFO) << ret << " payload bytes written (first chunk)";
  313. written += ret;
  314. LOG(INFO) << "sleeping for " << sleep_secs << " seconds...";
  315. sleep(sleep_secs);
  316. if ((ret = WritePayload(fd, midway_offset, end_offset)) < 0)
  317. return -1;
  318. LOG(INFO) << ret << " payload bytes written (second chunk)";
  319. written += ret;
  320. } else {
  321. if ((ret = WritePayload(fd, start_offset, end_offset)) < 0)
  322. return -1;
  323. LOG(INFO) << ret << " payload bytes written";
  324. written += ret;
  325. }
  326. LOG(INFO) << "response generation complete, " << written
  327. << " total bytes written";
  328. return written;
  329. }
  330. ssize_t HandleGet(int fd,
  331. const HttpRequest& request,
  332. const size_t total_length) {
  333. return HandleGet(fd, request, total_length, 0, 0, 0);
  334. }
  335. // Handles /redirect/<code>/<url> requests by returning the specified
  336. // redirect <code> with a location pointing to /<url>.
  337. void HandleRedirect(int fd, const HttpRequest& request) {
  338. LOG(INFO) << "Redirecting...";
  339. string url = request.url;
  340. CHECK_EQ(static_cast<size_t>(0), url.find("/redirect/"));
  341. url.erase(0, strlen("/redirect/"));
  342. string::size_type url_start = url.find('/');
  343. CHECK_NE(url_start, string::npos);
  344. HttpResponseCode code = StringToHttpResponseCode(url.c_str());
  345. url.erase(0, url_start);
  346. url = "http://" + request.host + url;
  347. const char* status = GetHttpResponseDescription(code);
  348. if (!status)
  349. CHECK(false) << "Unrecognized redirection code: " << code;
  350. LOG(INFO) << "Code: " << code << " " << status;
  351. LOG(INFO) << "New URL: " << url;
  352. ssize_t ret;
  353. if ((ret = WriteString(fd, "HTTP/1.1 " + Itoa(code) + " " + status + EOL)) <
  354. 0)
  355. return;
  356. WriteString(fd, "Location: " + url + EOL);
  357. }
  358. // Generate a page not found error response with actual text payload. Return
  359. // number of bytes written or -1 for error.
  360. ssize_t HandleError(int fd, const HttpRequest& request) {
  361. LOG(INFO) << "Generating error HTTP response";
  362. ssize_t ret;
  363. size_t written = 0;
  364. const string data("This is an error page.");
  365. if ((ret = WriteHeaders(fd, 0, data.size(), kHttpResponseNotFound)) < 0)
  366. return -1;
  367. written += ret;
  368. if ((ret = WriteString(fd, data)) < 0)
  369. return -1;
  370. written += ret;
  371. return written;
  372. }
  373. // Generate an error response if the requested offset is nonzero, up to a given
  374. // maximal number of successive failures. The error generated is an "Internal
  375. // Server Error" (500).
  376. ssize_t HandleErrorIfOffset(int fd,
  377. const HttpRequest& request,
  378. size_t end_offset,
  379. int max_fails) {
  380. static int num_fails = 0;
  381. if (request.start_offset > 0 && num_fails < max_fails) {
  382. LOG(INFO) << "Generating error HTTP response";
  383. ssize_t ret;
  384. size_t written = 0;
  385. const string data("This is an error page.");
  386. if ((ret = WriteHeaders(
  387. fd, 0, data.size(), kHttpResponseInternalServerError)) < 0)
  388. return -1;
  389. written += ret;
  390. if ((ret = WriteString(fd, data)) < 0)
  391. return -1;
  392. written += ret;
  393. num_fails++;
  394. return written;
  395. } else {
  396. num_fails = 0;
  397. return HandleGet(fd, request, end_offset);
  398. }
  399. }
  400. // Returns a valid response echoing in the body of the response all the headers
  401. // sent by the client.
  402. void HandleEchoHeaders(int fd, const HttpRequest& request) {
  403. WriteHeaders(fd, 0, request.raw_headers.size(), kHttpResponseOk);
  404. WriteString(fd, request.raw_headers);
  405. }
  406. void HandleHang(int fd) {
  407. LOG(INFO) << "Hanging until the other side of the connection is closed.";
  408. char c;
  409. while (HANDLE_EINTR(read(fd, &c, 1)) > 0) {
  410. }
  411. }
  412. void HandleDefault(int fd, const HttpRequest& request) {
  413. const off_t start_offset = request.start_offset;
  414. const string data("unhandled path");
  415. const size_t size = data.size();
  416. ssize_t ret;
  417. if ((ret = WriteHeaders(fd, start_offset, size, request.return_code)) < 0)
  418. return;
  419. WriteString(
  420. fd,
  421. (start_offset < static_cast<off_t>(size) ? data.substr(start_offset)
  422. : ""));
  423. }
  424. // Break a URL into terms delimited by slashes.
  425. class UrlTerms {
  426. public:
  427. UrlTerms(const string& url, size_t num_terms) {
  428. // URL must be non-empty and start with a slash.
  429. CHECK_GT(url.size(), static_cast<size_t>(0));
  430. CHECK_EQ(url[0], '/');
  431. // Split it into terms delimited by slashes, omitting the preceding slash.
  432. terms = base::SplitString(
  433. url.substr(1), "/", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  434. // Ensure expected length.
  435. CHECK_EQ(terms.size(), num_terms);
  436. }
  437. inline const string& Get(const off_t index) const { return terms[index]; }
  438. inline const char* GetCStr(const off_t index) const {
  439. return Get(index).c_str();
  440. }
  441. inline int GetInt(const off_t index) const { return atoi(GetCStr(index)); }
  442. inline size_t GetSizeT(const off_t index) const {
  443. return static_cast<size_t>(atol(GetCStr(index)));
  444. }
  445. private:
  446. vector<string> terms;
  447. };
  448. void HandleConnection(int fd) {
  449. HttpRequest request;
  450. ParseRequest(fd, &request);
  451. string& url = request.url;
  452. LOG(INFO) << "pid(" << getpid() << "): handling url " << url;
  453. if (url == "/quitquitquit") {
  454. HandleQuit(fd);
  455. } else if (base::StartsWith(
  456. url, "/download/", base::CompareCase::SENSITIVE)) {
  457. const UrlTerms terms(url, 2);
  458. HandleGet(fd, request, terms.GetSizeT(1));
  459. } else if (base::StartsWith(url, "/flaky/", base::CompareCase::SENSITIVE)) {
  460. const UrlTerms terms(url, 5);
  461. HandleGet(fd,
  462. request,
  463. terms.GetSizeT(1),
  464. terms.GetSizeT(2),
  465. terms.GetInt(3),
  466. terms.GetInt(4));
  467. } else if (url.find("/redirect/") == 0) {
  468. HandleRedirect(fd, request);
  469. } else if (url == "/error") {
  470. HandleError(fd, request);
  471. } else if (base::StartsWith(
  472. url, "/error-if-offset/", base::CompareCase::SENSITIVE)) {
  473. const UrlTerms terms(url, 3);
  474. HandleErrorIfOffset(fd, request, terms.GetSizeT(1), terms.GetInt(2));
  475. } else if (url == "/echo-headers") {
  476. HandleEchoHeaders(fd, request);
  477. } else if (url == "/hang") {
  478. HandleHang(fd);
  479. } else {
  480. HandleDefault(fd, request);
  481. }
  482. close(fd);
  483. }
  484. } // namespace chromeos_update_engine
  485. using namespace chromeos_update_engine; // NOLINT(build/namespaces)
  486. void usage(const char* prog_arg) {
  487. fprintf(stderr,
  488. "Usage: %s [ FILE ]\n"
  489. "Once accepting connections, the following is written to FILE (or "
  490. "stdout):\n"
  491. "\"%sN\" (where N is an integer port number)\n",
  492. basename(prog_arg),
  493. kListeningMsgPrefix);
  494. }
  495. int main(int argc, char** argv) {
  496. // Check invocation.
  497. if (argc > 2)
  498. errx(RC_BAD_ARGS, "unexpected number of arguments (use -h for usage)");
  499. // Parse (optional) argument.
  500. int report_fd = STDOUT_FILENO;
  501. if (argc == 2) {
  502. if (!strcmp(argv[1], "-h")) {
  503. usage(argv[0]);
  504. exit(RC_OK);
  505. }
  506. report_fd = open(argv[1], O_WRONLY | O_CREAT, 00644);
  507. }
  508. // Ignore SIGPIPE on write() to sockets.
  509. signal(SIGPIPE, SIG_IGN);
  510. int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  511. if (listen_fd < 0)
  512. LOG(FATAL) << "socket() failed";
  513. struct sockaddr_in server_addr = sockaddr_in();
  514. server_addr.sin_family = AF_INET;
  515. server_addr.sin_addr.s_addr = INADDR_ANY;
  516. server_addr.sin_port = 0;
  517. {
  518. // Get rid of "Address in use" error
  519. int tr = 1;
  520. if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &tr, sizeof(int)) ==
  521. -1) {
  522. perror("setsockopt");
  523. exit(RC_ERR_SETSOCKOPT);
  524. }
  525. }
  526. // Bind the socket and set for listening.
  527. if (bind(listen_fd,
  528. reinterpret_cast<struct sockaddr*>(&server_addr),
  529. sizeof(server_addr)) < 0) {
  530. perror("bind");
  531. exit(RC_ERR_BIND);
  532. }
  533. if (listen(listen_fd, 5) < 0) {
  534. perror("listen");
  535. exit(RC_ERR_LISTEN);
  536. }
  537. // Check the actual port.
  538. struct sockaddr_in bound_addr = sockaddr_in();
  539. socklen_t bound_addr_len = sizeof(bound_addr);
  540. if (getsockname(listen_fd,
  541. reinterpret_cast<struct sockaddr*>(&bound_addr),
  542. &bound_addr_len) < 0) {
  543. perror("getsockname");
  544. exit(RC_ERR_GETSOCKNAME);
  545. }
  546. in_port_t port = ntohs(bound_addr.sin_port);
  547. // Output the listening port, indicating that the server is processing
  548. // requests. IMPORTANT! (a) the format of this message is as expected by some
  549. // unit tests, avoid unilateral changes; (b) it is necessary to flush/sync the
  550. // file to prevent the spawning process from waiting indefinitely for this
  551. // message.
  552. string listening_msg = base::StringPrintf("%s%hu", kListeningMsgPrefix, port);
  553. LOG(INFO) << listening_msg;
  554. CHECK_EQ(write(report_fd, listening_msg.c_str(), listening_msg.length()),
  555. static_cast<int>(listening_msg.length()));
  556. CHECK_EQ(write(report_fd, "\n", 1), 1);
  557. if (report_fd == STDOUT_FILENO)
  558. fsync(report_fd);
  559. else
  560. close(report_fd);
  561. while (1) {
  562. LOG(INFO) << "pid(" << getpid() << "): waiting to accept new connection";
  563. int client_fd = accept(listen_fd, nullptr, nullptr);
  564. LOG(INFO) << "got past accept";
  565. if (client_fd < 0)
  566. LOG(FATAL) << "ERROR on accept";
  567. HandleConnection(client_fd);
  568. }
  569. return 0;
  570. }