SmHftRing.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /* $XConsortium: SmHftRing.c /main/5 1996/11/01 10:17:15 drk $ */
  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: SmHftRing.c
  34. **
  35. ** Project: DT Session Manager (dtsession)
  36. **
  37. ** Description:
  38. ** -----------
  39. ** This file contains the AIX 3.2.x specific code required to enable and
  40. ** disable the HFT ring. This is used for session locking.
  41. **
  42. *****************************************************************************
  43. *************************************<+>*************************************/
  44. #include <stdio.h>
  45. #include <sys/hft.h>
  46. #include <fcntl.h>
  47. #include "Sm.h"
  48. #include <X11/Xosdefs.h>
  49. #include <errno.h>
  50. #ifdef X_NOT_STDC_ENV
  51. extern int errno;
  52. #endif
  53. /*
  54. * Local functions.
  55. */
  56. static int open_hft_file(char *);
  57. static void close_hft_file(int);
  58. static int get_hft_ring(void);
  59. static int process_hft(int, unsigned int);
  60. static int scan_hft_ring(int);
  61. static int my_strpos(char *, char);
  62. static int is_local_display(void);
  63. #define HFTRFILE "/dev/hft/0"
  64. #define HFQSTAT hftqstat
  65. #define HFSMGRCMD hftsmgrcmd
  66. #define HFQEIO HFQERROR
  67. #define HFCSMGR HFTCSMGR
  68. #define HFQSMGR HFTQSMGR
  69. #define HFVTSTATUS (HFVTACTIVE | HFVTNOHOTKEY)
  70. #define HF_VTIODN hf_vtid
  71. /*
  72. * Global variables.
  73. */
  74. int hft_file;
  75. struct HFQSTAT hft_ring;
  76. static int
  77. open_hft_file(char *hft_file_name)
  78. {
  79. return(open(hft_file_name, O_RDONLY));
  80. }
  81. static void
  82. close_hft_file(int hft_file)
  83. {
  84. close(hft_file);
  85. }
  86. static int
  87. get_hft_ring(void)
  88. {
  89. int rc = -1;
  90. struct hfbuf hft_buffer;
  91. hft_buffer.hf_bufp = (char *)&hft_ring;
  92. hft_buffer.hf_buflen = sizeof(hft_ring);
  93. /*
  94. * Open the hft queue manager.
  95. */
  96. if ((hft_file = open_hft_file(HFTRFILE)) >= 0)
  97. {
  98. rc = ioctl(hft_file, HFQSMGR, &hft_buffer);
  99. close_hft_file(hft_file);
  100. }
  101. return(rc);
  102. }
  103. static int
  104. process_hft(int hft_action, unsigned int hft_vtid)
  105. {
  106. struct HFSMGRCMD hft_manager;
  107. char hft_file_name[20];
  108. int rc = -1;
  109. hft_manager.hf_cmd = hft_action;
  110. hft_manager.hf_vtid = hft_vtid;
  111. sprintf(hft_file_name, "/dev/hft/%u", hft_vtid);
  112. if ((hft_file = open_hft_file(hft_file_name)) >= 0)
  113. {
  114. rc = ioctl(hft_file, HFCSMGR, &hft_manager);
  115. close_hft_file(hft_file);
  116. }
  117. return(rc);
  118. }
  119. static int
  120. scan_hft_ring(int hft_action)
  121. {
  122. int hft;
  123. int rc = 0;
  124. if (get_hft_ring() != 0)
  125. {
  126. return(-1);
  127. }
  128. for (hft = 0; hft < hft_ring.hf_numvts; hft++)
  129. {
  130. if (!(hft_ring.hf_vtinfo[hft].hf_vtstate & HFVTSTATUS))
  131. {
  132. if (process_hft(hft_action, hft_ring.hf_vtinfo[hft].HF_VTIODN) != 0)
  133. {
  134. return(-1);
  135. }
  136. }
  137. }
  138. return(0);
  139. }
  140. static int
  141. my_strpos(char *s, char c)
  142. {
  143. char *t;
  144. return((t=strchr(s,c)) == NULL ? -1 : t - s);
  145. }
  146. static int
  147. is_local_display(void)
  148. {
  149. static int is_local = -1; /* display status */
  150. char *dpyname; /* display name */
  151. int n; /* position of colon char */
  152. char hostname[MAXHOSTNAMELEN]; /* host name */
  153. if (is_local == -1)
  154. {
  155. is_local =
  156. (((dpyname = getenv("DISPLAY")) != NULL) && /* name not null */
  157. ((n = my_strpos(dpyname, ':')) != -1) && /* name has a colon */
  158. ((n == 0) || /* name is :n */
  159. (strncmp(dpyname, "unix", n) == 0) || /* name is unix:n */
  160. (strncmp(dpyname, "localhost", n) == 0) || /* name is localhost:n */
  161. ((gethostname(hostname, MAXHOSTNAMELEN) == 0) && /* hostname ok */
  162. (strncmp(dpyname, hostname, n) == 0)))); /* name == hostname */
  163. }
  164. return(is_local);
  165. }
  166. void
  167. AixEnableHftRing(int enable)
  168. {
  169. if (is_local_display())
  170. {
  171. scan_hft_ring(enable ? SMUNHIDE : SMHIDE);
  172. }
  173. }