Example #1
0
/**
 * Parse /proc/%d/cmdline file
 * @param filepath Path to file ~ use preallocated buffer for the path
 * @param buffer output buffer with non-zero size
 * @return ps-like command info or NULL
 */
static inline bool get_process_cmdline(const char* filepath, struct oscap_buffer* const buffer){

	int fd = open(filepath, O_RDONLY, 0);

	if (fd < 0) {
		return false;
	}

	oscap_buffer_clear(buffer);



	for(;;) {
		static const int chunk_size = 1024;
		char chunk[chunk_size];
		// Read data, store to buffer
		ssize_t read_size = read(fd, chunk, chunk_size );
		if (read_size < 0) {
			close(fd);
			return false;
		}
		oscap_buffer_append_binary_data(buffer, chunk, read_size);

		// If reach end of file, then end the loop
		if (chunk_size != read_size) {
			break;
		}
	}

	close(fd);

	int length = oscap_buffer_get_length(buffer);
	char* buffer_mem = oscap_buffer_get_raw(buffer);

	if ( length == 0 ) { // empty file
		return false;
	} else {

		// Skip multiple trailing zeros
		int i = length - 1;
		while ( (i > 0) && (buffer_mem[i] == '\0') ) {
			--i;
		}

		// Program and args are separated by '\0'
		// Replace them with spaces ' '
		while( i >= 0 ){
			char chr = buffer_mem[i];
			if ( ( chr == '\0') || ( chr == '\n' ) ) {
				buffer_mem[i] = ' ';
			} else if ( !isprint(chr) ) { // "ps" replace non-printable characters with '.' (LC_ALL=C)
				buffer_mem[i] = '.';
			}
			--i;
		}
	}
	return true;
}
Example #2
0
int test_buffer_clear()
{
	struct oscap_buffer *s = oscap_buffer_new();
	ASSERT_LENGTH(s, 0);

	oscap_buffer_clear(s);
	ASSERT_LENGTH(s, 0);

	char data = 'A';
	oscap_buffer_append_binary_data(s, &data, 1);
	ASSERT_LENGTH(s, 1);

	oscap_buffer_clear(s);
	ASSERT_LENGTH(s, 0);

	oscap_buffer_free(s);
	return 0;
}