xref: /aosp_15_r20/external/ltp/lib/parse_opts.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  *    AUTHOR		: William Roske/Richard Logan
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * Further, this software is distributed without any warranty that it is
14  * free of the rightful claim of any third person regarding infringement
15  * or the like.  Any license provided herein, whether implied or
16  * otherwise, applies only to this software file.  Patent licenses, if
17  * any, provided herein do not apply to combinations of this program with
18  * other software, or any other product whatsoever.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  *
24  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25  * Mountain View, CA  94043, or:
26  *
27  * http://www.sgi.com
28  *
29  * For further information regarding this notice, see:
30  *
31  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
32  */
33 
34 #include "config.h"
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/param.h>
39 #include <sys/signal.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 #include <time.h>
43 #include <stdint.h>
44 
45 #include "test.h"
46 #include "ltp_priv.h"
47 #include "usctest.h"
48 #include "tst_clocks.h"
49 
50 #ifndef UNIT_TEST
51 #define UNIT_TEST	0
52 #endif
53 
54 /* Define flags and args for standard options */
55 static int STD_INFINITE = 0;	/* flag indciating to loop forever */
56 int STD_LOOP_COUNT = 1;		/* number of iterations */
57 
58 static float STD_LOOP_DURATION = 0.0;	/* duration value in fractional seconds */
59 
60 static char **STD_opt_arr = NULL;	/* array of option strings */
61 static int STD_argind = 1;	/* argv index to next argv element */
62 				/* (first argument) */
63 				/* To getopt users, it is like optind */
64 
65 /*
66  * The following variables are to support system testing additions.
67  */
68 static int STD_TP_barrier = 0;	/* flag to do barrier in TEST_PAUSE */
69 				/* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
70 static int STD_LP_barrier = 0;	/* flag to do barrier in TEST_LOOPING */
71 				/* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
72 static int STD_TP_shmem_sz = 0;	/* shmalloc this many words per pe in TEST_PAUSE */
73 static int STD_LD_shmem = 0;	/* flag to do shmem_puts and shmem_gets during delay */
74 static int STD_LP_shmem = 0;	/* flag to do shmem_puts and gets during TEST_LOOPING */
75 static int STD_LD_recfun = 0;	/* do recressive function calls in loop delay */
76 static int STD_LP_recfun = 0;	/* do recressive function calls in TEST_LOOPING */
77 static int STD_TP_sbrk = 0;	/* do sbrk in TEST_PAUSE */
78 static int STD_LP_sbrk = 0;	/* do sbrk in TEST_LOOPING */
79 static char *STD_start_break = 0;	/* original sbrk size */
80 static int Debug = 0;
81 
82 static struct std_option_t {
83 	char *optstr;
84 	char *help;
85 	char *flag;
86 	char **arg;
87 } std_options[] = {
88 	{"h", "  -h      Show this help screen\n", NULL, NULL},
89 	{"i:", "  -i n    Execute test n times\n", NULL, NULL},
90 	{"I:", "  -I x    Execute test for x seconds\n", NULL, NULL},
91 	{NULL, NULL, NULL, NULL}
92 };
93 
94 /*
95  * Structure for usc_recressive_func argument
96  */
97 struct usc_bigstack_t {
98 	char space[4096];
99 };
100 
101 static struct usc_bigstack_t *STD_bigstack = NULL;
102 
103 /* define the string length for Mesg and Mesg2 strings */
104 #define STRLEN 2048
105 
106 static char Mesg2[STRLEN];	/* holds possible return string */
107 static void usc_recressive_func();
108 
109 /*
110  * Define bits for options that might have env variable default
111  */
112 #define OPT_iteration		01
113 #define OPT_duration		04
114 #define OPT_delay		010
115 
print_help(void (* user_help)(void))116 static void print_help(void (*user_help)(void))
117 {
118 	int i;
119 
120 	for (i = 0; std_options[i].optstr; ++i) {
121 		if (std_options[i].help)
122 			printf("%s", std_options[i].help);
123 	}
124 
125 	if (user_help)
126 		user_help();
127 }
128 
129 /**********************************************************************
130  * parse_opts:
131  **********************************************************************/
parse_opts(int ac,char ** av,const option_t * user_optarr,void (* uhf)(void))132 const char *parse_opts(int ac, char **av, const option_t * user_optarr,
133                        void (*uhf)(void))
134 {
135 	int found;		/* flag to indicate that an option specified was */
136 	/* found in the user's list */
137 	int k;			/* scratch integer for returns and short time usage */
138 	float ftmp;		/* tmp float for parsing env variables */
139 	char *ptr;		/* used in getting env variables */
140 	int options = 0;	/* no options specified */
141 	int optstrlen, i;
142 	char *optionstr;
143 	int opt;
144 
145 	/*
146 	 * If not the first time this function is called, release the old STD_opt_arr
147 	 * vector.
148 	 */
149 	if (STD_opt_arr != NULL) {
150 		free(STD_opt_arr);
151 		STD_opt_arr = NULL;
152 	}
153 	/* Calculate how much space we need for the option string */
154 	optstrlen = 0;
155 	for (i = 0; std_options[i].optstr; ++i)
156 		optstrlen += strlen(std_options[i].optstr);
157 	if (user_optarr)
158 		for (i = 0; user_optarr[i].option; ++i) {
159 			if (strlen(user_optarr[i].option) > 2)
160 				return
161 				    "parse_opts: ERROR - Only short options are allowed";
162 			optstrlen += strlen(user_optarr[i].option);
163 		}
164 	optstrlen += 1;
165 
166 	/* Create the option string for getopt */
167 	optionstr = malloc(optstrlen);
168 	if (!optionstr)
169 		return
170 		    "parse_opts: ERROR - Could not allocate memory for optionstr";
171 
172 	optionstr[0] = '\0';
173 
174 	for (i = 0; std_options[i].optstr; ++i)
175 		strcat(optionstr, std_options[i].optstr);
176 	if (user_optarr)
177 		for (i = 0; user_optarr[i].option; ++i)
178 			/* only add the option if it wasn't there already */
179 			if (strchr(optionstr, user_optarr[i].option[0]) == NULL)
180 				strcat(optionstr, user_optarr[i].option);
181 
182 	/*
183 	 *  Loop through av parsing options.
184 	 */
185 	while ((opt = getopt(ac, av, optionstr)) > 0) {
186 
187 		STD_argind = optind;
188 
189 		switch (opt) {
190 		case '?':	/* Unknown option */
191 			return "Unknown option";
192 			break;
193 		case ':':	/* Missing Arg */
194 			return "Missing argument";
195 			break;
196 		case 'i':	/* Iterations */
197 			options |= OPT_iteration;
198 			STD_LOOP_COUNT = atoi(optarg);
199 			if (STD_LOOP_COUNT == 0)
200 				STD_INFINITE = 1;
201 			break;
202 		case 'I':	/* Time duration */
203 			options |= OPT_duration;
204 			STD_LOOP_DURATION = atof(optarg);
205 			if (STD_LOOP_DURATION == 0.0)
206 				STD_INFINITE = 1;
207 			break;
208 		case 'h':	/* Help */
209 			print_help(uhf);
210 			exit(0);
211 			break;
212 		default:
213 
214 			/* Check all the user specified options */
215 			found = 0;
216 			for (i = 0; user_optarr[i].option; ++i) {
217 
218 				if (opt == user_optarr[i].option[0]) {
219 					/* Yup, This is a user option, set the flag and look for argument */
220 					if (user_optarr[i].flag) {
221 						*user_optarr[i].flag = 1;
222 					}
223 					found++;
224 
225 					/* save the argument at the user's location */
226 					if (user_optarr[i].
227 					    option[strlen(user_optarr[i].option)
228 						   - 1] == ':') {
229 						*user_optarr[i].arg = optarg;
230 					}
231 					break;	/* option found - break out of the for loop */
232 				}
233 			}
234 			/* This condition "should never happen".  SO CHECK FOR IT!!!! */
235 			if (!found) {
236 				sprintf(Mesg2,
237 					"parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL "
238 					"ERROR", opt);
239 				return (Mesg2);
240 			}
241 		}
242 
243 	}
244 	free(optionstr);
245 
246 	STD_argind = optind;
247 
248 	/*
249 	 * Turn on debug
250 	 */
251 	if (getenv("USC_DEBUG") != NULL) {
252 		Debug = 1;
253 		printf("env USC_DEBUG is defined, turning on debug\n");
254 	}
255 	if (getenv("USC_VERBOSE") != NULL) {
256 		Debug = 1;
257 		printf("env USC_VERBOSE is defined, turning on debug\n");
258 	}
259 
260 	/*
261 	 * If the USC_ITERATION_ENV environmental variable is set to
262 	 * a number, use that number as iteration count (same as -c option).
263 	 * The -c option with arg will be used even if this env var is set.
264 	 */
265 	if (!(options & OPT_iteration)
266 	    && (ptr = getenv(USC_ITERATION_ENV)) != NULL) {
267 		if (sscanf(ptr, "%i", &k) == 1) {
268 			if (k == 0) {	/* if arg is 0, set infinite loop flag */
269 				STD_INFINITE = 1;
270 				if (Debug)
271 					printf
272 					    ("Using env %s, set STD_INFINITE to 1\n",
273 					     USC_ITERATION_ENV);
274 			} else {	/* else, set the loop count to the arguement */
275 				STD_LOOP_COUNT = k;
276 				if (Debug)
277 					printf
278 					    ("Using env %s, set STD_LOOP_COUNT to %d\n",
279 					     USC_ITERATION_ENV, k);
280 			}
281 		}
282 	}
283 
284 	/*
285 	 * If the USC_LOOP_WALLTIME environmental variable is set,
286 	 * use that number as duration (same as -I option).
287 	 * The -I option with arg will be used even if this env var is set.
288 	 */
289 
290 	if (!(options & OPT_duration) &&
291 	    (ptr = getenv(USC_LOOP_WALLTIME)) != NULL) {
292 		if (sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0) {
293 			STD_LOOP_DURATION = ftmp;
294 			if (Debug)
295 				printf
296 				    ("Using env %s, set STD_LOOP_DURATION to %f\n",
297 				     USC_LOOP_WALLTIME, ftmp);
298 			if (STD_LOOP_DURATION == 0.0) {	/* if arg is 0, set infinite loop flag */
299 				STD_INFINITE = 1;
300 				if (Debug)
301 					printf
302 					    ("Using env %s, set STD_INFINITE to 1\n",
303 					     USC_LOOP_WALLTIME);
304 			}
305 		}
306 	}
307 	if (!(options & OPT_duration) && (ptr = getenv("USC_DURATION")) != NULL) {
308 		if (sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0) {
309 			STD_LOOP_DURATION = ftmp;
310 			if (Debug)
311 				printf
312 				    ("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n",
313 				     ftmp);
314 			if (STD_LOOP_DURATION == 0.0) {	/* if arg is 0, set infinite loop flag */
315 				STD_INFINITE = 1;
316 				if (Debug)
317 					printf
318 					    ("Using env USC_DURATION, set STD_INFINITE to 1\n");
319 			}
320 		}
321 	}
322 
323 	/*
324 	 * The following are special system testing envs to turn on special
325 	 * hooks in the code.
326 	 */
327 	if ((ptr = getenv("USC_TP_BARRIER")) != NULL) {
328 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0)
329 			STD_TP_barrier = k;
330 		else
331 			STD_TP_barrier = 1;
332 		if (Debug)
333 			printf
334 			    ("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n",
335 			     STD_TP_barrier);
336 	}
337 
338 	if ((ptr = getenv("USC_LP_BARRIER")) != NULL) {
339 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0)
340 			STD_LP_barrier = k;
341 		else
342 			STD_LP_barrier = 1;
343 		if (Debug)
344 			printf
345 			    ("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n",
346 			     STD_LP_barrier);
347 	}
348 
349 	if ((ptr = getenv("USC_TP_SHMEM")) != NULL) {
350 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
351 			STD_TP_shmem_sz = k;
352 			if (Debug)
353 				printf
354 				    ("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n",
355 				     STD_TP_shmem_sz);
356 		}
357 	}
358 
359 	if ((ptr = getenv("USC_LP_SHMEM")) != NULL) {
360 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
361 			STD_LP_shmem = k;
362 			if (Debug)
363 				printf
364 				    ("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n",
365 				     STD_LP_shmem);
366 		}
367 	}
368 
369 	if ((ptr = getenv("USC_LD_SHMEM")) != NULL) {
370 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
371 			STD_LD_shmem = k;
372 			if (Debug)
373 				printf
374 				    ("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n",
375 				     STD_LD_shmem);
376 		}
377 	}
378 
379 	if ((ptr = getenv("USC_TP_SBRK")) != NULL) {
380 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
381 			STD_TP_sbrk = k;
382 			if (Debug)
383 				printf
384 				    ("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n",
385 				     STD_TP_sbrk);
386 		}
387 	}
388 
389 	if ((ptr = getenv("USC_LP_SBRK")) != NULL) {
390 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
391 			STD_LP_sbrk = k;
392 			if (Debug)
393 				printf
394 				    ("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n",
395 				     STD_LP_sbrk);
396 		}
397 	}
398 
399 	if ((ptr = getenv("USC_LP_RECFUN")) != NULL) {
400 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
401 			STD_LP_recfun = k;
402 			if (STD_bigstack != NULL)
403 				STD_bigstack =
404 				    malloc(sizeof(struct usc_bigstack_t));
405 			if (Debug)
406 				printf
407 				    ("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n",
408 				     STD_LP_recfun);
409 		}
410 	}
411 
412 	if ((ptr = getenv("USC_LD_RECFUN")) != NULL) {
413 		if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
414 			STD_LD_recfun = k;
415 			if (STD_bigstack != NULL)
416 				STD_bigstack =
417 				    malloc(sizeof(struct usc_bigstack_t));
418 			if (Debug)
419 				printf
420 				    ("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n",
421 				     STD_LD_recfun);
422 		}
423 	}
424 #if UNIT_TEST
425 	printf("The following variables after option and env parsing:\n");
426 	printf("STD_LOOP_DURATION   = %f\n", STD_LOOP_DURATION);
427 	printf("STD_LOOP_COUNT      = %d\n", STD_LOOP_COUNT);
428 	printf("STD_INFINITE        = %d\n", STD_INFINITE);
429 #endif
430 
431 	return NULL;
432 }
433 
434 /***********************************************************************
435  * This function will do desired end of global setup test
436  * hooks.
437  ***********************************************************************/
usc_global_setup_hook(void)438 int usc_global_setup_hook(void)
439 {
440 	if (STD_TP_sbrk || STD_LP_sbrk)
441 		STD_start_break = sbrk(0);	/* get original sbreak size */
442 
443 	if (STD_TP_sbrk) {
444 		sbrk(STD_TP_sbrk);
445 		if (Debug)
446 			printf("after sbrk(%d)\n", STD_TP_sbrk);
447 	}
448 	return 0;
449 }
450 
451 #define USECS_PER_SEC	1000000	/* microseconds per second */
452 
get_current_time(void)453 static uint64_t get_current_time(void)
454 {
455 	struct timespec ts;
456 
457 	tst_clock_gettime(CLOCK_MONOTONIC, &ts);
458 
459 	return (((uint64_t) ts.tv_sec) * USECS_PER_SEC) + ts.tv_nsec / 1000;
460 }
461 
462 /***********************************************************************
463  *
464  * This function will determine if test should continue iterating
465  * If the STD_INFINITE flag is set, return 1.
466  * If the STD_LOOP_COUNT variable is set, compare it against
467  * the counter.
468  * If the STD_LOOP_DURATION variable is set, compare current time against
469  * calculated stop_time.
470  * This function will return 1 until all desired looping methods
471  * have been met.
472  *
473  * counter integer is supplied by the user program.
474  ***********************************************************************/
usc_test_looping(int counter)475 int usc_test_looping(int counter)
476 {
477 	static int first_time = 1;
478 	static uint64_t stop_time = 0;
479 	int keepgoing = 0;
480 
481 	/*
482 	 * If this is the first iteration and we are looping for
483 	 * duration of STD_LOOP_DURATION seconds (fractional) or
484 	 * doing loop delays, get the clocks per second.
485 	 */
486 	if (first_time) {
487 		first_time = 0;
488 
489 		/*
490 		 * If looping for duration, calculate stop time in
491 		 * clocks.
492 		 */
493 		if (STD_LOOP_DURATION) {
494 			stop_time =
495 			    (uint64_t) (USECS_PER_SEC * STD_LOOP_DURATION)
496 			    + get_current_time();
497 		}
498 	}
499 
500 	if (STD_INFINITE)
501 		keepgoing++;
502 
503 	if (STD_LOOP_COUNT && counter < STD_LOOP_COUNT)
504 		keepgoing++;
505 
506 	if (STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time)
507 		keepgoing++;
508 
509 	if (keepgoing == 0)
510 		return 0;
511 
512 	/*
513 	 * The following code allows special system testing hooks.
514 	 */
515 
516 	if (STD_LP_recfun) {
517 		if (Debug)
518 			printf
519 			    ("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
520 			     STD_LP_recfun);
521 		usc_recressive_func(0, STD_LP_recfun, *STD_bigstack);
522 	}
523 
524 	if (STD_LP_sbrk) {
525 		if (Debug)
526 			printf("about to do sbrk(%d)\n", STD_LP_sbrk);
527 		sbrk(STD_LP_sbrk);
528 	}
529 
530 	if (keepgoing)
531 		return 1;
532 	else
533 		return 0;
534 }
535 
536 /*
537  * This function recressively calls itself max times.
538  */
usc_recressive_func(int cnt,int max,struct usc_bigstack_t bstack)539 static void usc_recressive_func(int cnt, int max, struct usc_bigstack_t bstack)
540 {
541 	if (cnt < max)
542 		usc_recressive_func(cnt + 1, max, bstack);
543 
544 }
545 
546 #if UNIT_TEST
547 
548 /******************************************************************************
549  * UNIT TEST CODE
550  * UNIT TEST CODE
551  *
552  * this following code is provide so that unit testing can
553  * be done fairly easily.
554  ******************************************************************************/
555 
556 int Help = 0;
557 int Help2 = 0;
558 char *ptr;
559 
560 long TEST_RETURN;
561 int TEST_ERRNO;
562 
563 /* for test specific parse_opts options */
564 option_t Options[] = {
565 	{"help", &Help2, NULL},	/* -help option */
566 	{"h", &Help, NULL},	/* -h option */
567 
568 #if INVALID_TEST_CASES
569 	{"missingflag", NULL, &ptr},	/* error */
570 	{"missingarg:", &Help, NULL},	/* error */
571 #endif /* INVALID_TEST_CASES */
572 
573 	{NULL, NULL, NULL}
574 };
575 
main(int argc,char ** argv)576 int main(int argc, char **argv)
577 {
578 	int lc;
579 	char *msg;
580 	struct timeval t;
581 	int cnt;
582 
583 	if ((msg = parse_opts(argc, argv, Options, NULL)) != NULL) {
584 		printf("ERROR: %s\n", msg);
585 		exit(1);
586 	}
587 
588 	TEST_PAUSE;
589 
590 	for (lc = 0; TEST_LOOPING(lc); lc++) {
591 
592 		TEST(gettimeofday(&t, NULL));
593 		printf("iter=%d: sec:%d, usec:%6.6d %s", lc + 1, t.tv_sec,
594 		       t.tv_usec, ctime(&t.tv_sec));
595 	}
596 
597 	TEST_CLEANUP;
598 
599 	exit(0);
600 }
601 
602 #endif /* UNIT_TEST */
603