Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygserver_transport_sockets.cc « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ade14bfff60b2ddbfba993659e854404c3b0839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* cygserver_transport_sockets.cc

   Copyright 2001, 2002 Red Hat Inc.

   Written by Robert Collins <rbtcollins@hotmail.com>

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

/* to allow this to link into cygwin and the .dll, a little magic is needed. */
#ifdef __OUTSIDE_CYGWIN__
#include "woutsup.h"
#else
#include "winsup.h"
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#include "cygwin/cygserver_transport.h"
#include "cygwin/cygserver_transport_sockets.h"

/* to allow this to link into cygwin and the .dll, a little magic is needed. */
#ifndef __OUTSIDE_CYGWIN__

extern "C" int cygwin_accept (int fd, struct sockaddr *, int *len);
extern "C" int cygwin_bind (int fd, const struct sockaddr *, int len);
extern "C" int cygwin_connect (int fd, const struct sockaddr *, int len);
extern "C" int cygwin_listen (int fd, int backlog);
extern "C" int cygwin_shutdown (int fd, int how);
extern "C" int cygwin_socket (int af, int type, int protocol);

#else /* __OUTSIDE_CYGWIN__ */

#define cygwin_accept(A,B,C)    ::accept (A,B,C)
#define cygwin_bind(A,B,C)      ::bind (A,B,C)
#define cygwin_connect(A,B,C)   ::connect (A,B,C)
#define cygwin_listen(A,B)      ::listen (A,B)
#define cygwin_shutdown(A,B)    ::shutdown (A,B)
#define cygwin_socket(A,B,C)    ::socket (A,B,C)

#endif /* __OUTSIDE_CYGWIN__ */

enum
  {
    MAX_CONNECT_RETRY = 64
  };

transport_layer_sockets::transport_layer_sockets (const int fd)
  : _fd (fd),
    _addr_len (0),
    _is_accepted_endpoint (true),
    _is_listening_endpoint (false)
{
  assert (_fd != -1);

  memset (&_addr, '\0', sizeof (_addr));
}

transport_layer_sockets::transport_layer_sockets ()
  : _fd (-1),
    _addr_len (0),
    _is_accepted_endpoint (false),
    _is_listening_endpoint (false)
{
  memset (&_addr, '\0', sizeof (_addr));

  _addr.sun_family = AF_UNIX;
  strcpy (_addr.sun_path, "/tmp/cygdaemo"); // FIXME: $TMP?
  _addr_len = SUN_LEN (&_addr);
}

transport_layer_sockets::~transport_layer_sockets ()
{
  close ();
}

#ifndef __INSIDE_CYGWIN__

int
transport_layer_sockets::listen ()
{
  assert (_fd == -1);
  assert (!_is_accepted_endpoint);
  assert (!_is_listening_endpoint);

  debug_printf ("listen () [this = %p]", this);

  struct stat sbuf;

  if (stat (_addr.sun_path, &sbuf) == -1)
    {
      if (errno != ENOENT)
	{
	  system_printf ("cannot access socket file `%s': %s",
			 _addr.sun_path, strerror (errno));
	  return -1;
	}
    }
  else if (S_ISSOCK (sbuf.st_mode))
    {
      // The socket already exists: is a duplicate cygserver running?

      const int newfd = cygwin_socket (AF_UNIX, SOCK_STREAM, 0);

      if (newfd == -1)
	{
	  system_printf ("failed to create UNIX domain socket: %s",
			 strerror (errno));
	  return -1;
	}

      if (cygwin_connect (newfd, (struct sockaddr *) &_addr, _addr_len) == 0)
	{
	  system_printf ("the daemon is already running");
	  (void) cygwin_shutdown (newfd, SHUT_WR);
	  char buf[BUFSIZ];
	  while (::read (newfd, buf, sizeof (buf)) > 0)
	    {}
	  (void) ::close (newfd);
	  return -1;
	}

      if (unlink (_addr.sun_path) == -1)
	{
	  system_printf ("failed to remove `%s': %s",
			 _addr.sun_path, strerror (errno));
	  (void) ::close (newfd);
	  return -1;
	}
    }
  else
    {
      system_printf ("cannot create socket `%s': File already exists",
		     _addr.sun_path);
      return -1;
    }

  _fd = cygwin_socket (AF_UNIX, SOCK_STREAM, 0);

  if (_fd == -1)
    {
      system_printf ("failed to create UNIX domain socket: %s",
		     strerror (errno));
      return -1;
    }

  if (cygwin_bind (_fd, (struct sockaddr *) &_addr, _addr_len) == -1)
    {
      const int saved_errno = errno;
      close ();
      errno = saved_errno;
      system_printf ("failed to bind UNIX domain socket `%s': %s",
		     _addr.sun_path, strerror (errno));
      return -1;
    }

  _is_listening_endpoint = true; // i.e. this really means "have bound".

  if (cygwin_listen (_fd, SOMAXCONN) == -1)
    {
      const int saved_errno = errno;
      close ();
      errno = saved_errno;
      system_printf ("failed to listen on UNIX domain socket `%s': %s",
		     _addr.sun_path, strerror (errno));
      return -1;
    }

  debug_printf ("0 = listen () [this = %p, fd = %d]", this, _fd);

  return 0;
}

class transport_layer_sockets *
transport_layer_sockets::accept (bool *const recoverable)
{
  assert (_fd != -1);
  assert (!_is_accepted_endpoint);
  assert (_is_listening_endpoint);

  debug_printf ("accept () [this = %p, fd = %d]", this, _fd);

  struct sockaddr_un client_addr;
  socklen_t client_addr_len = sizeof (client_addr);

  const int accept_fd =
    cygwin_accept (_fd, (struct sockaddr *) &client_addr, &client_addr_len);

  if (accept_fd == -1)
    {
      system_printf ("failed to accept connection: %s", strerror (errno));
      switch (errno)
	{
	case ECONNABORTED:
	case EINTR:
	case EMFILE:
	case ENFILE:
	case ENOBUFS:
	case ENOMEM:
	  *recoverable = true;
	  break;

	default:
	  *recoverable = false;
	  break;
	}
      return NULL;
    }

  debug_printf ("%d = accept () [this = %p, fd = %d]", accept_fd, this, _fd);

  return safe_new (transport_layer_sockets, accept_fd);
}

#endif /* !__INSIDE_CYGWIN__ */

void
transport_layer_sockets::close ()
{
  debug_printf ("close () [this = %p, fd = %d]", this, _fd);

  if (_is_listening_endpoint)
    (void) unlink (_addr.sun_path);

  if (_fd != -1)
    {
      (void) cygwin_shutdown (_fd, SHUT_WR);
      if (!_is_listening_endpoint)
	{
	  char buf[BUFSIZ];
	  while (::read (_fd, buf, sizeof (buf)) > 0)
	    {}
	}
      (void) ::close (_fd);
      _fd = -1;
    }

  _is_listening_endpoint = false;
}

ssize_t
transport_layer_sockets::read (void *const buf, const size_t buf_len)
{
  assert (_fd != -1);
  assert (!_is_listening_endpoint);

  assert (buf);
  assert (buf_len > 0);

  // verbose: debug_printf ("read (buf = %p, len = %u) [this = %p, fd = %d]",
  //		buf, buf_len, this, _fd);

  char *read_buf = static_cast<char *> (buf);
  size_t read_buf_len = buf_len;
  ssize_t res = 0;

  while (read_buf_len != 0
	 && (res = ::read (_fd, read_buf, read_buf_len)) > 0)
    {
      read_buf += res;
      read_buf_len -= res;

      assert (read_buf_len >= 0);
    }

  if (res != -1)
    {
      if (res == 0)
	errno = EIO;		// FIXME?

      res = buf_len - read_buf_len;
    }

  if (res != static_cast<ssize_t> (buf_len))
    debug_printf ("%d = read (buf = %p, len = %u) [this = %p, fd = %d]: %s",
		  res, buf, buf_len, this, _fd,
		  (res == -1 ? strerror (errno) : "EOF"));
  else
    {
      // verbose: debug_printf ("%d = read (buf = %p, len = %u) [this = %p, fd = %d]",
      //		    res, buf, buf_len, this, _fd);
    }

  return res;
}

ssize_t
transport_layer_sockets::write (void *const buf, const size_t buf_len)
{
  assert (_fd != -1);
  assert (!_is_listening_endpoint);

  assert (buf);
  assert (buf_len > 0);

  // verbose: debug_printf ("write (buf = %p, len = %u) [this = %p, fd = %d]",
  //		buf, buf_len, this, _fd);

  char *write_buf = static_cast<char *> (buf);
  size_t write_buf_len = buf_len;
  ssize_t res = 0;

  while (write_buf_len != 0
	 && (res = ::write (_fd, write_buf, write_buf_len)) > 0)
    {
      write_buf += res;
      write_buf_len -= res;

      assert (write_buf_len >= 0);
    }

  if (res != -1)
    {
      if (res == 0)
	errno = EIO;		// FIXME?

      res = buf_len - write_buf_len;
    }

  if (res != static_cast<ssize_t> (buf_len))
    debug_printf ("%d = write (buf = %p, len = %u) [this = %p, fd = %d]: %s",
		  res, buf, buf_len, this, _fd,
		  (res == -1 ? strerror (errno) : "EOF"));
  else
    {
      // verbose: debug_printf ("%d = write (buf = %p, len = %u) [this = %p, fd = %d]",
      //		    res, buf, buf_len, this, _fd);
    }

  return res;
}

int
transport_layer_sockets::connect ()
{
  assert (_fd == -1);
  assert (!_is_accepted_endpoint);
  assert (!_is_listening_endpoint);

  static bool assume_cygserver = false;

  debug_printf ("connect () [this = %p]", this);

  for (int retries = 0; retries != MAX_CONNECT_RETRY; retries++)
    {
      _fd = cygwin_socket (AF_UNIX, SOCK_STREAM, 0);

      if (_fd == -1)
	{
	  system_printf ("failed to create UNIX domain socket: %s",
			 strerror (errno));
	  return -1;
	}

      if (cygwin_connect (_fd, (struct sockaddr *) &_addr, _addr_len) == 0)
	{
	  assume_cygserver = true;
	  debug_printf ("0 = connect () [this = %p, fd = %d]", this, _fd);
	  return 0;
	}

      if (!assume_cygserver || errno != ECONNREFUSED)
	{
	  debug_printf ("failed to connect to server: %s", strerror (errno));
	  (void) ::close (_fd);
	  _fd = -1;
	  return -1;
	}

      (void) ::close (_fd);
      _fd = -1;
      Sleep (0);		// Give the server a chance.
    }

  debug_printf ("failed to connect to server: %s", strerror (errno));
  return -1;
}