SmConMgmt.c 9.0 KB

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