memcached.c
static pid_t start_server(in_port_t *port_out, bool daemon, int timeout) {
#if 0 // CLOUD9: Don't need to do this for Cloud9
    char environment[80];
    snprintf(environment, sizeof(environment),
             "MEMCACHED_PORT_FILENAME=/tmp/ports.%lu", (long)getpid());
    char *filename= environment + strlen("MEMCACHED_PORT_FILENAME=");
    char pid_file[80];
    snprintf(pid_file, sizeof(pid_file), "/tmp/pid.%lu", (long)getpid());

    remove(filename);
    remove(pid_file);
#endif // CLOUD9

#ifdef __sun
    /* I want to name the corefiles differently so that they don't
       overwrite each other
    */
    char coreadm[128];
    snprintf(coreadm, sizeof(coreadm),
             "coreadm -p core.%%f.%%p %lu", (unsigned long)getpid());
    system(coreadm);
#endif

    // CLOUD9: This is the synchronization object to ensure the server is initialized
    // before attempting to connect to it
    int fdpair[2];
    int res = pipe(fdpair);
    assert(res == 0);

    pid_t pid = fork();
    assert(pid != -1);

    if (pid == 0) {
        /* Child */
        char *argv[20];
        int arg = 0;
#if 0 // CLOUD9: Don't need to do this for Cloud9
        char tmo[24];
        snprintf(tmo, sizeof(tmo), "%u", timeout);

        putenv(environment);
#endif
#ifdef __sun
        putenv("LD_PRELOAD=watchmalloc.so.1");
        putenv("MALLOC_DEBUG=WATCH");
#endif

#if 0 // CLOUD9: No need to make it a daemon
        if (!daemon) {
            argv[arg++] = "./timedrun";
            argv[arg++] = tmo;
        }
#endif
        argv[arg++] = "./memcached-debug";
        argv[arg++] = "-v";
        argv[arg++] = "-p";
        argv[arg++] = "11211";
        argv[arg++] = "-U";
        argv[arg++] = "0";
        argv[arg++] = "-o";
        argv[arg++] = "hashpower=3";
		// CLOUD9: Use the ASCII protocol
        //argv[arg++] = "-B";
        //argv[arg++] = "ascii";
        /* Handle rpmbuild and the like doing this as root */
        if (getuid() == 0) {
            argv[arg++] = "-u";
            argv[arg++] = "root";
        }
#if 0 // CLOUD9
        if (daemon) {
            argv[arg++] = "-d";
            argv[arg++] = "-P";
            argv[arg++] = pid_file;
        }
#endif
#ifdef MESSAGE_DEBUG
         argv[arg++] = "-vvv";
#endif
        argv[arg++] = NULL;

// CLOUD9: We replaced here the original synchronization code with our own
        close(fdpair[0]);
        int ret = server_main(arg-1, argv, fdpair[1]);
        printf("done!\n");
        return ret;
        //assert(execv(argv[0], argv) != -1);
    }

    close(fdpair[1]);
    int data;
    res = read(fdpair[0], &data, sizeof(data));
    assert(res == 0);
    close(fdpair[0]);

    *port_out = 11211;
    printf("server initialization complete\n");

    return pid;
}
/**
 * Function to start the server and let it listen on a random port
 *
 * @param port_out where to store the TCP port number the server is
 *                 listening on
 * @param daemon set to true if you want to run the memcached server
 *               as a daemon process
 * @return the pid of the memcached server
 */
Function to start the server and let it listen on a random port
@param port_out where to store the TCP port number the server is
listening on
@param daemon set to true if you want to run the memcached server
as a daemon process
@return the pid of the memcached server
Function to start the server and let it listen on a random port @param port_out where to store the TCP port number the server is listening on @param daemon set to true if you want to run the memcached server as a daemon process @return the pid of the memcached server
static pid_t start_server(in_port_t *port_out, bool daemon, int timeout) {
#if 0 
    char environment[80];
    snprintf(environment, sizeof(environment),
             "MEMCACHED_PORT_FILENAME=/tmp/ports.%lu", (long)getpid());
    char *filename= environment + strlen("MEMCACHED_PORT_FILENAME=");
    char pid_file[80];
    snprintf(pid_file, sizeof(pid_file), "/tmp/pid.%lu", (long)getpid());
    remove(filename);
    remove(pid_file);
#endif 
#ifdef __sun
    char coreadm[128];
    snprintf(coreadm, sizeof(coreadm),
             "coreadm -p core.%%f.%%p %lu", (unsigned long)getpid());
    system(coreadm);
#endif
    : This is the synchronization object to ensure the server is initialized
    int fdpair[2];
    int res = pipe(fdpair);
    assert(res == 0);
    pid_t pid = fork();
    assert(pid != -1);
    if (pid == 0) {
        char *argv[20];
        int arg = 0;
#if 0 
        char tmo[24];
        snprintf(tmo, sizeof(tmo), "%u", timeout);
        putenv(environment);
#endif
#ifdef __sun
        putenv("LD_PRELOAD=watchmalloc.so.1");
        putenv("MALLOC_DEBUG=WATCH");
#endif
#if 0 : No need to make it a daemon
        if (!daemon) {
            argv[arg++] = "./timedrun";
            argv[arg++] = tmo;
        }
#endif
        argv[arg++] = "./memcached-debug";
        argv[arg++] = "-v";
        argv[arg++] = "-p";
        argv[arg++] = "11211";
        argv[arg++] = "-U";
        argv[arg++] = "0";
        argv[arg++] = "-o";
        argv[arg++] = "hashpower=3";
		: Use the ASCII protocol
        if (getuid() == 0) {
            argv[arg++] = "-u";
            argv[arg++] = "root";
        }
#if 0 
        if (daemon) {
            argv[arg++] = "-d";
            argv[arg++] = "-P";
            argv[arg++] = pid_file;
        }
#endif
#ifdef MESSAGE_DEBUG
         argv[arg++] = "-vvv";
#endif
        argv[arg++] = NULL;
: We replaced here the original synchronization code with our own
        close(fdpair[0]);
        int ret = server_main(arg-1, argv, fdpair[1]);
        printf("done!\n");
        return ret;
    }
    close(fdpair[1]);
    int data;
    res = read(fdpair[0], &data, sizeof(data));
    assert(res == 0);
    close(fdpair[0]);
    *port_out = 11211;
    printf("server initialization complete\n");
    return pid;
}
static pid_t start_server ( in_port_t * port_out , bool daemon , int timeout ) { #if 0 char environment [ 80 ] ; snprintf ( environment , sizeof ( environment ) , " " , ( long ) getpid ( ) ) ; char * filename = environment + strlen ( " " ) ; char pid_file [ 80 ] ; snprintf ( pid_file , sizeof ( pid_file ) , " " , ( long ) getpid ( ) ) ; remove ( filename ) ; remove ( pid_file ) ; #endif #ifdef __sun char coreadm [ 128 ] ; snprintf ( coreadm , sizeof ( coreadm ) , " " , ( unsigned long ) getpid ( ) ) ; system ( coreadm ) ; #endif int fdpair [ 2 ] ; int res = pipe ( fdpair ) ; assert ( res == 0 ) ; pid_t pid = fork ( ) ; assert ( pid != -1 ) ; if ( pid == 0 ) { char * argv [ 20 ] ; int arg = 0 ; #if 0 char tmo [ 24 ] ; snprintf ( tmo , sizeof ( tmo ) , " " , timeout ) ; putenv ( environment ) ; #endif #ifdef __sun putenv ( " " ) ; putenv ( " " ) ; #endif #if 0 if ( ! daemon ) { argv [ arg ++ ] = " " ; argv [ arg ++ ] = tmo ; } #endif argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; if ( getuid ( ) == 0 ) { argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; } #if 0 if ( daemon ) { argv [ arg ++ ] = " " ; argv [ arg ++ ] = " " ; argv [ arg ++ ] = pid_file ; } #endif #ifdef MESSAGE_DEBUG argv [ arg ++ ] = " " ; #endif argv [ arg ++ ] = NULL ; close ( fdpair [ 0 ] ) ; int ret = server_main ( arg - 1 , argv , fdpair [ 1 ] ) ; printf ( " \n " ) ; return ret ; } close ( fdpair [ 1 ] ) ; int data ; res = read ( fdpair [ 0 ] , & data , sizeof ( data ) ) ; assert ( res == 0 ) ; close ( fdpair [ 0 ] ) ; * port_out = 11211 ; printf ( " \n " ) ; return pid ; }
Function to start the server and let it listen on a random port
 @param port_out where to store the TCP port number the server is
                 listening on
 @param daemon set to true if you want to run the memcached server
               as a daemon process
 @return the pid of the memcached server
Function to start the server and let it listen on a random port @param port_out where to store the TCP port number the server is listening on @param daemon set to true if you want to run the memcached server as a daemon process @return the pid of the memcached server
// CLOUD9: Don't need to do this for Cloud9 // CLOUD9 /* I want to name the corefiles differently so that they don't
       overwrite each other
    */ // CLOUD9: This is the synchronization object to ensure the server is initialized // before attempting to connect to it /* Child */ // CLOUD9: Don't need to do this for Cloud9 // CLOUD9: No need to make it a daemon // CLOUD9: Use the ASCII protocol //argv[arg++] = "-B"; //argv[arg++] = "ascii"; /* Handle rpmbuild and the like doing this as root */ // CLOUD9 // CLOUD9: We replaced here the original synchronization code with our own //assert(execv(argv[0], argv) != -1);