Example #1
0
int chirp_sqlite3_column_jsonify(sqlite3_stmt *stmt, int n, buffer_t *B)
{
	int rc;

	switch (sqlite3_column_type(stmt, n)) {
		case SQLITE_NULL:
			CATCHUNIX(buffer_putliteral(B, "null"));
			break;
		case SQLITE_INTEGER:
			CATCHUNIX(buffer_putfstring(B, "%" PRId64, (int64_t) sqlite3_column_int64(stmt, n)));
			break;
		case SQLITE_FLOAT:
			CATCHUNIX(buffer_putfstring(B, "%.*e", DBL_DIG, sqlite3_column_double(stmt, n)));
			break;
		case SQLITE_TEXT:
			CATCHUNIX(buffer_putliteral(B, "\""));
			CATCHUNIX(jsonA_escapestring(B, (const char *)sqlite3_column_text(stmt, n)));
			CATCHUNIX(buffer_putliteral(B, "\""));
			break;
		default:
			abort();
	}

	rc = 0;
	goto out;
out:
	return rc;
}
Example #2
0
int jsonA_escapestring(buffer_t *B, const char *str)
{
	for (; *str; str++) {\
		switch (*str) {\
			case '/':
				if (buffer_putliteral(B, "\\/") == -1) return -1;
				break;
			case '\\':
				if (buffer_putliteral(B, "\\\\") == -1) return -1;
				break;
			case '\"':
				if (buffer_putliteral(B, "\\\"") == -1) return -1;
				break;
			case '\b':
				if (buffer_putliteral(B, "\\b") == -1) return -1;
				break;
			case '\f':
				if (buffer_putliteral(B, "\\f") == -1) return -1;
				break;
			case '\n':
				if (buffer_putliteral(B, "\\n") == -1) return -1;
				break;
			case '\r':
				if (buffer_putliteral(B, "\\r") == -1) return -1;
				break;
			case '\t':
				if (buffer_putliteral(B, "\\t") == -1) return -1;
				break;
			default:
				if (buffer_putfstring(B, "%c", (int)*str) == -1) return -1;
				break;
		}
	}
	return 0;
}
Example #3
0
int tracer_args_set( struct tracer *t, INT64_T syscall, const INT64_T *args, int nargs )
{
	if(!t->gotregs) {
		if(ptrace(PTRACE_GETREGS,t->pid,0,&t->regs) == -1)
			ERROR;
		t->gotregs = 1;
	}

#if 0 /* Enable this for extreme debugging... */
	{
		int i;
		buffer_t B;
		buffer_init(&B);
		buffer_putfstring(&B, "SET args[%zu] = {", (size_t)nargs);
		for (i = 0; i < nargs; i++)
			buffer_putfstring(&B, "%" PRId64 ", ", args[i]);
		buffer_putliteral(&B, "}");
		debug(D_DEBUG, "%s", buffer_tostring(&B, NULL));
		buffer_free(&B);
	}
#endif

#ifdef CCTOOLS_CPU_I386
	t->regs.regs32.orig_eax = syscall;
	if(nargs>=1) t->regs.regs32.ebx = args[0];
	if(nargs>=2) t->regs.regs32.ecx = args[1];
	if(nargs>=3) t->regs.regs32.edx = args[2];
	if(nargs>=4) t->regs.regs32.esi = args[3];
	if(nargs>=5) t->regs.regs32.edi = args[4];
	if(nargs>=6) t->regs.regs32.ebp = args[5];
#else
	if(tracer_is_64bit(t)) {
		t->regs.regs64.orig_rax = syscall;
		if(nargs>=1) t->regs.regs64.rdi = args[0];
		if(nargs>=2) t->regs.regs64.rsi = args[1];
		if(nargs>=3) t->regs.regs64.rdx = args[2];
		if(nargs>=4) t->regs.regs64.r10 = args[3];
		if(nargs>=5) t->regs.regs64.r8  = args[4];
		if(nargs>=6) t->regs.regs64.r9  = args[5];
	} else {
		t->regs.regs64.orig_rax = syscall;
		if(nargs>=1) t->regs.regs64.rbx = args[0];
		if(nargs>=2) t->regs.regs64.rcx = args[1];
		if(nargs>=3) t->regs.regs64.rdx = args[2];
		if(nargs>=4) t->regs.regs64.rsi = args[3];
		if(nargs>=5) t->regs.regs64.rdi  = args[4];
		if(nargs>=6) {
			if (t->has_args5_bug) t->regs.regs64.r9 = args[5];
			else                  t->regs.regs64.rbp = args[5];
		}
	}
#endif

	t->setregs = 1;

	return 1;
}
Example #4
0
void chirp_stats_summary(buffer_t *B)
{
	char *addr;
	struct chirp_stats *s;

	if(!stats_table)
		stats_table = hash_table_create(0, 0);

	buffer_putfstring(B, "bytes_written %" PRIu64 "\n", total_bytes_written);
	buffer_putfstring(B, "bytes_read %" PRIu64 "\n", total_bytes_read);
	buffer_putfstring(B, "total_ops %" PRIu64 "\n", total_ops);

	buffer_putliteral(B, "clients ");
	hash_table_firstkey(stats_table);
	while(hash_table_nextkey(stats_table, &addr, (void **) &s)) {
		buffer_putfstring(B, "%s,1,1,%" PRIu64 ",%" PRIu64 ",%" PRIu64 "; ", s->addr, s->ops, s->bytes_read, s->bytes_written);
	}
	buffer_putliteral(B, "\n");
}
Example #5
0
int chirp_sqlite3_row_jsonify(sqlite3_stmt *stmt, buffer_t *B)
{
	int rc;
	int i, first;

	CATCHUNIX(buffer_putliteral(B, "{"));
	for (i = 0, first = 1; i < sqlite3_column_count(stmt); i++, first = 0) {
		if (!first)
			CATCHUNIX(buffer_putliteral(B, ","));
		CATCHUNIX(buffer_putfstring(B, "\"%s\":", sqlite3_column_name(stmt, i)));
		chirp_sqlite3_column_jsonify(stmt, i, B);
	}
	CATCHUNIX(buffer_putliteral(B, "}"));

	rc = 0;
	goto out;
out:
	return rc;
}
Example #6
0
/*
 * Based on opengroup.org's definition of the Shell Command Language (also gnu's)
 * In section 2.2.3 on Double-Quoted Strings, it indicates you only need to
 * escape dollar sign, backtick, and backslash. I also escape double quote as
 * we are adding and exterior double quote around the string.
 *
 * [ $ \ ` " ] Are always escaped.
 * */
char *string_escape_shell( const char *str )
{
	buffer_t B[1];
	buffer_init(B);
	buffer_abortonfailure(B, 1);

	const char *s;
	buffer_putliteral(B,"\"");
	for(s=str;*s;s++) {
		if(*s=='"' || *s=='\\' || *s=='$' || *s=='`')
			buffer_putliteral(B,"\\");
		buffer_putlstring(B,s,1);
	}
	buffer_putliteral(B,"\"");

	char *result;
	buffer_dup(B,&result);
	buffer_free(B);

	return result;
}
Example #7
0
static void do_debug(INT64_T flags, const char *fmt, va_list args)
{
	buffer_t B;
	char ubuf[1<<16];

	buffer_init(&B);
	buffer_ubuf(&B, ubuf, sizeof(ubuf));
	buffer_max(&B, sizeof(ubuf));

	if (debug_write == debug_file_write || debug_write == debug_stderr_write || debug_write == debug_stdout_write) {
		struct timeval tv;
		struct tm *tm;
		gettimeofday(&tv, 0);
		tm = localtime(&tv.tv_sec);

		buffer_putfstring(&B, "%04d/%02d/%02d %02d:%02d:%02d.%02ld ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (long) tv.tv_usec / 10000);
		buffer_putfstring(&B, "%s[%d] ", debug_program_name, getpid());
	}
	/* Parrot prints debug messages for children: */
	if (getpid() != debug_getpid()) {
		buffer_putfstring(&B, "<child:%d> ", (int)debug_getpid());
	}
	buffer_putfstring(&B, "%s: ", debug_flags_to_name(flags));

	buffer_putvfstring(&B, fmt, args);
	while(isspace(buffer_tostring(&B)[buffer_pos(&B)-1]))
		buffer_rewind(&B, buffer_pos(&B)-1); /* chomp whitespace */
	buffer_putliteral(&B, "\n");

	debug_write(flags, buffer_tostring(&B));

	if(terminal_available && (flags & (D_ERROR | D_NOTICE | D_FATAL))) {
		if(debug_write != debug_stderr_write || !isatty(STDERR_FILENO)) {
			if(!terminal_f) {
				if((terminal_f = fopen(terminal_path, "a")) == NULL) {
					/* print to wherever stderr is pointing that we could not open the terminal. */
					terminal_available = 0;
				}
			}
		}

		if(terminal_f)
			fprintf(terminal_f, "%s", buffer_tostring(&B));
	}

	buffer_free(&B);
}
Example #8
0
static int find (buffer_t *B, const size_t base, buffer_t *path, const char *pattern, int recursive)
{
	int rc = 0;
	DIR *D = opendir(buffer_tostring(path));
	if (D) {
		struct dirent *entry;
		size_t current = buffer_pos(path);
		while ((entry = readdir(D))) {
			struct stat buf;

			if (buffer_putstring(path, entry->d_name) == -1) goto failure;
			/* N.B. We don't use FNM_PATHNAME, so `*.c' matches `foo/bar.c' */
			if (fnmatch(pattern, buffer_tostring(path)+base, 0) == 0) {
				if (buffer_printf(B, "%s%c", buffer_tostring(path), 0) == -1) goto failure; /* NUL padded */
				rc += 1;
			}
			if (recursive && strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..") && stat(buffer_tostring(path), &buf) == 0 && S_ISDIR(buf.st_mode)) {
				if (buffer_putliteral(path, "/") == -1) goto failure;
				int found = find(B, base, path, pattern, recursive);
				if (found == -1)
					goto failure;
				else if (found > 0)
					rc += found;
			}
			buffer_rewind(path, current);
		}
	} /* else skip */
	goto out;
failure:
	rc = -1;
	goto out;
out:
	if (D)
		closedir(D);
	return rc;
}
Example #9
0
struct link *http_query_size_via_proxy(const char *proxy, const char *urlin, const char *action, INT64_T * size, time_t stoptime, int cache_reload)
{
	char url[HTTP_LINE_MAX];
	char newurl[HTTP_LINE_MAX];
	char line[HTTP_LINE_MAX];
	char addr[LINK_ADDRESS_MAX];
	struct link *link;
	int save_errno;
	int response;
	char actual_host[HTTP_LINE_MAX];
	int actual_port;
	*size = 0;

	url_encode(urlin, url, sizeof(url));

	if(proxy && !strcmp(proxy, "DIRECT"))
		proxy = 0;

	if(proxy) {
		int fields = sscanf(proxy, "http://%[^:]:%d", actual_host, &actual_port);
		if(fields == 2) {
			/* host and port are good */
		} else if(fields == 1) {
			actual_port = HTTP_PORT;
		} else {
			debug(D_HTTP, "invalid proxy syntax: %s", proxy);
			return 0;
		}
	} else {
		int fields = sscanf(url, "http://%[^:]:%d", actual_host, &actual_port);
		if(fields != 2) {
			fields = sscanf(url, "http://%[^/]", actual_host);
			if(fields == 1) {
				actual_port = HTTP_PORT;
			} else {
				debug(D_HTTP, "malformed url: %s", url);
				return 0;
			}
		}
	}

	debug(D_HTTP, "connect %s port %d", actual_host, actual_port);
	if(!domain_name_lookup(actual_host, addr))
		return 0;

	link = link_connect(addr, actual_port, stoptime);
	if(!link) {
		errno = ECONNRESET;
		return 0;
	}


	{
		buffer_t B;

		buffer_init(&B);
		buffer_abortonfailure(&B, 1);

		buffer_printf(&B, "%s %s HTTP/1.1\r\n", action, url);
		if(cache_reload)
			buffer_putliteral(&B, "Cache-Control: max-age=0\r\n");
		buffer_putliteral(&B, "Connection: close\r\n");
		buffer_printf(&B, "Host: %s\r\n", actual_host);
		if(getenv("HTTP_USER_AGENT"))
			buffer_printf(&B, "User-Agent: Mozilla/5.0 (compatible; CCTools %d.%d.%s Parrot; http://www.nd.edu/~ccl/ %s)\r\n", CCTOOLS_VERSION_MAJOR, CCTOOLS_VERSION_MINOR, CCTOOLS_VERSION_MICRO, getenv("HTTP_USER_AGENT"));
		else
			buffer_printf(&B, "User-Agent: Mozilla/5.0 (compatible; CCTools %d.%d.%s Parrot; http://www.nd.edu/~ccl/)\r\n", CCTOOLS_VERSION_MAJOR, CCTOOLS_VERSION_MINOR, CCTOOLS_VERSION_MICRO);
		buffer_putliteral(&B, "\r\n"); /* header terminator */

		debug(D_HTTP, "%s", buffer_tostring(&B, NULL));
		link_putstring(link, buffer_tostring(&B, NULL), stoptime);

		buffer_free(&B);
	}

	if(link_readline(link, line, HTTP_LINE_MAX, stoptime)) {
		string_chomp(line);
		debug(D_HTTP, "%s", line);
		if(sscanf(line, "HTTP/%*d.%*d %d", &response) == 1) {
			newurl[0] = 0;
			while(link_readline(link, line, HTTP_LINE_MAX, stoptime)) {
				string_chomp(line);
				debug(D_HTTP, "%s", line);
				sscanf(line, "Location: %s", newurl);
				sscanf(line, "Content-Length: %" SCNd64, size);
				if(strlen(line) <= 2) {
					break;
				}
			}

			switch (response) {
			case 200:
				return link;
				break;
			case 301:
			case 302:
			case 303:
			case 307:
				link_close(link);
				if(newurl[0]) {
					if(!strcmp(url, newurl)) {
						debug(D_HTTP, "error: server gave %d redirect from %s back to the same url!", response, url);
						errno = EIO;
						return 0;
					} else {
						return http_query_size_via_proxy(proxy,newurl,action,size,stoptime,cache_reload);
					}
				} else {
					errno = ENOENT;
					return 0;
				}
				break;
			default:
				link_close(link);
				errno = http_response_to_errno(response);
				return 0;
				break;
			}
		} else {
			debug(D_HTTP, "malformed response");
			save_errno = ECONNRESET;
		}
	} else {
		debug(D_HTTP, "malformed response");
		save_errno = ECONNRESET;
	}

	link_close(link);
	errno = save_errno;
	return 0;
}