Harout Hedeshian | 6202ba7 | 2015-04-13 19:02:25 -0600 | [diff] [blame] | 1 | /************************************************************************ |
Jerome Stanislaus | 597482d | 2016-03-04 14:08:46 -0700 | [diff] [blame] | 2 | Copyright (c) 2015-2016, The Linux Foundation. All rights reserved. |
Harout Hedeshian | 6202ba7 | 2015-04-13 19:02:25 -0600 | [diff] [blame] | 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 | /** |
| 31 | * @file datatop_opt.c |
| 32 | * @brief Adds getopt functionality for CLI commands. |
| 33 | * |
| 34 | * Contains method for getopt functionality used for parsing |
| 35 | * the CLI arguments into executable commands. Handles |
| 36 | * errors which arise when parsing. |
| 37 | */ |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <string.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <unistd.h> |
| 43 | #include <ctype.h> |
| 44 | #include <getopt.h> |
Jerome Stanislaus | 597482d | 2016-03-04 14:08:46 -0700 | [diff] [blame] | 45 | #include <time.h> |
Harout Hedeshian | 6202ba7 | 2015-04-13 19:02:25 -0600 | [diff] [blame] | 46 | #include "datatop_opt.h" |
| 47 | #include "datatop_interface.h" |
| 48 | #include "datatop_linked_list.h" |
| 49 | #include "datatop_fileops.h" |
| 50 | |
| 51 | /** |
| 52 | * @brief Populate the comand line options with sane defaults |
| 53 | * |
| 54 | * @param clopts Struct used to hold data regarding CLI arguments. |
| 55 | */ |
| 56 | void dtop_load_default_options(struct cli_opts *clopts) |
| 57 | { |
| 58 | memset(clopts, 0, sizeof(struct cli_opts)); |
| 59 | |
| 60 | clopts->priority = DEFAULT_NICE; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @brief Parses all CLI commands for main() to execute. |
| 65 | * |
| 66 | * @param clopts Struct used to hold data regarding CLI arguments. |
| 67 | * @param argc Parameter used to read CLI commands from. |
| 68 | * @param argv Parameter used to read CLI arguments from. |
| 69 | * @return PARSE_SUCCESS - CLI arguments read successfully, |
| 70 | * @return PARSE_FAILURE - CLI arguments and/or input not valid. |
| 71 | * @return PARSE_FORCE_EXIT - Exit immediately, print help options. |
| 72 | */ |
| 73 | int dtop_parse_cli_opts(struct cli_opts *clopts, int argc, char **argv) |
| 74 | { |
| 75 | int option; |
Jerome Stanislaus | 597482d | 2016-03-04 14:08:46 -0700 | [diff] [blame] | 76 | time_t rawtime; |
| 77 | struct tm * timeinfo; |
| 78 | char timestamp[100]; |
| 79 | |
| 80 | time ( &rawtime ); |
| 81 | timeinfo = gmtime ( &rawtime ); |
| 82 | strftime (timestamp, 100,"%F_%H-%M-%S",timeinfo); |
Harout Hedeshian | 6202ba7 | 2015-04-13 19:02:25 -0600 | [diff] [blame] | 83 | |
| 84 | if (!clopts || !*argv) { |
| 85 | printf("Internal Error: Null Pointer\n"); |
| 86 | goto error; |
| 87 | } |
| 88 | |
Jerome Stanislaus | 597482d | 2016-03-04 14:08:46 -0700 | [diff] [blame] | 89 | while ((option = getopt(argc, argv, "phri:t:w:o:s:n:")) != -1) { |
Harout Hedeshian | 6202ba7 | 2015-04-13 19:02:25 -0600 | [diff] [blame] | 90 | switch (option) { |
| 91 | case 'p': |
| 92 | clopts->print_cl = OPT_CHOSE; |
| 93 | break; |
| 94 | |
| 95 | case 'h': |
| 96 | dtop_print_help_opts(); |
| 97 | return PARSE_FORCE_EXIT; |
| 98 | break; |
| 99 | |
| 100 | case 'n': |
| 101 | clopts->priority = strtol(optarg, 0, 10); |
| 102 | if (clopts->priority > 19 || clopts->priority < -20) { |
| 103 | printf("Argument for -n is not valid. "); |
| 104 | printf("Must be between -20 and 19.\n"); |
| 105 | goto error; |
| 106 | } |
| 107 | break; |
| 108 | |
| 109 | case 'i': |
| 110 | clopts->poll_per = strtol(optarg, 0, 10); |
| 111 | if (clopts->poll_per <= 0) { |
| 112 | printf("Argument for -i is not valid. "); |
| 113 | printf("Must be positive integer.\n"); |
| 114 | goto error; |
| 115 | } |
| 116 | break; |
| 117 | |
| 118 | case 't': |
| 119 | clopts->poll_time = strtol(optarg, 0, 10); |
| 120 | clopts->poll_time_selected = POLL_TIME_SELECTED; |
| 121 | if (clopts->poll_time <= 0) { |
| 122 | printf("Argument for -t is not valid. "); |
| 123 | printf("Must be positive integer.\n"); |
| 124 | goto error; |
| 125 | } |
| 126 | break; |
| 127 | |
| 128 | case 'w': |
| 129 | if (dtop_check_writefile_access(optarg) == VALID) { |
| 130 | clopts->file_name = optarg; |
| 131 | clopts->print_csv = OPT_CHOSE; |
| 132 | } else { |
| 133 | goto error; |
| 134 | } |
| 135 | break; |
| 136 | |
Jerome Stanislaus | 597482d | 2016-03-04 14:08:46 -0700 | [diff] [blame] | 137 | case 'o': |
| 138 | if (dtop_check_out_dir_presence(optarg) != VALID) { |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | if(strlen(optarg) + strlen(timestamp) > OUT_DIR_LEN_MAX) { |
| 143 | printf("Out dir too long!"); |
| 144 | goto error; |
| 145 | } |
| 146 | strcpy(clopts->out_dir, optarg); |
| 147 | strcat(clopts->out_dir, "/"); |
| 148 | strcat(clopts->out_dir, timestamp); |
| 149 | if(dtop_create_dir(clopts->out_dir) != INVALID) { |
| 150 | goto error; |
| 151 | } |
| 152 | break; |
| 153 | |
Harout Hedeshian | 6202ba7 | 2015-04-13 19:02:25 -0600 | [diff] [blame] | 154 | case 's': |
| 155 | if (dtop_check_writefile_access(optarg) == VALID) |
| 156 | clopts->snapshot_file = optarg; |
| 157 | else |
| 158 | goto error; |
| 159 | break; |
| 160 | |
Jerome Stanislaus | 597482d | 2016-03-04 14:08:46 -0700 | [diff] [blame] | 161 | case 'r': |
| 162 | clopts->iptables_rules_routes = OPT_CHOSE; |
| 163 | break; |
| 164 | |
Harout Hedeshian | 6202ba7 | 2015-04-13 19:02:25 -0600 | [diff] [blame] | 165 | case '?': |
| 166 | default: |
| 167 | goto error; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (clopts->poll_time == 0) { |
| 172 | if (clopts->print_csv == 1) |
| 173 | clopts->poll_time = POLL_NOT_SPECIFIED; |
| 174 | else |
| 175 | clopts->poll_time = POLL_TIME_DEFAULT; |
| 176 | } |
| 177 | if (clopts->poll_per == 0) |
| 178 | clopts->poll_per = DEFAULT_POLL_INTERVAL; |
| 179 | |
| 180 | return PARSE_SUCCESS; |
| 181 | |
| 182 | error: |
| 183 | printf("See datatop -h for help\n"); |
| 184 | return PARSE_FAILURE; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @brief Prints the options the user has for the program to terminal. |
| 189 | */ |
| 190 | void dtop_print_help_opts(void) |
| 191 | { |
| 192 | printf("The following datatop commands are:\n"); |
| 193 | printf("\t-p\t\t\tPrint output to terminal\n"); |
| 194 | printf("\t-i , seconds\t\tSpecify polling period\n"); |
| 195 | printf("\t-t , seconds\t\tSpecify polling duration\n"); |
| 196 | printf("\t-w , file name (.csv)\tWrite output to a file\n"); |
| 197 | printf("\t-s , file name\t\tPrint system snapshot to a file\n"); |
| 198 | printf("\t-n , nice value\t\tSet niceness (default 19)\n"); |
Jerome Stanislaus | 597482d | 2016-03-04 14:08:46 -0700 | [diff] [blame] | 199 | printf("\t-r , \t\t\tCapture IPTables, Rules and Routes\n"); |
| 200 | printf("\t-o , out directory for -w options\t\tOut dir where the set of files are saved\n"); |
Harout Hedeshian | 6202ba7 | 2015-04-13 19:02:25 -0600 | [diff] [blame] | 201 | printf("\t-h\t\t\tGet help\n"); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * @brief Prints the interactive options the user can enter during runtime. |
| 207 | */ |
| 208 | void dtop_print_interactive_opts(void) |
| 209 | { |
| 210 | printf("The following interactive commands are:\n"); |
| 211 | printf("\tq | quit\tTerminate program at any time\n"); |
| 212 | printf("\ti\t\tPrint dp differences, reset initial dp values\n"); |
| 213 | printf("\tl\t\tPrint dp differences since last reset\n"); |
| 214 | printf("\n"); |
| 215 | } |