OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // This is a small program that tries to connect to the X server. It |
| 6 // continually retries until it connects or 30 seconds pass. If it fails |
| 7 // to connect to the X server or fails to find needed functiona, it returns |
| 8 // an error code of -1. |
| 9 // |
| 10 // This is to help verify that a useful X server is available before we start |
| 11 // start running tests on the build bots. |
| 12 |
| 13 #include <errno.h> |
| 14 #include <stdio.h> |
| 15 #include <string.h> |
| 16 #include <time.h> |
| 17 #include <X11/Xlib.h> |
| 18 |
| 19 void Sleep(int duration_ms) { |
| 20 struct timespec sleep_time, remaining; |
| 21 |
| 22 // Contains the portion of duration_ms >= 1 sec. |
| 23 sleep_time.tv_sec = duration_ms / 1000; |
| 24 duration_ms -= sleep_time.tv_sec * 1000; |
| 25 |
| 26 // Contains the portion of duration_ms < 1 sec. |
| 27 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. |
| 28 |
| 29 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) |
| 30 sleep_time = remaining; |
| 31 } |
| 32 |
| 33 class XScopedDisplay { |
| 34 public: |
| 35 XScopedDisplay() : display_(NULL) {} |
| 36 ~XScopedDisplay() { |
| 37 if (display_) XCloseDisplay(display_); |
| 38 } |
| 39 |
| 40 void set(Display* display) { display_ = display; } |
| 41 Display* display() { return display_; } |
| 42 |
| 43 private: |
| 44 Display* display_; |
| 45 }; |
| 46 |
| 47 int main(int argc, char* argv[]) { |
| 48 XScopedDisplay scoped_display; |
| 49 if (argv[1] && strcmp(argv[1], "--noserver") == 0) { |
| 50 scoped_display.set(XOpenDisplay(NULL)); |
| 51 if (scoped_display.display()) { |
| 52 fprintf(stderr, "Found unexpected connectable display %s\n", |
| 53 XDisplayName(NULL)); |
| 54 } |
| 55 // Return success when we got an unexpected display so that the code |
| 56 // without the --noserver is the same, but slow, rather than inverted. |
| 57 return !scoped_display.display(); |
| 58 } |
| 59 |
| 60 int kNumTries = 78; // 78*77/2 * 10 = 30s of waiting |
| 61 int tries; |
| 62 for (tries = 0; tries < kNumTries; ++tries) { |
| 63 scoped_display.set(XOpenDisplay(NULL)); |
| 64 if (scoped_display.display()) |
| 65 break; |
| 66 Sleep(10 * tries); |
| 67 } |
| 68 |
| 69 if (!scoped_display.display()) { |
| 70 fprintf(stderr, "Failed to connect to %s\n", XDisplayName(NULL)); |
| 71 return -1; |
| 72 } |
| 73 |
| 74 fprintf(stderr, "Connected after %d retries\n", tries); |
| 75 |
| 76 return 0; |
| 77 } |
| 78 |
| 79 #if defined(LEAK_SANITIZER) |
| 80 // XOpenDisplay leaks memory if it takes more than one try to connect. This |
| 81 // causes LSan bots to fail. We don't care about memory leaks in xdisplaycheck |
| 82 // anyway, so just disable LSan completely. |
| 83 // This function isn't referenced from the executable itself. Make sure it isn't |
| 84 // stripped by the linker. |
| 85 __attribute__((used)) |
| 86 __attribute__((visibility("default"))) |
| 87 extern "C" int __lsan_is_turned_off() { return 1; } |
| 88 #endif |
OLD | NEW |