1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 5f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 6f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 7f08c3bdfSopenharmony_ci * (at your option) any later version. 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 15f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 16f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17f08c3bdfSopenharmony_ci * 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci/* 21f08c3bdfSopenharmony_ci * File: ltpapicmd.c 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * Description: This program impliments a command line version of some of the 24f08c3bdfSopenharmony_ci * LTP harness API's. This will enable tests written in shell and 25f08c3bdfSopenharmony_ci * other scripts to report problems and log results in the LTP 26f08c3bdfSopenharmony_ci * harness format. The intent is to have a common format in which 27f08c3bdfSopenharmony_ci * the C tests and tests written in scripts report results in 28f08c3bdfSopenharmony_ci * a common format. 29f08c3bdfSopenharmony_ci * 30f08c3bdfSopenharmony_ci * The following LTP API's are available currently in command line 31f08c3bdfSopenharmony_ci * form: 32f08c3bdfSopenharmony_ci * tst_brk - Print result message and break remaining test cases 33f08c3bdfSopenharmony_ci * tst_brkm - Print result message, including file contents, and 34f08c3bdfSopenharmony_ci * break remaining test cases 35f08c3bdfSopenharmony_ci * tst_res - Print result message, including file contents 36f08c3bdfSopenharmony_ci * tst_resm - Print result message 37f08c3bdfSopenharmony_ci * tst_exit - Exit test with a meaningful exit value 38f08c3bdfSopenharmony_ci * 39f08c3bdfSopenharmony_ci * These are the minimum set of functions or commands required to 40f08c3bdfSopenharmony_ci * report results. 41f08c3bdfSopenharmony_ci * 42f08c3bdfSopenharmony_ci * Exit: All commands exit with 43f08c3bdfSopenharmony_ci * 0 - on success 44f08c3bdfSopenharmony_ci * -1 - on failure 45f08c3bdfSopenharmony_ci * 46f08c3bdfSopenharmony_ci * History 47f08c3bdfSopenharmony_ci * Dec 10 2002 - Created - Manoj Iyer manjo@mail.utexas.edu 48f08c3bdfSopenharmony_ci * Dec 12 2002 - Modified - Code that checked if the environment variables 49f08c3bdfSopenharmony_ci * TCID and TST_TOTAL were set did not print usage message. 50f08c3bdfSopenharmony_ci * Modified code to print usage message in each case. 51f08c3bdfSopenharmony_ci * Dec 16 2002 - Modified - Code to get the test number, gets environment 52f08c3bdfSopenharmony_ci * variable TST_COUNT and initializes tst_count. 53f08c3bdfSopenharmony_ci * Dec 16 2002 - Documentation and comment changes. 54f08c3bdfSopenharmony_ci * Feb 11 2003 - tst_count was set to -1 during init or setup in the script. 55f08c3bdfSopenharmony_ci * this was causing tst_resm to issue a warning message. 56f08c3bdfSopenharmony_ci * This bug is now fixed. 57f08c3bdfSopenharmony_ci * 58f08c3bdfSopenharmony_ci */ 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci#include <sys/socket.h> 61f08c3bdfSopenharmony_ci#include <stdio.h> 62f08c3bdfSopenharmony_ci#include <string.h> 63f08c3bdfSopenharmony_ci#include <stdlib.h> 64f08c3bdfSopenharmony_ci#include <stdint.h> 65f08c3bdfSopenharmony_ci#include "test.h" 66f08c3bdfSopenharmony_ci#include "usctest.h" 67f08c3bdfSopenharmony_ci#include "safe_macros.h" 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_cichar *TCID; /* Name of the testcase */ 70f08c3bdfSopenharmony_ciint TST_TOTAL; /* Total number of testcases */ 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_cistatic char cmd_name[1024]; /* name by which this program is invoked tst_brk etc */ 73f08c3bdfSopenharmony_cistatic char *tst_total; /* total number of tests in the file. */ 74f08c3bdfSopenharmony_cistatic char *tst_cntstr; /* sets the value of tst_count with this value */ 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci/* 78f08c3bdfSopenharmony_ci * Function: ident_ttype - Return test result type. 79f08c3bdfSopenharmony_ci * 80f08c3bdfSopenharmony_ci * Description: This function will return the test result type, it actually 81f08c3bdfSopenharmony_ci * the string that is entered by the user to an integer value that 82f08c3bdfSopenharmony_ci * is understood by the API's. 83f08c3bdfSopenharmony_ci * 84f08c3bdfSopenharmony_ci * Return: test type TPASS, TFAIL, TBROK, TCONF, TWARN, or TINFO 85f08c3bdfSopenharmony_ci * on success 86f08c3bdfSopenharmony_ci * -1 on failure 87f08c3bdfSopenharmony_ci */ 88f08c3bdfSopenharmony_ciint ident_ttype(char *tstype) 89f08c3bdfSopenharmony_ci{ 90f08c3bdfSopenharmony_ci /* test result type one of TPASS, TFAIL, etc */ 91f08c3bdfSopenharmony_ci if (strcmp(tstype, "TBROK") == 0) 92f08c3bdfSopenharmony_ci return TBROK; 93f08c3bdfSopenharmony_ci else if (strcmp(tstype, "TFAIL") == 0) 94f08c3bdfSopenharmony_ci return TFAIL; 95f08c3bdfSopenharmony_ci else if (strcmp(tstype, "TPASS") == 0) 96f08c3bdfSopenharmony_ci return TPASS; 97f08c3bdfSopenharmony_ci else if (strcmp(tstype, "TCONF") == 0) 98f08c3bdfSopenharmony_ci return TCONF; 99f08c3bdfSopenharmony_ci else if (strcmp(tstype, "TWARN") == 0) 100f08c3bdfSopenharmony_ci return TWARN; 101f08c3bdfSopenharmony_ci else if (strcmp(tstype, "TINFO") == 0) 102f08c3bdfSopenharmony_ci return TINFO; 103f08c3bdfSopenharmony_ci else 104f08c3bdfSopenharmony_ci return -1; 105f08c3bdfSopenharmony_ci} 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_civoid tst_cat_file(const char *filename) 108f08c3bdfSopenharmony_ci{ 109f08c3bdfSopenharmony_ci const char *cmd[] = {"cat", filename, NULL}; 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci tst_cmd(NULL, cmd, NULL, NULL, 0); 112f08c3bdfSopenharmony_ci} 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_civoid apicmd_brk(int argc, char *argv[]) 115f08c3bdfSopenharmony_ci{ 116f08c3bdfSopenharmony_ci int trestype; 117f08c3bdfSopenharmony_ci char *file_name; 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci if (argc < 5) { 120f08c3bdfSopenharmony_ci fprintf(stderr, "Usage: %s TTYPE FNAME FUNC STRING\n" 121f08c3bdfSopenharmony_ci "\tTTYPE - Test Result Type; one of TFAIL, TBROK " 122f08c3bdfSopenharmony_ci "and TCONF.\n" 123f08c3bdfSopenharmony_ci "\tFNAME - Print contents of this file after the message\n" 124f08c3bdfSopenharmony_ci "\tFUNC - Cleanup function (ignored), but MUST be provided\n" 125f08c3bdfSopenharmony_ci "\tSTRING - Message explaining the test result\n", 126f08c3bdfSopenharmony_ci cmd_name); 127f08c3bdfSopenharmony_ci exit(1); 128f08c3bdfSopenharmony_ci } 129f08c3bdfSopenharmony_ci trestype = ident_ttype((argv++)[0]); 130f08c3bdfSopenharmony_ci file_name = (argv++)[0]; 131f08c3bdfSopenharmony_ci tst_cat_file(file_name); 132f08c3bdfSopenharmony_ci argv++; 133f08c3bdfSopenharmony_ci tst_brkm(trestype, NULL, "%s", *argv); 134f08c3bdfSopenharmony_ci 135f08c3bdfSopenharmony_ci} 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_civoid apicmd_res(int argc, char *argv[]) 138f08c3bdfSopenharmony_ci{ 139f08c3bdfSopenharmony_ci int trestype; 140f08c3bdfSopenharmony_ci char *file_name; 141f08c3bdfSopenharmony_ci 142f08c3bdfSopenharmony_ci if (argc < 4) { 143f08c3bdfSopenharmony_ci fprintf(stderr, "Usage: %s TTYPE FNAME STRING\n" 144f08c3bdfSopenharmony_ci "\tTTYPE - Test Result Type; one of TFAIL, TBROK " 145f08c3bdfSopenharmony_ci "and TCONF.\n" 146f08c3bdfSopenharmony_ci "\tFNAME - Print contents of this file after the message\n" 147f08c3bdfSopenharmony_ci "\tSTRING - Message explaining the test result\n", 148f08c3bdfSopenharmony_ci cmd_name); 149f08c3bdfSopenharmony_ci exit(1); 150f08c3bdfSopenharmony_ci } 151f08c3bdfSopenharmony_ci trestype = ident_ttype((argv++)[0]); 152f08c3bdfSopenharmony_ci file_name = (argv++)[0]; 153f08c3bdfSopenharmony_ci tst_cat_file(file_name); 154f08c3bdfSopenharmony_ci tst_resm(trestype, "%s", *argv); 155f08c3bdfSopenharmony_ci} 156f08c3bdfSopenharmony_ci 157f08c3bdfSopenharmony_civoid apicmd_brkm(int argc, char *argv[]) 158f08c3bdfSopenharmony_ci{ 159f08c3bdfSopenharmony_ci int trestype; 160f08c3bdfSopenharmony_ci 161f08c3bdfSopenharmony_ci if (argc < 4) { 162f08c3bdfSopenharmony_ci fprintf(stderr, "Usage: %s TTYPE FUNC STRING\n" 163f08c3bdfSopenharmony_ci "\tTTYPE - Test Result Type; one of TFAIL, TBROK " 164f08c3bdfSopenharmony_ci "and TCONF.\n" 165f08c3bdfSopenharmony_ci "\tFUNC - Cleanup function (ignored), but MUST be provided\n" 166f08c3bdfSopenharmony_ci "\tSTRING - Message explaining the test result\n", 167f08c3bdfSopenharmony_ci cmd_name); 168f08c3bdfSopenharmony_ci exit(1); 169f08c3bdfSopenharmony_ci } 170f08c3bdfSopenharmony_ci trestype = ident_ttype((argv++)[0]); 171f08c3bdfSopenharmony_ci argv++; 172f08c3bdfSopenharmony_ci tst_brkm(trestype, NULL, "%s", *argv); 173f08c3bdfSopenharmony_ci} 174f08c3bdfSopenharmony_ci 175f08c3bdfSopenharmony_civoid apicmd_resm(int argc, char *argv[]) 176f08c3bdfSopenharmony_ci{ 177f08c3bdfSopenharmony_ci int trestype; 178f08c3bdfSopenharmony_ci 179f08c3bdfSopenharmony_ci if (argc < 3) { 180f08c3bdfSopenharmony_ci fprintf(stderr, "Usage: %s TTYPE STRING\n" 181f08c3bdfSopenharmony_ci "\tTTYPE - Test Result Type; one of TFAIL, TBROK" 182f08c3bdfSopenharmony_ci "and TCONF.\n" 183f08c3bdfSopenharmony_ci "\tSTRING - Message explaining the test result\n", 184f08c3bdfSopenharmony_ci cmd_name); 185f08c3bdfSopenharmony_ci exit(1); 186f08c3bdfSopenharmony_ci } 187f08c3bdfSopenharmony_ci trestype = ident_ttype((argv++)[0]); 188f08c3bdfSopenharmony_ci tst_resm(trestype, "%s", *argv); 189f08c3bdfSopenharmony_ci} 190f08c3bdfSopenharmony_ci 191f08c3bdfSopenharmony_cistruct param_pair { 192f08c3bdfSopenharmony_ci char *cmd; 193f08c3bdfSopenharmony_ci int value; 194f08c3bdfSopenharmony_ci}; 195f08c3bdfSopenharmony_ci 196f08c3bdfSopenharmony_ciint apicmd_fs_has_free(int argc, char *argv[]) 197f08c3bdfSopenharmony_ci{ 198f08c3bdfSopenharmony_ci if (argc != 3) { 199f08c3bdfSopenharmony_ci fprintf(stderr, "Usage: tst_fs_has_free path required_bytes\n" 200f08c3bdfSopenharmony_ci "path: the pathname of the mounted filesystem\n" 201f08c3bdfSopenharmony_ci "required_bytes: the required free space" 202f08c3bdfSopenharmony_ci " (supports kB, MB and GB suffixes)\n"); 203f08c3bdfSopenharmony_ci exit(2); 204f08c3bdfSopenharmony_ci } 205f08c3bdfSopenharmony_ci 206f08c3bdfSopenharmony_ci char *endptr; 207f08c3bdfSopenharmony_ci unsigned int required_kib = strtoull(argv[1], &endptr, 0); 208f08c3bdfSopenharmony_ci unsigned int mul = TST_BYTES; 209f08c3bdfSopenharmony_ci 210f08c3bdfSopenharmony_ci if (*argv[1] == '\0') 211f08c3bdfSopenharmony_ci goto fs_has_free_err; 212f08c3bdfSopenharmony_ci 213f08c3bdfSopenharmony_ci if (*endptr != '\0') { 214f08c3bdfSopenharmony_ci if (!strcasecmp(endptr, "kB")) { 215f08c3bdfSopenharmony_ci mul = TST_KB; 216f08c3bdfSopenharmony_ci } else if (!strcasecmp(endptr, "MB")) { 217f08c3bdfSopenharmony_ci mul = TST_MB; 218f08c3bdfSopenharmony_ci } else if (!strcasecmp(endptr, "GB")) { 219f08c3bdfSopenharmony_ci mul = TST_GB; 220f08c3bdfSopenharmony_ci } else { 221f08c3bdfSopenharmony_ci goto fs_has_free_err; 222f08c3bdfSopenharmony_ci } 223f08c3bdfSopenharmony_ci } 224f08c3bdfSopenharmony_ci 225f08c3bdfSopenharmony_ci exit(!tst_fs_has_free(NULL, argv[0], required_kib, mul)); 226f08c3bdfSopenharmony_ci 227f08c3bdfSopenharmony_cifs_has_free_err: 228f08c3bdfSopenharmony_ci fprintf(stderr, "%s is not a valid size\n", argv[1]); 229f08c3bdfSopenharmony_ci exit(2); 230f08c3bdfSopenharmony_ci} 231f08c3bdfSopenharmony_ci 232f08c3bdfSopenharmony_ci/* 233f08c3bdfSopenharmony_ci * Function: main - entry point of this program 234f08c3bdfSopenharmony_ci * 235f08c3bdfSopenharmony_ci * Description: Parses the arguments to each command. Most commands have in 236f08c3bdfSopenharmony_ci * common atlest 2 arguments, type of test result, which is one of 237f08c3bdfSopenharmony_ci * TPASS, TFAIL, TBROK, TCONF, etc, and a message that describes 238f08c3bdfSopenharmony_ci * the result. Other arguments are a file, the contents of which 239f08c3bdfSopenharmony_ci * are printed after the type of test result and associated message 240f08c3bdfSopenharmony_ci * is printed, also a cleanup function that will be executed. 241f08c3bdfSopenharmony_ci * Currently this function name is ignored but MUST be provided 242f08c3bdfSopenharmony_ci * for compatability reasons. 243f08c3bdfSopenharmony_ci * 244f08c3bdfSopenharmony_ci * The different commands are actually a hard link to this program 245f08c3bdfSopenharmony_ci * the program invokes the appropriate function based on the 246f08c3bdfSopenharmony_ci * command name with which it was invoked. 247f08c3bdfSopenharmony_ci * 248f08c3bdfSopenharmony_ci * Set the values for TCID to the name of the test case. 249f08c3bdfSopenharmony_ci * set the value for TST_TOTAL for total number of tests this is 250f08c3bdfSopenharmony_ci * required in case one test breaks and all following tests also 251f08c3bdfSopenharmony_ci * should be reported as broken. 252f08c3bdfSopenharmony_ci * Set tst_count before every individual test. 253f08c3bdfSopenharmony_ci * 254f08c3bdfSopenharmony_ci * Exit: 0 on success 255f08c3bdfSopenharmony_ci * -1 on failure 256f08c3bdfSopenharmony_ci */ 257f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 258f08c3bdfSopenharmony_ci{ 259f08c3bdfSopenharmony_ci strcpy(cmd_name, SAFE_BASENAME(NULL, (argv++)[0])); 260f08c3bdfSopenharmony_ci 261f08c3bdfSopenharmony_ci TCID = getenv("TCID"); 262f08c3bdfSopenharmony_ci tst_total = getenv("TST_TOTAL"); 263f08c3bdfSopenharmony_ci tst_cntstr = getenv("TST_COUNT"); 264f08c3bdfSopenharmony_ci if (TCID == NULL || tst_total == NULL || tst_cntstr == NULL) { 265f08c3bdfSopenharmony_ci if(!strcmp(cmd_name, "tst_fs_has_free")) { 266f08c3bdfSopenharmony_ci fprintf(stderr, 267f08c3bdfSopenharmony_ci "\nSet variables TCID, TST_TOTAL, and TST_COUNT before each test:\n" 268f08c3bdfSopenharmony_ci "export TCID=<test name>\n" 269f08c3bdfSopenharmony_ci "export TST_TOTAL=<Total Number of Tests >\n" 270f08c3bdfSopenharmony_ci "export TST_COUNT=<Test case number>\n\n"); 271f08c3bdfSopenharmony_ci /* Make sure the user knows there's an error. */ 272f08c3bdfSopenharmony_ci abort(); 273f08c3bdfSopenharmony_ci } 274f08c3bdfSopenharmony_ci } else { 275f08c3bdfSopenharmony_ci TST_TOTAL = atoi(tst_total); 276f08c3bdfSopenharmony_ci tst_count = atoi(tst_cntstr); 277f08c3bdfSopenharmony_ci if (tst_count > 0) 278f08c3bdfSopenharmony_ci tst_count--; 279f08c3bdfSopenharmony_ci 280f08c3bdfSopenharmony_ci if (strcmp(TCID, " ") == 0) { 281f08c3bdfSopenharmony_ci fprintf(stderr, 282f08c3bdfSopenharmony_ci "Variable TCID not set, use: TCID=<test name>\n"); 283f08c3bdfSopenharmony_ci exit(1); 284f08c3bdfSopenharmony_ci } 285f08c3bdfSopenharmony_ci if (TST_TOTAL <= 0) { 286f08c3bdfSopenharmony_ci fprintf(stderr, 287f08c3bdfSopenharmony_ci "Variable TST_TOTAL is set to 0, must be " 288f08c3bdfSopenharmony_ci "greater than zero\n"); 289f08c3bdfSopenharmony_ci exit(1); 290f08c3bdfSopenharmony_ci } 291f08c3bdfSopenharmony_ci } 292f08c3bdfSopenharmony_ci 293f08c3bdfSopenharmony_ci if (strcmp(cmd_name, "tst_brk") == 0) { 294f08c3bdfSopenharmony_ci apicmd_brk(argc, argv); 295f08c3bdfSopenharmony_ci } else if (strcmp(cmd_name, "tst_res") == 0) { 296f08c3bdfSopenharmony_ci apicmd_res(argc, argv); 297f08c3bdfSopenharmony_ci } else if (strcmp(cmd_name, "tst_brkm") == 0) { 298f08c3bdfSopenharmony_ci apicmd_brkm(argc, argv); 299f08c3bdfSopenharmony_ci } else if (strcmp(cmd_name, "tst_resm") == 0) { 300f08c3bdfSopenharmony_ci apicmd_resm(argc, argv); 301f08c3bdfSopenharmony_ci } else if (strcmp(cmd_name, "tst_exit") == 0) { 302f08c3bdfSopenharmony_ci tst_exit(); 303f08c3bdfSopenharmony_ci } else if (strcmp(cmd_name, "tst_ncpus") == 0) { 304f08c3bdfSopenharmony_ci printf("%li\n", tst_ncpus()); 305f08c3bdfSopenharmony_ci } else if (strcmp(cmd_name, "tst_ncpus_conf") == 0) { 306f08c3bdfSopenharmony_ci printf("%li\n", tst_ncpus_conf()); 307f08c3bdfSopenharmony_ci } else if (strcmp(cmd_name, "tst_ncpus_max") == 0) { 308f08c3bdfSopenharmony_ci printf("%li\n", tst_ncpus_max()); 309f08c3bdfSopenharmony_ci } else if (strcmp(cmd_name, "tst_fs_has_free") == 0) { 310f08c3bdfSopenharmony_ci apicmd_fs_has_free(argc, argv); 311f08c3bdfSopenharmony_ci } 312f08c3bdfSopenharmony_ci 313f08c3bdfSopenharmony_ci exit(0); 314f08c3bdfSopenharmony_ci} 315