Пример #1
0
int main(int argc, char **argv)
{
	int greeting = TRUE;
	char filename[256];

	#ifdef SOCKS
		SOCKSinit(argv[0]);
	#endif

	if (signal(SIGTERM, trap_handler) == BADSIG)
	{
		syserr("signal SIGTERM");
	}

	if (signal(SIGSEGV, trap_handler) == BADSIG)
	{
		syserr("signal SIGSEGV");
	}

	if (signal(SIGHUP, trap_handler) == BADSIG)
	{
		syserr("signal SIGHUP");
	}

	if (signal(SIGABRT, abort_handler) == BADSIG)
	{
		syserr("signal SIGTERM");
	}

	if (signal(SIGINT, interrupt_handler) == BADSIG)
	{
		syserr("signal SIGINT");
	}

	if (signal(SIGTSTP, suspend_handler) == BADSIG)
	{
		syserr("signal SIGSTOP");
	}

	if (signal(SIGPIPE, pipe_handler) == BADSIG)
	{
		syserr("signal SIGPIPE");
	}

	if (signal(SIGWINCH, winch_handler) == BADSIG)
	{
		syserr("signal SIGWINCH");
	}


	srand(time(NULL));

	if (argc > 1)
	{
		int c;

		while ((c = getopt(argc, argv, "e: G h r: s: t: v")) != EOF)
		{
			if (c == 'G')
			{
				greeting = FALSE;
			}
		}

		optind = 1;
	}

	init_tintin(greeting);

	sprintf(filename, "%s/%s", gtd->home, TINTIN_DIR);

	if (mkdir(filename, 0777) || errno == EEXIST)
	{
		sprintf(filename, "%s/%s/%s", gtd->home, TINTIN_DIR, HISTORY_FILE);

		if (access(filename, F_OK ) != -1)
		{
			history_read(gts, filename);
		}
	}

	if (argc > 1)
	{
		int c;

		optind = 1;

		while ((c = getopt(argc, argv, "e: G h r: s: t: v")) != EOF)
		{
			switch (c)
			{
				case 'e':
					gtd->quiet++;
					gtd->ses = script_driver(gtd->ses, LIST_COMMAND, optarg);
					gtd->quiet--;
					break;

				case 'G':
					break;

				case 'h':
					tintin_printf(NULL, "Usage: %s [OPTION]... [FILE]...", argv[0]);
					tintin_printf(NULL, "");
					tintin_printf(NULL, "  -e  Execute given command.");
					tintin_printf(NULL, "  -G  Don't show the greeting screen.");
					tintin_printf(NULL, "  -h  This help section.");
					tintin_printf(NULL, "  -r  Read given file.");
					tintin_printf(NULL, "  -t  Set given title.");
					tintin_printf(NULL, "  -v  Enable verbose mode.");

					restore_terminal();
					exit(1);
					break;

				case 'r':
					gtd->ses = do_read(gtd->ses, optarg);
					break;

				case 't':
					printf("\033]0;%s\007", optarg);
					break;

				case 's':
					srand((unsigned int) atoll(optarg));
					break;

				case 'v':
					do_configure(gtd->ses, "{VERBOSE} {ON}");
					break;

				default:
					tintin_printf(NULL, "Unknown option '%c'.", c);
					break;
			}
		}

		if (argv[optind] != NULL)
		{
			gtd->ses = do_read(gtd->ses, argv[optind]);
		}
	}


	check_all_events(gts, SUB_ARG|SUB_SEC, 0, 2, "PROGRAM START", CLIENT_NAME, CLIENT_VERSION);
	check_all_events(gts, SUB_ARG|SUB_SEC, 0, 2, "SCREEN RESIZE", ntos(gts->cols), ntos(gts->rows));

	mainloop();

	return 0;
}
Пример #2
0
/* Iterate over the configure firewall access chains and purge expired
 * firewall rules.
*/
void
check_firewall_rules(const fko_srv_options_t * const opts)
{
    char             exp_str[12]     = {0};
    char             rule_num_str[6] = {0};
    char            *ndx, *rn_start, *rn_end, *tmp_mark;

    int             i, res, rn_offset, rule_num, is_err;
    time_t          now, rule_exp, min_exp = 0;

    struct fw_chain *ch = opts->fw_config->chain;

    time(&now);

    /* Iterate over each chain and look for active rules to delete.
    */
    for(i=0; i < NUM_FWKNOP_ACCESS_TYPES; i++)
    {
        /* If there are no active rules or we have not yet
         * reached our expected next expire time, continue.
        */
        if(ch[i].active_rules == 0 || ch[i].next_expire > now)
            continue;

        zero_cmd_buffers();

        rn_offset = 0;

        /* There should be a rule to delete.  Get the current list of
         * rules for this chain and delete the ones that are expired.
        */
        snprintf(cmd_buf, CMD_BUFSIZE-1, "%s " IPT_LIST_RULES_ARGS,
            opts->fw_config->fw_command,
            ch[i].table,
            ch[i].to_chain
        );

        res = run_extcmd(cmd_buf, cmd_out, STANDARD_CMD_OUT_BUFSIZE, 0);
        chop_newline(cmd_out);

        log_msg(LOG_DEBUG, "check_firewall_rules() CMD: '%s' (res: %d, cmd_out: %s)",
            cmd_buf, res, cmd_out);

        if(!EXTCMD_IS_SUCCESS(res))
        {
            log_msg(LOG_ERR, "Error %i from cmd:'%s': %s", res, cmd_buf, cmd_out);
            continue;
        }

        log_msg(LOG_DEBUG, "RES=%i, CMD_BUF: %s\nRULES LIST: %s", res, cmd_buf, cmd_out);

        ndx = strstr(cmd_out, EXPIRE_COMMENT_PREFIX);
        if(ndx == NULL)
        {
            /* we did not find an expected rule.
            */
            log_msg(LOG_ERR,
                "Did not find expire comment in rules list %i", i);

            if (ch[i].active_rules > 0)
                ch[i].active_rules--;

            continue;
        }

        /* walk the list and process rules as needed.
        */
        while (ndx != NULL) {
            /* Jump forward and extract the timestamp
            */
            ndx += strlen(EXPIRE_COMMENT_PREFIX);

            /* remember this spot for when we look for the next
             * rule.
            */
            tmp_mark = ndx;

            strlcpy(exp_str, ndx, sizeof(exp_str));
            rule_exp = (time_t)atoll(exp_str);

            if(rule_exp <= now)
            {
                /* Backtrack and get the rule number and delete it.
                */
                rn_start = ndx;
                while(--rn_start > cmd_out)
                {
                    if(*rn_start == '\n')
                        break;
                }

                if(*rn_start != '\n')
                {
                    /* This should not happen. But if it does, complain,
                     * decrement the active rule value, and go on.
                    */
                    log_msg(LOG_ERR,
                        "Rule parse error while finding rule line start in chain %i", i);

                    if (ch[i].active_rules > 0)
                        ch[i].active_rules--;

                    break;
                }
                rn_start++;

                rn_end = strchr(rn_start, ' ');
                if(rn_end == NULL)
                {
                    /* This should not happen. But if it does, complain,
                     * decrement the active rule value, and go on.
                    */
                    log_msg(LOG_ERR,
                        "Rule parse error while finding rule number in chain %i", i);

                    if (ch[i].active_rules > 0)
                        ch[i].active_rules--;

                    break;
                }

                strlcpy(rule_num_str, rn_start, (rn_end - rn_start)+1);

                rule_num = strtol_wrapper(rule_num_str, rn_offset, RCHK_MAX_IPT_RULE_NUM,
                        NO_EXIT_UPON_ERR, &is_err);
                if(is_err != FKO_SUCCESS)
                {
                    log_msg(LOG_ERR,
                        "Rule parse error while finding rule number in chain %i", i);

                    if (ch[i].active_rules > 0)
                        ch[i].active_rules--;

                    break;
                }

                zero_cmd_buffers();

                snprintf(cmd_buf, CMD_BUFSIZE-1, "%s " IPT_DEL_RULE_ARGS,
                    opts->fw_config->fw_command,
                    ch[i].table,
                    ch[i].to_chain,
                    rule_num - rn_offset
                );

                res = run_extcmd(cmd_buf, err_buf, CMD_BUFSIZE, 0);
                chop_newline(err_buf);

                log_msg(LOG_DEBUG, "check_firewall_rules() CMD: '%s' (res: %d, err: %s)",
                    cmd_buf, res, err_buf);

                if(EXTCMD_IS_SUCCESS(res))
                {
                    log_msg(LOG_INFO, "Removed rule %s from %s with expire time of %u",
                        rule_num_str, ch[i].to_chain, rule_exp
                    );

                    rn_offset++;

                    if (ch[i].active_rules > 0)
                        ch[i].active_rules--;
                }
                else
                    log_msg(LOG_ERR, "Error %i from cmd:'%s': %s", res, cmd_buf, err_buf);

            }
            else
            {
                /* Track the minimum future rule expire time.
                */
                if(rule_exp > now)
                    min_exp = (min_exp < rule_exp) ? min_exp : rule_exp;
            }

            /* Push our tracking index forward beyond (just processed) _exp_
             * string so we can continue to the next rule in the list.
            */
            ndx = strstr(tmp_mark, EXPIRE_COMMENT_PREFIX);
        }

        /* Set the next pending expire time accordingly. 0 if there are no
         * more rules, or whatever the next expected (min_exp) time will be.
        */
        if(ch[i].active_rules < 1)
            ch[i].next_expire = 0;
        else if(min_exp)
            ch[i].next_expire = min_exp;
    }
}
/**
    Parser callback for OSMDocument files
*/
void OSMDocumentParserCallback::StartElement(const char *name, const char** atts) {
    // START RELATIONS CODE
    if (strcmp(name, "member") == 0) {
        // std::cout << "In member..." << std::endl;

        if (m_pActRelation && atts != NULL) {
            const char** attribut = (const char**)atts;
            while (*attribut != NULL) {
                const char* name = *attribut++;
                const char* value = *attribut++;
                if (strcmp(name, "ref") == 0) {
                    long long wayRefId = atoll(value);
                    m_pActRelation->AddWayRef(wayRefId);
                }
            }
        }
    }
    // END RELATIONS CODE


    if (strcmp(name, "nd") == 0) {
        if (m_pActWay && atts != NULL) {
            const char* name = *atts++;
            const char* value = *atts++;
            if (strcmp(name, "ref") == 0) {
                long long nodeRefId = atoll(value);
                m_pActWay->AddNodeRef(m_rDocument.FindNode(nodeRefId));
                Node * node = m_rDocument.FindNode(nodeRefId);
                if (node != 0) {
                    node->numsOfUse+=1;
                } else {
                    std::cout << "Reference nd=" << nodeRefId
                              << " has no corresponding Node Entry (Maybe Node entry after Reference?)" << std::endl;
                }
            }
        }
    } else if (strcmp(name, "node") == 0) {
        if (atts != NULL) {
            long long id =-1;
            double lat =-1;
            double lon =-1;
            const char** attribut = (const char**)atts;
            while (*attribut != NULL) {
                const char* name = *attribut++;
                const char* value = *attribut++;
                if (strcmp(name, "id") == 0) {
                    id = atoll(value);
                } else if (strcmp(name, "lat") == 0) {
                    lat = atof(value);
                } else if (strcmp(name, "lon") == 0) {
                    lon = atof(value);
                }
            }
            if (id > 0) m_rDocument.AddNode(new Node(id, lat, lon));
        }
    } else if (strcmp(name, "relation") == 0) {   // THIS IS THE RELATION CODE...
        if (atts != NULL) {
            long long id =-1;
            const char** attribut = (const char**)atts;
            while (*attribut != NULL) {
                const char* name = *attribut++;
                const char* value = *attribut++;
                if (strcmp(name, "id") == 0) {
                    id = atoll(value);
                }
            }
            if (id > 0) m_pActRelation = new Relation(id);
            // std::cout<<"Starting relation: "<<id<<std::endl;
        }
    } else if (strcmp(name, "tag") == 0) {  // END OF THE RELATIONS CODE
        // <tag k="name" v="Pf�nderweg"/>
        if (atts != NULL) {
            std::string k;
            std::string v;
            const char** attribut = (const char**)atts;
            while (*attribut != NULL) {
                const char* name = *attribut++;
                const char* value = *attribut++;
                if (strcmp(name, "k") == 0) {
                    k = value;
                } else if (strcmp(name, "v") == 0) {
                    v = value;
                }
            }
            if (!k.empty()) {
                //  CHECKING OUT SOME DATA...
                // std::cout<<"k: "<<k<<", v: "<<v<<std::endl;
                // std::cout<<"m_pActWay: "<<m_rDocument.m_rConfig.m_Types.count(k)<<std::endl;
                // std::cout<<"thecount: "<<m_rDocument.m_rConfig.m_Types.count(k)<<std::endl;
                if (m_pActWay && k.compare("name") == 0) {
                    m_pActWay->name = v;
                } else if (m_pActWay && k.compare("oneway") == 0) {  // checks ONEWAY tag
                    if ((v.compare("yes") == 0) || (v.compare("true") == 0) || (v.compare("1") == 0)) {
                        m_pActWay->oneWayType = YES;
                    } else {}
                    // check false conditions: 0, no, false
                    if ((v.compare("no") == 0) || (v.compare("false") == 0) || (v.compare("0") == 0)) {
                        m_pActWay->oneWayType = NO;
                    } else {}
                    // check revers conditions: -1
                    if ((v.compare("-1") == 0)) {
                        m_pActWay->oneWayType = REVERSED;
                    }
                    // in case roundabout, if there is not additional oneway tag, set default oneway to YES
                } else if (m_pActWay && k.compare("junction") == 0 && v.compare("roundabout") == 0) {
                    if (m_pActWay->oneWayType == UNKNOWN) m_pActWay->oneWayType= YES;
                }
                // handle 3 cases if the key contains maxspeed
                else if (m_pActWay && k.find("maxspeed") != std::string::npos) {
                    // If the value contains mph, strip unit, convert to kph.
                    if (v.find("mph") != std::string::npos) {
                        // Assume format is /[0-9]{1,3} ?mph/
                        v = my_utils::read_number_substring(v);
                        std::ostringstream ostr;
                        ostr << floor(atoi(v.c_str()) * 1.60934);
                        v = ostr.str();
                    }
                    // handle maxspeed:forward tag
                    if (k.compare("maxspeed:forward") == 0) {
                        int mspeed_fwd = 50;

                        if (my_utils::is_number(v)) {
                            mspeed_fwd = atoi(v.c_str());
                        } else {
                            // TODO: handle non-numeric values, ex.: RO:urban
                            std::cout << "unknown maxspeed1 value: " << v << std::endl;
                        }
                        m_pActWay->maxspeed_forward = mspeed_fwd;
                    }
                    // handler maxspeed:backward
                    else if (k.compare("maxspeed:backward") == 0) {
                        int mspeed_backwd = 50;

                        if (my_utils::is_number(v)) {
                            mspeed_backwd = atoi(v.c_str());
                        } else {
                            // TODO: handle non-numeric values, ex.: RO:urban
                            std::cout << "unknown maxspeed2 value: " << v << std::endl;
                        }
                        m_pActWay->maxspeed_backward = mspeed_backwd;
                    } else if (k.compare("maxspeed") == 0) {
                        int mspeed_fwd = 50;
                        int mspeed_backwd = 50;

                        if (my_utils::is_number(v)) {
                            mspeed_fwd = atoi(v.c_str());
                            mspeed_backwd = atoi(v.c_str());
                        } else {
                            // TODO: handle non-numeric values, ex.: RO:urban
                            std::cout << "unknown maxspeed3 value: " << v << std::endl;
                        }
                        m_pActWay->maxspeed_backward = mspeed_backwd;
                        m_pActWay->maxspeed_forward = mspeed_fwd;
                    }
                }
                // else if (m_pActWay && k.compare("highway") == 0)
                else if (m_pActWay && m_rDocument.m_rConfig.m_Types.count(k)) {
                    m_pActWay->type = k;
                    m_pActWay->clss = v;

                    if (m_rDocument.m_rConfig.m_Types.count(m_pActWay->type) && m_rDocument.m_rConfig.m_Types[m_pActWay->type]->m_Classes.count(m_pActWay->clss)) {
                        m_pActWay->AddTag(k, v);

                        // std::cout<<"Added tag: "<<k<<" "<<v<<std::endl;

                        // set default maxspeed values from classes, if not set previously (default: -1)
                        if (m_pActWay->maxspeed_forward <= 0) {
                            int newValue = m_rDocument.m_rConfig.m_Types[m_pActWay->type]->m_Classes[m_pActWay->clss]->default_maxspeed;
                            m_pActWay->maxspeed_forward = newValue;
                        }
                        if (m_pActWay->maxspeed_backward <= 0) {
                            int newValue = m_rDocument.m_rConfig.m_Types[m_pActWay->type]->m_Classes[m_pActWay->clss]->default_maxspeed;
                            m_pActWay->maxspeed_backward = newValue;
                        }
                    }
                }
                // START TAG FOR RELATION
                else if (m_pActRelation && m_rDocument.m_rConfig.m_Types.count(k))  {
                    if (m_rDocument.m_rConfig.m_Types.count(k) && m_rDocument.m_rConfig.m_Types[k]->m_Classes.count(v)) {
                        m_pActRelation->AddTag(k, v);
                        // std::cout<<"Added Relation tag: "<<k<<" "<<v<<std::endl;
                    }
                    // std::cout<<"Relations tag: "<<k<<" "<<v<<std::endl;
                }
                if (m_pActRelation && k.compare("name") == 0) {
                    m_pActRelation->name = v;
                }
                // END TAG FOR RELATION
            }
        }
    } else if (strcmp(name, "way") == 0) {
        if (atts != NULL) {
            long long id =-1;
            bool visibility = false;
            const char** attribut = (const char**)atts;
            while (*attribut != NULL) {
                const char* name = *attribut++;
                const char* value = *attribut++;
                if (strcmp(name, "id") == 0) {
                    id = atoll(value);
                } else if (strcmp(name, "visible") == 0) {
                    visibility = strcmp(value, "true") == 0;
                }
            }
            if (id > 0) {
                m_pActWay = new Way(id, visibility, id , -1, -1);
            }
        }
    } else if (strcmp(name, "osm") == 0) {
    }
}
Пример #4
0
int main(int argc, char* argv[])
{
	int c;
	char *mem;
	float percent;
	unsigned int maxpercent = 0, dowrite = 0, verbose=0, j;
	unsigned long bytecount, alloc_bytes, max_pids;
	unsigned long long original_maxbytes, maxbytes = 0;
	unsigned long long pre_mem = 0, post_mem = 0;
	unsigned long long total_ram, total_free, D, C;
	int chunksize = 1024*1024; /* one meg at a time by default */
	struct sysinfo sstats;
	int i, pid_cntr;
	pid_t pid, *pid_list;
	struct sigaction act;

	act.sa_handler = handler;
	act.sa_flags = 0;
	sigemptyset(&act.sa_mask);
	sigaction(SIGRTMIN,  &act, 0);

	while ((c = getopt(argc, argv, "c:b:p:wvh")) != -1) {
		switch(c) {
		case 'c':
			chunksize = atoi(optarg);
			break;
		case 'b':
			if (maxpercent != 0)
				tst_brkm(TBROK, NULL,
				    "ERROR: -b option cannot be used with -p "
				    "option at the same time");
			maxbytes = atoll(optarg);
			break;
		case 'p':
			if (maxbytes != 0)
				tst_brkm(TBROK, NULL,
				    "ERROR: -p option cannot be used with -b "
				    "option at the same time");
			maxpercent = atoi(optarg);
			if (maxpercent <= 0)
				tst_brkm(TBROK, NULL,
				    "ERROR: -p option requires number greater "
				    "than 0");
			if (maxpercent > 99)
				tst_brkm(TBROK, NULL,
				    "ERROR: -p option cannot be greater than "
				    "99");
			break;
		case 'w':
			dowrite = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		case 'h':
		default:
			printf("usage: %s [-c <bytes>] [-b <bytes>|-p <percent>] [-v]\n", argv[0]);
			printf("\t-c <num>\tsize of chunk in bytes to malloc on each pass\n");
			printf("\t-b <bytes>\tmaximum number of bytes to allocate before stopping\n");
			printf("\t-p <bytes>\tpercent of total memory used at which the program stops\n");
			printf("\t-w\t\twrite to the memory after allocating\n");
			printf("\t-v\t\tverbose\n");
			printf("\t-h\t\tdisplay usage\n");
			exit(1);
		}
	}

	sysinfo(&sstats);
	total_ram = sstats.totalram + sstats.totalswap;
	total_free = sstats.freeram + sstats.freeswap;
	/* Total Free Pre-Test RAM */
	pre_mem = sstats.mem_unit * total_free;
	max_pids = total_ram / (unsigned long)FIVE_HUNDRED_MB + 1;

	if ((pid_list = malloc(max_pids * sizeof(pid_t))) == NULL)
		tst_brkm(TBROK|TERRNO, NULL, "malloc failed.");
	memset(pid_list, 0, max_pids * sizeof(pid_t));

	/* Currently used memory */
	C = sstats.mem_unit * (total_ram - total_free);
	tst_resm(TINFO, "Total memory already used on system = %llu kbytes",
	    C / 1024);

	if (maxpercent) {
		percent = (float)maxpercent / 100.00;

		/* Desired memory needed to reach maxpercent */
		D = percent * (sstats.mem_unit * total_ram);
		tst_resm(TINFO,
		    "Total memory used needed to reach maximum = %llu kbytes",
		    D / 1024);

		/* Are we already using more than maxpercent? */
		if (C > D) {
			tst_resm(TFAIL,
			    "More memory than the maximum amount you specified "
			    " is already being used");
			free(pid_list);
			tst_exit();
		}

		/* set maxbytes to the extra amount we want to allocate */
		maxbytes = D - C;
		tst_resm(TINFO, "Filling up %d%% of ram which is %llu kbytes",
		    maxpercent, maxbytes / 1024);
	}
	original_maxbytes = maxbytes;
	i = 0;
	pid_cntr = 0;
	pid = fork();
	if (pid != 0)
		pid_cntr++;
	pid_list[i] = pid;

#if defined (_s390_) /* s390's 31bit addressing requires smaller chunks */
	while (pid != 0 && maxbytes > FIVE_HUNDRED_MB) {
		i++;
		maxbytes -= FIVE_HUNDRED_MB;
		pid = fork();
		if (pid != 0) {
			pid_cntr++;
			pid_list[i] = pid;
		}
	}
	if (maxbytes > FIVE_HUNDRED_MB)
		alloc_bytes = FIVE_HUNDRED_MB;
	else
		alloc_bytes = (unsigned long) maxbytes;

#elif __WORDSIZE == 32
	while (pid != 0 && maxbytes > ONE_GB) {
		i++;
		maxbytes -= ONE_GB;
		pid = fork();
		if (pid != 0) {
			pid_cntr++;
			pid_list[i]=pid;
		}
	}
	if (maxbytes > ONE_GB)
		alloc_bytes = ONE_GB;
	else
		alloc_bytes = (unsigned long)maxbytes;

#elif __WORDSIZE == 64
	while (pid != 0 && maxbytes > THREE_GB) {
		i++;
		maxbytes -= THREE_GB;
		pid = fork();
		if (pid != 0) {
			pid_cntr++;
			pid_list[i] = pid;
		}
	}
	if (maxbytes > THREE_GB)
		alloc_bytes = THREE_GB;
	else
		alloc_bytes = maxbytes;
#endif

	if (pid == 0) {
		bytecount = chunksize;
		while (1) {
			if ((mem = malloc(chunksize)) == NULL) {
				tst_resm(TBROK|TERRNO,
				    "stopped at %lu bytes", bytecount);
				free(pid_list);
				tst_exit();
			}
			if (dowrite)
				for (j = 0; j < chunksize; j++)
					*(mem+j) = 'a';
			if (verbose)
				tst_resm(TINFO,
				    "allocated %lu bytes chunksize is %d",
				    bytecount, chunksize);
			bytecount += chunksize;
			if (alloc_bytes && bytecount >= alloc_bytes)
				break;
		}
		if (dowrite)
			tst_resm(TINFO, "... %lu bytes allocated and used.",
			    bytecount);
		else
			tst_resm(TINFO, "... %lu bytes allocated only.",
			    bytecount);
		kill(getppid(), SIGRTMIN);
		while (1)
			sleep(1);
	} else {
		i = 0;
		sysinfo(&sstats);

		if (dowrite) {
			/* Total Free Post-Test RAM */
			post_mem = (unsigned long long)sstats.mem_unit * sstats.freeram;
			post_mem = post_mem + (unsigned long long)sstats.mem_unit * sstats.freeswap;

			while ((((unsigned long long)pre_mem - post_mem) <
			    (unsigned long long)original_maxbytes) &&
			    pid_count < pid_cntr) {
				sleep(1);
				sysinfo(&sstats);
				post_mem = (unsigned long long)sstats.mem_unit * sstats.freeram;
				post_mem = post_mem + (unsigned long long)sstats.mem_unit * sstats.freeswap;
			}
		}
		while (pid_list[i] != 0) {
			kill(pid_list[i], SIGKILL);
			i++;
		}
		if (dowrite)
			tst_resm(TPASS, "%llu kbytes allocated and used.",
			    original_maxbytes / 1024);
		else
			tst_resm(TPASS, "%llu kbytes allocated only.",
			    original_maxbytes / 1024);
	}
	free(pid_list);
	tst_exit();
}
Пример #5
0
void fill_webdav_properties_into_resource(struct resource* newres, const ne_prop_result_set *set)
{
    const char *clength, *modtime, *file_id = NULL;
    const char *directDownloadUrl = NULL;
    const char *directDownloadCookies = NULL;
    const char *resourcetype = NULL;
    const char *etag = NULL;
    const char *perm = NULL;

    modtime      = ne_propset_value( set, &ls_props[0] );
    clength      = ne_propset_value( set, &ls_props[1] );
    resourcetype = ne_propset_value( set, &ls_props[2] );
    etag       = ne_propset_value( set, &ls_props[3] );
    file_id      = ne_propset_value( set, &ls_props[4] );
    directDownloadUrl = ne_propset_value( set, &ls_props[5] );
    directDownloadCookies = ne_propset_value( set, &ls_props[6] );
    perm = ne_propset_value( set, &ls_props[7] );

    if( resourcetype && strncmp( resourcetype, "<DAV:collection>", 16 ) == 0) {
        newres->type = resr_collection;
    } else {
        newres->type = resr_normal;
    }

    if (modtime) {
        newres->modtime = oc_httpdate_parse(modtime);
    }

    /* DEBUG_WEBDAV("Parsing Modtime: %s -> %llu", modtime, (unsigned long long) newres->modtime ); */
    newres->size = 0;
    if (clength) {
        newres->size = atoll(clength);
        /* DEBUG_WEBDAV("Parsed File size for %s from %s: %lld", newres->name, clength, (long long)newres->size ); */
    }

    if( etag ) {
        newres->md5 = csync_normalize_etag(etag);
    }

    csync_vio_set_file_id(newres->file_id, file_id);
    /*
    DEBUG_WEBDAV("propfind_results_recursive %s [%s] %s", newres->uri, newres->type == resr_collection ? "collection" : "file", newres->md5);
    */

    if (directDownloadUrl) {
        newres->directDownloadUrl = c_strdup(directDownloadUrl);
    }
    if (directDownloadCookies) {
        newres->directDownloadCookies = c_strdup(directDownloadCookies);
    }
    if (perm && !perm[0]) {
        // special meaning for our code: server returned permissions but are empty
        // meaning only reading is allowed for this resource
        newres->remotePerm[0] = ' ';
        // see _csync_detect_update()
    } else if (perm && strlen(perm) < sizeof(newres->remotePerm)) {
        strncpy(newres->remotePerm, perm, sizeof(newres->remotePerm));
    } else {
        // old server, keep newres->remotePerm empty
    }
}
Пример #6
0
vector<SQUID_Connection> sqstat::GetInfo(Options* pOpts) {
   sqconn con;

   string temp_str;

   active_conn = 0;

   long long esize;
   long etime;

   int n=0, delay_pool;

   vector<SQUID_Connection>::iterator Conn_it; // pointer to current peer
   vector<Uri_Stats>::iterator Stat_it; // pointer to current stat
   Uri_Stats newStats;

   try {
      con.open(pOpts->host, pOpts->port);
   }
   catch(sqconnException &e) {
      std::stringstream error;
      error << e.what() << " while connecting to " << pOpts->host << ":" << pOpts->port;
      throw sqstatException(error.str(), FAILED_TO_CONNECT);
   }

   connections.clear();

   time_t timenow = 0;
   time_t timediff = 0;

   try {
      string request = "GET cache_object://localhost/active_requests HTTP/1.0\n";
      if (!pOpts->pass.empty()) {
         string encoded = Base64::Encode("admin:" + pOpts->pass);
         request += "Authorization: Basic " + encoded + "\n";
      }
      con << request;
      Uri_Stats oldStats;
      while ((con >> temp_str) != 0) {
         if (connections.size()==0) {
            if (n==0) {
               if (temp_str != "HTTP/1.0 200 OK" && temp_str != "HTTP/1.1 200 OK") {
                  std::stringstream error;
                  error << "Access to squid statistic denied: " << temp_str;
                  /*string ip;
                  try {
                     ip = get_ip();
                  }
                  catch (string) {
                     ip = "<your_host_ip>";
                  }*/
                  /*error << "You must enable access to squid statistic in squid.conf by adding strings like:" << endl << endl;
                  error << "\tacl adminhost src <admin_host_ip_here>/255.255.255.255" << endl;
                  error << "\thttp_access allow manager adminhost" << endl;
                  error << "\thttp_access deny manager";*/
                  throw sqstatException(error.str(), ACCESS_DENIED);
               } else {
                  n=1;
                  timenow = time(NULL);
                  timediff = timenow - lastruntime;
                  continue;
               }
            }
         }

         vector<string> result;
         if (temp_str.substr(0,8) == "Server: ") {
            result = Utils::SplitString(temp_str, " ");
            if (result.size() == 2) {
               squid_version = result[1];
            } else { FormatChanged(temp_str); }
         } else if (temp_str.substr(0,12) == "Connection: ") {
            result = Utils::SplitString(temp_str, " ");
            if (result.size() == 2) {
               newStats.id = result[1];
               oldStats = FindUriStatsById(oldConnections, result[1]);
               newStats.uri = "";
               newStats.username = "";
               newStats.size = 0;
               newStats.count = 0;
               newStats.oldsize = 0;
               newStats.etime = 0;
               newStats.delay_pool = -1;
            } else { FormatChanged(temp_str); }
         } else if ((temp_str.substr(0,6) == "peer: ") or (temp_str.substr(0,8) == "remote: ")) {
            result = Utils::SplitString(temp_str, " ");
            if (result.size() == 2) {
               std::pair <string, string> peer = Utils::SplitIPPort(result[1]);
               if (!peer.first.empty()) {
                  Conn_it = std::find_if( connections.begin(), connections.end(), std::bind2nd( std::ptr_fun(ConnByPeer) , peer.first) );
                  // if it is new peer, create new SQUID_Connection
                  if (Conn_it == connections.end()) {
                     SQUID_Connection connection;
                     connection.peer = peer.first;
#ifdef WITH_RESOLVER
                     connection.hostname = DoResolve(pOpts, peer.first);
#endif
                     connections.push_back(connection);
                     Conn_it = connections.end() - 1;
                  }
                  Conn_it->stats.push_back(newStats);
                  Stat_it = Conn_it->stats.end() - 1;
               }
            } else { FormatChanged(temp_str); }
         } else if (temp_str.substr(0,4) == "uri ") {
            result = Utils::SplitString(temp_str, " ");
            if (result.size() == 2) {
               Stat_it->uri = result[1];
               Stat_it->count = 1;
               Stat_it->curr_speed = 0;
               Stat_it->av_speed = 0;
            } else { FormatChanged(temp_str); }
         } else if (temp_str.substr(0,11) == "out.offset ") {
            result = Utils::SplitString(temp_str, " ");
            if (result.size() == 4) {
               esize = atoll(result[3].c_str());
               Stat_it->size = esize;
               Stat_it->oldsize = oldStats.size;
               Conn_it->sum_size += esize;
            } else { FormatChanged(temp_str); }
         } else if (temp_str.substr(0,6) == "start ") {
            result = Utils::SplitString(temp_str, " ");
            if (result.size() == 5) {
               string temp = result[2].erase(0,1);
               etime = atoi(temp.c_str());
               Stat_it->etime = etime;
               if (etime > Conn_it->max_etime)
                  Conn_it->max_etime = etime;
            } else { FormatChanged(temp_str); }
         } else if (temp_str.substr(0,11) == "delay_pool ") {
            result = Utils::SplitString(temp_str, " ");
            if (result.size() == 2) {
               string temp = result[1];
               delay_pool = atoi(temp.c_str());
               Stat_it->delay_pool = delay_pool;
            } else { FormatChanged(temp_str); }
         } else if (temp_str.substr(0,9) == "username ") {
            result = Utils::SplitString(temp_str, " ");
            if (result.size() == 1)
               result.push_back("-");
            if (result.size() == 2) {
               string username = result[1];
               if (!(username == "-")) {
                  Utils::ToLower(username);
                  Stat_it->username = username;
                  if (!Utils::MemberOf(Conn_it->usernames, username))
                     Conn_it->usernames.push_back(username);
               }
            } else { FormatChanged(temp_str); }
         }
      }
   }
   catch(sqconnException &e) {
      std::stringstream error;
      error << e.what();
      throw sqstatException(error.str(), UNKNOWN_ERROR);
   }

   av_speed = 0;
   curr_speed = 0;
   for (vector<SQUID_Connection>::iterator Conn = connections.begin(); Conn != connections.end(); ++Conn) {

      sort(Conn->stats.begin(), Conn->stats.end(), CompareURLs);

      active_conn += Conn->stats.size();

      for (vector<Uri_Stats>::iterator Stats = Conn->stats.begin(); Stats != Conn->stats.end(); ++Stats) {
         long stat_av_speed = 0;
         if ((Stats->size != 0) && (Stats->etime != 0))
            stat_av_speed = Stats->size/Stats->etime;
         Stats->av_speed = stat_av_speed;
         Conn->av_speed += stat_av_speed;
         av_speed += stat_av_speed;
         long stat_curr_speed = 0;
         if ((Stats->size != 0) && (Stats->oldsize != 0) && (lastruntime != 0) && (Stats->size > Stats->oldsize)) {
            if (timediff < 1) timediff = 1;
            stat_curr_speed = (Stats->size - Stats->oldsize) / timediff;
            /*if ((stat_curr_speed > 10000000) || (stat_curr_speed < 0)) {
               cout << Stats->size << " " <<  Stats->oldsize << " " << timenow << " " << lastruntime << endl;
               throw;
            }*/
            Stats->curr_speed = stat_curr_speed;
            Conn->curr_speed += stat_curr_speed;
            curr_speed += stat_curr_speed;
         } /*else {
            Conn->curr_speed += stat_av_speed;
            curr_speed += stat_av_speed;
         }*/
      }
   }

   sort(connections.begin(), connections.end(), sqstat::CompareSIZE);

   oldConnections = connections;
   lastruntime = timenow;

   return connections;
}
Пример #7
0
int main(int argc, char * * argv)
{
	struct s_pwd pwd;
	struct s_pwd pwd2;

	unsigned int max_lvl, max_len;
	unsigned long long start, end;

	max_lvl = 0;
	max_len = 0;
	start = 0;
	end = 0;

	if((argc<3) || (argc>6))
	{
		printf("Usage: %s statfile max_lvl [max_len] [start] [end]\n", argv[0]);
		return -1;
	}
		
	max_lvl = atoi(argv[2]);

	if(argc>3)
		max_len = atoi(argv[3]);
	if(argc>4)
		start = atoll(argv[4]);
	if(argc>5)
		end = atoll(argv[5]);

	init_probatables(argv[1]);

	if(max_len==0)
	{
		for(max_len=6;max_len<20;max_len++)
		{
			nbparts = mem_alloc(256*(max_lvl+1)*sizeof(long long)*max_len);
			printf("len=%u (%lu KB for nbparts) ", max_len, 256UL*(max_lvl+1)*max_len*sizeof(long long)/1024);
			memset(nbparts, 0, 256*(max_lvl+1)*max_len*sizeof(long long));
			nb_parts(0, 0, 0, max_lvl, max_len);
			if(nbparts[0] > 1000000000)
				printf("%lld G possible passwords (%lld)\n", nbparts[0] / 1000000000, nbparts[0]);
			else if(nbparts[0] > 10000000)
				printf("%lld M possible passwords (%lld)\n", nbparts[0] / 1000000, nbparts[0]);
			else if(nbparts[0] > 10000)
				printf("%lld K possible passwords (%lld)\n", nbparts[0] / 1000, nbparts[0]);
			else 
				printf("%lld possible passwords\n", nbparts[0] );
			free(nbparts);
		}
		goto fin;
	}

	if(max_lvl==0)
	{
		for(max_lvl=100;max_lvl<350;max_lvl++)
		{
			nbparts = mem_alloc(256*(max_lvl+1)*sizeof(long long)*max_len);
			printf("lvl=%u (%lu KB for nbparts) ", max_lvl, 256UL*(max_lvl+1)*max_len*sizeof(long long)/1024);
			memset(nbparts, 0, 256*(max_lvl+1)*max_len*sizeof(long long));
			nb_parts(0, 0, 0, max_lvl, max_len);
			if(nbparts[0] > 1000000000)
				printf("%lld G possible passwords (%lld)\n", nbparts[0] / 1000000000, nbparts[0]);
			else if(nbparts[0] > 10000000)
				printf("%lld M possible passwords (%lld)\n", nbparts[0] / 1000000, nbparts[0]);
			else if(nbparts[0] > 10000)
				printf("%lld K possible passwords (%lld)\n", nbparts[0] / 1000, nbparts[0]);
			else 
				printf("%lld possible passwords\n", nbparts[0] );
			free(nbparts);
		}
		goto fin;
	}
		
	nbparts = mem_alloc(256*(max_lvl+1)*sizeof(long long)*max_len);
	fprintf(stderr, "allocated %lu KB for nbparts\n", 256UL*(max_lvl+1)*max_len*sizeof(long long)/1024);
	memset(nbparts, 0, 256*(max_lvl+1)*max_len*sizeof(long long));

	nb_parts(0, 0, 0, max_lvl, max_len);
	if(nbparts[0] > 1000000000)
		fprintf(stderr, "%lld G possible passwords (%lld)\n", nbparts[0] / 1000000000, nbparts[0]);
	else if(nbparts[0] > 10000000)
		fprintf(stderr, "%lld M possible passwords (%lld)\n", nbparts[0] / 1000000, nbparts[0]);
	else if(nbparts[0] > 10000)
		fprintf(stderr, "%lld K possible passwords (%lld)\n", nbparts[0] / 1000, nbparts[0]);
	else 
		fprintf(stderr, "%lld possible passwords\n", nbparts[0] );

	if(end==0)
		end = nbparts[0];

	pwd.level = 0;
	pwd.len = 0;
	pwd.index = 0;
	memset(pwd.password, 0, max_len+1);

	print_pwd(start, &pwd, max_lvl, max_len);
	print_pwd(start, &pwd2, max_lvl, max_len);

	fprintf(stderr, "starting with %s (%lld to %lld, %f%% of the scope)\n", pwd.password, start, end, 100*((float) end-start)/((float) nbparts[0]) );

	show_pwd(start, end, max_lvl, max_len);

	free(nbparts);
fin:
	free(proba1);
	free(proba2);
	free(first);
	return 0;
}
Пример #8
0
s32_t sbp_opendir(char* filename) {
	s32_t fd = get_slot();
	SBP_PREPARE_REQUEST

	mkent(head,METHOD,"OPENDIR");
	mkent(head,ARGC,"1");
	mkent(head,"Arg0",filename);
	mkent(head,CONTENT_LEN,"0");

	SBP_SEND_AND_PROCESS_REPLY

	if (strncmp(head.title, REQUEST_OK, strlen(REQUEST_OK)) == 0) {
		if ((fds[fd] = (struct sbp_filedesc *) malloc(
				sizeof(struct sbp_filedesc))) == NULL) {
			seterr(MEM_ERR,MALLOC);
			goto err_exit3;
		}// need sbp_close(fd)
		bzero(fds[fd], sizeof(struct sbp_filedesc));
		if ((fds[fd]->filename = (char *) malloc(sizeof(strlen(filename) + 1)))
				== NULL) {
			seterr(MEM_ERR,MALLOC);
			goto err_exit4;
		}
		memcpy(fds[fd]->filename, filename, strlen(filename));
		fds[fd]->filename[strlen(filename)] = 0;
		fds[fd]->offset = 0;
		fds[fd]->type = T_DIR;
		char* fd_s = get_head_entry_value(&head, DIR_FD);
		if(fd_s == NULL)
		{
			seterr(DATA_ERR,"Could not get FD");
			goto err_exit4;
		}
		fds[fd]->server_fd = atoll(fd_s);
//		char* auth_code = get_head_entry_value(&head, AUTH_CODE);
//		if (auth_code == NULL) {
//			seterr(DATA_ERR,AUTH_LEN);
//			goto err_exit4;
//		}
		char* dir_ent_num = get_head_entry_value(&head, DIR_ENT_NUM);
		if (dir_ent_num == NULL)
		{
			seterr(DATA_ERR,DIR_ENT_LEN);
			goto err_exit4;
		}
		fds[fd]->length = atoll(dir_ent_num);
//		int auth_len = strlen(auth_code);
//		if (auth_len > AUTH_CODE_LEN) {
//			seterr(DATA_ERR,AUTH_LEN);
//			goto err_exit4;
//		}
//		memcpy(fds[fd]->auth_code, auth_code, auth_len);

		goto ok_exit;
	} else if (strncmp(head.title, REQUEST_ERR, strlen(REQUEST_ERR)) == 0) {
		sbp_update_err(&head);
		goto err_exit3;
	} else {
		seterr(HEAD_ERR, UNKNOWN_HEAD);
	}

	err_exit4: sbp_closedir(fd);
	err_exit3: free_head(&head);
	err_exit2: free(rec_data);
	err_exit1: free(data);
	err_exit: free(usr);
	free(pass);
	return -1;
	ok_exit: free(data);
	free(rec_data);
	free_head(&head);
	free(usr);
	free(pass);
	return fd;

}
Пример #9
0
static int memcached_read (void) /* {{{ */
{
	char buf[1024];
	char *fields[3];
	char *ptr;
	char *line;
	char *saveptr;
	int fields_num;

	gauge_t bytes_used = NAN;
	gauge_t bytes_total = NAN;
	gauge_t hits = NAN;
	gauge_t gets = NAN;
	counter_t rusage_user = 0;
	counter_t rusage_syst = 0;
	counter_t octets_rx = 0;
	counter_t octets_tx = 0;

	/* get data from daemon */
	if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
		return -1;
	}

#define FIELD_IS(cnst) \
	(((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))

	ptr = buf;
	saveptr = NULL;
	while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
	{
		int name_len;

		ptr = NULL;

		fields_num = strsplit(line, fields, 3);
		if (fields_num != 3)
			continue;

		name_len = strlen(fields[1]);
		if (name_len == 0)
			continue;

		/*
		 * For an explanation on these fields please refer to
		 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
		 */

		/*
		 * CPU time consumed by the memcached process
		 */
		if (FIELD_IS ("rusage_user"))
		{
			rusage_user = atoll (fields[2]);
		}
		else if (FIELD_IS ("rusage_system"))
		{
			rusage_syst = atoll(fields[2]);
		}

		/*
		 * Number of threads of this instance
		 */
		else if (FIELD_IS ("threads"))
		{
			submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
		}

		/*
		 * Number of items stored
		 */
		else if (FIELD_IS ("curr_items"))
		{
			submit_gauge ("memcached_items", "current", atof (fields[2]));
		}

		/*
		 * Number of bytes used and available (total - used)
		 */
		else if (FIELD_IS ("bytes"))
		{
			bytes_used = atof (fields[2]);
		}
		else if (FIELD_IS ("limit_maxbytes"))
		{
			bytes_total = atof(fields[2]);
		}

		/*
		 * Connections
		 */
		else if (FIELD_IS ("curr_connections"))
		{
			submit_gauge ("memcached_connections", "current", atof (fields[2]));
		}

		/*
		 * Commands
		 */
		else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
		{
			const char *name = fields[1] + 4;
			submit_counter ("memcached_command", name, atoll (fields[2]));
			if (strcmp (name, "get") == 0)
				gets = atof (fields[2]);
		}

		/*
		 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
		 */
		else if (FIELD_IS ("get_hits"))
		{
			submit_counter ("memcached_ops", "hits", atoll (fields[2]));
			hits = atof (fields[2]);
		}
		else if (FIELD_IS ("get_misses"))
		{
			submit_counter ("memcached_ops", "misses", atoll (fields[2]));
		}
		else if (FIELD_IS ("evictions"))
		{
			submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
		}

		/*
		 * Network traffic
		 */
		else if (FIELD_IS ("bytes_read"))
		{
			octets_rx = atoll (fields[2]);
		}
		else if (FIELD_IS ("bytes_written"))
		{
			octets_tx = atoll (fields[2]);
		}
	} /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */

	if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
		submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);

	if ((rusage_user != 0) || (rusage_syst != 0))
		submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);

	if ((octets_rx != 0) || (octets_tx != 0))
		submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);

	if (!isnan (gets) && !isnan (hits))
	{
		gauge_t rate = NAN;

		if (gets != 0.0)
			rate = 100.0 * hits / gets;

		submit_gauge ("percent", "hitratio", rate);
	}

	return 0;
}
Пример #10
0
void evg_faults_init(void)
{
	FILE *f;
	char line[MAX_STRING_SIZE];
	char *line_ptr;
	struct evg_fault_t *fault;
	int line_num;
	long long last_cycle;

	evg_fault_list = linked_list_create();
	if (!*evg_faults_file_name)
		return;

	f = fopen(evg_faults_file_name, "rt");
	if (!f)
		fatal("%s: cannot open file", evg_faults_file_name);
	
	line_num = 0;
	last_cycle = 0;
	while (!feof(f))
	{
		const char *delim = " ";
	
		/* Read a line */
		line_num++;
		line_ptr = fgets(line, MAX_STRING_SIZE, f);
		if (!line_ptr)
			break;

		/* Allocate new fault */
		fault = calloc(1, sizeof(struct evg_fault_t));
		if (!fault)
			fatal("%s: out of memory", __FUNCTION__);

		/* Read <cycle> field */
		line_ptr = strtok(line_ptr, delim);
		if (!line_ptr)
			goto wrong_format;
		fault->cycle = atoll(line_ptr);
		if (fault->cycle < 1)
			fatal("%s: line %d: lowest possible cycle is 1",
				evg_faults_file_name, line_num);
		if (fault->cycle < last_cycle)
			fatal("%s: line %d: cycles must be ordered",
				evg_faults_file_name, line_num);

		/* <fault> - Type of fault */
		line_ptr = strtok(NULL, delim);
		if (!line_ptr)
			goto wrong_format;
		if (!strcmp(line_ptr, "ams"))
			fault->type = evg_fault_ams;
		else if (!strcmp(line_ptr, "reg"))
			fault->type = evg_fault_reg;
		else if (!strcmp(line_ptr, "mem"))
			fault->type = evg_fault_mem;
		else
			fatal("%s: line %d: invalid value for <fault> ('%s')",
				evg_faults_file_name, line_num, line_ptr);

		/* <cu_id> - Compute unit */
		line_ptr = strtok(NULL, delim);
		if (!line_ptr)
			goto wrong_format;
		fault->compute_unit_id = atoi(line_ptr);
		if (fault->compute_unit_id >= evg_gpu_num_compute_units || fault->compute_unit_id < 0)
			fatal("%s: line %d: invalid compute unit ID",
				evg_faults_file_name, line_num);

		/* Analyze rest of the line depending on fault type */
		switch (fault->type)
		{

		case evg_fault_ams:

			/* <stack_id> - Stack ID */
			line_ptr = strtok(NULL, delim);
			if (!line_ptr)
				goto wrong_format;
			fault->stack_id = atoi(line_ptr);
			if (fault->stack_id >= evg_gpu_max_wavefronts_per_compute_unit)
				fatal("%s: line %d: invalid stack ID",
					evg_faults_file_name, line_num);

			/* <am_id> - Active mask ID */
			line_ptr = strtok(NULL, delim);
			if (!line_ptr)
				goto wrong_format;
			fault->active_mask_id = atoi(line_ptr);
			if (fault->active_mask_id >= EVG_MAX_STACK_SIZE)
				fatal("%s: line %d: invalid active mask ID",
					evg_faults_file_name, line_num);

			/* <bit> */
			line_ptr = strtok(NULL, delim);
			if (!line_ptr)
				goto wrong_format;
			fault->bit = atoi(line_ptr);
			if (fault->bit >= evg_emu_wavefront_size)
				fatal("%s: line %d: invalid bit index",
					evg_faults_file_name, line_num);

			/* No more tokens */
			if (strtok(NULL, delim))
				fatal("%s: line %d: too many arguments",
					evg_faults_file_name, line_num);

			break;

		case evg_fault_reg:

			/* <reg_id> - Register ID */
			line_ptr = strtok(NULL, delim);
			if (!line_ptr)
				goto wrong_format;
			fault->reg_id = atoi(line_ptr);
			if (fault->reg_id >= evg_gpu_num_registers || fault->reg_id < 0)
				fatal("%s: line %d: invalid compute unit ID",
					evg_faults_file_name, line_num);

			/* <bit> */
			line_ptr = strtok(NULL, delim);
			if (!line_ptr)
				goto wrong_format;
			fault->bit = atoi(line_ptr);
			if (fault->bit < 0 || fault->bit >= 128)
				fatal("%s: line %d: invalid bit index",
					evg_faults_file_name, line_num);

			break;

		case evg_fault_mem:

			/* <byte> - Byte position in local memory */
			line_ptr = strtok(NULL, delim);
			if (!line_ptr)
				goto wrong_format;
			fault->byte = atoi(line_ptr);
			if (fault->byte >= evg_gpu_local_mem_size || fault->byte < 0)
				fatal("%s: line %d: invalid byte position",
					evg_faults_file_name, line_num);

			/* <bit> - Bit position */
			line_ptr = strtok(NULL, delim);
			if (!line_ptr)
				goto wrong_format;
			fault->bit = atoi(line_ptr);
			if (fault->bit > 7 || fault->bit < 0)
				fatal("%s: line %d: invalid bit position",
					evg_faults_file_name, line_num);

			break;
		}

		/* Insert fault in fault list */
		linked_list_out(evg_fault_list);
		linked_list_insert(evg_fault_list, fault);
		last_cycle = fault->cycle;
		continue;

wrong_format:
		fatal("%s: line %d: not enough arguments",
			evg_faults_file_name, line_num);
	}
	linked_list_head(evg_fault_list);
}
Пример #11
0
struct sbp_dirent* sbp_readdir(s32_t dirfd) {

	if(dirfd<0)return NULL;
	if (fds[dirfd] == NULL) {
		return NULL;
	}
	struct sbpfs_head head;
	char* usr;
	char* pass;
	char* data;
	char* rec_data;
	char tran_usr[16 + 7];
	u64_t rec_len = 0;
	int data_len = 0;
	head.data = ((void *)0);
	head.title = "SBPFS/1.0";
	head.entry_num = 0;
	if (sbp_getUandP(&usr, &pass) == -1) {
		printf("Can not get username and password\n");
		return NULL;
	}
	sprintf(tran_usr, "Client_%s", usr);
	head.entrys[head.entry_num].name = "User";
	head.entrys[head.entry_num++].value = tran_usr;;
	head.entrys[head.entry_num].name = "Password";
	head.entrys[head.entry_num++].value = pass;;
	char tran_fd[32];
	char tran_offset[32];
	sprintf(tran_fd,"%lld",fds[dirfd]->server_fd);
	sprintf(tran_offset,"%lld",fds[dirfd]->offset);
	mkent(head,METHOD,"READDIR");
	mkent(head,ARGC,"2");
	mkent(head,"Arg0",tran_fd);
	mkent(head,"Arg1",tran_offset);
	mkent(head,CONTENT_LEN,"0");

	SBP_SEND_AND_PROCESS_REPLY
	if (strncmp(head.title, REQUEST_OK, strlen(REQUEST_OK)) == 0) {
		u64_t content_l = -1;
		char* value = get_head_entry_value(&head,CONTENT_LEN);
		if(value == NULL)
		{
			seterr(HEAD_ERR,NO_CONTENT_LEN);
			goto err_exit3;
		}
		content_l = atoll(value);
		if(content_l != sizeof(struct sbp_dirent_for_trans))
		{
			goto err_exit3;
		}

		memcpy(&dir_ent,rec_data + head.head_len,content_l);
		dir_ent.offset = fds[dirfd]->offset++;
		goto ok_exit;
	} else if (strncmp(head.title, REQUEST_ERR, strlen(REQUEST_ERR)) == 0) {
		sbp_update_err(&head);
		goto err_exit3;
	} else {
		seterr(HEAD_ERR,UNKNOWN_HEAD);
	}
	err_exit3: free_head(&head);
	err_exit2: free(rec_data);
	err_exit1: free(data);
	err_exit: free(usr);
	free(pass);
	return NULL;
	ok_exit: free(data);
	free(rec_data);
	free_head(&head);
	free(usr);
	free(pass);
	return &dir_ent;
}
Пример #12
0
cmddatas_t *
read_cmdfile(
    char *filename)
{
    cmddatas_t *cmddatas = g_new0(cmddatas_t, 1);
    cmddata_t  *cmddata;
    char  *s, *fp, *operation;
    char   ch;
    char **xlines;
    int    i;
    int    pid;
    pid_t  pids[NB_PIDS];
    pid_t  new_pids[NB_PIDS];
    int    nb_pids = 0;
    int    result;

    cmddatas->lock = file_lock_new(filename);
    cmddatas->cmdfile = g_hash_table_new_full(g_direct_hash, g_direct_equal,
					      NULL, &free_cmddata);
    // open
    while ((result = file_lock_lock(cmddatas->lock)) == 1) {
        sleep(1);
    }
    if (result != 0) {
        g_debug("read_cmdfile open failed: %s", strerror(errno));
    }

    if (!cmddatas->lock->data) {
	cmddatas->version = 1;
	cmddatas->max_id = 0;
	return cmddatas;
    }
    xlines = g_strsplit(cmddatas->lock->data, "\n", 0);

    // read
    if (sscanf(xlines[0], "VERSION %d", &cmddatas->version) != 1) {};
    if (sscanf(xlines[1], "ID %d", &cmddatas->max_id) != 1) {};

    // read cmd
    for (i=2; xlines[i] != NULL; i++) {
	int id;
	s = xlines[i];
	if (*s == '\0') continue;
	ch = *s++;
	skip_whitespace(s, ch);
	if (ch == '\0' || sscanf((s - 1), "%d", &id) != 1) {
	    continue;
	}
	skip_integer(s, ch);
	skip_whitespace(s, ch);
	operation = s - 1;
        skip_non_whitespace(s, ch);
        s[-1] = '\0';
	if (!g_str_equal(operation, "FLUSH") &&
	    !g_str_equal(operation, "COPY")) {
	    g_debug("BAD operation %s: %s", operation, s);
	    continue;
	}
	cmddata = g_new0(cmddata_t, 1);
	cmddata->id = id;
	skip_whitespace(s, ch);
	fp = s - 1;
        skip_non_whitespace(s, ch);
        s[-1] = '\0';
        cmddata->config = unquote_string(fp);
	if (g_str_equal(operation, "FLUSH")) {
	    cmddata->operation = CMD_FLUSH;
	    skip_whitespace(s,ch);
	    fp = s - 1;
	    skip_quoted_string(s, ch);
	    s[-1] = '\0';
	    cmddata->holding_file = unquote_string(fp);
	} else if (g_str_equal(operation, "COPY")) {
	    char *src_labels;
	    char *slabels;
	    char *a;

	    cmddata->operation = CMD_COPY;
	    skip_whitespace(s,ch);
	    fp = s - 1;
	    skip_quoted_string(s, ch);
	    s[-1] = '\0';
	    cmddata->src_storage = unquote_string(fp);
	    skip_whitespace(s,ch);
	    fp = s - 1;
	    skip_quoted_string(s, ch);
	    s[-1] = '\0';
	    cmddata->src_pool = unquote_string(fp);
	    skip_whitespace(s,ch);
	    fp = s - 1;
	    skip_quoted_string(s, ch);
	    s[-1] = '\0';
	    cmddata->src_label = unquote_string(fp);
	    skip_whitespace(s,ch);
	    fp = s - 1;
	    skip_integer(s, ch);
	    s[-1] = '\0';
	    cmddata->src_fileno = atoi(fp);

	    skip_whitespace(s,ch);
	    fp = s - 1;
	    skip_quoted_string(s, ch);
	    s[-1] = '\0';
	    slabels = src_labels = unquote_string(fp);
	    cmddata->src_labels_str = g_strdup(src_labels);
	    a = strstr(slabels, " ;");
	    if (a) {
		slabels = a+2;
		while ((a = strstr(slabels, " ;"))) {
		    *a = '\0';
		    cmddata->src_labels = g_slist_append(cmddata->src_labels, g_strdup(slabels));
		    slabels = a+2;
		}
	    }
	    g_free(src_labels);

	    skip_whitespace(s,ch);
	    fp = s - 1;
	    skip_integer(s, ch);
	    s[-1] = '\0';
	    cmddata->start_time = atoll(fp);
	} else {
	}
	skip_whitespace(s, ch);
	fp = s - 1;
	skip_quoted_string(s, ch);
	s[-1] = '\0';
	cmddata->hostname = unquote_string(fp);
	skip_whitespace(s, ch);
	fp = s - 1;
	skip_quoted_string(s, ch);
	s[-1] = '\0';
	cmddata->diskname = unquote_string(fp);
	skip_whitespace(s, ch);
	fp = s - 1;
	skip_quoted_string(s, ch);
	s[-1] = '\0';
	cmddata->dump_timestamp = unquote_string(fp);
	skip_whitespace(s,ch);
	fp = s - 1;
	skip_quoted_string(s, ch);
	s[-1] = '\0';
	cmddata->level = atoi(fp);
	skip_whitespace(s, ch);
	fp = s - 1;
	skip_quoted_string(s, ch);
	s[-1] = '\0';
	cmddata->dst_storage = unquote_string(fp);
	skip_whitespace(s, ch);
	fp = s - 1;
	skip_non_whitespace(s, ch);
	s[-1] = '\0';
	if (sscanf(fp, "WORKING:%d", &pid) != 1) {
	}
	cmddata->working_pid = pid;
	skip_whitespace(s, ch);
	fp = s - 1;
	skip_non_whitespace(s, ch);
	s[-1] = '\0';
	if (g_str_equal(fp, "DONE")) {
	    cmddata->status = CMD_DONE;
	} else if (g_str_equal(fp, "TODO")) {
	    cmddata->status = CMD_TODO;
	} else if (strncmp(fp, "PARTIAL", 7) == 0) {
	    long long lsize;
	    cmddata->status = CMD_PARTIAL;
	    if (sscanf(fp, "PARTIAL:%lld", &lsize) != 1) {
	    } else {
		cmddata->size = lsize;
	    }
	} else {
	}

	/* validate working_pid */
	if (!checked_working_pid && cmddata->working_pid != 0) {
	    int   i;

	    for (i = 0; i < nb_pids; i++) {
		if (pids[i] == cmddata->working_pid) {
		    cmddata->working_pid = new_pids[i];
		    i += 100;
		    continue;
		}
	    }
	    if (nb_pids < NB_PIDS) {
		pids[nb_pids] = cmddata->working_pid;
		if (kill(cmddata->working_pid, 0) != 0)
		    cmddata->working_pid =0;
		new_pids[nb_pids] = cmddata->working_pid;
		nb_pids++;
	    }
	}

	g_hash_table_insert(cmddatas->cmdfile, GINT_TO_POINTER(cmddata->id), cmddata);
    }

    g_strfreev(xlines);
    checked_working_pid = 1;

    return cmddatas;
}
void Blaster::runBlaster(char *file1,char *file2,
			 int32_t maxNumThreads, int32_t wait, bool isLogFile,
			 bool verbose,bool justDisplay,
			 bool useProxy ,
			 bool injectUrlWithLinks ,
			 bool injectUrl ) {
	if (!init())
		return;
	m_blasterDiff=true;
	if (!file2)
		m_blasterDiff=false;
	
	// set File class
	File f1;
	f1.set ( file1 );

	// open files
	if ( ! f1.open ( O_RDONLY ) ) {
		log("blaster:open: %s %s",file1,mstrerror(g_errno)); 
		return; 
	}

	// get file size
	int32_t fileSize1 = f1.getFileSize() ;
	// store a \0 at the end
	int32_t m_bufSize1 = fileSize1 + 1;

	m_doInjectionWithLinks = injectUrlWithLinks;
	m_doInjection = injectUrl;

	// make buffers to hold all
	m_buf1 = (char *) mmalloc ( m_bufSize1 , "blaster1" );
	if ( ! m_buf1) {
		log("blaster:mmalloc: %s",mstrerror(errno));
		return;
	}

	//char *bufEnd = buf + bufSize;

	// set m_p1
	m_p1    = m_buf1;
	m_p1end = m_buf1 + m_bufSize1 - 1;

	// read em all in
	if ( ! f1.read ( m_buf1 , fileSize1 , 0 ) ) {
		log("blaster:read: %s %s",file1,mstrerror(g_errno));
		return;
	}

	// change \n to \0
	//char *p = buf;
	int32_t  n = 0;
	for ( int32_t i = 0 ; i < m_bufSize1 ; i++ ) {
		if ( m_buf1[i] != '\n' ) continue;
		m_buf1[i] = '\0';
		n++;
	}


	if (m_blasterDiff){
		File f2;
		f2.set ( file2 );
		if ( ! f2.open ( O_RDONLY ) ) {
			log("blaster:open: %s %s",file2,mstrerror(g_errno)); 
			return; 
		}
		int32_t fileSize2 = f2.getFileSize() ;
		int32_t m_bufSize2 = fileSize2 + 1;
		m_buf2 = (char *) mmalloc ( m_bufSize2 , "blaster2" );
		if ( ! m_buf2) {
			log("blaster:mmalloc: %s",mstrerror(errno));
			return;
		}
		// set m_p2
		m_p2    = m_buf2;
		m_p2end = m_buf2 + m_bufSize2 - 1;
		if ( ! f2.read ( m_buf2 , fileSize2 , 0 ) ) {
			log("blaster:read: %s %s",file2,mstrerror(g_errno));
			return;
		}
		int32_t m=0;
		for ( int32_t i = 0 ; i < m_bufSize2 ; i++ ) {
			if ( m_buf2[i] != '\n' ) continue;
			m_buf2[i] = '\0';
			m++;
		}
		// Working on only the least number of urls from both files, 
		//because we need to work in pairs
		if (m<n) n=m;
		else m=n;
		m_totalUrls=n;

		// should we print out all the logs?
		m_verbose=verbose;
		// Should we use the proxy for getting the first Doc
		m_useProxy=useProxy;
		// Should we just display the not present links and not fetch
		// the page to see if they are actually present ?
		m_justDisplay=justDisplay;
	}
	else{
		m_isLogFile=isLogFile;
		
		/*if reading a gigablast log file, find the lines that have 
		  GET and POST commands for search, and register a sleep
		  callback for those lines with sleepWrapperLog*/
		if(!isLogFile)
			m_totalUrls=n;
		else {
			m_totalUrls=0;
			char *p=m_buf1;
			char *pend=p+m_bufSize1;
			
			// start is the time in milliseconds of the first log 
			// message
			int64_t start=atoll(m_buf1);
			while(p<pend) {
				char *lineStart=p;
				char *urlStart=strstr(p," GET /search");
				if (!urlStart)
					urlStart=strstr(p," POST /search");
				if(!urlStart){
					p+=gbstrlen(p)+1; //goto next line
					continue;
				}
				urlStart++;
				m_wait=atoll(lineStart)-start;
				// register it here
				g_loop.registerSleepCallback(m_wait , 
							     urlStart, 
							     sleepWrapperLog);
				m_totalUrls++;
				p+=gbstrlen(p)+1;
			}
		}
	}
	log(LOG_INIT,"blaster: read %"INT32" urls into memory", 
	    m_totalUrls );

	if(!isLogFile){
		// get min time bewteen each spider in milliseconds
		m_wait = wait;
			
		// # of threads
		m_maxNumThreads = maxNumThreads;
		
		m_launched=0;
		
		m_portSwitch = 0;
		//if ( argc == 4 ) m_portSwitch = 1;
		//else             m_portSwitch = 0;
			
		// start our spider loop
		//startSpidering( );
		
		// wakeup wrapper every X ms
		g_loop.registerSleepCallback ( m_wait , NULL , 
					       sleepWrapper );
	}
	// this print to print how many docs have been processed
	m_print=false;
	m_startTime=gettimeofdayInMilliseconds();
	m_totalDone=0;
	// . now start g_loops main interrupt handling loop
	// . it should block forever
	// . when it gets a signal it dispatches to a server or db to handle it
	if ( ! g_loop.runLoop()    ) {
		log("blaster::runLoop failed" ); return; }
	// dummy return (0-->normal exit status for the shell)
	return;
}
Пример #14
0
int64 auth_verify_from_mydb_by_client(const char* guest, char* accesstoken, int accesslen)
{
    int ret, row, col;
    sqlite3 *db = NULL;
    char *errmsg = NULL, **result, sql[1024];
    uint64 time;
    int64 uid;

    // open database
    ret = sqlite3_open("./auth.db", &db);
    if (ret != SQLITE_OK) {
        LOGGER_ERROR("auth_verify_from_mydb_by_client open error: %s", sqlite3_errmsg(db));
        goto QUIT;
    }

    // check table exist
    snprintf(sql, sizeof(sql), "select count(*) from sqlite_master where type='table' and name='guest'");
    ret = sqlite3_get_table(db, sql, &result, &row, &col, &errmsg);
    if (ret != SQLITE_OK) {
        LOGGER_ERROR("auth_verify_from_mydb_by_client check table exist error: %d, %s", sqlite3_errcode(db), sqlite3_errmsg(db));
        goto QUIT;
    }

    // if not exist then create
    if (atoi(result[1]) <= 0) {
        snprintf(sql, sizeof(sql), "create table guest(uid integer primary key autoincrement, name varchar(128), token varchar(128), time datatime)");
        ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        if (ret != SQLITE_OK) {
            LOGGER_ERROR("auth_verify_from_mydb_by_client create table error: %d, %s", sqlite3_errcode(db), sqlite3_errmsg(db));
            goto QUIT;
        }
    }

    // check guest if exist
    snprintf(sql, sizeof(sql), "select * from guest where name='%s'", guest);
    ret = sqlite3_get_table(db, sql, &result, &row, &col, &errmsg);
    if (ret != SQLITE_OK) {
        LOGGER_ERROR("auth_verify_from_mydb_by_client get table error: %d, %s", sqlite3_errcode(db), sqlite3_errmsg(db));
        goto QUIT;
    }

    time = GetTimeSec();
    snprintf(accesstoken, accesslen, "%lld", time);

    // if not exist then insert
    if (row <= 0) {
        snprintf(sql, sizeof(sql), "insert into guest (name, token, time) values('%s', '%s', %lld)", guest, accesstoken, time);
        ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        if (ret != SQLITE_OK) {
            LOGGER_ERROR("auth_verify_from_mydb_by_client insert guest error: %s, %s", guest, sqlite3_errmsg(db));
            goto QUIT;
        }

        snprintf(sql, sizeof(sql), "select * from guest where name='%s'", guest);
        ret = sqlite3_get_table(db, sql, &result, &row, &col, &errmsg);
        if (ret != SQLITE_OK) {
            LOGGER_ERROR("auth_verify_from_mydb_by_client get table error: %d, %s", sqlite3_errcode(db), sqlite3_errmsg(db));
            goto QUIT;
        }
    } else {
        snprintf(sql, sizeof(sql), "update guest set token='%s', time=%lld where name='%s'", accesstoken, time, guest);
        ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        if (ret != SQLITE_OK) {
            LOGGER_ERROR("auth_verify_from_mydb_by_client update guest error: %s, %s", guest, sqlite3_errmsg(db));
            goto QUIT;
        }
    }

    uid = atoll(result[col]);

QUIT:
    if (db) sqlite3_close(db);
    if (errmsg) sqlite3_free(errmsg);

    return uid;
}
Пример #15
0
/*
 * result parsing list.
 * This function is called to parse the result of the propfind request
 * to list directories on the WebDAV server. I takes a single resource
 * and fills a resource struct and stores it to the result list which
 * is stored in the listdir_context.
 */
static void results(void *userdata,
                    const ne_uri *uri,
                    const ne_prop_result_set *set)
{
    struct listdir_context *fetchCtx = userdata;
    struct resource *newres = 0;
    const char *clength, *modtime = NULL;
    const char *resourcetype = NULL;
    const char *md5sum = NULL;
    const char *file_id = NULL;
    const ne_status *status = NULL;
    char *path = ne_path_unescape( uri->path );

    (void) status;
    if( ! fetchCtx ) {
        DEBUG_WEBDAV("No valid fetchContext");
        return;
    }

    if( ! fetchCtx->target ) {
        DEBUG_WEBDAV("error: target must not be zero!" );
        return;
    }

    /* Fill the resource structure with the data about the file */
    newres = c_malloc(sizeof(struct resource));
    ZERO_STRUCTP(newres);
    newres->uri =  path; /* no need to strdup because ne_path_unescape already allocates */
    newres->name = c_basename( path );

    modtime      = ne_propset_value( set, &ls_props[0] );
    clength      = ne_propset_value( set, &ls_props[1] );
    resourcetype = ne_propset_value( set, &ls_props[2] );
    md5sum       = ne_propset_value( set, &ls_props[3] );
    file_id      = ne_propset_value( set, &ls_props[4] );

    newres->type = resr_normal;
    if( clength == NULL && resourcetype && strncmp( resourcetype, "<DAV:collection>", 16 ) == 0) {
        newres->type = resr_collection;
    }

    if (modtime) {
        newres->modtime = oc_httpdate_parse(modtime);
    }

    /* DEBUG_WEBDAV("Parsing Modtime: %s -> %llu", modtime, (unsigned long long) newres->modtime ); */
    newres->size = 0;
    if (clength) {
        newres->size = atoll(clength);
        /* DEBUG_WEBDAV("Parsed File size for %s from %s: %lld", newres->name, clength, (long long)newres->size ); */
    }

    if( md5sum ) {
        newres->md5 = csync_normalize_etag(md5sum);
    }

    csync_vio_set_file_id(newres->file_id, file_id);

    /* prepend the new resource to the result list */
    newres->next   = fetchCtx->list;
    fetchCtx->list = newres;
    fetchCtx->result_count = fetchCtx->result_count + 1;
    /* DEBUG_WEBDAV( "results for URI %s: %d %d", newres->name, (int)newres->size, (int)newres->type ); */
}
Пример #16
0
static int disk_read (void)
{
#if HAVE_IOKIT_IOKITLIB_H
	io_registry_entry_t	disk;
	io_registry_entry_t	disk_child;
	io_iterator_t		disk_list;
	CFDictionaryRef		props_dict;
	CFDictionaryRef		stats_dict;
	CFDictionaryRef		child_dict;
	kern_return_t           status;

	signed long long read_ops;
	signed long long read_byt;
	signed long long read_tme;
	signed long long write_ops;
	signed long long write_byt;
	signed long long write_tme;

	int  disk_major;
	int  disk_minor;
	char disk_name[64];

	/* Get the list of all disk objects. */
	if (IOServiceGetMatchingServices (io_master_port,
				IOServiceMatching (kIOBlockStorageDriverClass),
				&disk_list) != kIOReturnSuccess)
	{
		ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
		return (-1);
	}

	while ((disk = IOIteratorNext (disk_list)) != 0)
	{
		props_dict = NULL;
		stats_dict = NULL;
		child_dict = NULL;

		/* `disk_child' must be released */
		if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
			       	!= kIOReturnSuccess)
		{
			/* This fails for example for DVD/CD drives.. */
			DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
			IOObjectRelease (disk);
			continue;
		}

		/* We create `props_dict' => we need to release it later */
		if (IORegistryEntryCreateCFProperties (disk,
					(CFMutableDictionaryRef *) &props_dict,
					kCFAllocatorDefault,
					kNilOptions)
				!= kIOReturnSuccess)
		{
			ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
			IOObjectRelease (disk_child);
			IOObjectRelease (disk);
			continue;
		}

		if (props_dict == NULL)
		{
			DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
			IOObjectRelease (disk_child);
			IOObjectRelease (disk);
			continue;
		}

		stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
				CFSTR (kIOBlockStorageDriverStatisticsKey));

		if (stats_dict == NULL)
		{
			DEBUG ("CFDictionaryGetValue (%s) failed.",
				       	kIOBlockStorageDriverStatisticsKey);
			CFRelease (props_dict);
			IOObjectRelease (disk_child);
			IOObjectRelease (disk);
			continue;
		}

		if (IORegistryEntryCreateCFProperties (disk_child,
					(CFMutableDictionaryRef *) &child_dict,
					kCFAllocatorDefault,
					kNilOptions)
				!= kIOReturnSuccess)
		{
			DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
			IOObjectRelease (disk_child);
			CFRelease (props_dict);
			IOObjectRelease (disk);
			continue;
		}

		/* kIOBSDNameKey */
		disk_major = (int) dict_get_value (child_dict,
			       	kIOBSDMajorKey);
		disk_minor = (int) dict_get_value (child_dict,
			       	kIOBSDMinorKey);
		read_ops  = dict_get_value (stats_dict,
				kIOBlockStorageDriverStatisticsReadsKey);
		read_byt  = dict_get_value (stats_dict,
				kIOBlockStorageDriverStatisticsBytesReadKey);
		read_tme  = dict_get_value (stats_dict,
				kIOBlockStorageDriverStatisticsTotalReadTimeKey);
		write_ops = dict_get_value (stats_dict,
				kIOBlockStorageDriverStatisticsWritesKey);
		write_byt = dict_get_value (stats_dict,
				kIOBlockStorageDriverStatisticsBytesWrittenKey);
		/* This property describes the number of nanoseconds spent
		 * performing writes since the block storage driver was
		 * instantiated. It is one of the statistic entries listed
		 * under the top-level kIOBlockStorageDriverStatisticsKey
		 * property table. It has an OSNumber value. */
		write_tme = dict_get_value (stats_dict,
				kIOBlockStorageDriverStatisticsTotalWriteTimeKey);

		if (ssnprintf (disk_name, sizeof (disk_name),
				"%i-%i", disk_major, disk_minor) >= sizeof (disk_name))
		{
			DEBUG ("snprintf (major, minor) failed.");
			CFRelease (child_dict);
			IOObjectRelease (disk_child);
			CFRelease (props_dict);
			IOObjectRelease (disk);
			continue;
		}
		DEBUG ("disk_name = %s", disk_name);

		if ((read_byt != -1LL) || (write_byt != -1LL))
			disk_submit (disk_name, "disk_octets", read_byt, write_byt);
		if ((read_ops != -1LL) || (write_ops != -1LL))
			disk_submit (disk_name, "disk_ops", read_ops, write_ops);
		if ((read_tme != -1LL) || (write_tme != -1LL))
			disk_submit (disk_name, "disk_time",
					read_tme / 1000,
					write_tme / 1000);

		CFRelease (child_dict);
		IOObjectRelease (disk_child);
		CFRelease (props_dict);
		IOObjectRelease (disk);
	}
	IOObjectRelease (disk_list);
/* #endif HAVE_IOKIT_IOKITLIB_H */

#elif KERNEL_LINUX
	FILE *fh;
	char buffer[1024];
	
	char *fields[32];
	int numfields;
	int fieldshift = 0;

	int minor = 0;

	counter_t read_sectors  = 0;
	counter_t write_sectors = 0;

	counter_t read_ops      = 0;
	counter_t read_merged   = 0;
	counter_t read_time     = 0;
	counter_t write_ops     = 0;
	counter_t write_merged  = 0;
	counter_t write_time    = 0;
	int is_disk = 0;

	diskstats_t *ds, *pre_ds;

	if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
	{
		fh = fopen ("/proc/partitions", "r");
		if (fh == NULL)
		{
			ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
			return (-1);
		}

		/* Kernel is 2.4.* */
		fieldshift = 1;
	}

	while (fgets (buffer, sizeof (buffer), fh) != NULL)
	{
		char *disk_name;

		numfields = strsplit (buffer, fields, 32);

		if ((numfields != (14 + fieldshift)) && (numfields != 7))
			continue;

		minor = atoll (fields[1]);

		disk_name = fields[2 + fieldshift];

		for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
			if (strcmp (disk_name, ds->name) == 0)
				break;

		if (ds == NULL)
		{
			if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
				continue;

			if ((ds->name = strdup (disk_name)) == NULL)
			{
				free (ds);
				continue;
			}

			if (pre_ds == NULL)
				disklist = ds;
			else
				pre_ds->next = ds;
		}

		is_disk = 0;
		if (numfields == 7)
		{
			/* Kernel 2.6, Partition */
			read_ops      = atoll (fields[3]);
			read_sectors  = atoll (fields[4]);
			write_ops     = atoll (fields[5]);
			write_sectors = atoll (fields[6]);
		}
		else if (numfields == (14 + fieldshift))
		{
			read_ops  =  atoll (fields[3 + fieldshift]);
			write_ops =  atoll (fields[7 + fieldshift]);

			read_sectors  = atoll (fields[5 + fieldshift]);
			write_sectors = atoll (fields[9 + fieldshift]);

			if ((fieldshift == 0) || (minor == 0))
			{
				is_disk = 1;
				read_merged  = atoll (fields[4 + fieldshift]);
				read_time    = atoll (fields[6 + fieldshift]);
				write_merged = atoll (fields[8 + fieldshift]);
				write_time   = atoll (fields[10+ fieldshift]);
			}
		}
		else
		{
			DEBUG ("numfields = %i; => unknown file format.", numfields);
			continue;
		}

		{
			counter_t diff_read_sectors;
			counter_t diff_write_sectors;

		/* If the counter wraps around, it's only 32 bits.. */
			if (read_sectors < ds->read_sectors)
				diff_read_sectors = 1 + read_sectors
					+ (UINT_MAX - ds->read_sectors);
			else
				diff_read_sectors = read_sectors - ds->read_sectors;
			if (write_sectors < ds->write_sectors)
				diff_write_sectors = 1 + write_sectors
					+ (UINT_MAX - ds->write_sectors);
			else
				diff_write_sectors = write_sectors - ds->write_sectors;

			ds->read_bytes += 512 * diff_read_sectors;
			ds->write_bytes += 512 * diff_write_sectors;
			ds->read_sectors = read_sectors;
			ds->write_sectors = write_sectors;
		}

		/* Calculate the average time an io-op needs to complete */
		if (is_disk)
		{
			counter_t diff_read_ops;
			counter_t diff_write_ops;
			counter_t diff_read_time;
			counter_t diff_write_time;

			if (read_ops < ds->read_ops)
				diff_read_ops = 1 + read_ops
					+ (UINT_MAX - ds->read_ops);
			else
				diff_read_ops = read_ops - ds->read_ops;
			DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
					"ds->read_ops = %llu; diff_read_ops = %llu;",
					disk_name,
					read_ops, ds->read_ops, diff_read_ops);

			if (write_ops < ds->write_ops)
				diff_write_ops = 1 + write_ops
					+ (UINT_MAX - ds->write_ops);
			else
				diff_write_ops = write_ops - ds->write_ops;

			if (read_time < ds->read_time)
				diff_read_time = 1 + read_time
					+ (UINT_MAX - ds->read_time);
			else
				diff_read_time = read_time - ds->read_time;

			if (write_time < ds->write_time)
				diff_write_time = 1 + write_time
					+ (UINT_MAX - ds->write_time);
			else
				diff_write_time = write_time - ds->write_time;

			if (diff_read_ops != 0)
				ds->avg_read_time += (diff_read_time
						+ (diff_read_ops / 2))
					/ diff_read_ops;
			if (diff_write_ops != 0)
				ds->avg_write_time += (diff_write_time
						+ (diff_write_ops / 2))
					/ diff_write_ops;

			ds->read_ops = read_ops;
			ds->read_time = read_time;
			ds->write_ops = write_ops;
			ds->write_time = write_time;
		} /* if (is_disk) */

		/* Don't write to the RRDs if we've just started.. */
		ds->poll_count++;
		if (ds->poll_count <= 2)
		{
			DEBUG ("disk plugin: (ds->poll_count = %i) <= "
					"(min_poll_count = 2); => Not writing.",
					ds->poll_count);
			continue;
		}

		if ((read_ops == 0) && (write_ops == 0))
		{
			DEBUG ("disk plugin: ((read_ops == 0) && "
					"(write_ops == 0)); => Not writing.");
			continue;
		}

		if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
			disk_submit (disk_name, "disk_octets",
					ds->read_bytes, ds->write_bytes);

		if ((ds->read_ops != 0) || (ds->write_ops != 0))
			disk_submit (disk_name, "disk_ops",
					read_ops, write_ops);

		if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
			disk_submit (disk_name, "disk_time",
					ds->avg_read_time, ds->avg_write_time);

		if (is_disk)
		{
			disk_submit (disk_name, "disk_merged",
					read_merged, write_merged);
		} /* if (is_disk) */
	} /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */

	fclose (fh);
/* #endif defined(KERNEL_LINUX) */

#elif HAVE_LIBKSTAT
# if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
#  define KIO_ROCTETS reads
#  define KIO_WOCTETS writes
#  define KIO_ROPS    nreads
#  define KIO_WOPS    nwrites
#  define KIO_RTIME   rtime
#  define KIO_WTIME   wtime
# elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
#  define KIO_ROCTETS nread
#  define KIO_WOCTETS nwritten
#  define KIO_ROPS    reads
#  define KIO_WOPS    writes
#  define KIO_RTIME   rtime
#  define KIO_WTIME   wtime
# else
#  error "kstat_io_t does not have the required members"
# endif
	static kstat_io_t kio;
	int i;

	if (kc == NULL)
		return (-1);

	for (i = 0; i < numdisk; i++)
	{
		if (kstat_read (kc, ksp[i], &kio) == -1)
			continue;

		if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
		{
			disk_submit (ksp[i]->ks_name, "disk_octets",
					kio.KIO_ROCTETS, kio.KIO_WOCTETS);
			disk_submit (ksp[i]->ks_name, "disk_ops",
					kio.KIO_ROPS, kio.KIO_WOPS);
			/* FIXME: Convert this to microseconds if necessary */
			disk_submit (ksp[i]->ks_name, "disk_time",
					kio.KIO_RTIME, kio.KIO_WTIME);
		}
		else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
		{
			disk_submit (ksp[i]->ks_name, "disk_octets",
					kio.KIO_ROCTETS, kio.KIO_WOCTETS);
			disk_submit (ksp[i]->ks_name, "disk_ops",
					kio.KIO_ROPS, kio.KIO_WOPS);
		}
	}
/* #endif defined(HAVE_LIBKSTAT) */

#elif defined(HAVE_LIBSTATGRAB)
	sg_disk_io_stats *ds;
	int disks, counter;
	char name[DATA_MAX_NAME_LEN];
	
	if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
		return (0);
		
	for (counter=0; counter < disks; counter++) {
		strncpy(name, ds->disk_name, sizeof(name));
		name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
		disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
		ds++;
	}
/* #endif defined(HAVE_LIBSTATGRAB) */

#elif defined(HAVE_PERFSTAT)
	counter_t read_sectors;
	counter_t write_sectors;
	counter_t read_time;
	counter_t write_time;
	counter_t read_ops;
	counter_t write_ops;
	perfstat_id_t firstpath;
	int rnumdisk;
	int i;

	if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0) 
	{
		char errbuf[1024];
		WARNING ("disk plugin: perfstat_disk: %s",
				sstrerror (errno, errbuf, sizeof (errbuf)));
		return (-1);
	}

	if (numdisk != pnumdisk || stat_disk==NULL) {
		if (stat_disk!=NULL) 
			free(stat_disk);
		stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
	} 
	pnumdisk = numdisk;

	firstpath.name[0]='\0';
	if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0) 
	{
		char errbuf[1024];
		WARNING ("disk plugin: perfstat_disk : %s",
				sstrerror (errno, errbuf, sizeof (errbuf)));
		return (-1);
	}

	for (i = 0; i < rnumdisk; i++) 
	{
		read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
		write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
		disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);

		read_ops = stat_disk[i].xrate;
		write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
		disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);

		read_time = stat_disk[i].rserv;
		read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
		write_time = stat_disk[i].wserv;
		write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
		disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
	}
#endif /* defined(HAVE_PERFSTAT) */

	return (0);
} /* int disk_read */
Пример #17
0
    void FillVObjectDefaultValue(const CEntityParent* pEntity, const string& strAttri, VOBJECT& v, const string& strDefault)
    {
        if(strDefault.empty())
        {
            FillVObjectInitValue(pEntity, strAttri, v);
            return;
        }

        switch(v.vt)
        {
            case V_UINT8:
            {
                v.vv.u8 = (uint8_t)atoi(strDefault.c_str());
                break;
            }
            case V_INT8:
            {
                v.vv.i8 = (int8_t)atoi(strDefault.c_str());
                break;
            }
            case V_UINT16:
            {
                v.vv.u16 = (uint16_t)atoi(strDefault.c_str());
                break;
            }
            case V_INT16:
            {
                v.vv.i16 = (int16_t)atoi(strDefault.c_str());
                break;
            }
            case V_UINT32:
            {
                v.vv.u32 = (uint32_t)atoll(strDefault.c_str());
                break;
            }
            case V_INT32:
            {
                v.vv.i32 = (int32_t)atoll(strDefault.c_str());
                break;
            }
            case V_UINT64:
            {
                v.vv.u64 = (uint64_t)atoll(strDefault.c_str());
                break;
            }
            case V_INT64:
            {
                v.vv.i64 = (int64_t)atoll(strDefault.c_str());
                break;
            }
            case V_FLOAT32:
            {
                v.vv.f32 = (float32_t)atof(strDefault.c_str());
                break;
            }
            case V_FLOAT64:
            {
                v.vv.f64 = (float64_t)atof(strDefault.c_str());
                break;
            }
            case V_STR:
            {
                v.vv.s = new string(strDefault);
                break;
            }
            case V_BLOB:
            {
                v.vv.p = new charArrayDummy;
                break;
            }
            case V_LUATABLE:
            {
                lua_State* L = GetWorld()->GetLuaState();

#ifdef __USE_MSGPACK

                if( LuaUnpickleFromBlob(L, (char*)strDefault.c_str(), (uint16_t)strDefault.size()))
#else
                if( LuaUnpickleFromString(L, strDefault) )
#endif
                {
                    CLuaCallback& cb = GetWorld()->GetLuaTables();
                    v.vv.i32 = (int32_t)cb.Ref(L);
                    break;
                }
                else
                {
                    //初始化失败,give warning,todo
                    v.vv.i32 = -1;
                }
                break;
            }
            case V_LUA_OBJECT:
            {
                v.vv.i32 = -1;
                break;
            }
            case V_REDIS_HASH:
            {
                lua_State* L = GetWorld()->GetLuaState();
                CRedisHash* p = CreateRedisHash(L);
                p->SetEntity(pEntity, strAttri);
                v.vv.p = p;
                break;
            }
            default:
            {
                //nothing to do
            }
        }

    }
Пример #18
0
int
main(int argc, char ** argv) {
    extern char * optarg;
    extern int    optind;
    extern int    opterr;
    extern int    optopt;
    int           c;

    while ((c = getopt(argc, argv, "t:a:p:b:ndrs:")) != -1) {
        switch (c) {
            case 't':
                num_threads  = atoi(optarg);
                break;
            case 'a':
                baddr        = strdup(optarg);
                break;
            case 'p':
                bport        = atoi(optarg);
                break;
            case 'b':
                backlog      = atoll(optarg);
            case 'n':
                nodelay      = 1;
                break;
            case 'd':
                defer_accept = 1;
                break;
            case 'r':
                reuse_port   = 1;
                break;
            case 's':
                payload_sz   = atoll(optarg);
                break;
            default:
                fprintf(stdout, "Usage: %s [flags]\n", argv[0]);
                fprintf(stdout, "  -t <n> : number of worker threads [Default: %d]\n", num_threads);
                fprintf(stdout, "  -a <s> : bind address             [Default: %s]\n", baddr);
                fprintf(stdout, "  -p <n> : bind port                [Default: %d]\n", bport);
                fprintf(stdout, "  -b <b> : listen backlog           [Default: %d]\n", backlog);
                fprintf(stdout, "  -s <n> : size of the response     [Default: %zu]\n", payload_sz);
                fprintf(stdout, "  -n     : disable nagle (nodelay)  [Default: %s]\n", nodelay ? "true" : "false");
                fprintf(stdout, "  -d     : enable deferred accept   [Default: %s]\n", defer_accept ? "true" : "false");
                fprintf(stdout, "  -r     : enable linux reuseport   [Default: %s]\n", reuse_port ? "true" : "false");
                exit(EXIT_FAILURE);
        } /* switch */
    }

    {
        struct event_base * evbase;
        evhtp_t           * htp;
        char                payload[payload_sz];

        evbase = event_base_new();
        evhtp_alloc_assert(evbase);

        htp    = evhtp_new(evbase, NULL);
        evhtp_alloc_assert(htp);

        evhtp_set_parser_flags(htp, EVHTP_PARSE_QUERY_FLAG_LENIENT);

        htp->enable_nodelay      = nodelay;
        htp->enable_defer_accept = defer_accept;
        htp->enable_reuseport    = reuse_port;

        memset(payload, 0x42, payload_sz);

        evhtp_assert(evhtp_set_cb(htp, "/data", response_cb, payload));

#ifndef EVHTP_DISABLE_EVTHR
        if (num_threads > 0) {
            evhtp_assert(evhtp_use_threads(htp, NULL, num_threads, NULL) != -1);
        }
#endif

        evhtp_errno_assert(evhtp_bind_socket(htp, baddr, bport, backlog) >= 0);
        event_base_loop(evbase, 0);
    }


    return 0;
} /* main */
Пример #19
0
int main(int argc, char **argv)
{
	pthread_t *pthreads;
	int pids[16];
	int i, j;
	long long time, time1;
	size_t len;
	char c = 'a';
	char unit;
	struct timespec start, end;
	struct timeval start1, end1;
	int size;
	unsigned long long count;
	FILE *output;
	char fs_type[20];
	char quill_enabled[40];
	char file_size_num[20];
	char filename[60];
	unsigned long long offset;

	if (argc < 7) {
		printf("Usage: ./pthread_test $FS $QUILL $fops $num_threads $FILE_SIZE $filename\n");
		return 0;
	}

	strcpy(fs_type, argv[1]);
	strcpy(quill_enabled, argv[2]);

	if (!strcmp(argv[3], "read"))
		fops = read;
	else if (!strcmp(argv[3], "pread"))
		fops = pread;
	else if (!strcmp(argv[3], "write"))
		fops = write;
	else if (!strcmp(argv[3], "pwrite"))
		fops = pwrite;
	else {
		printf("fops error!\n");
		return 0;
	}

	num_threads = atoi(argv[4]);
	if (num_threads <= 0 || num_threads > 16)
		num_threads = 1;

	strcpy(file_size_num, argv[5]);
	len = strlen(file_size_num);
	unit = file_size_num[len - 1];
	file_size_num[len - 1] = '\0';
	FILE_SIZE = atoll(file_size_num);
	switch (unit) {
	case 'K':
	case 'k':
		FILE_SIZE *= 1024;
		break;
	case 'M':
	case 'm':
		FILE_SIZE *= 1048576;
		break;
	case 'G':
	case 'g':
		FILE_SIZE *= 1073741824;
		break;
	default:
		printf("ERROR: FILE_SIZE should be #K/M/G format.\n");
		return 0;
		break;
	}

	if (FILE_SIZE < END_SIZE)
		FILE_SIZE = END_SIZE;
	if (FILE_SIZE > 2147483648) // RAM disk size
		FILE_SIZE = 2147483648;

	strcpy(filename, argv[6]);
	output = fopen(filename, "a");

	printf("# pthreads: %d\n", num_threads);
	buf = (char **)malloc(num_threads * sizeof(char *));
	for (i = 0; i < num_threads; i++) { 
		if (posix_memalign((void **)(buf + i), END_SIZE, END_SIZE)) { // up to 64MB
			printf("ERROR - POSIX NOMEM!\n");
			return 0;
		}
	}

	printf("# fds: ");
	fd = malloc(num_threads * sizeof(int));
	for (i = 0; i < num_threads; i++) {
		fd[i] = open("/mnt/ramdisk/test1", O_CREAT | O_RDWR, 0640); 
		printf("%d ", fd[i]);
	}
	printf("\n");

	//Warm up
	printf("warm up...\n");
	size = 4096;
	count = FILE_SIZE / (size * num_threads);
	for (i = 0; i < num_threads; i++) {
		offset = FILE_SIZE / num_threads * i;
		if (fops == read || fops == write)
			lseek(fd[i], offset, SEEK_SET);
		for (j = 0; j < count; j++) { 
//			pwrite(fd, buf[i], size, offset);
			if (fops == read || fops == write)
				fops(fd[i], buf[i], size);
			else
				fops(fd[i], buf[i], size, offset);
			offset += size;
		}
	}
	printf("warm up finished\n");

	//Allocate the threads
	pthreads = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
	for (i = 0; i < num_threads; i++) {
		pids[i] = i;
		pthread_create(pthreads + i, NULL, pthread_transfer, (void *)(pids + i)); 
	}

	for (size = start_size; size <= END_SIZE; size <<= 1) {
		count = FILE_SIZE / (size * num_threads);
		for (i = 0; i < num_threads; i++)
			memset(buf[i], c, size);
		c++;
		for (i = 0; i < num_threads; i++)
			lseek(fd[i], 0, SEEK_SET);

		clock_gettime(CLOCK_MONOTONIC, &start);
		gettimeofday(&start1, NULL);
		start_all_pthreads();

		while (!all_pthreads_finished())
			;
		clock_gettime(CLOCK_MONOTONIC, &end);
		gettimeofday(&end1, NULL);

		time = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);
		time1 = (end1.tv_sec - start1.tv_sec) * 1e6 + (end1.tv_usec - start1.tv_usec);
		printf("%s: Size %d bytes,\t %lld times,\t %lld nanoseconds, \t %lld us, \t Bandwidth %f MB/s, latency %lld nanoseconds.\n", argv[3], size, count, time, time1, FILE_SIZE * 1024.0 / time, time / count);
		fprintf(output, "%s,%s,%s,%d,%d,%lld,%lld,%lld,%f\n", fs_type, quill_enabled, argv[3], num_threads, size, FILE_SIZE, count, time, FILE_SIZE * 1.0 / time);
	}

	fclose(output);
	for (i = 0; i < num_threads; i++) {
		pthread_join(pthreads[i], NULL);
		free(buf[i]);
		close(fd[i]);
	}
	free(buf);
	free(fd);
	free(pthreads);
	return 0;
}
Пример #20
0
/* A function to fix the locked reactions by reading the optional argument -L */
int fix_locked (int n_locked, double *s, double **s_locked, double *lock_v, char *locked){
    
    char *l_index1 = locked, *l_index2, *l_index3, *copy1, *copy2;
    
    int locked_string_length = strlen( locked ), which, n_zeros=0;
    
    double l_value, **dummy_s = s_locked;
    
    
    /* if there is only one locked reaction */
    if ( n_locked == 1 ){
        
        /* cut the string at ":" separating the index of reaction to lock from the value to assign */
        l_index1 = strchr(locked, ':');
        
        /* copy the index bit of the string to a new string */
        copy1 = get_substring (locked, l_index1);
        
        /* get the index from the substring */
        which = atoll (copy1) - 1;
        
        /* get the value to assign from the rest of the string */
        l_value = atof (l_index1 + 1);
        
        /* if value = 0 flag that there is a null reaction*/
        if (l_value == 0.) n_zeros += 1;
        
        /* assign the pointer to the fixed reaction */
        *dummy_s = s + which;
        
        /* assign the value to the array of fixed values lock_v*/
        *lock_v = l_value;
        
        /* free the substring */
        free ( copy1 );
        
    }
    
    /* otherwise */
    else if ( n_locked > 1 ){
        
        /* for each fixed reaction (found through the ":")*/
        while ( (int) (l_index1 - locked) < locked_string_length + 1 && strchr(l_index1, ':') != NULL )  {
            
            /* find the substring containing the index of the reaction */
            l_index2 = strchr(l_index1, ':');
            
            /* find the substring containing the value to assign */
            l_index3 = strchr(l_index1, ',');
            
            /* if it is the last reaction there are no more "," - the value substring lasts till the end */
            if ( l_index3 == NULL ) l_index3 = locked + locked_string_length;
            
            /* copy the index substring to a new string */
            copy1 = get_substring (l_index1, l_index2);
            
            /* get the numerical index of the index */
            which = atoi (copy1) - 1;
            
            /* copy the value substring to a new string */
            copy2 = get_substring (l_index2 + 1 , l_index3);
            
            /* get the numerical value of the flux value */
            l_value = atof (copy2);
            
            /* if value = 0 flag that there is a null reaction*/
            if (l_value == 0.) n_zeros += 1;
            
            /* assign a pointer to the locked reaction */
            *dummy_s = s + which;
            
            /* assign the value to the array */
            *lock_v = l_value;
            
            /* free the first substring */
            free ( copy1 );
            
            /* free the second substring */
            free ( copy2 );
            
            /* get to next pointer to locked reactions */
            dummy_s++;
            
            /* get to next array element of locked values*/
            lock_v++;
            
            /* iterate over the whole string up to the next "," */
            l_index1 = l_index3 + 1;
            
        }
        
    }
    
    /* return the number of zero reactions*/
    /* useful to check the problem is solvable, i.e. no metabolite is consumed only */
    return n_zeros;
    
}
Пример #21
0
int
main(int argc, char **argv)
{
	int		bigfd;
	int		c;
	int		oflag = 0;
	int		err = 0;
	int		k;
	long		i;
	int64_t		good_writes = 0;
	uchar_t		nxtfillchar;
	char		*prog = argv[0];
	/*
	 * Default Parameters
	 */
	int		write_count = BIGFILESIZE;
	uchar_t		fillchar = DATA;
	int		block_size = BLOCKSZ;
	char		*filename = NULL;
	char		*operation = NULL;
	offset_t	noffset, offset = 0;
	int		verbose = 0;
	int		rsync = 0;
	int		wsync = 0;

	/*
	 * Process Arguments
	 */
	while ((c = getopt(argc, argv, "b:c:d:s:f:o:vwr")) != -1) {
		switch (c) {
			case 'b':
				block_size = atoi(optarg);
				break;
			case 'c':
				write_count = atoi(optarg);
				break;
			case 'd':
				fillchar = atoi(optarg);
				break;
			case 's':
				offset = atoll(optarg);
				break;
			case 'f':
				filename = optarg;
				break;
			case 'o':
				operation = optarg;
				break;
			case 'v':
				verbose = 1;
				break;
			case 'w':
				wsync = 1;
				break;
			case 'r':
				rsync = 1;
				break;
			case '?':
				(void) printf("unknown arg %c\n", optopt);
				usage(prog);
				break;
		}
	}

	/*
	 * Validate Parameters
	 */
	if (!filename) {
		(void) printf("Filename not specified (-f <file>)\n");
		err++;
	}

	if (!operation) {
		(void) printf("Operation not specified (-o <operation>).\n");
		err++;
	}

	if (block_size > BIGBUFFERSIZE) {
		(void) printf("block_size is too large max==%d.\n",
		    BIGBUFFERSIZE);
		err++;
	}

	if (err) usage(prog);

	/*
	 * Prepare the buffer and determine the requested operation
	 */
	nxtfillchar = fillchar;
	k = 0;

	for (i = 0; i < block_size; i++) {
		bigbuffer[i] = nxtfillchar;

		if (fillchar == 0) {
			if ((k % DATA_RANGE) == 0) {
				k = 0;
			}
			nxtfillchar = k++;
		}
	}

	/*
	 * using the strncmp of operation will make the operation match the
	 * first shortest match - as the operations are unique from the first
	 * character this means that we match single character operations
	 */
	if ((strncmp(operation, "create", strlen(operation) + 1)) == 0 ||
	    (strncmp(operation, "overwrite", strlen(operation) + 1)) == 0) {
		oflag = (O_RDWR|O_CREAT);
	} else if ((strncmp(operation, "append", strlen(operation) + 1)) == 0) {
		oflag = (O_RDWR|O_APPEND);
	} else {
		(void) printf("valid operations are <create|append> not '%s'\n",
		    operation);
		usage(prog);
	}

	if (rsync) {
		oflag = oflag | O_RSYNC;
	}

	if (wsync) {
		oflag = oflag | O_SYNC;
	}

	/*
	 * Given an operation (create/overwrite/append), open the file
	 * accordingly and perform a write of the appropriate type.
	 */
	if ((bigfd = open(filename, oflag, 0666)) == -1) {
		(void) printf("open %s: failed [%s]%d. Aborting!\n", filename,
		    strerror(errno), errno);
		exit(errno);
	}
	noffset = lseek64(bigfd, offset, SEEK_SET);
	if (noffset != offset) {
		(void) printf("llseek %s (%lld/%lld) failed [%s]%d.Aborting!\n",
		    filename, offset, noffset, strerror(errno), errno);
		exit(errno);
	}

	if (verbose) {
		(void) printf("%s: block_size = %d, write_count = %d, "
		    "offset = %lld, data = %s%d\n", filename, block_size,
		    write_count, offset,
		    (fillchar == 0) ? "0->" : "",
		    (fillchar == 0) ? DATA_RANGE : fillchar);
	}

	for (i = 0; i < write_count; i++) {
		ssize_t n;

		if ((n = write(bigfd, &bigbuffer, block_size)) == -1) {
			(void) printf("write failed (%ld), good_writes = %"
			    PRId64 ", " "error: %s[%d]\n",
			    (long)n, good_writes,
			    strerror(errno),
			    errno);
			exit(errno);
		}
		good_writes++;
	}

	if (verbose) {
		(void) printf("Success: good_writes = %" PRId64 "(%"
		    PRId64 ")\n", good_writes, (good_writes * block_size));
	}

	return (0);
}
Пример #22
0
TokenNumber::TokenNumber(const std::string &number)
 : Token(TokenType::NUMBER_LITERAL)
{
	value = atoll(number.c_str());
}
int main(int argc, char *argv[])
{
	unsigned int bytes = 0;
	uint16_t i = 0;
	lockdownd_service_descriptor_t service = NULL;
	lockdownd_client_t client = NULL;
	idevice_t phone = NULL;
	uint64_t lockfile = 0;
	np_client_t gnp = NULL;

	if (argc > 1 && !strcasecmp(argv[1], "--debug")) {
		idevice_set_debug_level(1);
	} else {
		idevice_set_debug_level(0);
	}

	if (IDEVICE_E_SUCCESS != idevice_new(&phone, NULL)) {
		printf("No device found, is it plugged in?\n");
		return -1;
	}

	char *udid = NULL;
	if (IDEVICE_E_SUCCESS == idevice_get_udid(phone, &udid)) {
		printf("DeviceUniqueID : %s\n", udid);
	}
	if (udid)
		free(udid);

	if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "ideviceclient")) {
		idevice_free(phone);
		printf("Exiting.\n");
		return -1;
	}

	char *nnn = NULL;
	if (LOCKDOWN_E_SUCCESS == lockdownd_get_device_name(client, &nnn)) {
		printf("DeviceName : %s\n", nnn);
		free(nnn);
	}

	lockdownd_start_service(client, "com.apple.afc", &service);

	if (service && service->port) {
		afc_client_t afc = NULL;
		afc_client_new(phone, service, &afc);

		if (afc) {
			service->port = 0;
			service->ssl_enabled = 0;
			lockdownd_start_service(client, "com.apple.mobile.notification_proxy", &service);
			if (service->port) {
				printf("Notification Proxy started.\n");
				np_client_new(phone, service, &gnp);
			} else {
				printf("ERROR: Notification proxy could not be started.\n");
			}

			if (gnp) {
				const char *nspec[5] = {
					NP_SYNC_CANCEL_REQUEST,
					NP_SYNC_SUSPEND_REQUEST,
					NP_SYNC_RESUME_REQUEST,
					NP_ITDBPREP_DID_END,
					NULL
				};
				np_observe_notifications(gnp, nspec);
				np_set_notify_callback(gnp, notifier, NULL);
			}

			perform_notification(phone, client, NP_SYNC_WILL_START);

			afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile);
			if (lockfile) {
				printf("locking file\n");
				afc_file_lock(afc, lockfile, AFC_LOCK_EX);

				perform_notification(phone, client, NP_SYNC_DID_START);
			}

			char **dirs = NULL;
			afc_read_directory(afc, "/eafaedf", &dirs);
			if (!dirs)
				afc_read_directory(afc, "/", &dirs);
			printf("Directory time.\n");
			for (i = 0; dirs[i]; i++) {
				printf("/%s\n", dirs[i]);
				free(dirs[i]);
			}
			if (dirs)
				free(dirs);

			dirs = NULL;
			afc_get_device_info(afc, &dirs);
			if (dirs) {
				for (i = 0; dirs[i]; i += 2) {
					printf("%s: %s\n", dirs[i], dirs[i + 1]);
					free(dirs[i]);
				}
				free(dirs);
			}

			uint64_t my_file = 0;
			char **info = NULL;
			uint64_t fsize = 0;
			if (AFC_E_SUCCESS == afc_get_file_info(afc, "/readme.libimobiledevice.fx", &info) && info) {
				for (i = 0; info[i]; i += 2) {
					printf("%s: %s\n", info[i], info[i+1]);
					if (!strcmp(info[i], "st_size")) {
						fsize = atoll(info[i+1]);
					}
				}
			}

			if (AFC_E_SUCCESS ==
				afc_file_open(afc, "/readme.libimobiledevice.fx", AFC_FOPEN_RDONLY, &my_file) && my_file) {
				printf("A file size: %llu\n", (long long)fsize);
				char *file_data = (char *) malloc(sizeof(char) * fsize);
				afc_file_read(afc, my_file, file_data, fsize, &bytes);
				if (bytes > 0) {
					printf("The file's data:\n");
					fwrite(file_data, 1, bytes, stdout);
				}
				printf("\nClosing my file.\n");
				afc_file_close(afc, my_file);
				free(file_data);
			} else
				printf("couldn't open a file\n");

			afc_file_open(afc, "/readme.libimobiledevice.fx", AFC_FOPEN_WR, &my_file);
			if (my_file) {
				char *outdatafile = strdup("this is a bitchin text file\n");
				afc_file_write(afc, my_file, outdatafile, strlen(outdatafile), &bytes);
				free(outdatafile);
				if (bytes > 0)
					printf("Wrote a surprise. ;)\n");
				else
					printf("I wanted to write a surprise, but... :(\n");
				afc_file_close(afc, my_file);
			}
			printf("Deleting a file...\n");
			bytes = afc_remove_path(afc, "/delme");
			if (bytes)
				printf("Success.\n");
			else
				printf("Failure. (expected unless you have a /delme file on your phone)\n");

			printf("Renaming a file...\n");
			bytes = afc_rename_path(afc, "/renme", "/renme2");
			if (bytes > 0)
				printf("Success.\n");
			else
				printf("Failure. (expected unless you have a /renme file on your phone)\n");

			printf("Seek & read\n");
			afc_file_open(afc, "/readme.libimobiledevice.fx", AFC_FOPEN_RDONLY, &my_file);
			if (AFC_E_SUCCESS != afc_file_seek(afc, my_file, 5, SEEK_CUR))
				printf("WARN: SEEK DID NOT WORK\n");
			char *threeletterword = (char *) malloc(sizeof(char) * 5);
			afc_file_read(afc, my_file, threeletterword, 3, &bytes);
			threeletterword[3] = '\0';
			if (bytes > 0)
				printf("Result: %s\n", threeletterword);
			else
				printf("Couldn't read!\n");
			free(threeletterword);
			afc_file_close(afc, my_file);
		}

		if (gnp && lockfile) {
			printf("XXX sleeping\n");
			sleep(5);

			printf("XXX unlocking file\n");
			afc_file_lock(afc, lockfile, AFC_LOCK_UN);

			printf("XXX closing file\n");
			afc_file_close(afc, lockfile);

			printf("XXX sleeping\n");
			sleep(5);
			//perform_notification(phone, client, NP_SYNC_DID_FINISH);
		}

		if (gnp) {
			np_client_free(gnp);
			gnp = NULL;
		}

		afc_client_free(afc);

		lockdownd_service_descriptor_free(service);
		service = NULL;
	} else {
		printf("Start service failure.\n");
	}

	printf("All done.\n");

	lockdownd_client_free(client);
	idevice_free(phone);

	return 0;
}
Пример #24
0
/* options__process
 * A snippet from main() to get all the options sent via CLI, then verifies them.
 */
void options__process(int argc, char **argv) {
  /* Reset everything since there isn't an initialization function for Options structs. */
  /* Page Information */
  opts.page_directory = (char *)malloc(strlen("sample_data") + 1); strcpy(opts.page_directory, "sample_data");
  opts.page_count = 0;
  opts.page_limit = MAX_PAGE_LIMIT;
  opts.smallest_page = UINT16_MAX;
  opts.biggest_page = 0;
  opts.dataset_size = 0;
  opts.dataset_max = MAX_DATASET_MAX;
  /* Resource Control */
  opts.max_memory = 10 * 1024 * 1024;
  opts.fixed_ratio = -1;
  opts.workers = sysconf(_SC_NPROCESSORS_ONLN) > 0 ? (uint16_t)sysconf(_SC_NPROCESSORS_ONLN) : 1;
  opts.cpu_count = sysconf(_SC_NPROCESSORS_ONLN) > 0 ? (uint16_t)sysconf(_SC_NPROCESSORS_ONLN) : 1;
  /* Tyche Management */
  opts.duration = 5;
  opts.compressor_id = LZ4_COMPRESSOR_ID;
  opts.compressor_level = 1;
  opts.min_pages_retrieved = 5;
  opts.max_pages_retrieved = 5;
  opts.bias_percent = 1.0;
  opts.bias_aggregate = 1.0;
  opts.update_frequency = 0.0;
  opts.delete_frequency = 0.0;
  /* Run Test? */
  opts.test = NULL;
  opts.extended_test_options = NULL;
  /* Niceness Features */
  opts.quiet = 0;
  opts.verbosity = 0;

  /* Process everything passed from CLI now. */
  char *save_ptr = NULL;
  char *token = NULL;
  int c = 0;
  opterr = 0;
  while ((c = getopt(argc, argv, "b:B:c:Cd:D:f:hm:M:n:p:qt:U:w:X:v")) != -1) {
    switch (c) {
      case 'b':
        opts.dataset_max = (uint64_t)atoll(optarg);
        break;
      case 'B':
        token = strtok_r(optarg, ",", &save_ptr);
        if(token != NULL)
          opts.bias_percent = 1.0 * atof(token) / 100;
        token = strtok_r(NULL, ",", &save_ptr);
        if (token != NULL)
          opts.bias_aggregate = 1.0 * atof(token) / 100;
        break;
      case 'c':
        if(strcmp(optarg, "lz4") != 0 && strcmp(optarg, "zlib") != 0 && strcmp(optarg, "zstd"))
          show_error(E_BAD_CLI, "You must specify either 'lz4' or 'zlib' for compression (-c), not: %s", optarg);
        if(strcmp(optarg, "lz4") == 0)
          opts.compressor_id = LZ4_COMPRESSOR_ID;
        if(strcmp(optarg, "zlib") == 0)
          opts.compressor_id = ZLIB_COMPRESSOR_ID;
        if(strcmp(optarg, "zstd") == 0)
          opts.compressor_id = ZSTD_COMPRESSOR_ID;
        break;
      case 'C':
        opts.compressor_id = NO_COMPRESSOR_ID;
        break;
      case 'd':
        opts.duration = (uint16_t)atoi(optarg);
        if (atoi(optarg) > MAX_DURATION)
          opts.duration = MAX_DURATION;
        break;
      case 'D':
        opts.delete_frequency = 1.0 * atof(optarg) / 100;
        break;
      case 'f':
        opts.fixed_ratio = (int8_t)atoi(optarg);
        break;
      case 'h':
        options__show_help();
        exit(E_OK);
        break;
      case 'm':
        opts.max_memory = (uint64_t)atoll(optarg);
        break;
      case 'M':
        token = strtok_r(optarg, ",", &save_ptr);
        if(token != NULL)
          opts.min_pages_retrieved = atoi(token);
        token = strtok_r(NULL, ",", &save_ptr);
        if (token != NULL)
          opts.max_pages_retrieved = atoi(token);
        break;
      case 'n':
        opts.page_limit = (uint32_t)atoll(optarg);
        break;
      case 'p':
        opts.page_directory = optarg;
        break;
      case 'q':
        opts.quiet = 1;
        break;
      case 't':
        if (opts.test != NULL)
          show_error(E_BAD_CLI, "You cannot specify the -t option more than once.");
        opts.test = optarg;
        break;
      case 'U':
        opts.update_frequency = 1.0 * atof(optarg) / 100;
        break;
      case 'w':
        opts.workers = (uint16_t)atoi(optarg);
        if (atoi(optarg) > MAX_WORKERS)
          opts.workers = MAX_WORKERS;
        break;
      case 'X':
        free(opts.extended_test_options);
        opts.extended_test_options = optarg;
        if(strcmp(opts.extended_test_options, "help") == 0) {
          options__show_extended_test_options();
          exit(E_OK);
        }
        break;
      case 'v':
        if(opts.verbosity >= MAX_VERBOSITY)
          show_error(E_BAD_CLI, "Verbosity is already at maximum value: %d", opts.verbosity);
        opts.verbosity++;
        break;
      case '?':
        options__show_help();
        if (optopt == 'b' || optopt == 'B' || optopt == 'c' || optopt == 'd' || optopt == 'D' || optopt == 'f' || optopt == 'm' || optopt == 'M' || optopt == 'n' || optopt == 'p' || optopt == 't' || optopt == 'U' || optopt == 'w' || optopt == 'X')
          show_error(E_BAD_CLI, "Option -%c requires an argument.", optopt);
        if (isprint (optopt))
          show_error(E_BAD_CLI, "Unknown option `-%c'.", optopt);
        show_error(E_BAD_CLI, "Unknown option character `\\x%x'.\n", optopt);
        break;
      default:
        options__show_help();
        exit(E_BAD_CLI);
    }
  }

  /* Pre-flight Checks */
  // -- A page directory is always required.  If it's an invalid path, the io__* functions will catch it.
  if (opts.page_directory == NULL)
    show_error(E_BAD_CLI, "You must specify a directory to search for pages for the test (-p).");
  // -- Memory needs to be at least MIN_MEMORY and less than the installed physical memory.
  const size_t PHYSICAL_MEMORY = (size_t)sysconf(_SC_PHYS_PAGES) * (size_t)sysconf(_SC_PAGESIZE);
  if (opts.max_memory < MIN_MEMORY)
    show_error(E_BAD_CLI, "The memory argument you supplied (-m) is too low.  You sent %"PRIu64", but a minimum of %"PRIu64" is required.", opts.max_memory, MIN_MEMORY);
  if (PHYSICAL_MEMORY == 0)
    show_error(E_GENERIC, "Unable to discern the amount of memory this system has.  Can't be sure we have enough memory to do this test.");
  if (opts.max_memory > PHYSICAL_MEMORY)
    show_error(E_BAD_CLI, "The memory argument you supplied (-m) is too high.  You sent %"PRIu64", but your system maximum physical memory is %d.", opts.max_memory, PHYSICAL_MEMORY);
  // -- Fixed ratio should be -1 (not fixed) or 1 to 100.  Zero is either an atoi() error or nonsensical (can't have just a compressed list and 0% raw.
  if (opts.fixed_ratio == 0)
    show_error(E_BAD_CLI, "The fixed ratio (-f) is 0.  You either sent invalid input (atoi() failed), or you misunderstood the option; fixed size of 0 would mean 0% for raw buffers which is nonsensical.");
  if (opts.fixed_ratio < -1)
    show_error(E_BAD_CLI, "The fixed ratio (-f) cannot be negative... that's just weird.  Why did you send %"PRIi8"?", opts.fixed_ratio);
  if (opts.fixed_ratio > 100)
    show_error(E_BAD_CLI, "The fixed ratio (-f) cannot be over 100... you can't have more than 100%% of your memory assigned to something.  You sent %"PRIi8".", opts.fixed_ratio);
  // -- Workers must be 1+.  Will be 0 if atoi() fails or user is derp.
  if (opts.workers == 0)
    show_error(E_BAD_CLI, "The worker count (-w) is 0.  You either sent invalid input (atoi() failed), or you misunderstood the option.  You need at least 1 worker to, ya know, do work.");
  if (opts.workers == MAX_WORKERS)
    show_error(E_BAD_CLI, "You specified more workers (-w) than allowed (max: %d).", MAX_WORKERS);
  // -- Duration must be non-zero and less than MAX_DURATION.
  if (opts.duration == 0)
    show_error(E_BAD_CLI, "The duration (-d) is 0.  You either sent invalid input (atoi() failed), or you misunderstood the option.  The test must run for at least 1 second.");
  if (opts.duration == MAX_DURATION)
    show_error(E_BAD_CLI, "You specified a duration (-d) greater than the max allowed (%d).", MAX_DURATION);
  // -- Dataset max cannot be 0.  Other than that... shrugs.
  if (opts.dataset_max == 0)
    show_error(E_BAD_CLI, "The maximum dataset bytes (-b) is 0.  You either sent invalid input (atoi() failed), or you misunderstood the option; it limits the number of bytes the scan functions will find before moving on with the test.");
  // -- Page limit cannot be 0.
  if (opts.page_limit == 0)
    show_error(E_BAD_CLI, "The page limit (-n) is 0.  You either sent invalid input (atoi() failed), or you misunderstood the option; it limits the number of pages the scan functions will find before moving on with the test.");
  // -- When compression is disabled, warn the user!
  if (opts.compressor_id == NO_COMPRESSOR_ID)
    fprintf(stderr, "WARNING!!  Compression is DISABLED (you sent -C).\n");
  // -- If bias isn't between 0 and 100 we're in trouble.
  if (opts.bias_percent < 0 || opts.bias_percent > 100)
    show_error(E_BAD_CLI, "The bias percentage (-B X,Y) must be between 0 and 100 inclusive, not %d.\n", opts.bias_percent);
  if (opts.bias_aggregate < 0 || opts.bias_aggregate > 100)
    show_error(E_BAD_CLI, "The bias aggregate (-B X,Y) must be between 0 and 100 inclusive, not %d.\n", opts.bias_aggregate);
  // -- If the update or delete frequencies aren't between 0 and 100 we're in trouble.
  if (opts.update_frequency < 0 || opts.update_frequency > 100)
    show_error(E_BAD_CLI, "The update frequency (-U) must be between 0 and 100 inclusive, not %d.\n", opts.update_frequency);
  if (opts.delete_frequency < 0 || opts.delete_frequency > 100)
    show_error(E_BAD_CLI, "The delete frequency (-D) must be between 0 and 100 inclusive, not %d.\n", opts.delete_frequency);
  // -- The min pages retrieved in a round can't greater than max...
  if (opts.min_pages_retrieved > opts.max_pages_retrieved)
    show_error(E_BAD_CLI, "You can't set the minimum pages per round (X) higher than the maximum per round (Y) for -M.\n");

  return;
}
Пример #25
0
int main(int argc,
         char **argv)
{
    long long z;
    struct zbc_device *dev;
    int i, ret = 1;
    zbc_zone_t *zones = NULL;
    char *path;

    /* Check command line */
    if ( argc < 2 ) {
usage:
        printf("Usage: %s [option] <dev> <lba>\n"
               "   lba -1 is to set all bit flag\n"
               "Options:\n"
               "    -v   : Verbose mode\n",
               argv[0]);
        return( 1 );
    }

    /* Parse options */
    for(i = 1; i < (argc - 1); i++) {

        if ( strcmp(argv[i], "-v") == 0 ) {

            zbc_set_log_level("debug");

        } else if ( argv[i][0] == '-' ) {

            printf("Unknown option \"%s\"\n",
                   argv[i]);
            goto usage;

        } else {

            break;

        }

    }

    if ( i != (argc - 2) ) {
        goto usage;
    }

    /* Open device */
    path = argv[i];
    z = atoll(argv[i+1]);

    ret = zbc_open(path, O_RDONLY, &dev);
    if ( ret != 0 ) {
	fprintf(stderr, "[TEST][ERROR],open device failed\n");
	printf("[TEST][ERROR][SENSE_KEY],open-device-failed\n");
	printf("[TEST][ERROR][ASC_ASCQ],open-device-failed\n");
        return( 1 );
    }

    /* Reset write pointer */
    ret = zbc_reset_write_pointer(dev, (uint64_t)z);
    if ( ret != 0 ) {
        fprintf(stderr,
                "[TEST][ERROR],zbc_test_reset_write_ptr failed\n");

        {
            zbc_errno_t zbc_err;
            const char *sk_name;
            const char *ascq_name;

            zbc_errno(dev, &zbc_err);
            sk_name = zbc_sk_str(zbc_err.sk);
            ascq_name = zbc_asc_ascq_str(zbc_err.asc_ascq);

            printf("[TEST][ERROR][SENSE_KEY],%s\n", sk_name);
            printf("[TEST][ERROR][ASC_ASCQ],%s\n", ascq_name);
        }

        ret = 1;
    }

    if ( zones ) {
        free(zones);
    }

    /* Close device file */
    zbc_close(dev);

    return( ret );

}
Пример #26
0
bool cConfigItem_integer::setParamFromValueStr(string value_str) {
	if(value_str.empty()) {
		return(false);
	}
	int ok = 0;
	const char *value = value_str.c_str();
	if(value) {
		int _value;
		if(getValueFromMapValues(value, &_value)) {
			if(param_int) {
				*param_int = _value;
				++ok;
			}
			if(param_uint) {
				*param_uint = _value;
				++ok;
			}
			if(param_int64) {
				*param_int64 = _value;
				++ok;
			}
			if(param_uint64) {
				*param_uint64 = _value;
				++ok;
			}
			return(ok > 0);
		}
		if(param_int) {
			*param_int = atoi(value);
			if(maximum && *param_int > maximum) {
				*param_int = maximum;
			}
			if(minimum && *param_int < minimum) {
				*param_int = minimum;
			}
			if(ifZeroOrNegative && *param_int <= 0) {
				*param_int = ifZeroOrNegative;
			}
			if(multiple) {
				*param_int *= multiple;
			}
			if(!*param_int && yesValue && yesno(value)) {
				*param_int = yesValue;
			}
			++ok;
		}
		if(param_uint) {
			if(ip) {
				struct sockaddr_in sa;
				inet_pton(AF_INET, value, &sa.sin_addr);
				*param_uint = sa.sin_addr.s_addr;
			} else  {
				*param_uint = atol(value);
				if(maximum && *param_uint > (unsigned)maximum) {
					*param_uint = maximum;
				}
				if(minimum && *param_uint < (unsigned)minimum) {
					*param_uint = minimum;
				}
				if(ifZeroOrNegative && *param_uint == 0) {
					*param_uint = ifZeroOrNegative;
				}
				if(multiple) {
					*param_uint *= multiple;
				}
				if(!*param_uint && yesValue && yesno(value)) {
					*param_uint = yesValue;
				}
			}
			++ok;
		}
		if(param_int64) {
			*param_int64 = atoll(value);
			if(maximum && *param_int64 > (unsigned)maximum) {
				*param_int64 = maximum;
			}
			if(minimum && *param_int64 < (unsigned)minimum) {
				*param_int64 = minimum;
			}
			if(ifZeroOrNegative && *param_int64 <= 0) {
				*param_int64 = ifZeroOrNegative;
			}
			if(multiple) {
				*param_int64 *= multiple;
			}
			if(!*param_int64 && yesValue && yesno(value)) {
				*param_int64 = yesValue;
			}
			++ok;
		}
		if(param_uint64) {
			*param_uint64 = atoll(value);
			if(maximum && *param_uint64 > (unsigned)maximum) {
				*param_uint64 = maximum;
			}
			if(minimum && *param_uint64 < (unsigned)minimum) {
				*param_uint64 = minimum;
			}
			if(ifZeroOrNegative && *param_uint64 <= 0) {
				*param_uint64 = ifZeroOrNegative;
			}
			if(multiple) {
				*param_uint64 *= multiple;
			}
			if(!*param_uint64 && yesValue && yesno(value)) {
				*param_uint64 = yesValue;
			}
			++ok;
		}
	}
	return(ok > 0);
}
Пример #27
0
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        usage();
        exit(1);
    }

    long long row_id = atoll(argv[1]);
    if (row_id < 1)
    {
        fprintf(stderr, "No valid row_id: %s\n", argv[1]);
        usage();
        exit(1);
    }

    char *cmd = get_command(row_id);
    if (cmd == NULL)
    {
        fprintf(stderr, "Error: Could not read job command\n");
        exit(1);
    }

    pid_t childpid;

    if ((childpid = fork()) == -1)
    {
        perror("fork failed\n");
        exit(1);
    }

    if (childpid == 0)
    {
        //child
        char *args[] = {cmd, NULL};
        execv(args[0], args);
        return 0;			// dead code, just to avoid GCC warning
    }
    else
    {
        // parent
        drmaa2_save_pid(row_id, childpid);

        pid_t child;
        int status;
        child = waitpid(childpid, &status, 0);

        drmaa2_save_exit_status(row_id, status);

        if (WIFEXITED(status))
        {
            DRMAA2_DEBUG_PRINT("Process terminated normally by a call to _exit(2) or exit(3).\n");
            DRMAA2_DEBUG_PRINT("%d  - evaluates to the low-order 8 bits of the argument passed to _exit(2) or exit(3) by the child.\n", WEXITSTATUS(status));
        }
        if (WIFSIGNALED(status))
        {
            DRMAA2_DEBUG_PRINT("Process terminated due to receipt of a signal.\n");
            DRMAA2_DEBUG_PRINT("%d  - evaluates to the number of the signal that caused the termination of the process.\n", WTERMSIG(status));
            DRMAA2_DEBUG_PRINT("%d  - evaluates as true if the termination of the process was accompanied by the creation of a core \
	             file containing an image of the process when the signal was received.\n", WCOREDUMP(status));
        }
        if (WIFSTOPPED(status))
        {
            DRMAA2_DEBUG_PRINT("Process has not terminated, but has stopped and can be restarted.  This macro can be true only if the wait call \
	             specified the WUNTRACED option or if the child process is being traced (see ptrace(2)).\n");
            DRMAA2_DEBUG_PRINT("%d  - evaluates to the number of the signal that caused the process to stop.\n", WSTOPSIG(status));
        }
        return 0;
    }

}
Пример #28
0
BSONObj* BSONParser::parseBSON(const char* c, __int32& pos) throw(BSONException) {
    BSONObj* res = new BSONObj();
    __int32 state = 0; // 0 - nothing, 1 - name, 2- value
    __int32 lenBuffer = strlen(c);
    char* buffer = (char*)malloc(lenBuffer);
    char* name = NULL;
    void* value = NULL;
    __int32 len = 0;
    BSONTYPE type;
    __int32 stringOpen = 0; // 0 - closed
    // 1 - Single quote opened
    // 2 - Double quote opened
    __int32 x;
    for (x= pos; x < strlen(c); x++) {
        if (c[x] == '{') {
            if (state == 2) {
                value = parseBSON(c, x);
                type = BSON_TYPE;
            } else if (state == 0) {
                memset(buffer, 0, lenBuffer);
                state = 1;// name
                type = LONG64_TYPE;
            } else { // state == 1
                throw "json value is not allowed as name";
            }
            continue;
        }
        if (c[x] == '[') {
            value = parseArray(c, x);
            type = BSONARRAY_TYPE;
        }
        if (c[x] == '}' || c[x] == ',') {
            if (name != NULL) {
                if ((type != BSON_TYPE) && (type != BSONARRAY_TYPE)) {
                    value = (char*)malloc(len+1);
                    memset(value, 0, len + 1);
                    strcpy((char*)value, buffer);
                }
                len = 0;
                memset(buffer, 0, lenBuffer);
                switch (type) {
                case BOOL_TYPE: {
                    bool bVal = strcmp((char*)value, "true") == 0;
                    res->add(name, bVal);
                    break;
                }
                case INT_TYPE: {
                    __int32 iVal = atoi((char*)value);
                    res->add(name, iVal);
                    break;
                }
                case LONG_TYPE: {
                    __int64 lVal = atol((char*)value);
                    res->add(name, lVal);
                    break;
                }
                case LONG64_TYPE: {
#ifdef WINDOWS
                    __LONG64 lVal = _atoi64((char*)value);
#else
                    __LONG64 lVal = atoll((char*)value);
#endif
                    if (lVal <= INT_MAX) {
                        res->add(name, (__int32)lVal);
                    } else if (lVal <= LONG_MAX) {
                        res->add(name, (__int64)lVal);
                    } else {
                        res->add(name, lVal);
                    }
                    break;
                }
                case DOUBLE_TYPE: {
                    double dVal = atof((char*)value);
                    res->add(name, dVal);
                    break;
                }
                case STRING_TYPE:
                {
                    res->add(name, (char*)value);
                    break;
                }
                case BSON_TYPE:
                {
                    res->add(name, *((BSONObj*)value));
                    delete (BSONObj*)value;
                    break;
                }
                case BSONARRAY_TYPE:
                {
                    res->add(name, *((BSONArrayObj*)value));
                    delete (BSONArrayObj*)value;
                    break;
                }

                }
                free(name);
                name = NULL;
                if ((type != BSON_TYPE) && (type != BSONARRAY_TYPE)) {
                    free(value);
                    value = NULL;
                }
                if (c[x] != '}') {
                    state = 1; // name
                    type = LONG64_TYPE;
                    continue;
                }
            }
            if (c[x] == '}') {
                break;
            }
        }
        if (c[x] == ':') {
            name = (char*)malloc(len+1);
            memset(name, 0, len + 1);
            strcpy(name, buffer);
            len = 0;
            memset(buffer, 0, lenBuffer);
            state = 2; //value
            // default type
            type = LONG64_TYPE;
        } else {
            if (c[x] == '\'' || (c[x] == '\"')) {
                // Collect all the characters
                type = STRING_TYPE;
                char stringChar = c[x];
                bool escaped = false;
                x++;
                __int32 startPos = x;
                while ((x < strlen(c)) && ((c[x] != stringChar) || (escaped))) {
                    if (c[x] == '\\') {
                        escaped = true;
                    } else {
                        escaped = false;
                    }
                    buffer[len] = c[x];
                    len++;
                    x++;
                }
                if (x >= strlen(c)) {
                    char c[100];
                    sprintf(c, "An error ocurred parsing the bson. Error: unclosed string at %d",  startPos);

                    throw new BSONException(c);
                }
                continue;
            }

            if (c[x] == ' ' && stringOpen == 0) {
                continue;
            }
            if (c[x] == '\r' || c[x] == '\n') {
                continue;
            }
            if (c[x] == '.' && state == 2) {
                type = DOUBLE_TYPE;
            }
            buffer[len] = c[x];
            len++;
        }

    }
    pos = x;

    free(buffer);
    return res;
}
// scan each parm for OBJ_IR (injection request)
// and set it from the hr class then.
// use ptr_string/size_string stuff to point into the hr buf.
// but if we call serialize() then it makes news ones into its own blob.
// so we gotta know our first and last ptr_* pointers for serialize/deseria().
// kinda like how search input works
void setInjectionRequestFromParms ( TcpSocket *sock , 
				    HttpRequest *hr ,
				    CollectionRec *cr ,
				    InjectionRequest *ir ) {

	// just in case set all to zero
	memset ( ir , 0 , sizeof(InjectionRequest ));

	if ( ! cr ) {
		log("inject: no coll rec");
		return;
	}

	// use this, is more reliable, "coll" can disappear from under us
	ir->m_collnum = cr->m_collnum;

	// scan the parms
	for ( int i = 0 ; i < g_parms.m_numParms ; i++ ) {
		Parm *m = &g_parms.m_parms[i];
		if ( m->m_obj != OBJ_IR ) continue;
		// get it
		if ( m->m_type == TYPE_CHARPTR ||
		     m->m_type == TYPE_FILEUPLOADBUTTON ) {
			int32_t stringLen;
			char *str =hr->getString(m->m_cgi,&stringLen,m->m_def);
			// avoid overwriting the "url" parm with the "u" parm
			// since it is just an alias
			if ( ! str ) continue;
			// serialize it as a string
			char *foo = (char *)ir + m->m_off;
			char **ptrPtr = (char **)foo;
			// store the ptr pointing into hr buf for now
			*ptrPtr = str;
			// how many strings are we past ptr_url?
			int32_t count = ptrPtr - &ir->ptr_url;
			// and length. include \0
			int32_t *sizePtr = &ir->size_url + count;
			if ( str ) *sizePtr = stringLen + 1;
			else *sizePtr = 0;
			continue;
		}
		// numbers are easy
		else if ( m->m_type == TYPE_LONG ) {
			int32_t *ii = (int32_t *)((char *)ir + m->m_off);
			int32_t def = atoll(m->m_def);
			*ii = hr->getLong(m->m_cgi,def);
		}
		else if ( m->m_type == TYPE_CHECKBOX || 
			  m->m_type == TYPE_BOOL ) {
			char *ii = (char *)((char *)ir + m->m_off);
			int32_t def = atoll(m->m_def);
			*ii = (char)hr->getLong(m->m_cgi,def);
		}
		else if ( m->m_type == TYPE_IP ) {
			char *ii = (char *)((char *)ir + m->m_off);
			char *is = hr->getString(m->m_cgi,NULL);
			*(int32_t *)ii = 0; // default ip to 0
			// otherwise, set the ip
			if ( is ) *(int32_t *)ii = atoip(is);
		}
		// if unsupported let developer know
		else { char *xx=NULL;*xx=0; }
	}


	// if content is "" make it NULL so XmlDoc will download it
	// if user really wants empty content they can put a space in there
	// TODO: update help then...
	if ( ir->ptr_content && ! ir->ptr_content[0]  )
		ir->ptr_content = NULL;

	if ( ir->ptr_contentFile && ! ir->ptr_contentFile[0]  )
		ir->ptr_contentFile = NULL;

	if ( ir->ptr_contentDelim && ! ir->ptr_contentDelim[0] )
		ir->ptr_contentDelim = NULL;

	if ( ir->ptr_queryToScrape && ! ir->ptr_queryToScrape[0] ) 
		ir->ptr_queryToScrape = NULL;

	if ( ir->ptr_url && ! ir->ptr_url[0] ) 
		ir->ptr_url = NULL;

	// if we had a delimeter but not content, zero it out...
	if ( ! ir->ptr_content && ! ir->ptr_contentFile ) 
		ir->ptr_contentDelim = NULL;
}
Пример #30
0
int main(int argc, char* argv[])
{
	bool debug = false;
	uint32 dropLastNchains = 0;
	uint32 dropHighSPcount = 0;
	int sptl = 0;

	if (argc < 2)
	{
		usage();
		return 0;
	}
	
	std::vector<std::string> pathNames;

	// Parse command line args
	int i;
	for( i = 1; i < argc; i++ )
	{
		if ( strncmp( argv[i], "-v", 2 ) == 0 )
			debug = true;
		else if ( strncmp( argv[i], "-drop_last_n_chains=", 20 ) == 0 )
		{
			uint32 j;

			for ( j = 20; argv[i][j] >= '0' && argv[i][j] <= '9'; j++ )
			{
				dropLastNchains *= 10;
				dropLastNchains += ((int) argv[i][j] ) - 0x30;
			}

			if ( argv[i][j] != '\0' )
			{
				printf("Error: Invalid drop_last_n_chains number.\n\n");
				usage();
				exit( 1 );
			}
		}
		else if ( strncmp( argv[i], "-drop_high_sp_n_chains=", 23 ) == 0 )
		{
			uint32 j;

			for ( j = 23; argv[i][j] >= '0' && argv[i][j] <= '9'; j++ )
			{
				dropHighSPcount *= 10;
				dropHighSPcount += ((int) argv[i][j] ) - 0x30;
			}

			if ( argv[i][j] != '\0' )
			{
				printf("Error: Invalid drop_high_sp_n_chains number.\n\n");
				usage();
				exit( 1 );
			}
		}
		else if ( strncmp( argv[i], "-sptl=", 6 ) == 0 )
		{
			uint32 j;

			for ( j = 6; argv[i][j] >= '0' && argv[i][j] <= '9'; j++ )
			{
				sptl *= 10;
				sptl += ((int) argv[i][j] ) - 0x30;
			}

			if ( argv[i][j] != '\0' )
			{
				printf("Error: Invalid sptl number.\n\n");
				usage();
				exit( 1 );
			}
		}
		else
			GetTableList( argv[i], pathNames );
	}

	if ( debug )
	{
		for( int i = 0; i < argc; i++ )
			printf("%i: %s\n", i, argv[i]);
	}

	if ( pathNames.size() == 0 )
	{
		printf("no rainbow table found\n");
		return 0;
	}

	std::string resultFile, sType;

	for ( uint32 i = 0; i < pathNames.size(); i++ )
	{
		if( pathNames[i].substr( pathNames[i].length() - 4, pathNames[i].length()) == "rti2")
		{
			resultFile = pathNames[i].substr(0, pathNames[i].length() - 2); // Resulting file is .rt, not .rti2
			sType = "RTI2";
		}
		else if( pathNames[i].substr( pathNames[i].length() - 3, pathNames[i].length()) == "rti")
		{
			resultFile = pathNames[i].substr( 0, pathNames[i].length() - 1 ); // Resulting file is .rt, not .rti
			sType = "RTI";
		}
		else 
		{
			printf("File %s is not a RTI or a RTI2 file", pathNames[i].c_str() );
			continue;
		}
		
		// XXX this assumes someone is converting either just the last file
		// *or* doing a whole set which will read the last file first
		if ( dropLastNchains > 0 && i == 0 )
		{
			std::string::size_type lastX = resultFile.find_last_of('x');

			if ( lastX == std::string::npos )
			{
				std::cout << "Could not parse the filename to drop the last chains"
					<< std::endl;
				exit( -1 );
			}
			
			std::string::size_type firstSplit
				= resultFile.find_first_of('_',lastX);

			#if defined(_WIN32) && !defined(__GNUC__)
				uint64 chains = _atoi64( resultFile.substr( lastX + 1, firstSplit - lastX - 1).c_str() );
			#else
				uint64 chains = atoll( resultFile.substr( lastX + 1, firstSplit - lastX - 1 ).c_str() );
			#endif

			chains -= dropLastNchains;

			resultFile.replace( lastX + 1, firstSplit - lastX - 1, uint64tostr( chains ) );
		}

		ConvertRainbowTable( pathNames[i], resultFile, sType, debug, dropLastNchains, dropHighSPcount, sptl );
		dropLastNchains = 0;
		dropHighSPcount = 0;

		printf("\n");
	}
	return 0;
}