Beispiel #1
0
enum crawler_result_type lru_crawler_crawl(char *slabs) {
    char *b = NULL;
    uint32_t sid = 0;
    uint8_t tocrawl[POWER_LARGEST];
    if (pthread_mutex_trylock(&lru_crawler_lock) != 0) {
        return CRAWLER_RUNNING;
    }
    pthread_mutex_lock(&cache_lock);

    if (strcmp(slabs, "all") == 0) {
        for (sid = 0; sid < LARGEST_ID; sid++) {
            tocrawl[sid] = 1;
        }
    } else {
        for (char *p = strtok_r(slabs, ",", &b);
             p != NULL;
             p = strtok_r(NULL, ",", &b)) {

            if (!safe_strtoul(p, &sid) || sid < POWER_SMALLEST
                    || sid >= POWER_LARGEST) {
                pthread_mutex_unlock(&cache_lock);
                pthread_mutex_unlock(&lru_crawler_lock);
                return CRAWLER_BADCLASS;
            }
            tocrawl[sid] = 1;
        }
    }

    for (sid = 0; sid < LARGEST_ID; sid++) {
        if (tocrawl[sid] != 0 && tails[sid] != NULL) {
            if (settings.verbose > 2)
                fprintf(stderr, "Kicking LRU crawler off for slab %d\n", sid);
            crawlers[sid].nbytes = 0;
            crawlers[sid].nkey = 0;
            crawlers[sid].it_flags = 1; /* For a crawler, this means enabled. */
            crawlers[sid].next = 0;
            crawlers[sid].prev = 0;
            crawlers[sid].time = 0;
            crawlers[sid].remaining = settings.lru_crawler_tocrawl;
            crawlers[sid].slabs_clsid = sid;
            crawler_link_q((item *)&crawlers[sid]);
            crawler_count++;
        }
    }
    pthread_mutex_unlock(&cache_lock);
    pthread_cond_signal(&lru_crawler_cond);
    STATS_LOCK();
    stats.lru_crawler_running = true;
    STATS_UNLOCK();
    pthread_mutex_unlock(&lru_crawler_lock);
    return CRAWLER_OK;
}
void SetTelliepulser(client *c, int argc, sds *argv)
{
  float rate=0, length=0;
  uint32_t nPulse=0;
  safe_strtof(argv[1],&rate);
  safe_strtof(argv[2],&length);
  safe_strtoul(argv[3],&nPulse);

  int ret= Pulser(rate,length,nPulse,MappedTPulserBaseAddress);

  if(ret == 0) addReplyStatus(c, "+OK");
  else addReplyError(c, tubii_err);;
}
Beispiel #3
0
/**
 * VALUE key flags datalength\r\n
 * <DATA>\r\n
 * END\r\n
 */
static int parseFirstLineResponse(fallocator_t fallocator, responseParserImpl_t* pParser, char** tokens,
		int ntokens) {
	int returnValue = 0;

	if (0 == strcmp(tokens[0], "END")) {
		pParser->endOfResponse = 1;
		goto OnSuccess;
	}
	if (0 == strcmp(tokens[0], "VALUE")) {
		pParser->key = tokens[1];
		tokens[1] = 0;

		IfTrue(safe_strtoul(tokens[2], &pParser->flags), INFO, "Error parsing flags");
		IfTrue(safe_strtoul(tokens[3], &pParser->valueLength), INFO, "Error parsing value length");
	}
	goto OnSuccess;
OnError:
	returnValue = -1;
OnSuccess:
	LOG(DEBUG, "endOfResponse %d", pParser->endOfResponse);
	return returnValue;
}
void SetMZHappyPulser(client *c, int argc, sds *argv)
{
  // For Ian's debugging purposes, MZHappy can also be used as a pulser
  float rate=0, length=0;
  uint32_t nPulse=0;
  safe_strtof(argv[1],&rate);
  safe_strtof(argv[2],&length);
  safe_strtoul(argv[3],&nPulse);

  int ret= Pulser(rate,length,nPulse,MappedHappyBaseAddress);

  if(ret == 0) addReplyStatus(c, "+OK");
  else addReplyError(c, tubii_err);
}
Beispiel #5
0
static void save_pid ( const char *pid_file )
{
    FILE *fp;
    if ( access (pid_file, F_OK) == 0 )
    {
        if ( ( fp = fopen (pid_file, "r") ) != NULL )
        {
            char buffer[1024];
            if ( fgets (buffer, sizeof (buffer ), fp) != NULL )
            {
                unsigned int pid;
                if ( safe_strtoul (buffer, &pid) && kill (( pid_t ) pid, 0) == 0 )
                {
                    fprintf (stderr, "WARNING: The pid file contained the following (running) pid: %u\n", pid);
                }
            }
            fclose (fp);
        }
    }

    /* Create the pid file first with a temporary name, then
     * atomically move the file to the real name to avoid a race with
     * another process opening the file to read the pid, but finding
     * it empty.
     */
    char tmp_pid_file[1024];
    snprintf (tmp_pid_file, sizeof (tmp_pid_file ), "%s.tmp", pid_file);

    if ( ( fp = fopen (tmp_pid_file, "w") ) == NULL )
    {
        vperror ("Could not open the pid file %s for writing", tmp_pid_file);
        return;
    }

    fprintf (fp, "%ld\n", ( long ) getpid ());
    if ( fclose (fp) == - 1 )
    {
        vperror ("Could not close the pid file %s", tmp_pid_file);
    }

    if ( rename (tmp_pid_file, pid_file) != 0 )
    {
        vperror ("Could not rename the pid file from %s to %s",
                 tmp_pid_file, pid_file);
    }
}
Beispiel #6
0
/**
 * Get the verbosity level on the server.
 *
 * There isn't a single command to retrieve the current verbosity level,
 * but it is available through the settings stats...
 *
 * @param bio connection to the server.
 */
static int get_verbosity(BIO *bio)
{
    const char *settings = "settings";
    const uint16_t settingslen = (uint16_t)strlen(settings);

    protocol_binary_request_stats request = {
        .message.header.request.magic = PROTOCOL_BINARY_REQ,
        .message.header.request.opcode = PROTOCOL_BINARY_CMD_STAT,
        .message.header.request.keylen = htons(settingslen),
        .message.header.request.bodylen = htonl(settingslen)
    };

    ensure_send(bio, &request, sizeof(request));
    ensure_send(bio, settings, settingslen);

    // loop and receive the result and print the verbosity when we get it
    struct statistic st;
    do {
        receive_stat_response(bio, &st);
        if (st.key != NULL && strcasecmp(st.key, "verbosity") == 0) {
            uint32_t level;
            if (safe_strtoul(st.value, &level)) {
                const char *levels[] = { "warning",
                                         "info",
                                         "debug",
                                         "detail",
                                         "unknown" };
                const char *ptr = levels[4];

                if (level < 4) {
                    ptr = levels[level];
                }
                fprintf(stderr, "%s\n", ptr);
            } else {
                fprintf(stderr, "%s\n", st.value);
            }
        }
        free(st.key);
        free(st.value);
    } while (st.key != NULL);

    return EXIT_SUCCESS;
}
// ELLIE commands
void SetGenericpulser(client *c, int argc, sds *argv)
{
  // Need to sync. create pulse and delay it async.
  float rate=0, length=0;
  uint32_t nPulse=0;
  safe_strtof(argv[1],&rate);
  safe_strtof(argv[2],&length);
  safe_strtoul(argv[3],&nPulse);

  // 1. Create Pulse
  int ret1= Pulser(rate,length,nPulse,MappedPulserBaseAddress);
  if(ret1!=0){
    addReplyError(c, tubii_err);
    return;
  }

  // 2. Delay Pulse
  /*int ret2= Muxer("4");
  int ret3= LoadShift(argv[1]);
  */
  addReplyStatus(c, "+OK");
}
Beispiel #8
0
static enum test_return test_safe_strtoul(void) {
    uint32_t val;
    assert(safe_strtoul("123", &val));
    assert(val == 123);
    assert(safe_strtoul("+123", &val));
    assert(val == 123);
    assert(!safe_strtoul("", &val));  /* empty */
    assert(!safe_strtoul("123BOGUS", &val));  /* non-numeric */
    /* Not sure what it does, but this works with ICC :/
       assert(!safe_strtoul("92837498237498237498029383", &val)); // out of range
    */

    /* extremes: */
    assert(safe_strtoul("4294967295", &val)); /* 2**32 - 1 */
    assert(val == 4294967295L);
    /* This actually works on 64-bit ubuntu
       assert(!safe_strtoul("4294967296", &val)); // 2**32
    */
    assert(!safe_strtoul("-1", &val));  /* negative */
    return TEST_PASS;
}
Beispiel #9
0
int ipcalc_main(int argc, char **argv)
{
	unsigned long mode;

	in_addr_t netmask;
	in_addr_t broadcast;
	in_addr_t network;
	in_addr_t ipaddr;
	struct in_addr a;

#ifdef CONFIG_FEATURE_IPCALC_FANCY
	unsigned long netprefix = 0;
	int have_netmask = 0;
	char *ipstr, *prefixstr;
#endif

	static const struct option long_options[] = {
		{"netmask",		no_argument, NULL, 'm'},
		{"broadcast",	no_argument, NULL, 'b'},
		{"network",		no_argument, NULL, 'n'},
#ifdef CONFIG_FEATURE_IPCALC_FANCY
		{"prefix",		no_argument, NULL, 'p'},
		{"hostname",	no_argument, NULL, 'h'},
		{"silent",		no_argument, NULL, 's'},
#endif
		{NULL, 0, NULL, 0}
	};

	bb_applet_long_options = long_options;
	mode = bb_getopt_ulflags(argc, argv,
#ifdef CONFIG_FEATURE_IPCALC_FANCY
			"mbnphs"
#else
			"mbn"
#endif
			);

	argc -= optind;
	argv += optind;
	if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
		if (argc > 2 || argc <= 0)
			bb_show_usage();
	} else {
		if (argc != 1)
			bb_show_usage();
	}

#ifdef CONFIG_FEATURE_IPCALC_FANCY
	prefixstr = ipstr = argv[0];

	while(*prefixstr) {
		if (*prefixstr == '/') {
			*prefixstr = (char)0;
			prefixstr++;
			if (*prefixstr) {
				unsigned int msk;

				if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
					IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
							exit(EXIT_FAILURE));
				}
				netmask = 0;
				msk = 0x80000000;
				while (netprefix > 0) {
					netmask |= msk;
					msk >>= 1;
					netprefix--;
				}
				netmask = htonl(netmask);
				/* Even if it was 0, we will signify that we have a netmask. This allows */
				/* for specification of default routes, etc which have a 0 netmask/prefix */
				have_netmask = 1;
			}
			break;
		}
		prefixstr++;
	}
Beispiel #10
0
/** Note: the key and val param buffers are modified.
 */
void cproxy_parse_behavior_key_val(char *key,
                                   char *val,
                                   proxy_behavior *behavior) {
    uint32_t ms = 0;
    uint32_t x = 0;
    bool ok = false;

    assert(behavior != NULL);

    if (key != NULL &&
        val != NULL) {
        key = trimstr(key);
        val = trimstr(val);

        if (wordeq(key, "cycle")) {
            ok = safe_strtoul(val, &behavior->cycle);
        } else if (wordeq(key, "downstream_max") ||
                   wordeq(key, "concurrency")) {
            ok = safe_strtoul(val, &behavior->downstream_max);
        } else if (wordeq(key, "downstream_conn_max")) {
            ok = safe_strtoul(val, &behavior->downstream_conn_max);
        } else if (wordeq(key, "weight") ||
                   wordeq(key, "downstream_weight")) {
            ok = safe_strtoul(val, &behavior->downstream_weight);
        } else if (wordeq(key, "retry") ||
                   wordeq(key, "downstream_retry")) {
            ok = safe_strtoul(val, &behavior->downstream_retry);
        } else if (wordeq(key, "protocol") ||
                   wordeq(key, "downstream_protocol")) {
            if (wordeq(val, "ascii") ||
                wordeq(val, "memcached-ascii") ||
                wordeq(val, "membase-ascii")) {
                behavior->downstream_protocol =
                    proxy_downstream_ascii_prot;
                ok = true;
            } else if (wordeq(val, "binary") ||
                       wordeq(val, "memcached-binary") ||
                       wordeq(val, "membase-binary")) {
                behavior->downstream_protocol =
                    proxy_downstream_binary_prot;
                ok = true;
            } else {
                if (settings.verbose > 1) {
                    moxi_log_write("unknown behavior prot: %s\n", val);
                }
            }
        } else if (wordeq(key, "timeout") ||
                   wordeq(key, "downstream_timeout") ||
                   wordeq(key, "downstream_conn_timeout")) {
            ok = safe_strtoul(val, &ms);
            behavior->downstream_timeout.tv_sec  = floor(ms / 1000.0);
            behavior->downstream_timeout.tv_usec = (ms % 1000) * 1000;
        } else if (wordeq(key, "downstream_conn_queue_timeout")) {
            ok = safe_strtoul(val, &ms);
            behavior->downstream_conn_queue_timeout.tv_sec  = floor(ms / 1000.0);
            behavior->downstream_conn_queue_timeout.tv_usec = (ms % 1000) * 1000;
        } else if (wordeq(key, "wait_queue_timeout")) {
            ok = safe_strtoul(val, &ms);
            behavior->wait_queue_timeout.tv_sec  = floor(ms / 1000.0);
            behavior->wait_queue_timeout.tv_usec = (ms % 1000) * 1000;
        } else if (wordeq(key, "connect_timeout")) {
            ok = safe_strtoul(val, &ms);
            behavior->connect_timeout.tv_sec  = floor(ms / 1000.0);
            behavior->connect_timeout.tv_usec = (ms % 1000) * 1000;
        } else if (wordeq(key, "auth_timeout")) {
            ok = safe_strtoul(val, &ms);
            behavior->auth_timeout.tv_sec  = floor(ms / 1000.0);
            behavior->auth_timeout.tv_usec = (ms % 1000) * 1000;
        } else if (wordeq(key, "time_stats")) {
            ok = safe_strtoul(val, &x);
            behavior->time_stats = x;
        } else if (wordeq(key, "mcs_opts")) {
            if (strlen(val) < sizeof(behavior->mcs_opts)) {
                strcpy(behavior->mcs_opts, val);
                ok = true;
            }
        } else if (wordeq(key, "connect_max_errors")) {
            ok = safe_strtoul(val, &behavior->connect_max_errors);
        } else if (wordeq(key, "connect_retry_interval")) {
            ok = safe_strtoul(val, &behavior->connect_retry_interval);
        } else if (wordeq(key, "front_cache_max")) {
            ok = safe_strtoul(val, &behavior->front_cache_max);
        } else if (wordeq(key, "front_cache_lifespan")) {
            ok = safe_strtoul(val, &behavior->front_cache_lifespan);
        } else if (wordeq(key, "front_cache_spec")) {
            if (strlen(val) < sizeof(behavior->front_cache_spec)) {
                strcpy(behavior->front_cache_spec, val);
                ok = true;
            }
        } else if (wordeq(key, "front_cache_unspec")) {
            if (strlen(val) < sizeof(behavior->front_cache_unspec)) {
                strcpy(behavior->front_cache_unspec, val);
                ok = true;
            }
        } else if (wordeq(key, "key_stats_max")) {
            ok = safe_strtoul(val, &behavior->key_stats_max);
        } else if (wordeq(key, "key_stats_lifespan")) {
            ok = safe_strtoul(val, &behavior->key_stats_lifespan);
        } else if (wordeq(key, "key_stats_spec")) {
            if (strlen(val) < sizeof(behavior->key_stats_spec)) {
                strcpy(behavior->key_stats_spec, val);
                ok = true;
            }
        } else if (wordeq(key, "key_stats_unspec")) {
            if (strlen(val) < sizeof(behavior->key_stats_unspec)) {
                strcpy(behavior->key_stats_unspec, val);
                ok = true;
            }
        } else if (wordeq(key, "optimize_set")) {
            if (strlen(val) < sizeof(behavior->optimize_set)) {
                strcpy(behavior->optimize_set, val);
                ok = true;
            }
        } else if (wordeq(key, "usr")) {
            if (strlen(val) < sizeof(behavior->usr)) {
                strcpy(behavior->usr, val);
                ok = true;
            }
        } else if (wordeq(key, "pwd")) {
            if (strlen(val) < sizeof(behavior->pwd)) {
                strcpy(behavior->pwd, val);
                ok = true;
            }
        } else if (wordeq(key, "host")) {
            if (strlen(val) < sizeof(behavior->host)) {
                strcpy(behavior->host, val);
                ok = true;
            }
        } else if (wordeq(key, "port")) {
            ok = safe_strtol(val, &behavior->port);
        } else if (wordeq(key, "bucket")) {
            if (strlen(val) < sizeof(behavior->bucket)) {
                strcpy(behavior->bucket, val);
                ok = true;
            }
        } else if (wordeq(key, "port_listen")) {
            ok = safe_strtol(val, &behavior->port_listen);
        } else if (wordeq(key, "default_bucket_name")) {
            if (strlen(val) < sizeof(behavior->default_bucket_name)) {
                strcpy(behavior->default_bucket_name, val);
                ok = true;
            }
        } else if (key[0] == '#') { // Comment.
            ok = true;
        } else {
            if (settings.verbose > 1) {
                moxi_log_write("ERROR: unknown behavior key: %s\n", key);
            }
        }
    }

    if (ok == false) {
        moxi_log_write("ERROR: config error in key: %s value: %s\n",
                       key, val);
    }
}
Beispiel #11
0
const char *valid_verify_sender_addr(const char *their_addr)
{
    static VSTRING *time_indep_sender_buf;	/* sender without time stamp */
    ssize_t base_len;
    unsigned long my_epoch;
    unsigned long their_epoch;
    char   *my_at_domain;
    char   *their_at_domain;
    char   *cp;

    /*
     * The null address is always time-independent.
     */
    if (*var_verify_sender == 0 || strcmp(var_verify_sender, "<>") == 0)
	return (*their_addr ? 0 : "");

    /*
     * One-time initialization. Generate the time-independent address that we
     * will return if the match is successful. This address is also used as a
     * matching template.
     */
    if (time_indep_sender_buf == 0) {
	time_indep_sender_buf = vstring_alloc(10);
	vstring_strcpy(time_indep_sender_buf, var_verify_sender);
	rewrite_clnt_internal(MAIL_ATTR_RWR_LOCAL, STR(time_indep_sender_buf),
			      time_indep_sender_buf);
    }

    /*
     * Check the time-independent sender localpart.
     */
    if ((my_at_domain = strchr(STR(time_indep_sender_buf), '@')) != 0)
	base_len = my_at_domain - STR(time_indep_sender_buf);
    else
	base_len = LEN(time_indep_sender_buf);
    if (strncasecmp_utf8(STR(time_indep_sender_buf), their_addr, base_len) != 0)
	return (0);				/* sender localpart mis-match */

    /*
     * Check the time-independent domain.
     */
    if ((their_at_domain = strchr(their_addr, '@')) == 0 && my_at_domain != 0)
	return (0);				/* sender domain mis-match */
    if (their_at_domain != 0
	&& (my_at_domain == 0
	    || strcasecmp_utf8(their_at_domain, my_at_domain) != 0))
	return (0);				/* sender domain mis-match */

    /*
     * Check the time-dependent portion.
     */
    if (var_verify_sender_ttl > 0) {
	their_epoch = safe_strtoul(their_addr + base_len, &cp, VERIFY_BASE);
	if ((*cp != '@' && *cp != 0)
	    || (their_epoch == ULONG_MAX && errno == ERANGE))
	    return (0);				/* malformed time stamp */
	my_epoch = VERIFY_SENDER_ADDR_EPOCH();
	if (their_epoch < my_epoch - 1 || their_epoch > my_epoch + 1)
	    return (0);				/* outside time window */
    }

    /*
     * No time-dependent portion.
     */
    else {
	if (their_addr[base_len] != '@' && their_addr[base_len] != 0)
	    return (0);				/* garbage after sender base */
    }
    return (STR(time_indep_sender_buf));
}
Beispiel #12
0
void load_TUBii_command(client *c, int argc, sds *argv)
{
    /* Load CAEN hardware settings from the database. */
    uint32_t key;
    PGconn *conn;
    PGresult *res = NULL;
    char conninfo[1024];
    char command[10000];
    char *name, *value_str;
    uint32_t value;
    int i;
    int rows;

    if (safe_strtoul(argv[1], &key)) {
        addReplyErrorFormat(c, "'%s' is not a valid uint32_t", argv[1]);
        return;
    }

    sprintf(command, "select * from TUBii where key = %i", key);

    sprintf(conninfo, "dbname=%s host=%s user=%s password=%s", dbconfig.name, dbconfig.host, dbconfig.user, dbconfig.password);

    /* Request row from the database. */
    conn = PQconnectdb(conninfo);

    if (PQstatus(conn) != CONNECTION_OK) {
        addReplyErrorFormat(c, "connection to database failed: %s",
                            PQerrorMessage(conn));
        goto pq_error;
    }

    res = PQexec(conn, command);

    if (PQresultStatus(res) != PGRES_TUPLES_OK) {
        addReplyErrorFormat(c, "select command failed: %s",
                            PQerrorMessage(conn));
        goto pq_error;
    }

    rows = PQntuples(res);
    if (rows != 1) {
        if (rows == 0) {
            addReplyErrorFormat(c, "no database row with key = %i", key);
        } else {
            addReplyError(c, "this should never happen. Call Tony");
        }
        goto pq_error;
    }

    for (i = 0; i < PQnfields(res); i++) {
        name = PQfname(res, i);

        if (!strcmp(name, "key") || !strcmp(name, "timestamp")) continue;

        value_str = PQgetvalue(res, 0, i);

        if (safe_strtoul(value_str, &value)) {
            addReplyErrorFormat(c, "unable to convert value '%s' for field %s",
                                value_str, name);
            goto pq_error;
        }

        if (!strcmp(name, "control_reg")) {
        	ControlReg(value);
        } else if (!strcmp(name, "trigger_mask")) {
        	triggerMask(value,0);
        } else if (!strcmp(name, "speaker_mask")) {
            speakerMask(value);
        } else if (!strcmp(name, "counter_mask")) {
            counterMask(value);
        } else if (!strcmp(name, "caen_gain_reg")) {
        	CAENWords(value, mReadReg((u32) MappedRegsBaseAddress, RegOffset12));
        } else if (!strcmp(name, "caen_channel_reg")) {
        	CAENWords(mReadReg((u32) MappedRegsBaseAddress, RegOffset11), value);
        } else if (!strcmp(name, "lockout_reg")) {
        	GTDelays(value, mReadReg((u32) MappedRegsBaseAddress, RegOffset15));
        } else if (!strcmp(name, "dgt_reg")) {
        	GTDelays(mReadReg((u32) MappedRegsBaseAddress, RegOffset14), value);
        } else if (!strcmp(name, "dac_reg")) {
        	DACThresholds(value);
        } else if (!strcmp(name, "counter_mode")) {
        	counterMode(value);
        } else if (!strcmp(name, "clock_status")) {
        	// Do Nowt
        } else if (!strcmp(name, "combo_enable_mask")) {
        	mWriteReg((u32) MappedComboBaseAddress, RegOffset2, value);
        } else if (!strcmp(name, "combo_mask")) {
        	mWriteReg((u32) MappedComboBaseAddress, RegOffset3, value);
        } else if (!strcmp(name, "prescale_value")) {
        	mWriteReg((u32) MappedPrescaleBaseAddress, RegOffset2, value);
        } else if (!strcmp(name, "prescale_channel")) {
        	mWriteReg((u32) MappedPrescaleBaseAddress, RegOffset3, value);
        } else if (!strcmp(name, "burst_rate")) {
        	mWriteReg((u32) MappedBurstBaseAddress, RegOffset2, value);
        } else if (!strcmp(name, "burst_channel")) {
        	mWriteReg((u32) MappedBurstBaseAddress, RegOffset3, value);
        } else {
            addReplyErrorFormat(c, "got unknown field '%s'", name);
            goto pq_error;
        }
    }

    addReplyStatus(c, "OK");

    PQclear(res);
    PQfinish(conn);

    return;

err:
 	addReplyError(c, tubii_err);
 	return;

pq_error:
    if (res) PQclear(res);
    PQfinish(conn);
}
Beispiel #13
0
void save_TUBii_command(client *c, int argc, sds *argv)
{
    /* Update the TUBii state. */
    uint32_t key;
    PGconn *conn;
    char conninfo[1024];
    PGresult *res = NULL;
    char command[10000];

    //char* dbname="test", dbhost="", dbuser="", dbpass="";
    sprintf(conninfo, "dbname=%s host=%s user=%s password=%s", dbconfig.name, dbconfig.host, dbconfig.user, dbconfig.password);

    /* Now we update the database */
    conn = PQconnectdb(conninfo);

    if (PQstatus(conn) != CONNECTION_OK) {
        addReplyErrorFormat(c, "connection to database failed: %s", PQerrorMessage(conn));
        goto pq_error;
    }

    sprintf(command, "insert into TUBii ("
                     "control_reg, trigger_mask, speaker_mask, counter_mask,"
    				 "caen_gain_reg, caen_channel_reg, lockout_reg, dgt_reg, dac_reg,"
    				 "combo_enable_mask, combo_mask, counter_mode, clock_status,"
    				 "prescale_value, prescale_channel, burst_rate, burst_channel"
    				 ") "
                     "VALUES ("
                     "%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u"
                     ") "
                     "RETURNING key",
                     mReadReg((u32) MappedRegsBaseAddress, RegOffset10),
                     getTriggerMask(), getSpeakerMask(), getCounterMask(),
                     mReadReg((u32) MappedRegsBaseAddress, RegOffset11),
                     mReadReg((u32) MappedRegsBaseAddress, RegOffset12),
                     mReadReg((u32) MappedRegsBaseAddress, RegOffset14),
                     mReadReg((u32) MappedRegsBaseAddress, RegOffset15),
                     mReadReg((u32) MappedRegsBaseAddress, RegOffset13),
                     mReadReg((u32) MappedComboBaseAddress, RegOffset2),
    				 mReadReg((u32) MappedComboBaseAddress, RegOffset3),
    				 counter_mode, clockStatus(),
    				 mReadReg((u32) MappedPrescaleBaseAddress, RegOffset2),
    				 mReadReg((u32) MappedPrescaleBaseAddress, RegOffset3),
    				 mReadReg((u32) MappedBurstBaseAddress, RegOffset2),
    				 mReadReg((u32) MappedBurstBaseAddress, RegOffset3)
                     );


    res = PQexec(conn, command);

    if (PQresultStatus(res) != PGRES_TUPLES_OK) {
        addReplyErrorFormat(c, "insert command failed: %s",
                            PQerrorMessage(conn));
        goto pq_error;
    }

    if (PQnfields(res) != 1) {
        addReplyError(c, "failed to get key from insert");
        goto pq_error;
    }

    if (safe_strtoul(PQgetvalue(res, 0, 0), &key)) {
        addReplyErrorFormat(c, "couldn't convert key from '%s' -> int", PQgetvalue(res, 0, 0));
        goto pq_error;
    }

    PQclear(res);
    PQfinish(conn);

    addReply(c, ":%u", key);
    return;

err:
    addReplyError(c, tubii_err);
    return;

pq_error:
    if (res) PQclear(res);
    PQfinish(conn);
}
static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
		const char *local_path, char *server_path)
{
	char buf[512];
	off_t filesize = 0;
	int fd_data;
	int fd_local = -1;
	off_t beg_range = 0;

	/* Connect to the data socket */
	if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
		bb_error_msg_and_die("PASV error: %s", buf + 4);
	}
	fd_data = xconnect_ftpdata(server, buf);

	if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
		unsigned long value=filesize;
		if (safe_strtoul(buf + 4, &value))
			bb_error_msg_and_die("SIZE error: %s", buf + 4);
		filesize = value;
	}

	if ((local_path[0] == '-') && (local_path[1] == '\0')) {
		fd_local = STDOUT_FILENO;
		do_continue = 0;
	}

	if (do_continue) {
		struct stat sbuf;
		if (lstat(local_path, &sbuf) < 0) {
			bb_perror_msg_and_die("fstat()");
		}
		if (sbuf.st_size > 0) {
			beg_range = sbuf.st_size;
		} else {
			do_continue = 0;
		}
	}

	if (do_continue) {
		sprintf(buf, "REST %ld", (long)beg_range);
		if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
			do_continue = 0;
		} else {
			filesize -= beg_range;
		}
	}

	if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
		bb_error_msg_and_die("RETR error: %s", buf + 4);
	}

	/* only make a local file if we know that one exists on the remote server */
	if (fd_local == -1) {
		if (do_continue) {
			fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
		} else {
			fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
		}
	}

	/* Copy the file */
	if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) {
		exit(EXIT_FAILURE);
	}

	/* close it all down */
	close(fd_data);
	if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
		bb_error_msg_and_die("ftp error: %s", buf + 4);
	}
	ftpcmd("QUIT", NULL, control_stream, buf);

	return(EXIT_SUCCESS);
}
static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream, char *local_path, char *server_path)
{
	int ret = -1;
	char buf[512];
	off_t filesize = 0;
	int fd_data = -1;
	int fd_local = -1;
	off_t beg_range = 0;

	/* Connect to the data socket */
	if (ftpcmd("PASV", NULL, control_stream, buf) != 227) 
	{
		printf("ftpcmd(PASV) Failed!\n");
		return -1;
	}
	
	fd_data = connect_ftpdata(server, buf);
	if (fd_data == -1)
	{
		printf("connect_ftpdata(%s) Failed!\n", buf);
		return -1;
	}

	if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) 
	{
		unsigned long value=filesize;
		//if (strtoul(buf + 4, &value, 10))
		if (safe_strtoul(buf + 4, &value))
		{			
			close(fd_data);
			return -1;
		}
		filesize = value;
	}

	if ((local_path[0] == '-') && (local_path[1] == '\0')) 
	{
		fd_local = STDOUT_FILENO;
		do_continue = 0;
	}

	if (do_continue) 
	{
		struct stat sbuf;
		if (lstat(local_path, &sbuf) < 0) 
		{
			close(fd_data);
			return -1;
		}
		if (sbuf.st_size > 0) 
		{
			beg_range = sbuf.st_size;
		} 
		else 
		{
			do_continue = 0;
		}
	}

	if (do_continue) 
	{
		sprintf(buf, "REST %ld", (long)beg_range);
		if (ftpcmd(buf, NULL, control_stream, buf) != 350) 
		{
			do_continue = 0;
		} 
		else 
		{
			filesize -= beg_range;
		}
	}

	if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) 
	{
		close(fd_data);
		return -1;
	}
	
	/* only make a local file if we know that one exists on the remote server */
	if (fd_local == -1) 
	{
		if (do_continue) 
		{
			fd_local = open(local_path, O_APPEND | O_WRONLY, 0777);
		} 
		else 
		{
			fd_local = open(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0777);
		}
	}

	if (fd_local < 0)
	{
		close(fd_data);
		return -1;
	}
	
	// Copy the file
	if (copyfd_size(fd_data, fd_local, filesize) == -1) 
	{		
		close(fd_data);
		close(fd_local);
		return -1;
	}	

	// close it all down
	close(fd_data);
	close(fd_local);
	
	if (ftpcmd(NULL, NULL, control_stream, buf) != 226) 
	{
		return -1;
	}
	
	ftpcmd("QUIT", NULL, control_stream, buf);

	return 0;
}
Beispiel #16
0
void cproxy_process_a2a_downstream(conn *c, char *line) {
    assert(c != NULL);
    assert(c->next == NULL);
    assert(c->extra != NULL);
    assert(c->cmd == -1);
    assert(c->item == NULL);
    assert(line != NULL);
    assert(line == c->rcurr);
    assert(IS_ASCII(c->protocol));
    assert(IS_PROXY(c->protocol));

    if (settings.verbose > 1)
        fprintf(stderr, "<%d cproxy_process_a2a_downstream %s\n",
                c->sfd, line);

    downstream *d = c->extra;

    assert(d != NULL);
    assert(d->ptd != NULL);
    assert(d->ptd->proxy != NULL);

    if (strncmp(line, "VALUE ", 6) == 0) {
        token_t      tokens[MAX_TOKENS];
        size_t       ntokens;
        unsigned int flags;
        int          clen = 0;
        int          vlen;
        uint64_t     cas = CPROXY_NOT_CAS;

        ntokens = scan_tokens(line, tokens, MAX_TOKENS, &clen);
        if (ntokens >= 5 && // Accounts for extra termimation token.
            ntokens <= 6 &&
            tokens[KEY_TOKEN].length <= KEY_MAX_LENGTH &&
            safe_strtoul(tokens[2].value, (uint32_t *) &flags) &&
            safe_strtoul(tokens[3].value, (uint32_t *) &vlen)) {
            char  *key  = tokens[KEY_TOKEN].value;
            size_t nkey = tokens[KEY_TOKEN].length;

            item *it = item_alloc(key, nkey, flags, 0, vlen + 2);
            if (it != NULL) {
                if (ntokens == 5 ||
                    safe_strtoull(tokens[4].value, &cas)) {
                    ITEM_set_cas(it, cas);

                    c->item = it;
                    c->ritem = ITEM_data(it);
                    c->rlbytes = it->nbytes;
                    c->cmd = -1;

                    conn_set_state(c, conn_nread);

                    return; // Success.
                } else {
                    if (settings.verbose > 1)
                        fprintf(stderr, "cproxy could not parse cas\n");
                }
            } else {
                if (settings.verbose > 1)
                    fprintf(stderr, "cproxy could not item_alloc size %u\n",
                            vlen + 2);
            }

            if (it != NULL)
                item_remove(it);
            it = NULL;

            c->sbytes = vlen + 2; // Number of bytes to swallow.

            conn_set_state(c, conn_swallow);

            // Note, eventually, we'll see an END later.
        } else {
            // We don't know how much to swallow, so close the downstream.
            // The conn_closing should release the downstream,
            // which should write a suffix/error to the upstream.
            //
            conn_set_state(c, conn_closing);
        }
    } else if (strncmp(line, "END", 3) == 0) {
        conn_set_state(c, conn_pause);
    } else if (strncmp(line, "OK", 2) == 0) {
        conn_set_state(c, conn_pause);

        // TODO: Handle flush_all's expiration parameter against
        // the front_cache.
        //
        // TODO: We flush the front_cache too often, inefficiently
        // on every downstream flush_all OK response, rather than
        // on just the last flush_all OK response.
        //
        conn *uc = d->upstream_conn;
        if (uc != NULL &&
            uc->cmd_curr == PROTOCOL_BINARY_CMD_FLUSH) {
            mcache_flush_all(&d->ptd->proxy->front_cache, 0);
        }
    } else if (strncmp(line, "STAT ", 5) == 0 ||
               strncmp(line, "ITEM ", 5) == 0 ||
               strncmp(line, "PREFIX ", 7) == 0) {
        assert(d->merger != NULL);

        conn *uc = d->upstream_conn;
        if (uc != NULL) {
            assert(uc->next == NULL);

            if (protocol_stats_merge_line(d->merger, line) == false) {
                // Forward the line as-is if we couldn't merge it.
                //
                int nline = strlen(line);

                item *it = item_alloc("s", 1, 0, 0, nline + 2);
                if (it != NULL) {
                    strncpy(ITEM_data(it), line, nline);
                    strncpy(ITEM_data(it) + nline, "\r\n", 2);

                    if (add_conn_item(uc, it)) {
                        add_iov(uc, ITEM_data(it), nline + 2);

                        it = NULL;
                    }

                    if (it != NULL)
                        item_remove(it);
                }
            }
        }

        conn_set_state(c, conn_new_cmd);
    } else {
        conn_set_state(c, conn_pause);

        // The upstream conn might be NULL when closed already
        // or while handling a noreply.
        //
        conn *uc = d->upstream_conn;
        if (uc != NULL) {
            assert(uc->next == NULL);

            out_string(uc, line);

            if (!update_event(uc, EV_WRITE | EV_PERSIST)) {
                if (settings.verbose > 1)
                    fprintf(stderr,
                            "Can't update upstream write event\n");

                d->ptd->stats.stats.err_oom++;
                cproxy_close_conn(uc);
            }

            cproxy_del_front_cache_key_ascii_response(d, line,
                                                      uc->cmd_start);
        }
    }
}
Beispiel #17
0
static int parseFirstLineRequest(fallocator_t fallocator, requestParserImpl_t* pParser, char** tokens, int ntokens) {
	int returnValue = 0;

	if (ntokens >= 2 && (((strcmp(tokens[0], "get") == 0) && (pParser->pCommand->command
			= COMMAND_GET)) || ((strcmp(tokens[0], "bget") == 0)
			&& (pParser->pCommand->command = COMMAND_BGET)))) {

		if (ntokens > 2) {
			//copy the keys ...
			pParser->pCommand->multiGetKeys = fallocatorMalloc(fallocator, (ntokens-1) * sizeof(char*));
			IfTrue(pParser->pCommand->multiGetKeys, WARN, "Error allocating memory");
			pParser->pCommand->multiGetKeysCount = ntokens -1 ;
			for (int i = 1; i < ntokens; i++) {
				pParser->pCommand->multiGetKeys[i-1] = tokens[i];
				tokens[i] = 0;
			}
		}else {
			pParser->pCommand->key = tokens[1];
			pParser->pCommand->keySize = strlen(tokens[1]);
			tokens[1] = 0;
		}
	} else if ((ntokens == 5 || ntokens == 6) && ((strcmp(tokens[0], "add")
			== 0 && (pParser->pCommand->command = COMMAND_ADD)) || (strcmp(tokens[0],
			"set") == 0 && (pParser->pCommand->command = COMMAND_SET)) || (strcmp(
			tokens[0], "replace") == 0 && (pParser->pCommand->command = COMMAND_REPLACE))
			|| (strcmp(tokens[0], "prepend") == 0 && (pParser->pCommand->command
					= COMMAND_PREPEND)) || (strcmp(tokens[0], "append") == 0
			&& (pParser->pCommand->command = COMMAND_APPEND)))) {
		pParser->pCommand->key = tokens[1];
		pParser->pCommand->keySize = strlen(tokens[1]);
		tokens[1] = 0;
		IfTrue(safe_strtoul(tokens[2], &pParser->pCommand->flags), INFO, "Error parsing flags");
		IfTrue(safe_strtoul(tokens[3], &pParser->pCommand->expiryTime), INFO, "Error parsing expiry time ");
		IfTrue(safe_strtoul(tokens[4], &pParser->pCommand->dataLength), INFO, "Error parsing data length");

		if (tokens[5] != NULL) {
			if (strcmp(tokens[5], "noreply") == 0) {
				pParser->pCommand->noreply = 1;
			}
		}

	} else if ((ntokens == 6 || ntokens == 7)
			&& (strcmp(tokens[0], "cas") == 0)) {
		pParser->pCommand->command = COMMAND_CAS;

		pParser->pCommand->key = tokens[1];
		pParser->pCommand->keySize = strlen(tokens[1]);
		tokens[1] = 0;
		IfTrue(safe_strtoul(tokens[2], &pParser->pCommand->flags), INFO, "Error parsing flags");
		IfTrue(safe_strtoul(tokens[3], &pParser->pCommand->expiryTime), INFO, "Error parsing expiry time ");
		IfTrue(safe_strtoul(tokens[4], &pParser->pCommand->dataLength), INFO, "Error parsing data length");
		IfTrue(safe_strtoull(tokens[5], &pParser->pCommand->cas), INFO, "Error parsing cas id");

		if (tokens[6] != NULL) {
			if (strcmp(tokens[6], "noreply") == 0) {
				pParser->pCommand->noreply = 1;
			}
		}

	} else if ((ntokens == 3 || ntokens == 4) && (strcmp(tokens[0], "incr")
			== 0)) {
		pParser->pCommand->command = COMMAND_INCR;
		pParser->pCommand->key = tokens[1];
		pParser->pCommand->keySize = strlen(tokens[1]);
		tokens[1] = 0;

		IfTrue(safe_strtoull(tokens[2], &pParser->pCommand->delta), INFO, "Error parsing delta");
		if (tokens[3] != NULL) {
			if (strcmp(tokens[3], "noreply") == 0) {
				pParser->pCommand->noreply = 1;
			}
		}
	} else if (ntokens >= 2 && (strcmp(tokens[0], "gets") == 0)) {
		pParser->pCommand->command = COMMAND_GETS;
		pParser->pCommand->key = tokens[1];
		pParser->pCommand->keySize = strlen(tokens[1]);
		tokens[1] = 0;
		//TODO - only suport one key per get for now

	} else if ((ntokens == 4 || ntokens == 5) && (strcmp(tokens[0], "decr")
			== 0)) {
		pParser->pCommand->command = COMMAND_DECR;
		pParser->pCommand->key = tokens[1];
		pParser->pCommand->keySize = strlen(tokens[1]);
		tokens[1] = 0;

		IfTrue(safe_strtoull(tokens[2], &pParser->pCommand->delta), INFO, "Error parsing delta");
		if (tokens[3] != NULL) {
			if (strcmp(tokens[3], "noreply") == 0) {
				pParser->pCommand->noreply = 1;
			}
		}
	} else if (ntokens >= 2 && ntokens <= 4 && (strcmp(tokens[0], "delete")
			== 0)) {
		pParser->pCommand->command = COMMAND_DELETE;
		pParser->pCommand->key = tokens[1];
		pParser->pCommand->keySize = strlen(tokens[1]);
		tokens[1] = 0;
		if (tokens[2] != NULL) {
			if (strcmp(tokens[2], "noreply") == 0) {
				pParser->pCommand->noreply = 1;
			}
		}
	} else if (ntokens >= 2 && (strcmp(tokens[0], "stats") == 0)) {
		pParser->pCommand->command = COMMAND_STATS;
		//TODO - later
	} else if (ntokens >= 1 && ntokens <= 2 && (strcmp(tokens[0], "flush_all")
			== 0)) {
		pParser->pCommand->command = COMMAND_FLUSH_ALL;
		//TODO - later
	} else if (ntokens == 1 && (strcmp(tokens[0], "version") == 0)) {
		pParser->pCommand->command = COMMAND_VERSION;
		//TODO - later
	} else if (ntokens == 1 && (strcmp(tokens[0], "quit") == 0)) {
		pParser->pCommand->command = COMMAND_QUIT;
		//TODO - later
	} else if ((ntokens == 2 || ntokens == 3)
			&& (strcmp(tokens[0], "verbosity") == 0)) {
		pParser->pCommand->command = COMMAND_VERBOSITY;
		IfTrue(safe_strtoul(tokens[2], &pParser->pCommand->flags), INFO,
				"Error parsing verosity level");
	} else {
		//this is error
	}
	goto OnSuccess;
OnError:
	returnValue = -1;
OnSuccess:
	return returnValue;
}