File: radprocess.h

package info (click to toggle)
radlib 2.12.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: ansic: 15,843; sh: 8,102; makefile: 501
file content (549 lines) | stat: -rw-r--r-- 16,472 bytes parent folder | download | duplicates (3)
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#ifndef INC_radprocessh
#define INC_radprocessh
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------------------------
 
  FILENAME:
        radprocess.h
 
  PURPOSE:
     Provide an asynchronous event-driven process control utility.
        Sets up the following utilities for a new process:
            Semaphores (semaphore.h)
            System Buffers (buffers.h)
            MsgLog (msgLog.h)
            Message Queue (queue.h)
            IO processing ("select")
            Timer List with 'numTimers' timers (timers.h)
            Event handler (events.h)
        Also provides wrappers for many "IPC" utilities.
 
  REVISION HISTORY:
        Date            Engineer        Revision        Remarks
        12/26/01        M.S. Teel       0               Original
        02/08/07        M.S. Teel       1               Add 64-bit support
 
  NOTES:
        
 
  LICENSE:
        Copyright 2001-2005 Mark S. Teel. All rights reserved.
 
        Redistribution and use in source and binary forms, with or without 
        modification, are permitted provided that the following conditions 
        are met:
 
        1. Redistributions of source code must retain the above copyright 
           notice, this list of conditions and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright 
           notice, this list of conditions and the following disclaimer in the 
           documentation and/or other materials provided with the distribution.
 
        THIS SOFTWARE IS PROVIDED BY Mark Teel ``AS IS'' AND ANY EXPRESS OR 
        IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
        DISCLAIMED. IN NO EVENT SHALL MARK TEEL OR CONTRIBUTORS BE LIABLE FOR 
        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
        OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
        HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
        STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
        IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
        POSSIBILITY OF SUCH DAMAGE.
  
----------------------------------------------------------------------------*/

/*  ... System include files
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>


/*  ... Library include files
*/
#include <radsysdefs.h>
#include <radsemaphores.h>
#include <radbuffers.h>
#include <radsystem.h>
#include <radqueue.h>
#include <radtimers.h>
#include <radevents.h>
#include <radtimeUtils.h>



/*  ... generic API definitions
*/
#define PROCESS_MAX_NAME_LEN    32



/*  !!!!!!!!!!!!!!!!!!!!!  HIDDEN, don't use  !!!!!!!!!!!!!!!!!!!!!!!!!
*/

#define PROC_TOTAL_IO_BLOCKS    16

enum ProcessFdTypes
{
    PROC_FD_PIPE_READ           = 0,        /* MUST be first two! */
    PROC_FD_PIPE_WRITE          = 1,
    PROC_FD_MSG_QUEUE           = 2,
    PROC_FD_USER_FIRST          = 3,
    PROC_FD_USER_LAST           = (PROC_TOTAL_IO_BLOCKS-1),
    PROC_FD_NUM_INDEXES         = PROC_TOTAL_IO_BLOCKS
};


typedef struct
{
    NODE            node;
    ULONG           id;
    void            (*msgHandler) (char *srcQueueName,
                                   UINT msgType,
                                   void *msg,
                                   UINT length,
                                   void *userData);
    void            *udata;
} PROC_MSGQ_HANDLER;

typedef struct processIoTag
{
    void            (*ioCallback) (int fd, void *userData);
    void            *userData;
} PROC_IO_BLK;


/*  ... define the process data; there will be one (static) instance
    ... of this structure for each process
*/
typedef struct
{
    char            name[PROCESS_MAX_NAME_LEN+1];
    pid_t           pid;
    fd_set          fdSet;
    int             fdMax;
    int             fds   [PROC_FD_NUM_INDEXES];
    PROC_IO_BLK     ioIDs [PROC_FD_NUM_INDEXES];
    T_QUEUE_ID      myQueue;
    long            defaultMsgQID;
    RADLIST         msgqHandlerList;

    // two flags for queue message handling
    int             keepMsgQBuffer;
    int             stopMsqQHandlerTraversal;

    EVENTS_ID       events;
    void            *userData;
    int             exitFlag;
} PROCESS_DATA;


/*  !!!!!!!!!!!!!!!!!!!!!!!!  END HIDDEN  !!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

/*  ... define an ID for process I/O registration so user file descriptors
    ... can be added to the "select" fd_set used by "processWait"
*/
typedef int PROC_IO_ID;



/*  ... API calls
*/

/*  ... initialize process management; called once during process init;
    ... automatically sets up the following utilities for a new process:
    ...     Semaphores (semaphore.h)
    ...     System Buffers (buffers.h)
    ...     MsgLog (msgLog.h)
    ...     Message Queue (queue.h)
    ...     IO processing ("select")
    ...     Timer List with 'numTimers' timers (timers.h)
    ...     Event handler (events.h)
    ...
    ... 'processName' is used for logging;
    ... 'queueName' is used for the msg queue and events;
    ... 'queueDbKey' is used to attach to the proper shared queue groups
    ...     and queue database;
    ... 'queueDbSemIndex' is necessary for mutex protection on the
    ...     shared queue groups and queue database;
    ... 'numTimers' determines the max number of timers for this process;
    ... 'runAsDaemon' determines if process can have a controlling terminal
    ...     and if msgLog's go to stderr or not;
    ... 'messageHandler' is the callback for queue messages;
    ... 'eventHandler' is the callback for events;
    ... 'userData' is passed to the event and message callbacks;
    ...
    ... returns OK or ERROR
    ...
    ... NOTE: If a messageHandler wants to retain ownership of the system
    ...       buffer passed in as 'msg', it should call the new function
    ...       'radProcessQueueKeepBuffer' prior to returning; it is then the 
    ...       responsibility of the receiving process to release the buffer 
    ...       via radBufferRls when it is finished with the buffer.
*/
extern int radProcessInit
(
    char    *processName,
    char    *queueName,
    int     numTimers,
    int     runAsDaemon,
    void    (*messageHandler) (char *srcQueueName,
                               UINT msgType,
                               void *msg,
                               UINT length,
                               void *userData),
    void    (*eventHandler) (UINT eventsRx,
                             UINT rxData,
                             void *userData),
    void    *userData
);

extern void radProcessExit (void);


/*  ... use this to set the internal exit flag (i.e. inside SIG handler)
*/
extern void radProcessSetExitFlag (void);

/*  ... use this to get the internal exit flag (i.e. inside SIG handler)
    ... returns TRUE if set otherwise FALSE
*/
extern int radProcessGetExitFlag (void);


/*  ... wait for messages, timers, events, and user IO blocks in one call;
    ... should be the focal point of a process's main loop;
    ... 'timeout' (in milliseconds), if > 0, will cause this function to 
    ... return TIMEOUT even if no I/O triggered after 'timeout' milliseconds 
    ... (remember linux PC timers are accurate to 10 ms typically);
    ... returns OK, ERROR, or TIMEOUT if 'timeout' occurred
*/
extern int radProcessWait (int timeout);




/*  *** general process utilities ***
*/
/*  ... get the calling process's name;
    ... returns the pointer 'store', where the name is copied
*/
extern char *radProcessGetName (char *store);

/*  ... get the calling process's pid
*/
extern pid_t radProcessGetPid (void);

/*  ... get the calling process's notify fd
*/
extern int radProcessGetNotifyFD (void);



/*  *** process I/O utilities ***
*/
/*  ... register your file descriptor for "processWait" inclusion;
    ... 'ioCallback' will be executed if data or an error occurs on 'fd';
    ... 'userData' will be passed to 'ioCallback';
    ... returns PROC_IO_ID or ERROR
*/
extern PROC_IO_ID radProcessIORegisterDescriptor
(
    int         fd,
    void        (*ioCallback) (int fd, void *userData),
    void        *userData
);

/*  ... de-register your file descriptor for "processWait" inclusion;
    ... returns OK or ERROR
*/
extern int radProcessIODeRegisterDescriptor
(
    PROC_IO_ID  id
);

extern int radProcessIODeRegisterDescriptorByFd
(
    int     fd
);

/*  ... register STDIN for "processWait" inclusion;
    ... 'ioCallback' will be executed if data or an error occurs on STDIN;
    ... 'userData will be passed to 'ioCallback';
    ... returns PROC_IO_ID or NULL
*/
extern PROC_IO_ID radProcessIORegisterSTDIN
(
    void        (*ioCallback) (int fd, void *userData),
    void        *userData
);



/*  *** process signal utilities ***
*/
/*  ... initialize ALL signals (except SIGKILL and SIGSTOP) to be caught
    ... by 'defaultHandler';
    ... returns OK or ERROR
*/
extern int radProcessSignalCatchAll (void (*defaultHandler) (int signum));

/*  ... assign a specific 'handler' to a specific 'signum';
    ... returns OK or ERROR
*/
extern int radProcessSignalCatch (int signum, void (*handler) (int signum));

/*  ... set 'signum' back to the default system handler;
    ... returns OK or ERROR
*/
extern int radProcessSignalRelease (int signum);

/*  ... set 'signum' to be ignored;
    ... returns OK or ERROR
*/
extern int radProcessSignalIgnore (int signum);

/*  ... retrieve the current handler for 'signum';
    ... returns the function pointer or NULL
*/
extern void (*(radProcessSignalGetHandler (int signum))) (int);




/*  *** queue wrappers ***
*/
/*  ... return my queue's name
*/
extern char *radProcessQueueGetName
(
    char        *store
);

/*  ... return my queue's ID
*/
extern T_QUEUE_ID radProcessQueueGetID
(
    void
);

/*  ... attach to an individual queue based on queue name
    ... so that messages can be sent to it
    ... returns OK or ERROR
*/
extern int radProcessQueueAttach
(
    char        *newName,
    int         group
);

/*  ... dettach from an individual queue based on queue name
*/
extern int radProcessQueueDettach
(
    char        *oldName,
    int         group
);

/*  ... add my queue to a group and add the group to my address list
    ... returns OK or ERROR
*/
extern int radProcessQueueJoinGroup
(
    int         groupNumber
);

/*  ... remove my queue from a group and remove a group from my address list
    ... returns OK or ERROR
*/
extern int radProcessQueueQuitGroup
(
    int         groupNumber
);

/*  ... write to a queue;
    ... assumes sysBuffer is a valid pointer to a system buffer (unless length
    ... is zero, in which case a zero-length message is sent);
    ... system buffer ownership is transfered to the receiving queue;
    ... returns OK, ERROR, or ERROR_ABORT if the dest queue is gone;
    ... user should dettach from a dest on ERROR_ABORT!
*/
extern int radProcessQueueSend
(
    char        *destQueueName,
    UINT        msgType,
    void        *sysBuffer,
    UINT        length
);

/*  ... write to all queues in a group;
    ... checks to make sure the group hasn't changed - if it has
    ... it refreshes the address list;
    ... assumes sysBuffer is a valid pointer to a system buffer;
    ... system buffer is released if this call returns OK;
    ... returns OK or ERROR
*/
extern int radProcessQueueSendGroup
(
    int         destGroup,
    UINT        msgType,
    void        *sysBuffer,
    UINT        length
);

/*  ... determine if calling process is attached to the given queue name
    ... returns TRUE or FALSE
*/
extern int radProcessQueueIsAttached
(
    char        *queueName
);

/*  ... ONLY called from inside a message queue handler to indicate that 
    ... retention of the buffer is desired; if not called, the buffer will be
    ... released as usual after the last handler in the traversal list has 
    ... been invoked; this replaces the cheesy method used before to retain
    ... buffers by setting the first byte of the 'srcQueueName' to '0';
    ... It is the caller's responsibility to free the buffer after making this
    ... call to retain it
    ... Note: this will implicitly set the 'radProcessQueueStopHandlerList' 
    ...       condition as well
*/
extern void radProcessQueueKeepBuffer
(
    void
);

/*  ... if more than one message queue handler has been defined via the 
    ... 'radProcessQueuePrependHandler', then this function is used to indicate
    ... that the traversal of the handler list should stop when the calling 
    ... handler returns (i.e., the calling handler "matches" the message 
    ... received)
*/
extern int radProcessQueueStopHandlerList
(
    void
);

/*  ... prepend an additional message queue handler to the existing list of 
    ... message handlers; this allows other utilities/objects/etc. to insert
    ... a message handler to process specific utility messages without the 
    ... radlib application process having to have knowledge of those messages;
    ... this will not interrupt normal application message reception (although
    ... the utility/object doing the insertion should ensure that message types
    ... it is using do not conflict with the application message type 
    ... definitions); this is completely backward compatible with existing 
    ... radlib applications;
    ... 'userdata' will be passed to 'msgHandler';
    ... returns a unique identifier > 0 that can be used to remove the message 
    ... handler later (see 'radProcessQueueRemoveHandler') or ERROR
*/
extern long radProcessQueuePrependHandler
(
    void            (*msgHandler) (char *srcQueueName,
                                   UINT msgType,
                                   void *msg,
                                   UINT length,
                                   void *userData),
    void            *userdata
);

/*  ... remove a message queue handler from the existing list of message 
    ... handlers; 'handlerID must be a valid return value from a previous
    ... call to 'radProcessQueuePrependHandler' (this implies the default 
    ... handler provided in 'radProcessInit' CANNOT be removed)
    ... returns OK or ERROR
*/
extern long radProcessQueueRemoveHandler
(
    long            handlerID
);



/*  *** timer wrappers ***
*/
extern TIMER_ID radProcessTimerCreate
(
    TIMER_ID  timer,
    void      (*routine) (void *parm),
    void      *parm
);
extern void radProcessTimerDelete
(
    TIMER_ID  timer
);
extern void radProcessTimerStart
(
    TIMER_ID  timer,
    ULONG     time
);
extern void radProcessTimerStop
(
    TIMER_ID  timer
);
extern int radProcessTimerStatus   /* TRUE if timer is running */
(
    TIMER_ID  timer
);
extern void radProcessTimerSetUserParm
(
    TIMER_ID  timer,
    void      *newParm
);



/*  *** event wrappers ***
*/
/*  ... add new events to catch;
    ... returns OK or ERROR
*/
extern int radProcessEventsAdd
(
    UINT            newEvents
);

/*  ... remove events from event mask;
    ... returns OK or ERROR
*/
extern int radProcessEventsRemove
(
    UINT            removeEvents
);

/*  ... return the currently enabled events mask
*/
extern UINT radProcessEventsGetEnabled
(
    void
);

/*  ... send an event to another process by queue name;
    ... if radProcessInit was initialized and destName == NULL,
    ...     send the events to the calling process;
    ... returns OK or ERROR
*/
extern int radProcessEventsSend
(
    char            *destName,
    UINT            eventsToSend,
    UINT            data
);

#ifdef __cplusplus
}
#endif
#endif