From: Michael Neuling <mikey@neuling.org>
To: horms@verge.net.au
Cc: linuxppc-dev@ozlabs.org, kexec@lists.infradead.org
Subject: [PATCH] kexec ppc64: cleanup get_devtree_details
Date: Tue, 01 May 2007 13:47:14 +1000 [thread overview]
Message-ID: <10869.1177991234@neuling.org> (raw)
Cleanup error paths in get_devtree_details. Also convert fstat from a
pointer to avoid malloc.
Signed-off-by: Michael Neuling <mikey@neuling.org>
---
It's error paths, so if I screwed something up, I won't have picked it
up with the simple testing I did.
kexec/arch/ppc64/kexec-ppc64.c | 136 ++++++++++++-----------------------------
1 file changed, 40 insertions(+), 96 deletions(-)
Index: kexec-tools-testing/kexec/arch/ppc64/kexec-ppc64.c
===================================================================
--- kexec-tools-testing.orig/kexec/arch/ppc64/kexec-ppc64.c
+++ kexec-tools-testing/kexec/arch/ppc64/kexec-ppc64.c
@@ -265,7 +265,7 @@ static int get_devtree_details(unsigned
DIR *dir, *cdir;
FILE *file;
struct dirent *dentry;
- struct stat *fstat = malloc(sizeof(struct stat));
+ struct stat fstat;
int n, i = 0;
if ((dir = opendir(device_tree)) == NULL) {
@@ -283,24 +283,18 @@ static int get_devtree_details(unsigned
strcat(fname, dentry->d_name);
if ((cdir = opendir(fname)) == NULL) {
perror(fname);
- closedir(dir);
- return -1;
+ goto error_opendir;
}
if (strncmp(dentry->d_name, "chosen", 6) == 0) {
strcat(fname, "/linux,kernel-end");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
if (fread(&kernel_end, sizeof(unsigned long), 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
fclose(file);
@@ -316,17 +310,12 @@ static int get_devtree_details(unsigned
strcat(fname, "/linux,crashkernel-base");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
if (fread(&crash_base, sizeof(unsigned long), 1,
file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
fclose(file);
@@ -336,17 +325,12 @@ static int get_devtree_details(unsigned
strcat(fname, "/linux,crashkernel-size");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
if (fread(&crash_size, sizeof(unsigned long), 1,
file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
if (crash_base > mem_min)
@@ -370,15 +354,11 @@ static int get_devtree_details(unsigned
continue;
}
perror(fname);
- closedir(dir);
- return -1;
+ goto error_opendir;
}
if (fread(&htab_base, sizeof(unsigned long), 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
memset(fname, 0, sizeof(fname));
strcpy(fname, device_tree);
@@ -386,16 +366,11 @@ static int get_devtree_details(unsigned
strcat(fname, "/linux,htab-size");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
if (fread(&htab_size, sizeof(unsigned long), 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
/* Add htab address to exclude_range - NON-LPAR only */
exclude_range[i].start = htab_base;
@@ -410,24 +385,16 @@ static int get_devtree_details(unsigned
strcat(fname, "/linux,initrd-start");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
/* check for 4 and 8 byte initrd offset sizes */
- if (stat(fname, fstat) != 0) {
+ if (stat(fname, &fstat) != 0) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
- if (fread(&initrd_start, fstat->st_size, 1, file) != 1) {
+ if (fread(&initrd_start, fstat.st_size, 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
fclose(file);
@@ -437,24 +404,16 @@ static int get_devtree_details(unsigned
strcat(fname, "/linux,initrd-end");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
/* check for 4 and 8 byte initrd offset sizes */
- if (stat(fname, fstat) != 0) {
+ if (stat(fname, &fstat) != 0) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
- if (fread(&initrd_end, fstat->st_size, 1, file) != 1) {
+ if (fread(&initrd_end, fstat.st_size, 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
fclose(file);
@@ -469,16 +428,11 @@ static int get_devtree_details(unsigned
strcat(fname, "/linux,rtas-base");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
if (fread(&rtas_base, sizeof(unsigned int), 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
memset(fname, 0, sizeof(fname));
strcpy(fname, device_tree);
@@ -486,16 +440,11 @@ static int get_devtree_details(unsigned
strcat(fname, "/rtas-size");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
if (fread(&rtas_size, sizeof(unsigned int), 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
closedir(cdir);
/* Add rtas to exclude_range */
@@ -510,16 +459,11 @@ static int get_devtree_details(unsigned
strcat(fname, "/reg");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
if ((n = fread(buf, 1, MAXBYTES, file)) < 0) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
rmo_base = ((unsigned long long *)buf)[0];
rmo_top = rmo_base + ((unsigned long long *)buf)[1];
@@ -540,14 +484,11 @@ static int get_devtree_details(unsigned
continue;
}
perror(fname);
- closedir(dir);
- return -1;
+ goto error_opendir;
}
if (fread(&tce_base, sizeof(unsigned long), 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
+ goto error_openfile;
return -1;
}
memset(fname, 0, sizeof(fname));
@@ -556,16 +497,11 @@ static int get_devtree_details(unsigned
strcat(fname, "/linux,tce-size");
if ((file = fopen(fname, "r")) == NULL) {
perror(fname);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_opencdir;
}
if (fread(&tce_size, sizeof(unsigned int), 1, file) != 1) {
perror(fname);
- fclose(file);
- closedir(cdir);
- closedir(dir);
- return -1;
+ goto error_openfile;
}
/* Add tce to exclude_range - NON-LPAR only */
exclude_range[i].start = tce_base;
@@ -590,6 +526,14 @@ static int get_devtree_details(unsigned
exclude_range[k].end);
#endif
return 0;
+
+error_openfile:
+ fclose(file);
+error_opencdir:
+ closedir(cdir);
+error_opendir:
+ closedir(dir);
+ return -1;
}
/* Setup a sorted list of memory ranges. */
next reply other threads:[~2007-05-01 3:47 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-05-01 3:47 Michael Neuling [this message]
2007-05-01 5:54 ` [PATCH] kexec ppc64: cleanup get_devtree_details Simon Horman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=10869.1177991234@neuling.org \
--to=mikey@neuling.org \
--cc=horms@verge.net.au \
--cc=kexec@lists.infradead.org \
--cc=linuxppc-dev@ozlabs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).