blob: e4d5d804bed3201c78181f26b35f1bb5f8e85bcc [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>
Brian Silverman66f079a2013-08-26 16:24:30 -07004#include <stdlib.h>
5#include <sys/types.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8#include <ifaddrs.h>
9#include <unistd.h>
10
11#include "aos/common/logging/logging.h"
12#include "aos/common/unique_malloc_ptr.h"
13#include "aos/common/once.h"
14
15namespace aos {
16namespace configuration {
17namespace {
18
Austin Schuh629821e2014-02-10 21:18:27 -080019// TODO(brians): This shouldn't be necesary for running tests. Provide a way to
20// set the IP address when running tests from the test.
Brian Silverman14fd0fb2014-01-14 21:42:01 -080021const char *const kLinuxNetInterface = "eth0";
Brian Silverman66f079a2013-08-26 16:24:30 -070022const in_addr *DoGetOwnIPAddress() {
Brian Silverman01be0002014-05-10 15:44:38 -070023 static const char *kOverrideVariable = "FRC971_IP_OVERRIDE";
24 const char *override_ip = getenv(kOverrideVariable);
25 if (override_ip != NULL) {
26 LOG(INFO, "Override IP is %s\n", override_ip);
27 static in_addr r;
28 if (inet_aton(override_ip, &r) != 0) {
29 return &r;
joe3779d0c2014-02-15 19:41:22 -080030 } else {
Brian Silverman01be0002014-05-10 15:44:38 -070031 LOG(WARNING, "error parsing %s value '%s'\n",
32 kOverrideVariable, override_ip);
joe3779d0c2014-02-15 19:41:22 -080033 }
Brian Silverman01be0002014-05-10 15:44:38 -070034 } else {
35 LOG(INFO, "Couldn't get environmental variable.\n");
36 }
joe3779d0c2014-02-15 19:41:22 -080037
Brian Silverman66f079a2013-08-26 16:24:30 -070038 ifaddrs *addrs;
39 if (getifaddrs(&addrs) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -070040 PLOG(FATAL, "getifaddrs(%p) failed", &addrs);
Brian Silverman66f079a2013-08-26 16:24:30 -070041 }
42 // Smart pointers don't work very well for iterating through a linked list,
43 // but it does do a very nice job of making sure that addrs gets freed.
44 unique_c_ptr<ifaddrs, freeifaddrs> addrs_deleter(addrs);
45
Austin Schuh629821e2014-02-10 21:18:27 -080046 for (; addrs != nullptr; addrs = addrs->ifa_next) {
Brian Silverman6da04272014-05-18 18:47:48 -070047 // ifa_addr tends to be nullptr on CAN interfaces.
Austin Schuh629821e2014-02-10 21:18:27 -080048 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 }
Brian Silverman01be0002014-05-10 15:44:38 -070074 PLOG(FATAL, "readlink(\"/proc/self/exe\", %p, %zu) failed", r, size);
Brian Silverman66f079a2013-08-26 16:24:30 -070075 }
76 if (ret < size) {
Brian Silvermane6335e42014-02-20 20:53:06 -080077 void *last_slash = memrchr(r, '/', ret);
Brian Silverman66f079a2013-08-26 16:24:30 -070078 if (last_slash == NULL) {
79 r[ret] = '\0';
80 LOG(FATAL, "couldn't find a '/' in \"%s\"\n", r);
81 }
82 *static_cast<char *>(last_slash) = '\0';
83 LOG(INFO, "got a root dir of \"%s\"\n", r);
84 return r;
85 }
86 }
87}
88
89const char *DoGetLoggingDirectory() {
90 static const char kSuffix[] = "/../../tmp/robot_logs";
91 const char *root = GetRootDirectory();
92 char *r = new char[strlen(root) + sizeof(kSuffix)];
93 strcpy(r, root);
94 strcat(r, kSuffix);
95 return r;
96}
97
98} // namespace
99
100const char *GetRootDirectory() {
101 static aos::Once<const char> once(DoGetRootDirectory);
102 return once.Get();
103}
104
105const char *GetLoggingDirectory() {
106 static aos::Once<const char> once(DoGetLoggingDirectory);
107 return once.Get();
108}
109
110const in_addr &GetOwnIPAddress() {
111 static aos::Once<const in_addr> once(DoGetOwnIPAddress);
112 return *once.Get();
113}
114
115} // namespace configuration
116} // namespace aos