Home | History | Annotate | Download | only in src
      1 /************************************************************************
      2 Copyright (c) 2015, The Linux Foundation. All rights reserved.
      3 
      4 Redistribution and use in source and binary forms, with or without
      5 modification, are permitted provided that the following conditions are
      6 met:
      7     * Redistributions of source code must retain the above copyright
      8       notice, this list of conditions and the following disclaimer.
      9     * Redistributions in binary form must reproduce the above
     10       copyright notice, this list of conditions and the following
     11       disclaimer in the documentation and/or other materials provided
     12       with the distribution.
     13     * Neither the name of The Linux Foundation nor the names of its
     14       contributors may be used to endorse or promote products derived
     15       from this software without specific prior written permission.
     16 
     17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 ************************************************************************/
     29 
     30 #include <unistd.h>
     31 #include <errno.h>
     32 #include <stdio.h>
     33 #include <string.h>
     34 #include <stdlib.h>
     35 #include <fcntl.h>
     36 #include <sys/types.h>
     37 #include <sys/wait.h>
     38 #include <sys/stat.h>
     39 
     40 #include "datatop_interface.h"
     41 #include "datatop_fileops.h"
     42 #include "datatop_str.h"
     43 #include "datatop_opt.h"
     44 
     45 #define DTOP_SNAP_SIZE 8192
     46 #define DTOP_SNAP_LINE (DTOP_SNAP_SIZE>>2)
     47 
     48 static int dtop_system_snapshot_helper_print(char *file, const char *str)
     49 {
     50 	FILE *snap_file = fopen(file, "a");
     51 
     52 	if (snap_file) {
     53 		if (fprintf(snap_file, "%s", str) < 0) {
     54 			fclose(snap_file);
     55 			return FILE_ERROR;
     56 		}
     57 	} else {
     58 			return FILE_ERROR;
     59 	}
     60 	fflush(snap_file);
     61 	fclose(snap_file);
     62 	return FILE_SUCCESS;
     63 }
     64 
     65 /**
     66  * @brief A helper function to dtop_print_system_snapshot.
     67  *
     68  * @param fw File that desired system data is printed to.
     69  * @return FILE_ERROR - Writing to file was unsuccessful.
     70  * @return FILE_SUCCESS - Writing to file was successful.
     71  */
     72 static int dtop_run_and_log(char *file, const char *c1, const char **args)
     73 {
     74 	int i;
     75 	pid_t child_pid;
     76 
     77 	i = 0;
     78 	dtop_system_snapshot_helper_print(file, "\n"
     79 	"--------------------------------------------------------------\n"
     80 	"Command: ");
     81 	while(args[i] != 0) {
     82 		dtop_system_snapshot_helper_print(file, args[i++]);
     83 		dtop_system_snapshot_helper_print(file, " ");
     84 	}
     85 	dtop_system_snapshot_helper_print(file, "\n");
     86 
     87 
     88 	child_pid = fork();
     89 	if (child_pid == 0) {
     90 		int fd = open(file, O_WRONLY | O_APPEND | O_CREAT,
     91 						S_IRUSR | S_IWUSR);
     92 		dup2(fd, STDOUT_FILENO);
     93 		dup2(fd, STDERR_FILENO);
     94 		close(fd);
     95 		execvp(c1, (char * const *)args);
     96 		printf("Failed to execute %s\n", c1);
     97 		printf("errno=%d error=%s\n", errno, strerror(errno));
     98 		close(STDOUT_FILENO);
     99 		close(STDERR_FILENO);
    100 		exit(0);
    101 	} else if (child_pid < 0) {
    102 		return FILE_ERROR;
    103 	} else {
    104 		int return_status;
    105 		waitpid(child_pid, &return_status, 0);
    106 
    107 		if (return_status != 0)
    108 			return FILE_ERROR;
    109 	}
    110 
    111 	return FILE_SUCCESS;
    112 }
    113 
    114 
    115 
    116 /* IPv4 */
    117 const char *ip_addr_cmd[] = {"ip", "addr", 0};
    118 const char *ip_route_cmd[] = {"ip", "route", 0};
    119 const char *ip_route_all_tables_cmd[] = {"ip", "route", "show", "table", "all", 0};
    120 const char *ip_rule_cmd[] = {"ip", "rule", "show", 0};
    121 const char *ip_tables_cmd[] = {"iptables", "-L", "-n", "-v", 0};
    122 const char *ip_tables_nat_cmd[] = {"iptables", "-t", "nat", "-L", "-n", "-v", 0};
    123 const char *ip_tables_mangle_cmd[] = {"iptables", "-t", "mangle", "-L", "-n", "-v", 0};
    124 const char *ip_tables_raw_cmd[] = {"iptables", "-t", "raw", "-L", "-n", "-v", 0};
    125 
    126 /* IPv6 */
    127 const char *ip6_addr_cmd[] = {"ip", "-6", "addr", 0};
    128 const char *ip6_route_cmd[] = {"ip", "-6", "route", 0};
    129 const char *ip6_route_all_tables_cmd[] = {"ip", "-6", "route", "show", "table", "all", 0};
    130 const char *ip6_rule_cmd[] = {"ip", "-6", "rule", "show", 0};
    131 const char *ip6_tables_cmd[] = {"ip6tables", "-L", "-n", "-v", 0};
    132 const char *ip6_tables_nat_cmd[] = {"ip6tables", "-t", "nat", "-L", "-n", "-v", 0};
    133 const char *ip6_tables_mangle_cmd[] = {"ip6tables", "-t", "mangle", "-L", "-n", "-v", 0};
    134 const char *ip6_tables_raw_cmd[] = {"ip6tables", "-t", "raw", "-L", "-n", "-v", 0};
    135 
    136 /* Misc */
    137 const char *rps_config[] = {"cat", "/sys/class/net/rmnet_mhi0/queues/rx-0/rps_cpus", 0};
    138 const char *if_config[] = {"/data/busybox/busybox", "ifconfig", 0};
    139 const char *netcfg[] = {"netcfg", 0};
    140 const char *softnet_stat[] = {"cat", "/proc/net/softnet_stat", 0};
    141 
    142 /* XFRM logging */
    143 const char *xfrm_state[] = {"ip", "xfrm", "state", "show", 0};
    144 const char *xfrm_policy[] = {"ip", "xfrm", "policy", "show", 0};
    145 const char *xfrm_netstat[] = {"cat", "/proc/net/xfrm_stat", 0};
    146 
    147 #define DO_DTOP_RUN_AND_LOG(X) \
    148 	dtop_run_and_log(file, X[0], X);
    149 /**
    150  * @brief Prints a System snapshot to a file specified by the user.
    151  *
    152  * @param fw File that system snapshot is printed to.
    153  * @return FILE_ERROR - Writing to file was unsuccessful.
    154  * @return FILE_SUCCESS - Writing to file was successful.
    155  */
    156 int dtop_print_system_snapshot(char *file)
    157 {
    158 	dtop_system_snapshot_helper_print(file,
    159 	"==============================================================\n"
    160 	"    System Data Snapshot - Captured with Data Top             \n"
    161 	"    Version ");
    162 	dtop_system_snapshot_helper_print(file, VERSION);
    163 	dtop_system_snapshot_helper_print(file, "\n"
    164 	"==============================================================\n"
    165 	"\n");
    166 
    167 	/* IPv4 */
    168 	DO_DTOP_RUN_AND_LOG(ip_addr_cmd);
    169 	DO_DTOP_RUN_AND_LOG(ip_route_cmd);
    170 	DO_DTOP_RUN_AND_LOG(ip_route_all_tables_cmd);
    171 	DO_DTOP_RUN_AND_LOG(ip_rule_cmd);
    172 	DO_DTOP_RUN_AND_LOG(ip_tables_cmd);
    173 	DO_DTOP_RUN_AND_LOG(ip_tables_nat_cmd);
    174 	DO_DTOP_RUN_AND_LOG(ip_tables_mangle_cmd);
    175 	DO_DTOP_RUN_AND_LOG(ip_tables_raw_cmd);
    176 
    177 	/* IPv6 */
    178 	DO_DTOP_RUN_AND_LOG(ip6_addr_cmd);
    179 	DO_DTOP_RUN_AND_LOG(ip6_route_cmd);
    180 	DO_DTOP_RUN_AND_LOG(ip6_route_all_tables_cmd);
    181 	DO_DTOP_RUN_AND_LOG(ip6_rule_cmd);
    182 	DO_DTOP_RUN_AND_LOG(ip6_tables_cmd);
    183 	DO_DTOP_RUN_AND_LOG(ip6_tables_nat_cmd);
    184 	DO_DTOP_RUN_AND_LOG(ip6_tables_mangle_cmd);
    185 	DO_DTOP_RUN_AND_LOG(ip6_tables_raw_cmd);
    186 
    187 	/* Misc */
    188 	DO_DTOP_RUN_AND_LOG(rps_config);
    189 	DO_DTOP_RUN_AND_LOG(if_config);
    190 	DO_DTOP_RUN_AND_LOG(netcfg);
    191 	DO_DTOP_RUN_AND_LOG(softnet_stat);
    192 
    193 	/* XFRM logging */
    194 	DO_DTOP_RUN_AND_LOG(xfrm_state);
    195 	DO_DTOP_RUN_AND_LOG(xfrm_policy);
    196 	DO_DTOP_RUN_AND_LOG(xfrm_netstat);
    197 
    198 	return FILE_SUCCESS;
    199 }
    200