Пример #1
0
void run_shell(void)
{
    char input[BUFFER_SIZE];

    while (true) {
        if ( !get_input_line(input, BUFFER_SIZE) ) {
            putchar('\n');
            break;
        }

        if ( !strcmp(input, "exit") ) {
            break;
        }

        TokenList tlist = parse_input(input);
        CmdCmpList clist = cmdline_create(tlist);
        token_list_destroy(tlist);

        if ( !cmdline_is_valid(clist) ) {
            printf("pgshell: syntax error\n");
        }
        else {
            run_foreground_command(clist);
        }

        cmdline_destroy(clist);
    }

    printf("Goodbye!\n");
}
Пример #2
0
static int run_foreground_command(CmdCmpList list)
{
    Command cmd = cmdline_component_at_index(list, 0)->content.cmd;
    
    if ( !execute_builtin(cmd) ) {
        const pid_t pid = xfork();

        if ( pid == 0 ) {
            char ** args = command_raw_args(cmd);
            execvp(args[0], args);

            switch ( errno ) {
                case EACCES:
                    fprintf(stderr, "pgshell: %s: Permission denied\n",
                            args[0]);
                    break;

                case ENOENT:
                    fprintf(stderr, "pgshell: %s: No such file or directory\n",
                            args[0]);
                    break;

                default:
                    fprintf(stderr, "pgshell: other exec() error\n");
                    break;
            }

            cmdline_destroy(list);
            exit(EXIT_FAILURE);
        }

        int status;
        waitpid(pid, &status, 0);
        return status;
    }

    return 0;
}
Пример #3
0
int main(int argc, char *argv[])
{
	transfer * st_sender =NULL;
	int chk_interval = 0, attach = 0, cap_num = 0;
	char *dst_ip = NULL;
	unsigned short dst_port = 0;
	int opt_idx = 0, opt_id = 0, arg_num = 0;
	void *arglist = NULL;
	pkt_cap_ctx_p p_cap_ctx = NULL;
	st_ctx_p p_st_ctx = NULL;
	cpu_set_t set;

	if (argc < 2) return 1;

	if (0 != cmdline_init()) {
		fprintf(stderr, "cmdline_init failed!\n");
		return 1;
	}

	if (0 != cmdline_parse(argc-1, &argv[1], opt_prof)) {
		fprintf(stderr, "cmdline_parse failed!\n");
		return 1;
	}

	while (-1 != cmdline_result_loop(&opt_idx, &opt_id, &arglist, &arg_num)) {
		switch (opt_id) {
			case OPT_HELP:
				return usage();

				break;
			case OPT_DEST:
				dst_ip = strdup(cmdline_get_arg(arglist, 0));
				dst_port = atoi(cmdline_get_arg(arglist, 1));

				break;
			case OPT_INTERVAL:
				chk_interval = atoi(cmdline_get_arg(arglist, 0));

				break;
			case OPT_CAP_NUM:
				cap_num = atoi(cmdline_get_arg(arglist, 0));

				break;
			case OPT_ATTACH:
				attach = atoi(cmdline_get_arg(arglist, 0));

				break;
			default:
				break;
		}
	}

	cmdline_show();
	cmdline_destroy();

	st_sender = new transfer(IS_CLIENT, SOCK_DGRAM, dst_ip, dst_port);
	if (!st_sender || true != st_sender->is_ok()) {
		return -1;
	}

	p_cap_ctx = pkt_cap_ctx_new(cap_num);
	MEM_ALLOC(p_st_ctx, st_ctx_p, sizeof(st_ctx_t), 1);

	int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
	fprintf(stdout, "cpu num = %d\n", cpu_num);
	p_st_ctx->num = cap_num;
	p_st_ctx->chk_interval = chk_interval;
	p_st_ctx->p_cap_ctx = p_cap_ctx;
	p_st_ctx->sender = st_sender;
	if (attach) p_st_ctx->id = cap_num > cpu_num ? (cpu_num-1) : cap_num;
	else p_st_ctx->id = -1;

	pthread_t st_thd;
	if (0 != pthread_create(&st_thd, NULL, st_cb, (void*)p_st_ctx)) {
		fprintf(stderr, "create st_cb failed!\n");
		return 1;
	}

	pthread_t cap_thd;
	for (int i = 0; i < cap_num; i++) {
		if (attach) p_cap_ctx[i].id = cap_num > cpu_num ? (i%(cpu_num-1)) : i;
		else p_cap_ctx[i].id = -1;
		if (0 != pthread_create(&cap_thd, NULL, pkt_cap_cb, &p_cap_ctx[i])) {
			fprintf(stderr, "create pkt_cap_cb failed!\n");
			return 1;
		}
	}

	void *ret = NULL;
	pthread_join(st_thd, &ret);

	return 0;
}