blob: a8cc4ae9113c0f139648726db03baed71d8a6e75 [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
Brian Silverman14fd0fb2014-01-14 21:42:01 -080023// TODO(brians): Don't hard-code this.
24const char *const kLinuxNetInterface = "eth0";
Brian Silverman66f079a2013-08-26 16:24:30 -070025const in_addr *DoGetOwnIPAddress() {
joe3779d0c2014-02-15 19:41:22 -080026 static const char *kOverrideVariable = "FRC971_IP_OVERRIDE";
27 const char *override_ip = getenv(kOverrideVariable);
28 if (override_ip != NULL) {
29 LOG(INFO, "Override IP is %s\n", override_ip);
30 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 } else {
37 LOG(INFO, "Couldn't get environmental variable.\n");
38 }
39
Brian Silverman66f079a2013-08-26 16:24:30 -070040 ifaddrs *addrs;
41 if (getifaddrs(&addrs) != 0) {
42 LOG(FATAL, "getifaddrs(%p) failed with %d: %s\n", &addrs,
43 errno, strerror(errno));
44 }
45 // Smart pointers don't work very well for iterating through a linked list,
46 // but it does do a very nice job of making sure that addrs gets freed.
47 unique_c_ptr<ifaddrs, freeifaddrs> addrs_deleter(addrs);
48
49 for (; addrs != NULL; addrs = addrs->ifa_next) {
50 if (addrs->ifa_addr->sa_family == AF_INET) {
Brian Silverman14fd0fb2014-01-14 21:42:01 -080051 if (strcmp(kLinuxNetInterface, addrs->ifa_name) == 0) {
Brian Silverman66f079a2013-08-26 16:24:30 -070052 static const in_addr r =
Brian Silverman63cf2412013-11-17 05:44:36 -080053 reinterpret_cast<sockaddr_in *>(__builtin_assume_aligned(
54 addrs->ifa_addr, alignof(sockaddr_in)))->sin_addr;
Brian Silverman66f079a2013-08-26 16:24:30 -070055 return &r;
56 }
57 }
58 }
59 LOG(FATAL, "couldn't find an AF_INET interface named \"%s\"\n",
Brian Silverman14fd0fb2014-01-14 21:42:01 -080060 kLinuxNetInterface);
Brian Silverman66f079a2013-08-26 16:24:30 -070061}
62
63const char *DoGetRootDirectory() {
64 ssize_t size = 0;
65 char *r = NULL;
66 while (true) {
67 if (r != NULL) delete r;
68 size += 256;
69 r = new char[size];
70
71 ssize_t ret = readlink("/proc/self/exe", r, size);
72 if (ret < 0) {
73 if (ret != -1) {
74 LOG(WARNING, "it returned %zd, not -1\n", ret);
75 }
76 LOG(FATAL, "readlink(\"/proc/self/exe\", %p, %zu) failed with %d: %s\n",
77 r, size, errno, strerror(errno));
78 }
79 if (ret < size) {
80 void *last_slash = memrchr(r, '/', size);
81 if (last_slash == NULL) {
82 r[ret] = '\0';
83 LOG(FATAL, "couldn't find a '/' in \"%s\"\n", r);
84 }
85 *static_cast<char *>(last_slash) = '\0';
86 LOG(INFO, "got a root dir of \"%s\"\n", r);
87 return r;
88 }
89 }
90}
91
92const char *DoGetLoggingDirectory() {
93 static const char kSuffix[] = "/../../tmp/robot_logs";
94 const char *root = GetRootDirectory();
95 char *r = new char[strlen(root) + sizeof(kSuffix)];
96 strcpy(r, root);
97 strcat(r, kSuffix);
98 return r;
99}
100
101} // namespace
102
103const char *GetRootDirectory() {
104 static aos::Once<const char> once(DoGetRootDirectory);
105 return once.Get();
106}
107
108const char *GetLoggingDirectory() {
109 static aos::Once<const char> once(DoGetLoggingDirectory);
110 return once.Get();
111}
112
113const in_addr &GetOwnIPAddress() {
114 static aos::Once<const in_addr> once(DoGetOwnIPAddress);
115 return *once.Get();
116}
117
118} // namespace configuration
119} // namespace aos