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 CLIENT *client = NULL; 55 struct netconfig *nconf = NULL; 56 struct netbuf svcaddr; 57 char addrbuf[ADDRBUFSIZE]; 58 enum clnt_stat cs; 59 int var_snd = 10; 60 int var_rec = -1; 61 struct timeval tv; 62 int i; 63 64 if (run_mode == 1) { 65 fprintf(stderr, "Thread %ld\n", (long)arg); 66 } 67 68 tv.tv_sec = 0; 69 tv.tv_usec = 100; 70 71 nconf = getnetconfigent("udp"); 72 if (nconf == NULL) { 73 //syslog(LOG_ERR, "getnetconfigent for udp failed"); 74 printf("err nconf\n"); 75 pthread_exit((void*)1l); 76 } 77 78 svcaddr.len = 0; 79 svcaddr.maxlen = ADDRBUFSIZE; 80 svcaddr.buf = addrbuf; 81 82 if (svcaddr.buf == NULL) { 83 pthread_exit((void*)1l); 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*)1l); 91 } 92 //printf("svc get\n"); 93 94 client = clnt_dg_create(RPC_ANYFD, &svcaddr, 95 progNum + atoi(arg), VERSNUM, 1024, 1024); 96 97 if (client == NULL) { 98 clnt_pcreateerror("ERR"); 99 pthread_exit((void*)1l); 100 } 101 102 for (i = 0; i < callNb; i++) { 103 cs = clnt_call(client, PROCNUM, 104 (xdrproc_t) xdr_int, (char *)&var_snd, 105 (xdrproc_t) xdr_int, (char *)&var_rec, tv); 106 107 thread_array_result[atoi(arg)] += (cs == RPC_SUCCESS); 108 } 109 110 pthread_exit(0); 111 } 112 113 int main(int argn, char *argc[]) 114 { 115 //Program parameters : argc[1] : HostName or Host IP 116 // argc[2] : Server Program Number 117 // argc[3] : Number of threads 118 // argc[4] : Number of calls per thread 119 // other arguments depend on test case 120 121 //run_mode can switch into stand alone program or program launch by shell script 122 //1 : stand alone, debug mode, more screen information 123 //0 : launch by shell script as test case, only one printf -> result status 124 run_mode = 0; 125 int test_status = 1; //Default test result set to FAILED 126 int threadNb = atoi(argc[3]); 127 long i; 128 pthread_t *pThreadArray; 129 void *ret; 130 131 progNum = atoi(argc[2]); 132 hostname = argc[1]; 133 callNb = atoi(argc[4]); 134 135 if (run_mode == 1) { 136 printf("Server #%d\n", progNum); 137 printf("Thread to create %d\n", threadNb); 138 } 139 //Initialization : create threads results array, init elements to 0 140 //Each thread will put function result (pas/fail) into array 141 thread_array_result = malloc(threadNb * sizeof(int)); 142 memset(&thread_array_result[0], 0, threadNb * sizeof(int)); 143 144 //Create all threads 145 //Run all threads 146 pThreadArray = malloc(threadNb * sizeof(pthread_t)); 147 for (i = 0; i < threadNb; i++) { 148 if (run_mode == 1) 149 fprintf(stderr, "Try to create thread %ld\n", i); 150 if (pthread_create(&pThreadArray[i], NULL, my_thread_process, (void*)i) 151 < 0) { 152 fprintf(stderr, "pthread_create error for thread 1\n"); 153 exit(1); 154 } 155 } 156 157 //Clean threads 158 for (i = 0; i < threadNb; i++) { 159 (void)pthread_join(pThreadArray[i], &ret); 160 } 161 162 //Check if all threads results are ok 163 test_status = 0; 164 for (i = 0; i < threadNb; i++) { 165 if (thread_array_result[i] != callNb) { 166 test_status = 1; 167 break; 168 } 169 } 170 171 if (run_mode == 1) { 172 for (i = 0; i < threadNb; i++) { 173 fprintf(stderr, "Result[%ld]=%d\n", i, 174 thread_array_result[i]); 175 } 176 } 177 //This last printf gives the result status to the tests suite 178 //normally should be 0: test has passed or 1: test has failed 179 printf("%d\n", test_status); 180 181 return test_status; 182 } 183