blob: ef6ae3e60f32e633a5e467097ef734011df89be5 [file] [log] [blame]
Brian Silverman14fd0fb2014-01-14 21:42:01 -08001#include "aos/linux_code/configuration.h"
Brian Silverman66f079a2013-08-26 16:24:30 -07002
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
16namespace aos {
17namespace configuration {
18namespace {
19
20// Including the terminating '\0'.
21const size_t kMaxAddrLength = 18;
22
Austin Schuh629821e2014-02-10 21:18:27 -080023// 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 Silverman14fd0fb2014-01-14 21:42:01 -080025const char *const kLinuxNetInterface = "eth0";
Brian Silverman66f079a2013-08-26 16:24:30 -070026const in_addr *DoGetOwnIPAddress() {
joe30af9622014-02-07 21:20:04 -080027 static const char *kOverrideVariable = "FRC971_IP_OVERRIDE";
28 const char *override_ip = getenv(kOverrideVariable);
Austin Schuh629821e2014-02-10 21:18:27 -080029 if (override_ip != nullptr) {
joe30af9622014-02-07 21:20:04 -080030 static in_addr r;
31 if (inet_aton(override_ip, &r) != 0) {
32 return &r;
33 } else {
34 LOG(WARNING, "error parsing %s value '%s'\n", kOverrideVariable, override_ip);
35 }
36 }
37
Brian Silverman66f079a2013-08-26 16:24:30 -070038 ifaddrs *addrs;
39 if (getifaddrs(&addrs) != 0) {
40 LOG(FATAL, "getifaddrs(%p) failed with %d: %s\n", &addrs,
41 errno, strerror(errno));
42 }
43 // Smart pointers don't work very well for iterating through a linked list,
44 // but it does do a very nice job of making sure that addrs gets freed.
45 unique_c_ptr<ifaddrs, freeifaddrs> addrs_deleter(addrs);
46
Austin Schuh629821e2014-02-10 21:18:27 -080047 for (; addrs != nullptr; addrs = addrs->ifa_next) {
48 if (addrs->ifa_addr != nullptr && addrs->ifa_addr->sa_family == AF_INET) {
Brian Silverman14fd0fb2014-01-14 21:42:01 -080049 if (strcmp(kLinuxNetInterface, addrs->ifa_name) == 0) {
Brian Silverman66f079a2013-08-26 16:24:30 -070050 static const in_addr r =
Brian Silverman63cf2412013-11-17 05:44:36 -080051 reinterpret_cast<sockaddr_in *>(__builtin_assume_aligned(
52 addrs->ifa_addr, alignof(sockaddr_in)))->sin_addr;
Brian Silverman66f079a2013-08-26 16:24:30 -070053 return &r;
54 }
55 }
56 }
57 LOG(FATAL, "couldn't find an AF_INET interface named \"%s\"\n",
Brian Silverman14fd0fb2014-01-14 21:42:01 -080058 kLinuxNetInterface);
Brian Silverman66f079a2013-08-26 16:24:30 -070059}
60
61const char *DoGetRootDirectory() {
62 ssize_t size = 0;
63 char *r = NULL;
64 while (true) {
65 if (r != NULL) delete r;
66 size += 256;
67 r = new char[size];
68
69 ssize_t ret = readlink("/proc/self/exe", r, size);
70 if (ret < 0) {
71 if (ret != -1) {
72 LOG(WARNING, "it returned %zd, not -1\n", ret);
73 }
74 LOG(FATAL, "readlink(\"/proc/self/exe\", %p, %zu) failed with %d: %s\n",
75 r, size, errno, strerror(errno));
76 }
77 if (ret < size) {
78 void *last_slash = memrchr(r, '/', size);
79 if (last_slash == NULL) {
80 r[ret] = '\0';
81 LOG(FATAL, "couldn't find a '/' in \"%s\"\n", r);
82 }
83 *static_cast<char *>(last_slash) = '\0';
84 LOG(INFO, "got a root dir of \"%s\"\n", r);
85 return r;
86 }
87 }
88}
89
90const char *DoGetLoggingDirectory() {
91 static const char kSuffix[] = "/../../tmp/robot_logs";
92 const char *root = GetRootDirectory();
93 char *r = new char[strlen(root) + sizeof(kSuffix)];
94 strcpy(r, root);
95 strcat(r, kSuffix);
96 return r;
97}
98
99} // namespace
100
101const char *GetRootDirectory() {
102 static aos::Once<const char> once(DoGetRootDirectory);
103 return once.Get();
104}
105
106const char *GetLoggingDirectory() {
107 static aos::Once<const char> once(DoGetLoggingDirectory);
108 return once.Get();
109}
110
111const in_addr &GetOwnIPAddress() {
112 static aos::Once<const in_addr> once(DoGetOwnIPAddress);
113 return *once.Get();
114}
115
116} // namespace configuration
117} // namespace aos