Home | History | Annotate | Download | only in lib
      1 /*
      2  * Copyright (c) 2016 Linux Test Project
      3  *
      4  * This program is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU General Public License as
      6  * published by the Free Software Foundation; either version 2 of
      7  * the License, or (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it would be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program; if not, write the Free Software Foundation,
     16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     17  */
     18 
     19 #include <errno.h>
     20 #include <limits.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 #define TST_NO_DEFAULT_MAIN
     24 #include "tst_test.h"
     25 
     26 static void print_help(void)
     27 {
     28 	printf("Usage: tst_checkpoint wait TIMEOUT ID\n");
     29 	printf("   or: tst_checkpoint wake TIMEOUT ID NR_WAKE\n");
     30 	printf("       TIMEOUT - timeout in ms\n");
     31 	printf("       ID - checkpoint id\n");
     32 	printf("       NR_WAKE - number of processes to wake up\n");
     33 }
     34 
     35 static int get_val(const char *name, const char *arg, unsigned int *val)
     36 {
     37 	unsigned long temp;
     38 	char *e;
     39 
     40 	errno = 0;
     41 	temp = strtoul(arg, &e, 10);
     42 	if (errno || (e == arg) || (temp > UINT_MAX)) {
     43 		fprintf(stderr, "ERROR: Invalid %s '%s'\n",
     44 			name, arg);
     45 		return -1;
     46 	}
     47 
     48 	*val = temp;
     49 
     50 	return 0;
     51 }
     52 
     53 int main(int argc, char *argv[])
     54 {
     55 	unsigned int id, timeout, nr_wake;
     56 	int ret;
     57 	int type;
     58 
     59 	if (argc < 3)
     60 		goto help;
     61 
     62 	if (!strcmp(argv[1], "wait")) {
     63 		type = 0;
     64 
     65 		if (argc != 4)
     66 			goto help;
     67 	} else if (!strcmp(argv[1], "wake")) {
     68 		type = 1;
     69 
     70 		if (argc != 5)
     71 			goto help;
     72 
     73 		if (get_val("NR_WAKE", argv[4], &nr_wake))
     74 			goto help;
     75 	} else {
     76 		fprintf(stderr, "ERROR: Invalid COMMAND '%s'\n",
     77 			argv[1]);
     78 		goto help;
     79 	}
     80 
     81 	if (get_val("TIMEOUT", argv[2], &timeout)
     82 	    || get_val("ID", argv[3], &id)) {
     83 		goto help;
     84 	}
     85 
     86 	tst_reinit();
     87 
     88 	if (type)
     89 		ret = tst_checkpoint_wake(id, nr_wake, timeout);
     90 	else
     91 		ret = tst_checkpoint_wait(id, timeout);
     92 
     93 	if (ret)
     94 		return 1;
     95 	else
     96 		return 0;
     97 
     98 help:
     99 	print_help();
    100 	return 1;
    101 }
    102