dtcm: Coverity 88675
[oweals/cde.git] / cde / programs / dtsession / SmHftRing.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 /* $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
45 #include <stdio.h>
46 #include <sys/hft.h>
47 #include <fcntl.h>
48 #include "Sm.h"
49
50 #include <X11/Xosdefs.h>
51 #include <errno.h>
52 #ifdef X_NOT_STDC_ENV
53 extern int errno;
54 #endif
55
56 /*
57  * Local functions.
58  */
59
60 static int open_hft_file(char *);
61 static void close_hft_file(int);
62 static int get_hft_ring(void);
63 static int process_hft(int, unsigned int);
64 static int scan_hft_ring(int);
65 static int my_strpos(char *, char);
66 static int is_local_display(void);
67
68 #define HFTRFILE      "/dev/hft/0"
69 #define HFQSTAT       hftqstat
70 #define HFSMGRCMD     hftsmgrcmd
71 #define HFQEIO        HFQERROR
72 #define HFCSMGR       HFTCSMGR
73 #define HFQSMGR       HFTQSMGR
74 #define HFVTSTATUS    (HFVTACTIVE | HFVTNOHOTKEY)
75 #define HF_VTIODN     hf_vtid
76
77 /*
78  * Global variables.
79  */
80 int                   hft_file;
81 struct HFQSTAT        hft_ring;
82
83 static int
84 open_hft_file(char *hft_file_name)
85 {
86   return(open(hft_file_name, O_RDONLY));
87 }
88
89 static void
90 close_hft_file(int hft_file)
91 {
92   close(hft_file); 
93 }
94
95 static int
96 get_hft_ring(void)
97 {
98   int rc = -1;
99   struct hfbuf hft_buffer;
100
101   hft_buffer.hf_bufp = (char *)&hft_ring;
102   hft_buffer.hf_buflen = sizeof(hft_ring);
103
104
105  /*
106   * Open the hft queue manager.
107   */ 
108   if ((hft_file = open_hft_file(HFTRFILE)) >= 0)
109   {
110     rc = ioctl(hft_file, HFQSMGR, &hft_buffer);
111     close_hft_file(hft_file);
112   }
113
114   return(rc);
115 }
116
117 static int
118 process_hft(int hft_action, unsigned int hft_vtid)
119 {
120   struct HFSMGRCMD hft_manager;
121   char hft_file_name[20];
122   int rc = -1;
123
124   hft_manager.hf_cmd = hft_action;
125   hft_manager.hf_vtid = hft_vtid;
126
127   sprintf(hft_file_name, "/dev/hft/%u", hft_vtid);
128   if ((hft_file = open_hft_file(hft_file_name)) >= 0)
129   {
130     rc = ioctl(hft_file, HFCSMGR, &hft_manager);
131     close_hft_file(hft_file);  
132   }
133
134   return(rc);
135 }
136
137 static int
138 scan_hft_ring(int hft_action)
139 {
140   int hft;
141   int rc = 0;
142
143   if (get_hft_ring() != 0)
144   {
145     return(-1);
146   }
147
148   for (hft = 0; hft < hft_ring.hf_numvts; hft++)
149   {
150     if (!(hft_ring.hf_vtinfo[hft].hf_vtstate & HFVTSTATUS))
151     {
152       if (process_hft(hft_action, hft_ring.hf_vtinfo[hft].HF_VTIODN) != 0)
153       {
154         return(-1);
155       }
156     }
157   }  
158   return(0);
159 }
160
161 static int
162 my_strpos(char *s, char c)
163 {
164   char *t;
165
166   return((t=strchr(s,c)) == NULL ? -1 : t - s);
167 }
168
169 static int 
170 is_local_display(void)
171 {
172   static int is_local = -1; /* display status */
173   char *dpyname; /* display name */
174   int n; /* position of colon char */
175   char hostname[MAXHOSTNAMELEN]; /* host name */
176
177   if (is_local == -1)
178   {
179     is_local =
180       (((dpyname = getenv("DISPLAY")) != NULL) &&  /* name not null */
181        ((n = my_strpos(dpyname, ':')) != -1) && /* name has a colon */
182        ((n == 0) || /* name is :n */
183         (strncmp(dpyname, "unix", n) == 0) || /* name is unix:n */
184         (strncmp(dpyname, "localhost", n) == 0) || /* name is localhost:n */
185         ((gethostname(hostname, MAXHOSTNAMELEN) == 0) && /* hostname ok */
186          (strncmp(dpyname, hostname, n) == 0)))); /* name == hostname */
187   }
188    
189   return(is_local);
190 }
191
192 void
193 AixEnableHftRing(int enable)
194 {
195   if (is_local_display())
196   {
197     scan_hft_ring(enable ? SMUNHIDE : SMHIDE);
198   }
199 }