Home | History | Annotate | Download | only in tirpc_expertlevel_rpcb_rmtcall
      1 /*
      2 * Copyright (c) Bull S.A.  2007 All Rights Reserved.
      3 *
      4 * This program is free software; you can redistribute it and/or modify it
      5 * under the terms of version 2 of the GNU General Public License as
      6 * published by the Free Software Foundation.
      7 *
      8 * This program is distributed in the hope that it would be useful, but
      9 * WITHOUT ANY WARRANTY; without even the implied warranty of
     10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11 *
     12 * Further, this software is distributed without any warranty that it is
     13 * free of the rightful claim of any third person regarding infringement
     14 * or the like.  Any license provided herein, whether implied or
     15 * otherwise, applies only to this software file.  Patent licenses, if
     16 * any, provided herein do not apply to combinations of this program with
     17 * other software, or any other product whatsoever.
     18 *
     19 * You should have received a copy of the GNU General Public License along
     20 * with this program; if not, write the Free Software Foundation, Inc.,
     21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     22 *
     23 * History:
     24 * Created by: Cyril Lacabanne (Cyril.Lacabanne (at) bull.net)
     25 *
     26 */
     27 
     28 #include <stdio.h>
     29 #include <stdlib.h>
     30 #include <time.h>
     31 #include <pthread.h>
     32 #include <tirpc/netconfig.h>
     33 #include <tirpc/rpc/rpc.h>
     34 #include <tirpc/rpc/types.h>
     35 #include <tirpc/rpc/xdr.h>
     36 #include <tirpc/rpc/svc.h>
     37 #include <errno.h>
     38 
     39 //Standard define
     40 #define PROCNUM 1
     41 #define VERSNUM 1
     42 
     43 //Sys define
     44 #define ADDRBUFSIZE 100
     45 
     46 static int *thread_array_result;
     47 int run_mode;
     48 int progNum;
     49 char *hostname;
     50 int callNb;
     51 
     52 void *my_thread_process(void *arg)
     53 {
     54 	struct netconfig *nconf = NULL;
     55 	struct netbuf svcaddr;
     56 	char addrbuf[ADDRBUFSIZE];
     57 	enum clnt_stat cs;
     58 	int var_snd = 0;
     59 	int var_rec = -1;
     60 	struct timeval tv;
     61 	int i;
     62 
     63 	if (run_mode == 1) {
     64 		fprintf(stderr, "Thread %ld\n", (long)arg);
     65 	}
     66 
     67 	tv.tv_sec = 0;
     68 	tv.tv_usec = 100;
     69 
     70 	nconf = getnetconfigent("udp");
     71 	if (nconf == NULL) {
     72 		//syslog(LOG_ERR, "getnetconfigent for udp failed");
     73 		fprintf(stderr, "err nconf\n");
     74 		pthread_exit((void*)5l);
     75 	}
     76 
     77 	svcaddr.len = 0;
     78 	svcaddr.maxlen = ADDRBUFSIZE;
     79 	svcaddr.buf = addrbuf;
     80 
     81 	if (svcaddr.buf == NULL) {
     82 		/* if malloc() failed, print error messages and exit */
     83 		pthread_exit((void*)5l);
     84 	}
     85 	//printf("svcaddr reserved (%s)\n", argc[1]);
     86 
     87 	if (!rpcb_getaddr(progNum + atoi(arg), VERSNUM, nconf,
     88 			  &svcaddr, hostname)) {
     89 		fprintf(stderr, "rpcb_getaddr failed!!\n");
     90 		pthread_exit((void*)5l);
     91 	}
     92 
     93 	for (i = 0; i < callNb; i++) {
     94 		cs = rpcb_rmtcall(nconf, hostname, progNum + atoi(arg), VERSNUM,
     95 				  PROCNUM, (xdrproc_t) xdr_int,
     96 				  (char *)&var_snd, (xdrproc_t) xdr_int,
     97 				  (char *)&var_rec, tv, &svcaddr);
     98 
     99 		thread_array_result[atoi(arg)] += (cs == RPC_SUCCESS);
    100 	}
    101 
    102 	pthread_exit(0);
    103 }
    104 
    105 int main(int argn, char *argc[])
    106 {
    107 	//Program parameters : argc[1] : HostName or Host IP
    108 	//                                         argc[2] : Server Program Number
    109 	//                                         argc[3] : Number of threads
    110 	//                                         argc[4] : Number of calls per thread
    111 	//                                         other arguments depend on test case
    112 
    113 	//run_mode can switch into stand alone program or program launch by shell script
    114 	//1 : stand alone, debug mode, more screen information
    115 	//0 : launch by shell script as test case, only one printf -> result status
    116 	run_mode = 0;
    117 	int test_status = 1;	//Default test result set to FAILED
    118 	int threadNb = atoi(argc[3]);
    119 	long i;
    120 	pthread_t *pThreadArray;
    121 	void *ret;
    122 
    123 	progNum = atoi(argc[2]);
    124 	hostname = argc[1];
    125 	callNb = atoi(argc[4]);
    126 
    127 	if (run_mode == 1) {
    128 		printf("Server #%d\n", progNum);
    129 		printf("Thread to create %d\n", threadNb);
    130 	}
    131 	//Initialization : create threads results array, init elements to 0
    132 	//Each thread will put function result (pas/fail) into array
    133 	thread_array_result = malloc(threadNb * sizeof(int));
    134 	memset(&thread_array_result[0], 0, threadNb * sizeof(int));
    135 
    136 	//Create all threads
    137 	//Run all threads
    138 	pThreadArray = malloc(threadNb * sizeof(pthread_t));
    139 	for (i = 0; i < threadNb; i++) {
    140 		if (run_mode == 1)
    141 			fprintf(stderr, "Try to create thread %ld\n", i);
    142 		if (pthread_create(&pThreadArray[i], NULL, my_thread_process, (void*)i)
    143 		    < 0) {
    144 			fprintf(stderr, "pthread_create error for thread 1\n");
    145 			exit(1);
    146 		}
    147 	}
    148 
    149 	//Clean threads
    150 	for (i = 0; i < threadNb; i++) {
    151 		(void)pthread_join(pThreadArray[i], &ret);
    152 	}
    153 
    154 	//Check if all threads results are ok
    155 	test_status = 0;
    156 	for (i = 0; i < threadNb; i++) {
    157 		if (thread_array_result[i] != callNb) {
    158 			test_status = 1;
    159 			break;
    160 		}
    161 	}
    162 
    163 	if (run_mode == 1) {
    164 		for (i = 0; i < threadNb; i++) {
    165 			fprintf(stderr, "Result[%ld]=%d\n", i,
    166 				thread_array_result[i]);
    167 		}
    168 	}
    169 	//This last printf gives the result status to the tests suite
    170 	//normally should be 0: test has passed or 1: test has failed
    171 	printf("%d\n", test_status);
    172 
    173 	return test_status;
    174 }
    175