File: test_cl_commlib.c

package info (click to toggle)
gridengine 6.2u5-1squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 57,132 kB
  • ctags: 56,142
  • sloc: ansic: 438,030; java: 66,252; sh: 36,376; jsp: 7,757; xml: 5,850; makefile: 5,514; csh: 4,571; cpp: 2,848; perl: 2,401; tcl: 692; lisp: 669; yacc: 668; ruby: 642; lex: 344
file content (468 lines) | stat: -rw-r--r-- 15,898 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468

/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2001 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <pwd.h>

#include "cl_commlib.h"
#include "cl_log_list.h"
#include "cl_endpoint_list.h"
#include "uti/sge_profiling.h"
#define CL_DO_SLOW 0

void sighandler_server(int sig);
static int pipe_signal = 0;
static int hup_signal = 0;
static int do_shutdown = 0;
static int received_qping = 0;

void sighandler_server(
int sig 
) {
   if (sig == SIGPIPE) {
      pipe_signal = 1;
      return;
   }

   if (sig == SIGHUP) {
      hup_signal = 1;
      return;
   }

   /* shutdown all sockets */
   cl_com_ignore_timeouts(CL_TRUE);

   do_shutdown = 1;
}

const char* my_application_tag_name(unsigned long tag) {
   if (tag > 0) {
      return "TAG > 0";
   }
   return "DEFAULT_APPLICATION_TAG";
}

unsigned long my_application_status(char** info_message) {
   if ( info_message != NULL ) {
      (*info_message) = strdup("not specified (state 1)");
   }
#if 0
   received_qping++;
#endif
   if (received_qping > 10) {
      do_shutdown = 1;
   }
   return (unsigned long)1;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "my_ssl_verify_func()"
static cl_bool_t my_ssl_verify_func(cl_ssl_verify_mode_t mode, cl_bool_t service_mode, const char* value) {
   char* user_name = NULL;
   struct passwd *paswd = NULL;

   paswd = getpwuid(getuid());
   if (paswd) {
      user_name = paswd->pw_name;
   }
   if (user_name == NULL) {
      user_name = "unexpected user name";
   }
   if (service_mode == CL_TRUE) {
      CL_LOG(CL_LOG_WARNING,"running in service mode");
      switch(mode) {
         case CL_SSL_PEER_NAME: {
            CL_LOG(CL_LOG_WARNING,"CL_SSL_PEER_NAME");
            if (strcmp(value,"SGE admin user") != 0) {
               CL_LOG(CL_LOG_WARNING,"CL_SSL_PEER_NAME is not \"SGE admin user\"");
               return CL_FALSE;
            }
            break;
         }
         case CL_SSL_USER_NAME: {
            CL_LOG(CL_LOG_WARNING,"CL_SSL_USER_NAME");
            if (strcmp(value,user_name) != 0) {
               CL_LOG_STR(CL_LOG_WARNING,"CL_SSL_USER_NAME is not", user_name);
               return CL_FALSE;
            }
            break;
         }
      }
   } else {
      CL_LOG(CL_LOG_WARNING,"running in client mode");
      switch(mode) {
         case CL_SSL_PEER_NAME: {
            CL_LOG(CL_LOG_WARNING,"CL_SSL_PEER_NAME");
            if (strcmp(value,"SGE admin user") != 0) {
               CL_LOG(CL_LOG_WARNING,"CL_SSL_PEER_NAME is not \"SGE Daemon\"");
               return CL_FALSE;
            }
            break;
         }
         case CL_SSL_USER_NAME: {
            CL_LOG(CL_LOG_WARNING,"CL_SSL_USER_NAME");
            if (strcmp(value,user_name) != 0) {
               CL_LOG_STR(CL_LOG_WARNING,"CL_SSL_USER_NAME is not", user_name);
               return CL_FALSE;
            }
            break;
         }
      }
   }
   return CL_TRUE;
}


#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "main()"
extern int main(int argc, char** argv)
{
  struct sigaction sa;
  cl_ssl_setup_t ssl_config;
  static int runs = 100;
  int handle_port = 0;
  cl_com_handle_t* handle = NULL; 
  cl_com_message_t* message = NULL;
  cl_com_endpoint_t* sender = NULL;
  int i;
  unsigned long max_connections;
  cl_log_t log_level;
  cl_framework_t framework = CL_CT_TCP;

  memset(&ssl_config, 0, sizeof(ssl_config));
  ssl_config.ssl_method           = CL_SSL_v23;                 /*  v23 method                                  */
  ssl_config.ssl_CA_cert_pem_file = getenv("SSL_CA_CERT_FILE"); /*  CA certificate file                         */
  ssl_config.ssl_CA_key_pem_file  = NULL;                       /*  private certificate file of CA (not used)   */
  ssl_config.ssl_cert_pem_file    = getenv("SSL_CERT_FILE");    /*  certificates file                           */
  ssl_config.ssl_key_pem_file     = getenv("SSL_KEY_FILE");     /*  key file                                    */
  ssl_config.ssl_rand_file        = getenv("SSL_RAND_FILE");    /*  rand file (if RAND_status() not ok)         */
  ssl_config.ssl_crl_file         = getenv("SSL_CRL_FILE");     /*  revocation list file                        */
  ssl_config.ssl_reconnect_file   = NULL;                       /*  file for reconnect data    (not used)       */
  ssl_config.ssl_refresh_time     = 0;                          /*  key alive time for connections (not used)   */
  ssl_config.ssl_password         = NULL;                       /*  password for encrypted keyfiles (not used)  */
  ssl_config.ssl_verify_func      = my_ssl_verify_func;         /*  function callback for peer user/name check  */

  if (getenv("CL_PORT")) {
     handle_port = atoi(getenv("CL_PORT"));
  }

  if (argc < 2) {
      printf("param1=debug_level [param2=framework(TCP/SSL)]\n");
      exit(1);
  }

  prof_mt_init(); 

  if (argv[2]) {
     framework = CL_CT_UNDEFINED;
     if (strcmp(argv[2], "TCP") == 0) {
        framework=CL_CT_TCP;
        printf("using TCP framework\n");
     }
     if (strcmp(argv[2], "SSL") == 0) {
        framework=CL_CT_SSL;
        printf("using SSL framework\n");

        if (ssl_config.ssl_CA_cert_pem_file == NULL ||
            ssl_config.ssl_cert_pem_file    == NULL ||
            ssl_config.ssl_key_pem_file     == NULL ||
            ssl_config.ssl_rand_file        == NULL) {
           printf("please set the following environment variables:\n");
           printf("SSL_CA_CERT_FILE         = CA certificate file\n");
           printf("SSL_CERT_FILE            = certificates file\n");
           printf("SSL_KEY_FILE             = key file\n");
           printf("(optional) SSL_RAND_FILE = rand file (if RAND_status() not ok)\n");
        }
     }
     if (framework == CL_CT_UNDEFINED) {
        printf("unexpected framework type\n");
        exit(1);
     }
  }

  /* setup signalhandling */
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = sighandler_server;  /* one handler for all signals */
  sigemptyset(&sa.sa_mask);
  sigaction(SIGINT, &sa, NULL);
  sigaction(SIGTERM, &sa, NULL);
  sigaction(SIGHUP, &sa, NULL);
  sigaction(SIGPIPE, &sa, NULL);


  printf("commlib setup ...\n");
  /* this is for compiler warning on irix65 */
  if (pipe_signal) {
     printf("pipe\n");
  }
  if (hup_signal) {
     printf("hup\n");
  };

  switch(atoi(argv[1])) {
     case 0:
        log_level=CL_LOG_OFF;
        break;
     case 1:
        log_level=CL_LOG_ERROR;
        break;
     case 2:
        log_level=CL_LOG_WARNING;
        break;
     case 3:
        log_level=CL_LOG_INFO;
        break;
     case 4:
        log_level=CL_LOG_DEBUG;
        break;
     default:
        log_level=CL_LOG_OFF;
        break;
  }
  cl_com_setup_commlib(CL_RW_THREAD, log_level, NULL);


  cl_com_set_parameter_list_value("parameter1","value1");
  cl_com_set_parameter_list_value("parameter2","value2");
  cl_com_set_parameter_list_value("parameter3","value3");
  cl_com_set_parameter_list_value("parameter4","value4");

  cl_com_set_alias_file("./alias_file");

  cl_com_set_status_func(my_application_status); 
 
  cl_com_set_tag_name_func(my_application_tag_name);

  if (framework == CL_CT_SSL) {
     cl_com_specify_ssl_configuration(&ssl_config);
  }

  handle=cl_com_create_handle(NULL, framework, CL_CM_CT_MESSAGE, CL_TRUE, handle_port, CL_TCP_DEFAULT, "server", 1, 1, 0 );
  if (handle == NULL) {
     printf("could not get handle\n");
     cl_com_cleanup_commlib();
     exit(-1);
  }

  
  cl_com_get_service_port(handle,&i), 


  printf("server running on host \"%s\", port %d, component name is \"%s\", id is %ld\n", 
         handle->local->comp_host, 
         i, 
         handle->local->comp_name,  
         handle->local->comp_id);

  cl_com_add_allowed_host(handle, handle->local->comp_host);

  cl_com_get_max_connections(handle,&max_connections);
  printf("max open connections is set to %lu\n", max_connections);
  cl_com_set_max_connection_close_mode(handle,CL_ON_MAX_COUNT_CLOSE_AUTOCLOSE_CLIENTS );


  cl_com_append_known_endpoint_from_name(handle->local->comp_host, "server", 1, 5000, CL_CM_AC_ENABLED, CL_FALSE );

  if (getenv("CL_RUNS")) { 
     runs = atoi(getenv("CL_RUNS"));
  }
  while(do_shutdown != 1) {
     int ret_val;

     if (getenv("CL_FORK_SLEEP_CHILD")) {
        int sleep_child = atoi(getenv("CL_FORK_SLEEP_CHILD"));
        int sleep_parent = 1;
        pid_t child_pid = 0;
        if (getenv("CL_FORK_SLEEP_PARENT")) {
           sleep_parent = atoi(getenv("CL_FORK_SLEEP_PARENT"));
        }
        if ((child_pid = fork()) == 0) {
           printf("fork() - child - mypid=(%d)!\n", (int) getpid());
           printf("fork() - child - sleep %d ...\n", (int) sleep_child);
           fflush(stdout);
           sleep(sleep_child);
           printf("fork() - child - sleep %d done\n", (int) sleep_child);
           fflush(stdout);
           exit(0);
        } else {
           printf("fork() - parent - mypid=(%d)!\n", (int) getpid());
           printf("fork() - parent - childpid=(%d)!\n", (int) child_pid);
           printf("fork() - parent - sleep %d ...\n", (int) sleep_parent);
           fflush(stdout);
           sleep(sleep_parent);
           printf("fork() - parent - sleep %d done\n", (int) sleep_parent);
           printf("fork() - parent - killing child pid %d ...\n", (int) child_pid);
           fflush(stdout);
           kill(child_pid, SIGKILL);
           printf("fork() - parent - killing child pid %d done\n", (int) child_pid);
           fflush(stdout);
        }
     } 

     CL_LOG(CL_LOG_INFO,"main()");
     cl_commlib_trigger(handle, 1); 

     if (getenv("CL_RUNS")) { 
        printf("runs: %d\n", runs);
        runs--;
     }

     if (runs<= 0) {
        do_shutdown = 1;
     }
#if 0
     {
        cl_raw_list_t* tmp_endpoint_list = NULL;
        cl_endpoint_list_elem_t* elem = NULL;
   
        cl_commlib_search_endpoint(handle, NULL, NULL, 1, CL_TRUE, &tmp_endpoint_list);
        elem = cl_endpoint_list_get_first_elem(tmp_endpoint_list);
        printf("\nconnected endpoints with id=1:\n");
        printf("==============================\n");
   
        while(elem) {
           printf("%s/%s/%d\n", elem->endpoint->comp_host, elem->endpoint->comp_name, elem->endpoint->comp_id);
           elem = cl_endpoint_list_get_next_elem(tmp_endpoint_list, elem);
        }
        cl_endpoint_list_cleanup(&tmp_endpoint_list);
   
        cl_commlib_search_endpoint(handle, NULL, NULL, 1, CL_FALSE, &tmp_endpoint_list);
        elem = cl_endpoint_list_get_first_elem(tmp_endpoint_list);
        printf("\nconnected and known endpoints with id=1:\n");
        printf("=========================================\n");
   
        while(elem) {
           printf("%s/%s/%d\n", elem->endpoint->comp_host, elem->endpoint->comp_name, elem->endpoint->comp_id);
           elem = cl_endpoint_list_get_next_elem(tmp_endpoint_list, elem);
        }
        cl_endpoint_list_cleanup(&tmp_endpoint_list);
   
        cl_commlib_search_endpoint(handle, NULL, "client", 0, CL_FALSE, &tmp_endpoint_list);
        elem = cl_endpoint_list_get_first_elem(tmp_endpoint_list);
        printf("\nconnected and known endpoints with comp_name=client:\n");
        printf("=====================================================\n");
   
        while(elem) {
           printf("%s/%s/%d\n", elem->endpoint->comp_host, elem->endpoint->comp_name, elem->endpoint->comp_id);
           elem = cl_endpoint_list_get_next_elem(tmp_endpoint_list, elem);
        }
        cl_endpoint_list_cleanup(&tmp_endpoint_list);
     }
#endif

#if 0
     /* TODO: check behaviour for unknown host and for a host which is down */
     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*/
#endif
 
     ret_val = cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender);
     CL_LOG_STR(CL_LOG_INFO,"cl_commlib_receive_message() returned",cl_get_error_text(ret_val));

     if (message != NULL) {
          CL_LOG_STR(CL_LOG_INFO,"received message from",sender->comp_host);

/*        printf("received message from \"%s/%s/%ld\"\n", sender->comp_host, sender->comp_name, sender->comp_id); */



        if (strstr((char*)message->message,"exit") != NULL) {
           printf("received \"exit\" message from host %s, component %s, id %ld\n",
                  sender->comp_host,sender->comp_name,sender->comp_id );
           cl_commlib_close_connection(handle, sender->comp_host,sender->comp_name,sender->comp_id, CL_FALSE );
           
        } else {
           ret_val = cl_commlib_send_message(handle, 
                                sender->comp_host, 
                                sender->comp_name, 
                                sender->comp_id, CL_MIH_MAT_NAK,  
                                &message->message, 
                                message->message_length, 
                                NULL, message->message_id,0, 
                                CL_FALSE,CL_FALSE);
           if (ret_val != CL_RETVAL_OK) {
              CL_LOG_INT(CL_LOG_ERROR,"sent message response for message id", (int)message->message_id);
              CL_LOG_STR(CL_LOG_ERROR,"cl_commlib_send_message() returned:",cl_get_error_text(ret_val));
           } else {
              CL_LOG_INT(CL_LOG_INFO,"sent message response for message id", (int)message->message_id);
           }
           cl_com_application_debug(handle, "message sent (1)");
           cl_com_application_debug(handle, "message sent (2)");
        }

        cl_com_free_message(&message);
        cl_com_free_endpoint(&sender);
        message = NULL;
     } 
#if CL_DO_SLOW
     sleep(1);
#endif
  }

  printf("shutting down server ...\n");
  handle = cl_com_get_handle( "server", 1 );
  if (handle == NULL) {
     printf("could not find handle\n");
     exit(1);
  } else {
     printf("found handle\n");
  }

  while ( cl_commlib_shutdown_handle(handle, CL_TRUE) == CL_RETVAL_MESSAGE_IN_BUFFER) {
     message = NULL;
     cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender);

     if (message != NULL) {
        printf("ignoring message from \"%s\"\n", sender->comp_host); 
        cl_com_free_message(&message);
        cl_com_free_endpoint(&sender);
        message = NULL;
     }
  }

  printf("commlib cleanup ...\n");
  cl_com_cleanup_commlib();
  fflush(stdout);

  printf("main done\n");
  fflush(stdout);
  return 0;
}