Exemple #1
0
Command *cmd_dequeue(Commands cmds) {
    if ( cmds.cmd_start == (void *)0 ) {
        return (void *)0;
    }
    
    // ignore completed commands
    Command *c = cmds.cmd_start;
    Command *p = (void *)0;
    while ( (*c).complete == 1 ) {
        p = c;
        c = (*c).next;
        if ( c == (void *)0 ) {
            return (void *)0;
        }
    }

    (*c).complete = 1;
    
    // remove from queue if not persistent
    if ( p == (void *)0 && !(*c).persist ) {
        cmds.cmd_start = (*cmds.cmd_start).next;
    } else if ( !(*c).persist ) {
        (*p).next = (*c).next;
        //add to finished list
        Command *f = cmd_append(c,cmds.cmd_completed);
        if ( f != (void *)0 ) {
            cmds.fin_len++;
        }
        cmds.queue_len--;
    }
    return c;
}
Exemple #2
0
/**
 * Change a file. Or deleted/truncated/append/created.
 * - The file must exist.
 */
void cmd_change(const char* path, int size)
{
	struct stat st;

	if (!size)
		return;

	if (lstat(path, &st) != 0) {
		/* LCOV_EXCL_START */
		log_fatal("Error accessing %s\n", path);
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	if (S_ISLNK(st.st_mode)) {
		/* symlink */
		if (rnd(2) == 0) {
			/* delete */
			if (remove(path) != 0) {
				/* LCOV_EXCL_START */
				log_fatal("Error removing %s\n", path);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
		} else {
			/* recreate */
			char linkto[PATH_MAX];

			if (remove(path) != 0) {
				/* LCOV_EXCL_START */
				log_fatal("Error removing %s\n", path);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}

			rnd_name(linkto);

			cmd_generate_symlink(path, linkto);
		}
	} else if (S_ISREG(st.st_mode)) {
		int r;

		r = rnd(4);

		if (r == 0) {
			cmd_write(path, size);
		} else if (r == 1) {
			cmd_append(path, size);
		} else if (r == 2) {
			cmd_truncate(path, size);
		} else {
			cmd_delete(path);
		}
	}
}
Exemple #3
0
int *cmd_queue(Command *cmd, Commands cmds){
    if ( cmds.cmd_start == (void *)0 ) {
        cmds.cmd_start = cmd;
        cmds.queue_len++;
        return 0;
    }
    Command *c = cmd_append(cmd,cmds.cmd_start);
    if ( c != (void *)0 ) {
        cmds.queue_len++;
    }
    return 0;
}
Exemple #4
0
void CLIState::execute_active_line()
{
	if (m_line.empty())
		return;
	const char *p = m_line.c_str();
	con_printf(CON_NORMAL, "con%c%s", g_prompt_strings[0], p);
	cmd_append(p);
	m_lines[0] = move(m_line);
	m_lines.emplace_front();
	m_history_position = 0;
	if (m_lines.size() > m_maximum_history_lines)
		m_lines.pop_back();
	clear_active_line();
}
int main(void) {
	int isRunning = 1;
	char buffer[MAX_LINE_LENGTH];
	char command[MAX_LINE_LENGTH];
	while(isRunning){	
		//int i;
		printf("> ");
		fgets(buffer, MAX_LINE_LENGTH, stdin);
		/*for( i = 0; i < MAX_LINE_LENGTH; i++ ){
		  buffer[i] = tolower(buffer[i]);
		}*/
		sscanf(buffer, " %s ", (char*)&command);
		//Switch for commands
		if(strcmp(command, "BYE") == 0){					/*BYE*/
			deleteAllEntry(entry_head);
			deleteAllSnapshot(snapshot_head);
			printf("bye");
			isRunning = 0;
		}else if(strcmp(command, "HELP") == 0){				/*HELP*/
			cmd_help();
		}else if(strcmp(command, "LIST") == 0){
			sscanf(buffer + strlen(command), "%s", (char*)&command);
			if(strcmp(command, "KEYS") == 0){				/*LIST KEYS*/
				cmd_list_keys();
			}else if(strcmp(command, "ENTRIES") == 0){		/*LIST ENTRIES*/
				cmd_list_entries();
			}else if(strcmp(command, "SNAPSHOTS") == 0){	/*LIST SNAPSHOTS*/
				cmd_list_snapshots();
			}
		}else if(strcmp(command, "GET") == 0){				/*GET*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_get(key);
		}else if(strcmp(command, "DEL") == 0){				/*DEL*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_del(key);
		}else if(strcmp(command, "PURGE") == 0){			/*PURGE*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_purge(key);
		}else if(strcmp(command, "SET") == 0){				/*SET*/
			int i = 0;
			char key[MAX_KEY_LENGTH];
			char *endptr;
			int *values = (int*)malloc(MAX_LINE_LENGTH/2*sizeof(int));
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			endptr = buffer + strlen(command) + strlen(key) + 2;
			while(1){
				values[i] = strtol(endptr, &endptr, 10);
				if(*endptr == '\n'){
					cmd_set(key, values, i+1);
					break;
				}
				i++;
			}
			free(values);
		}else if(strcmp(command, "PUSH") == 0){				/*PUSH*/
			int i = 0;
			char key[MAX_KEY_LENGTH];
			char *endptr;
			int *values = (int*)malloc(MAX_LINE_LENGTH/2*sizeof(int));
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			endptr = buffer + strlen(command) + strlen(key) + 2;
			while(1){
				values[i] = strtol(endptr, &endptr, 10);
				if(*endptr == '\n'){
					cmd_push(key, values, i+1);
					break;
				}
				i++;
			}
			free(values);
		}else if(strcmp(command, "APPEND") == 0){			/*APPEND*/ 
			int i = 0;
			char key[MAX_KEY_LENGTH];
			char *endptr;
			int *values = (int*)malloc(MAX_LINE_LENGTH/2*sizeof(int));
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			endptr = buffer + strlen(command) + strlen(key) + 2;
			while(1){
				values[i] = strtol(endptr, &endptr, 10);
				if(*endptr == '\n'){
					cmd_append(key, values, i+1);
					break;
				}
				i++;
			}
			free(values);
		}else if(strcmp(command, "PICK") == 0){				/*PICK*/
			char key[MAX_KEY_LENGTH];
			int index;
			sscanf(buffer + strlen(command)+1, "%s", (char*)&key);
			index = strtol(buffer + strlen(command) + strlen(key)+2, (char**)&command, 0);
			cmd_pick(key, index);
		}else if(strcmp(command, "PLUCK") == 0){			/*PLUCK*/
			char key[MAX_KEY_LENGTH];
			int index;
			sscanf(buffer + strlen(command)+1, "%s", (char*)&key);
			index = strtol(buffer + strlen(command) + strlen(key)+2, (char**)&command, 0);
			cmd_pluck(key, index);
		}else if(strcmp(command, "POP") == 0){				/*POP*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_pop(key);
		}else if(strcmp(command, "DROP") == 0){				/*DROP*/
			int id = strtol(buffer + strlen(command) +1 , (char**)&command, 10);
			cmd_drop(id);
		}else if(strcmp(command, "ROLLBACK") == 0){			/*ROLLBACK*/
			int id = strtol(buffer + strlen(command) +1 , (char**)&command, 10);
			cmd_rollback(id);
		}else if(strcmp(command, "CHECKOUT") == 0){			/*CHECKOUT*/
			int id = strtol(buffer + strlen(command) +1 , (char**)&command, 10);
			cmd_checkout(id);
		}else if(strcmp(command, "SNAPSHOT") == 0){			/*SNAPSHOT*/
			cmd_snapshot();
		}else if(strcmp(command, "MIN") == 0){				/*MIN*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_min(key);
		}else if(strcmp(command, "MAX") == 0){				/*MAX*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_max(key);
		}else if(strcmp(command, "SUM") == 0){				/*SUM*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_sum(key);
		}else if(strcmp(command, "LEN") == 0){				/*LEN*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_len(key);
		}else if(strcmp(command, "REV") == 0){				/*REV*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_rev(key);
		}else if(strcmp(command, "UNIQ") == 0){				/*UNIQ*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_uniq(key);
		}else if(strcmp(command, "SORT") == 0){				/*SORT*/
			char key[MAX_KEY_LENGTH];
			sscanf(buffer + strlen(command), "%s", (char*)&key);
			cmd_sort(key);
		}else{
			printf("\n");
		}
	}
	return 0;
}
Exemple #6
0
int main(int argc, char* argv[])
{
	int i, j, b;

	lock_init();

	if (argc < 2) {
		help();
		exit(EXIT_SUCCESS);
	}

	if (strcmp(argv[1], "generate") == 0) {
		int disk, file, size;

		if (argc != 6) {
			/* LCOV_EXCL_START */
			help();
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}

		seed = atoi(argv[2]);
		disk = atoi(argv[3]);
		file = atoi(argv[4]);
		size = atoi(argv[5]);

		for (i = 0; i < disk; ++i) {
			for (j = 0; j < file; ++j) {
				if (j == 0)
					/* create at least a big one */
					cmd_generate(i + 1, size);
				else if (j == 1)
					/* create at least an empty one */
					cmd_generate(i + 1, 0);
				else
					cmd_generate(i + 1, rnd(size));
			}
		}
	} else if (strcmp(argv[1], "write") == 0) {
		int fail, size;

		if (argc < 6) {
			/* LCOV_EXCL_START */
			help();
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}

		seed = atoi(argv[2]);
		fail = atoi(argv[3]);
		size = atoi(argv[4]);
		b = 5;

		/* sort the file names */
		qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);

		for (i = b; i < argc; ++i)
			for (j = 0; j < fail; ++j)
				cmd_write(argv[i], rndnz(size));
	} else if (strcmp(argv[1], "damage") == 0) {
		int fail, size;

		if (argc < 6) {
			/* LCOV_EXCL_START */
			help();
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}

		seed = atoi(argv[2]);
		fail = atoi(argv[3]);
		size = atoi(argv[4]);
		b = 5;

		/* sort the file names */
		qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);

		for (i = b; i < argc; ++i)
			for (j = 0; j < fail; ++j)
				cmd_damage(argv[i], rndnz(size)); /* at least one byte */
	} else if (strcmp(argv[1], "append") == 0) {
		int size;

		if (argc < 5) {
			/* LCOV_EXCL_START */
			help();
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}

		seed = atoi(argv[2]);
		size = atoi(argv[3]);
		b = 4;

		/* sort the file names */
		qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);

		for (i = b; i < argc; ++i)
			cmd_append(argv[i], rndnz(size)); /* at least one byte */
	} else if (strcmp(argv[1], "truncate") == 0) {
		int size;

		if (argc < 5) {
			/* LCOV_EXCL_START */
			help();
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}

		seed = atoi(argv[2]);
		size = atoi(argv[3]);
		b = 4;

		/* sort the file names */
		qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);

		for (i = b; i < argc; ++i)
			cmd_truncate(argv[i], rnd(size));
	} else if (strcmp(argv[1], "change") == 0) {
		int size;

		if (argc < 5) {
			/* LCOV_EXCL_START */
			help();
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}

		seed = atoi(argv[2]);
		size = atoi(argv[3]);
		b = 4;

		/* sort the file names */
		qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);

		for (i = b; i < argc; ++i)
			cmd_change(argv[i], rnd(size));
	} else {
		/* LCOV_EXCL_START */
		help();
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	lock_done();

	return 0;
}
static int start_process(Channel * c, char ** envp, char * dir, char * exe, char ** args, int attach,
                int * pid, int * selfattach, ChildProcess ** prs) {
    typedef struct _SYSTEM_HANDLE_INFORMATION {
        ULONG Count;
        struct HANDLE_INFORMATION {
            USHORT ProcessId;
            USHORT CreatorBackTraceIndex;
            UCHAR ObjectTypeNumber;
            UCHAR Flags;
            USHORT Handle;
            PVOID Object;
            ACCESS_MASK GrantedAccess;
        } Handles[1];
    } SYSTEM_HANDLE_INFORMATION;
    FARPROC QuerySystemInformationProc = GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtQuerySystemInformation");
    DWORD size;
    NTSTATUS status;
    SYSTEM_HANDLE_INFORMATION * hi = NULL;
    int fpipes[3][2];
    HANDLE hpipes[3][2];
    char * cmd = NULL;
    int err = 0;
    int i;

    if (args != NULL) {
        int i = 0;
        int cmd_size = 0;
        int cmd_pos = 0;
#           define cmd_append(ch) { \
            if (!cmd) { \
                cmd_size = 0x1000; \
                cmd = (char *)loc_alloc(cmd_size); \
            } \
            else if (cmd_pos >= cmd_size) { \
                char * tmp = (char *)loc_alloc(cmd_size * 2); \
                memcpy(tmp, cmd, cmd_pos); \
                loc_free(cmd); \
                cmd = tmp; \
                cmd_size *= 2; \
            }; \
            cmd[cmd_pos++] = (ch); \
        }
        while (args[i] != NULL) {
            char * p = args[i++];
            if (cmd_pos > 0) cmd_append(' ');
            cmd_append('"');
            while (*p) {
                if (*p == '"') cmd_append('\\');
                cmd_append(*p);
                p++;
            }
            cmd_append('"');
        }
        cmd_append(0);
#       undef cmd_append
    }

    size = sizeof(SYSTEM_HANDLE_INFORMATION) * 16;
    hi = loc_alloc(size);
    for (;;) {
        status = QuerySystemInformationProc(SystemHandleInformation, hi, size, &size);
        if (status != STATUS_INFO_LENGTH_MISMATCH) break;
        hi = loc_realloc(hi, size);
    }
    if (status == 0) {
        ULONG i;
        DWORD id = GetCurrentProcessId();
        for (i = 0; i < hi->Count; i++) {
            if (hi->Handles[i].ProcessId != id) continue;
            SetHandleInformation((HANDLE)(int)hi->Handles[i].Handle, HANDLE_FLAG_INHERIT, FALSE);
        }
    }
    else {
        err = set_win32_errno(status);
        trace(LOG_ALWAYS, "Can't start process '%s': %s", exe, errno_to_str(err));
    }
    loc_free(hi);

    memset(hpipes, 0, sizeof(hpipes));
    for (i = 0; i < 3; i++) fpipes[i][0] = fpipes[i][1] = -1;
    if (!err) {
#if defined(__CYGWIN__)
        for (i = 0; i < 3; i++) {
            if (pipe(fpipes[i]) < 0) {
                err = errno;
                break;
            }
            hpipes[i][0] = (HANDLE)get_osfhandle(fpipes[i][0]);
            hpipes[i][1] = (HANDLE)get_osfhandle(fpipes[i][1]);
        }
#else
        for (i = 0; i < 3; i++) {
            if (!CreatePipe(&hpipes[i][0], &hpipes[i][1], NULL, PIPE_SIZE)) {
                err = set_win32_errno(GetLastError());
                break;
            }
            fpipes[i][0] = _open_osfhandle((intptr_t)hpipes[i][0], O_TEXT);
            fpipes[i][1] = _open_osfhandle((intptr_t)hpipes[i][1], O_TEXT);
        }
#endif
    }
    if (!err) {
        STARTUPINFO si;
        PROCESS_INFORMATION prs_info;
        SetHandleInformation(hpipes[0][0], HANDLE_FLAG_INHERIT, TRUE);
        SetHandleInformation(hpipes[1][1], HANDLE_FLAG_INHERIT, TRUE);
        SetHandleInformation(hpipes[2][1], HANDLE_FLAG_INHERIT, TRUE);
        memset(&si, 0, sizeof(si));
        memset(&prs_info, 0, sizeof(prs_info));
        si.cb = sizeof(si);
        si.dwFlags |= STARTF_USESTDHANDLES;
        si.hStdInput  = hpipes[0][0];
        si.hStdOutput = hpipes[1][1];
        si.hStdError  = hpipes[2][1];
        if (CreateProcess(exe, cmd, NULL, NULL, TRUE, (attach ? CREATE_SUSPENDED : 0),
                (envp ? envp[0] : NULL), (dir[0] ? dir : NULL), &si, &prs_info) == 0)
        {
            err = set_win32_errno(GetLastError());
        }
        else {
            *pid = prs_info.dwProcessId;
            CloseHandle(prs_info.hThread);
            CloseHandle(prs_info.hProcess);
        }
    }
    close(fpipes[0][0]);
    close(fpipes[1][1]);
    close(fpipes[2][1]);
    if (!err) {
        *prs = loc_alloc_zero(sizeof(ChildProcess));
        (*prs)->inp = fpipes[0][1];
        (*prs)->out = fpipes[1][0];
        (*prs)->err = fpipes[2][0];
        (*prs)->pid = *pid;
        (*prs)->bcg = c->bcg;
        list_add_first(&(*prs)->link, &prs_list);
    }
    else {
        close(fpipes[0][1]);
        close(fpipes[1][0]);
        close(fpipes[2][0]);
    }
    loc_free(cmd);
    if (!err) return 0;
    trace(LOG_ALWAYS, "Can't start process '%s': %s", exe, errno_to_str(err));
    errno = err;
    return -1;
}