~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/comm/test_cl_commlib.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*___INFO__MARK_BEGIN__*/
 
3
/*************************************************************************
 
4
 * 
 
5
 *  The Contents of this file are made available subject to the terms of
 
6
 *  the Sun Industry Standards Source License Version 1.2
 
7
 * 
 
8
 *  Sun Microsystems Inc., March, 2001
 
9
 * 
 
10
 * 
 
11
 *  Sun Industry Standards Source License Version 1.2
 
12
 *  =================================================
 
13
 *  The contents of this file are subject to the Sun Industry Standards
 
14
 *  Source License Version 1.2 (the "License"); You may not use this file
 
15
 *  except in compliance with the License. You may obtain a copy of the
 
16
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
17
 * 
 
18
 *  Software provided under this License is provided on an "AS IS" basis,
 
19
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
20
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
21
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
22
 *  See the License for the specific provisions governing your rights and
 
23
 *  obligations concerning the Software.
 
24
 * 
 
25
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
26
 * 
 
27
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
28
 * 
 
29
 *   All Rights Reserved.
 
30
 * 
 
31
 ************************************************************************/
 
32
/*___INFO__MARK_END__*/
 
33
 
 
34
#include <stdio.h>
 
35
#include <stdlib.h>
 
36
#include <string.h>
 
37
#include <unistd.h>
 
38
#include <signal.h>
 
39
#include <sys/time.h>
 
40
#include <pwd.h>
 
41
 
 
42
#include "cl_commlib.h"
 
43
#include "cl_log_list.h"
 
44
#include "cl_endpoint_list.h"
 
45
#include "uti/sge_profiling.h"
 
46
#define CL_DO_SLOW 0
 
47
 
 
48
void sighandler_server(int sig);
 
49
static int pipe_signal = 0;
 
50
static int hup_signal = 0;
 
51
static int do_shutdown = 0;
 
52
static int received_qping = 0;
 
53
 
 
54
void sighandler_server(
 
55
int sig 
 
56
) {
 
57
   if (sig == SIGPIPE) {
 
58
      pipe_signal = 1;
 
59
      return;
 
60
   }
 
61
 
 
62
   if (sig == SIGHUP) {
 
63
      hup_signal = 1;
 
64
      return;
 
65
   }
 
66
 
 
67
   /* shutdown all sockets */
 
68
   cl_com_ignore_timeouts(CL_TRUE);
 
69
 
 
70
   do_shutdown = 1;
 
71
}
 
72
 
 
73
const char* my_application_tag_name(unsigned long tag) {
 
74
   if (tag > 0) {
 
75
      return "TAG > 0";
 
76
   }
 
77
   return "DEFAULT_APPLICATION_TAG";
 
78
}
 
79
 
 
80
unsigned long my_application_status(char** info_message) {
 
81
   if ( info_message != NULL ) {
 
82
      (*info_message) = strdup("not specified (state 1)");
 
83
   }
 
84
#if 0
 
85
   received_qping++;
 
86
#endif
 
87
   if (received_qping > 10) {
 
88
      do_shutdown = 1;
 
89
   }
 
90
   return (unsigned long)1;
 
91
}
 
92
 
 
93
#ifdef __CL_FUNCTION__
 
94
#undef __CL_FUNCTION__
 
95
#endif
 
96
#define __CL_FUNCTION__ "my_ssl_verify_func()"
 
97
static cl_bool_t my_ssl_verify_func(cl_ssl_verify_mode_t mode, cl_bool_t service_mode, const char* value) {
 
98
   char* user_name = NULL;
 
99
   struct passwd *paswd = NULL;
 
100
 
 
101
   paswd = getpwuid(getuid());
 
102
   if (paswd) {
 
103
      user_name = paswd->pw_name;
 
104
   }
 
105
   if (user_name == NULL) {
 
106
      user_name = "unexpected user name";
 
107
   }
 
108
   if (service_mode == CL_TRUE) {
 
109
      CL_LOG(CL_LOG_WARNING,"running in service mode");
 
110
      switch(mode) {
 
111
         case CL_SSL_PEER_NAME: {
 
112
            CL_LOG(CL_LOG_WARNING,"CL_SSL_PEER_NAME");
 
113
            if (strcmp(value,"SGE admin user") != 0) {
 
114
               CL_LOG(CL_LOG_WARNING,"CL_SSL_PEER_NAME is not \"SGE admin user\"");
 
115
               return CL_FALSE;
 
116
            }
 
117
            break;
 
118
         }
 
119
         case CL_SSL_USER_NAME: {
 
120
            CL_LOG(CL_LOG_WARNING,"CL_SSL_USER_NAME");
 
121
            if (strcmp(value,user_name) != 0) {
 
122
               CL_LOG_STR(CL_LOG_WARNING,"CL_SSL_USER_NAME is not", user_name);
 
123
               return CL_FALSE;
 
124
            }
 
125
            break;
 
126
         }
 
127
      }
 
128
   } else {
 
129
      CL_LOG(CL_LOG_WARNING,"running in client mode");
 
130
      switch(mode) {
 
131
         case CL_SSL_PEER_NAME: {
 
132
            CL_LOG(CL_LOG_WARNING,"CL_SSL_PEER_NAME");
 
133
            if (strcmp(value,"SGE admin user") != 0) {
 
134
               CL_LOG(CL_LOG_WARNING,"CL_SSL_PEER_NAME is not \"SGE Daemon\"");
 
135
               return CL_FALSE;
 
136
            }
 
137
            break;
 
138
         }
 
139
         case CL_SSL_USER_NAME: {
 
140
            CL_LOG(CL_LOG_WARNING,"CL_SSL_USER_NAME");
 
141
            if (strcmp(value,user_name) != 0) {
 
142
               CL_LOG_STR(CL_LOG_WARNING,"CL_SSL_USER_NAME is not", user_name);
 
143
               return CL_FALSE;
 
144
            }
 
145
            break;
 
146
         }
 
147
      }
 
148
   }
 
149
   return CL_TRUE;
 
150
}
 
151
 
 
152
 
 
153
#ifdef __CL_FUNCTION__
 
154
#undef __CL_FUNCTION__
 
155
#endif
 
156
#define __CL_FUNCTION__ "main()"
 
157
extern int main(int argc, char** argv)
 
158
{
 
159
  struct sigaction sa;
 
160
  cl_ssl_setup_t ssl_config;
 
161
  static int runs = 100;
 
162
 
 
163
 
 
164
  int handle_port = 0;
 
165
  cl_com_handle_t* handle = NULL; 
 
166
  cl_com_message_t* message = NULL;
 
167
  cl_com_endpoint_t* sender = NULL;
 
168
  int i;
 
169
  unsigned long max_connections;
 
170
  cl_log_t log_level;
 
171
  cl_framework_t framework = CL_CT_TCP;
 
172
 
 
173
  ssl_config.ssl_method           = CL_SSL_v23;                 /*  v23 method                                  */
 
174
  ssl_config.ssl_CA_cert_pem_file = getenv("SSL_CA_CERT_FILE"); /*  CA certificate file                         */
 
175
  ssl_config.ssl_CA_key_pem_file  = NULL;                       /*  private certificate file of CA (not used)   */
 
176
  ssl_config.ssl_cert_pem_file    = getenv("SSL_CERT_FILE");    /*  certificates file                           */
 
177
  ssl_config.ssl_key_pem_file     = getenv("SSL_KEY_FILE");     /*  key file                                    */
 
178
  ssl_config.ssl_rand_file        = getenv("SSL_RAND_FILE");    /*  rand file (if RAND_status() not ok)         */
 
179
  ssl_config.ssl_crl_file         = getenv("SSL_CRL_FILE");     /*  revocation list file                        */
 
180
  ssl_config.ssl_reconnect_file   = NULL;                       /*  file for reconnect data    (not used)       */
 
181
  ssl_config.ssl_refresh_time     = 0;                          /*  key alive time for connections (not used)   */
 
182
  ssl_config.ssl_password         = NULL;                       /*  password for encrypted keyfiles (not used)  */
 
183
  ssl_config.ssl_verify_func      = my_ssl_verify_func;         /*  function callback for peer user/name check  */
 
184
 
 
185
  if (getenv("CL_PORT")) {
 
186
     handle_port = atoi(getenv("CL_PORT"));
 
187
  }
 
188
 
 
189
  if (argc < 2) {
 
190
      printf("param1=debug_level [param2=framework(TCP/SSL)]\n");
 
191
      exit(1);
 
192
  }
 
193
 
 
194
  prof_mt_init(); 
 
195
 
 
196
  if (argv[2]) {
 
197
     framework = CL_CT_UNDEFINED;
 
198
     if (strcmp(argv[2], "TCP") == 0) {
 
199
        framework=CL_CT_TCP;
 
200
        printf("using TCP framework\n");
 
201
     }
 
202
     if (strcmp(argv[2], "SSL") == 0) {
 
203
        framework=CL_CT_SSL;
 
204
        printf("using SSL framework\n");
 
205
 
 
206
        if (ssl_config.ssl_CA_cert_pem_file == NULL ||
 
207
            ssl_config.ssl_cert_pem_file    == NULL ||
 
208
            ssl_config.ssl_key_pem_file     == NULL ||
 
209
            ssl_config.ssl_rand_file        == NULL) {
 
210
           printf("please set the following environment variables:\n");
 
211
           printf("SSL_CA_CERT_FILE         = CA certificate file\n");
 
212
           printf("SSL_CERT_FILE            = certificates file\n");
 
213
           printf("SSL_KEY_FILE             = key file\n");
 
214
           printf("(optional) SSL_RAND_FILE = rand file (if RAND_status() not ok)\n");
 
215
        }
 
216
     }
 
217
     if (framework == CL_CT_UNDEFINED) {
 
218
        printf("unexpected framework type\n");
 
219
        exit(1);
 
220
     }
 
221
  }
 
222
 
 
223
  /* setup signalhandling */
 
224
  memset(&sa, 0, sizeof(sa));
 
225
  sa.sa_handler = sighandler_server;  /* one handler for all signals */
 
226
  sigemptyset(&sa.sa_mask);
 
227
  sigaction(SIGINT, &sa, NULL);
 
228
  sigaction(SIGTERM, &sa, NULL);
 
229
  sigaction(SIGHUP, &sa, NULL);
 
230
  sigaction(SIGPIPE, &sa, NULL);
 
231
 
 
232
 
 
233
  printf("commlib setup ...\n");
 
234
  /* this is for compiler warning on irix65 */
 
235
  if (pipe_signal) {
 
236
     printf("pipe\n");
 
237
  }
 
238
  if (hup_signal) {
 
239
     printf("hup\n");
 
240
  };
 
241
 
 
242
  switch(atoi(argv[1])) {
 
243
     case 0:
 
244
        log_level=CL_LOG_OFF;
 
245
        break;
 
246
     case 1:
 
247
        log_level=CL_LOG_ERROR;
 
248
        break;
 
249
     case 2:
 
250
        log_level=CL_LOG_WARNING;
 
251
        break;
 
252
     case 3:
 
253
        log_level=CL_LOG_INFO;
 
254
        break;
 
255
     case 4:
 
256
        log_level=CL_LOG_DEBUG;
 
257
        break;
 
258
     default:
 
259
        log_level=CL_LOG_OFF;
 
260
        break;
 
261
  }
 
262
  cl_com_setup_commlib(CL_RW_THREAD, log_level, NULL);
 
263
 
 
264
 
 
265
  cl_com_set_parameter_list_value("parameter1","value1");
 
266
  cl_com_set_parameter_list_value("parameter2","value2");
 
267
  cl_com_set_parameter_list_value("parameter3","value3");
 
268
  cl_com_set_parameter_list_value("parameter4","value4");
 
269
 
 
270
  cl_com_set_alias_file("./alias_file");
 
271
 
 
272
  cl_com_set_status_func(my_application_status); 
 
273
 
 
274
  cl_com_set_tag_name_func(my_application_tag_name);
 
275
 
 
276
  cl_com_specify_ssl_configuration(&ssl_config);
 
277
 
 
278
  handle=cl_com_create_handle(NULL, framework, CL_CM_CT_MESSAGE, CL_TRUE, handle_port, CL_TCP_DEFAULT, "server", 1, 1, 0 );
 
279
  if (handle == NULL) {
 
280
     printf("could not get handle\n");
 
281
     cl_com_cleanup_commlib();
 
282
     exit(-1);
 
283
  }
 
284
 
 
285
  
 
286
  cl_com_get_service_port(handle,&i), 
 
287
 
 
288
 
 
289
  printf("server running on host \"%s\", port %d, component name is \"%s\", id is %ld\n", 
 
290
         handle->local->comp_host, 
 
291
         i, 
 
292
         handle->local->comp_name,  
 
293
         handle->local->comp_id);
 
294
 
 
295
  cl_com_add_allowed_host(handle, handle->local->comp_host);
 
296
 
 
297
  cl_com_get_max_connections(handle,&max_connections);
 
298
  printf("max open connections is set to %lu\n", max_connections);
 
299
  cl_com_set_max_connection_close_mode(handle,CL_ON_MAX_COUNT_CLOSE_AUTOCLOSE_CLIENTS );
 
300
 
 
301
 
 
302
  cl_com_append_known_endpoint_from_name(handle->local->comp_host, "server", 1, 5000, CL_CM_AC_ENABLED, CL_FALSE );
 
303
 
 
304
  if (getenv("CL_RUNS")) { 
 
305
     runs = atoi(getenv("CL_RUNS"));
 
306
  }
 
307
  while(do_shutdown != 1) {
 
308
     int ret_val;
 
309
 
 
310
     CL_LOG(CL_LOG_INFO,"main()");
 
311
     cl_commlib_trigger(handle, 1); 
 
312
 
 
313
     if (getenv("CL_RUNS")) { 
 
314
        printf("runs: %d\n", runs);
 
315
        runs--;
 
316
     }
 
317
 
 
318
     if (runs<= 0) {
 
319
        do_shutdown = 1;
 
320
     }
 
321
#if 0
 
322
     {
 
323
        cl_raw_list_t* tmp_endpoint_list = NULL;
 
324
        cl_endpoint_list_elem_t* elem = NULL;
 
325
   
 
326
        cl_commlib_search_endpoint(handle, NULL, NULL, 1, CL_TRUE, &tmp_endpoint_list);
 
327
        elem = cl_endpoint_list_get_first_elem(tmp_endpoint_list);
 
328
        printf("\nconnected endpoints with id=1:\n");
 
329
        printf("==============================\n");
 
330
   
 
331
        while(elem) {
 
332
           printf("%s/%s/%d\n", elem->endpoint->comp_host, elem->endpoint->comp_name, elem->endpoint->comp_id);
 
333
           elem = cl_endpoint_list_get_next_elem(tmp_endpoint_list, elem);
 
334
        }
 
335
        cl_endpoint_list_cleanup(&tmp_endpoint_list);
 
336
   
 
337
        cl_commlib_search_endpoint(handle, NULL, NULL, 1, CL_FALSE, &tmp_endpoint_list);
 
338
        elem = cl_endpoint_list_get_first_elem(tmp_endpoint_list);
 
339
        printf("\nconnected and known endpoints with id=1:\n");
 
340
        printf("=========================================\n");
 
341
   
 
342
        while(elem) {
 
343
           printf("%s/%s/%d\n", elem->endpoint->comp_host, elem->endpoint->comp_name, elem->endpoint->comp_id);
 
344
           elem = cl_endpoint_list_get_next_elem(tmp_endpoint_list, elem);
 
345
        }
 
346
        cl_endpoint_list_cleanup(&tmp_endpoint_list);
 
347
   
 
348
        cl_commlib_search_endpoint(handle, NULL, "client", 0, CL_FALSE, &tmp_endpoint_list);
 
349
        elem = cl_endpoint_list_get_first_elem(tmp_endpoint_list);
 
350
        printf("\nconnected and known endpoints with comp_name=client:\n");
 
351
        printf("=====================================================\n");
 
352
   
 
353
        while(elem) {
 
354
           printf("%s/%s/%d\n", elem->endpoint->comp_host, elem->endpoint->comp_name, elem->endpoint->comp_id);
 
355
           elem = cl_endpoint_list_get_next_elem(tmp_endpoint_list, elem);
 
356
        }
 
357
        cl_endpoint_list_cleanup(&tmp_endpoint_list);
 
358
     }
 
359
#endif
 
360
 
 
361
#if 0
 
362
     /* TODO: check behaviour for unknown host and for a host which is down */
 
363
     cl_commlib_send_message(handle, "down_host", "nocomp", 1, CL_MIH_MAT_ACK, (cl_byte_t*)"blub", 5, NULL, 1, 1 ); /* check wait for ack / ack_types  TODO*/
 
364
#endif
 
365
 
 
366
     ret_val = cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender);
 
367
     CL_LOG_STR(CL_LOG_INFO,"cl_commlib_receive_message() returned",cl_get_error_text(ret_val));
 
368
 
 
369
     if (message != NULL) {
 
370
          CL_LOG_STR(CL_LOG_INFO,"received message from",sender->comp_host);
 
371
 
 
372
/*        printf("received message from \"%s/%s/%ld\"\n", sender->comp_host, sender->comp_name, sender->comp_id); */
 
373
 
 
374
 
 
375
 
 
376
        if (strstr((char*)message->message,"exit") != NULL) {
 
377
           printf("received \"exit\" message from host %s, component %s, id %ld\n",
 
378
                  sender->comp_host,sender->comp_name,sender->comp_id );
 
379
           cl_commlib_close_connection(handle, sender->comp_host,sender->comp_name,sender->comp_id, CL_FALSE );
 
380
           
 
381
        } else {
 
382
           ret_val = cl_commlib_send_message(handle, 
 
383
                                sender->comp_host, 
 
384
                                sender->comp_name, 
 
385
                                sender->comp_id, CL_MIH_MAT_NAK,  
 
386
                                &message->message, 
 
387
                                message->message_length, 
 
388
                                NULL, message->message_id,0, 
 
389
                                CL_FALSE,CL_FALSE);
 
390
           if (ret_val != CL_RETVAL_OK) {
 
391
              CL_LOG_INT(CL_LOG_ERROR,"sent message response for message id", (int)message->message_id);
 
392
              CL_LOG_STR(CL_LOG_ERROR,"cl_commlib_send_message() returned:",cl_get_error_text(ret_val));
 
393
           } else {
 
394
              CL_LOG_INT(CL_LOG_INFO,"sent message response for message id", (int)message->message_id);
 
395
           }
 
396
           cl_com_application_debug(handle, "message sent (1)");
 
397
           cl_com_application_debug(handle, "message sent (2)");
 
398
        }
 
399
 
 
400
        cl_com_free_message(&message);
 
401
        cl_com_free_endpoint(&sender);
 
402
        message = NULL;
 
403
     } 
 
404
#if CL_DO_SLOW
 
405
     sleep(1);
 
406
#endif
 
407
  }
 
408
 
 
409
  printf("shutting down server ...\n");
 
410
  handle = cl_com_get_handle( "server", 1 );
 
411
  if (handle == NULL) {
 
412
     printf("could not find handle\n");
 
413
     exit(1);
 
414
  } else {
 
415
     printf("found handle\n");
 
416
  }
 
417
 
 
418
  while ( cl_commlib_shutdown_handle(handle, CL_TRUE) == CL_RETVAL_MESSAGE_IN_BUFFER) {
 
419
     message = NULL;
 
420
     cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender);
 
421
 
 
422
     if (message != NULL) {
 
423
        printf("ignoring message from \"%s\"\n", sender->comp_host); 
 
424
        cl_com_free_message(&message);
 
425
        cl_com_free_endpoint(&sender);
 
426
        message = NULL;
 
427
     }
 
428
  }
 
429
 
 
430
  printf("commlib cleanup ...\n");
 
431
  cl_com_cleanup_commlib();
 
432
  fflush(NULL);
 
433
  
 
434
 
 
435
  printf("main done\n");
 
436
  fflush(stdout);
 
437
  return 0;
 
438
}