Commit a974f0c1 authored by Benjamin Gray's avatar Benjamin Gray Committed by Michael Ellerman
Browse files

selftests/powerpc: Add generic read/write file util



File read/write is reimplemented in about 5 different ways in the
various PowerPC selftests. This indicates it should be a common util.

Add a common read_file / write_file implementation and convert users
to it where (easily) possible.

Signed-off-by: default avatarBenjamin Gray <bgray@linux.ibm.com>
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20230203003947.38033-2-bgray@linux.ibm.com
parent b5050639
Loading
Loading
Loading
Loading
+8 −25
Original line number Diff line number Diff line
@@ -64,48 +64,31 @@ inline void set_dscr_usr(unsigned long val)
/* Default DSCR access */
unsigned long get_default_dscr(void)
{
	int fd = -1, ret;
	char buf[16];
	int err;
	char buf[16] = {0};
	unsigned long val;

	if (fd == -1) {
		fd = open(DSCR_DEFAULT, O_RDONLY);
		if (fd == -1) {
			perror("open() failed");
			exit(1);
		}
	}
	memset(buf, 0, sizeof(buf));
	lseek(fd, 0, SEEK_SET);
	ret = read(fd, buf, sizeof(buf));
	if (ret == -1) {
	err = read_file(DSCR_DEFAULT, buf, sizeof(buf) - 1, NULL);
	if (err) {
		perror("read() failed");
		exit(1);
	}
	sscanf(buf, "%lx", &val);
	close(fd);
	return val;
}

void set_default_dscr(unsigned long val)
{
	int fd = -1, ret;
	int err;
	char buf[16];

	if (fd == -1) {
		fd = open(DSCR_DEFAULT, O_RDWR);
		if (fd == -1) {
			perror("open() failed");
			exit(1);
		}
	}
	sprintf(buf, "%lx\n", val);
	ret = write(fd, buf, strlen(buf));
	if (ret == -1) {

	err = write_file(DSCR_DEFAULT, buf, strlen(buf));
	if (err) {
		perror("write() failed");
		exit(1);
	}
	close(fd);
}

double uniform_deviate(int seed)
+5 −16
Original line number Diff line number Diff line
@@ -12,24 +12,13 @@

static int check_cpu_dscr_default(char *file, unsigned long val)
{
	char buf[10];
	int fd, rc;
	char buf[10] = {0};
	int rc;

	fd = open(file, O_RDWR);
	if (fd == -1) {
		perror("open() failed");
		return 1;
	}

	rc = read(fd, buf, sizeof(buf));
	if (rc == -1) {
		perror("read() failed");
		close(fd);
		return 1;
	}
	close(fd);
	rc = read_file(file, buf, sizeof(buf) - 1, NULL);
	if (rc)
		return rc;

	buf[rc] = '\0';
	if (strtol(buf, NULL, 16) != val) {
		printf("DSCR match failed: %ld (system) %ld (cpu)\n",
					val, strtol(buf, NULL, 16));
+2 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ void *get_auxv_entry(int type);

int pick_online_cpu(void);

int read_file(const char *path, char *buf, size_t count, size_t *len);
int write_file(const char *path, const char *buf, size_t count);
int read_debugfs_file(char *debugfs_file, int *result);
int write_debugfs_file(char *debugfs_file, int result);
int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
+18 −30
Original line number Diff line number Diff line
@@ -146,49 +146,37 @@ int gzip_header_blank(char *buf)
/* Caller must free the allocated buffer return nonzero on error. */
int read_alloc_input_file(char *fname, char **buf, size_t *bufsize)
{
	int err;
	struct stat statbuf;
	FILE *fp;
	char *p;
	size_t num_bytes;

	if (stat(fname, &statbuf)) {
		perror(fname);
		return(-1);
	}
	fp = fopen(fname, "r");
	if (fp == NULL) {
		perror(fname);
		return(-1);
		return -1;
	}

	assert(NULL != (p = (char *) malloc(statbuf.st_size)));
	num_bytes = fread(p, 1, statbuf.st_size, fp);
	if (ferror(fp) || (num_bytes != statbuf.st_size)) {

	err = read_file(fname, p, statbuf.st_size, &num_bytes);
	if (err) {
		perror(fname);
		return(-1);
		goto fail;
	}

	if (num_bytes != statbuf.st_size) {
		fprintf(stderr, "Actual bytes != expected bytes\n");
		err = -1;
		goto fail;
	}

	*buf = p;
	*bufsize = num_bytes;
	return 0;
}

/* Returns nonzero on error */
int write_output_file(char *fname, char *buf, size_t bufsize)
{
	FILE *fp;
	size_t num_bytes;

	fp = fopen(fname, "w");
	if (fp == NULL) {
		perror(fname);
		return(-1);
	}
	num_bytes = fwrite(buf, 1, bufsize, fp);
	if (ferror(fp) || (num_bytes != bufsize)) {
		perror(fname);
		return(-1);
	}
	fclose(fp);
	return 0;
fail:
	free(p);
	return err;
}

/*
@@ -399,7 +387,7 @@ int compress_file(int argc, char **argv, void *handle)
	assert(FNAME_MAX > (strlen(argv[1]) + strlen(FEXT)));
	strcpy(outname, argv[1]);
	strcat(outname, FEXT);
	if (write_output_file(outname, outbuf, dsttotlen)) {
	if (write_file(outname, outbuf, dsttotlen)) {
		fprintf(stderr, "write error: %s\n", outname);
		exit(-1);
	}
+8 −23
Original line number Diff line number Diff line
@@ -190,38 +190,23 @@ int parse_proc_maps(void)

bool require_paranoia_below(int level)
{
	int err;
	long current;
	char *end, buf[16];
	FILE *f;
	bool rc;

	rc = false;

	f = fopen(PARANOID_PATH, "r");
	if (!f) {
		perror("fopen");
		goto out;
	}
	char *end;
	char buf[16] = {0};

	if (!fgets(buf, sizeof(buf), f)) {
	err = read_file(PARANOID_PATH, buf, sizeof(buf) - 1, NULL);
	if (err) {
		printf("Couldn't read " PARANOID_PATH "?\n");
		goto out_close;
		return false;
	}

	current = strtol(buf, &end, 10);

	if (end == buf) {
		printf("Couldn't parse " PARANOID_PATH "?\n");
		goto out_close;
		return false;
	}

	if (current >= level)
		goto out_close;

	rc = true;
out_close:
	fclose(f);
out:
	return rc;
	return current < level;
}
Loading