~ubuntu-branches/ubuntu/precise/libfs/precise-security

« back to all changes in this revision

Viewing changes to src/FSConnServ.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Stone
  • Date: 2005-07-23 00:15:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050723001532-1n80puvkrct21vfl
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Xorg: FSConnServ.c,v 1.4 2001/02/09 02:03:25 xorgcvs Exp $ */
 
2
 
 
3
/*
 
4
 * Copyright 1990 Network Computing Devices;
 
5
 * Portions Copyright 1987 by Digital Equipment Corporation
 
6
 *
 
7
 * Permission to use, copy, modify, distribute, and sell this software 
 
8
 * and its documentation for any purpose is hereby granted without fee, 
 
9
 * provided that the above copyright notice appear in all copies and 
 
10
 * that both that copyright notice and this permission notice appear 
 
11
 * in supporting documentation, and that the names of Network Computing 
 
12
 * Devices or Digital not be used in advertising or publicity pertaining 
 
13
 * to distribution of the software without specific, written prior 
 
14
 * permission. Network Computing Devices or Digital make no representations 
 
15
 * about the suitability of this software for any purpose.  It is provided 
 
16
 * "as is" without express or implied warranty.
 
17
 *
 
18
 * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
 
19
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF 
 
20
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
 
21
 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES 
 
22
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
 
23
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 
 
24
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
 
25
 * SOFTWARE.
 
26
 */
 
27
 
 
28
/*
 
29
 
 
30
Copyright 1987, 1994, 1998  The Open Group
 
31
 
 
32
Permission to use, copy, modify, distribute, and sell this software and its
 
33
documentation for any purpose is hereby granted without fee, provided that
 
34
the above copyright notice appear in all copies and that both that
 
35
copyright notice and this permission notice appear in supporting
 
36
documentation.
 
37
 
 
38
The above copyright notice and this permission notice shall be included in
 
39
all copies or substantial portions of the Software.
 
40
 
 
41
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
42
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
43
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
44
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
45
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
46
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
47
 
 
48
Except as contained in this notice, the name of The Open Group shall not be
 
49
used in advertising or otherwise to promote the sale, use or other dealings
 
50
in this Software without prior written authorization from The Open Group.
 
51
 
 
52
*/
 
53
/* $XFree86: xc/lib/FS/FSConnServ.c,v 3.10 2001/10/28 03:32:27 tsi Exp $ */
 
54
 
 
55
#ifdef HAVE_CONFIG_H
 
56
#include <config.h>
 
57
#endif
 
58
#include        "FSlibint.h"
 
59
#include        <stdio.h>
 
60
#include        "X11/Xpoll.h"
 
61
#ifdef NCD
 
62
#include        <fcntl.h>
 
63
#endif
 
64
#ifdef WIN32
 
65
#define ECHECK(err) (WSAGetLastError() == err)
 
66
#else
 
67
#ifdef ISC
 
68
#define ECHECK(err) ((errno == err) || errno == EAGAIN || errno == EWOULDBLOCK)
 
69
#else
 
70
#define ECHECK(err) (errno == err)
 
71
#endif
 
72
#endif
 
73
 
 
74
/*
 
75
 * Attempts to connect to server, given server name. Returns transport
 
76
 * connection object or NULL if connection fails.
 
77
 */
 
78
 
 
79
#define FS_CONNECTION_RETRIES 5
 
80
 
 
81
XtransConnInfo
 
82
_FSConnectServer(server_name)
 
83
    char       *server_name;
 
84
{
 
85
    XtransConnInfo trans_conn = NULL;   /* transport connection object */
 
86
    int retry, connect_stat;
 
87
    int  madeConnection = 0;
 
88
 
 
89
    /*
 
90
     * Open the network connection.
 
91
     */
 
92
 
 
93
    for (retry = FS_CONNECTION_RETRIES; retry >= 0; retry--)
 
94
    {
 
95
        if ((trans_conn = _FSTransOpenCOTSClient(server_name)) == NULL)
 
96
        {
 
97
            break;
 
98
        }
 
99
 
 
100
        if ((connect_stat = _FSTransConnect(trans_conn,server_name)) < 0)
 
101
        {
 
102
            _FSTransClose(trans_conn);
 
103
 
 
104
            if (connect_stat == TRANS_TRY_CONNECT_AGAIN)
 
105
            {
 
106
                sleep(1);
 
107
                continue;
 
108
            }
 
109
            else
 
110
                break;
 
111
        }
 
112
        else
 
113
        {
 
114
            madeConnection = 1;
 
115
            break;
 
116
        }
 
117
    }
 
118
 
 
119
    if (!madeConnection)
 
120
        return (NULL);
 
121
 
 
122
 
 
123
    /*
 
124
     * set it non-blocking.  This is so we can read data when blocked for
 
125
     * writing in the library.
 
126
     */
 
127
 
 
128
    _FSTransSetOption(trans_conn, TRANS_NONBLOCKING, 1);
 
129
 
 
130
    return (trans_conn);
 
131
}
 
132
 
 
133
/*
 
134
 * Disconnect from server.
 
135
 */
 
136
 
 
137
void
 
138
_FSDisconnectServer(trans_conn)
 
139
    XtransConnInfo      trans_conn;
 
140
 
 
141
{
 
142
    (void) _FSTransClose(trans_conn);
 
143
}
 
144
 
 
145
 
 
146
/*
 
147
 * This is an OS dependent routine which:
 
148
 * 1) returns as soon as the connection can be written on....
 
149
 * 2) if the connection can be read, must enqueue events and handle errors,
 
150
 * until the connection is writable.
 
151
 */
 
152
void _FSWaitForWritable(svr)
 
153
    FSServer     *svr;
 
154
{
 
155
    fd_set      r_mask;
 
156
    fd_set      w_mask;
 
157
    int         nfound;
 
158
 
 
159
    FD_ZERO(&r_mask);
 
160
    FD_ZERO(&w_mask);
 
161
 
 
162
    while (1) {
 
163
        FD_SET(svr->fd, &r_mask);
 
164
        FD_SET(svr->fd, &w_mask);
 
165
 
 
166
        do {
 
167
            nfound = Select(svr->fd + 1, &r_mask, &w_mask, NULL, NULL);
 
168
            if (nfound < 0 && !ECHECK(EINTR))
 
169
                (*_FSIOErrorFunction) (svr);
 
170
        } while (nfound <= 0);
 
171
 
 
172
        if (XFD_ANYSET(&r_mask)) {
 
173
            char        buf[BUFSIZE];
 
174
            BytesReadable_t pend_not_register;
 
175
            register BytesReadable_t pend;
 
176
            register fsEvent *ev;
 
177
 
 
178
            /* find out how much data can be read */
 
179
            if (_FSTransBytesReadable(svr->trans_conn, &pend_not_register) < 0)
 
180
                (*_FSIOErrorFunction) (svr);
 
181
            pend = pend_not_register;
 
182
 
 
183
            /*
 
184
             * must read at least one fsEvent; if none is pending, then we'll
 
185
             * just block waiting for it
 
186
             */
 
187
            if (pend < SIZEOF(fsEvent))
 
188
                pend = SIZEOF(fsEvent);
 
189
 
 
190
            /* but we won't read more than the max buffer size */
 
191
            if (pend > BUFSIZE)
 
192
                pend = BUFSIZE;
 
193
 
 
194
            /* round down to an integral number of FSReps */
 
195
            pend = (pend / SIZEOF(fsEvent)) * SIZEOF(fsEvent);
 
196
 
 
197
            _FSRead(svr, buf, pend);
 
198
 
 
199
            /* no space between comma and type or else macro will die */
 
200
            STARTITERATE(ev, fsEvent, buf, (pend > 0),
 
201
                         (pend -= SIZEOF(fsEvent))) {
 
202
                if (ev->type == FS_Error)
 
203
                    _FSError(svr, (fsError *) ev);
 
204
                else            /* it's an event packet; enqueue it */
 
205
                    _FSEnq(svr, ev);
 
206
            }
 
207
            ENDITERATE
 
208
        }
 
209
        if (XFD_ANYSET(&w_mask))
 
210
            return;
 
211
    }
 
212
}
 
213
 
 
214
 
 
215
void _FSWaitForReadable(svr)
 
216
    FSServer     *svr;
 
217
{
 
218
    fd_set      r_mask;
 
219
    int         result;
 
220
 
 
221
    FD_ZERO(&r_mask);
 
222
    do {
 
223
        FD_SET(svr->fd, &r_mask);
 
224
        result = Select(svr->fd + 1, &r_mask, NULL, NULL, NULL);
 
225
        if (result == -1 && !ECHECK(EINTR))
 
226
            (*_FSIOErrorFunction) (svr);
 
227
    } while (result <= 0);
 
228
}
 
229
 
 
230
void _FSSendClientPrefix(svr, client)
 
231
    FSServer     *svr;
 
232
    fsConnClientPrefix *client;
 
233
{
 
234
    struct iovec iovarray[5],
 
235
               *iov = iovarray;
 
236
    int         niov = 0;
 
237
 
 
238
#define add_to_iov(b,l) \
 
239
          { iov->iov_base = (b); iov->iov_len = (l); iov++, niov++; }
 
240
 
 
241
    add_to_iov((caddr_t) client, SIZEOF(fsConnClientPrefix));
 
242
 
 
243
#undef add_to_iov
 
244
 
 
245
    (void) _FSTransWritev(svr->trans_conn, iovarray, niov);
 
246
    return;
 
247
}