libDtSearch: Remove optional code for NO_DBN which is not used on CDE
[oweals/cde.git] / cde / programs / dtsession / SmConMgmt.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 libraries 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 /* $TOG: SmConMgmt.c /main/6 1998/04/06 14:35:42 mgreess $ */
24 /*                                                                      *
25  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
26  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
27  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
28  * (c) Copyright 1993, 1994 Novell, Inc.                                *
29  */
30
31 /*************************************<+>*************************************
32  *****************************************************************************
33  **
34  **  File:        SmConMgmt.c
35  **
36  **  Project:     HP DT Session Manager (dtsession)
37  **
38  **  Description:
39  **  -----------
40  **      Contains all code which performs contention management functionality
41  **
42  **
43  *******************************************************************
44  **  (c) Copyright Hewlett-Packard Company, 1990.  All rights are  
45  **  reserved.  Copying or other reproduction of this program      
46  **  except for archival purposes is prohibited without prior      
47  **  written consent of Hewlett-Packard Company.                     
48  ********************************************************************
49  **
50  **
51  **
52  *****************************************************************************
53  *************************************<+>*************************************/
54
55
56 #include <stdio.h>
57 #include <fcntl.h>
58 #if !defined(__linux__)
59 #include <nlist.h>
60 #endif
61 #include <X11/Intrinsic.h>
62 #include "Sm.h"
63 #include "SmProtocol.h"
64 #include "SmConMgmt.h"
65 #include "SmCommun.h"
66
67 /*
68  * Define statements
69  */
70
71 #ifdef mips
72 #include <sys/fixpoint.h>
73 #endif /* mips */
74
75 #ifndef KMEM_FILE
76 #define KMEM_FILE "/dev/kmem"
77 #endif
78
79 #ifndef KERNEL_FILE
80 # ifdef mips
81 #  define KERNEL_FILE "/vmunix"
82 # else /* not mips */
83 #  define KERNEL_FILE "/hp-ux"
84 # endif /* mips */
85 #endif
86
87 #ifdef mips
88 #define GPGSLIM "gpgslim"
89 #define FREEMEM "freemem"
90 #else /* not mips */
91 # ifdef __hpux
92 #  ifdef __hp9000s800
93 #   define GPGSLIM "gpgslim"
94 #   define FREEMEM "freemem"
95 #  endif /* __hp9000s800 */
96 # endif /* __hpux */
97 #endif /* mips */
98
99 #ifndef GPGSLIM
100 #define GPGSLIM "_gpgslim"
101 #endif /* not defined GPGSLIM */
102
103 #ifndef FREEMEM
104 #define FREEMEM "_freemem"
105 #endif /* not defined FREEMEM */
106
107
108 int clientRunning;
109 /*
110  * Variables global to this module only
111  */
112 #if !defined(__linux__)
113 static struct nlist namelist[3];
114 #endif
115 static int freemem_loc, gpgslim_loc, gpgslim, freemem;
116 static int clientTimeout;
117
118 /*
119  * Functions local to this module
120  */
121
122 static void HandleClientMessage(Widget smWidget, XtPointer dummy, 
123                                 XEvent *event);
124 static void WaitClientTimeout(XtPointer, XtIntervalId *);
125
126
127 \f
128 /*************************************<->*************************************
129  *
130  *  GetMemoryUtilization ()
131  *
132  *
133  *  Description:
134  *  -----------
135  *  Returns 1 of 3 values.  Tells the calling program that memory is not
136  *  available, that it is full (paging has started), or that it is not
137  *  full.
138  *
139  *
140  *  Inputs:
141  *  ------
142  *
143  * 
144  *  Outputs:
145  *  -------
146  *  An integer value representing the current memory utilization of the
147  *  system.
148  *
149  *
150  *  Comments:
151  *  --------
152  *  WARNING:  This routine, by its very nature is non-portable.  It depends
153  *  on an OS having some kind of access to its memory utilization.
154  *
155  *  DOUBLE WARNING: The mips architecture code has never been tested.
156  *  
157  * 
158  *************************************<->***********************************/
159
160 int 
161 GetMemoryUtilization(void)
162 {
163 #if !defined(__linux__)
164     static int init = 0;
165     static int kmem;
166 #if !defined(SVR4) && !defined(hpV4) && !defined(_POWER) && !defined(CSRG_BASED)
167     extern void nlist();
168 #endif
169     int i;
170
171
172 #ifdef __hpux
173     setresgid(-1, smGD.conMgmtGID, -1);
174 #else
175 #ifndef SVR4
176     setregid(smGD.runningGID, smGD.conMgmtGID);
177 #else
178     setgid(smGD.runningGID);
179     setegid(smGD.conMgmtGID);
180 #endif
181 #endif
182
183     if(!init)
184     {
185         namelist[0].n_name = FREEMEM;
186         namelist[1].n_name = GPGSLIM;
187         namelist[2].n_name = (char *) NULL;
188         nlist( KERNEL_FILE, namelist);
189         for(i = 0; i < 2; i++)
190         {
191             if (namelist[i].n_type == 0 ||
192                 namelist[i].n_value == 0)
193             {
194 #ifdef __hpux
195                 setresgid(-1, smGD.runningGID, -1);
196 #else
197 #ifndef SVR4
198                 setregid(smGD.conMgmtGID, smGD.runningGID);
199 #else
200                 setgid(smGD.conMgmtGID);
201                 setegid(smGD.runningGID);
202 #endif
203 #endif
204                 return(MEM_NOT_AVAILABLE);
205             }
206         }
207         
208         freemem_loc =  namelist[0].n_value;
209         gpgslim_loc =  namelist[1].n_value;
210         
211         kmem = open(KMEM_FILE, O_RDONLY);
212         if (kmem < 0)
213         {
214 #ifdef __hpux
215             setresgid(-1, smGD.runningGID, -1);
216 #else
217 #ifndef SVR4
218             setregid(smGD.conMgmtGID, smGD.runningGID);
219 #else
220             setgid(smGD.conMgmtGID);
221             setegid(smGD.runningGID);
222 #endif
223 #endif
224             return(MEM_NOT_AVAILABLE);
225         }
226         (void) lseek(kmem, gpgslim_loc, 0);
227 #ifdef mips
228         {
229             fix temp;
230             (void) read(kmem, (char *)&temp, sizeof(fix));
231             gpgslim = FIX_TO_DBL(temp);
232         }
233 #else /* not mips */
234         (void) read(kmem, (char *)&gpgslim, sizeof(int));
235 #endif /* mips */
236         init = 1;
237     }
238     
239     (void) lseek(kmem, freemem_loc, 0);
240 #ifdef mips
241     {
242         fix temp;
243         (void) read(kmem, (char *)&temp, sizeof(fix));
244         freemem = FIX_TO_DBL(temp);
245     }
246 #else /* not mips */
247     (void) read(kmem, (char *)&freemem, sizeof(int));
248 #endif /* mips */
249
250 #ifdef __hpux
251     setresgid(-1, smGD.runningGID, -1);
252 #else
253 #ifndef SVR4
254     setregid(smGD.conMgmtGID, smGD.runningGID);
255 #else
256     setgid(smGD.conMgmtGID);
257     setegid(smGD.runningGID);
258 #endif
259 #endif
260
261     if(freemem >= gpgslim)
262     {
263         return(MEM_NOT_FULL);
264     }
265     else
266     {
267         return(MEM_FULL);
268     }
269 #else /* linux */
270     return(MEM_NOT_FULL);
271 #endif /* linux */
272 }
273
274
275 \f
276 /*************************************<->*************************************
277  *
278  *  WaitForClientMap ()
279  *
280  *
281  *  Description:
282  *  -----------
283  *  This routine waits for the workspace manager to send it information
284  *  about a client being mapped, before returning to start the next client
285  *
286  *
287  *  Inputs:
288  *  ------
289  *
290  * 
291  *  Outputs:
292  *  -------
293  *
294  *  Comments:
295  *  --------
296  * 
297  *************************************<->***********************************/
298 void 
299 WaitForClientMap( void )
300 {
301     XtInputMask         isThere;
302     XEvent              event;
303     XClientMessageEvent *cEvent = (XClientMessageEvent *) &event;
304     XtIntervalId        clientTimerId;
305     
306     XtAddEventHandler(smGD.topLevelWid,
307                       0,
308                       True,
309                       (XtEventHandler)HandleClientMessage,
310                       (XtPointer) NULL);
311     
312     /*
313      * Set a timer which stops the block on waiting for the
314      * client to start.  This value is fetched from the 
315      * waitClientTimeout resource.
316      */
317     clientRunning = False;
318     clientTimeout = False;
319     clientTimerId = XtAppAddTimeOut(smGD.appCon, 
320                                     smRes.waitClientTimeout,
321                                     WaitClientTimeout, NULL);
322     
323     while((clientRunning == False) && (clientTimeout == False))
324     {
325         XtAppProcessEvent(smGD.appCon, XtIMAll);
326     }
327     
328     XtRemoveTimeOut(clientTimerId);
329     XtRemoveEventHandler(smGD.topLevelWid,
330                       0,
331                       True,
332                       (XtEventHandler)HandleClientMessage,
333                       (XtPointer) NULL);
334     return;
335 }
336
337 \f
338 /*************************************<->*************************************
339  *
340  *  WaitClientTimeout
341  *
342  *
343  *  Description:
344  *  -----------
345  *  Timeout procedure the WaitForClientMap routine.  It stops a loop waiting
346  *  for the last started app to get mapped.
347  *
348  *
349  *  Inputs:
350  *  ------
351  *
352  * 
353  *  Outputs:
354  *  -------
355  *  clientTimeout = (global) flag that stops the loop
356  *
357  *  Comments:
358  *  --------
359  * 
360  *************************************<->***********************************/
361 static void 
362 WaitClientTimeout(
363                   XtPointer client_data,
364                   XtIntervalId *id )
365 {
366     clientTimeout = True;
367     return;
368 }
369
370 \f
371 /*************************************<->*************************************
372  *
373  *  HandleClientMessage
374  *
375  *
376  *  Description:
377  *  -----------
378  *  This is the event handler registered to receive the client message
379  *  from dtwm when a client has beem managed
380  *
381  * 
382  *************************************<->***********************************/
383 static void 
384 HandleClientMessage( Widget smWidget,
385                     XtPointer dummy,
386                     XEvent *event)
387 {
388     if (event->type == ClientMessage)
389     {
390         ProcessClientMessage(event);
391     }
392     return;
393 } /* END OF FUNCTION HandleClientMessage */
394
395