380a5b0f0274aba96172364fb3ebb31444869993
[oweals/cde.git] / cde / programs / dtpdmd / records.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23
24 /******************************************************************************
25  ******************************************************************************
26  **
27  ** File:         records.c
28  ** RCS:          $XConsortium: records.c /main/4 1996/05/28 13:34:16 cde-hp $
29  **
30  ** Description:
31  **
32  ** (c) Copyright 1995, Hewlett-Packard Company, all rights reserved.
33  **
34  ******************************************************************************
35  *****************************************************************************/
36
37 #define RECORDS_DOT_C
38
39 #include "dtpdmdP.h"
40
41
42 /********************************************************************
43  *
44  * Routines to BLOCK and UNBLOCK the signal SIGCLD.
45  *
46  * Use whenever modifying the array of client tracking
47  * records (critical section).  If a SIGCLD happens,
48  * dtpdmd.c:handle_SIGCLD() will be called, and it must
49  * be able to work on a stable set of client tracking
50  * records.
51  */
52 static void block_SIGCLD( void )
53 {
54     sigset_t newset;
55     int      rtn;
56
57     sigemptyset( &newset );
58     sigaddset( &newset, SIGCHLD );
59     rtn = sigprocmask( SIG_BLOCK, &newset, (sigset_t *) NULL );
60 }
61
62 static void unblock_SIGCLD( void )
63 {
64     sigset_t newset;
65     int      rtn;
66
67     sigemptyset( &newset );
68     sigaddset( &newset, SIGCHLD );
69     rtn = sigprocmask( SIG_UNBLOCK, &newset, (sigset_t *) NULL );
70 }
71
72 /********************************************************************
73  *
74  * Try to find a service record based on ID.  Optionally create a
75  * new service record if one is not found.
76  */
77 XpPdmServiceRec *find_rec( Window requestor )
78 {
79     int i;
80     XpPdmServiceRec *r;
81
82
83     if (!requestor)
84         return( (XpPdmServiceRec *) NULL );
85
86     /*
87      * See if the record already exists.
88      */
89     for ( i=0; i < g.serviceRecNum; i++ ) {
90         if ( g.serviceRecs[i]->requestor == requestor )
91             return( g.serviceRecs[i] );
92     }
93
94     /*
95      * Will need to add - see if we need more room in the child
96      * tracking record array.
97      */
98     block_SIGCLD();
99
100     if ( g.serviceRecNum + 1 > g.maxServiceRecNum ) {
101         g.maxServiceRecNum += 5;
102         if ( g.maxServiceRecNum == 5 ) {
103             g.serviceRecs =
104                  (XpPdmServiceRec **) Xmalloc( sizeof(XpPdmServiceRec *) *
105                                                 g.maxServiceRecNum );
106         }
107         else {
108             g.serviceRecs =
109                  (XpPdmServiceRec **) Xrealloc( (char *) g.serviceRecs,
110                                                 sizeof(XpPdmServiceRec *) *
111                                                 g.maxServiceRecNum );
112         }
113     }
114
115     /*
116      * Create a new child tracking record and add to array.
117      */
118     r = (XpPdmServiceRec *) Xmalloc( sizeof(XpPdmServiceRec) );
119
120     g.serviceRecs[g.serviceRecNum] = r;
121
122     memset( (void *) r, NULL, sizeof(XpPdmServiceRec) );    /* cheat NULLing */
123     r->mgr_flag  = False;
124     r->mbox_flag = False;
125
126     r->message_pipe[0] = -1;
127     r->message_pipe[1] = -1;
128
129     g.serviceRecNum++;
130
131     unblock_SIGCLD();
132
133     return( r );
134 }
135
136 /********************************************************************
137  *
138  * Try to find a service record based on previously assigned
139  * mailbox window ID.
140  */
141 XpPdmServiceRec *find_rec_by_mbox_win( Window window )
142 {
143     int i;
144     XpPdmServiceRec *r;
145
146
147     if (!window)
148         return( (XpPdmServiceRec *) NULL );
149
150     /*
151      * See if the record already exists.
152      */
153     for ( i=0; i < g.serviceRecNum; i++ ) {
154         if ( g.serviceRecs[i]->mbox_window == window )
155             return( g.serviceRecs[i] );
156     }
157
158     return( (XpPdmServiceRec *) NULL );
159 }
160
161
162 /********************************************************************
163  *
164  * Delete the specified service record from global memory.
165  */
166 void delete_rec( XpPdmServiceRec *rec )
167 {
168     int i,j;
169
170     block_SIGCLD();
171
172     for ( i=0; i< g.serviceRecNum; i++ ) {
173         if ( g.serviceRecs[i] == rec ) {
174
175             /*
176              * Delete memory for current rec
177              */
178             if (rec->mbox_window)
179                 XDestroyWindow( rec->selection_display, rec->mbox_window );
180
181             unlink( rec->auth_filename );
182
183             Xfree( g.serviceRecs[i]->video_display_str );
184             Xfree( g.serviceRecs[i]->print_display_str );
185             Xfree( g.serviceRecs[i]->locale_hint );
186
187             for ( j = 0; g.serviceRecs[i]->pdm_exec_argvs[j]; j++ )
188                 Xfree( (char *) g.serviceRecs[i]->pdm_exec_argvs[j] );
189             Xfree( (char *) g.serviceRecs[i]->pdm_exec_argvs );
190
191             Xfree( g.serviceRecs[i]->pdm_exec_errormessage );
192
193             Xfree( g.serviceRecs[i]->message_string );
194             Xfree( g.serviceRecs[i]->message_string2 );
195
196             Xfree( g.serviceRecs[i]->in_buf );
197             for ( j=i; j < g.serviceRecs[i]->cookie_cnt; j++ )
198                 Xfree( (char *) g.serviceRecs[i]->cookies[j] );
199             Xfree( (char *) g.serviceRecs[i]->cookies );
200
201             Xfree( (char *) g.serviceRecs[i] );
202
203             /*
204              * Compress list around defunct entry
205              */
206             for ( j=i; j < g.serviceRecNum-1; j++ ) {
207                 g.serviceRecs[j] = g.serviceRecs[j+1];
208             }
209         }
210     }
211
212     g.serviceRecNum--;
213
214     unblock_SIGCLD();
215 }
216