summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_Cmailbox.c
blob: 173f9d3e75465cd48071972b668b4865679aabe9 (plain)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#if !defined( WNT ) && !defined(__hpux)

#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif

#include <stdio.h>
#include <errno.h>

#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif

#ifdef HAVE_STRING_H
# include <string.h>
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

/* #ifdef ANSI */
int create_sharedmemory (int **,char *,int);
int open_sharedmemory   (int **,char *,int);
int remove_sharedmemory (int * ,char *    );
/* #endif */

/* This is a multi-server & multi-client asynchronous mailbox management */


/* The UNIX mail box is based on shared memory 
/
/
/ The messages are send via shared memory .
/ AST like functions (VMS) are simulated with a signal handler using SIGUSR1.
/
/ To manage multiple mail boxes , an internal use shared memory is used
/ to communicate index among table of mail box informations .
/
/
/ Primitives to manage mail boxes :
/  osd_crmbox     Create a mail box
/  osd_opmbox     Open a mail box
/  osd_clmbox     Close a mail box
/  osd_wrmbox     Write into a mail box
/
/  user function needed to receive messages :
/   user_function (int *box_id, char *box_name, char *message_address, int message_length)
/
/
/
/ In following explanations, "special shared memory" designates an internal 
/ data area implemented with shared memory used to send index of mail box 
/ informations table for the signal handler.
/
/ Algorithms :
/
/  To create a mail box      - Find a free entry in mail box infos table
/                            - If first mail box, create special shared memory
/                            - Create a shared memory for messages
/                            - Put PID of creator into shared memory
/                            - Install signal handler for SIGUSR1
/
/
/  To open a mailbox         - Find a free entry in mail box infos table
/                            - Attach shared memory to process
/                            - Get PID of creator from shared memory
/
/
/
/  To close a mail box       - Remove shared memory
/                            - Free entry in mail box infos table
/                            - If last mail box, remove special shared memory
/
/
/  To write in a mail box    - Write message into shared memory using 
/                              following protocol :
/                                 Length Message
/
/                            - Send signal SIGUSR1 to server process
/
/
/  To receive message        - Get mail box identification from special shared
/                              memory.
/                            - Get message with protocol
/                            - Get all informations for user function
/                            - Call user function
/                            - Arm again the signal handler
*/



#define MAX_BOX    256          /* Maximum number of mail boxes */

/* Protocol to communicate PID of server to clients */

#define BEGIN_PROTOCOL 0xAABB
#define END_PROTOCOL   0xCCDD

#define SIZEOFNAME     64      /* length of mailbox name */


/* Mail boxes informations table */

static struct {

  int channel;                  /* Id of shared memory (IPC)   */
  int  size;                    /* Size of data area           */
  int (* user_func) ();         /* User function               */
  char name[SIZEOFNAME];        /* Name of mail box   VMS only */
  int *address;                /* Address of data area        */

} id_table[MAX_BOX +1];         /* Allows up to MAX_BOX mail boxes */
/*  char *address;   */             /* Address of data area        */

static int first_mail_box = 1;  /* Flag to initialize mail boxes */
static int pid;                 /* Pid of server or client       */
static int nb_mail = 0;
static int max_mail = MAX_BOX;

/* For special shared memory only */

/*static char *shared_infos;*/      /* Address of shared memory     */
static int *shared_infos;      /* Address of shared memory     */
static int   shared_shmid;      /* Id (IPC) of shared memory    */




static struct {
  int code1;      /* Beginning of protocol */
  int pid;        /* PID of server         */
  int code2;      /* End of protocol       */
 } protocol;










/*====== Private : ======================================================

  find an entry in the mail_box identification table

  -----------------------------------------------------------------------*/

static int
alloc_entry()
{
 int i;

 /* If first use, initialize id_table */

 if (first_mail_box) {
  memset(id_table,0,sizeof(id_table));
  first_mail_box = 0;

  /* Allocate special shared memory named "XptY"  why not ?! */

  if (! create_sharedmemory(&shared_infos, "XptY", sizeof( id_table ) ))
     max_mail = 1;
  else {
     shared_shmid = open_sharedmemory (&shared_infos, "XptY", sizeof (id_table) );
     if (shared_shmid == 0) max_mail = 1;
    }
 }


 i = 1;
 while ( id_table[i].address != NULL && i < MAX_BOX)
  i++;

 if (i == MAX_BOX-1) return -1;   /* Too many mail boxes opened */
   else return (i);
}




/*========= Private : ===================================================

 Remove special shared memory (internal use) when no more mail boxes

 -----------------------------------------------------------------------*/

static void
keep_clean()
{
 remove_sharedmemory (&shared_shmid,"Xpty");
 first_mail_box = 1;
}





/*========= Private : ===================================================

 Set specific error for Mail boxes
 This allow more detailed error decoding

 -----------------------------------------------------------------------*/

static void
set_err(int number)
{
 errno = number + 256;   /* Set specific error for Mail boxes */
}




/*====== Private : ======================================================

  Put PID of handler process into shared memory
  using a special protocol  AABB pid CCDD

----------------------------------------------------------------------*/


void
put_pid(int boxid)
{
 protocol.code1 = BEGIN_PROTOCOL;
 protocol.pid   = getpid();
 protocol.code2 = END_PROTOCOL;
 memcpy (id_table[boxid].address, &protocol, sizeof(protocol));
}

int
get_pid(int boxid)
{
 memcpy(&protocol, id_table[boxid].address, sizeof(protocol));
 if (protocol.code1 != BEGIN_PROTOCOL) return(-1);
 if (protocol.pid   <= 2) return(-2);
 if (protocol.code2 != END_PROTOCOL) return(-3);

 pid = protocol.pid;

 return(0);
}




/*====== Private : ======================================================

  Mail box handler

 This simulate a VMS AST function :
   Asynchronous function

----------------------------------------------------------------------*/

void
handler(int sig)
{         
 int boxid;
 int length;
/* char * address;*/
 int * address;
 char boxname [65];

 memcpy (boxname, shared_infos, SIZEOFNAME); /* Get name of mailbox */

 for (boxid=1; boxid <= MAX_BOX; boxid++)
     if (strcmp(boxname,id_table[boxid].name) == 0) break;

 if (boxid > MAX_BOX) return ; /* ****** What could we do ? ***** */
 
 address = id_table[boxid].address;

 address += sizeof(protocol);            /* Pass the protocol     JPT */

 memcpy(&length, address, sizeof(int));  /* Restore length of message */

 address += sizeof(int);                 /* Adjust pointer to the message */

 /* Call user function */

 /* Don't forget to give again PID 
    of handler process in case of multi-clients */


/* call user-function */
 
(*id_table[boxid].user_func) (&boxid,
                                id_table[boxid].name,
                                address,
                                length
                               );

 /* Re-arm handler */

  signal (SIGUSR1, handler); 


/* don't forget to give again PID of handler process in case of multi-clients
 */ 
 put_pid(boxid); 

}



/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/
/                          P U B L I C       functions
/
/@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/



/*====================== Create a mail box =========================

  Returns 0 if failed
          mail box identification if succeeded

  ================================================================*/

int
create_mailbox(char *box_name, int box_size,
               int (* async_func) (int *box_id, char *box_name,
               char *message_address, int message_length) )
{
/* int status;*/
 int index;
 int shmid;
 

 /* Test function security */

 if (async_func == NULL) {
  set_err (EFAULT);
  return (0);  /* Verifies function exists */
 }

 if (box_size == 0){
  set_err (EINVAL);
  return (0);
 }

 if (box_name == NULL) {
  set_err (EFAULT);
  return (0);
 }

 index = alloc_entry();               /* Find an entry for id_table */
 if (index == -1) {
  set_err(EMFILE);
  keep_clean();
  return(0);
 }

 if (max_mail == 1 && index > 0) {  /* If only one mail box authorized */
  set_err (EMFILE);
  return(0);
 }

 /* Create shared memory for the process */

 shmid = create_sharedmemory ( &id_table[index].address, box_name, box_size);
 if (shmid == 0) return (0);


 put_pid (index);    /* Put pid of server into shared memory  */

 id_table[index].channel = shmid;              /* Keep id of shared memory */
 id_table[index].size = box_size;              /* Keep size of mail box */
 strncpy(id_table[index].name,box_name,SIZEOFNAME);    /* Keep name of mail box */
 id_table[index].user_func = async_func;       /* Keep user function */


 /* Install asynchronous function : AST function */

  signal (SIGUSR1,  handler); 

 nb_mail++;          
 return (index);
}



/*====================== Open a mail box =======================

  Returns 0 if failed
          mail box identification if succeeded

  ================================================================*/

int
open_mailbox(char * box_name, int box_size)
{
 int status;
 int index;    /* Index for mail box informations access */

 /* Test function security */

 if (box_size == 0){
  set_err (EINVAL);
  return (0);
 }

 if (box_name == NULL) {
  set_err (EFAULT);
  return (0);
 }

 index = alloc_entry();               /* Find an entry for id_table */
 if (index == -1) {
  set_err(EMFILE);
  if (nb_mail == 0) keep_clean();
  return(0);
 }

 id_table[index].size = box_size;              /* Keep size of mail box */
 strncpy(id_table[index].name,box_name,SIZEOFNAME);    /* Keep name of mail box */

 /* Attach shared memory to the process */

 status = open_sharedmemory ( (int **)&id_table[index].address, box_name,
                       box_size);

 if (status !=0)

 if (get_pid (index) < 0){  /* Get pid from shared memory  */
   set_err(ESRCH);
   return (0);
 }
 

 id_table[index].channel = status;

 if (status != 0) {
  return (index);    
 } 
 else {               /* Error */
  id_table[index].address = NULL;    /* ensure pointer is empty */
  keep_clean();
  return(0);
 }
}





/*====================== Close a mail box =======================*/

int
remove_mailbox(int *boxid, char *box_name)
{
 if (boxid == 0){
  set_err(EINVAL);
  return (0);
 }

 if (box_name == NULL) {
  set_err(EFAULT);
  return (0);
 }


/*  (*boxid)--; JPT */

 nb_mail--;

 /* If last mail box removed, remove special shared memory */

 if (nb_mail == 0) keep_clean(); 

 remove_sharedmemory (&id_table[*boxid].channel, box_name );  /* Close shared memory */
 id_table[*boxid].address = NULL;

 return (1);
}



/*====================== Write into a mail box =======================*/

int
write_mailbox(int *boxid, char *box_name, char *message, int length)
{
 int status;
/* char * address;*/         /* Used for protocol  :  length   message */
 int * address;         /* Used for protocol  :  length   message */
 int  interm_length;     /* Used to copy length into shared memory */

 if (*boxid == 0){
  set_err(EBADF);
  return (0);
 }

 if (message == NULL) {
  set_err(EFAULT);
  return (0);
 }

 /* (*boxid)--; JPT */

 address = id_table[*boxid].address;

 address += sizeof(protocol); /* After the protocol JPT */

 interm_length = length; /* Use an intermediate variable for memcpy transfert */

 memcpy(address, &interm_length, sizeof(int));  /* Put length of message */
 address += sizeof(int);                        /* Adjust address for message */

 memcpy(address, message, length+1); /* Put message */

 memcpy(shared_infos, id_table[*boxid].name, SIZEOFNAME ); /* Give index in table infos */

 status = kill (pid, SIGUSR1);   /* Send signal to server */
 
  if (status == 0) return (1);
  else {
   set_err(errno);
   return (0);
  }
}
#endif