Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtpdmd / records.c
1
2 /******************************************************************************
3  ******************************************************************************
4  **
5  ** File:         records.c
6  ** RCS:          $XConsortium: records.c /main/4 1996/05/28 13:34:16 cde-hp $
7  **
8  ** Description:
9  **
10  ** (c) Copyright 1995, Hewlett-Packard Company, all rights reserved.
11  **
12  ******************************************************************************
13  *****************************************************************************/
14
15 #define RECORDS_DOT_C
16
17 #include "dtpdmdP.h"
18
19
20 /********************************************************************
21  *
22  * Routines to BLOCK and UNBLOCK the signal SIGCLD.
23  *
24  * Use whenever modifying the array of client tracking
25  * records (critical section).  If a SIGCLD happens,
26  * dtpdmd.c:handle_SIGCLD() will be called, and it must
27  * be able to work on a stable set of client tracking
28  * records.
29  */
30 static void block_SIGCLD( void )
31 {
32     sigset_t newset;
33     int      rtn;
34
35     sigemptyset( &newset );
36     sigaddset( &newset, SIGCLD );
37     rtn = sigprocmask( SIG_BLOCK, &newset, (sigset_t *) NULL );
38 }
39
40 static void unblock_SIGCLD( void )
41 {
42     sigset_t newset;
43     int      rtn;
44
45     sigemptyset( &newset );
46     sigaddset( &newset, SIGCLD );
47     rtn = sigprocmask( SIG_UNBLOCK, &newset, (sigset_t *) NULL );
48 }
49
50 /********************************************************************
51  *
52  * Try to find a service record based on ID.  Optionally create a
53  * new service record if one is not found.
54  */
55 XpPdmServiceRec *find_rec( Window requestor )
56 {
57     int i;
58     XpPdmServiceRec *r;
59
60
61     if (!requestor)
62         return( (XpPdmServiceRec *) NULL );
63
64     /*
65      * See if the record already exists.
66      */
67     for ( i=0; i < g.serviceRecNum; i++ ) {
68         if ( g.serviceRecs[i]->requestor == requestor )
69             return( g.serviceRecs[i] );
70     }
71
72     /*
73      * Will need to add - see if we need more room in the child
74      * tracking record array.
75      */
76     block_SIGCLD();
77
78     if ( g.serviceRecNum + 1 > g.maxServiceRecNum ) {
79         g.maxServiceRecNum += 5;
80         if ( g.maxServiceRecNum == 5 ) {
81             g.serviceRecs =
82                  (XpPdmServiceRec **) Xmalloc( sizeof(XpPdmServiceRec *) *
83                                                 g.maxServiceRecNum );
84         }
85         else {
86             g.serviceRecs =
87                  (XpPdmServiceRec **) Xrealloc( (char *) g.serviceRecs,
88                                                 sizeof(XpPdmServiceRec *) *
89                                                 g.maxServiceRecNum );
90         }
91     }
92
93     /*
94      * Create a new child tracking record and add to array.
95      */
96     r = (XpPdmServiceRec *) Xmalloc( sizeof(XpPdmServiceRec) );
97
98     g.serviceRecs[g.serviceRecNum] = r;
99
100     memset( (void *) r, NULL, sizeof(XpPdmServiceRec) );    /* cheat NULLing */
101     r->mgr_flag  = False;
102     r->mbox_flag = False;
103
104     r->message_pipe[0] = -1;
105     r->message_pipe[1] = -1;
106
107     g.serviceRecNum++;
108
109     unblock_SIGCLD();
110
111     return( r );
112 }
113
114 /********************************************************************
115  *
116  * Try to find a service record based on previously assigned
117  * mailbox window ID.
118  */
119 XpPdmServiceRec *find_rec_by_mbox_win( Window window )
120 {
121     int i;
122     XpPdmServiceRec *r;
123
124
125     if (!window)
126         return( (XpPdmServiceRec *) NULL );
127
128     /*
129      * See if the record already exists.
130      */
131     for ( i=0; i < g.serviceRecNum; i++ ) {
132         if ( g.serviceRecs[i]->mbox_window == window )
133             return( g.serviceRecs[i] );
134     }
135
136     return( (XpPdmServiceRec *) NULL );
137 }
138
139
140 /********************************************************************
141  *
142  * Delete the specified service record from global memory.
143  */
144 void delete_rec( XpPdmServiceRec *rec )
145 {
146     int i,j;
147
148     block_SIGCLD();
149
150     for ( i=0; i< g.serviceRecNum; i++ ) {
151         if ( g.serviceRecs[i] == rec ) {
152
153             /*
154              * Delete memory for current rec
155              */
156             if (rec->mbox_window)
157                 XDestroyWindow( rec->selection_display, rec->mbox_window );
158
159             unlink( rec->auth_filename );
160
161             Xfree( g.serviceRecs[i]->video_display_str );
162             Xfree( g.serviceRecs[i]->print_display_str );
163             Xfree( g.serviceRecs[i]->locale_hint );
164
165             for ( j = 0; g.serviceRecs[i]->pdm_exec_argvs[j]; j++ )
166                 Xfree( (char *) g.serviceRecs[i]->pdm_exec_argvs[j] );
167             Xfree( (char *) g.serviceRecs[i]->pdm_exec_argvs );
168
169             Xfree( g.serviceRecs[i]->pdm_exec_errormessage );
170
171             Xfree( g.serviceRecs[i]->message_string );
172             Xfree( g.serviceRecs[i]->message_string2 );
173
174             Xfree( g.serviceRecs[i]->in_buf );
175             for ( j=i; j < g.serviceRecs[i]->cookie_cnt; j++ )
176                 Xfree( (char *) g.serviceRecs[i]->cookies[j] );
177             Xfree( (char *) g.serviceRecs[i]->cookies );
178
179             Xfree( (char *) g.serviceRecs[i] );
180
181             /*
182              * Compress list around defunct entry
183              */
184             for ( j=i; j < g.serviceRecNum-1; j++ ) {
185                 g.serviceRecs[j] = g.serviceRecs[j+1];
186             }
187         }
188     }
189
190     g.serviceRecNum--;
191
192     unblock_SIGCLD();
193 }
194