Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 1 | #include "aos/linux_code/configuration.h" |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 2 | |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <arpa/inet.h> |
| 9 | #include <ifaddrs.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "aos/common/logging/logging.h" |
| 13 | #include "aos/common/unique_malloc_ptr.h" |
| 14 | #include "aos/common/once.h" |
| 15 | |
| 16 | namespace aos { |
| 17 | namespace configuration { |
| 18 | namespace { |
| 19 | |
| 20 | // Including the terminating '\0'. |
| 21 | const size_t kMaxAddrLength = 18; |
| 22 | |
Austin Schuh | 629821e | 2014-02-10 21:18:27 -0800 | [diff] [blame] | 23 | // TODO(brians): This shouldn't be necesary for running tests. Provide a way to |
| 24 | // set the IP address when running tests from the test. |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 25 | const char *const kLinuxNetInterface = "eth0"; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 26 | const in_addr *DoGetOwnIPAddress() { |
joe | 3779d0c | 2014-02-15 19:41:22 -0800 | [diff] [blame] | 27 | static const char *kOverrideVariable = "FRC971_IP_OVERRIDE"; |
| 28 | const char *override_ip = getenv(kOverrideVariable); |
| 29 | if (override_ip != NULL) { |
| 30 | LOG(INFO, "Override IP is %s\n", override_ip); |
| 31 | static in_addr r; |
| 32 | if (inet_aton(override_ip, &r) != 0) { |
| 33 | return &r; |
| 34 | } else { |
| 35 | LOG(WARNING, "error parsing %s value '%s'\n", kOverrideVariable, override_ip); |
| 36 | } |
| 37 | } else { |
| 38 | LOG(INFO, "Couldn't get environmental variable.\n"); |
| 39 | } |
| 40 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 41 | ifaddrs *addrs; |
| 42 | if (getifaddrs(&addrs) != 0) { |
| 43 | LOG(FATAL, "getifaddrs(%p) failed with %d: %s\n", &addrs, |
| 44 | errno, strerror(errno)); |
| 45 | } |
| 46 | // Smart pointers don't work very well for iterating through a linked list, |
| 47 | // but it does do a very nice job of making sure that addrs gets freed. |
| 48 | unique_c_ptr<ifaddrs, freeifaddrs> addrs_deleter(addrs); |
| 49 | |
Austin Schuh | 629821e | 2014-02-10 21:18:27 -0800 | [diff] [blame] | 50 | for (; addrs != nullptr; addrs = addrs->ifa_next) { |
| 51 | if (addrs->ifa_addr != nullptr && addrs->ifa_addr->sa_family == AF_INET) { |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 52 | if (strcmp(kLinuxNetInterface, addrs->ifa_name) == 0) { |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 53 | static const in_addr r = |
Brian Silverman | 63cf241 | 2013-11-17 05:44:36 -0800 | [diff] [blame] | 54 | reinterpret_cast<sockaddr_in *>(__builtin_assume_aligned( |
| 55 | addrs->ifa_addr, alignof(sockaddr_in)))->sin_addr; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 56 | return &r; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | LOG(FATAL, "couldn't find an AF_INET interface named \"%s\"\n", |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 61 | kLinuxNetInterface); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | const char *DoGetRootDirectory() { |
| 65 | ssize_t size = 0; |
| 66 | char *r = NULL; |
| 67 | while (true) { |
| 68 | if (r != NULL) delete r; |
| 69 | size += 256; |
| 70 | r = new char[size]; |
| 71 | |
| 72 | ssize_t ret = readlink("/proc/self/exe", r, size); |
| 73 | if (ret < 0) { |
| 74 | if (ret != -1) { |
| 75 | LOG(WARNING, "it returned %zd, not -1\n", ret); |
| 76 | } |
| 77 | LOG(FATAL, "readlink(\"/proc/self/exe\", %p, %zu) failed with %d: %s\n", |
| 78 | r, size, errno, strerror(errno)); |
| 79 | } |
| 80 | if (ret < size) { |
Brian Silverman | e6335e4 | 2014-02-20 20:53:06 -0800 | [diff] [blame^] | 81 | void *last_slash = memrchr(r, '/', ret); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 82 | if (last_slash == NULL) { |
| 83 | r[ret] = '\0'; |
| 84 | LOG(FATAL, "couldn't find a '/' in \"%s\"\n", r); |
| 85 | } |
| 86 | *static_cast<char *>(last_slash) = '\0'; |
| 87 | LOG(INFO, "got a root dir of \"%s\"\n", r); |
| 88 | return r; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | const char *DoGetLoggingDirectory() { |
| 94 | static const char kSuffix[] = "/../../tmp/robot_logs"; |
| 95 | const char *root = GetRootDirectory(); |
| 96 | char *r = new char[strlen(root) + sizeof(kSuffix)]; |
| 97 | strcpy(r, root); |
| 98 | strcat(r, kSuffix); |
| 99 | return r; |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | |
| 104 | const char *GetRootDirectory() { |
| 105 | static aos::Once<const char> once(DoGetRootDirectory); |
| 106 | return once.Get(); |
| 107 | } |
| 108 | |
| 109 | const char *GetLoggingDirectory() { |
| 110 | static aos::Once<const char> once(DoGetLoggingDirectory); |
| 111 | return once.Get(); |
| 112 | } |
| 113 | |
| 114 | const in_addr &GetOwnIPAddress() { |
| 115 | static aos::Once<const in_addr> once(DoGetOwnIPAddress); |
| 116 | return *once.Get(); |
| 117 | } |
| 118 | |
| 119 | } // namespace configuration |
| 120 | } // namespace aos |