Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 2 | * Copyright (C) ARM Limited 2010-2015. All rights reserved. |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 9 | #include <arpa/inet.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <pthread.h> |
| 12 | #include <sys/mman.h> |
| 13 | #include <sys/mount.h> |
| 14 | #include <sys/prctl.h> |
| 15 | #include <sys/resource.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/syscall.h> |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 19 | #include <sys/wait.h> |
| 20 | #include <unistd.h> |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 21 | |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 22 | #include "AnnotateListener.h" |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 23 | #include "CCNDriver.h" |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 24 | #include "Child.h" |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 25 | #include "EventsXML.h" |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 26 | #include "Logging.h" |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 27 | #include "Monitor.h" |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 28 | #include "OlySocket.h" |
| 29 | #include "OlyUtility.h" |
| 30 | #include "SessionData.h" |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 31 | #include "Setup.h" |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 32 | |
| 33 | extern Child* child; |
| 34 | static int shutdownFilesystem(); |
| 35 | static pthread_mutex_t numSessions_mutex; |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 36 | static OlyServerSocket* sock = NULL; |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 37 | static Monitor monitor; |
| 38 | static int numSessions = 0; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 39 | static bool driverRunningAtStart = false; |
| 40 | static bool driverMountedAtStart = false; |
| 41 | |
| 42 | struct cmdline_t { |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 43 | char *module; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 44 | int port; |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 45 | bool update; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 48 | #define DEFAULT_PORT 8080 |
| 49 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 50 | void cleanUp() { |
| 51 | if (shutdownFilesystem() == -1) { |
| 52 | logg->logMessage("Error shutting down gator filesystem"); |
| 53 | } |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 54 | delete sock; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 55 | delete util; |
| 56 | delete logg; |
| 57 | } |
| 58 | |
| 59 | // CTRL C Signal Handler |
| 60 | static void handler(int signum) { |
| 61 | logg->logMessage("Received signal %d, gator daemon exiting", signum); |
| 62 | |
| 63 | // Case 1: both child and parent receive the signal |
| 64 | if (numSessions > 0) { |
| 65 | // Arbitrary sleep of 1 second to give time for the child to exit; |
| 66 | // if something bad happens, continue the shutdown process regardless |
| 67 | sleep(1); |
| 68 | } |
| 69 | |
| 70 | // Case 2: only the parent received the signal |
| 71 | if (numSessions > 0) { |
| 72 | // Kill child threads - the first signal exits gracefully |
| 73 | logg->logMessage("Killing process group as %d child was running when signal was received", numSessions); |
| 74 | kill(0, SIGINT); |
| 75 | |
| 76 | // Give time for the child to exit |
| 77 | sleep(1); |
| 78 | |
| 79 | if (numSessions > 0) { |
| 80 | // The second signal force kills the child |
| 81 | logg->logMessage("Force kill the child"); |
| 82 | kill(0, SIGINT); |
| 83 | // Again, sleep for 1 second |
| 84 | sleep(1); |
| 85 | |
| 86 | if (numSessions > 0) { |
| 87 | // Something bad has really happened; the child is not exiting and therefore may hold the /dev/gator resource open |
| 88 | printf("Unable to kill the gatord child process, thus gator.ko may still be loaded.\n"); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | cleanUp(); |
| 94 | exit(0); |
| 95 | } |
| 96 | |
| 97 | // Child exit Signal Handler |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 98 | static void child_exit(int) { |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 99 | int status; |
| 100 | int pid = wait(&status); |
| 101 | if (pid != -1) { |
| 102 | pthread_mutex_lock(&numSessions_mutex); |
| 103 | numSessions--; |
| 104 | pthread_mutex_unlock(&numSessions_mutex); |
| 105 | logg->logMessage("Child process %d exited with status %d", pid, status); |
| 106 | } |
| 107 | } |
| 108 | |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 109 | static const int UDP_REQ_PORT = 30001; |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 110 | |
| 111 | typedef struct { |
| 112 | char rviHeader[8]; |
| 113 | uint32_t messageID; |
| 114 | uint8_t ethernetAddress[8]; |
| 115 | uint32_t ethernetType; |
| 116 | uint32_t dhcp; |
| 117 | char dhcpName[40]; |
| 118 | uint32_t ipAddress; |
| 119 | uint32_t defaultGateway; |
| 120 | uint32_t subnetMask; |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 121 | uint32_t activeConnections; |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 122 | } RVIConfigureInfo; |
| 123 | |
| 124 | static const char DST_REQ[] = { 'D', 'S', 'T', '_', 'R', 'E', 'Q', ' ', 0, 0, 0, 0x64 }; |
| 125 | |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 126 | class UdpListener { |
| 127 | public: |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 128 | UdpListener() : mDstAns(), mReq(-1) {} |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 129 | |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 130 | void setup(int port) { |
| 131 | mReq = udpPort(UDP_REQ_PORT); |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 132 | |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 133 | // Format the answer buffer |
| 134 | memset(&mDstAns, 0, sizeof(mDstAns)); |
| 135 | memcpy(mDstAns.rviHeader, "STR_ANS ", sizeof(mDstAns.rviHeader)); |
| 136 | if (gethostname(mDstAns.dhcpName, sizeof(mDstAns.dhcpName) - 1) != 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 137 | logg->logError("gethostname failed"); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 138 | handleException(); |
| 139 | } |
| 140 | // Subvert the defaultGateway field for the port number |
| 141 | if (port != DEFAULT_PORT) { |
| 142 | mDstAns.defaultGateway = port; |
| 143 | } |
| 144 | // Subvert the subnetMask field for the protocol version |
| 145 | mDstAns.subnetMask = PROTOCOL_VERSION; |
| 146 | } |
| 147 | |
| 148 | int getReq() const { |
| 149 | return mReq; |
| 150 | } |
| 151 | |
| 152 | void handle() { |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 153 | char buf[128]; |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 154 | struct sockaddr_in6 sockaddr; |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 155 | socklen_t addrlen; |
| 156 | int read; |
| 157 | addrlen = sizeof(sockaddr); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 158 | read = recvfrom(mReq, &buf, sizeof(buf), 0, (struct sockaddr *)&sockaddr, &addrlen); |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 159 | if (read < 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 160 | logg->logError("recvfrom failed"); |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 161 | handleException(); |
| 162 | } else if ((read == 12) && (memcmp(buf, DST_REQ, sizeof(DST_REQ)) == 0)) { |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 163 | // Don't care if sendto fails - gatord shouldn't exit because of it and Streamline will retry |
| 164 | sendto(mReq, &mDstAns, sizeof(mDstAns), 0, (struct sockaddr *)&sockaddr, addrlen); |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 165 | } |
| 166 | } |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 167 | |
| 168 | void close() { |
| 169 | ::close(mReq); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | private: |
| 173 | int udpPort(int port) { |
| 174 | int s; |
| 175 | struct sockaddr_in6 sockaddr; |
| 176 | int on; |
| 177 | int family = AF_INET6; |
| 178 | |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 179 | s = socket_cloexec(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 180 | if (s == -1) { |
| 181 | family = AF_INET; |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 182 | s = socket_cloexec(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 183 | if (s == -1) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 184 | logg->logError("socket failed"); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 185 | handleException(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | on = 1; |
| 190 | if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) != 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 191 | logg->logError("setsockopt REUSEADDR failed"); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 192 | handleException(); |
| 193 | } |
| 194 | |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 195 | // Listen on both IPv4 and IPv6 |
| 196 | on = 0; |
| 197 | if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&on, sizeof(on)) != 0) { |
| 198 | logg->logMessage("setsockopt IPV6_V6ONLY failed"); |
| 199 | } |
| 200 | |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 201 | memset((void*)&sockaddr, 0, sizeof(sockaddr)); |
| 202 | sockaddr.sin6_family = family; |
| 203 | sockaddr.sin6_port = htons(port); |
| 204 | sockaddr.sin6_addr = in6addr_any; |
| 205 | if (bind(s, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 206 | logg->logError("socket failed"); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 207 | handleException(); |
| 208 | } |
| 209 | |
| 210 | return s; |
| 211 | } |
| 212 | |
| 213 | RVIConfigureInfo mDstAns; |
| 214 | int mReq; |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | static UdpListener udpListener; |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 218 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 219 | // retval: -1 = failure; 0 = was already mounted; 1 = successfully mounted |
| 220 | static int mountGatorFS() { |
| 221 | // If already mounted, |
| 222 | if (access("/dev/gator/buffer", F_OK) == 0) { |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | // else, mount the filesystem |
| 227 | mkdir("/dev/gator", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
| 228 | if (mount("nodev", "/dev/gator", "gatorfs", 0, NULL) != 0) { |
| 229 | return -1; |
| 230 | } else { |
| 231 | return 1; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static bool init_module (const char * const location) { |
| 236 | bool ret(false); |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 237 | const int fd = open(location, O_RDONLY | O_CLOEXEC); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 238 | if (fd >= 0) { |
| 239 | struct stat st; |
| 240 | if (fstat(fd, &st) == 0) { |
| 241 | void * const p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 242 | if (p != MAP_FAILED) { |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 243 | if (syscall(__NR_init_module, p, (unsigned long)st.st_size, "") == 0) { |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 244 | ret = true; |
| 245 | } |
| 246 | munmap(p, st.st_size); |
| 247 | } |
| 248 | } |
| 249 | close(fd); |
| 250 | } |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 255 | static bool setupFilesystem(char* module) { |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 256 | if (module) { |
| 257 | // unmount and rmmod if the module was specified on the commandline, i.e. ensure that the specified module is indeed running |
| 258 | shutdownFilesystem(); |
| 259 | |
| 260 | // if still mounted |
| 261 | if (access("/dev/gator/buffer", F_OK) == 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 262 | logg->logError("Unable to remove the running gator.ko. Manually remove the module or use the running module by not specifying one on the commandline"); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 263 | handleException(); |
| 264 | } |
| 265 | } |
| 266 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 267 | const int retval = mountGatorFS(); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 268 | if (retval == 1) { |
| 269 | logg->logMessage("Driver already running at startup"); |
| 270 | driverRunningAtStart = true; |
| 271 | } else if (retval == 0) { |
| 272 | logg->logMessage("Driver already mounted at startup"); |
| 273 | driverRunningAtStart = driverMountedAtStart = true; |
| 274 | } else { |
| 275 | char command[256]; // arbitrarily large amount |
| 276 | char location[256]; // arbitrarily large amount |
| 277 | |
| 278 | if (module) { |
| 279 | strncpy(location, module, sizeof(location)); |
| 280 | } else { |
| 281 | // Is the driver co-located in the same directory? |
| 282 | if (util->getApplicationFullPath(location, sizeof(location)) != 0) { // allow some buffer space |
| 283 | logg->logMessage("Unable to determine the full path of gatord, the cwd will be used"); |
| 284 | } |
| 285 | strncat(location, "gator.ko", sizeof(location) - strlen(location) - 1); |
| 286 | } |
| 287 | |
| 288 | if (access(location, F_OK) == -1) { |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 289 | if (module == NULL) { |
| 290 | // The gator kernel is not already loaded and unable to locate gator.ko in the default location |
| 291 | return false; |
| 292 | } else { |
| 293 | // gator location specified on the command line but it was not found |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 294 | logg->logError("gator module not found at %s", location); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 295 | handleException(); |
| 296 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // Load driver |
| 300 | bool success = init_module(location); |
| 301 | if (!success) { |
| 302 | logg->logMessage("init_module failed, trying insmod"); |
| 303 | snprintf(command, sizeof(command), "insmod %s >/dev/null 2>&1", location); |
| 304 | if (system(command) != 0) { |
| 305 | logg->logMessage("Unable to load gator.ko driver with command: %s", command); |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 306 | logg->logError("Unable to load (insmod) gator.ko driver:\n >>> gator.ko must be built against the current kernel version & configuration\n >>> See dmesg for more details"); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 307 | handleException(); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (mountGatorFS() == -1) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 312 | logg->logError("Unable to mount the gator filesystem needed for profiling."); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 313 | handleException(); |
| 314 | } |
| 315 | } |
| 316 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 317 | return true; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static int shutdownFilesystem() { |
| 321 | if (driverMountedAtStart == false) { |
| 322 | umount("/dev/gator"); |
| 323 | } |
| 324 | if (driverRunningAtStart == false) { |
| 325 | if (syscall(__NR_delete_module, "gator", O_NONBLOCK) != 0) { |
| 326 | logg->logMessage("delete_module failed, trying rmmod"); |
| 327 | if (system("rmmod gator >/dev/null 2>&1") != 0) { |
| 328 | return -1; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return 0; // success |
| 334 | } |
| 335 | |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 336 | static const char OPTSTRING[] = "hvVudap:s:c:e:E:m:o:"; |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 337 | |
| 338 | static bool hasDebugFlag(int argc, char** argv) { |
| 339 | int c; |
| 340 | |
| 341 | optind = 1; |
| 342 | while ((c = getopt(argc, argv, OPTSTRING)) != -1) { |
| 343 | if (c == 'd') { |
| 344 | return true; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return false; |
| 349 | } |
| 350 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 351 | static struct cmdline_t parseCommandLine(int argc, char** argv) { |
| 352 | struct cmdline_t cmdline; |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 353 | cmdline.port = DEFAULT_PORT; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 354 | cmdline.module = NULL; |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 355 | cmdline.update = false; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 356 | char version_string[256]; // arbitrary length to hold the version information |
| 357 | int c; |
| 358 | |
| 359 | // build the version string |
| 360 | if (PROTOCOL_VERSION < PROTOCOL_DEV) { |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 361 | snprintf(version_string, sizeof(version_string), "Streamline gatord version %d (DS-5 v5.%d)", PROTOCOL_VERSION, PROTOCOL_VERSION); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 362 | } else { |
| 363 | snprintf(version_string, sizeof(version_string), "Streamline gatord development version %d", PROTOCOL_VERSION); |
| 364 | } |
| 365 | |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 366 | optind = 1; |
| 367 | while ((c = getopt(argc, argv, OPTSTRING)) != -1) { |
| 368 | switch (c) { |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 369 | case 'c': |
| 370 | gSessionData->mConfigurationXMLPath = optarg; |
| 371 | break; |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 372 | case 'd': |
| 373 | // Already handled |
| 374 | break; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 375 | case 'e': |
| 376 | gSessionData->mEventsXMLPath = optarg; |
| 377 | break; |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 378 | case 'E': |
| 379 | gSessionData->mEventsXMLAppend = optarg; |
| 380 | break; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 381 | case 'm': |
| 382 | cmdline.module = optarg; |
| 383 | break; |
| 384 | case 'p': |
| 385 | cmdline.port = strtol(optarg, NULL, 10); |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 386 | if ((cmdline.port == 8082) || (cmdline.port == 8083)) { |
| 387 | logg->logError("Gator can't use port %i, as it already uses ports 8082 and 8083 for annotations. Please select a different port.", cmdline.port); |
| 388 | handleException(); |
| 389 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 390 | break; |
| 391 | case 's': |
| 392 | gSessionData->mSessionXMLPath = optarg; |
| 393 | break; |
| 394 | case 'o': |
| 395 | gSessionData->mTargetPath = optarg; |
| 396 | break; |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 397 | case 'u': |
| 398 | cmdline.update = true; |
| 399 | break; |
| 400 | case 'a': |
| 401 | gSessionData->mAllowCommands = true; |
| 402 | break; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 403 | case 'h': |
| 404 | case '?': |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 405 | logg->logError( |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 406 | "%s. All parameters are optional:\n" |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 407 | "-c config_xml path and filename of the configuration XML to use\n" |
| 408 | "-e events_xml path and filename of the events XML to use\n" |
| 409 | "-E events_xml path and filename of events XML to append\n" |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 410 | "-h this help page\n" |
| 411 | "-m module path and filename of gator.ko\n" |
| 412 | "-p port_number port upon which the server listens; default is 8080\n" |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 413 | "-s session_xml path and filename of a session.xml used for local capture\n" |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 414 | "-o apc_dir path and name of the output for a local capture\n" |
| 415 | "-v version information\n" |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 416 | "-d enable debug messages\n" |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 417 | "-a allow the user to issue a command from Streamline" |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 418 | , version_string); |
| 419 | handleException(); |
| 420 | break; |
| 421 | case 'v': |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 422 | logg->logError("%s", version_string); |
| 423 | handleException(); |
| 424 | break; |
| 425 | case 'V': |
| 426 | logg->logError("%s\nSRC_MD5: %s", version_string, gSrcMd5); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 427 | handleException(); |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // Error checking |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 433 | if (cmdline.port != DEFAULT_PORT && gSessionData->mSessionXMLPath != NULL) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 434 | logg->logError("Only a port or a session xml can be specified, not both"); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 435 | handleException(); |
| 436 | } |
| 437 | |
| 438 | if (gSessionData->mTargetPath != NULL && gSessionData->mSessionXMLPath == NULL) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 439 | logg->logError("Missing -s command line option required for a local capture."); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 440 | handleException(); |
| 441 | } |
| 442 | |
| 443 | if (optind < argc) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 444 | logg->logError("Unknown argument: %s. Use '-h' for help.", argv[optind]); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 445 | handleException(); |
| 446 | } |
| 447 | |
| 448 | return cmdline; |
| 449 | } |
| 450 | |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 451 | static AnnotateListener annotateListener; |
| 452 | |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 453 | static void handleClient() { |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 454 | OlySocket client(sock->acceptConnection()); |
| 455 | |
| 456 | int pid = fork(); |
| 457 | if (pid < 0) { |
| 458 | // Error |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 459 | logg->logError("Fork process failed. Please power cycle the target device if this error persists."); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 460 | } else if (pid == 0) { |
| 461 | // Child |
| 462 | sock->closeServerSocket(); |
| 463 | udpListener.close(); |
| 464 | monitor.close(); |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 465 | annotateListener.close(); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 466 | child = new Child(&client, numSessions + 1); |
| 467 | child->run(); |
| 468 | delete child; |
| 469 | exit(0); |
| 470 | } else { |
| 471 | // Parent |
| 472 | client.closeSocket(); |
| 473 | |
| 474 | pthread_mutex_lock(&numSessions_mutex); |
| 475 | numSessions++; |
| 476 | pthread_mutex_unlock(&numSessions_mutex); |
| 477 | |
| 478 | // Maximum number of connections is 2 |
| 479 | int wait = 0; |
| 480 | while (numSessions > 1) { |
| 481 | // Throttle until one of the children exits before continuing to accept another socket connection |
| 482 | logg->logMessage("%d sessions active!", numSessions); |
| 483 | if (wait++ >= 10) { // Wait no more than 10 seconds |
| 484 | // Kill last created child |
| 485 | kill(pid, SIGALRM); |
| 486 | break; |
| 487 | } |
| 488 | sleep(1); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 493 | // Gator data flow: collector -> collector fifo -> sender |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 494 | int main(int argc, char** argv) { |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 495 | // Ensure proper signal handling by making gatord the process group leader |
| 496 | // e.g. it may not be the group leader when launched as 'sudo gatord' |
| 497 | setsid(); |
| 498 | |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 499 | // Set up global thread-safe logging |
| 500 | logg = new Logging(hasDebugFlag(argc, argv)); |
| 501 | // Global data class |
| 502 | gSessionData = new SessionData(); |
| 503 | // Set up global utility class |
| 504 | util = new OlyUtility(); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 505 | |
| 506 | // Initialize drivers |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 507 | new CCNDriver(); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 508 | |
| 509 | prctl(PR_SET_NAME, (unsigned long)&"gatord-main", 0, 0, 0); |
| 510 | pthread_mutex_init(&numSessions_mutex, NULL); |
| 511 | |
| 512 | signal(SIGINT, handler); |
| 513 | signal(SIGTERM, handler); |
| 514 | signal(SIGABRT, handler); |
| 515 | |
| 516 | // Set to high priority |
| 517 | if (setpriority(PRIO_PROCESS, syscall(__NR_gettid), -19) == -1) { |
| 518 | logg->logMessage("setpriority() failed"); |
| 519 | } |
| 520 | |
| 521 | // Parse the command line parameters |
| 522 | struct cmdline_t cmdline = parseCommandLine(argc, argv); |
| 523 | |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 524 | if (cmdline.update) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 525 | update(argv[0]); |
| 526 | cmdline.update = false; |
| 527 | gSessionData->mAllowCommands = true; |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 530 | // Verify root permissions |
| 531 | uid_t euid = geteuid(); |
| 532 | if (euid) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 533 | logg->logError("gatord must be launched with root privileges"); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 534 | handleException(); |
| 535 | } |
| 536 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 537 | // Call before setting up the SIGCHLD handler, as system() spawns child processes |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 538 | if (!setupFilesystem(cmdline.module)) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 539 | logg->logMessage("Unable to set up gatorfs, trying perf"); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 540 | if (!gSessionData->perf.setup()) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 541 | logg->logError( |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 542 | "Unable to locate gator.ko driver:\n" |
| 543 | " >>> gator.ko should be co-located with gatord in the same directory\n" |
| 544 | " >>> OR insmod gator.ko prior to launching gatord\n" |
| 545 | " >>> OR specify the location of gator.ko on the command line\n" |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 546 | " >>> OR run Linux 3.4 or later with perf (CONFIG_PERF_EVENTS and CONFIG_HW_PERF_EVENTS) and tracing (CONFIG_TRACING and CONFIG_CONTEXT_SWITCH_TRACER) support to collect data via userspace only"); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 547 | handleException(); |
| 548 | } |
| 549 | } |
| 550 | |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 551 | { |
| 552 | EventsXML eventsXML; |
| 553 | mxml_node_t *xml = eventsXML.getTree(); |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 554 | // Initialize all drivers |
| 555 | for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) { |
| 556 | driver->readEvents(xml); |
| 557 | } |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 558 | mxmlDelete(xml); |
| 559 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 560 | |
| 561 | // Handle child exit codes |
| 562 | signal(SIGCHLD, child_exit); |
| 563 | |
| 564 | // Ignore the SIGPIPE signal so that any send to a broken socket will return an error code instead of asserting a signal |
| 565 | // Handling the error at the send function call is much easier than trying to do anything intelligent in the sig handler |
| 566 | signal(SIGPIPE, SIG_IGN); |
| 567 | |
| 568 | // If the command line argument is a session xml file, no need to open a socket |
| 569 | if (gSessionData->mSessionXMLPath) { |
| 570 | child = new Child(); |
| 571 | child->run(); |
| 572 | delete child; |
| 573 | } else { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 574 | annotateListener.setup(); |
| 575 | int pipefd[2]; |
| 576 | if (pipe_cloexec(pipefd) != 0) { |
| 577 | logg->logError("Unable to set up annotate pipe"); |
| 578 | handleException(); |
| 579 | } |
| 580 | gSessionData->mAnnotateStart = pipefd[1]; |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 581 | sock = new OlyServerSocket(cmdline.port); |
| 582 | udpListener.setup(cmdline.port); |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 583 | if (!monitor.init() || |
| 584 | !monitor.add(sock->getFd()) || |
| 585 | !monitor.add(udpListener.getReq()) || |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 586 | !monitor.add(annotateListener.getSockFd()) || |
| 587 | !monitor.add(annotateListener.getUdsFd()) || |
| 588 | !monitor.add(pipefd[0]) || |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 589 | false) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 590 | logg->logError("Monitor setup failed"); |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame] | 591 | handleException(); |
| 592 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 593 | // Forever loop, can be exited via a signal or exception |
| 594 | while (1) { |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 595 | struct epoll_event events[2]; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 596 | logg->logMessage("Waiting on connection..."); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 597 | int ready = monitor.wait(events, ARRAY_LENGTH(events), -1); |
| 598 | if (ready < 0) { |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 599 | logg->logError("Monitor::wait failed"); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 600 | handleException(); |
| 601 | } |
| 602 | for (int i = 0; i < ready; ++i) { |
| 603 | if (events[i].data.fd == sock->getFd()) { |
| 604 | handleClient(); |
| 605 | } else if (events[i].data.fd == udpListener.getReq()) { |
| 606 | udpListener.handle(); |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 607 | } else if (events[i].data.fd == annotateListener.getSockFd()) { |
| 608 | annotateListener.handleSock(); |
| 609 | } else if (events[i].data.fd == annotateListener.getUdsFd()) { |
| 610 | annotateListener.handleUds(); |
| 611 | } else if (events[i].data.fd == pipefd[0]) { |
| 612 | uint64_t val; |
| 613 | if (read(pipefd[0], &val, sizeof(val)) != sizeof(val)) { |
| 614 | logg->logMessage("Reading annotate pipe failed"); |
| 615 | } |
| 616 | annotateListener.signal(); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | cleanUp(); |
| 623 | return 0; |
| 624 | } |