Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a vicious bug: TCP socket use a buffer and read operation get as much data as...
[simgrid.git] / src / gras / gras.c
1 /* $Id$ */
2
3 /* gras.c -- generic functions not fitting anywhere else                    */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson.                                 */
6 /* All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "xbt/log.h"
12 #include "xbt/module.h" /* xbt_init/exit */
13
14 #include "gras_modinter.h"   /* module init/exit */
15 #include "amok/amok_modinter.h"   /* module init/exit */
16 #include "xbt_modinter.h"   /* module init/exit */
17
18 #include "gras.h"
19 #include "gras/process.h" /* FIXME: killme and put process_init in modinter */
20
21 #include "portable.h" /* hexa_*(); signalling stuff */
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories (cf. section \ref GRAS_API)");
24 static int gras_running_process = 0;
25
26 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
27 static void gras_sigusr_handler(int sig) {
28    INFO0("SIGUSR1 received. Display the backtrace");
29    xbt_backtrace_display();
30 }
31
32 static void gras_sigint_handler(int sig) {
33    static double lastone = 0;
34    if (lastone == 0 || gras_os_time() - lastone > 5) {
35       lastone = gras_os_time();
36       xbt_backtrace_display();
37       fprintf(stderr,"\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
38    } else {
39       exit(1);
40    }
41 }
42 #endif
43
44 void gras_init(int *argc,char **argv) {
45
46   VERB0("Initialize GRAS");
47   
48   /* First initialize the XBT */
49   xbt_init(argc,argv);
50    
51   /* module registrations: 
52    *    - declare process specific data we need (without creating them) 
53    */
54   if (gras_running_process == 0) {
55      gras_trp_register();
56      gras_msg_register();
57   }
58    
59   /*
60    * Initialize the process specific stuff
61    */
62   gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
63   
64   /*
65    * Initialize the global stuff if it's not the first process created
66    */
67   if (gras_running_process++ == 0) {
68     gras_emul_init();
69     gras_msg_init();
70     gras_trp_init();
71     gras_datadesc_init();
72 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
73     signal(SIGUSR1,gras_sigusr_handler);
74     signal(SIGINT,gras_sigint_handler);
75 #endif     
76   }
77    
78   /* and then init amok */
79   amok_init();
80 }
81
82 void gras_exit(void) {
83   INFO0("Exiting GRAS");
84   if (--gras_running_process == 0) {
85     gras_msg_exit();
86     gras_trp_exit();
87     gras_datadesc_exit();
88     gras_emul_exit();
89   }
90   gras_process_exit();
91   xbt_exit();
92 }
93
94 const char *hexa_str(unsigned char *data, int size) {
95   static char*buff=NULL;
96   static int buffsize=0;
97   int i,pos=0;  
98   
99   if (buffsize<5*(size+1)) {
100     if (buff)
101       free(buff);
102     buffsize=5*(size+1);
103     buff=xbt_malloc(buffsize);
104   }
105   for (i=0;i<size;i++)  {
106     if (data[i]<32 || data[i]>126)
107       sprintf(buff+pos,".(%02x)",data[i]);
108     else
109       sprintf(buff+pos,"%c(%02x)",data[i],data[i]);
110     while (buff[++pos]);
111    }
112   buff[pos]='\0';  
113   return buff;
114 }
115 void hexa_print(const char*name, unsigned char *data, int size) {
116    printf("%s: %s\n", name,hexa_str(data,size));
117 }
118