Changeset 727

Show
Ignore:
Timestamp:
02/27/08 05:54:16 (6 months ago)
Author:
dsallings
Message:

Merge commit 'trunk' into lbinary as of r726

Conflicts:

server/memcached.c

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/binary/server/memcached.c

    r725 r727  
    6666static void drive_machine(conn *c); 
    6767static int new_socket(struct addrinfo *ai); 
    68 static int *server_socket(const int port, const int prot, int *count); 
     68static int server_socket(const int port, const int prot); 
    6969static int try_read_command(conn *c); 
    7070static int try_read_network(conn *c); 
     
    29872987 
    29882988 
    2989 static int *server_socket(const int port, const int prot, int *count) { 
     2989static int server_socket(const int port, const int prot) { 
    29902990    int sfd; 
    2991     int *sfd_list; 
    2992     int *sfd_ptr; 
    29932991    struct linger ling = {0, 0}; 
    29942992    struct addrinfo *ai; 
     
    30263024        perror("getaddrinfo()"); 
    30273025 
    3028       return NULL; 
    3029     } 
    3030  
    3031     for (*count= 1, next= ai; next->ai_next; next= next->ai_next, (*count)++); 
    3032  
    3033     sfd_list= (int *)calloc(*count, sizeof(int)); 
    3034     if (sfd_list == NULL) { 
    3035         fprintf(stderr, "calloc()\n"); 
    3036         return NULL; 
    3037     } 
    3038     memset(sfd_list, -1, sizeof(int) * (*count)); 
    3039  
    3040     for (sfd_ptr= sfd_list, next= ai; next; next= next->ai_next, sfd_ptr++) { 
     3026      return 1; 
     3027    } 
     3028 
     3029    for (next= ai; next; next= next->ai_next) { 
     3030        conn *listen_conn_add; 
    30413031        if ((sfd = new_socket(next)) == -1) { 
    3042             free(sfd_list); 
    30433032            freeaddrinfo(ai); 
    3044             return NULL
     3033            return 1
    30453034        } 
    30463035 
     
    30573046            if (errno != EADDRINUSE) { 
    30583047                perror("bind()"); 
    3059                 /* If we are not at the first element, loop back and close all sockets */ 
    3060                 if (sfd_ptr != sfd_list) { 
    3061                     do { 
    3062                         --sfd_ptr; 
    3063                         close(*sfd_ptr); 
    3064                     } while (sfd_ptr != sfd_list); 
    3065                 } 
    30663048                close(sfd); 
    30673049                freeaddrinfo(ai); 
    3068                 free(sfd_list); 
    3069                 return NULL; 
     3050                return 1; 
    30703051            } 
    30713052            close(sfd); 
    3072             *sfd_ptr= -1; 
    30733053        } else { 
    30743054          success++; 
    3075           *sfd_ptr= sfd; 
    30763055          if (!IS_UDP(prot) && listen(sfd, 1024) == -1) { 
    30773056              perror("listen()"); 
    3078                 if (sfd_ptr != sfd_list) { 
    3079                     do { 
    3080                         --sfd_ptr; 
    3081                         close(*sfd_ptr); 
    3082                     } while (sfd_ptr != sfd_list); 
    3083                 } 
    30843057              close(sfd); 
    30853058              freeaddrinfo(ai); 
    3086               free(sfd_list); 
    3087               return NULL; 
     3059              return 1; 
    30883060          } 
    30893061      } 
     3062 
     3063      if (IS_UDP(prot)) { 
     3064        int c; 
     3065 
     3066        for (c = 0; c < settings.num_threads; c++) { 
     3067            /* this is guaranteed to hit all threads because we round-robin */ 
     3068            dispatch_conn_new(sfd, conn_read, EV_READ | EV_PERSIST, 
     3069                              UDP_READ_BUFFER_SIZE, ascii_udp_prot); 
     3070        } 
     3071      } else { 
     3072        if (!(listen_conn_add = conn_new(sfd, conn_listening, 
     3073                                      EV_READ | EV_PERSIST, 1, 
     3074                                      prot, main_base))) { 
     3075            fprintf(stderr, "failed to create listening connection\n"); 
     3076            exit(EXIT_FAILURE); 
     3077        } 
     3078 
     3079        if (listen_conn == NULL) { 
     3080            listen_conn = listen_conn_add; 
     3081        } else { 
     3082            listen_conn_add->next = listen_conn->next; 
     3083            listen_conn->next = listen_conn_add; 
     3084        } 
     3085      } 
    30903086    } 
    30913087 
    30923088    freeaddrinfo(ai); 
    30933089 
    3094     if (success == 0) { 
    3095         free(sfd_list); 
    3096         return NULL; 
    3097     } 
    3098  
    3099     return sfd_list; 
     3090    return 0; 
    31003091} 
    31013092 
     
    31183109} 
    31193110 
    3120 static int *server_socket_unix(const char *path, int access_mask) { 
     3111static int server_socket_unix(const char *path, int access_mask) { 
    31213112    int sfd; 
    3122     int *sfd_list; 
    31233113    struct linger ling = {0, 0}; 
    31243114    struct sockaddr_un addr; 
     
    31283118 
    31293119    if (!path) { 
    3130         return NULL
     3120        return 1
    31313121    } 
    31323122 
    31333123    if ((sfd = new_socket_unix()) == -1) { 
    3134         return NULL; 
    3135     } 
    3136  
    3137     /* 
    3138       When UNIX domain sockets get refactored, this will go away. 
    3139       For now we know there is just one socket. 
    3140     */ 
    3141     sfd_list= (int *)calloc(1, sizeof(int)); 
    3142     if (sfd_list == NULL) { 
    3143         fprintf(stderr, "calloc()\n"); 
    3144         return NULL; 
    3145     } 
    3146     memset(sfd_list, -1, sizeof(int) * 1); 
     3124        return 1; 
     3125    } 
    31473126 
    31483127    /* 
     
    31703149        perror("bind()"); 
    31713150        close(sfd); 
    3172         free(sfd_list); 
    31733151        umask(old_umask); 
    3174         return NULL
     3152        return 1
    31753153    } 
    31763154    umask(old_umask); 
     
    31783156        perror("listen()"); 
    31793157        close(sfd); 
    3180         free(sfd_list); 
    3181         return NULL; 
    3182     } 
    3183  
    3184     *sfd_list= sfd; 
    3185  
    3186     return sfd_list; 
     3158        return 1; 
     3159    } 
     3160    if (!(listen_conn = conn_new(sfd, conn_listening, 
     3161                                     EV_READ | EV_PERSIST, 1, false, main_base))) { 
     3162        fprintf(stderr, "failed to create listening connection\n"); 
     3163        exit(EXIT_FAILURE); 
     3164    } 
     3165 
     3166    return 0; 
    31873167} 
    31883168 
     
    34223402                listen_conn = listen_conn_add; 
    34233403            } else { 
    3424                                listen_conn_add->next = listen_conn->next; 
    3425                                listen_conn->next = listen_conn_add; 
     3404                listen_conn_add->next = listen_conn->next; 
     3405                listen_conn->next = listen_conn_add; 
    34263406            } 
    34273407        } 
     
    34893469    static int *l_socket = NULL; 
    34903470    static int *bl_socket = NULL; 
    3491     static int l_socket_count = 0; 
    3492     static int bl_socket_count = 0; 
    34933471 
    34943472    /* udp socket */ 
     
    36523630    } 
    36533631 
    3654     /* 
    3655      * initialization order: first create the listening sockets 
    3656      * (may need root on low ports), then drop root if needed, 
    3657      * then daemonise if needed, then init libevent (in some cases 
    3658      * descriptors created by libevent wouldn't survive forking). 
    3659      */ 
    3660  
    3661     /* create the listening socket and bind it */ 
    3662     if (settings.socketpath == NULL) { 
    3663         l_socket = server_socket(settings.port, 0, &l_socket_count); 
    3664         if (l_socket == NULL) { 
    3665             fprintf(stderr, "failed to listen\n"); 
    3666             exit(EXIT_FAILURE); 
    3667         } 
    3668         /* Try the binary port. */ 
    3669         if(settings.binport > 0) { 
    3670             bl_socket = server_socket(settings.binport, 0, &bl_socket_count); 
    3671             if (bl_socket == NULL) { 
    3672                  fprintf(stderr, "failed to listen to binary protocol\n"); 
    3673                     exit(EXIT_FAILURE); 
    3674             } 
    3675         } 
    3676     } 
    3677  
    3678     if (settings.udpport > 0 && settings.socketpath == NULL) { 
    3679         /* create the UDP listening socket and bind it */ 
    3680         u_socket = server_socket(settings.udpport, ascii_udp_prot, 
    3681             &u_socket_count); 
    3682         if (u_socket == NULL) { 
    3683             fprintf(stderr, "failed to listen on UDP port %d\n", settings.udpport); 
    3684             exit(EXIT_FAILURE); 
    3685         } 
    3686     } 
    3687  
    36883632    /* lock paged memory if needed */ 
    36893633    if (lock_memory) { 
     
    37133657            return 1; 
    37143658        } 
    3715     } 
    3716  
    3717     /* create unix mode sockets after dropping privileges */ 
    3718     if (settings.socketpath != NULL) { 
    3719         l_socket = server_socket_unix(settings.socketpath,settings.access); 
    3720         if (l_socket == NULL) { 
    3721           fprintf(stderr, "failed to listen\n"); 
    3722           exit(EXIT_FAILURE); 
    3723         } 
    3724         /* We only support one of these, so whe know the count */ 
    3725         l_socket_count = 1; 
    37263659    } 
    37273660 
     
    37363669        } 
    37373670    } 
    3738  
    37393671 
    37403672    /* initialize main thread libevent instance */ 
     
    37713703        exit(EXIT_FAILURE); 
    37723704    } 
    3773     /* Set up all of the listening connections */ 
    3774     setup_listening_conns(l_socket, l_socket_count, ascii_prot); 
    3775     setup_listening_conns(bl_socket, bl_socket_count, binary_prot); 
    3776  
    37773705    /* start up worker threads if MT mode */ 
    37783706    thread_init(settings.num_threads, main_base); 
     
    37913719    } 
    37923720    delete_handler(0, 0, 0); /* sets up the event */ 
    3793     /* create the initial listening udp connection, monitored on all threads */ 
    3794     if (u_socket) { 
    3795         for (c = 0; c < settings.num_threads; c++) { 
    3796             /* this is guaranteed to hit all threads because we round-robin */ 
    3797             dispatch_conn_new(*u_socket, conn_read, EV_READ | EV_PERSIST, 
    3798                               UDP_READ_BUFFER_SIZE, ascii_udp_prot); 
    3799         } 
    3800     } 
     3721 
     3722    /* create unix mode sockets after dropping privileges */ 
     3723    if (settings.socketpath != NULL) { 
     3724        if (server_socket_unix(settings.socketpath,settings.access)) { 
     3725          fprintf(stderr, "failed to listen\n"); 
     3726          exit(EXIT_FAILURE); 
     3727        } 
     3728    } 
     3729 
     3730    /* create the listening socket, bind it, and init */ 
     3731    if (settings.socketpath == NULL) { 
     3732        int udp_port; 
     3733 
     3734        if (server_socket(settings.port, ascii_prot)) { 
     3735            fprintf(stderr, "failed to listen\n"); 
     3736            exit(EXIT_FAILURE); 
     3737        } 
     3738 
     3739        /* Try the binary port. */ 
     3740        if(settings.binport > 0) { 
     3741            if(server_socket(settings.binport, binary_prot)) { 
     3742                 fprintf(stderr, "failed to listen to binary protocol\n"); 
     3743                    exit(EXIT_FAILURE); 
     3744            } 
     3745        } 
     3746        /* 
     3747         * initialization order: first create the listening sockets 
     3748         * (may need root on low ports), then drop root if needed, 
     3749         * then daemonise if needed, then init libevent (in some cases 
     3750         * descriptors created by libevent wouldn't survive forking). 
     3751         */ 
     3752        udp_port = settings.udpport ? settings.udpport : settings.port; 
     3753 
     3754        /* create the UDP listening socket and bind it */ 
     3755        if (server_socket(udp_port, ascii_udp_prot)) { 
     3756            fprintf(stderr, "failed to listen on UDP port %d\n", settings.udpport); 
     3757            exit(EXIT_FAILURE); 
     3758        } 
     3759    } 
     3760 
    38013761    /* enter the event loop */ 
    38023762    event_base_loop(main_base, 0);