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