blob: 55e5263f4b60711d80de828ac2f13fb634565f74 [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() {
joe30af9622014-02-07 21:20:04 -080026 static const char *kOverrideVariable = "FRC971_IP_OVERRIDE";
27 const char *override_ip = getenv(kOverrideVariable);
28 if (override_ip != NULL) {
29 static in_addr r;
30 if (inet_aton(override_ip, &r) != 0) {
31 return &r;
32 } else {
33 LOG(WARNING, "error parsing %s value '%s'\n", kOverrideVariable, override_ip);
34 }
35 }
36
Brian Silverman66f079a2013-08-26 16:24:30 -070037 ifaddrs *addrs;
38 if (getifaddrs(&addrs) != 0) {
39 LOG(FATAL, "getifaddrs(%p) failed with %d: %s\n", &addrs,
40 errno, strerror(errno));
41 }
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
46 for (; addrs != NULL; addrs = addrs->ifa_next) {
47 if (addrs->ifa_addr->sa_family == AF_INET) {
Brian Silverman14fd0fb2014-01-14 21:42:01 -080048 if (strcmp(kLinuxNetInterface, addrs->ifa_name) == 0) {
Brian Silverman66f079a2013-08-26 16:24:30 -070049 static const in_addr r =
Brian Silverman63cf2412013-11-17 05:44:36 -080050 reinterpret_cast<sockaddr_in *>(__builtin_assume_aligned(
51 addrs->ifa_addr, alignof(sockaddr_in)))->sin_addr;
Brian Silverman66f079a2013-08-26 16:24:30 -070052 return &r;
53 }
54 }
55 }
56 LOG(FATAL, "couldn't find an AF_INET interface named \"%s\"\n",
Brian Silverman14fd0fb2014-01-14 21:42:01 -080057 kLinuxNetInterface);
Brian Silverman66f079a2013-08-26 16:24:30 -070058}
59
60const char *DoGetRootDirectory() {
61 ssize_t size = 0;
62 char *r = NULL;
63 while (true) {
64 if (r != NULL) delete r;
65 size += 256;
66 r = new char[size];
67
68 ssize_t ret = readlink("/proc/self/exe", r, size);
69 if (ret < 0) {
70 if (ret != -1) {
71 LOG(WARNING, "it returned %zd, not -1\n", ret);
72 }
73 LOG(FATAL, "readlink(\"/proc/self/exe\", %p, %zu) failed with %d: %s\n",
74 r, size, errno, strerror(errno));
75 }
76 if (ret < size) {
77 void *last_slash = memrchr(r, '/', size);
78 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