~brianaker/libmemcached/1062704

« back to all changes in this revision

Viewing changes to libmemcached/connect.cc

  • Committer: Brian Aker
  • Date: 2012-10-06 13:05:23 UTC
  • Revision ID: brian@tangent.org-20121006130523-iapc7d1uh96tohx5
Clean up SOCK_NONBLOCK

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
#include <sys/time.h>
44
44
 
45
45
#ifndef SOCK_CLOEXEC 
46
 
#define SOCK_CLOEXEC 0
 
46
#  define SOCK_CLOEXEC 0
 
47
#endif
 
48
 
 
49
#ifndef SOCK_NONBLOCK 
 
50
#  define SOCK_NONBLOCK 0
 
51
#endif
 
52
 
 
53
#ifndef FD_CLOEXEC
 
54
# define FD_CLOEXEC 0
47
55
#endif
48
56
 
49
57
static memcached_return_t connect_poll(org::libmemcached::Instance* server)
245
253
#else
246
254
  int flags;
247
255
 
248
 
  do
249
 
  {
250
 
    flags= fcntl(server->fd, F_GETFL, 0);
251
 
  } while (flags == -1 && (errno == EINTR || errno == EAGAIN));
252
 
 
253
 
  if (flags == -1)
254
 
  {
255
 
    memcached_set_errno(*server, errno, NULL);
256
 
  }
257
 
  else if ((flags & O_NONBLOCK) == 0)
258
 
  {
259
 
    int rval;
260
 
 
 
256
  if (SOCK_NONBLOCK == 0)
 
257
  {
261
258
    do
262
259
    {
263
 
      rval= fcntl(server->fd, F_SETFL, flags | O_NONBLOCK);
264
 
    } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
 
260
      flags= fcntl(server->fd, F_GETFL, 0);
 
261
    } while (flags == -1 && (errno == EINTR || errno == EAGAIN));
265
262
 
266
 
    if (rval == -1)
 
263
    if (flags == -1)
267
264
    {
268
265
      memcached_set_errno(*server, errno, NULL);
269
266
    }
 
267
    else if ((flags & O_NONBLOCK) == 0)
 
268
    {
 
269
      int rval;
 
270
 
 
271
      do
 
272
      {
 
273
        rval= fcntl(server->fd, F_SETFL, flags | O_NONBLOCK);
 
274
      } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
 
275
 
 
276
      if (rval == -1)
 
277
      {
 
278
        memcached_set_errno(*server, errno, NULL);
 
279
      }
 
280
    }
270
281
  }
271
282
#endif
272
283
}
386
397
    assert(error == 0);
387
398
  }
388
399
 
389
 
 
390
400
  /* libmemcached will always use nonblocking IO to avoid write deadlocks */
391
401
  set_socket_nonblocking(server);
392
402
}
396
406
#ifndef WIN32
397
407
  WATCHPOINT_ASSERT(server->fd == INVALID_SOCKET);
398
408
 
399
 
  if ((server->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
 
409
  int type= SOCK_STREAM;
 
410
  if (SOCK_CLOEXEC)
 
411
  {
 
412
    type|= SOCK_CLOEXEC;
 
413
  }
 
414
 
 
415
  if (SOCK_NONBLOCK)
 
416
  {
 
417
    type|= SOCK_NONBLOCK;
 
418
  }
 
419
 
 
420
  if ((server->fd= socket(AF_UNIX, type, 0)) < 0)
400
421
  {
401
422
    memcached_set_errno(*server, errno, NULL);
402
423
    return MEMCACHED_CONNECTION_FAILURE;
481
502
    }
482
503
 
483
504
    int type= server->address_info_next->ai_socktype;
484
 
    if (HAVE_SOCK_CLOEXEC)
 
505
    if (SOCK_CLOEXEC)
485
506
    {
486
507
      type|= SOCK_CLOEXEC;
487
508
    }
488
509
 
 
510
    if (SOCK_NONBLOCK)
 
511
    {
 
512
      type|= SOCK_NONBLOCK;
 
513
    }
 
514
 
489
515
    if ((server->fd= socket(server->address_info_next->ai_family,
490
516
                            type,
491
517
                            server->address_info_next->ai_protocol)) < 0)
493
519
      return memcached_set_errno(*server, get_socket_errno(), NULL);
494
520
    }
495
521
 
496
 
    if (HAVE_SOCK_CLOEXEC == 0)
 
522
    // If SOCK_CLOEXEC exists then we don't need to call the following
 
523
    if (SOCK_CLOEXEC == 0)
497
524
    {
498
 
#ifdef FD_CLOEXEC
499
 
      int rval;
500
 
      do
 
525
      if (FD_CLOEXEC)
501
526
      {
502
 
        rval= fcntl (server->fd, F_SETFD, FD_CLOEXEC);
503
 
      } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
504
 
#endif
 
527
        int rval;
 
528
        do
 
529
        {
 
530
          rval= fcntl (server->fd, F_SETFD, FD_CLOEXEC);
 
531
        } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
 
532
      }
505
533
    }
506
534
 
507
535
    set_socket_options(server);