blob: 6b1cdf6bbc1e0396bc8a328f2607799fca8d2699 [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() {
joe3779d0c2014-02-15 19:41:22 -080027 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 Silverman66f079a2013-08-26 16:24:30 -070041 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 Schuh629821e2014-02-10 21:18:27 -080050 for (; addrs != nullptr; addrs = addrs->ifa_next) {
51 if (addrs->ifa_addr != nullptr && addrs->ifa_addr->sa_family == AF_INET) {
Brian Silverman14fd0fb2014-01-14 21:42:01 -080052 if (strcmp(kLinuxNetInterface, addrs->ifa_name) == 0) {
Brian Silverman66f079a2013-08-26 16:24:30 -070053 static const in_addr r =
Brian Silverman63cf2412013-11-17 05:44:36 -080054 reinterpret_cast<sockaddr_in *>(__builtin_assume_aligned(
55 addrs->ifa_addr, alignof(sockaddr_in)))->sin_addr;
Brian Silverman66f079a2013-08-26 16:24:30 -070056 return &r;
57 }
58 }
59 }
60 LOG(FATAL, "couldn't find an AF_INET interface named \"%s\"\n",
Brian Silverman14fd0fb2014-01-14 21:42:01 -080061 kLinuxNetInterface);
Brian Silverman66f079a2013-08-26 16:24:30 -070062}
63
64const 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 Silvermane6335e42014-02-20 20:53:06 -080081 void *last_slash = memrchr(r, '/', ret);
Brian Silverman66f079a2013-08-26 16:24:30 -070082 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
93const 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
104const char *GetRootDirectory() {
105 static aos::Once<const char> once(DoGetRootDirectory);
106 return once.Get();
107}
108
109const char *GetLoggingDirectory() {
110 static aos::Once<const char> once(DoGetLoggingDirectory);
111 return once.Get();
112}
113
114const in_addr &GetOwnIPAddress() {
115 static aos::Once<const in_addr> once(DoGetOwnIPAddress);
116 return *once.Get();
117}
118
119} // namespace configuration
120} // namespace aos